Exemplo n.º 1
0
        public string decodeImplication(Sentence clause)
        {
            string decode = clause.ToString();

            if (!(clause is BinarySentence))
            {
                return(decode);
            }
            BinarySentence bs   = (BinarySentence)clause;
            string         oper = bs.Operator;

            if (oper.Equals("=>") || oper.Equals("IMPLIES"))
            {
                decode = bs.ToString();
            }
            if (oper.Equals("OR"))
            {
                object firstValue  = bs.First.Accept(this, null);
                object secondValue = bs.Second.Accept(this, null);
                if ((firstValue.Equals(false)) && (secondValue.Equals(true)))
                {
                    decode = "( (NOT " + bs.First.ToString() + ") => " + bs.Second.ToString() + "  )";
                }
            }
            return(decode);
        }
Exemplo n.º 2
0
        public Object visitBinarySentence(BinarySentence bs, Object arg)
        {
            bool?firstValue  = (bool?)bs.getFirst().accept(this, null);
            bool?secondValue = (bool?)bs.getSecond().accept(this, null);

            if (!firstValue.HasValue || !secondValue.HasValue)
            {
                // strictly not true for or/and
                // -FIX later
                return(null);
            }
            else
            {
                String op = bs.getOperator();
                if (op.Equals("AND"))
                {
                    return(firstValue.Value && secondValue.Value);
                }
                else if (op.Equals("OR"))
                {
                    return(firstValue.Value || secondValue.Value);
                }
                else if (op.Equals("=>"))
                {
                    return(!(firstValue.Value && !secondValue.Value));
                }
                else if (op.Equals("<=>"))
                {
                    return(firstValue.Equals(secondValue));
                }
                return(null);
            }
        }
Exemplo n.º 3
0
            public HornClause(Sentence sentence, Stack <Symbol> agenda,
                              Dictionary <HornClause, int> count, Dictionary <Symbol, bool?> inferred)
            {
                if (sentence is Symbol)
                {
                    this.Head = (Symbol)sentence;
                    agenda.Push(this.Head);
                    premiseSymbols      = new List <Symbol>();
                    count[this]         = 0;
                    inferred[this.Head] = false;
                }
                else if (!this.IsImpliedSentence(sentence))
                {
                    throw new ApplicationException("Sentence " + sentence + " is not a horn clause");
                }
                else
                {
                    BinarySentence bs = (BinarySentence)sentence;
                    this.Head           = (Symbol)bs.Second;
                    inferred[this.Head] = false;
                    ISet <Symbol> symbolsInPremise = new SymbolCollector().GetSymbolsIn(bs.First);
                    foreach (var symbol in symbolsInPremise)
                    {
                        inferred[symbol] = false;
                    }
                    premiseSymbols = symbolsInPremise.ToList();

                    count[this] = premiseSymbols.Count;
                }
            }
Exemplo n.º 4
0
        public object VisitBinarySentence(BinarySentence bs, ISet <Sentence> arg)
        {
            object firstValue  = bs.First.Accept(this, null);
            object secondValue = bs.Second.Accept(this, null);

            if ((firstValue == null) || (secondValue == null))   // strictly not
            // true for or/and
            // -FIX later
            {
                return(null);
            }
            else
            {
                string oper = bs.Operator;
                if (oper.Equals("AND"))
                {
                    return(this.EvaluateAnd((bool)firstValue, (bool)secondValue));
                }
                if (oper.Equals("OR"))
                {
                    return(this.EvaluateOr((bool)firstValue, (bool)secondValue));
                }
                if (oper.Equals("=>") || oper.Equals("IMPLIES"))
                {
                    return(this.EvaluateImplied((bool)firstValue,
                                                (bool)secondValue));
                }
                if (oper.Equals("<=>") || oper.Equals("EQUIV"))
                {
                    return(this.EvaluateBiConditional((bool)firstValue,
                                                      (bool)secondValue));
                }
                return(null);
            }
        }
