Exemple #1
0
        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);
            }
        }
Exemple #2
0
 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)));
 }
Exemple #3
0
        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));
        }
Exemple #4
0
        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);
        }
Exemple #5
0
        public bool?visitUnarySentence(ComplexSentence fs, bool?arg)
        {
            object negatedValue = fs.getSimplerSentence(0).accept(this, null);

            if (negatedValue != null)
            {
                return(!((bool)negatedValue));
            }
            else
            {
                return(null);
            }
        }
Exemple #6
0
 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);
 }
Exemple #7
0
        // 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);
        }
Exemple #8
0
        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);
        }
Exemple #9
0
        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);
        }
Exemple #10
0
        //
        // SUPPORTING CODE
        //

        /**
         * Determine if KB |= &alpha;, i.e. alpha is entailed by KB.
         *
         * @param kb
         *            a Knowledge Base in propositional logic.
         * @param alpha
         *            a propositional sentence.
         * @return true, if &alpha; 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()));
        }
Exemple #11
0
        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())
            {
                // ~(~&alpha;) &equiv; &alpha; (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())
                {
                    // ~(&alpha; & &beta;) &equiv; (~&alpha; | ~&beta;) (De Morgan)
                    result = new ComplexSentence(Connective.OR, notAlpha, notBeta);
                }
                else
                {
                    // ~(&alpha; | &beta;) &equiv; (~&alpha; & ~&beta;) (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);
        }
Exemple #12
0
        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())
            {
                // ~(~&alpha;) &equiv; &alpha; (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())
                {
                    // ~(&alpha; & &beta;) &equiv; (~&alpha; | ~&beta;) (De Morgan)
                    result = new ComplexSentence(Connective.OR, notAlpha, notBeta);
                }
                else
                {
                    // ~(&alpha; | &beta;) &equiv; (~&alpha; & ~&beta;) (De Morgan)
                    result = new ComplexSentence(Connective.AND, notAlpha, notBeta);
                }
            }
            else
            {
                throw new IllegalArgumentException("Biconditionals and Implications should not exist in input: " + s);
            }

            return(result);
        }
Exemple #13
0
        public override Sentence VisitBinarySentence(ComplexSentence s, object arg)
        {
            Sentence result = null;

            if (s.IsImplicationSentence())
            {
                // Elimina =>, reemplazando & alpha; => &beta;< br > con ~&alpha; | &beta;
                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);
        }
Exemple #14
0
        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);
        }
Exemple #15
0
        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 &alpha; => &beta;
                // with ~&alpha; | &beta;
                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 &alpha; <=> &beta;
                // with (&alpha; => &beta;) & (&beta; => &alpha;)
                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;
        }
Exemple #18
0
        public override Sentence VisitBinarySentence(ComplexSentence s, object arg)
        {
            Sentence result = null;

            if (s.IsBiconditionalSentence())
            {
                // Elimina <=>, reemplazando &alpha; <=> &beta; con (&alpha; => &beta;) & (&beta; => &alpha;)
                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);
        }
Exemple #19
0
        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())
                    {
                        // (&alpha; | (&beta; & &gamma;))
                        // Nota: incluso si ambos son sentencias 'and' preferiremos usar s2
                        alpha        = s1;
                        betaAndGamma = s2;
                    }
                    else
                    {
                        // Nota: Hace falta manejar este caso también
                        // ((&beta; & &gamma;) | &alpha;)
                        alpha        = s2;
                        betaAndGamma = s1;
                    }

                    Sentence beta  = betaAndGamma.GetSimplerSentence(0);
                    Sentence gamma = betaAndGamma.GetSimplerSentence(1);

                    if (s2.IsAndSentence())
                    {
                        // ((&alpha; | &beta;) & (&alpha; | &gamma;))
                        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
                    {
                        // ((&beta; | &alpha;) & (&gamma; | &alpha;))
                        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);
        }
Exemple #20
0
 // 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)));
 }
Exemple #21
0
 // 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)));
 }
Exemple #22
0
        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);
        }
Exemple #23
0
        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())
                    {
                        // (&alpha; & (&beta; | &gamma;))
                        // 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
                        // ((&beta; | &gamma;) & &alpha;)
                        alpha        = s2;
                        betaAndGamma = s1;
                    }

                    Sentence beta  = betaAndGamma.getSimplerSentence(0);
                    Sentence gamma = betaAndGamma.getSimplerSentence(1);

                    if (s2.isOrSentence())
                    {
                        // ((&alpha; & &beta;) | (&alpha; & &gamma;))
                        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
                    {
                        // ((&beta; & &alpha;) | (&gamma; & &alpha;))
                        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);
        }