/**
   * Method to parse a Slot
   *
   * @param slot Element The XOM element that represents the slot to be parsed.
   * @return Returns the data structure that represents the slot in a way that can be used by the
   *     reasoning engine.
   * @throws ParseException Thrown if there is an error parsing the slot.
   */
  private Term parseSlot(Element slot) throws ParseException {
    Elements children = slot.getChildElements();

    Element firstChildName = children.get(0);
    boolean dataSlot = false;
    if (firstChildName.getLocalName().equals(tagNames.DATA)) {
      dataSlot = true;
    }

    if (!firstChildName.getLocalName().equals(tagNames.IND)
        && !firstChildName.getLocalName().equals(tagNames.DATA)) {
      throw new ParseException("In a <slot> only <Ind> and <Data> are allowed.");
    }
    // Getting the Role from the symbol Table, it will assign one if it
    // doesnt already exist
    int role = SymbolTable.internRole(firstChildName.getValue().trim());

    Element element = children.get(1);
    Term term = parseDefaultElement(element);

    term.setRole(role);

    if (dataSlot) {
      term.setDataSlot(true);
    }

    return term;
  }
  /**
   * Method to parse a naf (Negation as Failure)
   *
   * @param naf Element The XOM element that represents the naf atom to be parsed.
   * @return Returns the data structure that represents the naf in a way that can be used by the
   *     reasoning engine.
   * @throws ParseException Thrown if there is an error parsing the naf.
   */
  private Term parseNaf(Element naf) throws ParseException {
    Elements children = naf.getChildElements();
    Element el = skipRoleTag(children.get(0));

    Vector<Term> subterms = new Vector<Term>();
    if (el.getLocalName().equals(tagNames.ATOM)) {
      subterms.add(parseAtom(el, false, false));
    } else if (el.getLocalName().equals(tagNames.AND)) {
      children = el.getChildElements();
      for (int i = 0; i < children.size(); i++) {
        Element el2 = children.get(i);
        if (!el2.getLocalName().equals(tagNames.ATOM)) {
          throw new ParseException("And child of Naf element should only contain Atom elements.");
        }

        subterms.add(parseAtom(el2, false, false));
      }
    } else {
      throw new ParseException("Naf element should only contain Atom or And child element.");
    }

    Term t = new Term(SymbolTable.INAF, SymbolTable.INOROLE, Types.IOBJECT, subterms);

    t.setAtom(true);
    return t;
  }
  /**
   * This method is used to parse and <oid> element.
   *
   * @param oid Element The XOM element that represents the oid to be parsed.
   * @return Returns the data structure that represents the oid in a way that can be used by the
   *     reasoning engine.
   * @throws ParseException Thrown if there is an error parsing the oid.
   */
  private Term parseOid(Element oid) throws ParseException {
    Element element = oid.getChildElements().get(0);
    Term term = parseDefaultElement(element);
    term.role = SymbolTable.IOID;

    return term;
  }
  /**
   * This method is used to parse an Assertion in the RuleML Document.
   *
   * @param ass Element The XOM Element object that represents the assertion.
   * @return A term object that represents the assertion in a way that can be used by the reasoning
   *     engine.
   * @throws ParseException Thrown if there is a serious error parsing the assertion.
   */
  private Term parseAssert(Element ass) throws ParseException {
    Elements children = ass.getChildElements();

    Element firstChild = skipRoleTag(children.get(0));

    if (firstChild.getLocalName().equals(tagNames.ATOM)) {
      DefiniteClause dc = parseFact(firstChild);
      Vector<Term> v = new Vector<Term>();
      for (int i = 0; i < dc.atoms.length; i++) {
        v.add(dc.atoms[i]);
      }
      Term t = new Term(SymbolTable.IASSERT, SymbolTable.INOROLE, Types.IOBJECT, v);
      t.setAtom(true);
      return t;
    } else if (firstChild.getLocalName().equals(tagNames.IMPLIES)) {
      Vector<DefiniteClause> v2 = parseImplies(firstChild);
      DefiniteClause dc = (DefiniteClause) v2.get(0);
      Vector<Term> v = new Vector<Term>();
      for (int i = 0; i < dc.atoms.length; i++) {
        v.add(dc.atoms[i]);
      }
      Term t = new Term(SymbolTable.IASSERT, SymbolTable.INOROLE, Types.IOBJECT, v);
      t.setAtom(true);
      return t;
    } else {
      throw new ParseException("Assert element can only contain Atom (fact) or Implies elements.");
    }
  }
  /**
   * Method to parse a resl (Rested Slot)
   *
   * @param resl Element The XOM element that represents the resl to be parsed.
   * @return Returns the data structure that represents the resl in a way that can be used by the
   *     reasoning engine.
   * @throws ParseException Thrown if there is an error parsing the resl.
   */
  private Term parseResl(Element resl) throws ParseException {
    Elements children = resl.getChildElements();
    Element firstChild = skipRoleTag(children.get(0));

    Term t = parseDefaultElement(firstChild);

    // HACK: only change symbol, if term is no <Plex>
    if (t.getSymbol() != SymbolTable.IPLEX) {
      t.setRole(SymbolTable.IREST);
    }
    return t;
  }
