// Note: see page 327.
        public StandardizeApartResult standardizeApart(Sentence aSentence,
                StandardizeApartIndexical standardizeApartIndexical)
        {
            List<Variable> toRename = variableCollector
                    .collectAllVariables(aSentence);
            Dictionary<Variable, Term> renameSubstitution = new Dictionary<Variable, Term>();
            Dictionary<Variable, Term> reverseSubstitution = new Dictionary<Variable, Term>();

            foreach (Variable var in toRename)
            {
                Variable v = null;
                do
                {
                    v = new Variable(standardizeApartIndexical.getPrefix()
                            + standardizeApartIndexical.getNextIndex());
                    // Ensure the new variable name is not already
                    // accidentally used in the sentence
                } while (toRename.Contains(v));

                renameSubstitution.Add(var, v);
                reverseSubstitution.Add(v, var);
            }

            Sentence standardized = substVisitor.subst(renameSubstitution,
                    aSentence);

            return new StandardizeApartResult(aSentence, standardized,
                    renameSubstitution, reverseSubstitution);
        }
Beispiel #2
0
 public void testUnificationFailure()
 {
     Variable var = new Variable("x");
     Sentence sentence = parser.parse("Knows(y)");
     theta = null;
     Dictionary<Variable, Term> result = unifier.unify(var, sentence, theta);
     Assert.IsNull(result);
 }
Beispiel #3
0
 public override Object visitVariable(Variable variable, Object arg)
 {
     Dictionary<Variable, Term> substitution = (Dictionary<Variable, Term>)arg;
     if (substitution.ContainsKey(variable))
     {
         return substitution[variable].copy();
     }
     return variable.copy();
 }
Beispiel #4
0
        public void testVariableEqualsConstant()
        {
            Variable var1 = new Variable("x");
            Constant constant = new Constant("John");

            Dictionary<Variable, Term> result = unifier.unify(var1, constant, theta);
            Assert.AreEqual(theta, result);
            Assert.AreEqual(1, theta.Keys.Count);
            Assert.IsTrue(theta.ContainsKey(var1));
            Assert.AreEqual(constant, theta[var1]);
        }
Beispiel #5
0
        public void testThetaPassedBackIfXEqualsYBothVariables()
        {
            Variable var1 = new Variable("x");
            Variable var2 = new Variable("x");

            theta.Add(new Variable("dummy"), new Variable("dummy"));
            Dictionary<Variable, Term> result = unifier.unify(var1, var2, theta);
            Assert.AreEqual(theta, result);
            Assert.AreEqual(1, theta.Keys.Count);
            Assert.IsTrue(theta.ContainsKey(new Variable("dummy")));
        }
Beispiel #6
0
        public void testSimpleVariableUnification()
        {
            Variable var1 = new Variable("x");
            List<Term> terms1 = new List<Term>();
            terms1.Add(var1);
            Predicate p1 = new Predicate("King", terms1); // King(x)

            List<Term> terms2 = new List<Term>();
            terms2.Add(new Constant("John"));
            Predicate p2 = new Predicate("King", terms2); // King(John)

            Dictionary<Variable, Term> result = unifier.unify(p1, p2, theta);
            Assert.AreEqual(theta, result);
            Assert.AreEqual(1, theta.Keys.Count);
            Assert.IsTrue(theta.ContainsKey(new Variable("x"))); // x =
            Assert.AreEqual(new Constant("John"), theta[var1]); // John
        }
        public Clause standardizeApart(Clause clause,
                StandardizeApartIndexical standardizeApartIndexical)
        {

            List<Variable> toRename = variableCollector.collectAllVariables(clause);
            Dictionary<Variable, Term> renameSubstitution = new Dictionary<Variable, Term>();

            foreach (Variable var in toRename)
            {
                Variable v = null;
                do
                {
                    v = new Variable(standardizeApartIndexical.getPrefix()
                            + standardizeApartIndexical.getNextIndex());
                    // Ensure the new variable name is not already
                    // accidentally used in the sentence
                } while (toRename.Contains(v));

                if (renameSubstitution.ContainsKey(var))
                {
                    renameSubstitution[var] = v;
                }
                else
                {
                    renameSubstitution.Add(var, v);
                }
            }

            if (renameSubstitution.Count > 0)
            {
                List<Literal> literals = new List<Literal>();

                foreach (Literal l in clause.getLiterals())
                {
                    literals.Add(substVisitor.subst(renameSubstitution, l));
                }
                Clause renamed = new Clause(literals);
                renamed.setProofStep(new ProofStepRenaming(renamed, clause
                        .getProofStep()));
                return renamed;
            }

            return clause;
        }