Exemplo n.º 5
0
        public void testBinarySentenceParse()
        {
            BinarySentence sen = (BinarySentence)parser
                                 .parse("(PETER  AND  NORVIG)");

            Assert.AreEqual(typeof(BinarySentence), sen.GetType());
        }
Exemplo n.º 6
0
        public Object visitBinarySentence(BinarySentence bs, Object arg)
        {
            Object firstValue  = bs.getFirst().accept(this, null);
            Object secondValue = bs.getSecond().accept(this, null);

            if ((firstValue == null) || (secondValue == null))
            {             //strictly not
                // true for or/and
                // -FIX later
                return(null);
            }
            else
            {
                string oper = bs.getOperator();
                if (oper.Equals("AND"))
                {
                    return(evaluateAnd((bool)firstValue, (bool)secondValue));
                }
                if (oper.Equals("OR"))
                {
                    return(evaluateOr((bool)firstValue, (bool)secondValue));
                }
                if (oper.Equals("=>"))
                {
                    return(evaluateImplied((bool)firstValue,
                                           (bool)secondValue));
                }
                if (oper.Equals("<=>"))
                {
                    return(evaluateBiConditional((bool)firstValue,
                                                 (bool)secondValue));
                }
                return(null);
            }
        }
Exemplo n.º 7
0
        public virtual Object visitBinarySentence(BinarySentence bs, Object arg)
        {
            Hashtable s         = (Hashtable)arg;
            Hashtable termunion = new SetOps().union((Hashtable)bs.getFirst().accept(this, arg), (Hashtable)bs.getSecond().accept(this, arg));

            return(new SetOps().union(s, termunion));
        }
Exemplo n.º 8
0
        private Sentence TransformImpliedSentence(BinarySentence bs)
        {
            Sentence first = new UnarySentence((Sentence)bs.First.Accept(
                                                   this, null));

            return(new BinarySentence("OR", first, (Sentence)bs.Second
                                      .Accept(this, null)));
        }
Exemplo n.º 9
0
        private Sentence transformImpliedSentence(BinarySentence bs)
        {
            Sentence first = new UnarySentence((Sentence)bs.getFirst().accept(
                                                   this, null));

            return(new BinarySentence("OR", first, (Sentence)bs.getSecond()
                                      .accept(this, null)));
        }
Exemplo n.º 10
0
        public virtual object VisitBinarySentence(BinarySentence bs, ISet <Sentence> arg)
        {
            var termunion =
                new HashSet <Sentence>((ISet <Sentence>)bs.First.Accept(this, arg)).Union(
                    (ISet <Sentence>)bs.Second.Accept(this, arg));
            var ret = new HashSet <Sentence>(arg);

            ret.UnionWith(termunion);
            return(ret);
        }
Exemplo n.º 11
0
        private Sentence transformBiConditionalSentence(BinarySentence bs)
        {
            Sentence first = new BinarySentence("=>", (Sentence)bs.getFirst()
                                                .accept(this, null), (Sentence)bs.getSecond().accept(this,
                                                                                                     null));
            Sentence second = new BinarySentence("=>", (Sentence)bs.getSecond()
                                                 .accept(this, null), (Sentence)bs.getFirst()
                                                 .accept(this, null));

            return(new BinarySentence("AND", first, second));
        }
Exemplo n.º 12
0
        public override object VisitBinarySentence(BinarySentence bs, ISet <Sentence> args)
        {
            ISet <Sentence> soFar = (ISet <Sentence>)args;

            Sentence first  = bs.First;
            Sentence second = bs.Second;

            this.ProcessSubTerm(second, this.ProcessSubTerm(first, soFar));

            return(soFar);
        }
Exemplo n.º 13
0
        public override Object visitBinarySentence(BinarySentence bs, Object args)
        {
            List <Sentence> soFar = (List <Sentence>)args;

            Sentence first  = bs.getFirst();
            Sentence second = bs.getSecond();

            processSubTerm(second, processSubTerm(first, soFar));

            return(soFar);
        }
