public bool?visitBinarySentence(ComplexSentence bs, bool?arg) { bool?firstValue = bs.getSimplerSentence(0).accept(this, null); bool?secondValue = bs.getSimplerSentence(1).accept(this, null); if ((firstValue == null) || (secondValue == null)) { // strictly not true for or/and // -FIX later return(null); } else { Connective connective = bs.getConnective(); if (connective.Equals(Connective.AND)) { return(firstValue.Value && secondValue.Value); } else if (connective.Equals(Connective.OR)) { return(firstValue.Value || secondValue.Value); } else if (connective.Equals(Connective.IMPLICATION)) { return(!(firstValue.Value && !secondValue.Value)); } else if (connective.Equals(Connective.BICONDITIONAL)) { return(firstValue.Equals(secondValue)); } return(null); } }
public virtual Sentence visitBinarySentence(ComplexSentence s, A arg) { // a new Complex Sentence with the same connective but possibly // with its simpler sentences replaced by the visitor. return(new ComplexSentence(s.getConnective(), s.getSimplerSentence(0) .accept(this, arg), s.getSimplerSentence(1).accept(this, arg))); }
public virtual ISet <T> visitBinarySentence(ComplexSentence s, ISet <T> arg) { ISet <T> termunion = SetOps.union( s.getSimplerSentence(0).accept(this, arg), s .getSimplerSentence(1).accept(this, arg)); return(SetOps.union(arg, termunion)); }
protected ISet <Clause> setOfClausesInTheCNFRepresentationOfKBAndNotAlpha(KnowledgeBase kb, Sentence alpha) { // KB & ~alpha; Sentence isContradiction = new ComplexSentence(Connective.AND, kb.asSentence(), new ComplexSentence(Connective.NOT, alpha)); // the set of clauses in the CNF representation ISet <Clause> clauses = CollectionFactory.CreateSet <Clause>(ConvertToConjunctionOfClauses.convert(isContradiction).getClauses()); discardTautologies(clauses); return(clauses); }
public bool?visitUnarySentence(ComplexSentence fs, bool?arg) { object negatedValue = fs.getSimplerSentence(0).accept(this, null); if (negatedValue != null) { return(!((bool)negatedValue)); } else { return(null); } }
public override ISet <Literal> visitBinarySentence(ComplexSentence s, ISet <Literal> arg) { if (s.isOrSentence()) { s.getSimplerSentence(0).accept(this, arg); s.getSimplerSentence(1).accept(this, arg); } else { throw new IllegalArgumentException("Sentence is not in CNF: " + s); } return(arg); }
// No hace falta override porque PLVisitor es una interfaz public bool?VisitBinarySentence(ComplexSentence bs, bool?arg) { bool? firstValue = (bool?)bs.GetSimplerSentence(0).Accept(this, null); bool? secondValue = (bool?)bs.GetSimplerSentence(1).Accept(this, null); bool bothValuesKnown = firstValue != null && secondValue != null; Connective connective = bs.GetConnective(); // He reprogramado todo esto if (connective.Equals(Connective.AND)) { if (firstValue.Equals(false) || secondValue.Equals(false)) { return(false); } else if (bothValuesKnown) { return(true); // Si no se cumple, saldrá como null } } else if (connective.Equals(Connective.OR)) { if (firstValue.Equals(true) || secondValue.Equals(true)) { return(true); } else if (bothValuesKnown) { return(false); // Si no se cumple, saldrá como null } } else if (connective.Equals(Connective.IMPLICATION)) { if (firstValue.Equals(false) || secondValue.Equals(true)) { return(true); } else if (bothValuesKnown) { return(false); // Si no se cumple, saldrá como null } } else if (connective.Equals(Connective.BICONDITIONAL)) { if (bothValuesKnown) { return(firstValue.Equals(secondValue)); // Si no se cumple, saldrá como null } } return(null); }
public override ISet <Clause> visitUnarySentence(ComplexSentence s, ISet <Clause> arg) { if (!s.getSimplerSentence(0).isPropositionSymbol()) { throw new IllegalStateException("Sentence is not in CNF: " + s); } // a negative unit clause Literal negativeLiteral = new Literal((PropositionSymbol)s.getSimplerSentence(0), false); arg.Add(new Clause(negativeLiteral)); return(arg); }
public override ISet <Literal> VisitUnarySentence(ComplexSentence s, ISet <Literal> arg) { if (!s.GetSimplerSentence(0).IsPropositionSymbol()) { throw new InvalidOperationException("Sentence is not in CNF: " + s); //IllegalStateException } // un literal negativo Literal negativeLiteral = new Literal((PropositionSymbol)s.GetSimplerSentence(0), false); arg.Add(negativeLiteral); return(arg); }
// // SUPPORTING CODE // /** * Determine if KB |= α, i.e. alpha is entailed by KB. * * @param kb * a Knowledge Base in propositional logic. * @param alpha * a propositional sentence. * @return true, if α is entailed by KB, false otherwise. */ public bool isEntailed(KnowledgeBase kb, Sentence alpha) { // AIMA3e p.g. 260: kb |= alpha, can be done by testing // unsatisfiability of kb & ~alpha. ISet <Clause> kbAndNotAlpha = CollectionFactory.CreateSet <Clause>(); Sentence notQuery = new ComplexSentence(Connective.NOT, alpha); ISet <PropositionSymbol> symbols = CollectionFactory.CreateSet <PropositionSymbol>(); ICollection <PropositionSymbol> querySymbols = CollectionFactory.CreateQueue <PropositionSymbol>(SymbolCollector.getSymbolsFrom(notQuery)); kbAndNotAlpha.AddAll(kb.asCNF()); kbAndNotAlpha.AddAll(ConvertToConjunctionOfClauses.convert(notQuery).getClauses()); symbols.AddAll(querySymbols); symbols.AddAll(kb.getSymbols()); return(!dpll(kbAndNotAlpha, CollectionFactory.CreateQueue <PropositionSymbol>(symbols), new Model())); }
public override Sentence VisitUnarySentence(ComplexSentence s, object arg) { Sentence result = null; Sentence negated = s.GetSimplerSentence(0); if (negated.IsPropositionSymbol()) { // Ya se ha movido completamente result = s; } else if (negated.IsNotSentence()) { // ~(~α) ≡ α (eliminación de la doble negación) Sentence alpha = negated.GetSimplerSentence(0); result = alpha.Accept(this, arg); } else if (negated.IsAndSentence() || negated.IsOrSentence()) { Sentence alpha = negated.GetSimplerSentence(0); Sentence beta = negated.GetSimplerSentence(1); // Esto asegura que la eliminación de la doble negación sucede Sentence notAlpha = (new ComplexSentence(Connective.NOT, alpha)) .Accept(this, null); Sentence notBeta = (new ComplexSentence(Connective.NOT, beta)) .Accept(this, null); if (negated.IsAndSentence()) { // ~(α & β) ≡ (~α | ~β) (De Morgan) result = new ComplexSentence(Connective.OR, notAlpha, notBeta); } else { // ~(α | β) ≡ (~α & ~β) (De Morgan) result = new ComplexSentence(Connective.AND, notAlpha, notBeta); } } else { throw new ArgumentException( //IllegalArgumentException "Biconditionals and Implications should not exist in input: " + s); } return(result); }
public override Sentence visitUnarySentence(ComplexSentence s, object arg) { Sentence result = null; Sentence negated = s.getSimplerSentence(0); if (negated.isPropositionSymbol()) { // Already moved in fully result = s; } else if (negated.isNotSentence()) { // ~(~α) ≡ α (double-negation elimination) Sentence alpha = negated.getSimplerSentence(0); result = alpha.accept(this, arg); } else if (negated.isAndSentence() || negated.isOrSentence()) { Sentence alpha = negated.getSimplerSentence(0); Sentence beta = negated.getSimplerSentence(1); // This ensures double-negation elimination happens Sentence notAlpha = (new ComplexSentence(Connective.NOT, alpha)).accept(this, null); Sentence notBeta = (new ComplexSentence(Connective.NOT, beta)).accept(this, null); if (negated.isAndSentence()) { // ~(α & β) ≡ (~α | ~β) (De Morgan) result = new ComplexSentence(Connective.OR, notAlpha, notBeta); } else { // ~(α | β) ≡ (~α & ~β) (De Morgan) result = new ComplexSentence(Connective.AND, notAlpha, notBeta); } } else { throw new IllegalArgumentException("Biconditionals and Implications should not exist in input: " + s); } return(result); }
public override Sentence VisitBinarySentence(ComplexSentence s, object arg) { Sentence result = null; if (s.IsImplicationSentence()) { // Elimina =>, reemplazando & alpha; => β< br > con ~α | β Sentence alpha = s.GetSimplerSentence(0).Accept(this, arg); Sentence beta = s.GetSimplerSentence(1).Accept(this, arg); Sentence notAlpha = new ComplexSentence(Connective.NOT, alpha); result = new ComplexSentence(Connective.OR, notAlpha, beta); } else { result = base.VisitBinarySentence(s, arg); } return(result); }
public override ISet <Clause> VisitBinarySentence(ComplexSentence s, ISet <Clause> arg) { if (s.IsAndSentence()) { s.GetSimplerSentence(0).Accept(this, arg); s.GetSimplerSentence(1).Accept(this, arg); } else if (s.IsOrSentence()) { IList <Literal> literals = new List <Literal>(LiteralCollector.getLiterals(s)); //ArrayList arg.Add(new Clause(literals)); } else { throw new ArgumentException("Sentence is not in CNF: " + s); //IllegalArgumentException } return(arg); }
public override ISet <Clause> visitBinarySentence(ComplexSentence s, ISet <Clause> arg) { if (s.isAndSentence()) { s.getSimplerSentence(0).accept(this, arg); s.getSimplerSentence(1).accept(this, arg); } else if (s.isOrSentence()) { ICollection <Literal> literals = CollectionFactory.CreateQueue <Literal>(LiteralCollector.getLiterals(s)); arg.Add(new Clause(literals)); } else { throw new IllegalArgumentException("Sentence is not in CNF: " + s); } return(arg); }
public override Sentence visitBinarySentence(ComplexSentence s, object arg) { Sentence result = null; if (s.isImplicationSentence()) { // Eliminate =>, replacing α => β // with ~α | β Sentence alpha = s.getSimplerSentence(0).accept(this, arg); Sentence beta = s.getSimplerSentence(1).accept(this, arg); Sentence notAlpha = new ComplexSentence(Connective.NOT, alpha); result = new ComplexSentence(Connective.OR, notAlpha, beta); } else { result = base.visitBinarySentence(s, arg); } return(result); }
public override Sentence visitBinarySentence(ComplexSentence s, object arg) { Sentence result = null; if (s.isBiconditionalSentence()) { // Eliminate <=>, replace α <=> β // with (α => β) & (β => α) Sentence alpha = s.getSimplerSentence(0).accept(this, arg); Sentence beta = s.getSimplerSentence(1).accept(this, arg); Sentence alphaImpliesBeta = new ComplexSentence( Connective.IMPLICATION, alpha, beta); Sentence betaImpliesAlpha = new ComplexSentence( Connective.IMPLICATION, beta, alpha); result = new ComplexSentence(Connective.AND, alphaImpliesBeta, betaImpliesAlpha); } else { result = base.visitBinarySentence(s, arg); } return result; }
public override Sentence VisitBinarySentence(ComplexSentence s, object arg) { Sentence result = null; if (s.IsBiconditionalSentence()) { // Elimina <=>, reemplazando α <=> β con (α => β) & (β => α) Sentence alpha = s.GetSimplerSentence(0).Accept(this, arg); Sentence beta = s.GetSimplerSentence(1).Accept(this, arg); Sentence alphaImpliesBeta = new ComplexSentence( Connective.IMPLICATION, alpha, beta); Sentence betaImpliesAlpha = new ComplexSentence( Connective.IMPLICATION, beta, alpha); result = new ComplexSentence(Connective.AND, alphaImpliesBeta, betaImpliesAlpha); } else { result = base.VisitBinarySentence(s, arg); } return(result); }
public override Sentence VisitBinarySentence(ComplexSentence s, object arg) { Sentence result = null; if (s.IsOrSentence()) { Sentence s1 = s.GetSimplerSentence(0).Accept(this, arg); Sentence s2 = s.GetSimplerSentence(1).Accept(this, arg); if (s1.IsAndSentence() || s2.IsAndSentence()) { Sentence alpha, betaAndGamma; if (s2.IsAndSentence()) { // (α | (β & γ)) // Nota: incluso si ambos son sentencias 'and' preferiremos usar s2 alpha = s1; betaAndGamma = s2; } else { // Nota: Hace falta manejar este caso también // ((β & γ) | α) alpha = s2; betaAndGamma = s1; } Sentence beta = betaAndGamma.GetSimplerSentence(0); Sentence gamma = betaAndGamma.GetSimplerSentence(1); if (s2.IsAndSentence()) { // ((α | β) & (α | γ)) Sentence alphaOrBeta = (new ComplexSentence(Connective.OR, alpha, beta)).Accept(this, null); Sentence alphaOrGamma = (new ComplexSentence(Connective.OR, alpha, gamma)).Accept(this, null); result = new ComplexSentence(Connective.AND, alphaOrBeta, alphaOrGamma); } else { // ((β | α) & (γ | α)) Sentence betaOrAlpha = (new ComplexSentence(Connective.OR, beta, alpha)).Accept(this, null); Sentence gammaOrAlpha = (new ComplexSentence(Connective.OR, gamma, alpha)).Accept(this, null); result = new ComplexSentence(Connective.AND, betaOrAlpha, gammaOrAlpha); } } else { result = new ComplexSentence(Connective.OR, s1, s2); } } else { result = base.VisitBinarySentence(s, arg); } return(result); }
// Es virtual por si hace falta sobreescribirlo public virtual ISet <T> VisitUnarySentence(ComplexSentence s, ISet <T> arg) { return(SetOps.Union(arg, s.GetSimplerSentence(0).Accept(this, arg))); }
// No hace falta override porque PLVisitor es una interfaz, pero pongo virtual por si queremos seguir sobreescribiendo public virtual Sentence VisitBinarySentence(ComplexSentence s, A arg) { // Una nueva sentencia compleja con la misma conectiva pero posiblemente con sus sentencias más simples reemplazadas por el visitante. return(new ComplexSentence(s.GetConnective(), s.GetSimplerSentence(0).Accept(this, arg), s.GetSimplerSentence(1).Accept(this, arg))); }
private ICollection <ParseNode> groupSimplerSentencesByConnective( Connective connectiveToConstruct, ICollection <ParseNode> parseNodes) { ICollection <ParseNode> newParseNodes = CollectionFactory.CreateQueue <ParseNode>(); int numSentencesMade = 0; // Go right to left in order to make right associative, // which is a natural default for propositional logic for (int i = parseNodes.Size() - 1; i >= 0; i--) { ParseNode parseNode = parseNodes.Get(i); if (parseNode.node is Connective) { Connective tokenConnective = (Connective)parseNode.node; if (tokenConnective == Connective.NOT) { // A unary connective if (i + 1 < parseNodes.Size() && parseNodes.Get(i + 1).node is Sentence) { if (tokenConnective == connectiveToConstruct) { ComplexSentence newSentence = new ComplexSentence( connectiveToConstruct, (Sentence)parseNodes.Get(i + 1).node); parseNodes.Set(i, new ParseNode(newSentence, parseNode.token)); parseNodes.Set(i + 1, null); numSentencesMade++; } } else { throw new ParserException( "Unary connective argurment is not a sentence at input position " + parseNode.token .getStartCharPositionInInput(), parseNode.token); } } else { // A Binary connective if ((i - 1 >= 0 && parseNodes.Get(i - 1).node is Sentence) && (i + 1 < parseNodes.Size() && parseNodes .Get(i + 1).node is Sentence)) { // A binary connective if (tokenConnective == connectiveToConstruct) { ComplexSentence newSentence = new ComplexSentence( connectiveToConstruct, (Sentence)parseNodes.Get(i - 1).node, (Sentence)parseNodes.Get(i + 1).node); parseNodes.Set(i - 1, new ParseNode(newSentence, parseNode.token)); parseNodes.Set(i, null); parseNodes.Set(i + 1, null); numSentencesMade++; } } else { throw new ParserException("Binary connective argurments are not sentences at input position " + parseNode.token .getStartCharPositionInInput(), parseNode.token); } } } } for (int i = 0; i < parseNodes.Size(); ++i) { ParseNode parseNode = parseNodes.Get(i); if (parseNode != null) { newParseNodes.Add(parseNode); } } // Ensure no tokens left unaccounted for in this pass. int toSubtract = 0; if (connectiveToConstruct == Connective.NOT) { toSubtract = (numSentencesMade * 2) - numSentencesMade; } else { toSubtract = (numSentencesMade * 3) - numSentencesMade; } if (parseNodes.Size() - toSubtract != newParseNodes.Size()) { throw new ParserException( "Unable to construct sentence for connective: " + connectiveToConstruct + " from: " + parseNodes, getTokens(parseNodes)); } return(newParseNodes); }
protected ISet <Clause> translateToSAT(Describe init, Describe transition, Describe goal, int t) { Sentence s = ComplexSentence.newConjunction(init.assertions(t), transition.assertions(t), goal.assertions(t)); return(ConvertToConjunctionOfClauses.convert(s).getClauses()); }
public override Sentence visitBinarySentence(ComplexSentence s, object arg) { Sentence result = null; if (s.isAndSentence()) { Sentence s1 = s.getSimplerSentence(0).accept(this, arg); Sentence s2 = s.getSimplerSentence(1).accept(this, arg); if (s1.isOrSentence() || s2.isOrSentence()) { Sentence alpha, betaAndGamma; if (s2.isOrSentence()) { // (α & (β | γ)) // Note: even if both are 'or' sentence // we will prefer to use s2 alpha = s1; betaAndGamma = s2; } else { // Note: Need to handle this case too // ((β | γ) & α) alpha = s2; betaAndGamma = s1; } Sentence beta = betaAndGamma.getSimplerSentence(0); Sentence gamma = betaAndGamma.getSimplerSentence(1); if (s2.isOrSentence()) { // ((α & β) | (α & γ)) Sentence alphaAndBeta = (new ComplexSentence(Connective.AND, alpha, beta)).accept(this, null); Sentence alphaAndGamma = (new ComplexSentence(Connective.AND, alpha, gamma)).accept(this, null); result = new ComplexSentence(Connective.OR, alphaAndBeta, alphaAndGamma); } else { // ((β & α) | (γ & α)) Sentence betaAndAlpha = (new ComplexSentence(Connective.AND, beta, alpha)).accept(this, null); Sentence gammaAndAlpha = (new ComplexSentence(Connective.AND, gamma, alpha)).accept(this, null); result = new ComplexSentence(Connective.OR, betaAndAlpha, gammaAndAlpha); } } else { result = new ComplexSentence(Connective.AND, s1, s2); } } else { result = base.visitBinarySentence(s, arg); } return(result); }