public object visitConnectedSentence(ConnectedSentence sentence, object arg) { sentence.getFirst().accept(this, arg); sentence.getSecond().accept(this, arg); return(sentence); }
public object visitConnectedSentence(ConnectedSentence sentence, object arg) { throw new IllegalStateException("visitConnectedSentence() should not be called."); }
// // START-InferenceProcedure public InferenceResult ask(FOLKnowledgeBase KB, Sentence alpha) { // clauses <- the set of clauses in CNF representation of KB ^ ~alpha ISet <Clause> clauses = CollectionFactory.CreateSet <Clause>(); foreach (Clause cIter in KB.getAllClauses()) { Clause c = cIter; c = KB.standardizeApart(c); c.setStandardizedApartCheckNotRequired(); clauses.AddAll(c.getFactors()); } Sentence notAlpha = new NotSentence(alpha); // Want to use an answer literal to pull // query variables where necessary Literal answerLiteral = KB.createAnswerLiteral(notAlpha); ISet <Variable> answerLiteralVariables = KB.collectAllVariables(answerLiteral.getAtomicSentence()); Clause answerClause = new Clause(); if (answerLiteralVariables.Size() > 0) { Sentence notAlphaWithAnswer = new ConnectedSentence(Connectors.OR, notAlpha, answerLiteral.getAtomicSentence()); foreach (Clause cIter in KB.convertToClauses(notAlphaWithAnswer)) { Clause c = cIter; c = KB.standardizeApart(c); c.setProofStep(new ProofStepGoal(c)); c.setStandardizedApartCheckNotRequired(); clauses.AddAll(c.getFactors()); } answerClause.addLiteral(answerLiteral); } else { foreach (Clause cIter in KB.convertToClauses(notAlpha)) { Clause c = cIter; c = KB.standardizeApart(c); c.setProofStep(new ProofStepGoal(c)); c.setStandardizedApartCheckNotRequired(); clauses.AddAll(c.getFactors()); } } TFMAnswerHandler ansHandler = new TFMAnswerHandler(answerLiteral, answerLiteralVariables, answerClause, maxQueryTime); // new <- {} ISet <Clause> newClauses = CollectionFactory.CreateSet <Clause>(); ISet <Clause> toAdd = CollectionFactory.CreateSet <Clause>(); // loop do int noOfPrevClauses = clauses.Size(); do { if (null != tracer) { tracer.stepStartWhile(clauses, clauses.Size(), newClauses.Size()); } newClauses.Clear(); // for each Ci, Cj in clauses do Clause[] clausesA = clauses.ToArray(); // Basically, using the simple T)wo F)inger M)ethod here. for (int i = 0; i < clausesA.Length; i++) { Clause cI = clausesA[i]; if (null != tracer) { tracer.stepOuterFor(cI); } for (int j = i; j < clausesA.Length; j++) { Clause cJ = clausesA[j]; if (null != tracer) { tracer.stepInnerFor(cI, cJ); } // resolvent <- FOL-RESOLVE(Ci, Cj) ISet <Clause> resolvents = cI.binaryResolvents(cJ); if (resolvents.Size() > 0) { toAdd.Clear(); // new <- new <UNION> resolvent foreach (Clause rc in resolvents) { toAdd.AddAll(rc.getFactors()); } if (null != tracer) { tracer.stepResolved(cI, cJ, toAdd); } ansHandler.checkForPossibleAnswers(toAdd); if (ansHandler.isComplete()) { break; } newClauses.AddAll(toAdd); } if (ansHandler.isComplete()) { break; } } if (ansHandler.isComplete()) { break; } } noOfPrevClauses = clauses.Size(); // clauses <- clauses <UNION> new clauses.AddAll(newClauses); if (ansHandler.isComplete()) { break; } // if new is a <SUBSET> of clauses then finished // searching for an answer // (i.e. when they were added the # clauses // did not increase). } while (noOfPrevClauses < clauses.Size()); if (null != tracer) { tracer.stepFinished(clauses, ansHandler); } return(ansHandler); }
public object VisitConnectedSentence(ConnectedSentence sentence, object arg) { sentence.First.Accept(this, arg); sentence.Second.Accept(this, arg); return(sentence); }
// START-InferenceProcedure public InferenceResult ask(FOLKnowledgeBase KB, Sentence alpha) { List <Clause> sos = new List <Clause>(); List <Clause> usable = new List <Clause>(); // Usable set will be the set of clauses in the KB, // are assuming this is satisfiable as using the // Set of Support strategy. foreach (Clause c in KB.getAllClauses()) { Clause c2 = KB.standardizeApart(c); c2.setStandardizedApartCheckNotRequired(); usable.AddRange(c2.getFactors()); } // Ensure reflexivity axiom is added to usable if using paramodulation. if (isUseParamodulation()) { // Reflexivity Axiom: x = x TermEquality reflexivityAxiom = new TermEquality(new Variable("x"), new Variable("x")); Clause reflexivityClause = new Clause(); reflexivityClause.addLiteral(new Literal(reflexivityAxiom)); reflexivityClause = KB.standardizeApart(reflexivityClause); reflexivityClause.setStandardizedApartCheckNotRequired(); usable.Add(reflexivityClause); } Sentence notAlpha = new NotSentence(alpha); // Want to use an answer literal to pull // query variables where necessary Literal answerLiteral = KB.createAnswerLiteral(notAlpha); List <Variable> answerLiteralVariables = KB .collectAllVariables(answerLiteral.getAtomicSentence()); Clause answerClause = new Clause(); if (answerLiteralVariables.Count > 0) { Sentence notAlphaWithAnswer = new ConnectedSentence(Connectors.OR, notAlpha, answerLiteral.getAtomicSentence()); foreach (Clause c in KB.convertToClauses(notAlphaWithAnswer)) { Clause c2 = KB.standardizeApart(c); c2.setProofStep(new ProofStepGoal(c2)); c2.setStandardizedApartCheckNotRequired(); sos.AddRange(c2.getFactors()); } answerClause.addLiteral(answerLiteral); } else { foreach (Clause c in KB.convertToClauses(notAlpha)) { Clause c2 = KB.standardizeApart(c); c2.setProofStep(new ProofStepGoal(c2)); c2.setStandardizedApartCheckNotRequired(); sos.AddRange(c2.getFactors()); } } // Ensure all subsumed clauses are removed foreach (Clause c in SubsumptionElimination.findSubsumedClauses(usable)) { usable.Remove(c); } foreach (Clause c in SubsumptionElimination.findSubsumedClauses(sos)) { sos.Remove(c); } OTTERAnswerHandler ansHandler = new OTTERAnswerHandler(answerLiteral, answerLiteralVariables, answerClause, maxQueryTime); IndexedClauses idxdClauses = new IndexedClauses( getLightestClauseHeuristic(), sos, usable); return(otter(ansHandler, idxdClauses, sos, usable)); }
public object VisitConnectedSentence(ConnectedSentence sentence, object arg) { return(new ConnectedSentence(sentence.Connector, (ISentence)sentence.First.Accept(this, arg), (ISentence)sentence.Second.Accept(this, arg))); }
public Object visitConnectedSentence(ConnectedSentence sentence, Object arg) { throw new InvalidOperationException("Should not be called"); }
public IInferenceResult Ask(FOLKnowledgeBase KB, ISentence alpha) { ISet<Clause> sos = new HashedSet<Clause>(); ISet<Clause> usable = new HashedSet<Clause>(); // Usable set will be the set of clauses in the KB, // are assuming this is satisfiable as using the // ISet of Support strategy. //foreach (var standardizedC in KB.GetAllClauses().Select(c => KB.StandardizeApart(c))) foreach (var standardizedC in KB.GetAllClauses()) { standardizedC.SetStandardizedApartCheckNotRequired(); usable.UnionWith(standardizedC.GetFactors()); } // Ensure reflexivity axiom is added to usable if using paramodulation. if (this.UseParamodulation) { // Reflexivity Axiom: x = x var reflexivityAxiom = new TermEquality(new Variable("x"), new Variable("x")); var reflexivityClause = new Clause(); reflexivityClause.AddLiteral(new Literal(reflexivityAxiom)); reflexivityClause = KB.StandardizeApart(reflexivityClause); reflexivityClause.SetStandardizedApartCheckNotRequired(); usable.Add(reflexivityClause); } ISentence notAlpha = new NotSentence(alpha); // Want to use an answer literal to pull // query variables where necessary var answerLiteral = KB.CreateAnswerLiteral(notAlpha); var answerLiteralVariables = KB .CollectAllVariables(answerLiteral.AtomicSentence); var answerClause = new Clause(); if (answerLiteralVariables.Count > 0) { ISentence notAlphaWithAnswer = new ConnectedSentence(Connectors.Or, notAlpha, answerLiteral.AtomicSentence); // foreach (var standardizedC in // KB.ConvertToClauses(notAlphaWithAnswer).Select(KB.StandardizeApart)) foreach (var standardizedC in KB.ConvertToClauses(notAlphaWithAnswer)) { Clause c = KB.StandardizeApart(standardizedC); c.SetProofStep(new ProofStepGoal(c)); c.SetStandardizedApartCheckNotRequired(); sos.UnionWith(c.GetFactors()); } answerClause.AddLiteral(answerLiteral); } else { //foreach (var standardizedC in // KB.ConvertToClauses(notAlpha).Select(KB.StandardizeApart)) foreach (var standardizedC in KB.ConvertToClauses(notAlpha)) { Clause c = KB.StandardizeApart(standardizedC); c.SetProofStep(new ProofStepGoal(c)); c.SetStandardizedApartCheckNotRequired(); sos.UnionWith(standardizedC.GetFactors()); } } // Ensure all subsumed clauses are removed usable.ExceptWith(SubsumptionElimination.FindSubsumedClauses(usable)); sos.ExceptWith(SubsumptionElimination.FindSubsumedClauses(sos)); var ansHandler = new OTTERAnswerHandler(answerLiteral, answerLiteralVariables, answerClause, this.MaxQueryTime); var idxdClauses = new IndexedClauses(LightestClauseHeuristic, sos, usable); return this.Otter(ansHandler, idxdClauses, sos, usable); }
public object VisitNotSentence(NotSentence notSentence, object arg) { // CNF requires NOT (~) to appear only in literals, so we 'move ~ // inwards' by repeated application of the following equivalences: ISentence negated = notSentence.Negated; // ~(~alpha) equivalent to alpha (double negation elimination) if (negated is NotSentence) { return(((NotSentence)negated).Negated.Accept(this, arg)); } if (negated is ConnectedSentence) { ConnectedSentence negConnected = (ConnectedSentence)negated; ISentence alpha = negConnected.First; ISentence beta = negConnected.Second; // ~(alpha ^ beta) equivalent to (~alpha V ~beta) (De Morgan) if (Connectors.IsAnd(negConnected.Connector)) { // I need to ensure the ~s are moved in deeper ISentence notAlpha = (ISentence)(new NotSentence(alpha)).Accept( this, arg); ISentence notBeta = (ISentence)(new NotSentence(beta)).Accept( this, arg); return(new ConnectedSentence(Connectors.Or, notAlpha, notBeta)); } // ~(alpha V beta) equivalent to (~alpha ^ ~beta) (De Morgan) if (Connectors.IsOr(negConnected.Connector)) { // I need to ensure the ~s are moved in deeper ISentence notAlpha = (ISentence)(new NotSentence(alpha)).Accept( this, arg); ISentence notBeta = (ISentence)(new NotSentence(beta)).Accept( this, arg); return(new ConnectedSentence(Connectors.And, notAlpha, notBeta)); } } // in addition, rules for negated quantifiers: if (negated is QuantifiedSentence) { QuantifiedSentence negQuantified = (QuantifiedSentence)negated; // I need to ensure the ~ is moved in deeper ISentence notP = (ISentence)(new NotSentence(negQuantified .Quantified)).Accept(this, arg); // ~ForAll x p becomes Exists x ~p if (Quantifiers.IsForall(negQuantified.Quantifier)) { return(new QuantifiedSentence(Quantifiers.Exists, negQuantified .Variables, notP)); } // ~Exists x p becomes ForAll x ~p if (Quantifiers.IsExists(negQuantified.Quantifier)) { return(new QuantifiedSentence(Quantifiers.ForAll, negQuantified .Variables, notP)); } } return(new NotSentence((ISentence)negated.Accept(this, arg))); }
public Object visitConnectedSentence(ConnectedSentence sentence, Object arg) { return(new ConnectedSentence(sentence.getConnector(), (Sentence)sentence.getFirst().accept(this, arg), (Sentence)sentence.getSecond().accept(this, arg))); }
public Object visitNotSentence(NotSentence notSentence, Object arg) { // CNF requires NOT (~) to appear only in literals, so we 'move ~ // inwards' by repeated application of the following equivalences: Sentence negated = notSentence.getNegated(); // ~(~alpha) equivalent to alpha (double negation elimination) if (negated is NotSentence) { return(((NotSentence)negated).getNegated().accept(this, arg)); } if (negated is ConnectedSentence) { ConnectedSentence negConnected = (ConnectedSentence)negated; Sentence alpha = negConnected.getFirst(); Sentence beta = negConnected.getSecond(); // ~(alpha ^ beta) equivalent to (~alpha V ~beta) (De Morgan) if (Connectors.isAND(negConnected.getConnector())) { // I need to ensure the ~s are moved in deeper Sentence notAlpha = (Sentence)(new NotSentence(alpha)).accept( this, arg); Sentence notBeta = (Sentence)(new NotSentence(beta)).accept( this, arg); return(new ConnectedSentence(Connectors.OR, notAlpha, notBeta)); } // ~(alpha V beta) equivalent to (~alpha ^ ~beta) (De Morgan) if (Connectors.isOR(negConnected.getConnector())) { // I need to ensure the ~s are moved in deeper Sentence notAlpha = (Sentence)(new NotSentence(alpha)).accept( this, arg); Sentence notBeta = (Sentence)(new NotSentence(beta)).accept( this, arg); return(new ConnectedSentence(Connectors.AND, notAlpha, notBeta)); } } // in addition, rules for negated quantifiers: if (negated is QuantifiedSentence) { QuantifiedSentence negQuantified = (QuantifiedSentence)negated; // I need to ensure the ~ is moved in deeper Sentence notP = (Sentence)(new NotSentence(negQuantified .getQuantified())).accept(this, arg); // ~FORALL x p becomes EXISTS x ~p if (Quantifiers.isFORALL(negQuantified.getQuantifier())) { return(new QuantifiedSentence(Quantifiers.EXISTS, negQuantified .getVariables(), notP)); } // ~EXISTS x p becomes FORALL x ~p if (Quantifiers.isEXISTS(negQuantified.getQuantifier())) { return(new QuantifiedSentence(Quantifiers.FORALL, negQuantified .getVariables(), notP)); } } return(new NotSentence((Sentence)negated.accept(this, arg))); }
public Object visitConnectedSentence(ConnectedSentence sentence, Object arg) { throw new NotImplementedException( "visitConnectedSentence() should not be called."); }
public IInferenceResult Ask(FOLKnowledgeBase kb, ISentence alpha) { // clauses <- the set of clauses in CNF representation of KB ^ ~alpha ISet <Clause> clauses = new HashedSet <Clause>(); foreach (Clause c in kb.GetAllClauses()) { var standardizedC = kb.StandardizeApart(c); standardizedC.SetStandardizedApartCheckNotRequired(); clauses.UnionWith(standardizedC.GetFactors()); } ISentence notAlpha = new NotSentence(alpha); // Want to use an answer literal to pull // query variables where necessary Literal answerLiteral = kb.CreateAnswerLiteral(notAlpha); ISet <Variable> answerLiteralVariables = kb .CollectAllVariables(answerLiteral.AtomicSentence); Clause answerClause = new Clause(); if (answerLiteralVariables.Count > 0) { ISentence notAlphaWithAnswer = new ConnectedSentence(Connectors.Or, notAlpha, answerLiteral.AtomicSentence); foreach (Clause c in kb.ConvertToClauses(notAlphaWithAnswer)) { var standardizedC = kb.StandardizeApart(c); standardizedC.SetProofStep(new ProofStepGoal(standardizedC)); standardizedC.SetStandardizedApartCheckNotRequired(); clauses.UnionWith(standardizedC.GetFactors()); } answerClause.AddLiteral(answerLiteral); } else { foreach (Clause c in kb.ConvertToClauses(notAlpha)) { var standardizedC = kb.StandardizeApart(c); standardizedC.SetProofStep(new ProofStepGoal(standardizedC)); standardizedC.SetStandardizedApartCheckNotRequired(); clauses.UnionWith(standardizedC.GetFactors()); } } var ansHandler = new TFMAnswerHandler(answerLiteral, answerLiteralVariables, answerClause, this.MaxQueryTime); // new <- {} ISet <Clause> newClauses = new HashedSet <Clause>(); ISet <Clause> toAdd = new HashedSet <Clause>(); // loop do int noOfPrevClauses = clauses.Count; do { if (Tracer != null) { Tracer.StepStartWhile(clauses, clauses.Count, newClauses .Count); } newClauses.Clear(); // for each Ci, Cj in clauses do Clause[] clausesA = new Clause[clauses.Count]; clausesA = clauses.ToArray(); // Basically, using the simple T)wo F)inger M)ethod here. for (int i = 0; i < clausesA.Length; i++) { Clause cI = clausesA[i]; if (null != Tracer) { Tracer.StepOuterFor(cI); } for (int j = i; j < clausesA.Length; j++) { Clause cJ = clausesA[j]; if (null != Tracer) { Tracer.StepInnerFor(cI, cJ); } // resolvent <- FOL-RESOLVE(Ci, Cj) ISet <Clause> resolvents = cI.BinaryResolvents(cJ); if (resolvents.Count > 0) { toAdd.Clear(); // new <- new <UNION> resolvent foreach (Clause rc in resolvents) { toAdd.UnionWith(rc.GetFactors()); } if (null != Tracer) { Tracer.StepResolved(cI, cJ, toAdd); } ansHandler.CheckForPossibleAnswers(toAdd); if (ansHandler.IsComplete()) { break; } newClauses.UnionWith(toAdd); } if (ansHandler.IsComplete()) { break; } } if (ansHandler.IsComplete()) { break; } } noOfPrevClauses = clauses.Count; // clauses <- clauses <UNION> new clauses.UnionWith(newClauses); if (ansHandler.IsComplete()) { break; } // if new is a <SUBSET> of clauses then finished // searching for an answer // (i.e. when they were added the # clauses // did not increase). } while (noOfPrevClauses < clauses.Count); if (null != Tracer) { Tracer.StepFinished(clauses, ansHandler); } return(ansHandler); }
public object VisitConnectedSentence(ConnectedSentence sentence, object arg) { throw new InvalidOperationException( "visitConnectedSentence() should not be called."); }