Exemplo n.º 14
0
        private Sentence TransformBiConditionalSentence(BinarySentence bs)
        {
            Sentence first = new BinarySentence("=>", (Sentence)bs.First
                                                .Accept(this, null), (Sentence)bs.Second.Accept(this,
                                                                                                null));
            Sentence second = new BinarySentence("=>", (Sentence)bs.Second
                                                 .Accept(this, null), (Sentence)bs.First
                                                 .Accept(this, null));

            return(new BinarySentence("AND", first, second));
        }
Exemplo n.º 15
0
 public object VisitBinarySentence(BinarySentence fs, ISet <Sentence> arg)
 {
     if (fs.IsAndSentence())
     {
         return(true);
     }
     else
     {
         bool first  = ((bool)fs.First.Accept(this, null));
         bool second = ((bool)fs.Second.Accept(this, null));
         return(first || second);
     }
 }
Exemplo n.º 16
0
 public Object visitBinarySentence(BinarySentence fs, Object arg)
 {
     if (fs.isAndSentence())
     {
         return(true);
     }
     else
     {
         bool first  = ((bool)fs.getFirst().accept(this, null));
         bool second = ((bool)fs.getSecond().accept(this, null));
         return(first || second);
     }
 }
Exemplo n.º 17
0
        private Sentence DistributeOrOverAnd(BinarySentence bs)
        {
            BinarySentence andTerm = bs.FirstTermIsAndSentence() ? (BinarySentence)bs
                                     .First
                    : (BinarySentence)bs.Second;
            Sentence otherterm = bs.FirstTermIsAndSentence() ? bs.Second : bs
                                 .First;
            // (alpha or (beta and gamma) = ((alpha or beta) and (alpha or gamma))
            Sentence alpha       = (Sentence)otherterm.Accept(this, null);
            Sentence beta        = (Sentence)andTerm.First.Accept(this, null);
            Sentence gamma       = (Sentence)andTerm.Second.Accept(this, null);
            Sentence distributed = new BinarySentence("AND", new BinarySentence(
                                                          "OR", alpha, beta), new BinarySentence("OR", alpha, gamma));

            return(distributed);
        }
Exemplo n.º 18
0
 public override object VisitBinarySentence(BinarySentence bs, ISet <Sentence> arg)
 {
     if (bs.IsBiconditional())
     {
         return(this.TransformBiConditionalSentence(bs));
     }
     if (bs.IsImplication())
     {
         return(this.TransformImpliedSentence(bs));
     }
     if (bs.IsOrSentence() &&
         (bs.FirstTermIsAndSentence() || bs.SecondTermIsAndSentence()))
     {
         return(this.DistributeOrOverAnd(bs));
     }
     return(base.VisitBinarySentence(bs, arg));
 }
Exemplo n.º 19
0
        public void testComplexSentenceParse()
        {
            BinarySentence sen = (BinarySentence)parser
                                 .parse("((OR  NORVIG AIMA LISP) AND TRUE)");

            Assert.AreEqual(typeof(BinarySentence), sen.GetType());

            sen = (BinarySentence)parser
                  .parse("((OR  NORVIG AIMA LISP) AND (((LISP => COOL))))");
            Assert.AreEqual(typeof(BinarySentence), sen.GetType());
            Assert.AreEqual(
                " ( ( OR NORVIG AIMA LISP  )  AND  ( LISP => COOL ) )", sen
                .ToString());

            String s = "((NOT (P AND Q ))  AND ((NOT (R AND S))))";

            sen = (BinarySentence)parser.parse(s);
            Assert.AreEqual(
                " (  ( NOT  ( P AND Q ) )  AND  ( NOT  ( R AND S ) )  )", sen
                .ToString());

            s   = "((P AND Q) OR (S AND T))";
            sen = (BinarySentence)parser.parse(s);
            Assert
            .AreEqual(" (  ( P AND Q ) OR  ( S AND T ) )", sen
                      .ToString());
            Assert.AreEqual("OR", sen.getOperator());

            s = "(NOT ((P AND Q) => (S AND T)))";
            UnarySentence nsen = (UnarySentence)parser.parse(s);

            // AreEqual("=>",sen.getOperator());
            s    = "(NOT (P <=> (S AND T)))";
            nsen = (UnarySentence)parser.parse(s);
            Assert.AreEqual(" ( NOT  ( P <=>  ( S AND T ) ) ) ", nsen
                            .ToString());

            s   = "(P <=> (S AND T))";
            sen = (BinarySentence)parser.parse(s);

            s   = "(P => Q)";
            sen = (BinarySentence)parser.parse(s);

            s   = "((P AND Q) => R)";
            sen = (BinarySentence)parser.parse(s);
        }
