/// <summary>
        /// This method quarantees that the substitution term is unique toward each variable.
        /// 
        /// dict: {{x:3},{x:4}} is not allowed 
        /// dict: {{x:3}, {y:4}} is allowed
        /// </summary>
        /// <param name="point"></param>
        /// <param name="dict"></param>
        /// <param name="ps"></param>
        /// <param name="goal"></param>
        /// <returns></returns>
        public bool Reify(EqGoal goal)
        {
            var point = Shape as Point;
            Debug.Assert(point != null);

            //EqGoal tempGoal = goal.GetLatestDerivedGoal();
            EqGoal tempGoal = goal;
            bool cond1 = Var.IsVar(tempGoal.Lhs) && LogicSharp.IsNumeric(tempGoal.Rhs);
            bool cond2 = Var.IsVar(tempGoal.Rhs) && LogicSharp.IsNumeric(tempGoal.Lhs);
            Debug.Assert(cond1 || cond2);

            if (point.Concrete) return false;

            object xResult = EvalGoal(point.XCoordinate, tempGoal);
            object yResult = EvalGoal(point.YCoordinate, tempGoal);

            //Atomic operation
            if (!point.XCoordinate.Equals(xResult))
            {
                GenerateXCacheSymbol(xResult, goal);
                RaiseReify(null);
                return true;
            }
            else if (!point.YCoordinate.Equals(yResult))
            {
                GenerateYCacheSymbol(yResult, goal);
                RaiseReify(null);
                return true;
            }
            else
            {
                return false;
            }
        }
        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));
        }
 private static EqGoal InferYCoord(this PointSymbol inputPointSymbol, string label)
 {
     var point = inputPointSymbol.Shape as Point;
     Debug.Assert(point != null);
     var goal = new EqGoal(new Var(label), point.YCoordinate);
     return goal;
 }
        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);

          
        }
Exemple #5
0
        public bool ContainsVar(EqGoal goal)
        {
            Debug.Assert(goal != null);
            var variable = goal.Lhs as Var;

            Debug.Assert(variable != null);
            return(ContainsVar(variable));
        }
Exemple #6
0
        public void test_reify_object2()
        {
            var foo = new DyLogicObject();
            foo.Properties.Add("y", 1);
            var goal = new EqGoal(new Var("x"), 2);
            foo.Reify(goal);

            Assert.True(foo.Properties.Count == 2);
        }
Exemple #7
0
        public static bool IsEqGoal(this Equation eq, out object goal, bool allowEval = true)
        {
            goal = null;

            if (!allowEval)
            {
                if (SatisfyGoalCondition(eq))
                {
                    var lst      = new List <object>();
                    var goalTemp = new EqGoal(eq);
                    lst.Add(goalTemp);
                    return(true);
                }
                return(false);
            }

            eq.Eval();
            if (eq.CachedEntities.Count == 1)
            {
                var outEq = eq.CachedEntities.ToList()[0] as Equation;
                if (outEq != null && SatisfyGoalCondition(outEq))
                {
                    goal = new EqGoal(outEq)
                    {
                        Traces = eq.Traces
                    };
                    return(true);
                }
            }

            if (eq.CachedEntities.Count > 1)
            {
                var lst = new List <object>();
                foreach (var temp in eq.CachedEntities.ToList())
                {
                    var eqTemp = temp as Equation;
                    if (eqTemp != null && SatisfyGoalCondition(eqTemp))
                    {
                        var goalTemp = new EqGoal(eqTemp)
                        {
                            Traces = eqTemp.Traces
                        };
                        lst.Add(goalTemp);
                    }
                }
                if (lst.Count == 0)
                {
                    return(false);
                }
                goal = lst;
                return(true);
            }
            return(false);
        }
Exemple #8
0
 public static object Run(Var variable, EqGoal goal)
 {
     if (variable.Equals(goal.Lhs))
     {
         return(goal.Rhs);
     }
     if (variable.Equals(goal.Rhs))
     {
         return(goal.Lhs);
     }
     return(null);
 }
        public void Equation_Var_Substitution_4()
        {
            //x = 3, x = y
            var x   = new Var('a');
            var eq1 = new EqGoal(x, 3);   // x=3
            var y   = new Var('y');
            var eq2 = new Equation(x, y); // x=y

            bool result = eq2.Reify(eq1);

            Assert.True(result);
            Assert.True(eq2.CachedEntities.Count == 1);
        }
