コード例 #1
0
        public void TestLineSegment_Unify_Reify_0()
        {
            /*
            * Input sequence:
            * 1: (AB) [LineSegment]  => LineSegment
            * 2: A(2,3)          => Point
            * 3: B(2,3)          => Point
            * Update: AB [Label] => LineSegment
            */
             var graph = new RelationGraph();

             var ptA = new Point("A", 2, 3);
             var ptASym = new PointSymbol(ptA);
             graph.AddNode(ptASym); //api call
             Assert.True(graph.Nodes.Count == 1);

             var query = new Query(ShapeType.LineSegment);
             var queryNode = graph.AddNode(query) as QueryNode;
             Assert.Null(queryNode);

             var ptB = new Point("B", 3, 4);
             var PtBSym = new PointSymbol(ptB);
             graph.AddNode(PtBSym); //api call
             Assert.True(graph.Nodes.Count == 3);

             queryNode = graph.RetrieveQueryNode(query);
             Assert.NotNull(queryNode);
        }
コード例 #2
0
        /*
         * Annotated scaffolding
         *
         */ 
        public static void FromPointsToLineSegment(LineSegmentSymbol lss)
        {
            var ls = lss.Shape as LineSegment;
            Debug.Assert(ls != null);
            var pt1Symbol = new PointSymbol(ls.Pt1);
            var pt2Symbol = new PointSymbol(ls.Pt2);

            string strategy;

            TraceStep ts00 = new TraceStep(null, pt1Symbol, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(pt1Symbol));
            TraceStep ts01 = new TraceStep(null, pt2Symbol, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(pt2Symbol));

            TraceStep ts02 = null;
            if (ls.Pt1.Concrete && ls.Pt2.Concrete)
            {
                ts02 = new TraceStep(null, lss, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(lss));
            }
            strategy = "Plot a Line Segment passing through two points.";
            lss._innerLoop.Add(ts00);
            lss._innerLoop.Add(ts01);
            if (ts02 != null)
            {
                lss._innerLoop.Add(ts02);
            }
            lss.GenerateATrace(strategy);

/*            string stepMetaRule = "Consider draw a line segment passing through the two points.";
            string stepAppliedRule = String.Format("Draw the line segment passing through two points {0} and {1}",
                pt1Symbol, pt2Symbol);

            var traceStep = new TraceStep(null, lss, stepMetaRule, stepAppliedRule);
            lss._innerLoop.Add(traceStep);*/
            
        }
コード例 #3
0
 public void Test_CreateLine_2()
 {
     var pt1 = new Point(1.0, 2.0);
     var pt2 = new Point(1.0, 2.0);
     var ptSym1 = new PointSymbol(pt1);
     var ptSym2 = new PointSymbol(pt2);
     var lineSym = LineBinaryRelation.Unify(ptSym1, ptSym2);
     Assert.Null(lineSym);
 }
コード例 #4
0
        public void Problem16()
        {
            var pt1 = new Point(-1, 2);
            var ps1 = new PointSymbol(pt1);

            var pt2 = new Point(5, 8);
            var ps2 = new PointSymbol(pt2);

            var pt3 = new Point(2, 4);
            var midPoint = new PointSymbol(pt3);

            TraceInstructionalDesign.FromPointsToMidPoint(ps1, ps2, midPoint);
        }
コード例 #5
0
 public void Test_CreateLine_4()
 {
     var pt1 = new Point(2.0, 1.0);
     var pt2 = new Point(2.0, 2.0);
     var ptSym1 = new PointSymbol(pt1);
     var ptSym2 = new PointSymbol(pt2);
     var lineSym = LineBinaryRelation.Unify(ptSym1, ptSym2) as LineSymbol;
     Assert.NotNull(lineSym);
     var line = lineSym.Shape as Line;
     Assert.NotNull(line);
     Assert.True(line.A.Equals(1.0));
     Assert.True(line.B.Equals(0.0));
     Assert.True(line.C.Equals(-2.0));
 }
コード例 #6
0
        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);
        }
コード例 #7
0
        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;
        }
コード例 #8
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);
        }
コード例 #9
0
        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));*/
        }
コード例 #10
0
 public static LineSymbol Unify(EqGoal goal, PointSymbol pt)
 {
     return Unify(pt, goal);
 }
コード例 #11
0
ファイル: Scenario.cs プロジェクト: buptkang/Relation.Logic
        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"));
        }
コード例 #12
0
ファイル: Scenario.cs プロジェクト: buptkang/Relation.Logic
        public void TestScenario_02_WorkedExample_2()
        {
            //add three nodes in sequence
            //delete point A

            var graph = new RelationGraph();

            var pt1 = new Point("A", 2, 4);
            var pt1Symbol = new PointSymbol(pt1);
            var v = new Var("v");
            var pt2 = new Point("B", 5, v);
            var pt2Symbol = new PointSymbol(pt2);

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

            var d = new Var("d");
            var eqGoal = new EqGoal(d, 5);
            graph.AddNode(eqGoal);
            Assert.True(graph.Nodes.Count == 5);

            //Form a Cycle Directed Graph
            Assert.True(graph.FoundCycleInGraph());
            var ptNode = graph.RetrieveShapeNode(pt2Symbol);
            Assert.True(ptNode.InEdges.Count == 2);

            graph.DeleteNode(pt1Symbol);
            Assert.True(graph.Nodes.Count == 2);

            var pt2Node = graph.RetrieveShapeNode(pt2Symbol);
            Assert.NotNull(pt2Node);
            Assert.True(pt2Node.InEdges.Count == 0);
            Assert.True(pt2Node.OutEdges.Count == 0);
        }
コード例 #13
0
ファイル: Scenario.cs プロジェクト: buptkang/Relation.Logic
        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"));
        }
コード例 #14
0
ファイル: Scenario.cs プロジェクト: buptkang/Relation.Logic
 public void TestScenario_01_WorkedExample_2_0()
 {
     /*
      * 1: A(2,3) [Point] => Point
      * 2: B(3,4) [Point] => Point
      * 3: AB [Label]     => [Line, LineSegment]
      */
     var graph = new RelationGraph();
     var ptA = new Point("A", 2, 3);
     var ptASymbol = new PointSymbol(ptA);
     var ptB = new Point("B", 3, 4);
     var ptBSymbol = new PointSymbol(ptB);
     graph.AddNode(ptASymbol);
     graph.AddNode(ptBSymbol);
     const string label = "AB";
     var query = new Query(label);
     var qn = graph.AddNode(query);
     Assert.Null(qn);
     Assert.False(query.Success);
     Assert.NotNull(query.FeedBack);
     var types = query.FeedBack as List<ShapeType>;
     Assert.NotNull(types);
     Assert.True(types.Count == 2);
     var shapes = graph.RetrieveShapes();
     Assert.True(shapes.Count == 2);
 }
コード例 #15
0
ファイル: Scenario.cs プロジェクト: buptkang/Relation.Logic
        public void TestScenario_01_WorkedExample_2_2()
        {
            var pt1 = new Point("A", 2, 0);
            var pt1Symbol = new PointSymbol(pt1);
            var pt2 = new Point("B", 5, 4);
            var pt2Symbol = new PointSymbol(pt2);

            var graph = new RelationGraph();
            graph.AddNode(pt1Symbol);
            graph.AddNode(pt2Symbol);

            var query2 = new Query("AB", ShapeType.LineSegment);
            var queryNode2 = graph.AddNode(query2) as QueryNode;
            Assert.NotNull(queryNode2);
            Assert.True(queryNode2.Query.Success);
            Assert.True(queryNode2.Query.CachedEntities.Count == 1);
            var cachedLss = queryNode2.Query.CachedEntities.ToList()[0] as LineSegmentSymbol;
            Assert.NotNull(cachedLss);
            //Assert.True(cachedLss.Traces.Count == 0);

            var query = new Query("d"); // one constraint
            var queryNode = graph.AddNode(query) as QueryNode;
            Assert.NotNull(queryNode);
            Assert.True(queryNode.Query.Success);
            Assert.True(queryNode.Query.CachedEntities.Count == 1);

            var cachedGoal = queryNode.Query.CachedEntities.ToList()[0] as EqGoal;
            Assert.NotNull(cachedGoal);
            Assert.True(cachedGoal.ToString().Equals("d=5"));
            //Trace Check
            //Assert.True(cachedGoal.Traces.Count == 2);
        }
