示例#1
0
        public void TestReify_Dict()
        {
            var x = new Var();
            var y = new Var();
            /*           
             *          s = {x: 2, y: 4}
                        e = {1: x, 3: {5: y}}
                        assert reify(e, s) == {1: 2, 3: {5: 4}}
             */
            var dict = new Dictionary<object, object>();
            dict.Add(x, 2);
            dict.Add(y, 4);

            var testDict = new Dictionary<object, object>();
            testDict.Add(1, x);
            var embedDict = new Dictionary<object, object>();
            embedDict.Add(5, y);
            testDict.Add(3, embedDict);

            var mockDict = new Dictionary<object, object>();
            mockDict.Add(1, 2);
            var mockEmbedDict = new Dictionary<object, object>();
            mockEmbedDict.Add(5, 4);
            mockDict.Add(3, mockEmbedDict);

            var obj = LogicSharp.Reify(testDict, dict) as Dictionary<object, object>;
            var test1 = obj[1];

            Assert.True(test1.Equals(mockDict[1]));

            var test2 = obj[3] as Dictionary<object, object>;
            var mocktest2 = mockDict[3] as Dictionary<object, object>;
            Assert.True(test2[5].Equals(mocktest2[5]));

        }
        public void Equation_EqGoal_Satisfy()
        {
            //2=2
            var eq = new Equation(2, 2);
            object obj;
            bool result = eq.IsEqGoal(out obj);
            Assert.False(result);

            //3=4
            eq = new Equation(3, 4);
            result = eq.IsEqGoal(out obj);
            Assert.False(result);

            //3=5-2
            var term = new Term(Expression.Add, new List<object>() { 5, -2 });
            eq = new Equation(3, term);
            result = eq.IsEqGoal(out obj);
            Assert.False(result);

            //x = x
            //TODO
            var variable = new Var('x');
            eq = new Equation(variable, variable);
            /*            result = eq.IsEqGoal(out obj);
                        Assert.True(result);*/

            //x = 2x-x
            //TODO
            term = new Term(Expression.Multiply, new List<object>() { 2, variable });
            var term0 = new Term(Expression.Multiply, new List<object>() { -1, variable });
            var term1 = new Term(Expression.Add, new List<object>() { term, term0 });
            eq = new Equation(variable, term1);
            /*            result = eq.IsEqGoal(out obj);
                        Assert.True(result);*/
        }
示例#3
0
        public void test_reify_object_dynamic()
        {
            //dynamic approach
            /*            obj = reify_object(Foo(1, var(3)), {var(3): 4})
                        assert obj.a == 1
                        assert obj.b == 4

                        f = Foo(1, 2)
                        assert reify_object(f, {}) is f
             */

            dynamic foo = new DyLogicObject();
            foo.a = 1;
            var variable = new Var(3);
            foo.b = variable;

            var dict = new Dictionary<object, object>();
            dict.Add(variable, 4);

            dynamic obj = LogicSharp.Reify_Object(foo, dict);
            Assert.NotNull(obj);
            Assert.True(1.Equals(obj.a));
            Assert.True(4.Equals(obj.b));

            dynamic f = new DyLogicObject();
            f.a = 1;
            f.b = 2;

            dict = new Dictionary<object, object>();
            obj = LogicSharp.Reify_Object(f, dict);
            Assert.True(obj == f);
        }
示例#4
0
        public void Test_print()
        {
            var variable = new Var('x');
            var term = new Term(Expression.Multiply, new Tuple<object, object>(1, variable));

            Assert.True(term.ToString().Equals("(1*x)"));
        }
示例#5
0
        public void Test_IsVar_2()
        {
            //var itself
            object ob2 = new Var('1');
            Assert.True(Var.ContainsVar(ob2));

            //Tuple
            object ob1 = new Tuple<object, object>(new Var('x'), 1);
            object ob0 = new Tuple<object>(1);
            Assert.True(Var.ContainsVar(ob1));
            Assert.False(Var.ContainsVar(ob0));

            //IEnumerable
            object ob3 = new List<object>() {1, 1, 1};
            object ob4 = new List<object>() { new Var('x'), 1 };
            Assert.False(Var.ContainsVar(ob3));
            Assert.True(Var.ContainsVar(ob4));

            //Dictionary
            var ob5 = new Dictionary<object, object>();
            ob5.Add(new Var('y'), 1);
            var ob6 = new Dictionary<object, object>();
            ob6.Add(2,3);
            Assert.True(Var.ContainsVar(ob5));
            Assert.False(Var.ContainsVar(ob6));
        }
