コード例 #1
0
ファイル: Term.Trace.cs プロジェクト: JakeCode11/CSharp.Logic
        public void GenerateTrace(object source, object target, object concept,
                                  string metaScaffold, string scaffold)
        {
            Term currentTerm;

            if (_innerLoop.Count == 0)
            {
                currentTerm = this;
            }
            else
            {
                currentTerm = _innerLoop[_innerLoop.Count - 1].Target as Term;
                if (currentTerm == null)
                {
                    return;
                }
            }
            Term          cloneTerm = currentTerm.Clone();
            List <object> lst;
            bool          isFound = cloneTerm.SearchList(source, out lst);

            if (isFound)
            {
                Debug.Assert(lst != null);
                var index = lst.FindIndex(x => x.Equals(source));
                lst[index] = target;
                object objj = cloneTerm.ReConstruct();
                var    ts   = new TraceStep(currentTerm, objj, concept, metaScaffold, scaffold);
                _innerLoop.Add(ts);
            }
            else
            {
                var targetTerm = target as Term;
                if (targetTerm != null)
                {
                    object objj = targetTerm.ReConstruct();
                    var    ts   = new TraceStep(currentTerm, objj, concept, metaScaffold, scaffold);
                    _innerLoop.Add(ts);
                }
                else
                {
                    var ts = new TraceStep(currentTerm, target, concept, metaScaffold, scaffold);
                    _innerLoop.Add(ts);
                }
            }
        }
コード例 #2
0
        private Equation EvalTermInEquation(Equation rootEq, bool isLhs)
        {
            Equation localEq = this;
            object   obj     = isLhs ? Lhs : Rhs;

            var term = obj as Term;

            if (term == null)
            {
                return(localEq);
            }
            object evalResult = term.Eval();

            if (evalResult.Equals(term))
            {
                return(localEq);
            }
            var cloneEq = Clone();

            if (isLhs)
            {
                cloneEq.Lhs = evalResult;
            }
            else
            {
                cloneEq.Rhs = evalResult;
            }

            Equation currentEq = FindCurrentEq(rootEq);

            #region Trace Transformation from term to Equation

            if (term.Traces.Count != 0)
            {
                localEq = currentEq;
                foreach (var trace in term.Traces)
                {
                    var strategy = trace.Item1 as string;
                    var lst      = trace.Item2 as List <TraceStep>;
                    foreach (var ts in lst)
                    {
                        var cloneEq2    = Generate(currentEq, ts.Source, ts.Target, isLhs);
                        var eqTraceStep = new TraceStep(localEq, cloneEq2, ts.KC, ts.Rule, ts.AppliedRule);
                        rootEq._innerLoop.Add(eqTraceStep);
                        localEq = cloneEq2;
                    }
                    //GenerateATrace(strategy);
                }
            }

            if (term._innerLoop.Count != 0)
            {
                foreach (var ts in term._innerLoop)
                {
                    var cloneEq1    = Generate(currentEq, ts.Source, ts.Target, isLhs);
                    var eqTraceStep = new TraceStep(localEq, cloneEq1, ts.KC, ts.Rule, ts.AppliedRule);
                    rootEq._innerLoop.Add(eqTraceStep);
                    localEq = cloneEq1;
                }
            }
            #endregion

            return(cloneEq);
        }
コード例 #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);
        }