Exemple #10
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());
 }
 public object EvalGoal(object field, EqGoal goal)
 {
     var substitute = goal.ToDict();
     object result = null;
     if (Var.ContainsVar(field))
     {
         result = LogicSharp.Reify(field, substitute);
     }
     else
     {
         result = field;
     }
     return result; 
 }
Exemple #12
0
        public object EvalGoal(object field, EqGoal goal)
        {
            var    substitute = goal.ToDict();
            object result     = null;

            if (Var.ContainsVar(field))
            {
                result = LogicSharp.Reify(field, substitute);
            }
            else
            {
                result = field;
            }
            return(result);
        }
        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);
        }
Exemple #14
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_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);
 }
Exemple #16
0
        public static bool IsEqGoal(this Equation eq, out object goal, bool allowEval = true)
        {
            goal = null;

            if (!allowEval)
            {
                if (SatisfyGoalCondition(eq))
                {
                    var lst = new List<object>();
                    var goalTemp = new EqGoal(eq);
                    lst.Add(goalTemp);
                    return true;
                }
                return false;
            }

            eq.Eval();
            if (eq.CachedEntities.Count == 1)
            {
                var outEq = eq.CachedEntities.ToList()[0] as Equation;
                if (outEq != null && SatisfyGoalCondition(outEq))
                {
                    goal = new EqGoal(outEq) {Traces = eq.Traces};
                    return true;
                }
            }

            if (eq.CachedEntities.Count > 1)
            {
                var lst = new List<object>();
                foreach (var temp in eq.CachedEntities.ToList())
                {
                    var eqTemp = temp as Equation;
                    if (eqTemp != null && SatisfyGoalCondition(eqTemp))
                    {
                        var goalTemp = new EqGoal(eqTemp) { Traces = eqTemp.Traces };
                        lst.Add(goalTemp);
                    }
                }
                if (lst.Count == 0) return false;
                goal = lst;
                return true;
            }
            return false;
        }
        public static object Unify(PointSymbol pt1, PointSymbol pt2, EqGoal goal = null)
        {
            //point identify check
            if (pt1.Equals(pt2)) return null;

            //Mid-point build process
            if (pt1.Shape.Concrete && pt2.Shape.Concrete)
            {
                var point1 = pt1.Shape as Point;
                var point2 = pt2.Shape as Point;
                Debug.Assert(point1 != null);
                Debug.Assert(point2 != null);
                var midPoint = PointGenerationRule.GenerateMidPoint(point1, point2);
                TraceInstructionalDesign.FromPointsToMidPoint(pt1, pt2, midPoint);
                return midPoint;
            }
            return null;
        }
        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);
        }
Exemple #19
0
        public bool UnReify(EqGoal goal)
        {
            if (CachedEntities.Count == 0)
            {
                return(false);
            }

            var goalVar = goal.Lhs as Var;

            Debug.Assert(goalVar != null);

            if (ContainsVar(goalVar))
            {
                CachedEntities.Clear();
                return(true);
            }
            return(false);
        }
        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);
        }
Exemple #21
0
        /// <summary>
        /// Trace Derivation purpose
        /// </summary>
        /// <param name="goal"></param>
        /// <returns></returns>
        public static EqGoal GetLatestDerivedGoal(this EqGoal goal)
        {
            throw new Exception("TODO");

            /* //pre-processing of goal
             * EqGoal tempGoal;
             * if (goal.Traces.Count != 0)
             * {
             *   var trace = goal.Traces[0];
             *   Debug.Assert(trace.Target != null);
             *   var traceGoal = trace.Target as EqGoal;
             *   Debug.Assert(traceGoal != null);
             *   tempGoal = traceGoal;
             * }
             * else
             * {
             *   tempGoal = goal;
             * }
             * return tempGoal;*/
        }
        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));
        }
Exemple #23
0
        public static Dictionary <object, object> ToDict(this EqGoal goal)
        {
            object variable;
            object value;

            if (Var.IsVar(goal.Lhs))
            {
                variable = goal.Lhs;
                value    = goal.Rhs;
            }
            else
            {
                variable = goal.Rhs;
                value    = goal.Lhs;
            }

            var pair       = new KeyValuePair <object, object>(variable, value);
            var substitute = new Dictionary <object, object>();

            substitute.Add(pair.Key, pair.Value);
            return(substitute);
        }