コード例 #16
0
ファイル: Test.Generator.cs プロジェクト: buptkang/MathCog
        public void Test_Point_1()
        {
            var point = new Point(1.0, 2.0);
            var pointSymbol = new PointSymbol(point);
            string str = pointSymbol.ToString();
            Assert.True(str.Equals("(1,2)"));
            Expr expr = pointSymbol.ToExpr();

            var str1 = expr.ToString();
            Assert.True(str.Equals("(1,2)"));
        }
コード例 #17
0
        public static void FromPointsToMidPoint(PointSymbol ps1, PointSymbol ps2, PointSymbol midPoint)
        {
            //1. plot the ps1, ps2 and the line
            //2. generate x coordinate
            //3. generate y coordinate
            //4. generate point (x,y)

            string strategy;

            var ts00 = new TraceStep(null, ps1, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(ps1));
            var ts01 = new TraceStep(null, ps2, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(ps2));
            var ls = LineBinaryRelation.Unify(ps1, ps2) as LineSymbol;
            var ts02 = new TraceStep(null, ls, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(ls));

            strategy = "Plot the given two points and the connected line.";
            midPoint._innerLoop.Add(ts00);
            midPoint._innerLoop.Add(ts01);
            midPoint._innerLoop.Add(ts02);
            midPoint.GenerateATrace(strategy);
            ////////////////////////////////////////////////

            var xCoord = new Var("x");
            var pt1 = ps1.Shape as Point;
            Debug.Assert(pt1 != null);
            var pt2 = ps2.Shape as Point;
            Debug.Assert(pt2 != null);

            var term1 = new Term(Expression.Add, new List<object>(){pt1.XCoordinate, pt2.XCoordinate});
            var term2 = new Term(Expression.Divide, new List<object>(){term1, 2});
            var eq = new Equation(xCoord, term2);

            /////////////////////////////////////////////////

            object obj;
            bool result = eq.IsEqGoal(out obj);
            EqGoal goal1 = null;
            if (result)
            {
                goal1 = obj as EqGoal;
                Debug.Assert(goal1!=null);
                Debug.Assert(goal1.Traces.Count == 1);
                var currentTrace = goal1.Traces.ToList()[0];
                strategy = "Generate x coordinate of midpoint.";
                var newTrace = new Tuple<object, object>(strategy, currentTrace.Item2);
                midPoint.Traces.Add(newTrace);           
            }

            //////////////////////////////////////////////////

            var yCoord = new Var("y");
            term1 = new Term(Expression.Add, new List<object>() { pt1.YCoordinate, pt2.YCoordinate });
            term2 = new Term(Expression.Divide, new List<object>() { term1, 2 });
            var eq1 = new Equation(yCoord, term2);

            result = eq1.IsEqGoal(out obj);
            EqGoal goal2 = null;
            if (result)
            {
                goal2 = obj as EqGoal;
                Debug.Assert(goal2 != null);
                Debug.Assert(goal2.Traces.Count == 1);
                var currentTrace1 = goal2.Traces.ToList()[0];
                strategy = "Generate y coordinate of midpoint.";
                var newTrace = new Tuple<object, object>(strategy, currentTrace1.Item2);
                midPoint.Traces.Add(newTrace);
            }

            //////////////////////////////////////////////////////

            var ps = new Point(xCoord, yCoord);
            var psym = new PointSymbol(ps);

            var mid = midPoint.Shape as Point;
            Debug.Assert(mid != null);
            var ps_inter1 = new Point(mid.XCoordinate, yCoord);
            var psym_inter1 = new PointSymbol(ps_inter1);
            var metaRule = "Consider substitute generate x coordinate into the point form.";
            var appliedRule = SubstitutionRule.ApplySubstitute(goal1, psym);

            var ts1 = new TraceStep(psym, psym_inter1, null, metaRule, appliedRule);
            metaRule = "Consider substitute generate y coordinate into the point form.";
            appliedRule = SubstitutionRule.ApplySubstitute(goal2, psym_inter1);
            var ts2 = new TraceStep(psym_inter1, midPoint, null, metaRule, appliedRule);

            strategy = "Generate point (x,y) by subsitute generated x and y.";
            midPoint._innerLoop.Add(ts1);
            midPoint._innerLoop.Add(ts2);            
            midPoint.GenerateATrace(strategy);
        }
コード例 #18
0
        /// <summary>
        /// construct a line through a point and a goal,
        /// e.g A(1,2) ^ S = 2=> Conjunctive Norm Form
        /// </summary>
        /// <param name="pt1"></param>
        /// <param name="goal"></param>
        /// <returns></returns>
        public static LineSymbol Unify(PointSymbol pt, EqGoal goal)
        {
            var variable1 = goal.Lhs as Var;
            Debug.Assert(variable1 != null);

            if(LineAcronym.EqualSlopeLabels(variable1.ToString()))
            {
                double dValue;
                bool result = LogicSharp.IsDouble(goal.Rhs, out dValue);
                if (result)
                {
                    if (!pt.Shape.Concrete) return null;
                    var line = LineGenerationRule.GenerateLine((Point)pt.Shape, dValue, null);
                    if (pt.Traces.Count != 0)
                    {
                        line.Traces.AddRange(pt.Traces);
                    }
                    if (goal.Traces.Count != 0)
                    {
                        line.Traces.AddRange(goal.Traces);
                    }

                    TraceInstructionalDesign.FromPointSlopeToLine(pt, goal, line);

                    line.OutputType = LineType.SlopeIntercept;
                    return line;
                }
                else
                {
                    var line = new Line(null); //ghost line
                    var ls = new LineSymbol(line);
                    ls.OutputType = LineType.SlopeIntercept;
                    return ls;
                }
            }
            return null;
        }
コード例 #19
0
        public static LineSegmentSymbol Unify(PointSymbol pt1, PointSymbol pt2)
        {
            //point identify check
            if (pt1.Equals(pt2))
            {
                return(null);
            }
            var point1 = pt1.Shape as Point;
            var point2 = pt2.Shape as Point;

            Debug.Assert(point1 != null);
            Debug.Assert(point2 != null);

            //lazy evaluation
            //Constraint solving on Graph
            var lineSeg = new LineSegment(null); //Ghost Line Segment

            lineSeg.Pt1 = point1;
            lineSeg.Pt2 = point2;
            var lss = new LineSegmentSymbol(lineSeg);

            //Line build process
            if (pt1.Shape.Concrete)
            {
                if (pt2.Shape.Concrete)
                {
                    return(LineSegmentGenerationRule.GenerateLineSegment(point1, point2));
                }

                if (pt2.CachedSymbols.Count != 0)
                {
                    foreach (ShapeSymbol ss in pt2.CachedSymbols)
                    {
                        var ps = ss as PointSymbol;
                        Debug.Assert(ps != null);
                        Debug.Assert(ps.Shape.Concrete);
                        var cachePoint = ps.Shape as Point;
                        Debug.Assert(cachePoint != null);
                        var gLss = LineSegmentGenerationRule.GenerateLineSegment(point1, cachePoint);
                        gLss.Traces.AddRange(ps.Traces);
                        lss.CachedSymbols.Add(gLss);
                    }
                }
                return(lss);
            }
            Debug.Assert(!pt1.Shape.Concrete);
            if (pt2.Shape.Concrete)
            {
                if (pt1.CachedSymbols.Count != 0)
                {
                    foreach (ShapeSymbol ss in pt1.CachedSymbols)
                    {
                        var ps = ss as PointSymbol;
                        Debug.Assert(ps != null);
                        Debug.Assert(ps.Shape.Concrete);
                        var cachePoint = ps.Shape as Point;
                        Debug.Assert(cachePoint != null);
                        var gLss = LineSegmentGenerationRule.GenerateLineSegment(cachePoint, point2);
                        gLss.Traces.AddRange(ps.Traces);
                        lss.CachedSymbols.Add(gLss);
                    }
                }
                return(lss);
            }
            Debug.Assert(!pt2.Shape.Concrete);

            foreach (ShapeSymbol ss1 in pt1.CachedSymbols)
            {
                foreach (ShapeSymbol ss2 in pt2.CachedSymbols)
                {
                    var ps1 = ss1 as PointSymbol;
                    Debug.Assert(ps1 != null);
                    Debug.Assert(ps1.Shape.Concrete);
                    var cachePoint1 = ps1.Shape as Point;
                    Debug.Assert(cachePoint1 != null);
                    var ps2 = ss2 as PointSymbol;
                    Debug.Assert(ps2 != null);
                    Debug.Assert(ps2.Shape.Concrete);
                    var cachePoint2 = ps2.Shape as Point;
                    Debug.Assert(cachePoint2 != null);
                    var gLss = LineSegmentGenerationRule.GenerateLineSegment(cachePoint1, cachePoint2);
                    gLss.Traces.AddRange(ps1.Traces);
                    gLss.Traces.AddRange(ps2.Traces);
                    lss.CachedSymbols.Add(gLss);
                }
            }
            return(lss);
        }
