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]); }
// // PRIVATE METHODS // private void constructFOLEg() { ithExampleConstant = new Constant(folDSDomain.getExampleConstant(egNo)); List<Term> terms = new List<Term>(); terms.Add(ithExampleConstant); // Create the classification sentence classification = new Predicate(folDSDomain.getGoalPredicateName(), terms); if (!example.getAttributeValueAsString( folDSDomain.getDataSetTargetName()).Equals( folDSDomain.getTrueGoalValue())) { // if not true then needs to be a Not sentence classification = new NotSentence(classification); } // Create the description sentence List<Sentence> descParts = new List<Sentence>(); foreach (String dname in folDSDomain.getDescriptionDataSetNames()) { String foldDName = folDSDomain.getFOLName(dname); terms = new List<Term>(); terms.Add(ithExampleConstant); // If multivalued becomes a two place predicate // e.g: Patrons(X1, Some) // otherwise: Hungry(X1) or ~ Hungry(X1) // see pg 769 of AIMA Sentence part = null; if (folDSDomain.isMultivalued(dname)) { terms.Add(new Constant(folDSDomain.getFOLName(example .getAttributeValueAsString(dname)))); part = new Predicate(foldDName, terms); } else { part = new Predicate(foldDName, terms); // Need to determine if false if (!folDSDomain.getTrueGoalValue().Equals( example.getAttributeValueAsString(dname))) { part = new NotSentence(part); } } descParts.Add(part); } if (descParts.Count == 1) { description = descParts[0]; } else if (descParts.Count > 1) { description = new ConnectedSentence(Connectors.AND, descParts [0], descParts[1]); for (int i = 2; i < descParts.Count; i++) { description = new ConnectedSentence(Connectors.AND, description, descParts[i]); } } }
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); }
public Object visitConstant(Constant constant, Object arg) { return arg; }
public Object visitConstant(Constant constant, Object arg) { // This should not be called throw new NotImplementedException("visitConstant() should not be called."); }
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)); }
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()); }
public Object visitConstant(Constant constant, Object arg) { if (!replaced) { if (toReplace.Equals(constant)) { replaced = true; return replaceWith; } } return constant; }
public Object visitConstant(Constant constant, Object arg) { if (null != (substitution = abstractModulation.unifier.unify(toMatch, constant))) { if (abstractModulation.isValidMatch(toMatch, toMatchVariables, constant, substitution)) { matchingTerm = constant; } } return constant; }