public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // obtain the 'bound' property via the Row string indexer
            string index         = parameter as string;
            object propertyValue = null;
            Record record        = value as Record;

            if (record != null)
            {
                propertyValue = record[index];
            }
            else
            {
                FieldSetter propertyValueChange = value as FieldSetter;
                if (propertyValueChange != null)
                {
                    propertyValue = propertyValueChange.Value;
                }
            }

            // convert if required
            if (_valueConverter != null)
            {
                propertyValue = _valueConverter.Convert(propertyValue, targetType, parameter, culture);
            }

            return(propertyValue);
        }
Example #2
0
        public void CanAssignFields()
        {
            var row = new RowValuesMock("a", "b", "c", "d");

            // The empty assignment must not change any values:
            row.SetValues("foo", 10, 2.4, DBNull.Value);
            FieldSetter.Create(string.Empty).Execute(row);
            row.AssertValues("foo", 10, 2.4, DBNull.Value);

            // This assignment must change all values:
            FieldSetter.Create("a = 'bar'; b = 11; c = 3.5; d = d ?? 'def'")
            .DefineFields(row)
            .Execute(row);
            row.AssertValues("bar", 11, 3.5, "def");

            // A more realistic example:
            row.SetValues(DBNull.Value, 11, 3.5, "hi");
            FieldSetter.Create("a=CONCAT(b+marker.c); b=10+10*RAND(4); c=TRUNC(1.5+b/2); d=null")
            .DefineFields(row, "marker")
            .SetRandomSeed(1234)
            .Execute(row);
            row.AssertValues("14.5", 20, 7, DBNull.Value);

            // On reading, DBNull shall be mapped to null,
            // on writing, null shall be mapped to DBNull:
            row.SetValues(DBNull.Value, null, "hi", "there");
            FieldSetter.Create("a=a??'null'; b=b??'null'; c=null; d=NULL")
            .DefineFields(row)
            .Execute(row);
            row.AssertValues("null", "null", DBNull.Value, DBNull.Value);
        }
Example #3
0
 public FieldSymbol(string name, TypeProxy type, FieldGetter getter = null, FieldSetter setter = null, FieldRef getref = null)
     : base(name, type)
 {
     this.getter = getter;
     this.setter = setter;
     this.getref = getref;
 }
Example #4
0
        public static FieldSetter ValidateTargetFields(
            this FieldSetter instance, FeatureClass featureClass, string parameterName)
        {
            if (instance == null)
            {
                return(null);
            }
            if (featureClass == null)
            {
                return(instance);
            }

            try
            {
                var fieldNames = featureClass.GetDefinition().GetFields().Select(f => f.Name);
                instance.ValidateTargetFields(fieldNames);
            }
            catch (Exception ex)
            {
                throw new InvalidConfigurationException(
                          $"Parameter {parameterName} is invalid: {ex.Message}");
            }

            return(instance);
        }
