public void TestScenario_28_WorkedExample_0()
        {
            var graph     = new RelationGraph();
            var y         = new Var("y");
            var pt1       = new Point("A", 2, y);
            var pt1Symbol = new PointSymbol(pt1);
            var pt2       = new Point("B", -1, 4);
            var pt2Symbol = new PointSymbol(pt2);

            graph.AddNode(pt1Symbol);
            graph.AddNode(pt2Symbol);
            var eqGoal = new EqGoal(y, -1);

            graph.AddNode(eqGoal);

            Assert.True(pt1Symbol.CachedSymbols.Count == 1);
            var ptNode = graph.RetrieveShapeNode(pt1Symbol);

            Assert.True(ptNode.InEdges.Count == 1);

            var query2     = new Query("d");
            var queryNode2 = graph.AddNode(query2) as QueryNode;

            Assert.NotNull(queryNode2);
            Assert.True(query2.Success);
            Assert.True(query2.CachedEntities.Count == 1);
            var cachedGoal = query2.CachedEntities.ToList()[0] as EqGoal;

            Assert.NotNull(cachedGoal);
            Assert.True(cachedGoal.ToString().Equals("d=5.83"));
        }
        public void TestScenario_06_WorkedExample_2()
        {
            var graph     = new RelationGraph();
            var pt1       = new Point(2, 3);
            var pt1Symbol = new PointSymbol(pt1);
            var y         = new Var("y");
            var pt2       = new Point(4, y);
            var pt2Symbol = new PointSymbol(pt2);

            graph.AddNode(pt1Symbol);
            graph.AddNode(pt2Symbol);

            var m      = new Var("m");
            var eqGoal = new EqGoal(m, 5);

            graph.AddNode(eqGoal);
            Assert.True(graph.Nodes.Count == 4);

            Assert.True(graph.FoundCycleInGraph());
            var ptNode = graph.RetrieveShapeNode(pt2Symbol);

            Assert.True(ptNode.InEdges.Count == 1);
            Assert.True(pt2Symbol.CachedSymbols.Count == 1);

            var query3     = new Query("k");
            var queryNode3 = graph.AddNode(query3) as QueryNode;

            Assert.NotNull(queryNode3);
            Assert.True(query3.Success);
            Assert.True(query3.CachedEntities.Count == 1);
            var cachedGoal = query3.CachedEntities.ToList()[0] as EqGoal;

            Assert.NotNull(cachedGoal);
            Assert.True(cachedGoal.ToString().Equals("k=-7"));
        }
        //forward solving
        private static object InferDistance(this LineSegmentSymbol inputLineSymbol, string label)
        {
            var lineSeg = inputLineSymbol.Shape as LineSegment;

            Debug.Assert(lineSeg != null);

            if (label != null && lineSeg.Distance != null)
            {
                var goal = new EqGoal(new Var(label), lineSeg.Distance);
                TraceInstructionalDesign.FromLineSegmentToDistance(inputLineSymbol);
                goal.Traces.AddRange(inputLineSymbol.Traces);
                return(goal);
            }
            if (label != null && inputLineSymbol.CachedSymbols.Count != 0)
            {
                var goalList = new List <object>();
                foreach (var lss in inputLineSymbol.CachedSymbols)
                {
                    var cachedLss = lss as LineSegmentSymbol;
                    Debug.Assert(cachedLss != null);
                    var cachedLs = cachedLss.Shape as LineSegment;
                    Debug.Assert(cachedLs != null);
                    var goal = new EqGoal(new Var(label), cachedLs.Distance);
                    //goal.Traces.AddRange(cachedLss.Traces);
                    TraceInstructionalDesign.FromLineSegmentToDistance(cachedLss);
                    goal.Traces.AddRange(cachedLss.Traces);
                    goalList.Add(goal);
                }
                return(goalList);
            }
            return(null);
        }
Exemple #4
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);
        }
