// Note: pg 278, FETCH(q) concept.
        public ISet <IMap <Variable, Term> > fetch(Literal l)
        {
            // Get all of the substitutions in the KB that p unifies with
            ISet <IMap <Variable, Term> > allUnifiers = CollectionFactory.CreateSet <IMap <Variable, Term> >();

            ICollection <Literal> matchingFacts = fetchMatchingFacts(l);

            if (null != matchingFacts)
            {
                foreach (Literal fact in matchingFacts)
                {
                    IMap <Variable, Term> substitution = unifier.unify(l.getAtomicSentence(), fact.getAtomicSentence());
                    if (null != substitution)
                    {
                        allUnifiers.Add(substitution);
                    }
                }
            }

            return(allUnifiers);
        }
Exemple #2
0
        // Note: pg 278, FETCH(q) concept.
        public /* lock */ List <Dictionary <Variable, Term> > fetch(Literal l)
        {
            // Get all of the substitutions in the KB that p unifies with
            List <Dictionary <Variable, Term> > allUnifiers = new List <Dictionary <Variable, Term> >();

            List <Literal> matchingFacts = fetchMatchingFacts(l);

            if (null != matchingFacts)
            {
                foreach (Literal fact in matchingFacts)
                {
                    Dictionary <Variable, Term> substitution = unifier.unify(l
                                                                             .getAtomicSentence(), fact.getAtomicSentence());
                    if (null != substitution)
                    {
                        allUnifiers.Add(substitution);
                    }
                }
            }
            return(allUnifiers);
        }
Exemple #3
0
        private static void unifierDemo()
        {
            FOLParser             parser  = new FOLParser(DomainFactory.knowsDomain());
            Unifier               unifier = new Unifier();
            IMap <Variable, Term> theta   = CollectionFactory.CreateInsertionOrderedMap <Variable, Term>();

            Sentence query         = parser.parse("Knows(John,x)");
            Sentence johnKnowsJane = parser.parse("Knows(y,Mother(y))");

            System.Console.WriteLine("------------");
            System.Console.WriteLine("Unifier Demo");
            System.Console.WriteLine("------------");
            IMap <Variable, Term> subst = unifier.unify(query, johnKnowsJane, theta);

            System.Console.WriteLine("Unify '" + query + "' with '" + johnKnowsJane + "' to get the substitution " + subst + ".");
            System.Console.WriteLine("");
        }
Exemple #4
0
        private static void unifierDemo()
        {
            FOLParser parser  = new FOLParser(DomainFactory.knowsDomain());
            Unifier   unifier = new Unifier();
            var       theta   = new Dictionary <Variable, Term>();

            Sentence query         = parser.parse("Knows(John,x)");
            Sentence johnKnowsJane = parser.parse("Knows(y,Mother(y))");

            System.Console.WriteLine("------------");
            System.Console.WriteLine("Unifier Demo");
            System.Console.WriteLine("------------");
            var subst = unifier.unify(query, johnKnowsJane, theta);

            System.Console.WriteLine("Unify '" + query + "' with '" + johnKnowsJane
                                     + "' to get the substitution " + DictToString(subst) + ".");
            System.Console.WriteLine("");
        }
