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);
        }
 // Lazy evaluation of relation based on the existing nodes,
 /// if the relation exists, build the relation.
 public bool ConstraintSatisfy(Query query, out object obj)
 {
     obj = null;
     object refObj = query.Constraint1;
     ShapeType? st = query.Constraint2;
     if (refObj == null && st == null) throw new Exception("TODO: brute search to create all relations.");
     bool result = false;
     if (refObj == null) result = ConstraintSatisfy(null, st, out obj);
     else
     {
         var term = refObj as Term;
         if (term != null)
         {
             result = ConstraintSatisfy(term, out obj);
         }
         else
         {
             result = ConstraintSatisfy(refObj.ToString(), st, out obj);
         }
     }
     return result;
 }
Example #3
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);
        }
Example #4
0
        public static bool IsQuery(this starPadSDK.MathExpr.Expr expr, out object property)
        {
            property = null;
            if (!(expr is CompositeExpr)) return false;
            var composite = expr as CompositeExpr;
            if (!composite.Head.Equals(WellKnownSym.equals)) return false;

            if (composite.Args.Length == 1)
            {
                var expr1 = composite.Args[0];

                object obj;
                var result = expr1.IsLabel(out obj);
                if (result)
                {
                    //property = new KeyValuePair<string, object>("Label", new Var(obj));
                    property = new Query(new Var(obj));
                    return true;
                }
                result = expr1.IsExpression(out obj);
                if (result)
                {
                    //TODO
                    //property = new KeyValuePair<string, object>("Term", obj);
                    property = new Query(obj);
                    return true;
                }
                return false;
            }
            else if (composite.Args.Length == 2)
            {
                var expr1 = composite.Args[0];
                var expr2 = composite.Args[1];

                if(expr2 is ErrorExpr)
                {
                    object obj;
                    var result = expr1.IsLabel(out obj);
                    if (result)
                    {
                        //property = new KeyValuePair<string, object>("Label", new Var(obj));
                        property = new Query(new Var(obj));
                        return true;
                    }

                    result = expr1.IsExpression(out obj);
                    if (result)
                    {
                        //property = new KeyValuePair<string, object>("Term", obj);
                        property = new Query(obj, null);
                        return true;
                    }
                    return false;
                }
                else
                {
                    return false;
                }
            }
            return false;
        }
Example #5
0
 /// <summary>
 /// Relation Input Pattern Match
 /// </summary>
 /// <param name="expr"></param>
 /// <param name="str"></param>
 /// <param name="st"></param>
 /// <param name="query"></param>
 /// <param name="output"></param>
 /// <returns></returns>
 private bool EvalExprPatterns(Expr expr, Query query, out object output, bool userInput = false)
 {
     object obj = null;
     if (!userInput)
     {
         obj = RelationGraph.AddNode(query);
     }
     output = new AGQueryExpr(expr, query);
     return obj != null;
     //return EvalNonDeterministic(expr, obj, out output);
 }
Example #6
0
        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"));
        }
Example #7
0
        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"));
        }
Example #8
0
        public void TestScenario_04_WorkedExample_1()
        {
            var graph = new RelationGraph();

            var m = new Var("m");
            var eqGoal1 = new EqGoal(m, 3);
            var k = new Var("k");
            var eqGoal2 = new EqGoal(k, 2);

            graph.AddNode(eqGoal1);
            graph.AddNode(eqGoal2);

            var query = new Query("lineG");
            var queryNode = graph.AddNode(query) as QueryNode;
            Assert.NotNull(queryNode);
            Assert.True(query.Success);
            Assert.True(query.CachedEntities.Count == 1);
            var cachedLine = query.CachedEntities.ToList()[0] as LineSymbol;
            Assert.NotNull(cachedLine);
            var lt = cachedLine.Shape.GetInputType();
            Assert.NotNull(lt);
            Assert.True(lt.Equals(LineType.Relation));
            Assert.True(cachedLine.ToString().Equals("3x-y+2=0"));
            Assert.True(cachedLine.Traces.Count == 2);

            var query1 = new Query("lineS");
            var queryNode1 = graph.AddNode(query1) as QueryNode;
            Assert.NotNull(queryNode1);
            Assert.True(query1.Success);
            Assert.True(query1.CachedEntities.Count == 1);
            var cachedLine1 = query1.CachedEntities.ToList()[0] as LineSymbol;
            Assert.NotNull(cachedLine1);
            Assert.True(cachedLine1.ToString().Equals("y=3x+2"));

            Assert.True(cachedLine1.Traces.Count == 1);
        }
