Ejemplo n.º 1
0
        public Substitution Copy(Substitution add)
        {
            Substitution result = Copy();
            result.Add(add);

            return result;
        }
Ejemplo n.º 2
0
        public override Term ApplySubstitution(Substitution sigma)
        {
            var args = new Term[Arguments.Length];

            for (int i = 0; i < Arguments.Length; i++)
                args[i] = Arguments[i].ApplySubstitution(sigma);

            return new TermFunction(false, FunctionName, args);
        }
Ejemplo n.º 3
0
        public override Expression ApplySubstitution(Substitution sigma)
        {
            var newSentences = new Expression[_sentences.Length];

            for (int i = 0; i < _sentences.Length; i++)
                newSentences[i] = _sentences[i].ApplySubstitution(sigma);

            return new Disjunction(newSentences);
        }
Ejemplo n.º 4
0
        public override Expression ApplySubstitution(Substitution sigma)
        {
            var newSentences = new Expression[Conjuncts.Length];

            for (int i = 0; i < Conjuncts.Length; i++)
                newSentences[i] = Conjuncts[i].ApplySubstitution(sigma);

            // TODO: remove duplicates if any were created during application of substitution
            return new Conjunction(false, newSentences);
        }
Ejemplo n.º 5
0
        public override Term ApplySubstitution(Substitution sigma)
        {
            // Does sigma apply to our variable?
            Term replacement = sigma.GetMapping(this);

            if (replacement == null)
                return this; // nothing to change!

            // Otherwise, return the variable's replacement
            return replacement;
        }
Ejemplo n.º 6
0
        private static void ExitUnificationLevel(Substitution subs)
        {
            if (!Logger.IsDebugEnabled) return;
            _unificationLevel--;

            if (_unificationLevel < 0)
                throw new Exception("Unification level < 0!!");

            Logger.Debug(subs == null
                             ? string.Format("{0}Unifier: failed to unify.", Util.MakeIndent(_unificationLevel))
                             : string.Format("{0}Unifier: mgu = {1}", Util.MakeIndent(_unificationLevel), subs));
        }
Ejemplo n.º 7
0
        public static Substitution Mgu(Fact f1, Fact f2)
        {
            // Make sure this is even worth our time to check
            if (f1.RelationName != f2.RelationName)
                return null;
            if (f1.Arity != f2.Arity)
                return null;

            EnterUnificationLevel(f1, f2);

            var subs = new Substitution();

            if (Mgu(f1, f2, subs))
            {
                ExitUnificationLevel(subs);
                return subs;
            }
            ExitUnificationLevel(null);
            return null;
        }
Ejemplo n.º 8
0
 private static bool Mgu(Term t1, Term t2, Substitution subsSoFar)
 {
     return t1.Mgu(t2, subsSoFar);
 }
Ejemplo n.º 9
0
 private static Implication RemoveNotDistinctLiteral(Implication rule, Negation notDistinctLiteral)
 {
     //Figure out the substitution we want...
     //If we have two constantsin Either Remove one or
     //maybe get rid of the ___?
     //One is a variablein Replace the variable with the other thing
     //throughout the rule
     var distinct = (Fact)notDistinctLiteral.Negated;
     Term arg1 = distinct.GetTerm(0);
     Term arg2 = distinct.GetTerm(1);
     if (arg1 == arg2)
     {
         //Just Remove that literal
         var newBody = new List<Expression>();
         newBody.AddRange(rule.Antecedents.Conjuncts);
         newBody.Remove(notDistinctLiteral);
         return new Implication(rule.Consequent, newBody.ToArray());
     }
     var p1 = arg1 as TermVariable;
     if (p1 != null)
     {
         //What we return will still have the not-distinct literal,
         //but it will get replaced in the next pass.
         //(Even if we have two variables, they will be equal next time through.)
         var sub = new Substitution();
         sub.AddMapping(p1, arg2);
         return (Implication)rule.ApplySubstitution(sub);
     }
     var variable = arg2 as TermVariable;
     if (variable != null)
     {
         var sub = new Substitution();
         sub.AddMapping(variable, arg1);
         return (Implication)rule.ApplySubstitution(sub);
     }
     if (arg1 is TermObject || arg2 is TermObject)
     {
         //We have two non-equal constants, or a constant and a function.
         //The rule should have no effect.
         return null;
     }
     //We have two functions. Complicated! (Have to replace them with unified version.)
     //We pass on this case for now.
     //TODO: Implement correctly.
     throw new Exception("We can't currently handle (not (distinct <function> <function>)).");
 }