Beispiel #8
0
        //
        // PROTECTED METHODS
        //

        // Note: You can subclass and override this method in order
        // to re-implement the OCCUR-CHECK?() to always
        // return false if you want that to be the default
        // behavior, as is the case with Prolog.
        protected bool occurCheck(Dictionary<Variable, Term> theta, Variable var,
                FOLNode x)
        {
            if (x is Function)
            {
                List<Variable> varsToCheck = _variableCollector
                        .collectAllVariables((Function)x);
                if (varsToCheck.Contains(var))
                {
                    return true;
                }

                // Now need to check if cascading will cause occurs to happen
                // e.g.
                // Loves(SF1(v2),v2)
                // Loves(v3,SF0(v3))
                // or
                // P(v1,SF0(v1),SF0(v1))
                // P(v2,SF0(v2),v2 )
                // or
                // P(v1, F(v2),F(v2),F(v2),v1, F(F(v1)),F(F(F(v1))),v2)
                // P(F(v3),v4, v5, v6, F(F(v5)),v4, F(v3), F(F(v5)))
                return cascadeOccurCheck(theta, var, varsToCheck,
                        new List<Variable>(varsToCheck));
            }
            return false;
        }
 public Object visitVariable(Variable var, Object arg)
 {
     List<Variable> variables = (List<Variable>)arg;
     variables.Add(var);
     return var;
 }
 public Object visitVariable(Variable variable, Object arg)
 {
     if (!replaced)
     {
         if (toReplace.Equals(variable))
         {
             replaced = true;
             return replaceWith;
         }
     }
     return variable;
 }
Beispiel #11
0
 public Object visitVariable(Variable variable, Object arg)
 {
     // This should not be called
     throw new NotImplementedException("visitVariable() should not be called.");
 }
Beispiel #12
0
 // See:
 // http://logic.stanford.edu/classes/cs157/2008/miscellaneous/faq.html#jump165
 // for need for this.
 private void cascadeSubstitution(Dictionary<Variable, Term> theta, Variable var,
         Term x)
 {
     theta.Add(var, x);
     List<Variable> thetaKeys = theta.Keys.ToList<Variable>();
     foreach (Variable v in thetaKeys)
     {
         Term t = theta[v];
         if (theta.ContainsKey(v))
         {
             theta[v] = _substVisitor.subst(theta, t);
         }
         else
         {
             theta.Add(v, _substVisitor.subst(theta, t));
         }
     }
 }
 public virtual Object visitVariable(Variable variable, Object arg)
 {
     return variable.copy();
 }