Exemple #6
0
  public DefiniteClause buildResult(Term t) {
    if (t.getSymbol() != symbol) {
      return null;
    }

    if (t.subTerms.length != 3) {
      return null;
    }

    Term p2 = t.subTerms[2].deepCopy();

    if (p2.getSymbol() < 0) {
      return null;
    }

    if (p2.getType() != Types.IFLOAT && p2.getType() != Types.IINTEGER) {
      return null;
    }

    String p2s = p2.getSymbolString();

    double p2d;
    try {
      p2d = Double.parseDouble(p2s);
    } catch (Exception e) {
      return null;
    }

    double result = Math.tan(p2d);
    String results = "" + result;

    Term r1 = new Term(SymbolTable.internSymbol(results), SymbolTable.INOROLE, Types.IFLOAT);

    Term roid =
        new Term(SymbolTable.internSymbol("$jdrew-tan-" + p2s), SymbolTable.IOID, Types.ITHING);

    Vector v = new Vector();
    v.add(roid);
    v.add(r1);
    v.add(p2);

    Term atm = new Term(symbol, SymbolTable.INOROLE, Types.IOBJECT, v);
    atm.setAtom(true);
    Vector v2 = new Vector();
    v2.add(atm);
    return new DefiniteClause(v2, new Vector());
  }
 /**
  * Method to parse a repo (Rested Positional Slot)
  *
  * @param repo Element The XOM element that represents the repo to be parsed.
  * @return Term Returns the data structure that represents the repo in a way that can be used by
  *     the reasoning engine.
  * @throws ParseException Thrown if there is an error parsing the repo.
  */
 private Term parseRepo(Element repo) throws ParseException {
   Term t = parseResl(repo);
   t.setRole(SymbolTable.IPREST);
   return t;
 }
  /**
   * Method to parse an Atom
   *
   * @param Atom Element The XOM element that represents the Atom to be parsed.
   * @param head boolean This tells the engine if the atom is a head of a rule.
   * @param neg boolean This tells the engine if the atom is a negative atom (Neg)
   * @return Returns the data structure that represents the Atom in a way that can be used by the
   *     reasoning engine.
   * @throws ParseException Thrown if there is an error parsing the Atom.
   */
  private Term parseAtom(Element atom, boolean head, boolean neg) throws ParseException {

    boolean foundoid = false;

    Elements children = atom.getChildElements();

    // checking for op tag before the rel
    Element op = getFirstChildElement(atom, tagNames.OP);

    Element rel = null;

    if (op != null) {
      Elements relTag = op.getChildElements();
      rel = relTag.get(0);
    } else {
      rel = getFirstChildElement(atom, tagNames.REL);
    }

    if (rel == null) {
      throw new ParseException("In an <Atom>, first child of <op> must be a <Rel>.");
    }

    String relname = rel.getValue().trim();
    if (neg) {
      relname = "$neg-" + relname;
    }

    int symbol = SymbolTable.internSymbol(relname);

    Vector<Term> subterms = new Vector<Term>();
    int startIndex = getFirstChildElementIndex(children, 0) + 1;
    for (int i = startIndex; i < children.size(); i++) {
      Element element = children.get(i);
      Term term = null;
      if (element.getLocalName().equals(tagNames.OID)) {
        if (foundoid) {
          throw new ParseException("<Atom> must contain a most one <oid>.");
        }
        term = parseOid(element);
        foundoid = true;
      } else {
        term = parseDefaultElement(element);
      }
      subterms.add(term);
    }

    // Generate a fake OID if the atom does not have one.
    // This is a requirement put in place by the reasoner.
    if (!foundoid) {
      if (head) {
        String symname = "$gensym" + SymbolTable.genid++;
        int symid = SymbolTable.internSymbol(symname);
        Term t2 = new Term(symid, SymbolTable.IOID, Types.IOBJECT);
        subterms.add(t2);
      } else {
        String varname = generateAnonymousIdentifier();
        int symid = this.internVariable(varname);
        Vector<Integer> types = new Vector<Integer>();
        types.add(Types.IOBJECT);
        this.varClasses.put(symid, types);
        Term t2 = new Term(symid, SymbolTable.IOID, Types.IOBJECT);
        subterms.add(t2);
      }
    }

    Term t = new Term(symbol, SymbolTable.INOROLE, Types.IOBJECT, subterms);
    t.setAtom(true);
    return t;
  }
  private Term parseData(Element data) throws ParseException {
    Term term = parseSimpleElement(data);
    term.setData(true);

    return term;
  }
  /**
   * This method is used to parse a implication element, creating a new variable name list if
   * indicated. Typically a new variable list is wanted; but in certain cases (such as parsing an
   * inner clause in an assert) the same variable list must be used.
   *
   * @param implies Element The XOM Element object that represents the implication to be parsed.
   * @return A vector of DefiniteClause data structures that represents the implication in a way
   *     that can be used by the reasoning engine.
   * @throws ParseException Thrown if there is a serious error parsing the implication.
   */
  private Vector<DefiniteClause> parseImplies(Element implies) throws ParseException {

    Vector<DefiniteClause> newclauses = new Vector<DefiniteClause>();

    // Implies must have two children (excluding OID elements)
    Elements children = implies.getChildElements();

    // Set first and second child and skip OID element if exists.
    Element firstChild = skipRoleTag(children.get(0));
    Element secondChild = skipRoleTag(children.get(1));
    if (firstChild.getLocalName().equals(tagNames.OID)) {
      firstChild = skipRoleTag(children.get(1));
      secondChild = skipRoleTag(children.get(2));
    }

    String firstChildName = firstChild.getLocalName();
    Element premise, conclusion;
    // Check if implies starts with premise element
    if (firstChildName.equals(tagNames.PREMISE)) {
      premise = firstChild.getChildElements().get(0);
      conclusion = secondChild.getChildElements().get(0);
    }
    // If implies starts with conclusion element
    else if (firstChildName.equals(tagNames.CONCLUSION)) {
      premise = secondChild.getChildElements().get(0);
      conclusion = firstChild.getChildElements().get(0);
    }
    // No premise or conclusion tag available
    else {
      // Use canonical order for stripe-skipped syntax
      premise = firstChild;
      conclusion = secondChild;
    }

    premise = skipRoleTag(premise);
    conclusion = skipRoleTag(conclusion);

    Vector<Term> subterms = new Vector<Term>();
    if (conclusion.getLocalName().equals(tagNames.ATOM)) {
      subterms.add(parseAtom(conclusion, true, false));
    } else if (conclusion.getLocalName().equals(tagNames.NEG)) {
      Elements headatms = conclusion.getChildElements(tagNames.ATOM);
      if (headatms.size() != 1) throw new ParseException("Neg should have one ATOM element");

      Term atom = parseAtom(headatms.get(0), true, true);
      subterms.add(atom);

      String atomstr = atom.toPOSLString(true);

      String clause = "$Sinconsistent() :-";
      clause += atomstr + ", \"" + atomstr.substring(6) + ".";

      System.err.println(clause);

      POSLParser pp = new POSLParser();
      try {
        DefiniteClause dc2 = pp.parseDefiniteClause(clause);
        if (dc2 != null) newclauses.add(dc2);
      } catch (Exception e) {
        throw new ParseException("Error creating inconsistency check rule.");
      }
    } else {
      throw new ParseException(
          "Second element of Implies should always be an Atom or Neg element.");
    }

    String premiseName = premise.getLocalName();

    if (premiseName.equals(tagNames.ATOM)) {
      subterms.add(parseAtom(premise, false, false));
    } else if (premiseName.equals(tagNames.NAF)) {
      subterms.add(parseNaf(premise));
    } else if (premiseName.equals(tagNames.ASSERT)) {
      subterms.add(parseAssert(premise));
    } else if (premiseName.equals(tagNames.NEG)) {
      subterms.add(parseAtom(premise, false, true));
    } else if (premiseName.equals(tagNames.AND)) {
      children = premise.getChildElements();
      for (int i = 0; i < children.size(); i++) {
        Element el = skipRoleTag(children.get(i));
        if (el.getLocalName().equals(tagNames.ATOM)) {
          subterms.add(parseAtom(el, false, false));
        } else if (el.getLocalName().equals(tagNames.NAF)) {
          subterms.add(parseNaf(el));
        } else if (el.getLocalName().equals(tagNames.ASSERT)) {
          subterms.add(parseAssert(el));
        } else if (el.getLocalName().equals(tagNames.NEG)) {
          subterms.add(parseAtom(el, false, true));
        } else {
          throw new ParseException(
              "Implies And element should only contain Atom and Naf elements.");
        }
      }
    } else {
      throw new ParseException("First element of Implies should be an Atom, Naf or And element.");
    }

    logger.debug("Building Types");
    Hashtable<Integer, Integer> types = this.buildTypeTable();
    logger.debug("Built Types");
    Iterator<Term> it = subterms.iterator();
    int i = 0;
    while (it.hasNext()) {
      Term t = (Term) it.next();
      this.fixVarTypes(t, types);
      logger.debug("Fixed atom : " + i++);
    }

    DefiniteClause dc = new DefiniteClause(subterms, variableNames);
    newclauses.add(0, dc);
    return newclauses;
  }
  public DefiniteClause buildResult(Term t) {
    if (t.getSymbol() != sym) {
      return null;
    }

    if (t.subTerms.length != 3) {
      return null;
    }

    Term p1 = t.subTerms[1].deepCopy();
    Term p2 = t.subTerms[2].deepCopy();

    if (p1.getSymbol() < 0 || p2.getSymbol() < 0) {
      return null;
    }

    String p1s = p1.getSymbolString();
    String p2s = p2.getSymbolString();

    if ((p1.getType() == Types.IFLOAT || p1.getType() == Types.IINTEGER)
        && (p2.getType() == Types.IFLOAT || p2.getType() == Types.IINTEGER)) {
      double d1;
      double d2;
      try {
        d1 = Double.parseDouble(p1s);
        d2 = Double.parseDouble(p2s);
      } catch (Exception e) {
        return null;
      }
      if (d1 > d2) {
        return null;
      }
    } else if (p1.getType() == Types.ISTRING && p2.getType() == Types.ISTRING) {
      if (p1s.compareTo(p2s) > 0) {
        return null;
      }
    } else {
      return null;
    }

    Term roid =
        new Term(
            SymbolTable.internSymbol("$jdrew-lte-" + p1s + "<=" + p2s),
            SymbolTable.IOID,
            Types.ITHING);

    Vector v = new Vector();
    v.add(roid);
    v.add(p1);
    v.add(p2);

    Term atm = new Term(sym, SymbolTable.INOROLE, Types.IOBJECT, v);
    atm.setAtom(true);
    Vector v2 = new Vector();
    v2.add(atm);
    return new DefiniteClause(v2, new Vector());
  }