Exemple #5
0
        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);
        }
        public void TestScenario3_CSP_3()
        {
            var graph   = new RelationGraph();
            var m       = new Var('m');
            var k       = new Var('k');
            var eqGoal1 = new EqGoal(m, 3); //m=3
            var eqGoal2 = new EqGoal(k, 2); //k=2

            graph.AddNode(eqGoal1);
            graph.AddNode(eqGoal2);
            var query = new Query(ShapeType.Line);
            var qn    = graph.AddNode(query) as QueryNode;

            Assert.NotNull(qn);
            Assert.NotNull(qn.Query);
            Assert.True(qn.Query.Equals(query));
            Assert.True(query.Success);
            Assert.Null(query.FeedBack);
            Assert.True(qn.InternalNodes.Count == 1);
            var sn = qn.InternalNodes[0] as ShapeNode;

            Assert.NotNull(sn);
            var ls = sn.ShapeSymbol as LineSymbol;

            Assert.NotNull(ls);
            Assert.True(ls.ToString().Equals("y=3x+2"));

            //Output Usage
            Assert.True(query.CachedEntities.Count == 1);
            ls = query.CachedEntities.ToList()[0] as LineSymbol;
            Assert.NotNull(ls);
            Assert.True(ls.ToString().Equals("y=3x+2"));
        }
Exemple #7
0
        private static object InferIntercept(this LineSymbol inputLineSymbol, string label)
        {
            var line = inputLineSymbol.Shape as Line;

            Debug.Assert(line != null);
            if (line.Intercept != null)
            {
                var goal = new EqGoal(new Var(label), line.Intercept);
                goal.Traces.AddRange(inputLineSymbol.Traces);
                TraceInstructionalDesign.FromLineToIntercept(inputLineSymbol, goal);
                return(goal);
            }

            if (inputLineSymbol.CachedSymbols.Count != 0)
            {
                var goalList = new List <object>();
                foreach (var lss in inputLineSymbol.CachedSymbols)
                {
                    var cachedLss = lss as LineSymbol;
                    Debug.Assert(cachedLss != null);
                    var cachedLs = cachedLss.Shape as Line;
                    Debug.Assert(cachedLs != null);
                    var goal = new EqGoal(new Var(label), cachedLs.Intercept);
                    goal.Traces.AddRange(cachedLss.Traces);
                    TraceInstructionalDesign.FromLineToIntercept(cachedLss, goal);
                    goalList.Add(goal);
                }
                return(goalList);
            }

            return(null);
        }
Exemple #8
0
        public void Test_UnReify_1_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);

            graph.DeleteNode(eqGoal);
            Assert.True(graph.Nodes.Count == 1);

            var eqNode = graph.Nodes[0] as EquationNode;

            Assert.NotNull(eqNode);
            Assert.True(eqNode.Equation.CachedEntities.Count == 0);
        }
Exemple #9
0
        public void TestLine_Unify_Reify_SequenceUncertainty_0()
        {
            var graph  = new RelationGraph();
            var a      = new Var('a');
            var eqGoal = new EqGoal(a, 1); // a=1

            graph.AddNode(eqGoal);

            var line = new Line(1, a, 1.0);
            var ls   = new LineSymbol(line);

            graph.AddNode(ls);

            List <ShapeSymbol> 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));
        }