コード例 #20
0
        public void Test_CreateLine_7()
        {
            var m = new Var("m");
            var eqGoal = new EqGoal(m, 1);
            var goalNode = new GoalNode(eqGoal);
            var point1 = new Point(5, 8);
            var ps1 = new PointSymbol(point1);
            var psNode1 = new ShapeNode(ps1);

            object obj;
            bool value = RelationLogic.ConstraintCheck(psNode1, goalNode, "lineG", null, out obj);
            Assert.True(value);
            var ls = obj as LineSymbol;
            Assert.NotNull(ls);
            Assert.True(ls.Shape.Concrete);
            Assert.True(ls.ToString().Equals("x-y+3=0"));
        }
コード例 #21
0
        public void TestLine_Unify_Reify_2()
        {
            /*
             * (2,x), (3,4)
             * line pass through two points
             */

            var graph  = new RelationGraph();
            var x      = new Var('x');
            var point  = new Point(2, x);
            var ps     = new PointSymbol(point);
            var point1 = new Point(3, 4);
            var ps1    = new PointSymbol(point1);

            graph.AddNode(ps);
            graph.AddNode(ps1);

            var query     = new Query(ShapeType.Line);
            var queryNode = graph.AddNode(query) as QueryNode;

            Assert.NotNull(queryNode);
            Assert.True(queryNode.InternalNodes.Count == 1);
            Assert.True(query.Success);
            Assert.True(query.CachedEntities.Count == 1);
            var cachedLine = query.CachedEntities.ToList()[0] as LineSymbol;

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

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

            graph.AddNode(eqGoal);

            queryNode = graph.RetrieveQueryNode(query);
            Assert.NotNull(queryNode);
            Assert.True(queryNode.InternalNodes.Count == 1);
            Assert.True(query.Success);
            Assert.True(query.CachedEntities.Count == 1);
            cachedLine = query.CachedEntities.ToList()[0] as LineSymbol;
            Assert.NotNull(cachedLine);

            var eqGoal2 = new EqGoal(x, 2); // x=2

            graph.AddNode(eqGoal2);
            queryNode = graph.RetrieveQueryNode(query);
            Assert.NotNull(queryNode);
            Assert.True(queryNode.InternalNodes.Count == 1);
            Assert.True(query.Success);
            Assert.True(query.CachedEntities.Count == 2);
            cachedLine = query.CachedEntities.ToList()[0] as LineSymbol;
            Assert.NotNull(cachedLine);


/*            graph.DeleteNode(eqGoal2);
 *          queryNode = graph.RetrieveQueryNode(query);
 *          Assert.NotNull(queryNode);
 *          Assert.True(queryNode.InternalNodes.Count == 1);
 *          Assert.True(query.Success);
 *          Assert.True(query.CachedEntities.Count == 1);
 *          cachedLine = query.CachedEntities.ToList()[0] as LineSymbol;
 *          Assert.NotNull(cachedLine);
 *          Assert.True(cachedLine.CachedSymbols.Count == 1);
 *          Assert.True(graph.Nodes.Count == 4);
 *
 *          graph.DeleteNode(eqGoal);
 *          queryNode = graph.RetrieveQueryNode(query);
 *          Assert.NotNull(queryNode);
 *          Assert.True(queryNode.InternalNodes.Count == 1);
 *          Assert.True(query.Success);
 *          Assert.True(query.CachedEntities.Count == 1);
 *          cachedLine = query.CachedEntities.ToList()[0] as LineSymbol;
 *          Assert.NotNull(cachedLine);
 *          Assert.True(cachedLine.CachedSymbols.Count == 0);
 *          Assert.True(graph.Nodes.Count == 3);*/
        }
コード例 #22
0
        public void GenerateYCacheSymbol(object obj, EqGoal goal)
        {
            var point = Shape as Point;

            Debug.Assert(point != null);
            CachedGoals.Add(new KeyValuePair <object, EqGoal>(PointAcronym.Y, goal));
            if (CachedSymbols.Count == 0)
            {
                var gPoint       = new Point(point.Label, point.XCoordinate, obj);
                var gPointSymbol = new PointSymbol(gPoint);
                gPointSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(PointAcronym.Y, goal));
                CachedSymbols.Add(gPointSymbol);
                gPointSymbol.Traces.AddRange(goal.Traces);

                //transform goal trace
                for (int i = goal.Traces.Count - 1; i >= 0; i--)
                {
                    gPoint.Traces.Insert(0, goal.Traces[i]);
                }
                //Substitution trace
                var rule        = SubstitutionRule.ApplySubstitute();
                var appliedRule = SubstitutionRule.ApplySubstitute(this, goal);
                var ts          = new TraceStep(this, gPointSymbol, SubstitutionRule.SubstituteKC(), rule, appliedRule);
                gPointSymbol._innerLoop.Add(ts);


                string strategy = "Reify a Point's y-coordinate by substituing a given fact.";

                gPointSymbol.GenerateATrace(strategy);
                //gPoint.Traces.Insert(0, ts);
            }
            else
            {
                foreach (ShapeSymbol ss in CachedSymbols.ToList())
                {
                    var pt = ss.Shape as Point;
                    if (pt != null)
                    {
                        var yResult = LogicSharp.Reify(pt.YCoordinate, goal.ToDict());
                        if (!pt.YCoordinate.Equals(yResult))
                        {
                            var gPt          = new Point(pt.Label, pt.XCoordinate, pt.YCoordinate);
                            var gPointSymbol = new PointSymbol(gPt);
                            //substitute
                            pt.YCoordinate = yResult;
                            ss.CachedGoals.Add(new KeyValuePair <object, EqGoal>(PointAcronym.Y, goal));
                            gPointSymbol.Traces.AddRange(goal.Traces);
                            //transform goal trace
                            for (int i = goal.Traces.Count - 1; i >= 0; i--)
                            {
                                pt.Traces.Insert(0, goal.Traces[i]);
                            }
                            string rule        = SubstitutionRule.ApplySubstitute();
                            string appliedRule = SubstitutionRule.ApplySubstitute(this, goal);
                            var    ts          = new TraceStep(this, gPointSymbol, SubstitutionRule.SubstituteKC(), rule, appliedRule);
                            gPointSymbol._innerLoop.Add(ts);
                            string strategy = "Reify a Point's y-coordinate by substituing a given fact.";
                            gPointSymbol.GenerateATrace(strategy);
                            //pt.Traces.Insert(0, ts);
                        }
                        else
                        {
                            //generate
                            var gPoint       = new Point(pt.Label, pt.XCoordinate, obj);
                            var gPointSymbol = new PointSymbol(gPoint);
                            gPointSymbol.Traces.AddRange(goal.Traces);
                            gPointSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(PointAcronym.Y, goal));
                            foreach (KeyValuePair <object, EqGoal> pair in ss.CachedGoals)
                            {
                                if (pair.Key.Equals(PointAcronym.X))
                                {
                                    gPointSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(PointAcronym.X, pair.Value));
                                }
                            }
                            CachedSymbols.Add(gPointSymbol);
                            //substitute
                            //Add traces from pt to gPoint
                            for (int i = pt.Traces.Count - 1; i >= 0; i--)
                            {
                                gPoint.Traces.Insert(0, pt.Traces[i]);
                            }
                            //transform goal trace
                            for (int i = goal.Traces.Count - 1; i >= 0; i--)
                            {
                                gPoint.Traces.Insert(0, goal.Traces[i]);
                            }
                            var rule        = SubstitutionRule.ApplySubstitute();
                            var appliedRule = SubstitutionRule.ApplySubstitute(this, goal);
                            var ts          = new TraceStep(this, gPointSymbol, SubstitutionRule.SubstituteKC(), rule, appliedRule);

                            gPointSymbol._innerLoop.Add(ts);
                            string strategy = "Reify a Point's y-coordinate by substituing a given fact.";
                            gPointSymbol.GenerateATrace(strategy);
                            //gPoint.Traces.Insert(0, ts);
                        }
                    }
                }
            }
        }