Example #9
0
        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"));
        }
        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;
        }
Example #11
0
 private void RemoveQueryFromCache(Query query)
 {
     object keyObj = null;
     foreach (KeyValuePair<object, object> pair in _preCache)
     {
         if (pair.Key is Guid)
         {
             Debug.Assert(pair.Key != null);
             var tempGuid = (Guid)pair.Key;
             if (tempGuid.CompareTo(query.QueryQuid) == 0)
             {
                 keyObj = pair.Key;
                 break;
             }
         }
     }
     if (keyObj != null) _preCache.Remove(query.QueryQuid);
 }
Example #12
0
        private bool DeleteQueryNode(Query query)
        {
            RemoveQueryFromCache(query);
            query.PropertyChanged -= query_PropertyChanged;
            var queryNode = SearchNode(query) as QueryNode;
            if (queryNode == null) return false;

            foreach (GraphNode gn in queryNode.InternalNodes)
            {
                foreach (GraphEdge inGe in gn.InEdges)
                {
                    var sourceNode = inGe.Source;
                    sourceNode.OutEdges.Remove(inGe);
                }
                foreach (GraphEdge outGe in gn.OutEdges)
                {
                    var targetNode = outGe.Target;
                    targetNode.InEdges.Remove(outGe);
                }
            }
            query.CachedEntities.Clear();
            _nodes.Remove(queryNode);
            return true;
        }
Example #13
0
        private bool AddQueryNode(Query query, out object obj)
        {
            //1. build relation using geometry constraint-solving
            //2. Reification

            ResetNodeRelatedFlag();
            bool result = ConstraintSatisfy(query, out obj);

            if (result)
            {
                obj = CreateQueryNode(query, obj);
                query.Success = true;
            }
            else
            {
                if (obj != null)
                {
                    query.Success = false;
                    query.FeedBack = obj;
                }
            }

            if (!result) return false;
            var qn = obj as QueryNode;
            _nodes.Add(qn);
            return true;
        }
Example #14
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);*/

        }
Example #15
0
        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);
        }
 private QueryNode CreateQueryNode(List<EqGoal> goals)
 {
     var query = new Query(goals[0].Lhs);
     var queryNode = new QueryNode(query);
     foreach (EqGoal igoal in goals)
     {
         var iGoalNode = new GoalNode(igoal);
         queryNode.InternalNodes.Add(iGoalNode);
     }
     return queryNode;
 }