Exemple #10
0
        /*
         * given m=2, k=3, y=3x+2
         */
        public static void FromSlopeInterceptToLineSlopeIntercept(EqGoal slopeGoal, EqGoal interceptGoal, LineSymbol ls)
        {
            //1. Substitute slope and intercept properties into the line slope-intercept form y=mx+b.
            ////////////////////////////////////////////////////////


            var ts0 = new TraceStep(null, slopeGoal, GeometryScaffold.KC_LineSlopeForm, PlottingRule.PlottingStrategy, PlottingRule.Plot(slopeGoal));
            var ts1 = new TraceStep(null, interceptGoal, GeometryScaffold.KC_LineInterceptForm, PlottingRule.PlottingStrategy, PlottingRule.Plot(interceptGoal));

            ls._innerLoop.Add(ts0);

            var abstractLs  = new Line(ls.Shape.Label, slopeGoal.Lhs, interceptGoal.Lhs);
            var abstractLss = new LineSymbol(abstractLs);
            var internalLs  = new Line(ls.Shape.Label, ls.SymSlope, interceptGoal.Lhs);
            var internalLss = new LineSymbol(internalLs);

            var traceStep1 = new TraceStep(abstractLss, internalLss, SubstitutionRule.SubstituteKC(), SubstitutionRule.SubstitutionStrategy, SubstitutionRule.ApplySubstitute(abstractLss, slopeGoal));

            ls._innerLoop.Add(traceStep1);

            ls._innerLoop.Add(ts1);

            /*
             *          var rule = "Substitute given property to line slope-intercept form.";
             *          var appliedRule1 = string.Format("Substitute slope={0} into y=mx+b", ls.SymSlope);
             *          var appliedRule2= string.Format("Substitute intercept={0} into y=mx+b", ls.SymIntercept);*/
            var traceStep2 = new TraceStep(internalLss, ls, SubstitutionRule.SubstituteKC(), SubstitutionRule.SubstitutionStrategy, SubstitutionRule.ApplySubstitute(internalLss, interceptGoal));

            ls._innerLoop.Add(traceStep2);

            string strategy = "Substitute slope and intercept properties into the line slope-intercept form y = mx + b.";

            ls.GenerateATrace(strategy);
        }
        /// <summary>
        /// for generated shapes
        /// </summary>
        /// <param name="goal"></param>
        /// <param name="parent"></param>
        public override void UndoGoal(EqGoal goal, object p)
        {
            var parent = p as Point;

            if (parent == null)
            {
                return;
            }

            var current = Shape as Point;

            Debug.Assert(current != null);

            string field = null;

            foreach (KeyValuePair <object, EqGoal> eg in CachedGoals)
            {
                if (eg.Value.Equals(goal))
                {
                    field = eg.Key.ToString();
                }
            }

            if (PointAcronym.X.Equals(field))
            {
                current.XCoordinate = parent.XCoordinate;
            }
            else if (PointAcronym.Y.Equals(field))
            {
                current.YCoordinate = parent.YCoordinate;
            }

            this.RemoveGoal(goal);
        }
Exemple #12
0
        /// <summary>
        /// Pattern for Coordinate, such as "Y=3", "x=4.0"
        /// </summary>
        /// <param name="expr"></param>
        /// <param name="coord"></param>
        /// <returns></returns>
        public static bool IsTerm(this starPadSDK.MathExpr.Expr expr,
                                  out object coord)
        {
            coord = null;
            var compExpr = expr as CompositeExpr;

            if (compExpr != null &&
                compExpr.Head.Equals(WellKnownSym.equals) &&
                compExpr.Args.Count() == 2)
            {
                var expr1 = compExpr.Args[0] as starPadSDK.MathExpr.Expr;
                var expr2 = compExpr.Args[1] as starPadSDK.MathExpr.Expr;

                object label;
                object number;
                if (expr1.IsLabel(out label) && expr2.IsNumeric(out number))
                {
                    //coord = new KeyValuePair<object, object>(label, number);
                    //var goal = new EqGoal();
                    //coord = new AGPropertyExpr(expr, new EqGoal(new Var(label), number));
                    coord = new EqGoal(new Var(label), number);
                    return(true);
                }
                else if (expr1.IsNumeric(out number) && expr2.IsLabel(out label))
                {
                    //coord = new KeyValuePair<object, object>(label,number);
                    //coord = new EqGoal(new Var(label), number);\
                    coord = new EqGoal(new Var(label), number);
                    return(true);
                }
            }

            return(false);
        }
 private bool DispatchUnreify(Equation eq, EqGoal goal)
 {
     if (eq == null)
     {
         return(false);
     }
     return(eq.UnReify(goal));
 }
Exemple #14
0
        public static Expr Generate(EqGoal goal)
        {
            var head = WellKnownSym.equals;
            var lhs  = Generate(goal.Lhs);
            var rhs  = Generate(goal.Rhs);

            Debug.Assert(lhs != null && rhs != null);
            return(new CompositeExpr(head, new Expr[] { lhs, rhs }));
        }
        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);
        }
Exemple #16
0
 public override bool UnifyProperty(EqGoal goal, out object obj)
 {
     obj = this.Unify(goal);
     if (obj == null)
     {
         return(false);
     }
     return(true);
 }