コード例 #23
0
        private static bool ConstraintCheck(PointSymbol pt1, PointSymbol pt2,
            string constraint1, ShapeType constraint2, out object output)
        {
            output = null;
            Debug.Assert(constraint1 != null);

            var label = constraint1 as string;
            char[] charr = label.ToCharArray();
            if (charr.Length != 2) return false;
            string str1 = label.ToCharArray()[0].ToString(CultureInfo.InvariantCulture);
            string str2 = label.ToCharArray()[1].ToString(CultureInfo.InvariantCulture);
            string label1 = pt1.Shape.Label;
            string label2 = pt2.Shape.Label;

            if (label1 == null || label2 == null)
            {
                if (constraint2 == ShapeType.LineSegment)
                {
                    if (constraint1 == LineSegmentAcronym.GeneralForm)
                    {
                        output = LineSegBinaryRelation.Unify(pt1, pt2);
                        if (output != null) return true;
                        output = LineSegmentGenerationRule.IdentityPoints;
                        return false;
                    }
                    if (constraint1 == LineAcronym.GeneralForm1)
                    {
                        output = LineBinaryRelation.Unify(pt1, pt2);
                        if (output != null) return true;
                        output = LineGenerationRule.IdentityPoints;
                        return false;
                    }
                }
            }

            bool condition1 = label1.Equals(str1) && label2.Equals(str2);
            bool condition2 = label1.Equals(str2) && label2.Equals(str1);
            if (condition1 || condition2)
            {
                if (constraint2 == ShapeType.Line)
                {
                    output = LineBinaryRelation.Unify(pt1, pt2);
                    if (output != null) return true;
                    output = LineGenerationRule.IdentityPoints;
                    return false;
                }

                if (constraint2 == ShapeType.LineSegment)
                {
                    output = LineSegBinaryRelation.Unify(pt1, pt2);
                    if (output != null) return true;
                    output = LineSegmentGenerationRule.IdentityPoints;
                    return false;
                }
            }
            return false;
        }
コード例 #24
0
        /// <summary>
        /// construct a line through two points
        /// </summary>
        /// <param name="pt1"></param>
        /// <param name="pt2"></param>
        /// <returns></returns>
        public static object Unify(PointSymbol pt1, PointSymbol pt2, EqGoal goal = null)
        {
            //point identify check
            if (pt1.Equals(pt2)) return null;

            //Line 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 winPt1 = new System.Windows.Point((double) point1.XCoordinate, (double) point1.YCoordinate);
                var winPt2 = new System.Windows.Point((double) point2.XCoordinate, (double) point2.YCoordinate);

                var lineSymbol = LineGenerationRule.GenerateLine(point1, point2);

                if (lineSymbol == null) return null;

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

                line.Rel1 = winPt1;
                line.Rel2 = winPt2;

                TraceInstructionalDesign.FromPointPointToLine(pt1, pt2, lineSymbol);
                return lineSymbol;
            }
            else
            {
                //lazy evaluation    
                //Constraint solving on Graph
                var line = new Line(null); //ghost line
                line.Rel1 = pt1.Shape;
                line.Rel2 = pt2.Shape;
                var ls =  new LineSymbol(line);

                #region Reification Purpose

                if (pt1.Shape.Concrete && !pt2.Shape.Concrete)
                {
                    var point1 = pt1.Shape as Point;
                    if (pt2.CachedSymbols.Count != 0)
                    {
                        foreach (ShapeSymbol ss in pt2.CachedSymbols)
                        {
                            var ps = ss as PointSymbol;
                            Debug.Assert(ps != null);
                            Debug.Assert(ps.Shape.Concrete);
                            var cachePoint = ps.Shape as Point;
                            Debug.Assert(cachePoint != null);
                            var gline = LineGenerationRule.GenerateLine(point1, cachePoint);
                            gline.Traces.AddRange(ps.Traces);
                            TraceInstructionalDesign.FromPointPointToLine(pt1, pt2, gline);
                            ls.CachedSymbols.Add(gline);
                        }
                    }
                }

                if (!pt1.Shape.Concrete && pt2.Shape.Concrete)
                {
                    var point2 = pt2.Shape as Point;
                    if (pt1.CachedSymbols.Count != 0)
                    {
                        foreach (ShapeSymbol ss in pt1.CachedSymbols)
                        {
                            var ps = ss as PointSymbol;
                            Debug.Assert(ps != null);
                            Debug.Assert(ps.Shape.Concrete);
                            var cachePoint = ps.Shape as Point;
                            Debug.Assert(cachePoint != null);
                            var gline = LineGenerationRule.GenerateLine(cachePoint, point2);
                            gline.Traces.AddRange(ps.Traces);
                            TraceInstructionalDesign.FromPointPointToLine(pt1, pt2, gline);
                            ls.CachedSymbols.Add(gline);
                        }
                    }
                }

                if (!pt1.Shape.Concrete && !pt2.Shape.Concrete)
                {
                    foreach (ShapeSymbol ss1 in pt1.CachedSymbols)
                    {
                        foreach (ShapeSymbol ss2 in pt2.CachedSymbols)
                        {
                            var ps1 = ss1 as PointSymbol;
                            Debug.Assert(ps1 != null);
                            Debug.Assert(ps1.Shape.Concrete);
                            var cachePoint1 = ps1.Shape as Point;
                            Debug.Assert(cachePoint1 != null);
                            var ps2 = ss2 as PointSymbol;
                            Debug.Assert(ps2 != null);
                            Debug.Assert(ps2.Shape.Concrete);
                            var cachePoint2 = ps2.Shape as Point;
                            Debug.Assert(cachePoint2 != null);
                            var gline = LineGenerationRule.GenerateLine(cachePoint1, cachePoint2);
                            gline.Traces.AddRange(ps1.Traces);
                            gline.Traces.AddRange(ps2.Traces);
                            TraceInstructionalDesign.FromPointPointToLine(pt1, pt2, gline);
                            ls.CachedSymbols.Add(gline);
                        }
                    }
                }

                #endregion

                if (goal != null)
                {
                    return ls.Unify(goal);
                }

                return ls;
            }
        }