Exemple #24
0
        public bool Reify(EqGoal goal)
        {
            var line = this.Shape as Line;
            Debug.Assert(line != null);
            EqGoal tempGoal = goal;
            bool cond1 = Var.IsVar(tempGoal.Lhs) && LogicSharp.IsNumeric(tempGoal.Rhs);
            bool cond2 = Var.IsVar(tempGoal.Rhs) && LogicSharp.IsNumeric(tempGoal.Lhs);
            Debug.Assert(cond1 || cond2);

            if (line.Concrete) return false;

            object aResult = EvalGoal(line.A, tempGoal);
            object bResult = EvalGoal(line.B, tempGoal);
            object cResult = EvalGoal(line.C, tempGoal);

            //Atomic operation
            if (aResult != null && !line.A.Equals(aResult))
            {
                CacheA(aResult, goal);
                RaiseReify(null);
                return true;
            }

            if (bResult != null && !line.B.Equals(bResult))
            {
                CacheB(bResult, goal);
                RaiseReify(null);
                return true;
            }

            if (cResult != null && !line.C.Equals(cResult))
            {
                CacheC(cResult, goal);
                RaiseReify(null);
                return true;
            }

            return false;
        }
Exemple #25
0
        public void Term_Algebra_Reify_2()
        {
            /*
             *  //a*b=>
             *  //a=1
             */
            var y    = new Var('y');
            var a    = new Var('a');
            var b    = new Var('b');
            var term = new Term(Expression.Multiply, new List <object>()
            {
                a, b
            });
            //Assert.True(term.ToString().Equals("(y+y)"));
            var eqGoal = new EqGoal(a, 1);
            var term1  = term.Reify(eqGoal) as Term;

            Assert.NotNull(term1);
            var obj = term1.Eval() as Var;

            Assert.NotNull(obj);
        }
        public void Equation_Var_Substitution_3()
        {
            //a = 2, b=a
            var  a        = new Var("a");
            var  b        = new Var("b");
            var  eqGoal   = new EqGoal(a, 2);
            var  equation = new EqGoal(b, a);
            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.Lhs.Equals(b));
            Assert.True(gGoal.Rhs.Equals(2));
        }
Exemple #27
0
        public void Term_Algebra_Reify_1()
        {
            /*
             *  //y+y=>
             *  //y = 2
             */
            var y    = new Var('y');
            var term = new Term(Expression.Add, new List <object>()
            {
                y, y
            });
            //Assert.True(term.ToString().Equals("(y+y)"));
            var eqGoal = new EqGoal(y, 2);
            var term1  = term.Reify(eqGoal) as Term;

            Assert.NotNull(term1);
            object obj = term1.Eval();

            Assert.NotNull(obj);
            Assert.True(obj.Equals(4));
            Assert.True(term1.Traces.Count == 1);
        }
        public void Test_Unify_1()
        {
            //true positive
            var x = new Var('x');
            var y = new Var('y');
            var point = new Point(x, y);
            var ps = new PointSymbol(point);
            var shapeNode = new ShapeNode(ps);

            var eqGoal  = new EqGoal(x, 1); // x=1

            object obj;
            bool result = RelationLogic.ConstraintCheck(shapeNode, eqGoal, null, out obj);
            Assert.True(result);
            Assert.NotNull(obj);

/*            var lst = obj as Tuple<object, object>;
            Assert.NotNull(lst);
            Assert.True(lst.Count == 1);
            var tuple = lst[0];
            Assert.True(tuple.Item1.Equals(shapeNode));
            Assert.True(tuple.Item2.Equals(eqGoal));*/
        }
        public bool UnReify(EqGoal goal)
        {
            var point = Shape as Point;
            Debug.Assert(point != null);
            if (!ContainGoal(goal)) return false;
            var updateLst = new ObservableCollection<ShapeSymbol>();
            var unchangedLst = new ObservableCollection<ShapeSymbol>();
            foreach (var shape in CachedSymbols.ToList())
            {
                var pt = shape as PointSymbol;
                if (pt == null) continue;
                if (pt.ContainGoal(goal))
                {
                    pt.UndoGoal(goal, point);
                    if (pt.CachedGoals.Count != 0)
                    {
                        updateLst.Add(pt);
                    }
                }
                else
                {
                    unchangedLst.Add(shape);
                }
            }

            if (unchangedLst.Count != 0)
            {
                CachedSymbols = unchangedLst;
            }
            else
            {
                CachedSymbols = updateLst;
            }

            RemoveGoal(goal);
            return true;
        }
