public AnswerHandler(FOLKnowledgeBase kb, Sentence query, long maxQueryTime, FOLModelElimination fOLModelElimination) { finishTime = CommonFactory.Now().AddMilliseconds(maxQueryTime); Sentence refutationQuery = new NotSentence(query); // Want to use an answer literal to pull // query variables where necessary Literal answerLiteral = kb.createAnswerLiteral(refutationQuery); answerLiteralVariables = kb.collectAllVariables(answerLiteral.getAtomicSentence()); // Create the Set of Support based on the Query. if (answerLiteralVariables.Size() > 0) { Sentence refutationQueryWithAnswer = new ConnectedSentence( Connectors.OR, refutationQuery, answerLiteral .getAtomicSentence().copy()); sos = fOLModelElimination.createChainsFromClauses(kb.convertToClauses(refutationQueryWithAnswer)); answerChain.addLiteral(answerLiteral); } else { sos = fOLModelElimination.createChainsFromClauses(kb.convertToClauses(refutationQuery)); } foreach (Chain s in sos) { s.setProofStep(new ProofStepGoal(s)); } }
public TFMAnswerHandler(Literal answerLiteral, ISet <Variable> answerLiteralVariables, Clause answerClause, long maxQueryTime) { this.answerLiteral = answerLiteral; this.answerLiteralVariables = answerLiteralVariables; this.answerClause = answerClause; // this.finishTime = CommonFactory.Now().AddMilliseconds(maxQueryTime); }
public void checkForPossibleAnswers(ISet <Clause> resolvents) { // If no bindings being looked for, then // is just a true false query. foreach (Clause aClause in resolvents) { if (answerClause.isEmpty()) { if (aClause.isEmpty()) { proofs.Add(new ProofFinal(aClause.getProofStep(), CollectionFactory.CreateMap <Variable, Term>())); complete = true; } } else { if (aClause.isEmpty()) { // This should not happen // as added an answer literal, which // implies the database (i.e. premises) are // unsatisfiable to begin with. throw new IllegalStateException("Generated an empty clause while looking for an answer, implies original KB is unsatisfiable"); } if (aClause.isUnitClause() && aClause.isDefiniteClause() && aClause .getPositiveLiterals() .Get(0) .getAtomicSentence() .getSymbolicName() .Equals(answerLiteral.getAtomicSentence() .getSymbolicName())) { IMap <Variable, Term> answerBindings = CollectionFactory.CreateMap <Variable, Term>(); ICollection <Term> answerTerms = aClause.getPositiveLiterals().Get(0).getAtomicSentence().getArgs(); int idx = 0; foreach (Variable v in answerLiteralVariables) { answerBindings.Put(v, answerTerms.Get(idx)); idx++; } bool addNewAnswer = true; foreach (Proof p in proofs) { if (p.getAnswerBindings().SequenceEqual(answerBindings)) { addNewAnswer = false; break; } } if (addNewAnswer) { proofs.Add(new ProofFinal(aClause.getProofStep(), answerBindings)); } } } if (CommonFactory.Now().BiggerThan(finishTime)) { complete = true; // Indicate that I have run out of query time timedOut = true; } } }
public void testBinaryResolventsOrderDoesNotMatter() { // This is a regression test, to ensure // the ordering of resolvents does not matter. // If the order ends up mattering, then likely // a problem was introduced in the Clause class // unifier, or related class. // Set up the initial set of clauses based on the // loves animal domain as it contains functions // new clauses will always be created (i.e. is an // infinite universe of discourse). FOLKnowledgeBase kb = new FOLKnowledgeBase(DomainFactory.lovesAnimalDomain()); kb.tell("FORALL x (FORALL y (Animal(y) => Loves(x, y)) => EXISTS y Loves(y, x))"); kb.tell("FORALL x (EXISTS y (Animal(y) AND Kills(x, y)) => FORALL z NOT(Loves(z, x)))"); kb.tell("FORALL x (Animal(x) => Loves(Jack, x))"); kb.tell("(Kills(Jack, Tuna) OR Kills(Curiosity, Tuna))"); kb.tell("Cat(Tuna)"); kb.tell("FORALL x (Cat(x) => Animal(x))"); ISet <Clause> clauses = CollectionFactory.CreateSet <Clause>(); clauses.AddAll(kb.getAllClauses()); ISet <Clause> newClauses = CollectionFactory.CreateSet <Clause>(); long maxRunTime = 30 * 1000; // 30 seconds IDateTime finishTime = CommonFactory.Now().AddMilliseconds(maxRunTime); do { clauses.AddAll(newClauses); newClauses.Clear(); Clause[] clausesA = clauses.ToArray(); for (int i = 0; i < clausesA.Length; ++i) { Clause cI = clausesA[i]; for (int j = 0; j < clausesA.Length; j++) { Clause cJ = clausesA[j]; newClauses.AddAll(cI.getFactors()); newClauses.AddAll(cJ.getFactors()); ISet <Clause> cIresolvents = cI.binaryResolvents(cJ); ISet <Clause> cJresolvents = cJ.binaryResolvents(cI); if (!cIresolvents.SequenceEqual(cJresolvents)) { System.Console.WriteLine("cI=" + cI); System.Console.WriteLine("cJ=" + cJ); System.Console.WriteLine("cIR=" + cIresolvents); System.Console.WriteLine("cJR=" + cJresolvents); Assert.Fail("Ordering of binary resolvents has become important, which should not be the case"); } foreach (Clause r in cIresolvents) { newClauses.AddAll(r.getFactors()); } if (CommonFactory.Now().BiggerThan(finishTime)) { break; } } if (CommonFactory.Now().BiggerThan(finishTime)) { break; } } } while (CommonFactory.Now().SmallerThan(finishTime)); }
public bool isAnswer(Chain nearParent) { bool isAns = false; if (answerChain.isEmpty()) { if (nearParent.isEmpty()) { proofs.Add(new ProofFinal(nearParent.getProofStep(), CollectionFactory.CreateMap <Variable, Term>())); complete = true; isAns = true; } } else { if (nearParent.isEmpty()) { // This should not happen // as added an answer literal to sos, which // implies the database (i.e. premises) are // unsatisfiable to begin with. throw new IllegalStateException( "Generated an empty chain while looking for an answer, implies original KB is unsatisfiable"); } if (1 == nearParent.getNumberLiterals() && nearParent .getHead() .getAtomicSentence() .getSymbolicName() .Equals(answerChain.getHead() .getAtomicSentence().getSymbolicName())) { IMap <Variable, Term> answerBindings = CollectionFactory.CreateMap <Variable, Term>(); ICollection <Term> answerTerms = nearParent.getHead() .getAtomicSentence().getArgs(); int idx = 0; foreach (Variable v in answerLiteralVariables) { answerBindings.Put(v, answerTerms.Get(idx)); idx++; } bool addNewAnswer = true; foreach (Proof p in proofs) { if (p.getAnswerBindings().SequenceEqual(answerBindings)) { addNewAnswer = false; break; } } if (addNewAnswer) { proofs.Add(new ProofFinal(nearParent.getProofStep(), answerBindings)); } isAns = true; } } if (CommonFactory.Now().BiggerThan(finishTime)) { complete = true; // Indicate that I have run out of query time timedOut = true; } return(isAns); }
public bool timeOutOccurred() { return(CommonFactory.Now().BiggerThan(endTime)); }
public void start() { endTime = CommonFactory.Now().AddMilliseconds(duration); }