コード例 #25
0
        public void GenerateYCacheSymbol(object obj, EqGoal goal)
        {
            var point = Shape as Point;
            Debug.Assert(point != null);
            CachedGoals.Add(new KeyValuePair<object, EqGoal>(PointAcronym.Y, goal));
            if (CachedSymbols.Count == 0)
            {
                var gPoint = new Point(point.Label, point.XCoordinate, obj);
                var gPointSymbol = new PointSymbol(gPoint);
                gPointSymbol.CachedGoals.Add(new KeyValuePair<object, EqGoal>(PointAcronym.Y, goal));
                CachedSymbols.Add(gPointSymbol);
                gPointSymbol.Traces.AddRange(goal.Traces);

                //transform goal trace
                for (int i = goal.Traces.Count - 1; i >= 0; i--)
                {
                    gPoint.Traces.Insert(0, goal.Traces[i]);
                }
                //Substitution trace
                var rule        = SubstitutionRule.ApplySubstitute();
                var appliedRule = SubstitutionRule.ApplySubstitute(this, goal);
                var ts = new TraceStep(this, gPointSymbol, SubstitutionRule.SubstituteKC(), rule, appliedRule);
                gPointSymbol._innerLoop.Add(ts);


                string strategy = "Reify a Point's y-coordinate by substituing a given fact.";

                gPointSymbol.GenerateATrace(strategy);
                //gPoint.Traces.Insert(0, ts);
            }
            else
            {
                foreach (ShapeSymbol ss in CachedSymbols.ToList())
                {
                    var pt = ss.Shape as Point;
                    if (pt != null)
                    {
                        var yResult = LogicSharp.Reify(pt.YCoordinate, goal.ToDict());
                        if (!pt.YCoordinate.Equals(yResult))
                        {
                            var gPt = new Point(pt.Label, pt.XCoordinate, pt.YCoordinate);
                            var gPointSymbol = new PointSymbol(gPt);
                            //substitute
                            pt.YCoordinate = yResult;
                            ss.CachedGoals.Add(new KeyValuePair<object, EqGoal>(PointAcronym.Y, goal));
                            gPointSymbol.Traces.AddRange(goal.Traces);
                            //transform goal trace
                            for (int i = goal.Traces.Count - 1; i >= 0; i--)
                            {
                                pt.Traces.Insert(0, goal.Traces[i]);
                            }
                            string rule        = SubstitutionRule.ApplySubstitute();
                            string appliedRule = SubstitutionRule.ApplySubstitute(this, goal);
                            var ts = new TraceStep(this, gPointSymbol, SubstitutionRule.SubstituteKC(), rule, appliedRule);
                            gPointSymbol._innerLoop.Add(ts);
                            string strategy = "Reify a Point's y-coordinate by substituing a given fact.";
                            gPointSymbol.GenerateATrace(strategy);
                            //pt.Traces.Insert(0, ts);
                        }
                        else
                        {
                            //generate
                            var gPoint = new Point(pt.Label, pt.XCoordinate, obj);
                            var gPointSymbol = new PointSymbol(gPoint);
                            gPointSymbol.Traces.AddRange(goal.Traces);
                            gPointSymbol.CachedGoals.Add(new KeyValuePair<object, EqGoal>(PointAcronym.Y, goal));
                            foreach (KeyValuePair<object, EqGoal> pair in ss.CachedGoals)
                            {
                                if (pair.Key.Equals(PointAcronym.X))
                                {
                                    gPointSymbol.CachedGoals.Add(new KeyValuePair<object, EqGoal>(PointAcronym.X, pair.Value));
                                }
                            }
                            CachedSymbols.Add(gPointSymbol);
                            //substitute
                            //Add traces from pt to gPoint
                            for (int i = pt.Traces.Count - 1; i >= 0; i--)
                            {
                                gPoint.Traces.Insert(0, pt.Traces[i]);
                            }
                            //transform goal trace
                            for (int i = goal.Traces.Count - 1; i >= 0; i--)
                            {
                                gPoint.Traces.Insert(0, goal.Traces[i]);
                            }
                            var rule = SubstitutionRule.ApplySubstitute();
                            var appliedRule = SubstitutionRule.ApplySubstitute(this, goal);
                            var ts = new TraceStep(this, gPointSymbol, SubstitutionRule.SubstituteKC(), rule, appliedRule);

                            gPointSymbol._innerLoop.Add(ts);
                            string strategy = "Reify a Point's y-coordinate by substituing a given fact.";
                            gPointSymbol.GenerateATrace(strategy);
                            //gPoint.Traces.Insert(0, ts);
                        }
                    }
                }
            }
        }
コード例 #26
0
 public static bool Unify(this PointSymbol shapeSymbol, object constraint, out object output)
 {
     output = shapeSymbol.Unify(constraint);
     return(output != null);
 }
コード例 #27
0
ファイル: Scenario.cs プロジェクト: buptkang/Relation.Logic
        public void TestScenario_01_WorkedExample_2_1()
        {
            /*
               * 1: A(2,3) [Point] => Point
               * 2: B(3,4) [Point] => Point
               * 3: AB [Label]     => [Line, LineSegment]
             */
            var graph = new RelationGraph();
            var ptA = new Point("A", 2, 3);
            var ptASymbol = new PointSymbol(ptA);
            var ptB = new Point("B", 3, 4);
            var ptBSymbol = new PointSymbol(ptB);
            graph.AddNode(ptASymbol);
            graph.AddNode(ptBSymbol);
            var query = new Query(ShapeType.Line); //deterministic
            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("x-y+1=0"));
        }
コード例 #28
0
        public static void LineSlopeIntercepToGraph(LineSymbol ls)
        {
            string strategy = strategy_graphing;

            //Plotting shapes
            //1. plotting Y-Intercept
            //2. plotting X-Intercept
            //3. plotting the line

            //////////////////////////////////////////////////////////////
            // Step 1:
            var pt    = new Point(0, ls.SymIntercept);
            var ptSym = new PointSymbol(pt);

            var ts0 = new TraceStep(null, ptSym, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(ptSym));

            ls._innerLoop.Add(ts0);
            //////////////////////////////////////////////////////////////
            // Step 2:

            var line = ls.Shape as Line;

            Debug.Assert(line != null);

            Equation eq;

            if (line.B == null)
            {
            }
            else
            {
                var x = new Var("x");
                //step 2.1
                var term = new Term(Expression.Multiply, new List <object>()
                {
                    line.Slope, x
                });
                var term1 = new Term(Expression.Add, new List <object>()
                {
                    term, line.Intercept
                });
                eq = new Equation(term1, 0);
                object obj;
                EqGoal gGoal  = null;
                bool   result = eq.IsEqGoal(out obj);
                if (result)
                {
                    gGoal = obj as EqGoal;
                }
                if (gGoal != null)
                {
                    double dX;
                    LogicSharp.IsDouble(gGoal.Rhs, out dX);
                    var pt1    = new Point(dX, 0);
                    var ptSym1 = new PointSymbol(pt1);
                    var ts1    = new TraceStep(null, ptSym1, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(ptSym1));
                    ls._innerLoop.Add(ts1);
                }
            }

            /////////////////////////////////////////////////////////////////

            const string step1MetaRule    = "Given the line slope-intercept form y=mx+b, plot the line by passing points (0,b) and (-b/m,0).";
            string       step1AppliedRule = String.Format("Plotting the line passing through (0,{0}) and ({1},0) ", ls.SymIntercept, ls.SymC);
            //var ts = new TraceStep(null, ls.SlopeInterceptForm, step1MetaRule, step1AppliedRule);


            string kc = GeometryScaffold.KC_LineGraphing;

            var ts = new TraceStep(null, ls, kc, step1MetaRule, step1AppliedRule);

            ls._innerLoop.Add(ts);

            //////////////////////////////////////////////////////////////////

            ls.GenerateATrace(strategy);
        }
コード例 #29
0
ファイル: Scenario.cs プロジェクト: buptkang/Relation.Logic
        public void TestScenario_02_WorkedExample_0()
        {
            //add three nodes in sequence

            var graph = new RelationGraph();
            var pt1 = new Point("A", 2, 4);
            var pt1Symbol = new PointSymbol(pt1);
            var v = new Var("v");
            var pt2 = new Point("B", 5, v);
            var pt2Symbol = new PointSymbol(pt2);

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

            var d = new Var("d");
            var eqGoal = new EqGoal(d, 5);
            graph.AddNode(eqGoal);

            Assert.True(graph.Nodes.Count == 5);

            //Form a Cycle Directed Graph
            Assert.True(graph.FoundCycleInGraph());
            var ptNode = graph.RetrieveShapeNode(pt2Symbol);
            Assert.True(ptNode.InEdges.Count == 2);

            Assert.True(pt2Symbol.CachedSymbols.Count == 2);

            var query = new Query("v");
            var queryNode = graph.AddNode(query) as QueryNode;
            Assert.NotNull(queryNode);
            Assert.True(query.Success);
            Assert.True(query.CachedEntities.Count == 2);
            var gGoal1 = query.CachedEntities.ToList()[0] as EqGoal;
            Assert.NotNull(gGoal1);

            var query2 = new Query("AB");
            var queryNode2 = graph.AddNode(query2) as QueryNode;
            Assert.Null(queryNode2);
            Assert.False(query2.Success);

            query2.Constraint2 = ShapeType.LineSegment;
            queryNode2 = graph.AddNode(query2) as QueryNode;
            Assert.NotNull(queryNode2);
            Assert.True(query2.Success);
            Assert.True(query2.CachedEntities.ToList().Count == 2);

            /* query2.Constraint2 = ShapeType.Line;
             queryNode2 = graph.AddNode(query2) as QueryNode;
             Assert.NotNull(queryNode2);
             Assert.True(query2.Success);
             Assert.True(query2.CachedEntities.ToList().Count == 2);*/

            //Instructional Scaffolding Verify (Trace Generation)

            //1. check v=0's trace
            Assert.True(gGoal1.Traces.Count == 3);
            //2. check point trace
            Assert.True(pt2Symbol.CachedSymbols.Count == 2);
            var cachedPt1 = pt2Symbol.CachedSymbols.ToList()[0];
            Assert.NotNull(cachedPt1);
            Assert.True(cachedPt1.Traces.Count == 4);
            var cachedPt2 = pt2Symbol.CachedSymbols.ToList()[1];
            Assert.NotNull(cachedPt2);
            Assert.True(cachedPt2.Traces.Count == 4);
            //3. check AB's plotting trace
            var cachedLineSeg1 = query2.CachedEntities.ToList()[0] as LineSegmentSymbol;
            Assert.NotNull(cachedLineSeg1);
            //Assert.True(cachedLineSeg1.Traces.Count == 3);
        }