Beispiel #14
0
        public void testSimpleEquals()
        {
            Term cons1 = new Constant("C1");
            Term cons2 = new Constant("C2");
            Term var1 = new Variable("v1");
            List<Term> pts1 = new List<Term>();
            List<Term> pts2 = new List<Term>();
            pts1.Add(cons1);
            pts1.Add(cons2);
            pts1.Add(var1);
            pts2.Add(cons2);
            pts2.Add(cons1);
            pts2.Add(var1);

            Clause c1 = new Clause();
            Clause c2 = new Clause();
            Assert.IsTrue(c1.Equals(c1));
            Assert.IsTrue(c2.Equals(c2));
            Assert.IsTrue(c1.Equals(c2));
            Assert.IsTrue(c2.Equals(c1));

            // Check negatives
            c1.addNegativeLiteral(new Predicate("Pred1", pts1));
            Assert.IsFalse(c1.Equals(c2));
            Assert.IsFalse(c2.Equals(c1));
            c2.addNegativeLiteral(new Predicate("Pred1", pts1));
            Assert.IsTrue(c1.Equals(c2));
            Assert.IsTrue(c2.Equals(c1));

            c1.addNegativeLiteral(new Predicate("Pred2", pts2));
            Assert.IsFalse(c1.Equals(c2));
            Assert.IsFalse(c2.Equals(c1));
            c2.addNegativeLiteral(new Predicate("Pred2", pts2));
            Assert.IsTrue(c1.Equals(c2));
            Assert.IsTrue(c2.Equals(c1));
            // Check same but added in different order
            c1.addNegativeLiteral(new Predicate("Pred3", pts1));
            Assert.IsFalse(c1.Equals(c2));
            Assert.IsFalse(c2.Equals(c1));
            c1.addNegativeLiteral(new Predicate("Pred4", pts1));
            Assert.IsFalse(c1.Equals(c2));
            Assert.IsFalse(c2.Equals(c1));
            c2.addNegativeLiteral(new Predicate("Pred4", pts1));
            Assert.IsFalse(c1.Equals(c2));
            Assert.IsFalse(c2.Equals(c1));
            c2.addNegativeLiteral(new Predicate("Pred3", pts1));
            Assert.IsTrue(c1.Equals(c2));
            Assert.IsTrue(c2.Equals(c1));

            // Check positives
            c1.addPositiveLiteral(new Predicate("Pred1", pts1));
            Assert.IsFalse(c1.Equals(c2));
            Assert.IsFalse(c2.Equals(c1));
            c2.addPositiveLiteral(new Predicate("Pred1", pts1));
            Assert.IsTrue(c1.Equals(c2));
            Assert.IsTrue(c2.Equals(c1));

            c1.addPositiveLiteral(new Predicate("Pred2", pts2));
            Assert.IsFalse(c1.Equals(c2));
            Assert.IsFalse(c2.Equals(c1));
            c2.addPositiveLiteral(new Predicate("Pred2", pts2));
            Assert.IsTrue(c1.Equals(c2));
            Assert.IsTrue(c2.Equals(c1));
            // Check same but added in different order
            c1.addPositiveLiteral(new Predicate("Pred3", pts1));
            Assert.IsFalse(c1.Equals(c2));
            Assert.IsFalse(c2.Equals(c1));
            c1.addPositiveLiteral(new Predicate("Pred4", pts1));
            Assert.IsFalse(c1.Equals(c2));
            Assert.IsFalse(c2.Equals(c1));
            c2.addPositiveLiteral(new Predicate("Pred4", pts1));
            Assert.IsFalse(c1.Equals(c2));
            Assert.IsFalse(c2.Equals(c1));
            c2.addPositiveLiteral(new Predicate("Pred3", pts1));
            Assert.IsTrue(c1.Equals(c2));
            Assert.IsTrue(c2.Equals(c1));
        }
Beispiel #15
0
        public void testHashCode()
        {
            Term cons1 = new Constant("C1");
            Term cons2 = new Constant("C2");
            Term var1 = new Variable("v1");
            List<Term> pts1 = new List<Term>();
            List<Term> pts2 = new List<Term>();
            pts1.Add(cons1);
            pts1.Add(cons2);
            pts1.Add(var1);
            pts2.Add(cons2);
            pts2.Add(cons1);
            pts2.Add(var1);

            Clause c1 = new Clause();
            Clause c2 = new Clause();
            Assert.AreEqual(c1.GetHashCode(), c2.GetHashCode());

            c1.addNegativeLiteral(new Predicate("Pred1", pts1));
            Assert.AreNotSame(c1.GetHashCode(), c2.GetHashCode());
            c2.addNegativeLiteral(new Predicate("Pred1", pts1));
            Assert.AreEqual(c1.GetHashCode(), c2.GetHashCode());

            c1.addPositiveLiteral(new Predicate("Pred1", pts1));
            Assert.AreNotSame(c1.GetHashCode(), c2.GetHashCode());
            c2.addPositiveLiteral(new Predicate("Pred1", pts1));
            Assert.AreEqual(c1.GetHashCode(), c2.GetHashCode());
        }
