Example #1
0
  /**
   * 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;
  }
Example #2
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());
  }
  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());
  }
Example #4
0
  /**
   * Method to parse a Skolem constant
   *
   * @param sko Element The XOM element that represents the Skolem to be parsed.
   * @return Returns the data structure that represents the Skolem in a way that can be used by the
   *     reasoning engine.
   * @throws ParseException Thrown if there is an error parsing the Skolem.
   */
  private Term parseSkolem(Element sko) throws ParseException {
    String skoname = sko.getValue().trim();

    if (skoname.isEmpty()) {
      return new Term(
          SymbolTable.internSymbol("$gensym" + SymbolTable.genid++),
          SymbolTable.INOROLE,
          Types.ITHING);
    } else {
      if (this.skolemMap.containsKey(skoname)) {
        String sym = skolemMap.get(skoname);
        return new Term(SymbolTable.internSymbol(sym), SymbolTable.INOROLE, Types.ITHING);
      } else {
        String sym = "$gensym" + (SymbolTable.genid++) + "$" + skoname;
        skolemMap.put(skoname, sym);
        return new Term(SymbolTable.internSymbol(sym), SymbolTable.INOROLE, Types.ITHING);
      }
    }
  }
Example #5
0
  /**
   * Parse a simple element that just contains plain data (like <Ind> and <Data>).
   *
   * @param element The element to parse
   * @return A Term data structure that represents the element in a way that can be understood by
   *     the reasoning engine.
   * @throws ParseException Thrown if the type specified in the optional type attribute is invalid.
   */
  private Term parseSimpleElement(Element element) throws ParseException {
    String symbolName = element.getValue().trim();

    if (symbolName.isEmpty()) {
      symbolName = generateAnonymousIdentifier();
    }

    int sym = SymbolTable.internSymbol(symbolName);
    int typeid = parseTypeAttribute(element);

    return new Term(sym, SymbolTable.INOROLE, typeid);
  }
Example #6
0
  /**
   * Method to parse a Expr (Expresion)
   *
   * @param expr Element The XOM element that represents the Expr to be parsed.
   * @return Returns the data structure that represents the Expr in a way that can be used by the
   *     reasoning engine.
   * @throws ParseException Thrown if there is an error parsing the Expr.
   */
  private Term parseExpression(Element expr) throws ParseException {
    Elements children = expr.getChildElements();
    Element op = children.get(0);

    boolean foundOp = false;

    if (op.getLocalName().equals(tagNames.OP)) {
      foundOp = true;
    }

    Element fun = null;
    if (foundOp) {
      Elements funTag = op.getChildElements();
      fun = funTag.get(0);

      // Remove <op> element including its child element <Fun>, so
      // it will not be parsed twice
      expr.removeChild(op);
    }

    if (!foundOp) {
      fun = children.get(0);

      // Remove <Fun> element so that it will not be parsed twice
      expr.removeChild(fun);
    }

    if (!fun.getLocalName().equals(tagNames.FUN)) {
      throw new ParseException("First child of op in an Expr must be a Fun element.");
    }

    int symbol = SymbolTable.internSymbol(fun.getValue().trim());
    int typeid = parseTypeAttribute(expr);

    Vector<Term> subterms = parseDefaultElements(expr);
    Term t = new Term(symbol, SymbolTable.INOROLE, typeid, subterms);
    return t;
  }
Example #7
0
  /**
   * 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;
  }