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);*/
        }
예제 #2
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)"));
        }
예제 #3
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"));
 }
예제 #4
0
 public void Test_Print4()
 {
     //d=2^2
     var t1 = new Term(Expression.Power, new List<object>() {2, 2});
     Assert.True(t1.ToString().Equals("2^2"));
     //d= (3+4)^0.5
     var t2 = new Term(Expression.Add, new List<object>() {3, 4});
     var t3 = new Term(Expression.Power, new List<object>() {t2, 0.5});
     Assert.True(t3.ToString().Equals("(3+4)^0.5"));
 }
예제 #5
0
        public static bool UnifyImpl(Term u, Term v, Dictionary<object, object> s)
        {
            bool opUnifiable = Unify(u.Op, v.Op, s);

            if (opUnifiable)
            {
                return Unify(u.Args, v.Args, s);
            }
            return false;
        }
예제 #6
0
 public void Term_Arith_3()
 {
     //Substraction
     var lst = new List<object>() { 1, -2, -3 };
     var term = new Term(Expression.Multiply, lst);
     object obj = term.Eval();
     Assert.NotNull(obj);
     Assert.True(obj.Equals(6));
     Assert.True(term.Traces.Count == 1);
 }
예제 #7
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);
 }
예제 #8
0
 public void Term_Arith_2()
 {
     //1+(-2)+(-3)
     //Substraction
     var lst = new List<object>() { 1, -2, -3 };
     var term = new Term(Expression.Add, lst);
     object obj = term.Eval();
     Assert.NotNull(obj);
     Assert.True(obj.Equals(-4));
     Assert.True(term.Traces.Count == 1);
 }
예제 #9
0
 public void Term_Arith_1()
 {
     //1+2+3
     //Addition
     var lst = new List<object>() {1, 2, 3};
     var term = new Term(Expression.Add, lst);
     object obj = term.Eval();
     Assert.NotNull(obj);
     Assert.True(obj.Equals(6));
     Assert.True(term.Traces.Count == 1);
 }
예제 #10
0
 public void Term_Arith_5()
 {
     //1-2*3
     var lst = new List<object>() { 2, 3 };
     var term = new Term(Expression.Multiply, lst);
     var lst1 = new List<object>() { 1, term };
     var term1 = new Term(Expression.Subtract, lst1);
     object obj = term1.Eval();
     Assert.NotNull(obj);
     Assert.True(obj.Equals(-5));
     Assert.True(term1.Traces.Count == 1);
 }
예제 #11
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);
 }
예제 #12
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"));
 }
예제 #13
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);
        }
예제 #14
0
        public void Test_OneStrategy_OneTraceStep()
        {
            //strategy1 : 1+1->2
            var expr1 = new Term(Expression.Add, new List<object>{1,1});
            var ts = new TraceStep(expr1, 2, "null", "meta-rule todo", "rule todo");
            var tsExpr = new TraceStepExpr(ts);
            var lst = new List<TraceStepExpr>() { tsExpr};
            var tuple = new Tuple<object, object>("strategy1", lst);
            var lstStrategy = new List<Tuple<object, object>>();
            lstStrategy.Add(tuple);

            var graph = new BehaviorGraph();
            graph.Insert(lstStrategy);            
            Assert.True(graph.Nodes.Count == 2);

            var node0 = graph.Nodes[0];
            Assert.Null(node0.SubGraph);
            var node1 = graph.Nodes[1];

            Assert.True(node0.OutEdges.Count == 1);
            var edgeInfo = node0.OutEdges[0].Property as OuterLoopEdgeProperty;
            Assert.NotNull(edgeInfo);
            Assert.True(edgeInfo.Strategy.Equals("strategy1"));

            Assert.NotNull(node1.SubGraph);
            Assert.True(node1.InEdges.Count == 1);
            Assert.True(node1.OutEdges.Count == 0);

            Assert.True(node1.SubGraph.Nodes.Count == 2);

            /////////////////////////////////////////////////////
            //Search Test

            var initNode = graph.RetrieveInitInnerNode();
            Assert.NotNull(initNode);
            int count = graph.PathFinding(initNode);
            Assert.True(count == 1);

            var nextObj = graph.SearchNextInnerLoopNode(initNode);
            var tuple2 = nextObj as Tuple<object,object>;
            Assert.NotNull(tuple2);
            var nextNode = tuple2.Item2 as BehaviorGraphNode;
            Assert.NotNull(nextNode);
            count = graph.PathFinding(nextNode);
            Assert.True(count == 0);

            var prevObj = graph.SearchPrevInnerLoopNode(nextNode) as BehaviorGraphNode;
            Assert.NotNull(prevObj);
            count = graph.PathFinding(prevObj);
            Assert.True(count == 1);
        }
예제 #15
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"));
        }
