示例#1
0
        private EquationNode CreateEqNode(Equation eq, object obj)
        {
            var eqNode = new EquationNode(eq);

            var lst = obj as List <Tuple <object, object> >;

            if (lst == null)
            {
                return(eqNode);
            }

            foreach (var tuple in lst)
            {
                if (tuple.Item1.Equals(eq))
                {
                    BuildRelation(eqNode, tuple.Item2);
                }

                if (tuple.Item2.Equals(eq))
                {
                    BuildRelation(tuple.Item1, eqNode);
                }
            }
            _nodes.Add(eqNode);
            return(eqNode);
        }
        private void Reify(EquationNode eqNode, GraphNode rootNode)
        {
            foreach (GraphEdge ge in eqNode.OutEdges)
            {
                var upperNode = ge.Target;
                if (upperNode.Equals(rootNode)) return;
                if (upperNode.Synthesized) return;

                Reify(upperNode, rootNode); //depth first search
            }
        }
示例#3
0
        public void Test_Unify_2()
        {
            // a=2, b=a
            var a = new Var("a");
            var b = new Var("b");
            var eqGoal = new EqGoal(a, 2);

            var equation = new Equation(b, a);
            var eqNode = new EquationNode(equation);

            object obj;
            bool result = RelationLogic.ConstraintCheck(eqNode, eqGoal, null, out obj);
            Assert.True(result);
            Assert.NotNull(obj);
        }
示例#4
0
        public void Test_Unify_2()
        {
            // a=2, b=a
            var a      = new Var("a");
            var b      = new Var("b");
            var eqGoal = new EqGoal(a, 2);

            var equation = new Equation(b, a);
            var eqNode   = new EquationNode(equation);

            object obj;
            bool   result = RelationLogic.ConstraintCheck(eqNode, eqGoal, null, out obj);

            Assert.True(result);
            Assert.NotNull(obj);
        }
        private void Reify(EquationNode eqNode, GraphNode rootNode)
        {
            foreach (GraphEdge ge in eqNode.OutEdges)
            {
                var upperNode = ge.Target;
                if (upperNode.Equals(rootNode))
                {
                    return;
                }
                if (upperNode.Synthesized)
                {
                    return;
                }

                Reify(upperNode, rootNode); //depth first search
            }
        }
示例#6
0
        public static bool Satisfy(this EquationNode eqNode, Var variable)
        {
            var equation = eqNode.Equation;

            var lhsTerm = equation.Lhs as Term;

            if (lhsTerm != null)
            {
                if (lhsTerm.ContainsVar(variable))
                {
                    return(true);
                }
            }

            var lhsVar = equation.Lhs as Var;

            if (lhsVar != null)
            {
                if (lhsVar.Equals(variable))
                {
                    return(true);
                }
            }

            var rhsTerm = equation.Rhs as Term;

            if (rhsTerm != null)
            {
                if (rhsTerm.ContainsVar(variable))
                {
                    return(true);
                }
            }

            var rhsVar = equation.Rhs as Var;

            if (rhsVar != null)
            {
                if (rhsVar.Equals(variable))
                {
                    return(true);
                }
            }

            return(false);
        }
        private static bool ConstraintCheck(EquationNode en, object constraint, out object output)
        {
            output = null;
            var eqGoal = constraint as EqGoal;

            if (eqGoal == null)
            {
                return(false);
            }
            var variable = eqGoal.Lhs as Var;

            if (variable != null && en.Satisfy(variable))
            {
                var lst = new List <Tuple <object, object> >();


                var equation = en.Equation;
                Debug.Assert(equation != null);

                equation.Reify(eqGoal);
                for (int i = 0; i < equation.CachedEntities.Count; i++)
                {
                    var cachedEq = equation.CachedEntities.ToList()[i] as Equation;
                    if (cachedEq == null)
                    {
                        continue;
                    }

                    object obj1;
                    bool   tempResult = cachedEq.IsEqGoal(out obj1);
                    if (tempResult)
                    {
                        var gGoal = obj1 as EqGoal;
                        Debug.Assert(gGoal != null);
                        lst.Add(new Tuple <object, object>(en, gGoal));
                    }
                }

                lst.Add(new Tuple <object, object>(constraint, en));
                //output = en.Equation;
                output = lst;
                return(true);
            }
            return(false);
        }