Exemple #17
0
        public void TestLine_Unify_Reify_1()
        {
            var graph = new RelationGraph();

            var a     = new Var('a');
            var point = new Point(2, a);
            var ps    = new PointSymbol(point);

            graph.AddNode(ps);
            var line = new Line(1, a, 1.0);
            var ls   = new LineSymbol(line);

            graph.AddNode(ls);

            List <ShapeSymbol> points = graph.RetrieveShapeSymbols(ShapeType.Point);

            Assert.True(points.Count == 1);
            var pt = points[0] as PointSymbol;

            Assert.NotNull(pt);
            Assert.True(pt.CachedSymbols.Count == 0);

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

            graph.AddNode(eqGoal);
            points = graph.RetrieveShapeSymbols(ShapeType.Point);
            Assert.True(points.Count == 1);
            pt = points[0] as PointSymbol;
            Assert.NotNull(pt);
            Assert.True(pt.CachedSymbols.Count == 1);
            var cachedPs = pt.CachedSymbols.ToList()[0] as PointSymbol;

            Assert.NotNull(cachedPs);
            var cachedPt = cachedPs.Shape as Point;

            Assert.NotNull(cachedPt);
            Assert.True(cachedPt.XCoordinate.Equals(2.0));
            Assert.True(cachedPt.YCoordinate.Equals(1.0));

            var 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));
        }
Exemple #18
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 #19
0
        private QueryNode CreateQueryNode(EqGoal goal)
        {
            var query     = new Query(goal.Lhs);
            var queryNode = new QueryNode(query);

            var iGoalNode = new GoalNode(goal);

            queryNode.InternalNodes.Add(iGoalNode);
            return(queryNode);
        }
Exemple #20
0
        public bool RelationExist(EqGoal goal)
        {
            object relations;

            ConstraintSatisfy(goal, out relations);
            var lst = relations as List <Tuple <object, object> >;

            Debug.Assert(lst != null);
            return(lst.Count != 0);
        }
Exemple #21
0
        //forward solving
        private static EqGoal InferSlope(this LineSymbol inputLineSymbol, string label)
        {
            var line = inputLineSymbol.Shape as Line;

            Debug.Assert(line != null);
            var goal = new EqGoal(new Var(label), line.Slope);

            goal.Traces.AddRange(inputLineSymbol.Traces);
            TraceInstructionalDesign.FromLineToSlope(inputLineSymbol, goal);
            return(goal);
        }
Exemple #22
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 #23
0
        /// <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);
        }
        public void Test_reify_1()
        {
            var  x      = new Var('x');
            var  y      = new Var('y');
            var  point  = new Point(x, y);
            var  ps     = new PointSymbol(point);
            var  t      = new Var('t');
            var  eqGoal = new EqGoal(t, 1); // t=1
            bool result = ps.Reify(eqGoal);

            Assert.False(result);
        }
Exemple #25
0
        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);
        }
        public void Test_CreateLine_TwoGoal_1()
        {
            var m     = new Var('m');
            var k     = new Var('k');
            var goal1 = new EqGoal(m, 2.0);
            var goal2 = new EqGoal(k, 1.0);

            var lineSym = LineBinaryRelation.Unify(goal1, goal2);

            Assert.NotNull(lineSym);

            Assert.True(lineSym.SymSlope.Equals("2"));
            Assert.True(lineSym.SymIntercept.Equals("1"));
        }
        public void Test_Sub_1()
        {
            var x     = new Var('x');
            var y     = new Var('y');
            var point = new Point(x, y);
            var ps    = new PointSymbol(point);
            var goal1 = new EqGoal(x, -3);
            var goal2 = new EqGoal(y, -4);

            ps.Reify(goal1);
            ps.Reify(goal2);
            Assert.True(ps.CachedGoals.Count == 2);
            Assert.True(ps.CachedSymbols.Count == 1);
        }
        private bool DispatchUnreify(ShapeSymbol ss, EqGoal goal)
        {
            var ps = ss as PointSymbol;
            var ls = ss as LineSymbol;

            if (ps != null)
            {
                return(ps.UnReify(goal));
            }
            if (ls != null)
            {
                return(ls.UnReify(goal));
            }
            return(false);
        }
Exemple #29
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 void Test_Unreify_0()
        {
            //true positive
            var  x      = new Var('x');
            var  y      = new Var('y');
            var  point  = new Point(x, y);
            var  ps     = new PointSymbol(point);
            var  eqGoal = new EqGoal(x, 1); // x=1
            bool result = ps.Reify(eqGoal);

            result = ps.UnReify(eqGoal);
            Assert.True(result);
            Assert.False(point.Concrete);
            Assert.True(ps.CachedGoals.Count == 0);
            Assert.True(ps.CachedSymbols.Count == 0);
        }