Beispiel #16
0
        //
        // PRIVATE METHODS
        //

        /**
         * <code>
         * function UNIFY-VAR(var, x, theta) returns a substitution
         *   inputs: var, a variable
         *       x, any expression
         *       theta, the substitution built up so far
         * </code>
         */
        private Dictionary<Variable, Term> unifyVar(Variable var, FOLNode x,
                Dictionary<Variable, Term> theta)
        {

            if (!(x is Term))
            {
                return null;
            }
            else if (theta.ContainsKey(var))
            {
                // if {var/val} E theta then return UNIFY(val, x, theta)
                return unify(theta[var], x, theta);
            }
            else if (theta.Keys.Contains(x))
            {
                // else if {x/val} E theta then return UNIFY(var, val, theta)
                return unify(var, (FOLNode)theta[(Variable)x], theta);
            }
            else if (occurCheck(theta, var, x))
            {
                // else if OCCUR-CHECK?(var, x) then return failure
                return null;
            }
            else
            {
                // else return add {var/x} to theta
                cascadeSubstitution(theta, var, (Term)x);
                return theta;
            }
        }
Beispiel #17
0
 private bool cascadeOccurCheck(Dictionary<Variable, Term> theta, Variable var,
         List<Variable> varsToCheck, List<Variable> varsCheckedAlready)
 {
     // Want to check if any of the variable to check end up
     // looping back around on the new variable.
     List<Variable> nextLevelToCheck = new List<Variable>();
     foreach (Variable v in varsToCheck)
     {
         Term t = null;
         if (theta.ContainsKey(v))
         {
           t = theta[v];
         }
         if (null == t)
         {
             // Variable may not be a key so skip
             continue;
         }
         if (t.Equals(var))
         {
             // e.g.
             // v1=v2
             // v2=SFO(v1)
             return true;
         }
         else if (t is Function)
         {
             // Need to ensure the function this variable
             // is to be replaced by does not contain var.
             List<Variable> indirectvars = _variableCollector
                     .collectAllVariables(t);
             if (indirectvars.Contains(var))
             {
                 return true;
             }
             else
             {
                 // Determine the next cascade/level
                 // of variables to check for looping
                 foreach (Variable iv in indirectvars)
                 {
                     if (!varsCheckedAlready.Contains(iv))
                     {
                         nextLevelToCheck.Add(iv);
                     }
                 }
             }
         }
     }
     if (nextLevelToCheck.Count > 0)
     {
         varsCheckedAlready.AddRange(nextLevelToCheck);
         return cascadeOccurCheck(theta, var, nextLevelToCheck,
                 varsCheckedAlready);
     }
     return false;
 }
Beispiel #18
0
        public Chain standardizeApart(Chain chain,
                StandardizeApartIndexical standardizeApartIndexical)
        {

            List<Variable> toRename = variableCollector.collectAllVariables(chain);
            Dictionary<Variable, Term> renameSubstitution = new Dictionary<Variable, Term>();

            foreach (Variable var in toRename)
            {
                Variable v = null;
                do
                {
                    v = new Variable(standardizeApartIndexical.getPrefix()
                            + standardizeApartIndexical.getNextIndex());
                    // Ensure the new variable name is not already
                    // accidentally used in the sentence
                } while (toRename.Contains(v));

                renameSubstitution.Add(var, v);
            }

            if (renameSubstitution.Count > 0)
            {
                List<Literal> lits = new List<Literal>();

                foreach (Literal l in chain.getLiterals())
                {
                    AtomicSentence atom = (AtomicSentence)substVisitor.subst(
                            renameSubstitution, l.getAtomicSentence());
                    lits.Add(l.newInstance(atom));
                }

                Chain renamed = new Chain(lits);

                renamed.setProofStep(new ProofStepRenaming(renamed, chain
                        .getProofStep()));

                return renamed;
            }

            return chain;
        }