示例#8
0
        public void Test_Unify_1()
        {
            //a = 1, a*b = -1;
            //true positive
           
            var a = new Var("a");
            var b = new Var("b");
            var eqGoal = new EqGoal(a, 1);

            var lhsTerm = new Term(Expression.Multiply, new List<object>() {a, b});
            var equation = new Equation(lhsTerm, -1);
            var eqNode = new EquationNode(equation);

            object obj;
            bool result = RelationLogic.ConstraintCheck(eqNode, eqGoal, null, out obj);
            Assert.True(result);
            Assert.NotNull(obj);
        }
示例#9
0
        public void Test_Unify_1()
        {
            //a = 1, a*b = -1;
            //true positive

            var a      = new Var("a");
            var b      = new Var("b");
            var eqGoal = new EqGoal(a, 1);

            var lhsTerm = new Term(Expression.Multiply, new List <object>()
            {
                a, b
            });
            var equation = new Equation(lhsTerm, -1);
            var eqNode   = new EquationNode(equation);

            object obj;
            bool   result = RelationLogic.ConstraintCheck(eqNode, eqGoal, null, out obj);

            Assert.True(result);
            Assert.NotNull(obj);
        }
示例#10
0
        private EquationNode AddEquationNode(Equation equation)
        {
            //1. build relation using geometry constraint-solving (synthesize new node) (Top-Down)
            //2. Reify itself (Bottom-up)
            object relations;

            ConstraintSatisfy(equation, out relations);
            EquationNode eqNode = CreateEqNode(equation, relations);

/*            GraphNode sinkNode = FindSinkNode(eqNode);
 *          Reify(sinkNode);*/

            Reify(eqNode);

/*            foreach (object obj in goalRels)
 *          {
 *              var goalNode = obj as GoalNode;
 *              if (goalNode == null) continue;
 *              Reify(goalNode);
 *              //Reify(eqNode);
 *          }*/
            return(eqNode);
        }
        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;
        }
        private EquationNode CreateEqNode(Equation eq, object obj)
        {
            var eqNode = new EquationNode(eq);

            var lst = obj as List<Tuple<object, object>>;
            if (lst == null) return eqNode;

            foreach (var tuple in lst)
            {
                if (tuple.Item1.Equals(eq))
                {
                    BuildRelation(eqNode, tuple.Item2);
                }
                
                if (tuple.Item2.Equals(eq))
                {
                    BuildRelation(tuple.Item1, eqNode);
                }
            }
            _nodes.Add(eqNode);
            return eqNode;
        }
 private void UnReify(EquationNode eqNode)
 {
     //TODO
 }
示例#14
0
        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);
        }
 private void UnReify(EquationNode eqNode)
 {
     //TODO
 }
示例#16
0
        public static bool Satisfy(this EquationNode eqNode, string variable)
        {
            var newVar = new Var(variable);

            return(eqNode.Satisfy(newVar));
        }
        private static bool ConstraintCheck(EquationNode en, object constraint, out object output)
        {
            output = null;
            var eqGoal = constraint as EqGoal;
            if (eqGoal == null) return false;
            var variable = eqGoal.Lhs as Var;
            if (variable != null && en.Satisfy(variable))
            {
                var lst = new List<Tuple<object, object>>();
                

                var equation = en.Equation;
                Debug.Assert(equation != null);

                equation.Reify(eqGoal);
                for (int i = 0; i < equation.CachedEntities.Count; i++)
                {
                    var cachedEq = equation.CachedEntities.ToList()[i] as Equation;
                    if (cachedEq == null) continue;

                    object obj1;
                    bool tempResult = cachedEq.IsEqGoal(out obj1);
                    if (tempResult)
                    {
                        var gGoal = obj1 as EqGoal;
                        Debug.Assert(gGoal != null);
                        lst.Add(new Tuple<object, object>(en, gGoal));
                    }
                }

                lst.Add(new Tuple<object, object>(constraint, en));
                //output = en.Equation;
                output = lst;
                return true;
            }
            return false;
        }