示例#6
0
        public void TestLine_Unify_Reify_0()
        {
            var graph = new RelationGraph();
            var a = new Var("a");
		    var line = new Line(a, 1, 1.0);
		    var ls = new LineSymbol(line);
		    graph.AddNode(ls);

            List<ShapeSymbol> lines = graph.RetrieveShapeSymbols(ShapeType.Line);
            Assert.True(lines.Count == 1);
            var lineSymbol = lines[0] as LineSymbol;
            Assert.NotNull(lineSymbol);
            Assert.True(lineSymbol.CachedSymbols.Count == 0);

            var eqGoal = new EqGoal(a, 1); // a=1
            graph.AddNode(eqGoal);

            lines = graph.RetrieveShapeSymbols(ShapeType.Line);
            Assert.True(lines.Count == 1);
            var currLine = lines[0] as LineSymbol;
            Assert.NotNull(currLine);
            Assert.True(currLine.CachedSymbols.Count == 1);
            var cachedLineSymbol = currLine.CachedSymbols.ToList()[0] as LineSymbol;
            Assert.NotNull(cachedLineSymbol);
            var cachedLine = cachedLineSymbol.Shape as Line;
            Assert.NotNull(cachedLine);
            Assert.True(cachedLine.A.Equals(1.0));
            Assert.True(cachedLine.B.Equals(1.0));
            Assert.True(cachedLine.C.Equals(1.0));

            graph.DeleteNode(eqGoal);
            lines = graph.RetrieveShapeSymbols(ShapeType.Line);
            Assert.True(lines.Count == 1);
            currLine = lines[0] as LineSymbol;
            Assert.NotNull(currLine);
            Assert.True(currLine.CachedSymbols.Count == 0);
            Assert.True(currLine.CachedGoals.Count == 0);

		    var eqGoal2 = new EqGoal(a, 3);
            graph.AddNode(eqGoal2);

            lines = graph.RetrieveShapeSymbols(ShapeType.Line);
            Assert.True(lines.Count == 1);
            currLine = lines[0] as LineSymbol;
            Assert.NotNull(currLine);
            Assert.True(currLine.CachedSymbols.Count == 1);
		    Assert.True(currLine.CachedGoals.Count == 1);

            graph.DeleteNode(eqGoal2);

            lines = graph.RetrieveShapeSymbols(ShapeType.Line);
            Assert.True(lines.Count == 1);
            currLine = lines[0] as LineSymbol;
            Assert.NotNull(currLine);
            Assert.True(currLine.CachedSymbols.Count == 0);
            Assert.True(currLine.CachedGoals.Count == 0);

          
        }
示例#7
0
 public void Test_Clone1()
 {
     var variable = new Var('a'); 
     var variable1 = variable.Clone();
     variable1.Token = "b";
     Assert.True(variable.Token.Equals('a'));
     Assert.True(variable.ToString().Equals("a"));
 }
示例#8
0
 public void Test_Commutative_2_NonLinear()
 {
     //x^2*3 -> 3*x^2
     var x = new Var('x');
     var x_square = new Term(Expression.Power, new List<object>() { x, 2 });
     var a = new Term(Expression.Multiply, new List<object>() { x_square, 3 });
     object result = a.EvalAlgebra();
     Assert.True(result.ToString().Equals("3(x^2)"));
 }
示例#9
0
 public void Test_print3()
 {
     var x = new Var('x');
     var term0 = new Term(Expression.Multiply, new List<object>() {3, x});
     var y = new Var('y');
     var term1 = new Term(Expression.Multiply, new List<object>() {-1, y});
     var term = new Term(Expression.Add, new List<object>() {term0, term1});
     Assert.True(term.ToString().Equals("3x-y"));
 }