Exemple #30
0
        public bool UnReify(EqGoal goal)
        {
            var line = this.Shape as Line;
            Debug.Assert(line != null);
            if (!ContainGoal(goal)) return false;
            var updateLst = new ObservableCollection<ShapeSymbol>();
            var unchangedLst = new ObservableCollection<ShapeSymbol>();
            foreach (var shape in CachedSymbols.ToList())
            {
                var iline = shape as LineSymbol;
                if (iline == null) continue;
                if (iline.ContainGoal(goal))
                {
                    iline.UndoGoal(goal, line);
                    if (iline.CachedGoals.Count != 0)
                    {
                        updateLst.Add(iline);
                    }
                }
                else
                {
                    unchangedLst.Add(shape);
                }
            }

            if (unchangedLst.Count != 0)
            {
                CachedSymbols = unchangedLst;
            }
            else
            {
                CachedSymbols = updateLst;
            }
            RemoveGoal(goal);
            return true;
        }
 public bool ContainGoal(EqGoal goal)
 {
     return CachedGoals.Any(pair => pair.Value.Equals(goal));
 }
 /// <summary>
 /// Goal Input Patter Match
 /// </summary>
 /// <param name="expr"></param>
 /// <param name="goal"></param>
 /// <param name="output"></param>
 /// <param name="userInput"></param>
 /// <returns></returns>
 private bool EvalExprPatterns(Expr expr, EqGoal goal, out object output, bool userInput = false)
 {
     object obj = null;
     if (!userInput)
     {
         obj = RelationGraph.AddNode(goal);
     }
     output = new AGPropertyExpr(expr, goal);
     return true;
 }
