Ejemplo n.º 1
0
 public object EvalGoal(object field, EqGoal goal)
 {
     var substitute = goal.ToDict();
     object result = null;
     if (Var.ContainsVar(field))
     {
         result = LogicSharp.Reify(field, substitute);
     }
     else
     {
         result = field;
     }
     return result; 
 }
Ejemplo n.º 2
0
        public object EvalGoal(object field, EqGoal goal)
        {
            var    substitute = goal.ToDict();
            object result     = null;

            if (Var.ContainsVar(field))
            {
                result = LogicSharp.Reify(field, substitute);
            }
            else
            {
                result = field;
            }
            return(result);
        }
Ejemplo n.º 3
0
        public bool Reify(EqGoal goal)
        {
            var lhsTerm = Lhs as Term;
            var rhsTerm = Rhs as Term;
            var lhsVar = Lhs as Var;
            var rhsVar = Rhs as Var;

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

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

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

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

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

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

                return true;
            }

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

                CachedEntities.Add(cloneEq);
                return true;
            }
            return false;
        }
Ejemplo n.º 4
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);
                        }
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public bool Reify(EqGoal goal)
        {
            var lhsTerm = Lhs as Term;
            var rhsTerm = Rhs as Term;
            var lhsVar  = Lhs as Var;
            var rhsVar  = Rhs as Var;

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

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

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

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

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

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

                return(true);
            }

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

                CachedEntities.Add(cloneEq);
                return(true);
            }
            return(false);
        }
Ejemplo n.º 6
0
        public void CacheC(object obj, EqGoal goal)
        {
            CachedGoals.Add(new KeyValuePair<object, EqGoal>(LineAcronym.C, goal));

            if (CachedSymbols.Count == 0)
            {
                var line = Shape as Line;
                Debug.Assert(line != null);

                #region generate new object

                var gLine = new Line(line.Label, line.A, line.B, obj);
                var gLineSymbol = new LineSymbol(gLine);
                gLineSymbol.CachedGoals.Add(new KeyValuePair<object, EqGoal>(LineAcronym.C, goal));
                CachedSymbols.Add(gLineSymbol);

                //Transform goal trace
                for (int i = goal.Traces.Count - 1; i >= 0; i--)
                {
                    gLine.Traces.Insert(0, goal.Traces[i]);
                }

                //Substitution trace
                string rule = SubstitutionRule.ApplySubstitute();
                string appliedRule = SubstitutionRule.ApplySubstitute(this, goal);
                string kc = SubstitutionRule.SubstituteKC();

                var ts = new TraceStep(this, gLineSymbol, kc, rule, appliedRule);
                gLine._innerLoop.Add(ts);
                gLine.GenerateATrace(SubstitutionRule.SubstitutionStrategy);
                //gLine.Traces.Insert(0, ts);
                #endregion
            }
            else
            {
                #region Iterate existing point object

                foreach (ShapeSymbol ss in CachedSymbols.ToList())
                {
                    var line = ss.Shape as Line;
                    if (line != null)
                    {
                        var cResult = LogicSharp.Reify(line.C, goal.ToDict());
                        if (!line.C.Equals(cResult))
                        {
                            var gline = new Line(line.Label, line.A, line.B, line.C);
                            var gLineSymbol = new LineSymbol(gline);
                            //substitute
                            line.C = cResult;
                            ss.CachedGoals.Add(new KeyValuePair<object, EqGoal>(LineAcronym.C, goal));

                            //transform goal trace
                            for (int i = goal.Traces.Count - 1; i >= 0; i--)
                            {
                                line.Traces.Insert(0, goal.Traces[i]);
                            }

                            string rule = SubstitutionRule.ApplySubstitute();
                            string appliedRule = SubstitutionRule.ApplySubstitute(gLineSymbol, goal);
                            string kc = SubstitutionRule.SubstituteKC();

                            var ts = new TraceStep(gLineSymbol, ss, kc, rule, appliedRule);
                            //line.Traces.Insert(0, ts);
                            line._innerLoop.Add(ts);
                            line.GenerateATrace(SubstitutionRule.SubstitutionStrategy);
                        }
                        else
                        {
                            //generate
                            var gLine = new Line(line.Label, line.A, line.B, obj);
                            var gLineSymbol = new LineSymbol(gLine);
                            gLineSymbol.CachedGoals.Add(new KeyValuePair<object, EqGoal>(LineAcronym.C, goal));
                            foreach (KeyValuePair<object, EqGoal> pair in ss.CachedGoals)
                            {
                                if (pair.Key.Equals(LineAcronym.A))
                                {
                                    gLineSymbol.CachedGoals.Add(new KeyValuePair<object, EqGoal>(LineAcronym.A, pair.Value));
                                }
                                else if (pair.Key.Equals(LineAcronym.B))
                                {
                                    gLineSymbol.CachedGoals.Add(new KeyValuePair<object, EqGoal>(LineAcronym.B, pair.Value));
                                }
                            }
                            CachedSymbols.Add(gLineSymbol);

                            //substitute
                            //Add traces from line to gLine
                            for (int i = line.Traces.Count - 1; i >= 0; i--)
                            {
                                gLine.Traces.Insert(0, line.Traces[i]);
                            }
                            //transform goal trace
                            for (int i = goal.Traces.Count - 1; i >= 0; i--)
                            {
                                gLine.Traces.Insert(0, goal.Traces[i]);
                            }
                            string rule = SubstitutionRule.ApplySubstitute();
                            string appliedRule = SubstitutionRule.ApplySubstitute(gLineSymbol, goal);
                            string kc = SubstitutionRule.SubstituteKC();

                            var ts = new TraceStep(ss, gLineSymbol, kc, rule, appliedRule);
                            gLine._innerLoop.Add(ts);
                            gLine.GenerateATrace(SubstitutionRule.SubstitutionStrategy);
                            //gLine.Traces.Insert(0, ts);
                        }
                    }
                }
                #endregion
            }
        }
Ejemplo n.º 7
0
 public object Reify(EqGoal eqGoal)
 {
     Dictionary<object, object> dict = eqGoal.ToDict();
     return Reify(dict);
 }