예제 #1
0
        public void CursorChannelExposedInMapTransform()
        {
            var env = new MLContext(seed: 0);
            // Correct use of CursorChannel attribute.
            var data1 = Utils.CreateArray(10, new OneIChannelWithAttribute());
            var idv1 = env.CreateDataView(data1);
            Assert.Null(data1[0].Channel);

            var filter1 = LambdaTransform.CreateFilter<OneIChannelWithAttribute, object>(env, idv1,
                (input, state) =>
                {
                    Assert.NotNull(input.Channel);
                    return false;
                }, null);
            filter1.GetRowCursorForAllColumns().MoveNext();

            // Error case: non-IChannel field marked with attribute.
            var data2 = Utils.CreateArray(10, new OneStringWithAttribute());
            var idv2 = env.CreateDataView(data2);
            Assert.Null(data2[0].Channel);

            var filter2 = LambdaTransform.CreateFilter<OneStringWithAttribute, object>(env, idv2,
                (input, state) =>
                {
                    Assert.Null(input.Channel);
                    return false;
                }, null);
            try
            {
                filter2.GetRowCursorForAllColumns().MoveNext();
                Assert.True(false, "Throw an error if attribute is applied to a field that is not an IChannel.");
            }
            catch (InvalidOperationException ex)
            {
                Assert.True(ex.IsMarked());
            }

            // Error case: multiple fields marked with attributes.
            var data3 = Utils.CreateArray(10, new TwoIChannelsWithAttributes());
            var idv3 = env.CreateDataView(data3);
            Assert.Null(data3[0].ChannelOne);
            Assert.Null(data3[2].ChannelTwo);

            var filter3 = LambdaTransform.CreateFilter<TwoIChannelsWithAttributes, object>(env, idv3,
                (input, state) =>
                {
                    Assert.Null(input.ChannelOne);
                    Assert.Null(input.ChannelTwo);
                    return false;
                }, null);
            try
            {
                filter3.GetRowCursorForAllColumns().MoveNext();
                Assert.True(false, "Throw an error if attribute is applied to a field that is not an IChannel.");
            }
            catch (InvalidOperationException ex)
            {
                Assert.True(ex.IsMarked());
            }

            // Correct case: non-marked IChannel field is not touched.
            var example4 = new TwoIChannelsOnlyOneWithAttribute();
            Assert.Null(example4.ChannelTwo);
            Assert.Null(example4.ChannelOne);
            var idv4 = env.CreateDataView(Utils.CreateArray(10, example4));

            var filter4 = LambdaTransform.CreateFilter<TwoIChannelsOnlyOneWithAttribute, object>(env, idv4,
                (input, state) =>
                {
                    Assert.Null(input.ChannelOne);
                    Assert.NotNull(input.ChannelTwo);
                    return false;
                }, null);
            filter1.GetRowCursorForAllColumns().MoveNext();
        }
예제 #2
0
        public void CursorChannelExposedInMapTransform()
        {
            using (var env = new ConsoleEnvironment(0))
            {
                // Correct use of CursorChannel attribute.
                var data1 = Utils.CreateArray(10, new OneIChannelWithAttribute());
                var idv1  = env.CreateDataView(data1);
                Assert.Null(data1[0].Channel);

                var map1 = LambdaTransform.CreateMap <OneIChannelWithAttribute, OneIChannelWithAttribute>(env, idv1,
                                                                                                          (input, output) =>
                {
                    output.OutField = input.OutField + input.OutField;
                });
                map1.GetRowCursor(col => true);

                var filter1 = LambdaTransform.CreateFilter <OneIChannelWithAttribute, object>(env, idv1,
                                                                                              (input, state) =>
                {
                    Assert.NotNull(input.Channel);
                    return(false);
                }, null);
                filter1.GetRowCursor(col => true).MoveNext();

                // Error case: non-IChannel field marked with attribute.
                var data2 = Utils.CreateArray(10, new OneStringWithAttribute());
                var idv2  = env.CreateDataView(data2);
                Assert.Null(data2[0].Channel);

                var filter2 = LambdaTransform.CreateFilter <OneStringWithAttribute, object>(env, idv2,
                                                                                            (input, state) =>
                {
                    Assert.Null(input.Channel);
                    return(false);
                }, null);
                try
                {
                    filter2.GetRowCursor(col => true).MoveNext();
                    Assert.True(false, "Throw an error if attribute is applied to a field that is not an IChannel.");
                }
                catch (InvalidOperationException ex)
                {
                    Assert.True(ex.IsMarked());
                }

                var map2 = LambdaTransform.CreateMap <OneStringWithAttribute, OneStringWithAttribute>(env, idv2,
                                                                                                      (input, output) =>
                {
                    output.OutField = input.OutField + input.OutField;
                });
                try
                {
                    map2.GetRowCursor(col => true);
                    Assert.True(false, "Throw an error if attribute is applied to a field that is not an IChannel.");
                }
                catch (InvalidOperationException ex)
                {
                    Assert.True(ex.IsMarked());
                }

                // Error case: multiple fields marked with attributes.
                var data3 = Utils.CreateArray(10, new TwoIChannelsWithAttributes());
                var idv3  = env.CreateDataView(data3);
                Assert.Null(data3[0].ChannelOne);
                Assert.Null(data3[2].ChannelTwo);

                var filter3 = LambdaTransform.CreateFilter <TwoIChannelsWithAttributes, object>(env, idv3,
                                                                                                (input, state) =>
                {
                    Assert.Null(input.ChannelOne);
                    Assert.Null(input.ChannelTwo);
                    return(false);
                }, null);
                try
                {
                    filter3.GetRowCursor(col => true).MoveNext();
                    Assert.True(false, "Throw an error if attribute is applied to a field that is not an IChannel.");
                }
                catch (InvalidOperationException ex)
                {
                    Assert.True(ex.IsMarked());
                }

                var map3 = LambdaTransform.CreateMap <TwoIChannelsWithAttributes, TwoIChannelsWithAttributes>(env, idv3,
                                                                                                              (input, output) =>
                {
                    output.OutField = input.OutField + input.OutField;
                });
                try
                {
                    map3.GetRowCursor(col => true);
                    Assert.True(false, "Throw an error if attribute is applied to a multiple fields.");
                }
                catch (InvalidOperationException ex)
                {
                    Assert.True(ex.IsMarked());
                }

                // Correct case: non-marked IChannel field is not touched.
                var example4 = new TwoIChannelsOnlyOneWithAttribute();
                Assert.Null(example4.ChannelTwo);
                Assert.Null(example4.ChannelOne);
                var idv4 = env.CreateDataView(Utils.CreateArray(10, example4));

                var map4 = LambdaTransform.CreateMap <TwoIChannelsOnlyOneWithAttribute, TwoIChannelsOnlyOneWithAttribute>(env, idv4,
                                                                                                                          (input, output) => { });
                map4.GetRowCursor(col => true);

                var filter4 = LambdaTransform.CreateFilter <TwoIChannelsOnlyOneWithAttribute, object>(env, idv4,
                                                                                                      (input, state) =>
                {
                    Assert.Null(input.ChannelOne);
                    Assert.NotNull(input.ChannelTwo);
                    return(false);
                }, null);
                filter1.GetRowCursor(col => true).MoveNext();
            }
        }