Ejemplo n.º 10
0
        private static Substitution Mgu(Disjunction dis1, Disjunction dis2)
        {
            List<Expression> contents1 = dis1.GetDisjuncts().ToList();
            List<Expression> contents2 = dis2.GetDisjuncts().ToList();
            if (contents1.Count != contents2.Count)
                return null;

            var subs = new Substitution();

            return contents1.Where((t, i) => Mgu(t, contents2[i], subs) == false).Any() ? null : subs;
        }
Ejemplo n.º 11
0
        private static bool Mgu(Fact f1, Fact f2, Substitution subsSoFar)
        {
            // Make sure this is even worth our time to check
            if (f1.RelationName != f2.RelationName)
                return false;
            if (f1.Arity != f2.Arity)
                return false;

            // Find the mgu for each column of the facts
            for (int i = 0; i < f1.Arity; i++)
            {
                // If there is no mgu, just die
                if (Mgu(f1.GetTerm(i), f2.GetTerm(i), subsSoFar) == false)
                    return false;
            }

            return true;
        }
Ejemplo n.º 12
0
        internal Substitution Reverse()
        {
            var result = new Substitution();

            foreach (var kv in Substitutions)
            {
                var newKey = kv.Value as TermVariable;
                if (newKey == null)
                    return null;
                result.Substitutions[newKey] = kv.Key;
            }
            return result;
        }
Ejemplo n.º 13
0
 public abstract Expression ApplySubstitution(Substitution sigma);
Ejemplo n.º 14
0
 public override Term ApplySubstitution(Substitution sigma)
 {
     // Nothing to do here -- no variables in object constants.
     return this;
 }
Ejemplo n.º 15
0
        public override bool Mgu(Term t, Substitution subsSoFar)
        {
            if (t is TermObject)// Cannot map functions to constants.
                return false;

            var variable = t as TermVariable;
            if (variable != null)// Reverse the problem; the TermVariable class handles this
                return variable.Mgu(this, subsSoFar);

            var function = t as TermFunction;
            if (function != null)
            {
                TermFunction f = function;

                // Make sure that our function names are equal
                if (FunctionName != f.FunctionName)
                    return false;

                // Make sure arities are the same
                if (Arity != f.Arity)
                    return false;

                // Finally, make sure we can get the mgu of all arguments
                for (int i = 0; i < Arity; i++)
                    if (Arguments[i].Mgu(f.Arguments[i], subsSoFar) == false)
                        return false;

                // All good!
                return true;
            }
            throw new Exception("TermFunction.mgu: Don't know how to handle term of type " + t.GetType().Name);
        }
Ejemplo n.º 16
0
 public abstract override Expression ApplySubstitution(Substitution sigma);
Ejemplo n.º 17
0
        public override bool Mgu(Term t, Substitution subsSoFar)
        {
            if (t is TermObject)
                return Equals(t);

            var variable = t as TermVariable;
            if (variable != null)   // Reverse the problem; the TermVariable class handles this
                return variable.Mgu(this, subsSoFar);

            if (t is TermFunction)   // Can't unify a function and a constant.
                return false;

            throw new ApplicationException("TermObject.mgu: Don't know how to handle term of type " + t.GetType().Name);
        }
Ejemplo n.º 18
0
        protected bool Equals(Substitution other)
        {
            if (other.NumMappings() != NumMappings())
                return false;

            List<TermVariable> subs = Substitutions.Keys.ToList();

            //this cannot be a foreach as GetMapping possibly modifies the collection
            for (int i = 0; i < subs.Count; i++)
            {
                TermVariable currrentKey = subs[i];
                if (!GetMapping(currrentKey).Equals(other.GetMapping(currrentKey)))
                    return false;
            }
            return true;
            //return other.NumMappings() == NumMappings() && _substitutions.All(kv => GetMapping(kv.Key).Equals(other.GetMapping(kv.Key)));
        }
