Ejemplo n.º 1
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 ArgumentException("Sentence is not in CNF: " + s);               //IllegalArgumentException
     }
     return(arg);
 }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        // No hace falta override porque PLVisitor es una interfaz
        public bool?VisitUnarySentence(ComplexSentence fs, bool?arg)
        {
            object negatedValue = fs.GetSimplerSentence(0).Accept(this, null);

            if (negatedValue != null)
            {
                return(!(bool)negatedValue);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
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)));
 }
Ejemplo n.º 10
0
        // Es virtual por si hace falta sobreescribirlo
        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));
        }
Ejemplo n.º 11
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)));
 }
Ejemplo n.º 12
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);
        }