Example #17
0
 public void TestScenario3_CSP_2()
 {
     /*
      * Input sequence:
      * 1. 2x + y + 1 = 0
      * 2: m = ?
      */
     var graph = new RelationGraph();
     var line = new Line(2, 1, 1);
     var lineSymbol = new LineSymbol(line);
     Assert.True(line.InputType == LineType.GeneralForm);
     graph.AddNode(lineSymbol);
     var variable = new Var('m');
     var query = new Query(variable);
     var qn = graph.AddNode(query) as QueryNode;
     Assert.True(qn != null);
     Assert.True(qn.InternalNodes.Count == 1);
     var goalNode = qn.InternalNodes[0] as GoalNode;
     Assert.NotNull(goalNode);
     var eqGoal = goalNode.Goal as EqGoal;
     Assert.NotNull(eqGoal);
     Assert.True(eqGoal.Rhs.Equals(-2));
     Assert.True(eqGoal.Lhs.Equals(variable));
     //Output Usage
     Assert.True(query.CachedEntities.Count == 1);
     var cachedGoal = query.CachedEntities.ToList()[0] as EqGoal;
     Assert.NotNull(cachedGoal);
     Assert.True(cachedGoal.Rhs.Equals(-2));
 }
        private QueryNode CreateQueryNode(Query query, object obj)
        {
            var queryNode = new QueryNode(query);

            var dict = obj as Dictionary<object, object>;
            if (dict == null) return queryNode;

            #region Unary and Binary Relation

            foreach (KeyValuePair<object, object> pair in dict)
            {
                var unaryNode = pair.Key as GraphNode;
                var binaryNode = pair.Key as Tuple<GraphNode, GraphNode>;
                var uunaryNode = pair.Key as List<GraphNode>;

                if (unaryNode != null)
                {
                    #region Unary Node
                    var eqGoal = pair.Value as EqGoal;
                    var shapeSymbol = pair.Value as ShapeSymbol;
                    if (eqGoal != null)
                    {
                        var gGoalNode = new GoalNode(eqGoal);
                        queryNode.InternalNodes.Add(gGoalNode);
                        CreateEdge(unaryNode, gGoalNode);
                        continue;
                    }
                    if (shapeSymbol != null)
                    {
                        var gShapeNode = new ShapeNode(shapeSymbol);
                        queryNode.InternalNodes.Add(gShapeNode);
                        var sourceNode = pair.Key;
                        Debug.Assert(sourceNode != null);
                        CreateEdge(unaryNode, gShapeNode);
                        continue;
                    }
                    #endregion
                }

                if (binaryNode != null)
                {
                    #region Binary Node
                    var gShape = pair.Value as ShapeSymbol;
                    if (gShape != null)
                    {
                        var shapeNode = new ShapeNode(gShape);
                        queryNode.InternalNodes.Add(shapeNode);
                        var sourceNode1 = binaryNode.Item1;
                        var sourceNode2 = binaryNode.Item2;
                        Debug.Assert(sourceNode1 != null);
                        Debug.Assert(sourceNode2 != null);
                        CreateEdge(sourceNode1, shapeNode);
                        CreateEdge(sourceNode2, shapeNode);
                    }

                    var gGoal = pair.Value as EqGoal;
                    if (gGoal != null)
                    {
                        var goalNode = new GoalNode(gGoal);
                        queryNode.InternalNodes.Add(goalNode);
                        var sourceNode1 = binaryNode.Item1;
                        var sourceNode2 = binaryNode.Item2;
                        Debug.Assert(sourceNode1 != null);
                        Debug.Assert(sourceNode2 != null);
                        CreateEdge(sourceNode1, goalNode);
                        CreateEdge(sourceNode2, goalNode);
                    }

                    #endregion
                }

                var findNode = SearchNode(pair.Key);
                if (findNode != null)
                {
                    #region Find Node
                    var eqGoal = pair.Value as EqGoal;
                    var shapeSymbol = pair.Value as ShapeSymbol;
                    if (eqGoal != null)
                    {
                        var gGoalNode = new GoalNode(eqGoal);
                        queryNode.InternalNodes.Add(gGoalNode);
                        CreateEdge(findNode, gGoalNode);
                        continue;
                    }
                    if (shapeSymbol != null)
                    {
                        var gShapeNode = new ShapeNode(shapeSymbol);
                        queryNode.InternalNodes.Add(gShapeNode);
                        var sourceNode = pair.Key;
                        Debug.Assert(sourceNode != null);
                        CreateEdge(findNode, gShapeNode);
                        continue;
                    }
                    #endregion
                }

                var findNodeInCurrentqQuery = queryNode.SearchInternalNode(pair.Key);
                if (findNodeInCurrentqQuery != null)
                {
                    #region Find Node
                    var eqGoal = pair.Value as EqGoal;
                    var shapeSymbol = pair.Value as ShapeSymbol;
                    if (eqGoal != null)
                    {
                        var gGoalNode = new GoalNode(eqGoal);
                        queryNode.InternalNodes.Add(gGoalNode);
                        CreateEdge(findNodeInCurrentqQuery, gGoalNode);
                        continue;
                    }
                    if (shapeSymbol != null)
                    {
                        var gShapeNode = new ShapeNode(shapeSymbol);
                        queryNode.InternalNodes.Add(gShapeNode);
                        var sourceNode = pair.Key;
                        Debug.Assert(sourceNode != null);
                        CreateEdge(findNodeInCurrentqQuery, gShapeNode);
                        continue;
                    }
                    #endregion
                }

                if (uunaryNode != null)
                {
                    var equation = pair.Value as Equation;
                    var eqNode = new EquationNode(equation);
                    queryNode.InternalNodes.Add(eqNode);
                    foreach (GraphNode gn in uunaryNode)
                    {
                        CreateEdge(gn, eqNode);
                    }
                }
            }

            #endregion

            return queryNode;
        }
Example #19
0
        public void TestScenario3_CSP_4()
        {
            var graph = new RelationGraph();
            var m = new Var('m');
            var k = new Var('k');
            var eqGoal1 = new EqGoal(m, 3); //m=3
            var eqGoal3 = new EqGoal(m, 4); //m=4
            var eqGoal2 = new EqGoal(k, 2); //k=2
            graph.AddNode(eqGoal1);
            graph.AddNode(eqGoal2);
            graph.AddNode(eqGoal3);
            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 == 2);

            Assert.True(query.CachedEntities.Count == 2);
        }
Example #20
0
 public QueryNode(Query query)
 {
     Query = query;
     InternalNodes = new ObservableCollection<GraphNode>();
     InternalNodes.CollectionChanged += InternalNodes_CollectionChanged;
 }