コード例 #30
0
        public static void FromPointSlopeToLine(PointSymbol ps, EqGoal goal, LineSymbol ls)
        {
            string strategy = "Substitute point and slope property into line form.";

            // 1. Substitute slope property into the line slope-intercept form.
            // 2. Calculate the line intercept by substituting the point into line pattern.

            //////////////////////////////////////////////////////////////////////

            string strategy1 = "Substitute slope property into the line slope-intercept form.";

            var m    = new Var("m");
            var k    = new Var("k");
            var x    = new Var("x");
            var y    = new Var("y");
            var term = new Term(Expression.Multiply, new List <object>()
            {
                m, x
            });
            var term1 = new Term(Expression.Add, new List <object>()
            {
                term, k
            });
            var eqPattern = new Equation(y, term1);

            var term2 = new Term(Expression.Multiply, new List <object>()
            {
                goal.Rhs, x
            });
            var term3 = new Term(Expression.Add, new List <object>()
            {
                term2, k
            });
            var eqInternal1 = new Equation(y, term3);

            var appliedRule1 = SubstitutionRule.ApplySubstitute(eqPattern, goal);

            var ts0 = new TraceStep(eqPattern, eqInternal1, SubstitutionRule.SubstituteKC(), strategy1, appliedRule1);

            ls._innerLoop.Add(ts0);

            //////////////////////////////////////////////////////////////////////

            var point = ps.Shape as Point;

            Debug.Assert(point != null);

            string strategy2 = "Calculate the line intercept by substituting the point into line pattern.";

            var term4 = new Term(Expression.Multiply, new List <object>()
            {
                goal.Rhs, point.XCoordinate
            });
            var term5 = new Term(Expression.Add, new List <object>()
            {
                term4, k
            });
            var eqinternal2 = new Equation(point.YCoordinate, term5);

            object obj;
            bool   result = eqinternal2.IsEqGoal(out obj);
            var    eqGoal = obj as EqGoal;

            Debug.Assert(eqGoal != null);

            var gTuple = eqGoal.Traces[0];
            var gLst   = gTuple.Item2 as List <TraceStep>;

            Debug.Assert(gLst != null);
            ls._innerLoop.AddRange(gLst);

            var appliedRule2 = SubstitutionRule.ApplySubstitute(eqInternal1, ps);

            var ts = new TraceStep(eqInternal1, ls, SubstitutionRule.SubstituteKC(), strategy2, appliedRule2);

            ls._innerLoop.Add(ts);
            ls.GenerateATrace(strategy);
        }
コード例 #31
0
ファイル: Scenario.cs プロジェクト: buptkang/Relation.Logic
        public void TestScenario2_Relation_1()
        {
            /*
			 * 1: A(2,3) [Point] => Point
			 * 2: B(3,4) [Point] => Point
			 * 3: AB [Label]     => [Line, LineSegment]
			 * 4: ask slope = ?
			 */
            var graph = new RelationGraph();
            var ptA = new Point("A", 2, 3);
            var ptASymbol = new PointSymbol(ptA);
            var ptB = new Point("B", 3, 4);
            var ptBSymbol = new PointSymbol(ptB);
            graph.AddNode(ptASymbol);
            graph.AddNode(ptBSymbol);
            const string label = "AB";
            var query = new Query(label, 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("x-y+1=0"));
            var shapes = graph.RetrieveShapeNodes();
            Assert.True(shapes.Count == 3);

            var variable = new Var('m');
            var query1 = new Query(variable); //m=
            qn = graph.AddNode(query1) as QueryNode;
            Assert.NotNull(qn);
            Assert.NotNull(qn.Query);
            Assert.True(query.Success);
            Assert.Null(query.FeedBack);
            Assert.True(qn.InternalNodes.Count == 1);
            var gn = qn.InternalNodes[0] as GoalNode;
            Assert.NotNull(gn);
            var eqGoal = gn.Goal as EqGoal;
            Assert.NotNull(eqGoal);
            Assert.True(eqGoal.Lhs.Equals(variable));
            Assert.True(eqGoal.Rhs.Equals(1));

            shapes = graph.RetrieveShapeNodes();
            Assert.True(shapes.Count == 3);
        }
コード例 #32
0
 public static LineSymbol Unify(EqGoal goal, PointSymbol pt)
 {
     return(Unify(pt, goal));
 }
コード例 #33
0
ファイル: Scenario.cs プロジェクト: buptkang/Relation.Logic
        public void TestScenario_06_WorkedExample_3()
        {
            var graph = new RelationGraph();
            var pt1 = new Point(2, 3);
            var pt1Symbol = new PointSymbol(pt1);
            graph.AddNode(pt1Symbol);

            var query2 = new Query("lineG");
            var queryNode2 = graph.AddNode(query2) as QueryNode;
            Assert.Null(queryNode2);
            Assert.False(query2.Success);

            var y = new Var("y");
            var pt2 = new Point(4, y);
            var pt2Symbol = new PointSymbol(pt2);

            graph.AddNode(pt2Symbol);

            var m = new Var("m");
            var eqGoal = new EqGoal(m, 5);
            graph.AddNode(eqGoal);
            Assert.True(graph.Nodes.Count == 5);

            Assert.True(graph.FoundCycleInGraph());
            var ptNode = graph.RetrieveShapeNode(pt2Symbol);
            Assert.True(ptNode.InEdges.Count == 1);
            Assert.True(pt2Symbol.CachedSymbols.Count == 1);

            Assert.True(query2.Success);
            Assert.True(query2.CachedEntities.Count == 1);
            var cachedLine = query2.CachedEntities.ToList()[0] as LineSymbol;
            Assert.NotNull(cachedLine);
            Assert.True(cachedLine.ToString().Equals("5x-y-7=0"));
        }
コード例 #34
0
        /// <summary>
        /// construct a line through two points
        /// </summary>
        /// <param name="pt1"></param>
        /// <param name="pt2"></param>
        /// <returns></returns>
        public static object Unify(PointSymbol pt1, PointSymbol pt2, EqGoal goal = null)
        {
            //point identify check
            if (pt1.Equals(pt2))
            {
                return(null);
            }

            //Line 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 winPt1 = new System.Windows.Point((double)point1.XCoordinate, (double)point1.YCoordinate);
                var winPt2 = new System.Windows.Point((double)point2.XCoordinate, (double)point2.YCoordinate);

                var lineSymbol = LineGenerationRule.GenerateLine(point1, point2);

                if (lineSymbol == null)
                {
                    return(null);
                }

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

                line.Rel1 = winPt1;
                line.Rel2 = winPt2;

                TraceInstructionalDesign.FromPointPointToLine(pt1, pt2, lineSymbol);
                return(lineSymbol);
            }
            else
            {
                //lazy evaluation
                //Constraint solving on Graph
                var line = new Line(null); //ghost line
                line.Rel1 = pt1.Shape;
                line.Rel2 = pt2.Shape;
                var ls = new LineSymbol(line);

                #region Reification Purpose

                if (pt1.Shape.Concrete && !pt2.Shape.Concrete)
                {
                    var point1 = pt1.Shape as Point;
                    if (pt2.CachedSymbols.Count != 0)
                    {
                        foreach (ShapeSymbol ss in pt2.CachedSymbols)
                        {
                            var ps = ss as PointSymbol;
                            Debug.Assert(ps != null);
                            Debug.Assert(ps.Shape.Concrete);
                            var cachePoint = ps.Shape as Point;
                            Debug.Assert(cachePoint != null);
                            var gline = LineGenerationRule.GenerateLine(point1, cachePoint);
                            gline.Traces.AddRange(ps.Traces);
                            TraceInstructionalDesign.FromPointPointToLine(pt1, pt2, gline);
                            ls.CachedSymbols.Add(gline);
                        }
                    }
                }

                if (!pt1.Shape.Concrete && pt2.Shape.Concrete)
                {
                    var point2 = pt2.Shape as Point;
                    if (pt1.CachedSymbols.Count != 0)
                    {
                        foreach (ShapeSymbol ss in pt1.CachedSymbols)
                        {
                            var ps = ss as PointSymbol;
                            Debug.Assert(ps != null);
                            Debug.Assert(ps.Shape.Concrete);
                            var cachePoint = ps.Shape as Point;
                            Debug.Assert(cachePoint != null);
                            var gline = LineGenerationRule.GenerateLine(cachePoint, point2);
                            gline.Traces.AddRange(ps.Traces);
                            TraceInstructionalDesign.FromPointPointToLine(pt1, pt2, gline);
                            ls.CachedSymbols.Add(gline);
                        }
                    }
                }

                if (!pt1.Shape.Concrete && !pt2.Shape.Concrete)
                {
                    foreach (ShapeSymbol ss1 in pt1.CachedSymbols)
                    {
                        foreach (ShapeSymbol ss2 in pt2.CachedSymbols)
                        {
                            var ps1 = ss1 as PointSymbol;
                            Debug.Assert(ps1 != null);
                            Debug.Assert(ps1.Shape.Concrete);
                            var cachePoint1 = ps1.Shape as Point;
                            Debug.Assert(cachePoint1 != null);
                            var ps2 = ss2 as PointSymbol;
                            Debug.Assert(ps2 != null);
                            Debug.Assert(ps2.Shape.Concrete);
                            var cachePoint2 = ps2.Shape as Point;
                            Debug.Assert(cachePoint2 != null);
                            var gline = LineGenerationRule.GenerateLine(cachePoint1, cachePoint2);
                            gline.Traces.AddRange(ps1.Traces);
                            gline.Traces.AddRange(ps2.Traces);
                            TraceInstructionalDesign.FromPointPointToLine(pt1, pt2, gline);
                            ls.CachedSymbols.Add(gline);
                        }
                    }
                }

                #endregion

                if (goal != null)
                {
                    return(ls.Unify(goal));
                }

                return(ls);
            }
        }
