Package aima.core.logic.fol.kb.data

Examples of aima.core.logic.fol.kb.data.Clause


    domain.addPredicate("Q");

    FOLParser parser = new FOLParser(domain);

    // p(x,y), q(a,b), p(b,a), q(y,x)
    Clause c = new Clause();
    c.addPositiveLiteral((Predicate) parser.parse("P(x,y)"));
    c.addPositiveLiteral((Predicate) parser.parse("Q(A,B)"));
    c.addNegativeLiteral((Predicate) parser.parse("P(B,A)"));
    c.addPositiveLiteral((Predicate) parser.parse("Q(y,x)"));

    Assert.assertEquals("[[~P(B,A), P(B,A), Q(A,B)]]", c
        .getNonTrivialFactors().toString());

    // p(x,y), q(a,b), p(b,a), q(y,x)
    c = new Clause();
    c.addPositiveLiteral((Predicate) parser.parse("P(x,y)"));
    c.addPositiveLiteral((Predicate) parser.parse("Q(A,B)"));
    c.addNegativeLiteral((Predicate) parser.parse("P(B,A)"));
    c.addNegativeLiteral((Predicate) parser.parse("Q(y,x)"));

    Assert.assertEquals("[]", c.getNonTrivialFactors().toString());

    // p(x,f(y)), p(g(u),x), p(f(y),u)
    c = new Clause();
    c.addPositiveLiteral((Predicate) parser.parse("P(x,F(y))"));
    c.addPositiveLiteral((Predicate) parser.parse("P(G(u),x)"));
    c.addPositiveLiteral((Predicate) parser.parse("P(F(y),u)"));

    // Should be: [{P(F(c#),F(c#)),P(G(F(c#)),F(c#))}]
    c = c.getNonTrivialFactors().iterator().next();
    Literal p = c.getPositiveLiterals().get(0);
    Assert.assertEquals("P", p.getAtomicSentence().getSymbolicName());
    Function f = (Function) p.getAtomicSentence().getArgs().get(0);
    Assert.assertEquals("F", f.getFunctionName());
    Variable v = (Variable) f.getTerms().get(0);
    f = (Function) p.getAtomicSentence().getArgs().get(1);
    Assert.assertEquals("F", f.getFunctionName());
    Assert.assertEquals(v, f.getTerms().get(0));

    //
    p = c.getPositiveLiterals().get(1);
    f = (Function) p.getAtomicSentence().getArgs().get(0);
    Assert.assertEquals("G", f.getFunctionName());
    f = (Function) f.getTerms().get(0);
    Assert.assertEquals("F", f.getFunctionName());
    Assert.assertEquals(v, f.getTerms().get(0));
    f = (Function) p.getAtomicSentence().getArgs().get(1);
    Assert.assertEquals("F", f.getFunctionName());
    Assert.assertEquals(v, f.getTerms().get(0));

    // p(g(x)), q(x), p(f(a)), p(x), p(g(f(x))), q(f(a))
    c = new Clause();
    c.addPositiveLiteral((Predicate) parser.parse("P(G(x))"));
    c.addPositiveLiteral((Predicate) parser.parse("Q(x)"));
    c.addPositiveLiteral((Predicate) parser.parse("P(F(A))"));
    c.addPositiveLiteral((Predicate) parser.parse("P(x)"));
    c.addPositiveLiteral((Predicate) parser.parse("P(G(F(x)))"));
    c.addPositiveLiteral((Predicate) parser.parse("Q(F(A))"));

    Assert.assertEquals("[[P(F(A)), P(G(F(F(A)))), P(G(F(A))), Q(F(A))]]",
        c.getNonTrivialFactors().toString());
  }
View Full Code Here


    Predicate p3 = (Predicate) parser.parse("W(z,x,u,w,y)");
    lits.add(new Literal(p1));
    lits.add(new Literal(p2));
    lits.add(new Literal(p3));

    Clause clExpression = new Clause(lits);

    TermEquality assertion = (TermEquality) parser.parse("G(x,y) = x");

    Clause altClExpression = demodulation.apply(assertion, clExpression);

    Assert.assertEquals("[P(x,G(A,C)), Q(z,D), W(z,x,u,w,y)]",
        altClExpression.toString());

    altClExpression = demodulation.apply(assertion, altClExpression);

    Assert.assertEquals("[P(x,A), Q(z,D), W(z,x,u,w,y)]",
        altClExpression.toString());
  }
View Full Code Here

    List<Literal> lits = new ArrayList<Literal>();
    Predicate p1 = (Predicate) parser.parse("P(y, F(A,y))");
    lits.add(new Literal(p1));

    Clause clExpression = new Clause(lits);

    TermEquality assertion = (TermEquality) parser.parse("F(x,B) = C");

    Clause altClExpression = demodulation.apply(assertion, clExpression);

    Assert.assertNull(altClExpression);
  }