Exemplo n.º 20
0
 public override Object visitBinarySentence(BinarySentence bs, Object arg)
 {
     if (bs.isBiconditional())
     {
         return(transformBiConditionalSentence(bs));
     }
     else if (bs.isImplication())
     {
         return(transformImpliedSentence(bs));
     }
     else if (bs.isOrSentence() &&
              (bs.firstTermIsAndSentence() || bs.secondTermIsAndSentence()))
     {
         return(distributeOrOverAnd(bs));
     }
     else
     {
         return(base.visitBinarySentence(bs, arg));
     }
 }
Exemplo n.º 21
0
        public virtual Object visitBinarySentence(BinarySentence bs, Object arg)
        {
            List <Sentence> s = new List <Sentence>();

            if (arg is List <Symbol> )
            {
                List <Symbol> symbols = ((List <Symbol>)arg);
                foreach (Symbol symbol in symbols)
                {
                    s.Add((Sentence)symbol);
                }
            }
            else
            {
                throw new ArgumentException("Could not cast arg to List<Sentence>");
            }
            List <Sentence> termunion = SetOps.union((List <Sentence>)bs.getFirst().accept(this, arg),
                                                     (List <Sentence>)bs.getSecond().accept(this, arg));

            return(SetOps.union(s, termunion));
        }
Exemplo n.º 22
0
 public static Sentence chainWith(string connector, ArrayList sentences)
 {
     if (sentences.Count == 0)
     {
         return(null);
     }
     else if (sentences.Count == 1)
     {
         return((Sentence)sentences[0]);
     }
     else
     {
         Sentence soFar = (Sentence)sentences[0];
         for (int i = 1; i < sentences.Count; i++)
         {
             Sentence next = (Sentence)sentences[i];
             soFar = new BinarySentence(connector, soFar, next);
         }
         return(soFar);
     }
 }