Example #5
0
        public void CanEmptyAndNull()
        {
            // An empty (or null) assignments string must not choke the parser
            // and it must not change any fields on Execute. This is important
            // because null/empty is probably the default value for the field
            // assignments parameter in all Carto Processes that use it.

            var row        = new RowValuesMock("foo", "bar", "baz", "quux");
            var fieldNames = row.FieldNames;

            var fsEmpty = FieldSetter.Create(string.Empty);

            Assert.NotNull(fsEmpty);
            row.SetValues(1.2, "2.3", DBNull.Value, null);
            fsEmpty.Execute(row);
            row.AssertValues(1.2, "2.3", DBNull.Value, null);

            var fsNull = FieldSetter.Create(null);

            Assert.NotNull(fsNull);
            row.SetValues(1.2, "2.3", DBNull.Value, null);
            fsEmpty.Execute(row);
            row.AssertValues(1.2, "2.3", DBNull.Value, null);

            // Target field validation must also succeed:
            fsEmpty.ValidateTargetFields(fieldNames);
            fsNull.ValidateTargetFields(fieldNames);
        }
        public void FieldSetter2Works()
        {
            var foo = new Foo();

            var setter = new FieldSetter <Foo, int>(_field);

            setter.Action.Target.Should().BeSameAs(setter);
            setter.Action.Method.Name.Should().Be(nameof(setter.SetValueReflection));
            setter.Action.Method.DeclaringType.Should().Be(typeof(FieldSetter <Foo, int>));

            using (var gc = new GCNoRegion(4194304))
            {
                var reflectionTimer = Stopwatch.StartNew();
                setter.SetValue(foo, 123);
                reflectionTimer.Stop();

                foo.Bar.Should().Be(123);

                setter.SetOptimizedAction();

                setter.Action.Target.Should().NotBeSameAs(setter);
                setter.Action.Method.Name.Should().Be(FieldSetter.SetValueOptimized);
                setter.Action.Method.DeclaringType.Should().NotBe(typeof(FieldSetter <Foo, int>));

                var optimizedTimer = Stopwatch.StartNew();
                setter.SetValue(foo, 456);
                optimizedTimer.Stop();

                foo.Bar.Should().Be(456);

                optimizedTimer.Elapsed.Should().BeLessThan(reflectionTimer.Elapsed);
            }
        }
Example #7
0
        public void CanReset()
        {
            var row = new RowValuesMock("a", "b", "c", "d");

            var another = new RowValuesMock("foo", "bar", "nix");

            another.SetValues("One", "Two", "Three");

            // Explicitly defined values override field values:

            var fs1 = FieldSetter.Create("a=foo; b=bar; c=nix")
                      .DefineValue("foo", "Foo")
                      .DefineValue("bar", "Bar")
                      .DefineValue("nix", null)
                      .DefineFields(another);

            fs1.Execute(row);
            row.AssertValues("Foo", "Bar", DBNull.Value, null);

            fs1.ForgetAll().DefineFields(another);
            fs1.Execute(row);
            row.AssertValues("One", "Two", "Three", null);

            fs1.ForgetAll();
            var ex1 = Assert.Catch <EvaluationException>(() => fs1.Execute(row));            // no such field

            Console.WriteLine(@"Expected exception: {0}", ex1.Message);
        }
Example #8
0
        public void CanLookupPrecedence()
        {
            // Precedence: defined value < standard function < field value
            // CONCAT is a standard function.

            var row = new RowValuesMock("a", "b");

            var other = new RowValuesMock("CONCAT");

            other.SetValues("Field value");

            var fs = FieldSetter.Create("a=CONCAT; b = other.CONCAT");

            fs.DefineValue("CONCAT", "Defined value");
            fs.DefineFields(other, "other");

            fs.Execute(row);
            // Defined value takes precedence, but qualified always refers to field:
            row.AssertValues("Defined value", "Field value");

            fs.DefineValue("CONCAT", null);
            fs.Execute(row);
            // Defined value takes precedence, even if it is null:
            row.AssertValues(DBNull.Value, "Field value");

            fs.ForgetAll();
            fs.DefineFields(other, "other");
            fs.Execute(row);
            row.AssertValues(new Function("CONCAT"), "Field value");
        }
Example #9
0
 public static FieldSetter DefineFields(
     this FieldSetter instance, RowBuffer row, string qualifier = null)
 {
     if (instance == null)
     {
         return(null);
     }
     return(instance.DefineFields(row.RowValues(), qualifier));
 }
Example #10
0
 public static void Execute(
     this FieldSetter instance, RowBuffer row, IEvaluationEnvironment env = null)
 {
     if (instance == null)
     {
         throw new ArgumentNullException(nameof(instance));
     }
     instance.Execute(row.RowValues(), env);
 }
