Exemplo n.º 1
0
        private static SymbolicTransform GenerateOneTimesXToXTransform()
        {
            var constraint = new BasicSymbolicConstraint(
                s => s.Type == SymbolType.Multiplication,
                new BasicSymbolicConstraint[] {
                new BasicSymbolicConstraint(
                    s => s.Type == SymbolType.Number && (Number)s == Numbers.One),
                new BasicSymbolicConstraint()
            }
                );
            var varName  = "value";
            var inputMap = new SymbolMap(
                new SymbolMap[]
            {
                new SymbolMap(),     // placeholder for left part
                new SymbolMap(new SymbolName(varName))
            }
                );
            var context = inputMap.GenerateContext();

            var outputExpression = new Variable(varName);
            var outputFunction   = new Function(outputExpression, context);

            return(SymbolicTransform.New(constraint, inputMap, outputFunction));
        }
Exemplo n.º 2
0
        private static SymbolicTransform GenerateAdditionForceValuesLeftTransform()
        {
            // constraint where value is on the right, and
            // some non-left subtree is on the left
            var constraint = new BasicSymbolicConstraint(
                s => s.Type == SymbolType.Addition,
                new BasicSymbolicConstraint[] {
                new BasicSymbolicConstraint(
                    s => !s.Type.IsValue()
                    ),
                new BasicSymbolicConstraint(
                    s => s.Type.IsValue()
                    )
            }
                );

            var leftName  = "left";
            var rightName = "right";
            var inputMap  = new SymbolMap(new SymbolMap[]
            {
                new SymbolMap(new SymbolName(leftName)),
                new SymbolMap(new SymbolName(rightName))
            });
            var context = inputMap.GenerateContext();

            var outputExpression = new Addition(
                new Variable(rightName),
                new Variable(leftName)
                );
            var outputFunction = new Function(outputExpression, context);

            return(SymbolicTransform.New(constraint, inputMap, outputFunction));
        }
Exemplo n.º 3
0
 private SymbolicTransform(
     BasicSymbolicConstraint constraint,
     SymbolicDeconstructor deconstructor,
     Function outputFunction)
 {
     _constraint    = constraint;
     _deconstructor = deconstructor;
     _output        = outputFunction;
 }
Exemplo n.º 4
0
        public bool Matches(BasicSymbolicConstraint constraint)
        {
            if (!constraint.BaseNodeIsValid(this))
            {
                return(false);
            }

            // No children, there better be no child constraints :D
            return(constraint.ChildConstraints.Length == 0);
        }
Exemplo n.º 5
0
        public static void ValuesWithoutTwoChildrenFailOnTwoChildConstraints()
        {
            var constraint = new BasicSymbolicConstraint(s => { return(true); }, new BasicSymbolicConstraint[] {
                SymbolicConstraints.Any, SymbolicConstraints.Any
            });
            var fails1 = Numbers.One;
            var fails2 = Operations.BasicNegation;
            var passes = Operations.BasicAddition;

            Assert.True(passes.Matches(constraint));
            Assert.False(fails1.Matches(constraint));
            Assert.False(fails2.Matches(constraint));
        }
Exemplo n.º 6
0
        public bool Matches(BasicSymbolicConstraint constraint)
        {
            if (!constraint.BaseNodeIsValid(this))
            {
                return(false);
            }

            if (constraint.Left != null && !Child.Matches(constraint.Left))
            {
                return(false);
            }

            // If for some reason this was seeking a binary+ operation let's not match
            return(constraint.ChildConstraints.Length <= 1);
        }
Exemplo n.º 7
0
        public static SymbolicTransform New(
            BasicSymbolicConstraint constraint,
            SymbolMap inputMap,
            Function outputFunction)
        {
            var inputContext = inputMap.GenerateContext();

            if (!outputFunction.ContainsVariables(inputContext))
            {
                throw new InvalidTransformException(
                          $"'{nameof(inputMap)}' contains variable names that do not exist in '{nameof(outputFunction)}'");
            }

            var deconstructor = new SymbolicDeconstructor(inputMap);

            return(new SymbolicTransform(constraint, deconstructor, outputFunction));
        }
Exemplo n.º 8
0
        public static void CanFailOnChildConstraints()
        {
            var constraint = new BasicSymbolicConstraint(
                s => { return(true); },
                new BasicSymbolicConstraint[]
            {
                SymbolicConstraints.Any,
                SymbolicConstraints.ReducibleConstant
            }
                );

            var passing1 = new Addition(Numbers.X, Numbers.One);
            var passing2 = Operations.BasicAddition;
            var failing1 = new Addition(Numbers.One, Numbers.X);
            var failing2 = Numbers.One;
            var failing3 = Operations.BasicSine;

            Assert.True(passing1.Matches(constraint));
            Assert.True(passing2.Matches(constraint));
            Assert.False(failing1.Matches(constraint));
            Assert.False(failing2.Matches(constraint));
            Assert.False(failing3.Matches(constraint));
        }