Example #21
0
        public void TestScenario_05_WorkedExample_0()
        {
            /*
             * 2y+2x-y+2x+4=0
             * 
             * si=
             */

            //(2*y)+(2*x)+(-1*y)+(2*x)+4=0
            var x = new Var('x');
            var y = new Var('y');

            var term1 = new Term(Expression.Multiply, new List<object>() { 2, y });
            var term2 = new Term(Expression.Multiply, new List<object>() { 2, x });
            var term3 = new Term(Expression.Multiply, new List<object>() { -1, y });
            var term4 = new Term(Expression.Multiply, new List<object>() { 2, x });
            var term = new Term(Expression.Add, new List<object>() { term1, term2, term3, term4, 4 });
            var eq = new Equation(term, 0);
            LineSymbol lineSymbol;
            bool result = eq.IsLineEquation(out lineSymbol);
            Assert.True(result);

            var graph = new RelationGraph();
            graph.AddNode(lineSymbol);

            var query = new Query("graph");
            GraphNode gn = graph.AddNode(query);
            var qn = gn as QueryNode;
            Assert.True(qn != null);
            Assert.True(qn.InternalNodes.Count == 1);
            var cachedShapeNode = qn.InternalNodes[0] as ShapeNode;
            Assert.NotNull(cachedShapeNode);
            var cachedLs = cachedShapeNode.ShapeSymbol as LineSymbol;
            Assert.NotNull(cachedLs);
            Assert.True(cachedLs.OutputType == LineType.GeneralForm);
            //Assert.True(cachedLs.ToString().Equals("y=-4x-4"));
            //Assert.True(cachedLs.Traces.Count == 5);

            var query2 = new Query("s");
            graph.AddNode(query2);
            Assert.True(query2.Success);
            Assert.True(query2.CachedEntities.Count == 1);
            var gGoal = query2.CachedEntities.ToList()[0] as EqGoal;
            Assert.NotNull(gGoal);
            Assert.True(gGoal.Rhs.ToString().Equals("-4"));
            //Assert.True(gGoal.Traces.Count == 6);
        }
Example #22
0
 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);
 }
Example #23
0
        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"));
        }
Example #24
0
        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"));
        }
Example #25
0
        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);*/
        }
Example #26
0
        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);
        }
Example #27
0
        private bool EvalExprPatterns(Expr expr,
                                      object obj,
                                      ShapeType? st,
                                      out object output,
                                      bool userInput = false)
        {
            output = null;

            //non-deterministic Multi-Candidate selection
            var dict = obj as Dictionary<PatternEnum, object>;
            //ambiguity of input pattern
            if (dict != null)
            {
                EvalExprPatterns(expr, dict, out output);
                obj = output;
            }

            //deterministic
            var ss = obj as ShapeSymbol;
            if (ss != null) return EvalExprPatterns(expr, ss, out output, userInput);

            //deterministic
            var goal = obj as EqGoal;
            if (goal != null) return EvalExprPatterns(expr, goal, out output, userInput);

            var query = obj as Query;
            if (query != null) return EvalExprPatterns(expr, query, out output, userInput);

            var equation = obj as Equation;
            if (equation != null) return EvalExprPatterns(expr, equation, out output, userInput);

            //non-deterministic Constraint-Solving
            var str = obj as string;
            var gQuery = new Query(str, st);
            if (str != null) return EvalExprPatterns(expr, gQuery, out output, userInput);

            if (userInput)
            {
                var iknowledge = new IKnowledge(expr);
                iknowledge.Tag = obj;
                output = iknowledge;
            }
            return false;
        }
Example #28
0
        private bool InternalValidate(Expr expr, Query query, out object trace)
        {
            trace = null;
            RelationGraph.AddNode(query);

            var queryExpr = new AGQueryExpr(expr, query);
            queryExpr.RetrieveRenderKnowledge();
            if (queryExpr.RenderKnowledge == null) return false;
            if (queryExpr.RenderKnowledge.Count == 0)
            {
                return false;
            }
            var agEquationExpr = queryExpr.RenderKnowledge[0] as AGEquationExpr;
            if (agEquationExpr == null)
            {
                return false;
            }
            agEquationExpr.IsSelected = true;
            agEquationExpr.GenerateSolvingTrace();

            if (agEquationExpr.AutoTrace == null) return false;
            trace = agEquationExpr.AutoTrace;

            RelationGraph.DeleteNode(query);
            return true;
        }