예제 #16
0
        /// <summary>
        //with calculation functionality, it does not 
        /// take care of term rewriting.
        /// 
        /// Terms should follow the below rules:
        /// tp: "2+2",  "2+3-1", "2+2*2"
        /// </summary>
        /// <returns> can be term or value</returns>
        /// 
        public static object Arithmetic(this object obj, Term rootTerm)
        {
            var term = obj as Term;
            if (term == null) return obj;

            Term localTerm = term.DepthFirstSearch(rootTerm);
            var list = localTerm.Args as List<object>;

            //List<object> objs = localTerm.FindArithValues();
            if (list == null || list.Count < 2) return localTerm;
            bool madeChanges;
            do
            {
                list = localTerm.Args as List<object>;
                if (list == null) throw new Exception("Cannot be null");
                int itemCount = list.Count;
                madeChanges = false;
                itemCount--;
                for (var i = 0; i < itemCount; i++)
                {
                    list = localTerm.Args as List<object>;
                    Debug.Assert(list != null);
                    itemCount = list.Count;
                    object obj1;
                    if (i + 1 >= list.Count) break;

                    if (SatisfyCalcCondition(term.Op, list[i], list[i + 1], out obj1))
                    {
                        var cloneTerm = localTerm.Clone();
                        var cloneLst = cloneTerm.Args as List<object>;
                        Debug.Assert(cloneLst != null);
                        cloneLst[i] = obj1;
                        cloneLst.RemoveAt(i + 1);

                        string metaScaffold = ArithRule.CalcRule(term.Op.Method.Name);
                        string scaffold = ArithRule.CalcRule(term.Op.Method.Name, list[i], list[i + 1], obj1);
                        string kc = ArithRule.FindKC(term.Op.Method.Name);
                       
                        rootTerm.GenerateTrace(localTerm, cloneTerm, kc, metaScaffold, scaffold);
                        localTerm = cloneTerm;
                        madeChanges = true;
                    }
                }
            } while (madeChanges);
        
            var lstArgs = localTerm.Args as List<object>;
            if (lstArgs.Count == 1) return lstArgs[0];
            return localTerm;
        }
예제 #17
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);
 }
예제 #18
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]));
        }
예제 #19
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);
 }
 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);
 }
        /// <summary>
        /// a+1=, 2+3+5=
        /// </summary>
        /// <param name="term"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool ConstraintSatisfy(Term term, out object obj)
        {
            var evalObj = term.Eval();
            var generatedEq = new Equation(term, evalObj);
            generatedEq.ImportTrace(term);

            var evalTerm = evalObj as Term;
            var evalVar = evalObj as Var;

            var dict = new Dictionary<object, object>();
            var connectLst = new List<GraphNode>();
            if (evalTerm != null)
            {
                for (var i = 0; i < _nodes.Count; i++)
                {
                    var goalNode = _nodes[i] as GoalNode;
                    if (goalNode != null)
                    {
                        if (evalTerm.ContainsVar((EqGoal)goalNode.Goal))
                        {
                            connectLst.Add(goalNode);
                        }
                    }
                }
            }
            if (evalVar != null)
            {
                for (var i = 0; i < _nodes.Count; i++)
                {
                    var goalNode = _nodes[i] as GoalNode;
                    if (goalNode != null)
                    {
                        var eqGoal = goalNode.Goal as EqGoal;
                        Debug.Assert(eqGoal != null);
                        var lhsVar = eqGoal.Lhs as Var;
                        Debug.Assert(lhsVar != null);
                        if (lhsVar.Equals(evalVar))
                        {
                            connectLst.Add(goalNode);
                        }
                    }
                }
            }
            dict.Add(connectLst, generatedEq);
            obj = dict;
            return true;
        }
예제 #22
0
        public void Test_Arith_3()
        {
            //1+2+3=6
            var lhs = new Term(Expression.Add, new List<object>() { 1, 2, 3 });
            var equation = new Equation(lhs, 6);
            bool result = equation.ContainsVar();
            Assert.False(result);
            Assert.True(equation.ToString().Equals("1+2+3=6"));

            object outputEq;
            bool? evalResult = equation.Eval(out outputEq);
            Assert.NotNull(evalResult);
            Assert.True(evalResult.Value);
            Assert.NotNull(outputEq);
            Assert.True(outputEq.ToString().Equals("6=6"));
            Assert.True(equation.Traces.Count == 1);
        }
예제 #23
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);
 }
        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);
        }
예제 #25
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);
        }
예제 #26
0
        /// <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);
        }
예제 #27
0
        public void Test_Arith_2()
        {
            //1+2=4
            var lhs = new Term(Expression.Add, new List<object>() { 1, 2 });
            var equation = new Equation(lhs, 4);
            bool result = equation.ContainsVar();
            Assert.False(result);
            Assert.True(equation.ToString().Equals("1+2=4"));

            object obj = equation.Eval();
            Assert.NotNull(obj);
            var satisfied = obj as bool?;
            Assert.NotNull(satisfied);
            Assert.False(satisfied.Value);
            Assert.True(equation.CachedEntities.Count == 1);
            var cachedEq = equation.CachedEntities.ToList()[0] as Equation;
            Assert.NotNull(cachedEq);

            Assert.True(cachedEq.ToString().Equals("3=4"));
            Assert.True(cachedEq.Traces.Count == 1);
        }
예제 #28
0
        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));
        }
예제 #29
0
        public static bool MatchQuadraticTerm(this Term currTerm, Term matchTerm)
        {
            if (currTerm == null || matchTerm == null) return false;
            if (!currTerm.Op.Method.Name.Equals(matchTerm.Op.Method.Name)) return false;

            var lst1 = currTerm.Args as List<object>;
            var lst2 = matchTerm.Args as List<object>;

            if (lst1.Count != lst2.Count) return false;
            if (!lst1[1].Equals(lst2[1])) return false;

            var var1 = lst1[0] as Var;
            var var2 = lst2[0] as Var;
            if (var1 != null && var2 != null)
            {
                if (var1.ToString().Equals(var2.ToString())) return true;
            }

            var t1 = lst1[0] as Term;
            var t2 = lst2[0] as Term;
            if (t1 != null && t2 != null && t1.Equals(t2)) return true;

            return false;
        }
 public void Goal_Gen_3()
 {
     //x=(-1)*0
     var x = new Var('x');
     var term = new Term(Expression.Multiply, new List<object>() { -1, 0 });
     var eq = new Equation(x, term);
     object obj;
     bool result = eq.IsEqGoal(out obj);
     Assert.True(result);
     var goal = obj as EqGoal;
     Assert.NotNull(goal);
     Assert.True(goal.Rhs.Equals(0));
 }
예제 #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);
        }