Package aima.core.logic.fol.parsing.ast

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


      this.inferenceProcedure = inferenceProcedure;
    }
  }

  public Sentence tell(String sentence) {
    Sentence s = parser.parse(sentence);
    tell(s);
    return s;
  }
View Full Code Here


  public Object visitQuantifiedSentence(QuantifiedSentence sentence,
      Object arg) {

    Map<Variable, Term> substitution = (Map<Variable, Term>) arg;

    Sentence quantified = sentence.getQuantified();
    Sentence quantifiedAfterSubs = (Sentence) quantified.accept(this, arg);

    List<Variable> variables = new ArrayList<Variable>();
    for (Variable v : sentence.getVariables()) {
      Term st = substitution.get(v);
      if (null != st) {
View Full Code Here

   * @return the specified sentence as a list of clauses, where each clause is
   *         a disjunction of literals.
   */
  public CNF convertToCNF(Sentence aSentence) {
    // I)mplications Out:
    Sentence implicationsOut = (Sentence) aSentence.accept(
        new ImplicationsOut(), null);

    // N)egations In:
    Sentence negationsIn = (Sentence) implicationsOut.accept(
        new NegationsIn(), null);

    // S)tandardize variables:
    // For sentences like:
    // (FORALL x P(x)) V (EXISTS x Q(x)),
    // which use the same variable name twice, change the name of one of the
    // variables.
    Sentence saQuantifiers = (Sentence) negationsIn.accept(
        new StandardizeQuantiferVariables(substVisitor),
        new LinkedHashSet<Variable>());

    // Remove explicit quantifiers, by skolemizing existentials
    // and dropping universals:
    // E)xistentials Out
    // A)lls Out:
    Sentence andsAndOrs = (Sentence) saQuantifiers.accept(
        new RemoveQuantifiers(parser), new LinkedHashSet<Variable>());

    // D)istribution
    // V over ^:
    Sentence orDistributedOverAnd = (Sentence) andsAndOrs.accept(
        new DistributeOrOverAnd(), null);

    // O)perators Out
    return (new CNFConstructor()).construct(orDistributedOverAnd);
  }
View Full Code Here

  public Object visitFunction(Function function, Object arg) {
    return function;
  }

  public Object visitNotSentence(NotSentence notSentence, Object arg) {
    Sentence negated = notSentence.getNegated();

    return new NotSentence((Sentence) negated.accept(this, arg));
  }
View Full Code Here

    return new NotSentence((Sentence) negated.accept(this, arg));
  }

  public Object visitConnectedSentence(ConnectedSentence sentence, Object arg) {
    Sentence alpha = (Sentence) sentence.getFirst().accept(this, arg);
    Sentence beta = (Sentence) sentence.getSecond().accept(this, arg);

    // Eliminate <=>, bi-conditional elimination,
    // replace (alpha <=> beta) with (~alpha V beta) ^ (alpha V ~beta).
    if (Connectors.isBICOND(sentence.getConnector())) {
      Sentence first = new ConnectedSentence(Connectors.OR,
          new NotSentence(alpha), beta);
      Sentence second = new ConnectedSentence(Connectors.OR, alpha,
          new NotSentence(beta));

      return new ConnectedSentence(Connectors.AND, first, second);
    }
View Full Code Here

  }

  public Object visitNotSentence(NotSentence notSentence, Object arg) {
    // CNF requires NOT (~) to appear only in literals, so we 'move ~
    // inwards' by repeated application of the following equivalences:
    Sentence negated = notSentence.getNegated();

    // ~(~alpha) equivalent to alpha (double negation elimination)
    if (negated instanceof NotSentence) {
      return ((NotSentence) negated).getNegated().accept(this, arg);
    }

    if (negated instanceof ConnectedSentence) {
      ConnectedSentence negConnected = (ConnectedSentence) negated;
      Sentence alpha = negConnected.getFirst();
      Sentence beta = negConnected.getSecond();
      // ~(alpha ^ beta) equivalent to (~alpha V ~beta) (De Morgan)
      if (Connectors.isAND(negConnected.getConnector())) {
        // I need to ensure the ~s are moved in deeper
        Sentence notAlpha = (Sentence) (new NotSentence(alpha)).accept(
            this, arg);
        Sentence notBeta = (Sentence) (new NotSentence(beta)).accept(
            this, arg);
        return new ConnectedSentence(Connectors.OR, notAlpha, notBeta);
      }

      // ~(alpha V beta) equivalent to (~alpha ^ ~beta) (De Morgan)
      if (Connectors.isOR(negConnected.getConnector())) {
        // I need to ensure the ~s are moved in deeper
        Sentence notAlpha = (Sentence) (new NotSentence(alpha)).accept(
            this, arg);
        Sentence notBeta = (Sentence) (new NotSentence(beta)).accept(
            this, arg);
        return new ConnectedSentence(Connectors.AND, notAlpha, notBeta);
      }
    }

    // in addition, rules for negated quantifiers:
    if (negated instanceof QuantifiedSentence) {
      QuantifiedSentence negQuantified = (QuantifiedSentence) negated;
      // I need to ensure the ~ is moved in deeper
      Sentence notP = (Sentence) (new NotSentence(
          negQuantified.getQuantified())).accept(this, arg);

      // ~FORALL x p becomes EXISTS x ~p
      if (Quantifiers.isFORALL(negQuantified.getQuantifier())) {
        return new QuantifiedSentence(Quantifiers.EXISTS,
View Full Code Here

      reflexivityClause = KB.standardizeApart(reflexivityClause);
      reflexivityClause.setStandardizedApartCheckNotRequired();
      usable.add(reflexivityClause);
    }

    Sentence notAlpha = new NotSentence(alpha);
    // 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();
View Full Code Here

        replVariables.add(v);
      }
    }

    // Apply the local subst
    Sentence subst = substVisitor.subst(localSubst,
        sentence.getQuantified());

    // Ensure all my existing and replaced variable
    // names are tracked
    seenSoFar.addAll(replVariables);

    Sentence sQuantified = (Sentence) subst.accept(this, arg);

    return new QuantifiedSentence(sentence.getQuantifier(), replVariables,
        sQuantified);
  }
View Full Code Here

  }

  @SuppressWarnings("unchecked")
  public Object visitQuantifiedSentence(QuantifiedSentence sentence,
      Object arg) {
    Sentence quantified = sentence.getQuantified();
    Set<Variable> universalScope = (Set<Variable>) arg;

    // Skolemize: Skolemization is the process of removing existential
    // quantifiers by elimination. This is done by introducing Skolem
    // functions. The general rule is that the arguments of the Skolem
    // function are all the universally quantified variables in whose
    // scope the existential quantifier appears.
    if (Quantifiers.isEXISTS(sentence.getQuantifier())) {
      Map<Variable, Term> skolemSubst = new LinkedHashMap<Variable, Term>();
      for (Variable eVar : sentence.getVariables()) {
        if (universalScope.size() > 0) {
          // Replace with a Skolem Function
          String skolemFunctionName = parser.getFOLDomain()
              .addSkolemFunction();
          skolemSubst.put(eVar, new Function(skolemFunctionName,
              new ArrayList<Term>(universalScope)));
        } else {
          // Replace with a Skolem Constant
          String skolemConstantName = parser.getFOLDomain()
              .addSkolemConstant();
          skolemSubst.put(eVar, new Constant(skolemConstantName));
        }
      }

      Sentence skolemized = substVisitor.subst(skolemSubst, quantified);
      return skolemized.accept(this, arg);
    }

    // Drop universal quantifiers.
    if (Quantifiers.isFORALL(sentence.getQuantifier())) {
      // Add to the universal scope so that
      // existential skolemization may be done correctly
      universalScope.addAll(sentence.getVariables());

      Sentence droppedUniversal = (Sentence) quantified.accept(this, arg);

      // Enusre my scope is removed before moving back up
      // the call stack when returning
      universalScope.removeAll(sentence.getVariables());
View Full Code Here

  public Object visitConnectedSentence(ConnectedSentence sentence, Object arg) {
    // Distribute V over ^:

    // This will cause flattening out of nested ^s and Vs
    Sentence alpha = (Sentence) sentence.getFirst().accept(this, arg);
    Sentence beta = (Sentence) sentence.getSecond().accept(this, arg);

    // (alpha V (beta ^ gamma)) equivalent to
    // ((alpha V beta) ^ (alpha V gamma))
    if (Connectors.isOR(sentence.getConnector())
        && ConnectedSentence.class.isInstance(beta)) {
      ConnectedSentence betaAndGamma = (ConnectedSentence) beta;
      if (Connectors.isAND(betaAndGamma.getConnector())) {
        beta = betaAndGamma.getFirst();
        Sentence gamma = betaAndGamma.getSecond();
        return new ConnectedSentence(Connectors.AND,
            (Sentence) (new ConnectedSentence(Connectors.OR, alpha,
                beta)).accept(this, arg),
            (Sentence) (new ConnectedSentence(Connectors.OR, alpha,
                gamma)).accept(this, arg));
      }
    }

    // ((alpha ^ gamma) V beta) equivalent to
    // ((alpha V beta) ^ (gamma V beta))
    if (Connectors.isOR(sentence.getConnector())
        && ConnectedSentence.class.isInstance(alpha)) {
      ConnectedSentence alphaAndGamma = (ConnectedSentence) alpha;
      if (Connectors.isAND(alphaAndGamma.getConnector())) {
        alpha = alphaAndGamma.getFirst();
        Sentence gamma = alphaAndGamma.getSecond();
        return new ConnectedSentence(Connectors.AND,
            (Sentence) (new ConnectedSentence(Connectors.OR, alpha,
                beta)).accept(this, arg),
            (Sentence) (new ConnectedSentence(Connectors.OR, gamma,
                beta)).accept(this, arg));
View Full Code Here

TOP

Related Classes of aima.core.logic.fol.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.