View Full Code Here

    List<Literal> lits = new ArrayList<Literal>();
    Predicate p1 = (Predicate) parser.parse("P(y, F(A,y))");
    lits.add(new Literal(p1));

    Clause clExpression = new Clause(lits);

    TermEquality assertion = (TermEquality) parser.parse("x = x");

    Clause altClExpression = demodulation.apply(assertion, clExpression);

    Assert.assertNull(altClExpression);
  }
View Full Code Here

    domain.addFunction("F");

    FOLParser parser = new FOLParser(domain);

    // {p(f(a)),~p(f(a))}
    Clause c = new Clause();
    c.addPositiveLiteral((Predicate) parser.parse("P(F(A))"));
    Assert.assertFalse(c.isTautology());
    c.addNegativeLiteral((Predicate) parser.parse("P(F(A))"));
    Assert.assertTrue(c.isTautology());

    // {p(x),q(y),~q(y),r(z)}
    c = new Clause();
    c.addPositiveLiteral((Predicate) parser.parse("P(x)"));
    Assert.assertFalse(c.isTautology());
    c.addPositiveLiteral((Predicate) parser.parse("Q(y)"));
    Assert.assertFalse(c.isTautology());
    c.addNegativeLiteral((Predicate) parser.parse("Q(y)"));
    Assert.assertTrue(c.isTautology());
    c.addPositiveLiteral((Predicate) parser.parse("R(z)"));
    Assert.assertTrue(c.isTautology());

    // {~p(a),p(x)}
    c = new Clause();
    c.addNegativeLiteral((Predicate) parser.parse("P(A)"));
    Assert.assertFalse(c.isTautology());
    c.addPositiveLiteral((Predicate) parser.parse("P(x)"));
    Assert.assertFalse(c.isTautology());
  }
