/** * 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 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; }
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()); }
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 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; }