Ejemplo n.º 19
0
        private Substitution Copy()
        {
            var result = new Substitution();

            foreach (var kv in Substitutions)
                result.Substitutions[kv.Key] = kv.Value.Clone();

            return result;
        }
Ejemplo n.º 20
0
 public void Add(Substitution add)
 {
     foreach (var kv in add.Substitutions)
         AddMapping(kv.Key, kv.Value);
 }
Ejemplo n.º 21
0
        public override bool Mgu(Term t, Substitution subsSoFar)
        {
            if (t is TermObject)
            {
                Term replacement = subsSoFar.GetMapping(this);

                if (replacement != null)
                {
                    TermVariable termVariable = replacement as TermVariable;
                    if (termVariable != null)
                    {
                        subsSoFar.AddMapping(this, t);
                        subsSoFar.AddMapping(termVariable, t);
                        return true;
                    }
                    return replacement.Equals(t);
                }

                // There was no replacement:
                // Add a mapping for the variable to this term-object
                subsSoFar.AddMapping(this, t);
                return true;

            }
            var variable = t as TermVariable;
            if (variable != null)
            {
                TermVariable it = variable;

                Term myReplacement = subsSoFar.GetMapping(this);
                Term itsReplacement = subsSoFar.GetMapping(it);

                if (itsReplacement == null)
                {
                    // just map 'it' to me (or my replacement)
                    if (myReplacement == null)
                    {
                        if (!Equals(it))
                            subsSoFar.AddMapping(it, this);
                    }
                    else
                    {
                        if (!(myReplacement is TermVariable) || !myReplacement.Equals(it))
                            subsSoFar.AddMapping(it, myReplacement);
                    }

                    return true;
                }

                // At this point, 'it' has a replacement.
                if (myReplacement == null)
                {
                    // I don't have a replacement, so map me to it, or to its replacement
                    if (!(itsReplacement is TermVariable) || !itsReplacement.Equals(this))
                        subsSoFar.AddMapping(this, itsReplacement);

                    return true;
                }

                // At this point, both term variables have replacements.
                // So make sure that they are the same!
                return myReplacement.Equals(itsReplacement);
            }
            var func = t as TermFunction;
            if (func != null)
            {
                Term myReplacement = subsSoFar.GetMapping(this);

                // Case 1: I have a replacement
                if (myReplacement != null)
                    // See if my replacement can be unified with the function
                    return myReplacement.Mgu(func, subsSoFar);

                    // Case 2: I have no replacement
                TermFunction itsReplacement = subsSoFar.GetMapping(func);

                if (itsReplacement.HasVariable(this))
                    return false;

                // just set my replacement to the function
                subsSoFar.AddMapping(this, itsReplacement);
                return true;
            }
            throw new Exception("TermVariable.mgu: Don't know how to handle term of type " + t.GetType().Name);
        }
Ejemplo n.º 22
0
        public override Expression ApplySubstitution(Substitution sigma)
        {
            var columns = new Term[Terms.Length];

            bool vars = false;
            for (int i = 0; i < Terms.Length; i++)
            {
                columns[i] = Terms[i].ApplySubstitution(sigma);
                if (columns[i].HasVariables)
                    vars = true;
            }

            if (vars)
                return new VariableFact(false, RelationName, columns);
            var newFact = new GroundFact(false, RelationName, columns);
            //if (factQuery != null && factQuery.Contains(newFact))
            //{
            //    usedFacts.Add(new FactMapping { ground = newFact, variable = this });
            //}
            return newFact;
        }
Ejemplo n.º 23
0
        private static bool Mgu(Expression exp1, Expression exp2, Substitution subsSoFar)
        {
            if (exp1.GetType() != exp2.GetType())
                return false;

            var fact1 = exp1 as Fact;
            if (fact1 != null)
                return Mgu(fact1, (Fact)exp2, subsSoFar);

            var dis1 = exp1 as Disjunction;
            if (dis1 != null)
                return Mgu(dis1, (Disjunction)exp2, subsSoFar);

            var neg1 = exp1 as Negation;
            if (neg1 != null)
                return Mgu(neg1, (Negation)exp2, subsSoFar);

            throw new Exception("Unhandled type in unifier Mgu");
        }
Ejemplo n.º 24
0
 public override Expression ApplySubstitution(Substitution sigma)
 {
     return new Negation(Negated.ApplySubstitution(sigma));
 }