/** * Utility method for outputting InferenceResults in a formatted textual * representation. * * @param ir * an InferenceResult * @return a String representation of the InferenceResult. */ public static String printInferenceResult(InferenceResult ir) { StringBuilder sb = new StringBuilder(); sb.Append("InferenceResult.isTrue=" + ir.isTrue()); sb.Append("\n"); sb.Append("InferenceResult.isPossiblyFalse=" + ir.isPossiblyFalse()); sb.Append("\n"); sb.Append("InferenceResult.isUnknownDueToTimeout=" + ir.isUnknownDueToTimeout()); sb.Append("\n"); sb.Append("InferenceResult.isPartialResultDueToTimeout=" + ir.isPartialResultDueToTimeout()); sb.Append("\n"); sb.Append("InferenceResult.#Proofs=" + ir.getProofs().Count); sb.Append("\n"); int proofNo = 0; List<Proof.Proof> proofs = ir.getProofs(); foreach (Proof.Proof p in proofs) { proofNo++; sb.Append("InferenceResult.Proof#" + proofNo + "=\n" + ProofPrinter.printProof(p)); } return sb.ToString(); }
/** * Utility method for outputting InferenceResults in a formatted textual * representation. * * @param ir * an InferenceResult * @return a String representation of the InferenceResult. */ public static String printInferenceResult(InferenceResult ir) { StringBuilder sb = new StringBuilder(); sb.Append("InferenceResult.isTrue=" + ir.isTrue()); sb.Append("\n"); sb.Append("InferenceResult.isPossiblyFalse=" + ir.isPossiblyFalse()); sb.Append("\n"); sb.Append("InferenceResult.isUnknownDueToTimeout=" + ir.isUnknownDueToTimeout()); sb.Append("\n"); sb.Append("InferenceResult.isPartialResultDueToTimeout=" + ir.isPartialResultDueToTimeout()); sb.Append("\n"); sb.Append("InferenceResult.#Proofs=" + ir.getProofs().Count); sb.Append("\n"); int proofNo = 0; List <Proof> proofs = ir.getProofs(); foreach (Proof p in proofs) { proofNo++; sb.Append("InferenceResult.Proof#" + proofNo + "=\n" + ProofPrinter.printProof(p)); } return(sb.ToString()); }
protected void testEqualityAndSubstitutionAxiomsKBabcdFFASucceeds( InferenceProcedure infp) { FOLKnowledgeBase akb = FOLKnowledgeBaseFactory .createABCDEqualityAndSubstitutionKnowledgeBase(infp, true); List <Term> terms = new List <Term>(); terms.Add(new Constant("A")); Function fa = new Function("F", terms); terms = new List <Term>(); terms.Add(fa); TermEquality query = new TermEquality(new Function("F", terms), new Constant("A")); InferenceResult answer = akb.ask(query); Assert.IsTrue(null != answer); Assert.IsFalse(answer.isPossiblyFalse()); Assert.IsTrue(answer.isTrue()); Assert.IsFalse(answer.isUnknownDueToTimeout()); Assert.IsFalse(answer.isPartialResultDueToTimeout()); Assert.IsTrue(1 == answer.getProofs().Count); Assert.IsTrue(0 == answer.getProofs()[0].getAnswerBindings() .Count); }
protected void testFullFOLKBLovesAnimalQueryKillsJackTunaFalse(InferenceProcedure infp, bool expectedToTimeOut) { FOLKnowledgeBase akb = FOLKnowledgeBaseFactory.createLovesAnimalKnowledgeBase(infp); ICollection <Term> terms = CollectionFactory.CreateQueue <Term>(); terms.Add(new Constant("Jack")); terms.Add(new Constant("Tuna")); Predicate query = new Predicate("Kills", terms); InferenceResult answer = akb.ask(query); Assert.IsTrue(null != answer); if (expectedToTimeOut) { Assert.IsFalse(answer.isPossiblyFalse()); Assert.IsFalse(answer.isTrue()); Assert.IsTrue(answer.isUnknownDueToTimeout()); Assert.IsFalse(answer.isPartialResultDueToTimeout()); Assert.IsTrue(0 == answer.getProofs().Size()); } else { Assert.IsTrue(answer.isPossiblyFalse()); Assert.IsFalse(answer.isTrue()); Assert.IsFalse(answer.isUnknownDueToTimeout()); Assert.IsFalse(answer.isPartialResultDueToTimeout()); Assert.IsTrue(0 == answer.getProofs().Size()); } }
protected void testEqualityNoAxiomsKBabcAEqualsCSucceeds(InferenceProcedure infp, bool expectedToFail) { FOLKnowledgeBase akb = FOLKnowledgeBaseFactory.createABCEqualityKnowledgeBase(infp, false); TermEquality query = new TermEquality(new Constant("A"), new Constant("C")); InferenceResult answer = akb.ask(query); Assert.IsTrue(null != answer); if (expectedToFail) { Assert.IsTrue(answer.isPossiblyFalse()); Assert.IsFalse(answer.isTrue()); Assert.IsFalse(answer.isUnknownDueToTimeout()); Assert.IsFalse(answer.isPartialResultDueToTimeout()); Assert.IsTrue(0 == answer.getProofs().Size()); } else { Assert.IsFalse(answer.isPossiblyFalse()); Assert.IsTrue(answer.isTrue()); Assert.IsFalse(answer.isUnknownDueToTimeout()); Assert.IsFalse(answer.isPartialResultDueToTimeout()); Assert.IsTrue(1 == answer.getProofs().Size()); Assert.IsTrue(0 == answer.getProofs().Get(0) .getAnswerBindings().Size()); } }
protected void testEqualityAndSubstitutionNoAxiomsKBabcdPDSucceeds(InferenceProcedure infp, bool expectedToFail) { FOLKnowledgeBase akb = FOLKnowledgeBaseFactory.createABCDEqualityAndSubstitutionKnowledgeBase(infp, false); ICollection <Term> terms = CollectionFactory.CreateQueue <Term>(); terms.Add(new Constant("D")); Predicate query = new Predicate("P", terms); InferenceResult answer = akb.ask(query); Assert.IsTrue(null != answer); if (expectedToFail) { Assert.IsTrue(answer.isPossiblyFalse()); Assert.IsFalse(answer.isTrue()); Assert.IsFalse(answer.isUnknownDueToTimeout()); Assert.IsFalse(answer.isPartialResultDueToTimeout()); Assert.IsTrue(0 == answer.getProofs().Size()); } else { Assert.IsFalse(answer.isPossiblyFalse()); Assert.IsTrue(answer.isTrue()); Assert.IsFalse(answer.isUnknownDueToTimeout()); Assert.IsFalse(answer.isPartialResultDueToTimeout()); Assert.IsTrue(1 == answer.getProofs().Size()); Assert.IsTrue(0 == answer.getProofs().Get(0) .getAnswerBindings().Size()); } }
protected void testFullFOLKBLovesAnimalQueryNotKillsJackTunaSucceeds( InferenceProcedure infp, bool expectedToTimeOut) { FOLKnowledgeBase akb = FOLKnowledgeBaseFactory .createLovesAnimalKnowledgeBase(infp); List <Term> terms = new List <Term>(); terms.Add(new Constant("Jack")); terms.Add(new Constant("Tuna")); NotSentence query = new NotSentence(new Predicate("Kills", terms)); InferenceResult answer = akb.ask(query); Assert.IsTrue(null != answer); if (expectedToTimeOut) { Assert.IsFalse(answer.isPossiblyFalse()); Assert.IsFalse(answer.isTrue()); Assert.IsTrue(answer.isUnknownDueToTimeout()); Assert.IsFalse(answer.isPartialResultDueToTimeout()); Assert.IsTrue(0 == answer.getProofs().Count); } else { Assert.IsFalse(answer.isPossiblyFalse()); Assert.IsTrue(answer.isTrue()); Assert.IsFalse(answer.isUnknownDueToTimeout()); Assert.IsFalse(answer.isPartialResultDueToTimeout()); Assert.IsTrue(1 == answer.getProofs().Count); Assert.IsTrue(0 == answer.getProofs()[0] .getAnswerBindings().Count); } }
public InferenceResult ask(Sentence query) { // Want to standardize apart the query to ensure // it does not clash with any of the sentences // in the database StandardizeApartResult saResult = _standardizeApart.standardizeApart(query, queryIndexical); // Need to map the result variables (as they are standardized apart) // to the original queries variables so that the caller can easily // understand and use the returned set of substitutions InferenceResult infResult = getInferenceProcedure().ask(this, saResult.getStandardized()); foreach (Proof p in infResult.getProofs()) { IMap <Variable, Term> im = p.getAnswerBindings(); IMap <Variable, Term> em = CollectionFactory.CreateInsertionOrderedMap <Variable, Term>(); foreach (Variable rev in saResult.getReverseSubstitution().GetKeys()) { em.Put((Variable)saResult.getReverseSubstitution().Get(rev), im.Get(rev)); } p.replaceAnswerBindings(em); } return(infResult); }
protected static void lovesAnimalDemo(InferenceProcedure ip) { StandardizeApartIndexicalFactory.flush(); FOLKnowledgeBase kb = FOLKnowledgeBaseFactory.createLovesAnimalKnowledgeBase(ip); string kbStr = kb.ToString(); ICollection <Term> terms = CollectionFactory.CreateQueue <Term>(); terms.Add(new Constant("Curiosity")); terms.Add(new Constant("Tuna")); Predicate query = new Predicate("Kills", terms); InferenceResult answer = kb.ask(query); System.Console.WriteLine("Loves Animal Knowledge Base:"); System.Console.WriteLine(kbStr); System.Console.WriteLine("Query: " + query); foreach (Proof p in answer.getProofs()) { System.Console.Write(ProofPrinter.printProof(p)); System.Console.WriteLine(""); } }
public InferenceResult ask(Sentence aQuery) { // Want to standardize apart the query to ensure // it does not clash with any of the sentences // in the database StandardizeApartResult saResult = _standardizeApart.standardizeApart( aQuery, queryIndexical); // Need to map the result variables (as they are standardized apart) // to the original queries variables so that the caller can easily // understand and use the returned set of substitutions InferenceResult infResult = getInferenceProcedure().ask(this, saResult.getStandardized()); List <Proof> proofs = infResult.getProofs(); foreach (Proof p in proofs) { Dictionary <Variable, Term> im = p.getAnswerBindings(); Dictionary <Variable, Term> em = new Dictionary <Variable, Term>(); foreach (Variable rev in saResult.getReverseSubstitution().Keys) { Term toInsert = im.ContainsKey(rev) ? im[rev] : null; em.Add((Variable)saResult.getReverseSubstitution()[rev], toInsert); } p.replaceAnswerBindings(em); } return(infResult); }
private static void kingsDemo1(InferenceProcedure ip) { StandardizeApartIndexicalFactory.flush(); FOLKnowledgeBase kb = FOLKnowledgeBaseFactory .createKingsKnowledgeBase(ip); String kbStr = kb.ToString(); List <Term> terms = new List <Term>(); terms.Add(new Constant("John")); Predicate query = new Predicate("Evil", terms); InferenceResult answer = kb.ask(query); System.Console.WriteLine("Kings Knowledge Base:"); System.Console.WriteLine(kbStr); System.Console.WriteLine("Query: " + query); foreach (Proof p in answer.getProofs()) { System.Console.Write(ProofPrinter.printProof(p)); System.Console.WriteLine(""); } }
protected void testDefiniteClauseKBKingsQueryJohnEvilSucceeds(InferenceProcedure infp) { FOLKnowledgeBase kkb = FOLKnowledgeBaseFactory.createKingsKnowledgeBase(infp); ICollection <Term> terms = CollectionFactory.CreateQueue <Term>(); terms.Add(new Constant("John")); Predicate query = new Predicate("Evil", terms); InferenceResult answer = kkb.ask(query); Assert.IsTrue(null != answer); Assert.IsFalse(answer.isPossiblyFalse()); Assert.IsTrue(answer.isTrue()); Assert.IsFalse(answer.isUnknownDueToTimeout()); Assert.IsFalse(answer.isPartialResultDueToTimeout()); Assert.IsTrue(1 == answer.getProofs().Size()); Assert.IsTrue(0 == answer.getProofs().Get(0).getAnswerBindings().Size()); }
protected void testDefiniteClauseKBKingsQueryKingXReturnsJohnAndRichardSucceeds( InferenceProcedure infp) { FOLKnowledgeBase kkb = FOLKnowledgeBaseFactory .createKingsKnowledgeBase(infp); List <Term> terms = new List <Term>(); terms.Add(new Variable("x")); Predicate query = new Predicate("King", terms); InferenceResult answer = kkb.ask(query); Assert.IsTrue(null != answer); Assert.IsFalse(answer.isPossiblyFalse()); Assert.IsTrue(answer.isTrue()); Assert.IsFalse(answer.isUnknownDueToTimeout()); Assert.IsFalse(answer.isPartialResultDueToTimeout()); Assert.IsTrue(2 == answer.getProofs().Count); Assert.IsTrue(1 == answer.getProofs()[0].getAnswerBindings() .Count); Assert.IsTrue(1 == answer.getProofs()[1].getAnswerBindings() .Count); bool gotJohn, gotRichard; gotJohn = gotRichard = false; Constant cJohn = new Constant("John"); Constant cRichard = new Constant("Richard"); foreach (Proof p in answer.getProofs()) { Dictionary <Variable, Term> ans = p.getAnswerBindings(); Assert.AreEqual(1, ans.Count); if (cJohn.Equals(ans[new Variable("x")])) { gotJohn = true; } if (cRichard.Equals(ans[new Variable("x")])) { gotRichard = true; } } Assert.IsTrue(gotJohn); Assert.IsTrue(gotRichard); }
protected void testDefiniteClauseKBWeaponsQueryCriminalXReturnsWestSucceeds(InferenceProcedure infp) { FOLKnowledgeBase wkb = FOLKnowledgeBaseFactory.createWeaponsKnowledgeBase(infp); ICollection <Term> terms = CollectionFactory.CreateQueue <Term>(); terms.Add(new Variable("x")); Predicate query = new Predicate("Criminal", terms); InferenceResult answer = wkb.ask(query); Assert.IsTrue(null != answer); Assert.IsFalse(answer.isPossiblyFalse()); Assert.IsTrue(answer.isTrue()); Assert.IsFalse(answer.isUnknownDueToTimeout()); Assert.IsFalse(answer.isPartialResultDueToTimeout()); Assert.IsTrue(1 == answer.getProofs().Size()); Assert.IsTrue(1 == answer.getProofs().Get(0).getAnswerBindings().Size()); Assert.AreEqual(new Constant("West"), answer.getProofs().Get(0).getAnswerBindings().Get(new Variable("x"))); }
public List <string> GetProof(InferenceResult result) { var toReturn = new List <string>(); Iterator terator = result.getProofs().iterator(); while (terator.hasNext()) { toReturn.AddRange(ProofPrinter.printProof((Proof)terator.next()).Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries)); } return(toReturn); }
public void testExhaustsSearchSpace() { // Taken from AIMA pg 679 FOLDomain domain = new FOLDomain(); domain.addPredicate("alternate"); domain.addPredicate("bar"); domain.addPredicate("fri_sat"); domain.addPredicate("hungry"); domain.addPredicate("patrons"); domain.addPredicate("price"); domain.addPredicate("raining"); domain.addPredicate("reservation"); domain.addPredicate("type"); domain.addPredicate("wait_estimate"); domain.addPredicate("will_wait"); domain.addConstant("Some"); domain.addConstant("Full"); domain.addConstant("French"); domain.addConstant("Thai"); domain.addConstant("Burger"); domain.addConstant("$"); domain.addConstant("_30_60"); domain.addConstant("X0"); FOLParser parser = new FOLParser(domain); // The hypothesis String c1 = "patrons(v,Some)"; String c2 = "patrons(v,Full) AND (hungry(v) AND type(v,French))"; String c3 = "patrons(v,Full) AND (hungry(v) AND (type(v,Thai) AND fri_sat(v)))"; String c4 = "patrons(v,Full) AND (hungry(v) AND type(v,Burger))"; String sh = "FORALL v (will_wait(v) <=> (" + c1 + " OR (" + c2 + " OR (" + c3 + " OR (" + c4 + ")))))"; Sentence hypothesis = parser.parse(sh); Sentence desc = parser .parse("(((((((((alternate(X0) AND NOT(bar(X0))) AND NOT(fri_sat(X0))) AND hungry(X0)) AND patrons(X0,Full)) AND price(X0,$)) AND NOT(raining(X0))) AND NOT(reservation(X0))) AND type(X0,Thai)) AND wait_estimate(X0,_30_60))"); Sentence classification = parser.parse("will_wait(X0)"); FOLKnowledgeBase kb = new FOLKnowledgeBase(domain, new FOLOTTERLikeTheoremProver(false)); kb.tell(hypothesis); kb.tell(desc); InferenceResult ir = kb.ask(classification); Assert.IsFalse(ir.isTrue()); Assert.IsTrue(ir.isPossiblyFalse()); Assert.IsFalse(ir.isUnknownDueToTimeout()); Assert.IsFalse(ir.isPartialResultDueToTimeout()); Assert.AreEqual(0, ir.getProofs().Count); }
protected void testHornClauseKBRingOfThievesQuerySkisXReturnsNancyRedBertDrew(InferenceProcedure infp) { FOLKnowledgeBase rotkb = FOLKnowledgeBaseFactory.createRingOfThievesKnowledgeBase(infp); ICollection <Term> terms = CollectionFactory.CreateQueue <Term>(); terms.Add(new Variable("x")); Predicate query = new Predicate("Skis", terms); InferenceResult answer = rotkb.ask(query); Assert.IsTrue(null != answer); Assert.IsFalse(answer.isPossiblyFalse()); Assert.IsTrue(answer.isTrue()); Assert.IsFalse(answer.isUnknownDueToTimeout()); // DB can expand infinitely so is only partial. Assert.IsTrue(answer.isPartialResultDueToTimeout()); Assert.AreEqual(4, answer.getProofs().Size()); Assert.AreEqual(1, answer.getProofs().Get(0).getAnswerBindings().Size()); Assert.AreEqual(1, answer.getProofs().Get(1).getAnswerBindings().Size()); Assert.AreEqual(1, answer.getProofs().Get(2).getAnswerBindings().Size()); Assert.AreEqual(1, answer.getProofs().Get(3).getAnswerBindings().Size()); ICollection <Constant> expected = CollectionFactory.CreateQueue <Constant>(); expected.Add(new Constant("Nancy")); expected.Add(new Constant("Red")); expected.Add(new Constant("Bert")); expected.Add(new Constant("Drew")); foreach (Proof p in answer.getProofs()) { expected.Remove(p.getAnswerBindings().Get(new Variable("x")) as Constant); } Assert.AreEqual(0, expected.Size()); }
protected void testDefiniteClauseKBKingsQueryEvilXReturnsJohnSucceeds( InferenceProcedure infp) { FOLKnowledgeBase kkb = FOLKnowledgeBaseFactory .createKingsKnowledgeBase(infp); List <Term> terms = new List <Term>(); terms.Add(new Variable("x")); Predicate query = new Predicate("Evil", terms); InferenceResult answer = kkb.ask(query); Assert.IsTrue(null != answer); Assert.IsFalse(answer.isPossiblyFalse()); Assert.IsTrue(answer.isTrue()); Assert.IsFalse(answer.isUnknownDueToTimeout()); Assert.IsFalse(answer.isPartialResultDueToTimeout()); Assert.IsTrue(1 == answer.getProofs().Count); Assert.IsTrue(1 == answer.getProofs()[0].getAnswerBindings() .Count); Assert.AreEqual(new Constant("John"), answer.getProofs()[0] .getAnswerBindings()[new Variable("x")]); }
/** * Utility method for outputting InferenceResults in a formatted textual * representation. * * @param ir * an InferenceResult * @return a string representation of the InferenceResult. */ public static string printInferenceResult(InferenceResult ir) { IStringBuilder sb = TextFactory.CreateStringBuilder(); sb.Append("InferenceResult.isTrue=" + ir.isTrue()); sb.Append("\n"); sb.Append("InferenceResult.isPossiblyFalse=" + ir.isPossiblyFalse()); sb.Append("\n"); sb.Append("InferenceResult.isUnknownDueToTimeout=" + ir.isUnknownDueToTimeout()); sb.Append("\n"); sb.Append("InferenceResult.isPartialResultDueToTimeout=" + ir.isPartialResultDueToTimeout()); sb.Append("\n"); sb.Append("InferenceResult.#Proofs=" + ir.getProofs().Size()); sb.Append("\n"); int proofNo = 0; foreach (Proof p in ir.getProofs()) { proofNo++; sb.Append("InferenceResult.Proof#" + proofNo + "=\n" + ProofPrinter.printProof(p)); } return(sb.ToString()); }
protected void testDefiniteClauseKBKingsQueryCriminalXFalse(InferenceProcedure infp) { FOLKnowledgeBase kkb = FOLKnowledgeBaseFactory.createKingsKnowledgeBase(infp); ICollection <Term> terms = CollectionFactory.CreateQueue <Term>(); terms.Add(new Variable("x")); Predicate query = new Predicate("Criminal", terms); InferenceResult answer = kkb.ask(query); Assert.IsTrue(null != answer); Assert.IsTrue(answer.isPossiblyFalse()); Assert.IsFalse(answer.isTrue()); Assert.IsFalse(answer.isUnknownDueToTimeout()); Assert.IsFalse(answer.isPartialResultDueToTimeout()); Assert.IsTrue(0 == answer.getProofs().Size()); }
protected void testDefiniteClauseKBKingsQueryRichardEvilFalse( InferenceProcedure infp) { FOLKnowledgeBase kkb = FOLKnowledgeBaseFactory .createKingsKnowledgeBase(infp); List <Term> terms = new List <Term>(); terms.Add(new Constant("Richard")); Predicate query = new Predicate("Evil", terms); InferenceResult answer = kkb.ask(query); Assert.IsTrue(null != answer); Assert.IsTrue(answer.isPossiblyFalse()); Assert.IsFalse(answer.isTrue()); Assert.IsFalse(answer.isUnknownDueToTimeout()); Assert.IsFalse(answer.isPartialResultDueToTimeout()); Assert.IsTrue(0 == answer.getProofs().Count); }
protected static void abcEqualityNoAxiomDemo(InferenceProcedure ip) { StandardizeApartIndexicalFactory.flush(); FOLKnowledgeBase kb = FOLKnowledgeBaseFactory.createABCEqualityKnowledgeBase(ip, false); string kbStr = kb.ToString(); TermEquality query = new TermEquality(new Constant("A"), new Constant("C")); InferenceResult answer = kb.ask(query); System.Console.WriteLine("ABC Equality No Axiom Knowledge Base:"); System.Console.WriteLine(kbStr); System.Console.WriteLine("Query: " + query); foreach (Proof p in answer.getProofs()) { System.Console.Write(ProofPrinter.printProof(p)); System.Console.WriteLine(""); } }
protected static void weaponsDemo(InferenceProcedure ip) { StandardizeApartIndexicalFactory.flush(); FOLKnowledgeBase kb = FOLKnowledgeBaseFactory.createWeaponsKnowledgeBase(ip); string kbStr = kb.ToString(); ICollection <Term> terms = CollectionFactory.CreateQueue <Term>(); terms.Add(new Variable("x")); Predicate query = new Predicate("Criminal", terms); InferenceResult answer = kb.ask(query); System.Console.WriteLine("Weapons Knowledge Base:"); System.Console.WriteLine(kbStr); System.Console.WriteLine("Query: " + query); foreach (Proof p in answer.getProofs()) { System.Console.Write(ProofPrinter.printProof(p)); System.Console.WriteLine(""); } }