Example #11
0
 public CreateAnnoMasksEngine(CreateAnnoMasks config, IProcessingContext context,
                              IProcessingFeedback feedback)
     : base(config.Name, context, feedback)
 {
     _inputDataset      = OpenRequiredDataset(config.InputDataset, nameof(config.InputDataset));
     _outputMaskDataset = OpenRequiredDataset(config.OutputMaskDataset, nameof(config.OutputMaskDataset));
     _relationshipClass = OpenAssociation(config.OutputAssociation);
     _maskAttributes    = ProcessingUtils.CreateFieldSetter(
         config.MaskAttributes, _outputMaskDataset.FeatureClass, nameof(config.MaskAttributes));
     _maskMargin                = ImplicitValue.Create(config.MaskMargin, nameof(config.MaskMargin));
     _maskMargin.Environment    = new StandardEnvironment().RegisterConversionFunctions();
     _simplificationToleranceMu = config.SimplificationTolerance;                 // TODO convert mm to mu
     _maskOutlineType           = config.MaskOutlineType;
     _fillHoles = config.FillHoles;
 }
Example #12
0
            public AlignMarkersEngine(AlignMarkers config, IProcessingContext context, IProcessingFeedback feedback)
                : base(config.Name, context, feedback)
            {
                _inputDataset =
                    OpenRequiredDataset(config.InputDataset, nameof(config.InputDataset));

                _referenceDatasets = OpenDatasets(config.ReferenceDatasets);

                _searchDistance = ProcessingUtils.Clip(
                    config.SearchDistance, 0, double.MaxValue,
                    nameof(config.SearchDistance));

                _markerFieldSetter = ProcessingUtils.CreateFieldSetter(
                    config.MarkerAttributes, _inputDataset.FeatureClass,
                    nameof(config.MarkerAttributes));
            }
Example #13
0
        public void CanReferenceManualBindings()
        {
            var row = new RowValuesMock("a", "b", "c", "d");

            var fs1 = FieldSetter.Create("a=foo; b=bar; c=pi; d=CONCAT(pi)")
                      .DefineValue("foo", "Foo")
                      .DefineValue("bar", "Bar")
                      .DefineValue("pi", 3.14159);

            fs1.Execute(row);
            row.AssertValues("Foo", "Bar", 3.14159, "3.14159");

            // Manual bindings override standard functions:
            fs1.DefineValue("CONCAT", 123);
            var ex1 = Assert.Catch <EvaluationException>(() => fs1.Execute(row));            // not a function

            Console.WriteLine(@"Expected exception: {0}", ex1.Message);
        }
Example #14
0
 public ValueAssigner(
     string?fieldName,
     FieldSetter <TResult, TField> setter,
     Func <long, TField>?convertInteger,
     Func <double, TField>?convertFloat,
     FromSpan <char, TField>?convertTextUtf16,
     FromSpan <byte, TField>?convertTextUtf8,
     FromSpan <byte, TField>?convertBlob,
     Func <TField>?convertNull)
 {
     _fieldName        = fieldName;
     _setter           = setter;
     _convertInteger   = convertInteger;
     _convertFloat     = convertFloat;
     _convertTextUtf16 = convertTextUtf16;
     _convertTextUtf8  = convertTextUtf8;
     _convertBlob      = convertBlob;
     _convertNull      = convertNull;
 }
Example #15
0
        public void CanParseFieldNames()
        {
            // Field names: 1 Letter or Underscore followed by 0..N Letters, Digits, Underscores

            FieldSetter.Create("foo = 'bar'");
            FieldSetter.Create("RuleID_1 = 5");
            FieldSetter.Create("_a = 6");
            FieldSetter.Create("_7_ = 7");

            Exception ex1 = Assert.Catch <FormatException>(() => FieldSetter.Create("123 = 'oops'"));

            Console.WriteLine(@"Expected error: {0}", ex1.Message);
            Exception ex2 = Assert.Catch <FormatException>(() => FieldSetter.Create("7X = 77"));

            Console.WriteLine(@"Expected error: {0}", ex2.Message);
            Exception ex3 = Assert.Catch <FormatException>(() => FieldSetter.Create("foo bar"));

            Console.WriteLine(@"Expected error: {0}", ex3.Message);
        }