Exemplo n.º 23
0
        public bool IsActiveImplication(Sentence clause)
        {
            // returns true when (A=>B), A=t, B=t
            // Or  (A=>B) translated into (-A OR B), A=f (since its been inverted),B=t ...

            object result = clause.Accept(this, null);

            if (result == null)
            {
                return(false);
            }
            if (!(clause is BinarySentence))
            {
                return(false);
            }
            BinarySentence bs   = (BinarySentence)clause;
            string         oper = bs.Operator;

            if (oper.Equals("=>") || oper.Equals("IMPLIES"))
            {
                object firstValue  = bs.First.Accept(this, null);
                object secondValue = bs.Second.Accept(this, null);
                if ((firstValue.Equals(true)) && (secondValue.Equals(true)))
                {
                    return(true);
                }
            }
            if (oper.Equals("OR"))
            {
                object firstValue  = bs.First.Accept(this, null);
                object secondValue = bs.Second.Accept(this, null);
                if ((firstValue.Equals(false)) && (secondValue.Equals(true)))
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 24
0
 public HornClause(Sentence sentence, PLFCEntails plfcEntails)
 {
     this.plfcEntails = plfcEntails;
     if (sentence is Symbol)
     {
         _head = (Symbol)sentence;
         plfcEntails.agenda.Push(_head);
         premiseSymbols = new List <Symbol>();
         plfcEntails.count.Add(this, 0);
         plfcEntails._inferred.Add(_head, false);
     }
     else if (!isImpliedSentence(sentence))
     {
         throw new ApplicationException("Sentence " + sentence
                                        + " is not a horn clause");
     }
     else
     {
         BinarySentence bs = (BinarySentence)sentence;
         _head = (Symbol)bs.getSecond();
         if (plfcEntails._inferred.ContainsKey(_head))
         {
             plfcEntails._inferred[_head] = false;
         }
         else
         {
             plfcEntails._inferred.Add(_head, false);
         }
         List <Symbol> symbolsInPremise = new SymbolCollector()
                                          .getSymbolsIn(bs.getFirst());
         foreach (Symbol s in symbolsInPremise)
         {
             plfcEntails._inferred.Add(s, false);
         }
         premiseSymbols = symbolsInPremise;
         plfcEntails.count.Add(this, premiseSymbols.Count);
     }
 }
Exemplo n.º 25
0
        public bool plResolution(KnowledgeBase kb, Sentence alpha)
        {
            Sentence kBAndNotAlpha = new BinarySentence("AND", kb.asSentence(),
                                                        new UnarySentence(alpha));
            List <Sentence> clauses = new CNFClauseGatherer()
                                      .getClausesFrom(new CNFTransformer().transform(kBAndNotAlpha));

            clauses = filterOutClausesWithTwoComplementaryLiterals(clauses);
            List <Sentence> newClauses = new List <Sentence>();

            while (true)
            {
                List <List <Sentence> > pairs = getCombinationPairs(clauses);

                for (int i = 0; i < pairs.Count; i++)
                {
                    List <Sentence> pair = pairs[i];
                    // System.Console.WriteLine("pair number" + i+" of "+pairs.Count);
                    List <Sentence> resolvents = plResolve(pair[0], pair[1]);
                    resolvents = filterOutClausesWithTwoComplementaryLiterals(resolvents);

                    if (resolvents.Contains(new Symbol("EMPTY_CLAUSE")))
                    {
                        return(true);
                    }
                    newClauses = SetOps.union(newClauses, resolvents);
                    // System.Console.WriteLine("clauseslist size = " +clauses.Count);
                }
                if (SetOps.intersection(newClauses, clauses).Count == newClauses
                    .Count)
                {// subset test
                    return(false);
                }
                clauses = SetOps.union(newClauses, clauses);
                clauses = filterOutClausesWithTwoComplementaryLiterals(clauses);
            }
        }
Exemplo n.º 26
0
 private Sentence transformNotSentence(UnarySentence us)
 {
     if (us.getNegated() is UnarySentence)
     {
         return((Sentence)((UnarySentence)us.getNegated()).getNegated()
                .accept(this, null));
     }
     else if (us.getNegated() is BinarySentence)
     {
         BinarySentence bs = (BinarySentence)us.getNegated();
         if (bs.isAndSentence())
         {
             Sentence first = new UnarySentence((Sentence)bs.getFirst()
                                                .accept(this, null));
             Sentence second = new UnarySentence((Sentence)bs.getSecond()
                                                 .accept(this, null));
             return(new BinarySentence("OR", first, second));
         }
         else if (bs.isOrSentence())
         {
             Sentence first = new UnarySentence((Sentence)bs.getFirst()
                                                .accept(this, null));
             Sentence second = new UnarySentence((Sentence)bs.getSecond()
                                                 .accept(this, null));
             return(new BinarySentence("AND", first, second));
         }
         else
         {
             return((Sentence)base.visitNotSentence(us, null));
         }
     }
     else
     {
         return((Sentence)base.visitNotSentence(us, null));
     }
 }
Exemplo n.º 27
0
        public bool PlResolution(KnowledgeBase kb, Sentence alpha)
        {
            Sentence kBAndNotAlpha = new BinarySentence("AND", kb.AsSentence(),
                                                        new UnarySentence(alpha));
            IList <Sentence> clauses = new CNFClauseGatherer()
                                       .GetClausesFrom(new CNFTransformer().Transform(kBAndNotAlpha)).ToList();

            clauses = this.FilterOutClausesWithTwoComplementaryLiterals(clauses);
            var newClauses = new List <Sentence>();

            while (true)
            {
                IList <IList <Sentence> > pairs = this.GetCombinationPairs(clauses.ToList());

                for (int i = 0; i < pairs.Count; i++)
                {
                    IList <Sentence> pair = pairs[i];
                    // System.out.println("pair number" + i+" of "+pairs.Count);
                    IList <Sentence> resolvents = this.PLResolve(pair[0], pair[1]);
                    resolvents = this.FilterOutClausesWithTwoComplementaryLiterals(resolvents);

                    if (resolvents.Contains(new Symbol("EMPTY_CLAUSE")))
                    {
                        return(true);
                    }
                    newClauses = newClauses.Union(resolvents).ToList();
                    // System.out.println("clauseslist size = " +clauses.Count);
                }
                if (newClauses.Intersect(clauses).Count() == newClauses.Count)
                {// subset test
                    return(false);
                }
                clauses = newClauses.Union(clauses).ToList();
                clauses = this.FilterOutClausesWithTwoComplementaryLiterals(clauses);
            }
        }
Exemplo n.º 28
0
 private Sentence TransformNotSentence(UnarySentence us)
 {
     if (us.Negated is UnarySentence)
     {
         return((Sentence)((UnarySentence)us.Negated).Negated
                .Accept(this, null));
     }
     else if (us.Negated is BinarySentence)
     {
         BinarySentence bs = (BinarySentence)us.Negated;
         if (bs.IsAndSentence())
         {
             Sentence first = new UnarySentence((Sentence)bs.First
                                                .Accept(this, null));
             Sentence second = new UnarySentence((Sentence)bs.Second
                                                 .Accept(this, null));
             return(new BinarySentence("OR", first, second));
         }
         else if (bs.IsOrSentence())
         {
             Sentence first = new UnarySentence((Sentence)bs.First
                                                .Accept(this, null));
             Sentence second = new UnarySentence((Sentence)bs.Second
                                                 .Accept(this, null));
             return(new BinarySentence("AND", first, second));
         }
         else
         {
             return((Sentence)base.VisitNotSentence(us, null));
         }
     }
     else
     {
         return((Sentence)base.VisitNotSentence(us, null));
     }
 }
Exemplo n.º 29
0
 public virtual object VisitBinarySentence(BinarySentence fs, ISet <Sentence> arg)
 {
     return(new BinarySentence(fs.Operator, (Sentence)fs.First
                               .Accept(this, arg), (Sentence)fs.Second.Accept(this, arg)));
 }
Exemplo n.º 30
0
            public HornClause(Sentence sentence, Stack _agenda, Hashtable _count, Hashtable _inferred)
            {
                if (sentence is Symbol)
                {
                    _head = (Symbol)sentence;

                    _agenda.Push(_head);
                    premiseSymbols = new ArrayList();
                    if (_count.Contains(this))
                    {
                        _count[this] = 0;
                    }
                    else
                    {
                        _count.Add(this, 0);
                    }
                    if (_inferred.Contains(_head))
                    {
                        _inferred[_head] = false;
                    }
                    else
                    {
                        _inferred.Add(_head, false);
                    }
                }
                else if (!isImpliedSentence(sentence))
                {
                    throw new Exception("Sentence " + sentence
                                        + " is not a horn clause");
                }
                else
                {
                    BinarySentence bs = (BinarySentence)sentence;
                    _head = (Symbol)bs.getSecond();
                    if (_inferred.Contains(_head))
                    {
                        _inferred[_head] = false;
                    }
                    else
                    {
                        _inferred.Add(_head, false);
                    }
                    Hashtable symbolsInPremise = new SymbolCollector().getSymbolsIn(bs
                                                                                    .getFirst());
//					Iterator iter = symbolsInPremise.iterator();
//					while (iter.hasNext())
                    foreach (object iter in symbolsInPremise.Keys)
                    {
                        if (_inferred.Contains(iter))
                        {
                            _inferred[iter] = false;
                        }
                        else
                        {
                            _inferred.Add(iter, false);
                        }
                    }
                    premiseSymbols = new Converter().setToList(symbolsInPremise);
                    if (_count.Contains(this))
                    {
                        _count[this] = premiseSymbols.Count;
                    }
                    else
                    {
                        _count.Add(this, premiseSymbols.Count);
                    }
                }
            }