示例#10
0
 public void Test_Commutative_2()
 {
     //x*3 -> 3*x
     var x = new Var('x');
     var a = new Term(Expression.Multiply, new List<object>() { x, 3 });
     object result = a.EvalAlgebra();
     Assert.NotNull(result);
     Assert.True(result.ToString().Equals("3x"));
     Assert.True(a.Traces.Count == 1);
 }
示例#11
0
 public void Test_Commutative_1_NonLinear_2()
 {
     // x+x^2 -> x^2+x
     var x = new Var('x');
     var x_square = new Term(Expression.Power, new List<object>() { x, 2 });
     var term = new Term(Expression.Add, new List<object>() { x, x_square });
     object result = term.EvalAlgebra();
     var gTerm = result as Term;
     Assert.NotNull(gTerm);
     Assert.NotNull(result);
     Assert.True(result.ToString().Equals("x^2+x"));
 }
示例#12
0
        public void UnifyTest()
        {
            var term1 = new Term(Expression.Add, new List<object>() { 1, 1 });
            var x = new Var('x');
            var term2 = new Term(Expression.Add, new List<object>() { 1, x });

            var dict = new Dictionary<object, object>();
            bool result = LogicSharp.Unify(term1, term2, dict);

            Assert.True(result);
            Assert.True(dict.Count == 1);
        }
示例#13
0
 public void Test_Commutative_1()
 {
     //3+x -> x+3             
     var x = new Var('x');
     var a = new Term(Expression.Add, new List<object>() { 3, x });
     object result = a.EvalAlgebra();
     var gTerm = result as Term;
     Assert.NotNull(gTerm);
     Assert.NotNull(result);
     Assert.True(result.ToString().Equals("x+3"));
     Assert.True(a.Traces.Count == 1);
 }
示例#14
0
 public void Term_Algebra_Arith_4()
 {
     // //a^2+ 4^2 - 25; 
     //a^2+25+-1*16
     var a = new Var('a');
     var term1 = new Term(Expression.Power, new List<object>() {a, 2});
     var term2 = new Term(Expression.Power, new List<object>() {4, 2});
     var term3 = new Term(Expression.Multiply, new List<object>() {-1, 25});
     var term = new Term(Expression.Add, new List<object>() {term1, term2, term3});
     object obj = term.Eval();
     Assert.NotNull(obj);
 }
示例#15
0
        public void Test_Check1()
        {
            var line = new Line(1, 2, 3);
            Assert.True(line.Concrete);

            Assert.True(line.A.Equals(1.0));
            Assert.True(line.B.Equals(2.0));
            Assert.True(line.C.Equals(3.0));

            var variable = new Var('x');
            var line2 = new Line(2, variable, 3);
            Assert.False(line2.Concrete);
        }
示例#16
0
 public void Test_Goal_1()
 {
     //a = 2
     var x = new Var('a');
     var eqGoal = new EqGoal(x, 2);
     var substitutions = new Dictionary<object, object>();
     var result = eqGoal.Unify(substitutions);
     Assert.True(result);
     Assert.True(eqGoal.Traces.Count == 0);
     Assert.True(substitutions.Count == 1);
     Assert.True(substitutions.ContainsKey(x));
     Assert.True(substitutions[x].Equals(2));
     Assert.True(eqGoal.EarlySafe());
 }
示例#17
0
        public void Test_print2()
        {
            var term00 = new Term(Expression.Add, new List<object>(){1,1});
            var x = new Var('x');
            var term0 = new Term(Expression.Multiply, new List<object>() {term00, x});

            var term1 = new Term(Expression.Add, new List<object>() {term0, 1, -4});

            Assert.True(term00.ToString().Equals("1+1"));

            Assert.True(term0.ToString().Equals("(1+1)x"));
            
            Assert.True(term1.ToString().Equals("(1+1)x+1-4"));
        }