Exemple #5
0
        // Note: Applies binary resolution rule and factoring
        // Note: returns a set with an empty clause if both clauses
        // are empty, otherwise returns a set of binary resolvents.
        public List <Clause> binaryResolvents(Clause othC)
        {
            List <Clause> resolvents = new List <Clause>();

            // Resolving two empty clauses
            // gives you an empty clause
            if (isEmpty() && othC.isEmpty())
            {
                resolvents.Add(new Clause());
                return(resolvents);
            }

            // Ensure Standardized Apart
            // Before attempting binary resolution
            othC = saIfRequired(othC);

            List <Literal> allPosLits = new List <Literal>();
            List <Literal> allNegLits = new List <Literal>();

            allPosLits.AddRange(this.positiveLiterals);
            allPosLits.AddRange(othC.positiveLiterals);
            allNegLits.AddRange(this.negativeLiterals);
            allNegLits.AddRange(othC.negativeLiterals);

            List <Literal> trPosLits    = new List <Literal>();
            List <Literal> trNegLits    = new List <Literal>();
            List <Literal> copyRPosLits = new List <Literal>();
            List <Literal> copyRNegLits = new List <Literal>();

            for (int i = 0; i < 2; i++)
            {
                trPosLits.Clear();
                trNegLits.Clear();

                if (i == 0)
                {
                    // See if this clauses positives
                    // unify with the other clauses
                    // negatives
                    trPosLits.AddRange(this.positiveLiterals);
                    trNegLits.AddRange(othC.negativeLiterals);
                }
                else
                {
                    // Try the other way round now
                    trPosLits.AddRange(othC.positiveLiterals);
                    trNegLits.AddRange(this.negativeLiterals);
                }

                // Now check to see if they resolve
                Dictionary <Variable, Term> copyRBindings = new Dictionary <Variable, Term>();
                foreach (Literal pl in trPosLits)
                {
                    foreach (Literal nl in trNegLits)
                    {
                        copyRBindings.Clear();
                        if (null != _unifier.unify(pl.getAtomicSentence(), nl
                                                   .getAtomicSentence(), copyRBindings))
                        {
                            copyRPosLits.Clear();
                            copyRNegLits.Clear();
                            bool found = false;
                            foreach (Literal l in allPosLits)
                            {
                                if (!found && pl.Equals(l))
                                {
                                    found = true;
                                    continue;
                                }
                                copyRPosLits.Add(_substVisitor.subst(copyRBindings,
                                                                     l));
                            }
                            found = false;
                            foreach (Literal l in allNegLits)
                            {
                                if (!found && nl.Equals(l))
                                {
                                    found = true;
                                    continue;
                                }
                                copyRNegLits.Add(_substVisitor.subst(copyRBindings,
                                                                     l));
                            }
                            // Ensure the resolvents are standardized apart
                            Dictionary <Variable, Term> renameSubstitituon = _standardizeApart
                                                                             .standardizeApart(copyRPosLits, copyRNegLits,
                                                                                               _saIndexical);
                            Clause c = new Clause(copyRPosLits, copyRNegLits);
                            c.setProofStep(new ProofStepClauseBinaryResolvent(c,
                                                                              this, othC, copyRBindings, renameSubstitituon));
                            if (isImmutable())
                            {
                                c.setImmutable();
                            }
                            if (!isStandardizedApartCheckRequired())
                            {
                                c.setStandardizedApartCheckNotRequired();
                            }
                            resolvents.Add(c);
                        }
                    }
                }
            }

            return(resolvents);
        }
Exemple #6
0
        public void testFailureIfThetaisNull()
        {
            Variable var      = new Variable("x");
            Sentence sentence = parser.parse("Knows(x)");

            theta = null;
            IMap <Variable, Term> result = unifier.unify(var, sentence, theta);

            Assert.IsNull(result);
        }
Exemple #7
0
        public Chain attemptReduction(Chain nearParent, int farParentIndex)
        {
            Chain nnpc = null;

            Literal nearLiteral = nearParent.getHead();

            IMap <string, ICollection <Chain> > candidateHeads = null;

            if (nearLiteral.isPositiveLiteral())
            {
                candidateHeads = negHeads;
            }
            else
            {
                candidateHeads = posHeads;
            }

            AtomicSentence      nearAtom   = nearLiteral.getAtomicSentence();
            string              nearestKey = nearAtom.getSymbolicName();
            ICollection <Chain> farParents = candidateHeads.Get(nearestKey);

            if (null != farParents)
            {
                Chain farParent = farParents.Get(farParentIndex);
                standardizeApart(farParent);
                Literal               farLiteral = farParent.getHead();
                AtomicSentence        farAtom    = farLiteral.getAtomicSentence();
                IMap <Variable, Term> subst      = unifier.unify(nearAtom, farAtom);

                // If I was able to unify with one
                // of the far heads
                if (null != subst)
                {
                    // Want to always apply reduction uniformly
                    Chain   topChain = farParent;
                    Literal botLit   = nearLiteral;
                    Chain   botChain = nearParent;

                    // Need to apply subst to all of the
                    // literals in the reduction
                    ICollection <Literal> reduction = CollectionFactory.CreateQueue <Literal>();
                    foreach (Literal l in topChain.getTail())
                    {
                        AtomicSentence atom = (AtomicSentence)substVisitor.subst(
                            subst, l.getAtomicSentence());
                        reduction.Add(l.newInstance(atom));
                    }
                    reduction.Add(new ReducedLiteral((AtomicSentence)substVisitor
                                                     .subst(subst, botLit.getAtomicSentence()), botLit
                                                     .isNegativeLiteral()));
                    foreach (Literal l in botChain.getTail())
                    {
                        AtomicSentence atom = (AtomicSentence)substVisitor.subst(subst, l.getAtomicSentence());
                        reduction.Add(l.newInstance(atom));
                    }

                    nnpc = new Chain(reduction);
                    nnpc.setProofStep(new ProofStepChainReduction(nnpc, nearParent,
                                                                  farParent, subst));
                }
            }

            return(nnpc);
        }