View Full Code Here

  public Set<Clause> apply(Clause c1, Clause c2, boolean standardizeApart) {
    Set<Clause> paraExpressions = new LinkedHashSet<Clause>();

    for (int i = 0; i < 2; i++) {
      Clause topClause, equalityClause;
      if (i == 0) {
        topClause = c1;
        equalityClause = c2;
      } else {
        topClause = c2;
        equalityClause = c1;
      }

      for (Literal possEqLit : equalityClause.getLiterals()) {
        // Must be a positive term equality to be used
        // for paramodulation.
        if (possEqLit.isPositiveLiteral()
            && possEqLit.getAtomicSentence() instanceof TermEquality) {
          TermEquality assertion = (TermEquality) possEqLit
              .getAtomicSentence();

          // Test matching for both sides of the equality
          for (int x = 0; x < 2; x++) {
            Term toMatch, toReplaceWith;
            if (x == 0) {
              toMatch = assertion.getTerm1();
              toReplaceWith = assertion.getTerm2();
            } else {
              toMatch = assertion.getTerm2();
              toReplaceWith = assertion.getTerm1();
            }

            for (Literal l1 : topClause.getLiterals()) {
              IdentifyCandidateMatchingTerm icm = getMatchingSubstitution(
                  toMatch, l1.getAtomicSentence());

              if (null != icm) {
                Term replaceWith = substVisitor.subst(
                    icm.getMatchingSubstitution(),
                    toReplaceWith);

                // Want to ignore reflexivity axiom situation,
                // i.e. x = x
                if (icm.getMatchingTerm().equals(replaceWith)) {
                  continue;
                }

                ReplaceMatchingTerm rmt = new ReplaceMatchingTerm();

                AtomicSentence altExpression = rmt.replace(
                    l1.getAtomicSentence(),
                    icm.getMatchingTerm(), replaceWith);

                // I have an alternative, create a new clause
                // with the alternative and the substitution
                // applied to all the literals before returning
                List<Literal> newLits = new ArrayList<Literal>();
                for (Literal l2 : topClause.getLiterals()) {
                  if (l1.equals(l2)) {
                    newLits.add(l1
                        .newInstance((AtomicSentence) substVisitor.subst(
                            icm.getMatchingSubstitution(),
                            altExpression)));
                  } else {
                    newLits.add(substVisitor.subst(
                        icm.getMatchingSubstitution(),
                        l2));
                  }
                }
                // Assign the equality clause literals,
                // excluding
                // the term equality used.
                for (Literal l2 : equalityClause.getLiterals()) {
                  if (possEqLit.equals(l2)) {
                    continue;
                  }
                  newLits.add(substVisitor.subst(
                      icm.getMatchingSubstitution(), l2));
                }

                // Only apply paramodulation at most once
                // for each term equality.
                Clause nc = null;
                if (standardizeApart) {
                  sApart.standardizeApart(newLits,
                      _emptyLiteralList, _saIndexical);
                  nc = new Clause(newLits);

                } else {
                  nc = new Clause(newLits);
                }
                nc.setProofStep(new ProofStepClauseParamodulation(
                    nc, topClause, equalityClause,
                    assertion));
                if (c1.isImmutable()) {
                  nc.setImmutable();
                }
                if (!c1.isStandardizedApartCheckRequired()) {
                  c1.setStandardizedApartCheckNotRequired();
                }
                paraExpressions.add(nc);
View Full Code Here

    // Want to use an answer literal to pull
    // query variables where necessary
    Literal answerLiteral = KB.createAnswerLiteral(notAlpha);
    Set<Variable> answerLiteralVariables = KB
        .collectAllVariables(answerLiteral.getAtomicSentence());
    Clause answerClause = new Clause();

    if (answerLiteralVariables.size() > 0) {
      Sentence notAlphaWithAnswer = new ConnectedSentence(Connectors.OR,
          notAlpha, answerLiteral.getAtomicSentence());
      for (Clause c : KB.convertToClauses(notAlphaWithAnswer)) {
        c = KB.standardizeApart(c);
        c.setProofStep(new ProofStepGoal(c));
        c.setStandardizedApartCheckNotRequired();
        clauses.addAll(c.getFactors());
      }

      answerClause.addLiteral(answerLiteral);
    } else {
      for (Clause c : KB.convertToClauses(notAlpha)) {
        c = KB.standardizeApart(c);
        c.setProofStep(new ProofStepGoal(c));
        c.setStandardizedApartCheckNotRequired();
        clauses.addAll(c.getFactors());
      }
    }

    TFMAnswerHandler ansHandler = new TFMAnswerHandler(answerLiteral,
        answerLiteralVariables, answerClause, maxQueryTime);

    // new <- {}
    Set<Clause> newClauses = new LinkedHashSet<Clause>();
    Set<Clause> toAdd = new LinkedHashSet<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 = new Clause[clauses.size()];
      clauses.toArray(clausesA);
      // 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);
          }
View Full Code Here

    Assert.assertEquals(1, weaponsKB.getNumberRules());

    List<Term> terms = new ArrayList<Term>();
    terms.add(new Variable("v0"));

    Clause dcRule = weaponsKB.getAllDefiniteClauseImplications().get(0);
    Assert.assertNotNull(dcRule);
    Assert.assertEquals(true, dcRule.isImplicationDefiniteClause());
    Assert.assertEquals(new Literal(new Predicate("Criminal", terms)),
        dcRule.getPositiveLiterals().get(0));
  }
View Full Code Here

    // subsumed clauses removed
    Assert.assertEquals(8, clauses.size());

    // Ensure only the 8 correct/expected clauses remain
    Clause cl1 = cnfConv
        .convertToCNF(
            parser.parse("(NOT(will_wait(v)) OR (patrons(v,Full) OR patrons(v,Some)))"))
        .getConjunctionOfClauses().get(0);
    Clause cl2 = cnfConv
        .convertToCNF(
            parser.parse("(NOT(will_wait(v)) OR (hungry(v) OR patrons(v,Some)))"))
        .getConjunctionOfClauses().get(0);
    Clause cl3 = cnfConv
        .convertToCNF(
            parser.parse("(NOT(will_wait(v)) OR (patrons(v,Some) OR (type(v,Burger) OR (type(v,French) OR type(v,Thai)))))"))
        .getConjunctionOfClauses().get(0);
    Clause cl4 = cnfConv
        .convertToCNF(
            parser.parse("(NOT(will_wait(v)) OR (fri_sat(v) OR (patrons(v,Some) OR (type(v,Burger) OR type(v,French)))))"))
        .getConjunctionOfClauses().get(0);
    Clause cl5 = cnfConv
        .convertToCNF(
            parser.parse("(NOT(patrons(v,Some)) OR will_wait(v))"))
        .getConjunctionOfClauses().get(0);
    Clause cl6 = cnfConv
        .convertToCNF(
            parser.parse("(NOT(hungry(v)) OR (NOT(patrons(v,Full)) OR (NOT(type(v,French)) OR will_wait(v))))"))
        .getConjunctionOfClauses().get(0);
    Clause cl7 = cnfConv
        .convertToCNF(
            parser.parse("(NOT(fri_sat(v)) OR (NOT(hungry(v)) OR (NOT(patrons(v,Full)) OR (NOT(type(v,Thai)) OR will_wait(v)))))"))
        .getConjunctionOfClauses().get(0);
    Clause cl8 = cnfConv
        .convertToCNF(
            parser.parse("(NOT(hungry(v)) OR (NOT(patrons(v,Full)) OR (NOT(type(v,Burger)) OR will_wait(v))))"))
        .getConjunctionOfClauses().get(0);

    Assert.assertTrue(clauses.contains(cl1));
View Full Code Here

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

    first.accept(this, arg);
    if (Connectors.isAND(sentence.getConnector())) {
      ad.clauses.add(new Clause());
    }
    second.accept(this, arg);

    return sentence;
  }
View Full Code Here

TOP

Related Classes of aima.core.logic.fol.kb.data.Clause

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.