Example #16
0
        public void CanParseAssignments()
        {
            // Make sure the empty string works!
            var fs0 = FieldSetter.Create(string.Empty);

            Assert.AreEqual(string.Empty, ToString(fs0.GetAssignments()));

            // Assignment op is either "=" or ":=" and trailing ";" is optional:
            var fs1 = FieldSetter.Create("a=a+a; b:=a*a;");

            Assert.AreEqual("a=a+a;b=a*a", ToString(fs1.GetAssignments()));

            // White space between tokens shall be ignored:
            var fs2 = FieldSetter.Create("  a  =  'b'  ;  c  =  'd'  ;  ");

            Assert.AreEqual("a='b';c='d'", ToString(fs2.GetAssignments()));

            var fs3 = FieldSetter.Create(" \t size=RAND(5,9);angle=RAND()*360.0   ;   foo = marker.size/2  ");

            Assert.AreEqual("size=RAND(5,9);angle=RAND()*360.0;foo=marker.size/2", ToString(fs3.GetAssignments()));
        }
Example #17
0
        public void CanValidateTargetFields()
        {
            var fields = new RowValuesMock("a", "b", "c").FieldNames;

            // All existing target fields:
            var good = FieldSetter.Create("a=3;b=5;c=7");

            good.ValidateTargetFields(fields);

            // Invalid target field:
            var bad = FieldSetter.Create("a=3;b=5;c=7;d=a+b+c");
            var ex1 = Assert.Catch <Exception>(() => bad.ValidateTargetFields(fields));

            Console.WriteLine(@"Expected exception: {0}", ex1.Message);

            // Multiple invalid target fields:
            var oops = FieldSetter.Create("a=3;b=5;c=6;foo=7;bar=8");
            var ex2  = Assert.Catch <Exception>(() => oops.ValidateTargetFields(fields));

            Console.WriteLine(@"Expected exception: {0}", ex2.Message);
        }
Example #18
0
        public void CanReferenceMultipleRows()
        {
            var one = new RowValuesMock("a", "b", "c");
            var two = new RowValuesMock("c", "d");

            one.SetValues("1", "2", "3");
            two.SetValues("III", "IV");

            var row = new RowValuesMock("a", "b", "x");

            row.SetValues(DBNull.Value, DBNull.Value, DBNull.Value);

            var fs1 = FieldSetter.Create("a=one.A; b=one.B; x=x??CONCAT(one.C, two.C, D)")
                      .DefineFields(one, "one")
                      .DefineFields(two, "two")
                      .DefineFields(row);

            fs1.Execute(row);
            row.AssertValues("1", "2", "3IIIIV");

            fs1.ForgetAll();             // forget all previous definitions
            var ex1 = Assert.Catch <EvaluationException>(() => fs1.Execute(row));

            Console.WriteLine(@"Expected exception: {0}", ex1.Message);

            var fs2 = FieldSetter.Create("a=one.D").DefineFields(one, "one");
            var ex2 = Assert.Catch <EvaluationException>(() => fs2.Execute(row));            // No such field: one.D

            Console.WriteLine(@"Expected exception: {0}", ex2.Message);

            var fs3 = FieldSetter.Create("a=A").DefineFields(one, "one")
                      .DefineFields(two, "two").DefineFields(row);
            var ex3 = Assert.Catch <EvaluationException>(() => fs3.Execute(row));            // Field name not unique

            Console.WriteLine(@"Expected exception: {0}", ex3.Message);
        }
Example #19
0
 protected void AddFieldConfig(FieldSetter Setter, params string[] FieldNames)
 {
     FieldsConfig.Add(FieldNames, Setter);
 }
Example #20
0
 void OnEnable()
 {
     script = (FieldSetter)target;
 }