コード例 #35
0
ファイル: Scenario.cs プロジェクト: buptkang/Relation.Logic
        public void TestScenario_01_WorkedExample_1()
        {
            var pt1 = new Point("A", 2, 0);
            var pt1Symbol = new PointSymbol(pt1);
            var pt2 = new Point("B", 5, 4);
            var pt2Symbol = new PointSymbol(pt2);

            var graph = new RelationGraph();
            graph.AddNode(pt1Symbol);
            graph.AddNode(pt2Symbol);

            var query = new Query("d"); // one constraint
            var queryNode = graph.AddNode(query) as QueryNode;
            Assert.NotNull(queryNode);            
            Assert.True(queryNode.Query.Success);
            Assert.True(queryNode.Query.CachedEntities.Count == 1);

            var cachedGoal = queryNode.Query.CachedEntities.ToList()[0] as EqGoal;
            Assert.NotNull(cachedGoal);
            Assert.True(cachedGoal.ToString().Equals("d=5"));
            //Trace Check
          /*  Assert.True(cachedGoal.Traces.Count == 3);
            Assert.True(cachedGoal._innerLoop.Count == 0);*/
        }
コード例 #36
0
        public void Test_CreateLine_6()
        {
            var point = new Point(-1, 2);
            var ps = new PointSymbol(point);
            var psNode = new ShapeNode(ps);
            var point1 = new Point(5, 8);
            var ps1 = new PointSymbol(point1);
            var psNode1 = new ShapeNode(ps1);

            object obj;
            bool value = RelationLogic.ConstraintCheck(psNode, psNode1, null, ShapeType.Line, out obj);
            Assert.True(value);
            var ls = obj as LineSymbol;
            Assert.NotNull(ls);
            Assert.True(ls.Shape.Concrete);
            Assert.True(ls.ToString().Equals("x-y+3=0"));
        }
コード例 #37
0
        public void TestPoint_Unify_Reify_0()
        {
            var graph = new RelationGraph();
            //true positive
            var x     = new Var('x');
            var y     = new Var('y');
            var point = new Point(x, y);
            var ps    = new PointSymbol(point);

            graph.AddNode(ps);
            Assert.True(graph.Nodes.Count == 1);

            /*
             *
             * Current Status:
             *
             * (x,y)
             *
             * next input:
             *
             * x=1
             */
            var eqGoal = new EqGoal(x, 1); // x=1

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

            List <ShapeSymbol> shapes = graph.RetrieveShapeSymbols();

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

            Assert.NotNull(pt);
            Assert.True(pt.Equals(ps));
            Assert.True(pt.CachedGoals.Count == 1);
            Assert.True(pt.CachedSymbols.Count == 1);

            var gPointSymbol = pt.CachedSymbols.ToList()[0] as PointSymbol;

            Assert.NotNull(gPointSymbol);
            var gPoint = gPointSymbol.Shape as Point;

            Assert.NotNull(gPoint);
            Assert.False(gPoint.Concrete);
            Assert.True(1.0.Equals(gPoint.XCoordinate));
            Assert.True(y.Equals(gPoint.YCoordinate));

            /******
             * current status:
             * (1,y)
             *
             * next input:
             * x = 2
             ****/
            #region Block 2
            var eqGoal1 = new EqGoal(x, 2); // x=2
            graph.AddNode(eqGoal1);
            shapes = graph.RetrieveShapeSymbols();
            Assert.True(shapes.Count == 1);
            pt = shapes[0] as PointSymbol;
            Assert.NotNull(pt);
            Assert.True(pt.Equals(ps));
            Assert.True(pt.CachedGoals.Count == 2);
            Assert.True(pt.CachedSymbols.Count == 2);
            Assert.False(point.Concrete);
            #endregion

            /******
             * current status:
             * (1,y)
             * (2,y)
             *
             * next input:
             * y = 1
             ****/
            #region Block 3
            var eqGoal2 = new EqGoal(y, 1); // y = 1
            graph.AddNode(eqGoal2);
            shapes = graph.RetrieveShapeSymbols();
            Assert.True(shapes.Count == 1);
            pt = shapes[0] as PointSymbol;
            Assert.False(point.Concrete);
            Assert.True(ps.CachedGoals.Count == 3);
            Assert.True(ps.CachedSymbols.Count == 2);
            foreach (var ss in ps.CachedSymbols)
            {
                Assert.True(ss.Shape.Concrete);
            }

            var goals = graph.RetrieveGoals();
            Assert.True(goals.Count == 3);
            #endregion

            /******
             * current status:
             * (1,1)
             * (2,1)
             *
             * next input:
             * y = 2
             ****/

            #region Block 4
            var eqGoal3 = new EqGoal(y, 2); // y = 2
            graph.AddNode(eqGoal3);

            shapes = graph.RetrieveShapeSymbols();
            Assert.True(shapes.Count == 1);
            ps = shapes[0] as PointSymbol;
            Assert.NotNull(ps);
            Assert.False(ps.Shape.Concrete);
            Assert.True(ps.CachedGoals.Count == 4);
            Assert.True(ps.CachedSymbols.Count == 4);
            foreach (var css in ps.CachedSymbols)
            {
                Assert.True(css.Shape.Concrete);
            }
            #endregion

            /*
             *  current status:
             *  (1,1), (2,1), (1,2), (2,2)
             *
             * next input:
             * delete y = 2
             *
             */

            #region Block 5
            graph.DeleteNode(eqGoal3);
            shapes = graph.RetrieveShapeSymbols();
            Assert.True(shapes.Count == 1);
            pt = shapes[0] as PointSymbol;
            Assert.NotNull(pt);
            Assert.False(pt.Shape.Concrete);
            Assert.True(pt.CachedGoals.Count == 3);
            Assert.True(pt.CachedSymbols.Count == 2);
            foreach (var ss in pt.CachedSymbols)
            {
                Assert.True(ss.Shape.Concrete);
            }

            goals = graph.RetrieveGoals();
            Assert.True(goals.Count == 3);

            #endregion

            /*
             *  current status:
             *  (1,1), (2,1)
             *
             *  next input:
             *  delete y = 1
             *
             */

            #region Block 6
            graph.DeleteNode(eqGoal2);
            shapes = graph.RetrieveShapeSymbols();
            Assert.True(shapes.Count == 1);
            pt = shapes[0] as PointSymbol;
            Assert.NotNull(pt);
            Assert.False(pt.Shape.Concrete);
            Assert.True(pt.CachedGoals.Count == 3);
            Assert.True(pt.CachedSymbols.Count == 2);

            #endregion

            /*            foreach (var shape in pt.CachedSymbols)
             * {
             *  Assert.False(shape.Shape.Concrete);
             * }
             * goals = graph.RetrieveGoals();
             * Assert.True(goals.Count == 2);
             *
             *
             * /////////////////////////////////////////////
             *
             * graph.DeleteNode(ps);
             * shapes = graph.RetrieveShapeSymbols();
             * Assert.True(shapes.Count == 0);*/
        }
