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

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


    }
    return 0;
  }

  public void resetNumberFarParentsTo(Chain farParent, int toSize) {
    Literal head = farParent.getHead();
    Map<String, List<Chain>> heads = null;
    if (head.isPositiveLiteral()) {
      heads = posHeads;
    } else {
      heads = negHeads;
    }
    String key = head.getAtomicSentence().getSymbolicName();
    List<Chain> farParents = heads.get(key);
    while (farParents.size() > toSize) {
      farParents.remove(farParents.size() - 1);
    }
  }
View Full Code Here


      farParents.remove(farParents.size() - 1);
    }
  }

  public int getNumberCandidateFarParents(Chain nearParent) {
    Literal nearestHead = nearParent.getHead();

    Map<String, List<Chain>> candidateHeads = null;
    if (nearestHead.isPositiveLiteral()) {
      candidateHeads = negHeads;
    } else {
      candidateHeads = posHeads;
    }

    String nearestKey = nearestHead.getAtomicSentence().getSymbolicName();

    List<Chain> farParents = candidateHeads.get(nearestKey);
    if (null != farParents) {
      return farParents.size();
    }
View Full Code Here

  }

  public Chain attemptReduction(Chain nearParent, int farParentIndex) {
    Chain nnpc = null;

    Literal nearLiteral = nearParent.getHead();

    Map<String, List<Chain>> candidateHeads = null;
    if (nearLiteral.isPositiveLiteral()) {
      candidateHeads = negHeads;
    } else {
      candidateHeads = posHeads;
    }

    AtomicSentence nearAtom = nearLiteral.getAtomicSentence();
    String nearestKey = nearAtom.getSymbolicName();
    List<Chain> farParents = candidateHeads.get(nearestKey);
    if (null != farParents) {
      Chain farParent = farParents.get(farParentIndex);
      standardizeApart(farParent);
      Literal farLiteral = farParent.getHead();
      AtomicSentence farAtom = farLiteral.getAtomicSentence();
      Map<Variable, Term> subst = unifier.unify(nearAtom, farAtom);

      // If I was able to unify with one
      // of the far heads
      if (null != subst) {
        // Want to always apply reduction uniformly
        Chain topChain = farParent;
        Literal botLit = nearLiteral;
        Chain botChain = nearParent;

        // Need to apply subst to all of the
        // literals in the reduction
        List<Literal> reduction = new ArrayList<Literal>();
        for (Literal l : topChain.getTail()) {
          AtomicSentence atom = (AtomicSentence) substVisitor.subst(
              subst, l.getAtomicSentence());
          reduction.add(l.newInstance(atom));
        }
        reduction.add(new ReducedLiteral((AtomicSentence) substVisitor
            .subst(subst, botLit.getAtomicSentence()), botLit
            .isNegativeLiteral()));
        for (Literal l : botChain.getTail()) {
          AtomicSentence atom = (AtomicSentence) substVisitor.subst(
              subst, l.getAtomicSentence());
          reduction.add(l.newInstance(atom));
View Full Code Here

    return nnpc;
  }

  public Chain addToIndex(Chain c) {
    Chain added = null;
    Literal head = c.getHead();
    if (null != head) {
      Map<String, List<Chain>> toAddTo = null;
      if (head.isPositiveLiteral()) {
        toAddTo = posHeads;
      } else {
        toAddTo = negHeads;
      }

      String key = head.getAtomicSentence().getSymbolicName();
      List<Chain> farParents = toAddTo.get(key);
      if (null == farParents) {
        farParents = new ArrayList<Chain>();
        toAddTo.put(key, farParents);
      }
View Full Code Here

          "Only Atomic Queries are supported.");
    }

    FCAskAnswerHandler ansHandler = new FCAskAnswerHandler();

    Literal alpha = new Literal((AtomicSentence) query);

    // local variables: new, the new sentences inferred on each iteration
    List<Literal> newSentences = new ArrayList<Literal>();

    // Ensure query is not already a know fact before
    // attempting forward chaining.
    Set<Map<Variable, Term>> answers = KB.fetch(alpha);
    if (answers.size() > 0) {
      ansHandler.addProofStep(new ProofStepFoChAlreadyAFact(alpha));
      ansHandler.setAnswers(answers);
      return ansHandler;
    }

    // repeat until new is empty
    do {

      // new <- {}
      newSentences.clear();
      // for each rule in KB do
      // (p1 ^ ... ^ pn => q) <-STANDARDIZE-VARIABLES(rule)
      for (Clause impl : KB.getAllDefiniteClauseImplications()) {
        impl = KB.standardizeApart(impl);
        // for each theta such that SUBST(theta, p1 ^ ... ^ pn) =
        // SUBST(theta, p'1 ^ ... ^ p'n)
        // --- for some p'1,...,p'n in KB
        for (Map<Variable, Term> theta : KB.fetch(invert(impl
            .getNegativeLiterals()))) {
          // q' <- SUBST(theta, q)
          Literal qDelta = KB.subst(theta, impl.getPositiveLiterals()
              .get(0));
          // if q' does not unify with some sentence already in KB or
          // new then do
          if (!KB.isRenaming(qDelta)
              && !KB.isRenaming(qDelta, newSentences)) {
            // add q' to new
            newSentences.add(qDelta);
            ansHandler.addProofStep(impl, qDelta, theta);
            // theta <- UNIFY(q', alpha)
            theta = KB.unify(qDelta.getAtomicSentence(),
                alpha.getAtomicSentence());
            // if theta is not fail then return theta
            if (null != theta) {
              for (Literal l : newSentences) {
                Sentence s = null;
View Full Code Here

  // PRIVATE METHODS
  //
  private List<Literal> invert(List<Literal> lits) {
    List<Literal> invLits = new ArrayList<Literal>();
    for (Literal l : lits) {
      invLits.add(new Literal(l.getAtomicSentence(), (l
          .isPositiveLiteral() ? true : false)));
    }
    return invLits;
  }
View Full Code Here

    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();
View Full Code Here

  public void testIsEmpty() {
    Chain c = new Chain();

    Assert.assertTrue(c.isEmpty());

    c.addLiteral(new Literal(new Predicate("P", new ArrayList<Term>())));

    Assert.assertFalse(c.isEmpty());

    List<Literal> lits = new ArrayList<Literal>();

    lits.add(new Literal(new Predicate("P", new ArrayList<Term>())));

    c = new Chain(lits);

    Assert.assertFalse(c.isEmpty());
  }
View Full Code Here

  }

  @Test
  public void testContrapositives() {
    List<Chain> conts;
    Literal p = new Literal(new Predicate("P", new ArrayList<Term>()));
    Literal notq = new Literal(new Predicate("Q", new ArrayList<Term>()),
        true);
    Literal notr = new Literal(new Predicate("R", new ArrayList<Term>()),
        true);

    Chain c = new Chain();

    conts = c.getContrapositives();
View Full Code Here

      throw new IllegalArgumentException(
          "Only Atomic Queries are supported.");
    }

    List<Literal> goals = new ArrayList<Literal>();
    goals.add(new Literal((AtomicSentence) query));

    BCAskAnswerHandler ansHandler = new BCAskAnswerHandler();

    List<List<ProofStepBwChGoal>> allProofSteps = folbcask(KB, ansHandler,
        goals, new HashMap<Variable, Term>());
View Full Code Here

TOP

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

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.