Exemple #33
0
        public bool Reify(EqGoal goal)
        {
            var lhsTerm = Lhs as Term;
            var rhsTerm = Rhs as Term;
            var lhsVar  = Lhs as Var;
            var rhsVar  = Rhs as Var;

            string strategy = "Reify equation's internal variable by substituing a given fact.";

            if (lhsVar != null)
            {
                var lhsNum = LogicSharp.Reify(lhsVar, goal.ToDict());
                if (lhsNum != null && !lhsNum.Equals(lhsVar))
                {
                    var cloneEq = Clone();
                    cloneEq.Lhs = lhsNum;

                    string rule        = SubstitutionRule.ApplySubstitute();
                    string appliedRule = SubstitutionRule.ApplySubstitute(this, cloneEq);
                    var    ts          = new TraceStep(this, cloneEq, SubstitutionRule.SubstituteKC(), rule, appliedRule);
                    cloneEq._innerLoop.Add(ts);
                    cloneEq.GenerateATrace(strategy);
                    CachedEntities.Add(cloneEq);
                    return(true);
                }
            }

            if (rhsVar != null)
            {
                var rhsNum = LogicSharp.Reify(rhsVar, goal.ToDict());
                if (rhsNum != null && !rhsNum.Equals(lhsVar))
                {
                    var cloneEq = Clone();
                    cloneEq.Rhs = rhsNum;
                    string rule        = SubstitutionRule.ApplySubstitute();
                    string appliedRule = SubstitutionRule.ApplySubstitute(this, goal);
                    var    ts          = new TraceStep(this, cloneEq, SubstitutionRule.SubstituteKC(), rule, appliedRule);
                    cloneEq._innerLoop.Add(ts);
                    cloneEq.GenerateATrace(strategy);
                    CachedEntities.Add(cloneEq);
                    return(true);
                }
            }

            if (lhsTerm != null)
            {
                var term1 = lhsTerm.Reify(goal) as Term;
                if (lhsTerm.Equals(term1) || term1 == null)
                {
                    return(false);
                }
                var obj = term1.Eval();

                var cloneEq = Clone();
                cloneEq.Lhs = obj;
                string rule        = SubstitutionRule.ApplySubstitute();
                string appliedRule = SubstitutionRule.ApplySubstitute(this, goal);
                var    ts          = new TraceStep(this, cloneEq, SubstitutionRule.SubstituteKC(), rule, appliedRule);
                cloneEq._innerLoop.Add(ts);
                cloneEq.GenerateATrace(strategy);
                CachedEntities.Add(cloneEq);

                return(true);
            }

            if (rhsTerm != null)
            {
                object obj = rhsTerm.Reify(goal);
                if (rhsTerm.Equals(obj))
                {
                    return(false);
                }
                var cloneEq = Clone();
                cloneEq.Rhs = obj;
                string rule        = SubstitutionRule.ApplySubstitute();
                string appliedRule = SubstitutionRule.ApplySubstitute(this, goal);
                var    ts          = new TraceStep(this, cloneEq, SubstitutionRule.SubstituteKC(), rule, appliedRule);
                cloneEq._innerLoop.Add(ts);
                cloneEq.GenerateATrace(strategy);

                CachedEntities.Add(cloneEq);
                return(true);
            }
            return(false);
        }
        public bool Reify(EqGoal goal)
        {
            var lhsTerm = Lhs as Term;
            var rhsTerm = Rhs as Term;
            var lhsVar = Lhs as Var;
            var rhsVar = Rhs as Var;

            string strategy = "Reify equation's internal variable by substituing a given fact.";

            if (lhsVar != null)
            {
                var lhsNum = LogicSharp.Reify(lhsVar, goal.ToDict());
                if (lhsNum != null && !lhsNum.Equals(lhsVar))
                {
                    var cloneEq = Clone();
                    cloneEq.Lhs = lhsNum;

                    string rule = SubstitutionRule.ApplySubstitute();
                    string appliedRule = SubstitutionRule.ApplySubstitute(this, cloneEq);
                    var ts = new TraceStep(this, cloneEq, SubstitutionRule.SubstituteKC(), rule, appliedRule);
                    cloneEq._innerLoop.Add(ts);
                    cloneEq.GenerateATrace(strategy);
                    CachedEntities.Add(cloneEq);
                    return true;
                }
            }

            if (rhsVar != null)
            {
                var rhsNum = LogicSharp.Reify(rhsVar, goal.ToDict());
                if (rhsNum != null && !rhsNum.Equals(lhsVar))
                {
                    
                    var cloneEq = Clone();
                    cloneEq.Rhs = rhsNum;
                    string rule = SubstitutionRule.ApplySubstitute();
                    string appliedRule = SubstitutionRule.ApplySubstitute(this, goal);
                    var ts = new TraceStep(this, cloneEq, SubstitutionRule.SubstituteKC(), rule, appliedRule);
                    cloneEq._innerLoop.Add(ts);
                    cloneEq.GenerateATrace(strategy);
                    CachedEntities.Add(cloneEq);
                    return true;
                }                
            }

            if(lhsTerm != null)
            {
                var term1 = lhsTerm.Reify(goal) as Term;
                if (lhsTerm.Equals(term1) || term1 == null)
                {
                    return false;
                }
                var obj = term1.Eval(); 

                var cloneEq = Clone();
                cloneEq.Lhs = obj;
                string rule = SubstitutionRule.ApplySubstitute();
                string appliedRule = SubstitutionRule.ApplySubstitute(this, goal);
                var ts = new TraceStep(this, cloneEq, SubstitutionRule.SubstituteKC(), rule, appliedRule);
                cloneEq._innerLoop.Add(ts);
                cloneEq.GenerateATrace(strategy);
                CachedEntities.Add(cloneEq);

                return true;
            }

            if (rhsTerm != null)
            {
                object obj = rhsTerm.Reify(goal);
                if (rhsTerm.Equals(obj))
                {
                    return false;
                }
                var cloneEq = Clone();
                cloneEq.Rhs = obj;
                string rule = SubstitutionRule.ApplySubstitute();
                string appliedRule = SubstitutionRule.ApplySubstitute(this, goal);
                var ts = new TraceStep(this, cloneEq, SubstitutionRule.SubstituteKC(), rule, appliedRule);
                cloneEq._innerLoop.Add(ts);
                cloneEq.GenerateATrace(strategy);

                CachedEntities.Add(cloneEq);
                return true;
            }
            return false;
        }
        public bool UnReify(EqGoal goal)
        {
            if (CachedEntities.Count == 0) return false;

            var goalVar = goal.Lhs as Var;
            Debug.Assert(goalVar != null);

            if (ContainsVar(goalVar))
            {
                CachedEntities.Clear();
                return true;
            }
            return false;
        }
 //backward-solving (Goal Search)
 public abstract bool UnifyProperty(EqGoal goal, out object obj);