コード例 #38
0
        private static bool ConstraintCheck(PointSymbol pt1, PointSymbol pt2,
                object constraint, out object output)
        {
            output = null;
            Debug.Assert(constraint != null);
            var shapeType = constraint as ShapeType?;
            if (shapeType != null)
            {
                #region ShapeType Constraint Solving
                if (shapeType == ShapeType.Line)
                {
                    output = LineBinaryRelation.Unify(pt1, pt2);
                    if (output != null) return true;
                    output = LineGenerationRule.IdentityPoints;
                    return false;
                }

                if (shapeType == ShapeType.LineSegment)
                {
                    output = LineSegBinaryRelation.Unify(pt1, pt2);
                    if (output != null) return true;
                    output = LineSegmentGenerationRule.IdentityPoints;
                    return false;
                }

                return false;
                #endregion
            }
            var label = constraint as string;
            if (label != null)
            {
                #region Label Constraint Solving

                if (PointAcronym.MidPoint.Equals(label))
                {
                    //Point
                    output = PointBinaryRelation.Unify(pt1, pt2);
                    var ps = output as PointSymbol;
                    if (ps != null)
                    {
                        return true;
                    }
                    return false;
                }

                #region Line Inference

                //Case 2
                if (LineAcronym.EqualGeneralFormLabels(label))
                {
                    output = LineBinaryRelation.Unify(pt1, pt2);
                    var ls = output as LineSymbol;
                    if (ls != null)
                    {
                        ls.OutputType = LineType.GeneralForm;
                        return true;
                    }
                    return false;
                }

                if (LineAcronym.EqualSlopeLabels(label))
                {
                    output = LineBinaryRelation.Unify(pt1, pt2);
                    if (output == null) return false;
                    var lss = output as LineSymbol;
                    //TraceInstructionalDesign.FromPointsToLine(lss);
                    var output1 = lss.Unify(label);
                    output = output1;
                    return true;
                }

                if (LineAcronym.EqualInterceptLabels(label))
                {
                    output = LineBinaryRelation.Unify(pt1, pt2);
                    if (output == null) return false;
                    var lss = output as LineSymbol;
                    //TraceInstructionalDesign.FromPointsToLine(lss);
                    var output1 = lss.Unify(label);
                    output = output1;
                    return true;
                }

                if (LineAcronym.EqualSlopeInterceptFormLabels(label))
                {
                    output = LineBinaryRelation.Unify(pt1, pt2);
                    var ls = output as LineSymbol;
                    if (ls != null)
                    {
                        ls.OutputType = LineType.SlopeIntercept;
                        return true;
                    }
                    return false;
                }

                #endregion

                #region Line Segment Inference

                if (LineSegmentAcronym.EqualDistanceLabel(label))
                {
                    output = LineSegBinaryRelation.Unify(pt1, pt2);
                    if (output == null) return false;
                    var lss = output as LineSegmentSymbol;
                    var output1 = lss.Unify(label);
                    output = output1;
                    #region Embed Line Segment(obsolete)
                    /*
                    Debug.Assert(lss != null);
                    if (lss.CachedSymbols.Count == 0)
                    {
                        output = output1;
                        var lst = new List<object>()
                        {
                            output, output1
                        };

                        output = lst;
                    }
                    else
                    {
                        var goalLst = output1 as List<EqGoal>;
                        Debug.Assert(goalLst != null);
                        var lst = new List<object>();
                        for (int i = 0; i < lss.CachedSymbols.Count; i++)
                        {
                            var cachedSS = lss.CachedSymbols.ToList()[i];
                            lst.Add(cachedSS);
                            lst.Add(goalLst[i]);
                        }
                        output = lst;
                    }*/

                    #endregion
                    return true;
                }

                #endregion

                #region ambiguious inference

                //Case 1
                char[] charr = label.ToCharArray();
                if (charr.Length != 2) return false;
                string str1 = label.ToCharArray()[0].ToString(CultureInfo.InvariantCulture);
                string str2 = label.ToCharArray()[1].ToString(CultureInfo.InvariantCulture);
                string label1 = pt1.Shape.Label;
                string label2 = pt2.Shape.Label;
                if (label1 == null || label2 == null) return false;
                bool condition1 = label1.Equals(str1) && label2.Equals(str2);
                bool condition2 = label1.Equals(str2) && label2.Equals(str1);
                if (condition1 || condition2)
                {
                    var supportTypes = new List<ShapeType> { ShapeType.Line, ShapeType.LineSegment };
                    output = supportTypes;
                    return false;
                }

                #endregion

                #endregion
            }

            var eqGoal = constraint as EqGoal;
            if (eqGoal != null)
            {
                var goalLabel = eqGoal.Lhs.ToString();

                #region Line Segment Inference

                if (LineSegmentAcronym.EqualDistanceLabel(goalLabel))
                {
                    output = LineSegBinaryRelation.Unify(pt1, pt2);
                    if (output == null) return false;
                    var lss = output as LineSegmentSymbol;
                    //TraceInstructionalDesign.FromPointsToLineSegment(lss);
                    output = lss.Unify(eqGoal);
                    if (output == null) return false;
                    return true;
                }

                if (LineAcronym.EqualSlopeLabels(goalLabel))
                {
                    output = LineBinaryRelation.Unify(pt1, pt2, eqGoal);
                    if (output == null) return false;
                    return true;
                }

                #endregion
            }

            return false;
        }
コード例 #39
0
        public void TestScenario_02_WorkedExample_0()
        {
            //add three nodes in sequence

            var graph     = new RelationGraph();
            var pt1       = new Point("A", 2, 4);
            var pt1Symbol = new PointSymbol(pt1);
            var v         = new Var("v");
            var pt2       = new Point("B", 5, v);
            var pt2Symbol = new PointSymbol(pt2);

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

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

            graph.AddNode(eqGoal);

            Assert.True(graph.Nodes.Count == 5);

            //Form a Cycle Directed Graph
            Assert.True(graph.FoundCycleInGraph());
            var ptNode = graph.RetrieveShapeNode(pt2Symbol);

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

            Assert.True(pt2Symbol.CachedSymbols.Count == 2);

            var query     = new Query("v");
            var queryNode = graph.AddNode(query) as QueryNode;

            Assert.NotNull(queryNode);
            Assert.True(query.Success);
            Assert.True(query.CachedEntities.Count == 2);
            var gGoal1 = query.CachedEntities.ToList()[0] as EqGoal;

            Assert.NotNull(gGoal1);

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

            Assert.Null(queryNode2);
            Assert.False(query2.Success);

            query2.Constraint2 = ShapeType.LineSegment;
            queryNode2         = graph.AddNode(query2) as QueryNode;
            Assert.NotNull(queryNode2);
            Assert.True(query2.Success);
            Assert.True(query2.CachedEntities.ToList().Count == 2);

            /* query2.Constraint2 = ShapeType.Line;
             * queryNode2 = graph.AddNode(query2) as QueryNode;
             * Assert.NotNull(queryNode2);
             * Assert.True(query2.Success);
             * Assert.True(query2.CachedEntities.ToList().Count == 2);*/

            //Instructional Scaffolding Verify (Trace Generation)

            //1. check v=0's trace
            Assert.True(gGoal1.Traces.Count == 3);
            //2. check point trace
            Assert.True(pt2Symbol.CachedSymbols.Count == 2);
            var cachedPt1 = pt2Symbol.CachedSymbols.ToList()[0];

            Assert.NotNull(cachedPt1);
            Assert.True(cachedPt1.Traces.Count == 4);
            var cachedPt2 = pt2Symbol.CachedSymbols.ToList()[1];

            Assert.NotNull(cachedPt2);
            Assert.True(cachedPt2.Traces.Count == 4);
            //3. check AB's plotting trace
            var cachedLineSeg1 = query2.CachedEntities.ToList()[0] as LineSegmentSymbol;

            Assert.NotNull(cachedLineSeg1);
            //Assert.True(cachedLineSeg1.Traces.Count == 3);
        }
コード例 #40
0
 public void Test_Relation_Point_Line_2()
 {
     var pt = new Point(0, -3);
     var ps = new PointSymbol(pt);
     var line = new Line(null, 4, 1, 4);
     var ls = new LineSymbol(line);
     bool result = ls.UnifyShape(ps);
     Assert.False(result);
 }