示例#18
0
        public void Test_Unify_3()
        {
            // a=2, b=a
            var a = new Var("a");
            var b = new Var("b");
            var eqGoal = new EqGoal(a, 2);
            var goalNode = new GoalNode(eqGoal);

            var equation = new Equation(b, a);

            object obj;
            bool result = RelationLogic.ConstraintCheck(goalNode, equation, null, out obj);
            Assert.True(result);
            Assert.NotNull(obj);
        }
示例#19
0
 public void Term_Algebra_Arith_3()
 {
     // 3 + x + 3
     var x = new Var('x');
     var lst = new List<object>() { 3, x, 3 };
     var term = new Term(Expression.Add, lst);
     object obj = term.Eval();
     Assert.NotNull(obj);
     var gTerm = obj as Term;
     Assert.NotNull(gTerm);
     var glst = gTerm.Args as List<object>;
     Assert.NotNull(glst);
     Assert.True(glst.Count == 2);
     Assert.True(term.Traces.Count == 1);
 }
 public void Test_Algebra_OneVariable_Linear_Goal_Gen_1()
 {
     //x = 2+3
     var x = new Var('x');
     var term = new Term(Expression.Add, new List<object>() { 2, 3 });
     var eq = new Equation(x, term);
     object obj;
     bool result = eq.IsEqGoal(out obj);
     Assert.True(result);
     var eqGoal = obj as EqGoal;
     Assert.NotNull(eqGoal);
     Assert.True(eqGoal.Traces.Count == 1);
     Assert.True(eqGoal.Lhs.Equals(x));
     Assert.True(eqGoal.Rhs.Equals(5));
     Assert.True(result);
 }
示例#21
0
 public void Test_Reify_1()
 {
     //a = 1, a*b = -1;
     //true positive
     var graph = new RelationGraph();
     var a = new Var("a");
     var b = new Var("b");
     var eqGoal = new EqGoal(a, 1);
     var lhsTerm = new Term(Expression.Multiply, new List<object>() { a, b });
     var equation = new Equation(lhsTerm, -1);
     graph.AddNode(eqGoal);
     var en = graph.AddNode(equation) as EquationNode;
     Assert.NotNull(en);
     Assert.True(en.Equation.CachedEntities.Count == 1);
     Assert.True(graph.Nodes.Count == 3);
 }
示例#22
0
        public void Test_Goal_Unify_1()
        {
            //S = a*b;
            var s = new Var('S');
            var a = new Var('a');
            var b = new Var('b');
            var term = new Term(Expression.Multiply, new List<object>() {a, b});
            var eqGoal = new EqGoal(s, term);
            var substitutions = new Dictionary<object, object>();
            bool result = eqGoal.Unify(substitutions);
            Assert.True(result);
            Assert.True(substitutions.Count == 1);
            Assert.True(substitutions.ContainsKey(s));

            Assert.True(Var.ContainsVar(substitutions[s]));
        }
        public void Test_Algebra_OneVariable_Linear_1()
        {
            //1+2=x
            var x = new Var('x');
            var lhs = new Term(Expression.Add, new List<object>() { 1, 2 });
            var equation = new Equation(lhs, x);
            bool result = equation.ContainsVar();
            Assert.True(result);
            Assert.True(equation.ToString().Equals("1+2=x"));

            equation.Eval();
            Assert.True(equation.CachedEntities.Count == 1);
            var outputEq = equation.CachedEntities.ToList()[0] as Equation;
            Assert.NotNull(outputEq);
            Assert.True(outputEq.ToString().Equals("x=3"));
            Assert.True(outputEq.Traces.Count == 1);
        }
        public void Test_CreateLineSegment_3()
        {
            var x = new Var('x');
            var point = new Point(2, x);
            var ps = new PointSymbol(point);
            var psNode = new ShapeNode(ps);
            var point1 = new Point(3, 4);
            var ps1 = new PointSymbol(point1);
            var psNode1 = new ShapeNode(ps1);

            object obj;
            bool value = RelationLogic.ConstraintCheck(psNode, psNode1, null, ShapeType.LineSegment, out obj);
            Assert.True(value);
            var lss = obj as LineSegmentSymbol;
            Assert.NotNull(lss);
            Assert.False(lss.Shape.Concrete);
        }