コード例 #4
0
        /// <summary>
        /// if x = y and y = z, then x = z
        /// if x = y, then x + a = y + a
        /// if x^2 = y^2, then x = y
        /// if x = y, then ax = ay
        /// ax = ay -> x=y
        /// </summary>
        /// <param name="goal"></param>
        /// <param name="gGoal"></param>
        /// <returns></returns>
        public static object ApplyTransitive(this Equation currentEq, Equation rootEq, bool withEqRule,
                                             bool lineCheck = false)
        {
            Equation localEq = currentEq;
            object   lhs     = currentEq.Lhs;
            object   rhs     = currentEq.Rhs;

            if (!withEqRule)
            {
                return(localEq);
            }

            //Power Inverse
            if (SatisfyTransitiveCondition2(lhs, rhs))
            {
                #region Condition2

                var cloneEq  = currentEq.Clone();
                var cloneEq2 = currentEq.Clone();

                var lhsTerm = cloneEq.Lhs as Term;
                Debug.Assert(lhsTerm != null);
                var cloneLst = lhsTerm.Args as List <object>;
                Debug.Assert(cloneLst != null);
                cloneEq.Lhs = cloneLst[0];
                cloneEq.Rhs = new Term(Expression.Power, new List <object>()
                {
                    cloneEq.Rhs, 0.5
                });

                var lhsTerm2 = cloneEq2.Lhs as Term;
                Debug.Assert(lhsTerm2 != null);
                var cloneLst2 = lhsTerm2.Args as List <object>;
                Debug.Assert(cloneLst2 != null);
                cloneEq2.Lhs = cloneLst2[0];
                var internal1 = new Term(Expression.Power, new List <object>()
                {
                    cloneEq2.Rhs, 0.5
                });
                cloneEq2.Rhs = new Term(Expression.Multiply, new List <object>()
                {
                    -1, internal1
                });

                string rule        = EquationsRule.Rule(EquationsRule.EquationRuleType.Transitive);
                string appliedRule = EquationsRule.Rule(
                    EquationsRule.EquationRuleType.Transitive,
                    localEq, null);
                string KC = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive);
                var    ts = new TraceStep(localEq, cloneEq, KC, rule, appliedRule);
                rootEq._innerLoop.Add(ts);
                //localEq = cloneEq;

                var lst = new List <Equation>();
                lst.Add(cloneEq);
                lst.Add(cloneEq2);
                return(lst);

                #endregion
            }

            if (!lineCheck)
            {
                //Add Inverse
                if (SatifyTransitiveCondition0(lhs, rhs))
                {
                    #region condition0

                    var cloneEq = currentEq.Clone();

                    var rhsTerm = new Term(Expression.Add, new List <object>()
                    {
                        cloneEq.Rhs
                    });

                    var lhsTerm = cloneEq.Lhs as Term;
                    Debug.Assert(lhsTerm != null);

                    var lst = lhsTerm.Args as List <object>;
                    Debug.Assert(lst != null);

                    for (int i = 0; i < lst.Count; i++)
                    {
                        var  temp     = lst[i];
                        bool isNumber = LogicSharp.IsNumeric(temp);
                        if (isNumber)
                        {
                            var inverseRhs = new Term(Expression.Multiply, new List <object>()
                            {
                                -1, temp
                            });
                            lst.Remove(temp);
                            var rhsArgLst = rhsTerm.Args as List <object>;
                            Debug.Assert(rhsArgLst != null);
                            rhsArgLst.Add(inverseRhs);
                            break;
                        }

                        var term = temp as Term;
                        if (term != null && !term.ContainsVar())
                        {
                            var inverseRhs = new Term(Expression.Multiply, new List <object>()
                            {
                                -1, temp
                            });
                            lst.Remove(i);
                            var rhsArgLst = rhsTerm.Args as List <object>;
                            Debug.Assert(rhsArgLst != null);
                            rhsArgLst.Add(inverseRhs);
                            break;
                        }
                    }

                    cloneEq.Rhs = rhsTerm;
                    if (lst.Count == 1)
                    {
                        cloneEq.Lhs = lst[0];
                    }

                    string rule        = EquationsRule.Rule(EquationsRule.EquationRuleType.Transitive);
                    string appliedRule = EquationsRule.Rule(
                        EquationsRule.EquationRuleType.Transitive,
                        localEq, null);

                    string KC = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive);

                    var traceStep = new TraceStep(localEq, cloneEq, KC, rule, appliedRule);
                    rootEq._innerLoop.Add(traceStep);
                    localEq = cloneEq;
                    return(localEq);

                    #endregion
                }
            }
            else
            {
                if (SatisfyTransitiveCondition1(lhs, rhs))
                {
                    #region Condition1
                    var cloneEq = currentEq.Clone();


                    var inverseRhs = new Term(Expression.Multiply, new List <object>()
                    {
                        -1, rhs
                    });
                    var lhsTerm = cloneEq.Lhs as Term;
                    if (lhsTerm != null)
                    {
                        var cloneLst = lhsTerm.Args as List <object>;
                        Debug.Assert(cloneLst != null);
                        if (lhsTerm.Op.Method.Name.Equals("Add"))
                        {
                            cloneLst.Add(inverseRhs);
                        }
                        else
                        {
                            cloneEq.Lhs = new Term(Expression.Add, new List <object>()
                            {
                                lhs, inverseRhs
                            });
                        }
                    }
                    else
                    {
                        cloneEq.Lhs = new Term(Expression.Add, new List <object>()
                        {
                            lhs, inverseRhs
                        });
                    }
                    cloneEq.Rhs = 0;
                    string rule        = EquationsRule.Rule(EquationsRule.EquationRuleType.Transitive);
                    string appliedRule = EquationsRule.Rule(
                        EquationsRule.EquationRuleType.Transitive,
                        localEq, null);

                    string KC = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive);

                    var traceStep = new TraceStep(localEq, cloneEq, KC, rule, appliedRule);
                    rootEq._innerLoop.Add(traceStep);
                    localEq = cloneEq;
                    #endregion
                }
            }

            //Mutliply Inverse
            if (SatisfyTransitiveCondition3(lhs, rhs))
            {
                #region condition3

                var cloneEq = currentEq.Clone();

                var rhsTerm = new Term(Expression.Multiply, new List <object>()
                {
                    cloneEq.Rhs
                });

                var lhsTerm = cloneEq.Lhs as Term;
                Debug.Assert(lhsTerm != null);

                var lst = lhsTerm.Args as List <object>;
                Debug.Assert(lst != null);

                for (int i = 0; i < lst.Count; i++)
                {
                    var  temp     = lst[i];
                    bool isNumber = LogicSharp.IsNumeric(temp);
                    if (isNumber)
                    {
                        var inverseRhs = new Term(Expression.Divide, new List <object>()
                        {
                            1, temp
                        });
                        lst.Remove(temp);
                        var rhsArgLst = rhsTerm.Args as List <object>;
                        Debug.Assert(rhsArgLst != null);
                        rhsArgLst.Add(inverseRhs);
                        break;
                    }

                    var term = temp as Term;
                    if (term != null && !term.ContainsVar())
                    {
                        var inverseRhs = new Term(Expression.Divide, new List <object>()
                        {
                            1, temp
                        });
                        lst.Remove(i);
                        var rhsArgLst = rhsTerm.Args as List <object>;
                        Debug.Assert(rhsArgLst != null);
                        rhsArgLst.Add(inverseRhs);
                        break;
                    }
                }

                cloneEq.Rhs = rhsTerm;
                if (lst.Count == 1)
                {
                    cloneEq.Lhs = lst[0];
                }

                string rule        = EquationsRule.Rule(EquationsRule.EquationRuleType.Transitive);
                string appliedRule = EquationsRule.Rule(
                    EquationsRule.EquationRuleType.Transitive,
                    localEq, null);

                string KC = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive);

                var traceStep = new TraceStep(localEq, cloneEq, KC, rule, appliedRule);
                rootEq._innerLoop.Add(traceStep);
                localEq = cloneEq;
                return(localEq);

                #endregion
            }

            //Divide Inverse
            if (SatisfyTransitiveCondition4(lhs, rhs))
            {
                #region condition4
                var cloneEq = currentEq.Clone();

                var lhsTerm = cloneEq.Lhs as Term;
                Debug.Assert(lhsTerm != null);

                var lst = lhsTerm.Args as List <object>;
                Debug.Assert(lst != null);
                Debug.Assert(lst.Count == 2);

                bool numerator   = LogicSharp.IsNumeric(lst[0]);
                bool deNumerator = LogicSharp.IsNumeric(lst[1]);

                if (deNumerator)
                {
                    var rhsTerm = new Term(Expression.Multiply, new List <object>()
                    {
                        lst[1], cloneEq.Rhs
                    });
                    var newEq = new Equation(lst[0], rhsTerm);

                    string rule        = EquationsRule.Rule(EquationsRule.EquationRuleType.Transitive);
                    string appliedRule = EquationsRule.Rule(
                        EquationsRule.EquationRuleType.Transitive,
                        localEq, newEq);

                    string KC = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive);

                    var traceStep = new TraceStep(localEq, newEq, KC, rule, appliedRule);
                    rootEq._innerLoop.Add(traceStep);
                    localEq = newEq;
                    return(localEq);
                }


                if (numerator)
                {
                    var rhsTerm = new Term(Expression.Divide, new List <object>()
                    {
                        lst[0], cloneEq.Rhs
                    });
                    var    newEq       = new Equation(lst[1], rhsTerm);
                    string rule        = EquationsRule.Rule(EquationsRule.EquationRuleType.Transitive);
                    string appliedRule = EquationsRule.Rule(
                        EquationsRule.EquationRuleType.Transitive,
                        localEq, newEq);

                    string KC = EquationsRule.RuleConcept(EquationsRule.EquationRuleType.Transitive);

                    var traceStep = new TraceStep(localEq, newEq, KC, rule, appliedRule);
                    rootEq._innerLoop.Add(traceStep);
                    localEq = newEq;
                    return(localEq);
                }

                #endregion
            }

            return(localEq);
        }