Exemple #37
0
 public virtual void UndoGoal(EqGoal goal, object parent)
 {
 }
Exemple #38
0
 public bool ContainGoal(EqGoal goal)
 {
     return(CachedGoals.Any(pair => pair.Value.Equals(goal)));
 }
        private bool GoalVerify(IKnowledge obj, EqGoal eqGoal, out string msg, out object output)
        {
            msg = AGTutorMessage.VerifyWrong;
            output = null;

            List<Tuple<object, object>> trace = null;

            var agPropExpr = new AGPropertyExpr(obj.Expr, eqGoal);
            agPropExpr.IsSelected = true;
            agPropExpr.GenerateSolvingTrace();
            trace = agPropExpr.AutoTrace;

            BehaviorGraphNode node;
            if (trace == null || trace.Count == 0)
            {
                node = UserGraph.UpdateSolvingCache(agPropExpr);
                //node = UserGraph.SearchInnerLoopNode(eqGoal);

                if (node == null)
                    return false;
            }
            if (trace != null)
            {
                bool matchResult = UserGraph.Match(trace);
                if (!matchResult) return false;
                //insert nodes
                UserGraph.Insert(trace);
                CurrentStateNode = UserGraph.SearchInnerLoopNode(obj); //update _currentStateNode;
            }
            else
            {
                CurrentStateNode = UserGraph.SearchInnerLoopNode(eqGoal); 
            }
           /* var nextTuple1 = UserGraph.SearchNextInnerLoopNode(CurrentStateNode);
            if (nextTuple1 == null) // query-end
            {
                msg = AGTutorMessage.SolvedProblem;
                return true;
            }
            */
            msg = AGTutorMessage.VerifyCorrect;
            return true;
        }
Exemple #40
0
        public void TestLogicAny()
        {
            var x = new Var('x');
            var goal1 = new EqGoal(x, 2);
            
            var lst = new List<Goal>();
            lst.Add(goal1);
            var dict = new Dictionary<object, object>();
            object result = LogicSharp.logic_Any(lst, dict);
            Assert.IsNotNull(result);
            Assert.IsInstanceOf(typeof(IEnumerable<KeyValuePair<object,object>>), result);
            Assert.IsInstanceOf(typeof(Dictionary<object, object>), result);
            var resultDict = result as Dictionary<object, object>;
            Assert.IsTrue(resultDict.Count == 1);
            Assert.True(dict.ContainsKey(x));

            //assert len(tuple(lany(eq(x, 2), eq(x, 3))({}))) == 2
            //assert len(tuple(lany((eq, x, 2), (eq, x, 3))({}))) == 2

            var goal2 = new EqGoal(x, 3);
            lst = new List<Goal>();
            lst.Add(goal1);
            lst.Add(goal2);
            dict = new Dictionary<object, object>();
            result = LogicSharp.logic_Any(lst, dict);
            Assert.IsInstanceOf(typeof(IEnumerable<KeyValuePair<object, object>>), result);
            Assert.IsInstanceOf(typeof(HashSet<KeyValuePair<object, object>>), result);
            var myHashSet = result as HashSet<KeyValuePair<object, object>>;
            Assert.NotNull(myHashSet);
            Assert.True(myHashSet.Count == 2);
     
            //assert len(tuple(lany(eq(x, 2), eq(x, 3))({x:2}))) == 1
            lst = new List<Goal>();
            lst.Add(goal1);
            lst.Add(goal2);
            dict = new Dictionary<object, object>();
            dict.Add(x,2);
            result = LogicSharp.logic_Any(lst, dict);
            Assert.IsNull(result);
        }
Exemple #41
0
        public void TestRun()
        {
            var x = new Var('x');
            var goal = new EqGoal(x, 3);

            object result = LogicSharp.Run(x, goal);
            Assert.NotNull(result);
            Assert.True(result.Equals(3));
        }