示例#25
0
 public void Term_Algebra_Arith_2()
 {
     var x = new Var('x');
     // 2*5 + x
     var lst = new List<object>() { 2, 5 };
     var term = new Term(Expression.Multiply, lst);
     var lst1 = new List<object>() { term, x };
     var term1 = new Term(Expression.Add, lst1);
     object obj = term1.Eval();
     Assert.NotNull(obj);
     var gTerm = obj as Term;
     Assert.NotNull(gTerm);
     var glst = gTerm.Args as List<object>;
     Assert.NotNull(glst);
     Assert.True(glst.Count == 2);
     Assert.True(term1.Traces.Count == 1);
 }
示例#26
0
        public void Test_Unify_2()
        {
            //true positive
            var x = new Var('x');
            var point = new Point(x, 4);
            var ps = new PointSymbol(point);
            var shapeNode = new ShapeNode(ps);

            var point1 = new Point(4, 5);
            var ps1 = new PointSymbol(point1);
            var shapeNode1 = new ShapeNode(ps1);

            var eqGoal = new EqGoal(x, 9);

            object obj;
            bool result = RelationLogic.ConstraintCheck(shapeNode, shapeNode1, eqGoal, null, out obj);
            Assert.False(result);
        }
示例#27
0
        public void Test_Unify_1()
        {
            //a = 1, a*b = -1;
            //true positive
           
            var a = new Var("a");
            var b = new Var("b");
            var eqGoal = new EqGoal(a, 1);

            var lhsTerm = new Term(Expression.Multiply, new List<object>() {a, b});
            var equation = new Equation(lhsTerm, -1);
            var eqNode = new EquationNode(equation);

            object obj;
            bool result = RelationLogic.ConstraintCheck(eqNode, eqGoal, null, out obj);
            Assert.True(result);
            Assert.NotNull(obj);
        }