Beispiel #19
0
        public Object visitQuantifiedSentence(QuantifiedSentence sentence,
                Object arg)
        {
            List<Variable> seenSoFar = (List<Variable>)arg;

            // Keep track of what I have to subst locally and
            // what my renamed variables will be.
            Dictionary<Variable, Term> localSubst = new Dictionary<Variable, Term>();
            List<Variable> replVariables = new List<Variable>();
            foreach (Variable v in sentence.getVariables())
            {
                // If local variable has be renamed already
                // then I need to come up with own name
                if (seenSoFar.Contains(v))
                {
                    Variable sV = new Variable(quantifiedIndexical.getPrefix()
                            + quantifiedIndexical.getNextIndex());
                    localSubst.Add(v, sV);
                    // Replacement variables should contain new name for variable
                    replVariables.Add(sV);
                }
                else
                {
                    // Not already replaced, this name is good
                    replVariables.Add(v);
                }
            }

            // Apply the local subst
            Sentence subst = substVisitor.subst(localSubst, sentence
                    .getQuantified());

            // Ensure all my existing and replaced variable
            // names are tracked
            seenSoFar.AddRange(replVariables);

            Sentence sQuantified = (Sentence)subst.accept(this, arg);

            return new QuantifiedSentence(sentence.getQuantifier(), replVariables,
                    sQuantified);
        }
Beispiel #20
0
        public Dictionary<Variable, Term> standardizeApart(List<Literal> l1Literals,
                List<Literal> l2Literals,
                StandardizeApartIndexical standardizeApartIndexical)
        {
            List<Variable> toRename = new List<Variable>();

            foreach (Literal pl in l1Literals)
            {
                toRename.AddRange(variableCollector.collectAllVariables(pl
                        .getAtomicSentence()));
            }
            foreach (Literal nl in l2Literals)
            {
                toRename.AddRange(variableCollector.collectAllVariables(nl
                        .getAtomicSentence()));
            }

            Dictionary<Variable, Term> renameSubstitution = new Dictionary<Variable, Term>();

            foreach (Variable var in toRename)
            {
                Variable v = null;
                do
                {
                    v = new Variable(standardizeApartIndexical.getPrefix()
                            + standardizeApartIndexical.getNextIndex());
                    // Ensure the new variable name is not already
                    // accidentally used in the sentence
                } while (toRename.Contains(v));
                if (renameSubstitution.ContainsKey(var))
                {
                    renameSubstitution[var] = v;
                }
                else
                {
                    renameSubstitution.Add(var, v);
                }
            }

            List<Literal> posLits = new List<Literal>();
            List<Literal> negLits = new List<Literal>();

            foreach (Literal pl in l1Literals)
            {
                posLits.Add(substVisitor.subst(renameSubstitution, pl));
            }
            foreach (Literal nl in l2Literals)
            {
                negLits.Add(substVisitor.subst(renameSubstitution, nl));
            }

            l1Literals.Clear();
            l1Literals.AddRange(posLits);
            l2Literals.Clear();
            l2Literals.AddRange(negLits);

            return renameSubstitution;
        }
 public Object visitVariable(Variable variable, Object arg)
 {
     return arg;
 }
            public Object visitVariable(Variable variable, Object arg)
            {

                if (null != (substitution = abstractModulation.unifier.unify(toMatch, variable)))
                {
                    if (abstractModulation.isValidMatch(toMatch, toMatchVariables, variable,
                            substitution))
                    {
                        matchingTerm = variable;
                    }
                }

                return variable;
            }