Package aima.core.logic.propositional.parsing.ast

Examples of aima.core.logic.propositional.parsing.ast.Sentence


   *
   * @return the answer to the specified question using the TT-Entails
   *         algorithm.
   */
  public boolean ttEntails(KnowledgeBase kb, String alpha) {
    Sentence kbSentence = kb.asSentence();
    Sentence querySentence = (Sentence) new PEParser().parse(alpha);
    SymbolCollector collector = new SymbolCollector();
    Set<Symbol> kbSymbols = collector.getSymbolsIn(kbSentence);
    Set<Symbol> querySymbols = collector.getSymbolsIn(querySentence);
    Set<Symbol> symbols = SetOps.union(kbSymbols, querySymbols);
    List<Symbol> symbolList = new Converter<Symbol>().setToList(symbols);
View Full Code Here


  public Object visitNotSentence(UnarySentence us, Object arg) {
    return transformNotSentence(us);
  }

  public Sentence transform(Sentence s) {
    Sentence toTransform = s;
    while (!(toTransform.equals(step(toTransform)))) {
      toTransform = step(toTransform);
    }

    return toTransform;
  }
View Full Code Here

  private Sentence step(Sentence s) {
    return (Sentence) s.accept(this, null);
  }

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

        .accept(this, null));
    return new BinarySentence("AND", first, second);
  }

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

      return (Sentence) ((UnarySentence) us.getNegated()).getNegated()
          .accept(this, null);
    } else if (us.getNegated() instanceof 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) super.visitNotSentence(us, null);
      }
View Full Code Here

  }

  private Sentence distributeOrOverAnd(BinarySentence bs) {
    BinarySentence andTerm = bs.firstTermIsAndSentence() ? (BinarySentence) bs
        .getFirst() : (BinarySentence) bs.getSecond();
    Sentence otherterm = bs.firstTermIsAndSentence() ? bs.getSecond() : bs
        .getFirst();
    // (alpha or (beta and gamma) = ((alpha or beta) and (alpha or gamma))
    Sentence alpha = (Sentence) otherterm.accept(this, null);
    Sentence beta = (Sentence) andTerm.getFirst().accept(this, null);
    Sentence gamma = (Sentence) andTerm.getSecond().accept(this, null);
    Sentence distributed = new BinarySentence("AND", new BinarySentence(
        "OR", alpha, beta), new BinarySentence("OR", alpha, gamma));
    return distributed;
  }
View Full Code Here

  private Random random = new Random();

  public Model findModelFor(String logicalSentence, int numberOfFlips,
      double probabilityOfRandomWalk) {
    myModel = new Model();
    Sentence s = (Sentence) new PEParser().parse(logicalSentence);
    CNFTransformer transformer = new CNFTransformer();
    CNFClauseGatherer clauseGatherer = new CNFClauseGatherer();
    SymbolCollector sc = new SymbolCollector();

    List<Symbol> symbols = new Converter<Symbol>().setToList(sc.getSymbolsIn(s));
    for (int i = 0; i < symbols.size(); i++) {
      Symbol sym = (Symbol) symbols.get(i);
      myModel = myModel.extend(sym, Util.randomBoolean());
    }
    List<Sentence> clauses = new Converter<Sentence>()
        .setToList(clauseGatherer.getClausesFrom(transformer
            .transform(s)));

    for (int i = 0; i < numberOfFlips; i++) {
      if (getNumberOfClausesSatisfiedIn(
          new Converter<Sentence>().listToSet(clauses), myModel) == clauses
          .size()) {
        return myModel;
      }
      Sentence clause = clauses.get(random.nextInt(clauses.size()));

      List<Symbol> symbolsInClause = new Converter<Symbol>().setToList(sc
          .getSymbolsIn(clause));
      if (random.nextDouble() >= probabilityOfRandomWalk) {
        Symbol randomSymbol = symbolsInClause.get(random
View Full Code Here

  private int getNumberOfClausesSatisfiedIn(Set<Sentence> clauses, Model model) {
    int retVal = 0;
    Iterator<Sentence> i = clauses.iterator();
    while (i.hasNext()) {
      Sentence s = i.next();
      if (model.isTrue(s)) {
        retVal += 1;
      }
    }
    return retVal;
View Full Code Here

  public Object visitMultiSentence(MultiSentence fs, Object arg) {
    List<Sentence> terms = fs.getSentences();
    List<Sentence> newTerms = new ArrayList<Sentence>();
    for (int i = 0; i < terms.size(); i++) {
      Sentence s = (Sentence) terms.get(i);
      Sentence subsTerm = (Sentence) s.accept(this, arg);
      newTerms.add(subsTerm);
    }
    return new MultiSentence(fs.getOperator(), newTerms);
  }
View Full Code Here

   *
   * @param aSentence
   *            a fact to be added to the knowledge base.
   */
  public void tell(String aSentence) {
    Sentence sentence = (Sentence) parser.parse(aSentence);
    if (!(sentences.contains(sentence))) {
      sentences.add(sentence);
    }
  }
View Full Code Here

TOP

Related Classes of aima.core.logic.propositional.parsing.ast.Sentence

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.