示例#28
0
        public void TestUnify()
        {
            /*
                        assert unify(1, 1, {}) == {}
                        assert unify(1, 2, {}) == False
                        assert unify(var(1), 2, {}) == {var(1): 2}
                        assert unify(2, var(1), {}) == {var(1): 2}
                        assert unify(2, var(1), {var(1):3}) = {}
                        assert unify(3, var(2), {var(1):3}) = {}
            */
            var dict = new Dictionary<object, object>();
            bool result = LogicSharp.Unify(1, 1, dict);
            Assert.True(result);
            dict = new Dictionary<object, object>();
            result = LogicSharp.Unify(1, 2, dict);
            Assert.False(result);

            dict = new Dictionary<object, object>();
            var variable = new Var(1);
            result = LogicSharp.Unify(variable, 2, dict);
            Assert.True(result);
            Assert.True(dict.Count == 1);
            Assert.True(dict.ContainsKey(variable));
            Assert.True(dict[variable].Equals(2));

            dict = new Dictionary<object, object>();
            result = LogicSharp.Unify(2, variable, dict);
            Assert.True(result);
            Assert.True(dict.Count == 1);
            Assert.True(dict.ContainsKey(variable));
            Assert.True(dict[variable].Equals(2));

            dict = new Dictionary<object, object>();
            dict.Add(variable, 3);
            result = LogicSharp.Unify(2, variable, dict);
            Assert.False(result);

            var variable2 = new Var(2);
            dict = new Dictionary<object, object>();
            dict.Add(variable, 3);
            result = LogicSharp.Unify(3, variable2, dict);
            Assert.True(result);
        }
        /// <summary>
        /// Trace from Line General Form to Slope-Intercept Form
        /// ax+by+c=0 => y=mx+b
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public static void FromLineGeneralFormToSlopeIntercept(LineSymbol ls)
        {
            string strategy = "Given a general form line ax+by+c=0, the slope-intercept form of it is y = -(a/b)x-c/b.";

            var line = ls.Shape as Line;
            Debug.Assert(line != null);

            var newLS = new LineSymbol(line);
            newLS.OutputType = LineType.SlopeIntercept;

            var x = new Var("x");
            var y = new Var("y");
            var xTerm = new Term(Expression.Multiply, new List<object>() { line.A, x });
            var yTerm = new Term(Expression.Multiply, new List<object>() { line.B, y });
            var oldEqLeft = new Term(Expression.Add, new List<object>() { xTerm, yTerm, line.C });
            var oldEq = new Equation(oldEqLeft, 0);

            var invertXTerm = new Term(Expression.Multiply, new List<object>() { -1, xTerm });
            var intertZ = new Term(Expression.Multiply, new List<object>() { -1, line.C });

            var internalRight = new Term(Expression.Add, new List<object>() { invertXTerm, intertZ });
            var internalEq = new Equation(yTerm, internalRight);

            var finalXTerm = new Term(Expression.Multiply, new List<object>() { line.Slope, x });
            var finalRight = new Term(Expression.Add, new List<object>() { finalXTerm, line.Intercept });
            var lastEq = new Equation(yTerm, finalRight);

            string step1metaRule = "Given the line general form ax+by+c=0, move x term and Constant term to the right side of equation.";
            string step1AppliedRule = String.Format("Move {0}x and {1} to right side of equation.", ls.SymA, ls.SymC);

            string kc = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive);
            var ts0 = new TraceStep(null, internalEq, kc, step1metaRule, step1AppliedRule);
            ls._innerLoop.Add(ts0);

            string appliedRule = string.Format("divide coefficient b in both side of equation.");

            kc = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Inverse);

            var ts1 = new TraceStep(null, lastEq, kc, AlgebraRule.AlgebraicStrategy, appliedRule);
            ls._innerLoop.Add(ts1);
            ls.GenerateATrace(strategy);
        }
        public void Equation_Var_Substitution_1()
        {
            //a = 1, a*b = -1;
            var a = new Var("a");
            var b = new Var("b");
            var eqGoal = new EqGoal(a, 1);

            var lhsTerm = new Term(Expression.Multiply, new List<object>() { a, b });
            var equation = new Equation(lhsTerm, -1);

            bool result = equation.Reify(eqGoal);
            Assert.True(result);
            Assert.True(equation.CachedEntities.Count == 1);
            var cachedEq = equation.CachedEntities.ToList()[0] as Equation;
            object obj;
            result = cachedEq.IsEqGoal(out obj);
            Assert.True(result);
            var gGoal = obj as EqGoal;
            Assert.NotNull(gGoal);
            Assert.True(gGoal.Rhs.Equals(-1));
        }
示例#31
0
        public void Goal_Gen_6()
        {
            // d^2 = (1.0-4.0)^2+(2.0-6.0)^2
            // d>0
            object pt1XCoord = 1.0;
            object pt1YCoord = 2.0;
            object pt2XCoord = 4.0;
            object pt2YCoord = 6.0;

            var variable = new Var('d');
            var lhs      = new Term(Expression.Power, new List <object>()
            {
                variable, 2.0
            });

            var term1 = new Term(Expression.Subtract, new List <object>()
            {
                pt1XCoord, pt2XCoord
            });
            var term11 = new Term(Expression.Power, new List <object>()
            {
                term1, 2.0
            });

            var term2 = new Term(Expression.Subtract, new List <object>()
            {
                pt1YCoord, pt2YCoord
            });
            var term22 = new Term(Expression.Power, new List <object>()
            {
                term2, 2.0
            });

            var rhs = new Term(Expression.Add, new List <object>()
            {
                term11, term22
            });
            //var obj = rhs.Eval();

            //Assert.True(obj.Equals(25));
            var    eq = new Equation(lhs, rhs);
            object obj;
            bool   result = eq.IsEqGoal(out obj);

            Assert.True(result);
            var lst = obj as List <object>;

            Assert.NotNull(lst);
            Assert.True(lst.Count == 2);
            //Trace Verify

            var gGoal = lst[0] as EqGoal;

            Assert.NotNull(gGoal);
            Assert.True(gGoal.Traces.Count == 1);
            var steps = gGoal.Traces[0].Item2 as List <TraceStep>;

            Assert.NotNull(steps);
            Assert.True(steps.Count == 7);
            var lastStep = steps[6];

            Assert.NotNull(lastStep);
        }