Exemple #42
0
        public void TestLogicConde()
        {
            /*            
             * x = var('x')
            assert results(conde([eq(x, 2)], [eq(x, 3)])) == ({x: 2}, {x: 3})
            assert results(conde([eq(x, 2), eq(x, 3)])) == ()
            */
            var x = new Var('x');
            var goal1 = new EqGoal(x, 2);
            var goal2 = new EqGoal(x, 3);
            var lst = new List<Goal>();
            lst.Add(goal1);
            var lst2 = new List<Goal>();
            lst2.Add(goal2);
            var lslst = new List<List<Goal>>();
            lslst.Add(lst);
            lslst.Add(lst2);

            var dict = new Dictionary<object, object>();
            object result = LogicSharp.logic_Conde(lslst,dict);
            Assert.IsInstanceOf(typeof(IEnumerable<KeyValuePair<object, object>>), result);
            Assert.IsInstanceOf(typeof(HashSet<KeyValuePair<object, object>>), result);
            var myHashSet = result as HashSet<KeyValuePair<object, object>>;
            Assert.NotNull(myHashSet);
            Assert.True(myHashSet.Count == 2);

            lst = new List<Goal>();
            lst.Add(goal1);
            lst.Add(goal2);
            lslst = new List<List<Goal>>();
            lslst.Add(lst);
            dict = new Dictionary<object, object>();
            result = LogicSharp.logic_Conde(lslst, dict);
            Assert.IsInstanceOf(typeof(IEnumerable<KeyValuePair<object, object>>), result);
            Assert.IsInstanceOf(typeof(HashSet<KeyValuePair<object, object>>), result);
            myHashSet = result as HashSet<KeyValuePair<object, object>>;
            Assert.IsEmpty(myHashSet); 
        }
Exemple #43
0
 public void RemoveGoal(EqGoal goal)
 {
     CachedGoals.RemoveWhere(pair => pair.Value.Equals(goal));
 }
 public void RemoveGoal(EqGoal goal)
 {
     CachedGoals.RemoveWhere(pair => pair.Value.Equals(goal));
 }
 public virtual void UndoGoal(EqGoal goal, object parent) { }
 public abstract bool UnifyExplicitProperty(EqGoal goal);
        public void Test_Unify()
        {
            var x = new Var("x");
            var pt1 = new Point(x, 2.0);
            var pt2 = new Point(3.0, 5.0);
            var ls = new LineSegment(pt1, pt2);
            var lss = new LineSegmentSymbol(ls);

            var d = new Var("d");
            var eqGoal = new EqGoal(d, 5); 
          
            object obj;
            bool result = lss.UnifyProperty(eqGoal, out obj);
            Assert.True(result);

            var lst = obj as List<object>;
            Assert.NotNull(lst);
            Assert.True(lst.Count == 2);
            var gGoal1 = lst[0] as EqGoal;
            Assert.NotNull(gGoal1);
            Assert.True(gGoal1.Lhs.ToString().Equals("x"));
            Assert.True(gGoal1.Rhs.ToString().Equals("7"));
            Assert.True(gGoal1.Traces.Count == 3);

            var gGoal2 = lst[1] as EqGoal;
            Assert.NotNull(gGoal2);
            Assert.True(gGoal2.Lhs.ToString().Equals("x"));
            Assert.True(gGoal2.Rhs.ToString().Equals("-1"));
           // Assert.True(gGoal2.Traces.Count == 1);
        }    
Exemple #48
0
        public void TestLogicAll()
        {
/*            x = var('x')
            assert results(lall((eq, x, 2))) == ({x: 2},)
            assert results(lall((eq, x, 2), (eq, x, 3))) == ()            */

            var x = new Var('x');
            var goal1 = new EqGoal(x, 2);
            var goal2 = new EqGoal(x, 3);

            var lst = new List<Goal>();
            lst.Add(goal1);
            lst.Add(goal2);
            var dict = new Dictionary<object, object>();
            object result = LogicSharp.logic_All(lst, dict);
            Assert.Null(result);

            /*assert results(lall((eq, x, 2), (eq, y, 3))) == ({x:2, y:3}) */
            var y = new Var('y');
            var goal3 = new EqGoal(y, 4);
            lst = new List<Goal>();
            lst.Add(goal1);
            lst.Add(goal3);
            dict = new Dictionary<object, object>();
            result = LogicSharp.logic_All(lst, dict);
            Assert.IsNotNull(result);
            Assert.IsInstanceOf(typeof(Dictionary<object, object>), result);
            var resultDict = result as Dictionary<object, object>;
            Assert.IsTrue(resultDict.Count == 2);
        }