Clover coverage report - Maven Clover report
Coverage timestamp: Tue Sep 16 2008 01:16:37 EEST
file stats: LOC: 449   Methods: 20
NCLOC: 319   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XMLExprParser.java 0% 0% 0% 0%
coverage
 1    /*
 2    wsmo4j - a WSMO API and Reference Implementation
 3    Copyright (c) 2005, University of Innsbruck, Austria
 4    This library is free software; you can redistribute it and/or modify it under
 5    the terms of the GNU Lesser General Public License as published by the Free
 6    Software Foundation; either version 2.1 of the License, or (at your option)
 7    any later version.
 8    This library is distributed in the hope that it will be useful, but WITHOUT
 9    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 10    FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 11    details.
 12    You should have received a copy of the GNU Lesser General Public License along
 13    with this library; if not, write to the Free Software Foundation, Inc.,
 14    59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 15    */
 16    package org.deri.wsmo4j.io.parser.xml;
 17   
 18   
 19    import java.math.*;
 20    import java.util.*;
 21   
 22    import org.deri.wsmo4j.logicalexpression.*;
 23    import org.omwg.logicalexpression.*;
 24    import org.omwg.logicalexpression.terms.*;
 25    import org.omwg.ontology.Variable;
 26    import org.w3c.dom.*;
 27    import org.wsmo.common.*;
 28    import org.wsmo.factory.*;
 29    import org.wsmo.wsml.*;
 30   
 31   
 32    /**
 33    * @author retkru
 34    * @see org.deri.wsmo4j.io.parser.xml.LogExprParserImpl
 35    */
 36    public class XMLExprParser {
 37   
 38    DataFactory df;
 39    LogicalExpressionFactory leFactory;
 40    WsmoFactory factory;
 41   
 42  0 public XMLExprParser(WsmoFactory factory,
 43    LogicalExpressionFactory leFactory, DataFactory df){
 44  0 this.df=df;
 45  0 this.leFactory=leFactory;
 46  0 this.factory=factory;
 47    }
 48   
 49    /**
 50    * Recursively builds logical expressions
 51    * @param exprNode SableCC Node that will be parsed, it expects a Node of the
 52    * type "PLogExpr"
 53    * @return logical expression object model
 54    * @throws ParserException in case exprNode is an invalid node or in case
 55    * a wrong element name for a term is submitted to the evaluateXMLTerm method
 56    * @see org.deri.wsmo4j.io.parser.xml.LogExprParserImpl#parse(Object)
 57    */
 58  0 public LogicalExpression evaluateXML(Element exprNode)
 59    throws ParserException {
 60  0 String exprName = exprNode.getNodeName();
 61  0 LogicalExpression logExpr = null;
 62    //resolve all non-text child nodes, in that way we can ensure not
 63    //to fall into the trap of whitespace text
 64  0 List exprs = null;
 65  0 if (exprName.equals("and")) {
 66  0 exprs = childElements(exprNode.getChildNodes());
 67  0 logExpr = evaluateXMLAnd(exprs);
 68    }
 69  0 else if (exprName.equals("or")) {
 70  0 exprs = childElements(exprNode.getChildNodes());
 71  0 logExpr = evaluateXMLOr(exprs);
 72    }
 73  0 else if (exprName.equals("forall")) {
 74  0 exprs = childElements(exprNode.getChildNodes());
 75  0 logExpr = evaluateXMLForAll(exprs);
 76    }
 77  0 else if (exprName.equals("exists")) {
 78  0 exprs = childElements(exprNode.getChildNodes());
 79  0 logExpr = evaluateXMLExists(exprs);
 80    }
 81  0 else if (exprName.equals("equivalent")) {
 82  0 exprs = childElements(exprNode.getChildNodes());
 83  0 logExpr = evaluateXMLEquals(exprs);
 84    }
 85  0 else if (exprName.equals("impliedBy")) {
 86  0 exprs = childElements(exprNode.getChildNodes());
 87  0 logExpr = evaluateXMLImpliedBy(exprs);
 88    }
 89  0 else if (exprName.equals("implies")) {
 90  0 exprs = childElements(exprNode.getChildNodes());
 91  0 logExpr = evaluateXMLImplies(exprs);
 92    }
 93  0 else if (exprName.equals("impliedByLP")) {
 94  0 exprs = childElements(exprNode.getChildNodes());
 95  0 logExpr = evaluateXMLImpliesLP(exprs);
 96    }
 97  0 else if (exprName.equals("naf")) {
 98  0 exprs = childElements(exprNode.getChildNodes());
 99  0 logExpr = evaluateXMLNaf(exprs);
 100    }
 101  0 else if (exprName.equals("neg")) {
 102  0 exprs = childElements(exprNode.getChildNodes());
 103  0 logExpr = evaluateXMLNeg(exprs);
 104    }
 105  0 else if (exprName.equals("constraint")) {
 106  0 exprs = childElements(exprNode.getChildNodes());
 107  0 logExpr = evaluateXMLConstraing(exprs);
 108    }
 109  0 else if (exprName.equals("molecule")) {
 110  0 NodeList cptE = exprNode.getElementsByTagName("isa");
 111  0 NodeList attrValue = exprNode.getElementsByTagName("attributeValue");
 112  0 NodeList attrDef = exprNode.getElementsByTagName("attributeDefinition");
 113   
 114  0 Term t = evaluateXMLTerm((Element)exprNode.getElementsByTagName("term").item(0));
 115  0 List <Molecule> molecules = new Vector <Molecule>();
 116   
 117  0 if (attrValue.getLength() > 0 || attrDef.getLength() > 0) {
 118  0 if (attrValue.getLength() > 0) {
 119  0 molecules.addAll(evaluateXMLAttrValue(attrValue, t));
 120    }
 121  0 if (attrDef.getLength() > 0) {
 122  0 molecules.addAll(evaluateXMLAttrDef(attrDef, t));
 123    }
 124    }
 125  0 if (cptE.getLength() > 0) {
 126  0 Node isa = cptE.item(0);
 127  0 String type = isa.getAttributes().getNamedItem("type").getNodeValue();
 128  0 if (type.equals("memberOf")) {
 129  0 Iterator types = evaluateXMLIsa(cptE).iterator();
 130  0 while (types.hasNext()){
 131  0 molecules.add(leFactory.createMemberShipMolecule(
 132    t,(Term)types.next()));
 133    }
 134    }
 135  0 else if (type.equals("subConceptOf")) {
 136  0 Iterator types = evaluateXMLIsa(cptE).iterator();
 137  0 while (types.hasNext()){
 138  0 molecules.add(leFactory.createSubConceptMolecule(
 139    t,(Term)types.next()));
 140    }
 141    }
 142    }
 143  0 if (molecules.size()==0){
 144  0 throw new ParserException("XML Parse Error: Molecule element without content detected! ("+t+")", null);
 145    }
 146  0 else if (molecules.size()==1){
 147  0 logExpr = molecules.remove(0);
 148    }
 149    else {
 150  0 logExpr = leFactory.createCompoundMolecule(molecules);
 151    }
 152    }
 153  0 else if (exprName.equals("atom")) {
 154  0 logExpr = evaluateXMLConstructed(exprNode, factory.createIRI(exprNode.getAttribute("name")));
 155   
 156    /*} else if (exprName.equals("term")) {
 157    args.add(_().createLEIRI(exprNode.getAttribute("name")));
 158    logExpr = _().createLogExpression(Operator.ATOM,pOp,args);*/
 159    }
 160    else {
 161  0 throw new ParserException("XML Parse Error... : " + exprName, null);
 162    }
 163  0 return logExpr;
 164    }
 165   
 166    /**
 167    * Creates Variables or basic terms
 168    * @param exprNode Element
 169    * @return a Term
 170    * @throws ParserException in case the Element name is wrong
 171    */
 172  0 private Term evaluateXMLTerm(Element exprNode)
 173    throws ParserException {
 174  0 String exprName = exprNode.getNodeName();
 175  0 Term t = null;
 176  0 if (exprName.equals("variable")) {
 177  0 try {
 178  0 t = leFactory.createVariable(exprNode.getAttribute("name").substring(1));
 179    }
 180    catch (IllegalArgumentException e) {
 181  0 throw new ParserException("XML Parse Error: Zero-length variable not allowed.", e);
 182    }
 183    //FIXME: ADD COMPLEX DATAVALUES!
 184    }
 185  0 else if (exprName.equals("term") ||
 186    exprName.equals("name") ||
 187    exprName.equals("arg") ||
 188    exprName.equals("value")) {
 189  0 String iriStr = exprNode.getAttribute("name");
 190  0 String notation = ConstantTransformer.getInstance().findNotation(iriStr);
 191  0 if (notation != null && notation.equals("_integer")) {
 192  0 t = df.createWsmlInteger(new BigInteger(exprNode.getFirstChild().getNodeValue()));
 193    }
 194  0 else if (notation != null && notation.equals("_decimal")) {
 195  0 t = df.createWsmlDecimal(new BigDecimal(exprNode.getFirstChild().getNodeValue()));
 196    }
 197  0 else if (notation != null && notation.equals("_string")) {
 198  0 t = df.createWsmlString(exprNode.getFirstChild().getNodeValue());
 199    }
 200  0 else if (iriStr.equals(Constants.ANONYMOUS_ID)) {
 201  0 t = factory.createAnonymousID();
 202    }
 203  0 else if (iriStr.startsWith(Constants.ANONYMOUS_ID)) {
 204    //numberedID
 205  0 t = leFactory.createAnonymousID(Byte.parseByte(iriStr.substring(iriStr.lastIndexOf("ID") + 2)));
 206    }
 207    else {
 208  0 t = helpEvaluateTerm(exprNode, iriStr);
 209    }
 210    }
 211  0 else if (exprName.equals("type")) {
 212  0 t = factory.createIRI(exprNode.getAttributes().getNamedItem("name").getNodeValue());
 213    }
 214    else {
 215  0 throw new ParserException("XML Parse Error: wrong element name: " + exprName + " for a term", null);
 216    }
 217  0 return t;
 218    }
 219   
 220    /**
 221    * Creates a variable, an IRI or a ConstructedTerm
 222    * @param exprNode Node
 223    * @param iriStr
 224    * @return a Term
 225    * @throws ParserException in case a wrong element name for a term is submitted
 226    * to the evaluateXMLTerm method
 227    */
 228  0 private Term helpEvaluateTerm(Element exprNode, String iriStr)
 229    throws ParserException {
 230  0 Term t;
 231  0 if (iriStr.startsWith("?")) {
 232  0 try {
 233  0 t = leFactory.createVariable(iriStr.substring(1));
 234    }
 235    catch (IllegalArgumentException e) {
 236  0 throw new ParserException("XML Parse Error: Zero-length variable not allowed.", e);
 237    }
 238    }
 239    else {
 240  0 IRI iri = factory.createIRI(iriStr);
 241  0 if (exprNode.getChildNodes().getLength() > 1) {
 242  0 Element e;
 243  0 Vector <Term> ll = new Vector <Term>();
 244  0 NodeList nl = exprNode.getChildNodes();
 245  0 for (int i = 0; i < nl.getLength(); i++) {
 246  0 Node n = nl.item(i);
 247  0 if (n instanceof Element) {
 248  0 e = (Element)n;
 249  0 ll.add(evaluateXMLTerm(e));
 250    }
 251    }
 252  0 t = leFactory.createConstructedTerm(iri, ll);
 253    }
 254    else {
 255  0 t = iri;
 256    }
 257    }
 258  0 return t;
 259    }
 260   
 261    /**
 262    * Builds a Quantified LogicalExpression
 263    * @param exprs a list of child elements of a specified Node
 264    * @param op type of operator
 265    * @return Quantified Logical Expression
 266    * @throws ParserException in case an invalid node is submitted to the
 267    * evaluateXML method
 268    */
 269  0 private LogicalExpression evaluateXMLExists(List exprs)
 270    throws ParserException {
 271  0 HashSet <Variable> hs = new HashSet <Variable>();
 272  0 for (int i = 0; i < exprs.size() - 1; i++) {
 273  0 hs.add((Variable) evaluateXMLTerm((Element)exprs.get(i)));
 274    }
 275  0 return leFactory.createExistentialQuantification(hs, evaluateXML((Element)exprs.get(exprs.size() - 1)));
 276    }
 277   
 278  0 private LogicalExpression evaluateXMLForAll(List exprs)
 279    throws ParserException {
 280  0 HashSet <Variable> hs = new HashSet <Variable> ();
 281  0 for (int i = 0; i < exprs.size() - 1; i++) {
 282  0 hs.add((Variable) evaluateXMLTerm((Element)exprs.get(i)));
 283    }
 284  0 return leFactory.createUniversalQuantification(hs, evaluateXML((Element)exprs.get(exprs.size() - 1)));
 285    }
 286   
 287    /**
 288    * Builds a Binary LogicalExpression
 289    * @param exprs a list of child elements of a specified Node
 290    * @param op type of operator
 291    * @return Binary Logical Expression
 292    * @throws ParserException in case an invalid node is submitted to the
 293    * evaluateXML method
 294    */
 295  0 private LogicalExpression evaluateXMLAnd(List exprs)
 296    throws ParserException {
 297  0 return leFactory.createConjunction(evaluateXML((Element)exprs.get(0)), evaluateXML((Element)exprs.get(1)));
 298    }
 299   
 300  0 private LogicalExpression evaluateXMLOr(List exprs)
 301    throws ParserException {
 302  0 return leFactory.createDisjunction(evaluateXML((Element)exprs.get(0)), evaluateXML((Element)exprs.get(1)));
 303    }
 304   
 305  0 private LogicalExpression evaluateXMLImplies(List exprs)
 306    throws ParserException {
 307  0 return leFactory.createImplication(evaluateXML((Element)exprs.get(0)), evaluateXML((Element)exprs.get(1)));
 308    }
 309   
 310  0 private LogicalExpression evaluateXMLImpliesLP(List exprs)
 311    throws ParserException {
 312  0 return leFactory.createLogicProgrammingRule(evaluateXML((Element)exprs.get(0)),
 313    evaluateXML((Element)exprs.get(1)));
 314    }
 315   
 316  0 private LogicalExpression evaluateXMLImpliedBy(List exprs)
 317    throws ParserException {
 318  0 return leFactory.createInverseImplication(evaluateXML((Element)exprs.get(0)), evaluateXML((Element)exprs.get(1)));
 319    }
 320   
 321  0 private LogicalExpression evaluateXMLEquals(List exprs)
 322    throws ParserException {
 323  0 return leFactory.createEquivalence(evaluateXML((Element)exprs.get(0)), evaluateXML((Element)exprs.get(1)));
 324    }
 325   
 326    /**
 327    * Builds an Unary LogicalExpression
 328    * @param exprs a list of child elements of a specified Node
 329    * @param op type of operator
 330    * @return Unary Logical Expression
 331    * @throws ParserException in case an invalid node is submitted to the
 332    * evaluateXML method
 333    */
 334  0 private LogicalExpression evaluateXMLNeg(List exprs)
 335    throws ParserException {
 336  0 return leFactory.createNegation(evaluateXML((Element)exprs.get(0)));
 337    }
 338   
 339  0 private LogicalExpression evaluateXMLNaf(List exprs)
 340    throws ParserException {
 341  0 return leFactory.createNegationAsFailure(evaluateXML((Element)exprs.get(0)));
 342    }
 343   
 344  0 private LogicalExpression evaluateXMLConstraing(List exprs)
 345    throws ParserException {
 346  0 return leFactory.createConstraint(evaluateXML((Element)exprs.get(0)));
 347    }
 348   
 349  0 private Set <Molecule> evaluateXMLAttrValue(NodeList attValue, Term id)
 350    throws ParserException {
 351  0 Set <Molecule> molecules = new HashSet <Molecule> ();
 352  0 for (int i = 0; i < attValue.getLength(); i++) {
 353  0 Node singleAttVal = attValue.item(i);
 354  0 Term attributeName = evaluateXMLTerm((Element)childElements(((Element)singleAttVal).getElementsByTagName("name")).get(0));
 355  0 List attValContent = childElements(((Element)singleAttVal).getElementsByTagName("value"));
 356  0 for (int j = 0; j < attValContent.size(); j++) {
 357  0 Term value = evaluateXMLTerm((Element)attValContent.get(j));
 358  0 molecules.add(leFactory.createAttributeValue(id, attributeName, value));
 359    }
 360    }
 361  0 return molecules;
 362    }
 363   
 364    /**
 365    * Put the Attribute Definitions of a Molecule (contained in the NodeList attDef)
 366    * into a Set. A NodeList contains all children of a specified Node.
 367    * @param attDef a list of child elements of a specified Node
 368    * @param id id of the molecule
 369    * @return a Set of molecules
 370    * @throws ParserException in case a wrong element name for a term is submitted
 371    * to the evaluateXMLTerm method
 372    */
 373  0 private Set <Molecule> evaluateXMLAttrDef(NodeList attDef, Term id)
 374    throws ParserException {
 375  0 Set <Molecule> molecules = new HashSet <Molecule> ();
 376  0 for (int i = 0; i < attDef.getLength(); i++) {
 377  0 Node singleAttDef = attDef.item(i);
 378  0 Term attributeName = evaluateXMLTerm((Element)childElements(((Element)singleAttDef).getElementsByTagName("name")).get(0));
 379  0 List attDefContent = childElements(((Element)singleAttDef).getElementsByTagName("type"));
 380  0 for (int j = 0; j < attDefContent.size(); j++) {
 381  0 Term type = evaluateXMLTerm((Element)attDefContent.get(j));
 382  0 if (singleAttDef.getAttributes().getNamedItem("type").getNodeValue().equals("constraining")) {
 383  0 molecules.add(leFactory.createAttributeConstraint(
 384    id, attributeName, type));
 385    }else{
 386  0 molecules.add(leFactory.createAttributeInference(
 387    id, attributeName, type));
 388    }
 389    }
 390    }
 391  0 return molecules;
 392    }
 393   
 394  0 private List <Term> evaluateXMLIsa(NodeList cptE)
 395    throws ParserException {
 396  0 NodeList isaTerms;
 397  0 List <Term> set = new Vector <Term> ();
 398  0 for (int i = 0; i < cptE.getLength(); i++) {
 399  0 isaTerms = ((Element)cptE.item(i)).getElementsByTagName("term");
 400  0 for (int j = 0; j < isaTerms.getLength(); j++) {
 401  0 set.add(evaluateXMLTerm((Element)isaTerms.item(j)));
 402    }
 403    }
 404  0 return set;
 405    }
 406   
 407    /**
 408    * Creates an Atom
 409    * @param exprNode exprNode SableCC Node that will be parsed, it expects a Node of the
 410    * type "PLogExpr"
 411    * @param identifier identifier of the Atom
 412    * @return Atom
 413    * @throws ParserException in case a wrong element name for a term is submitted
 414    * to the evaluateXMLTerm method
 415    */
 416  0 private LogicalExpression evaluateXMLConstructed(Node exprNode, IRI identifier)
 417    throws ParserException {
 418  0 Element e;
 419  0 Vector <Term> ll = new Vector <Term> ();
 420  0 NodeList nl = exprNode.getChildNodes();
 421  0 for (int i = 0; i < nl.getLength(); i++) {
 422  0 Node n = nl.item(i);
 423   
 424  0 if (n instanceof Element) {
 425  0 e = (Element)n;
 426  0 ll.add(evaluateXMLTerm(e));
 427    }
 428    }
 429  0 return leFactory.createAtom(identifier, ll);
 430    }
 431   
 432    /**
 433    * A NodeList contains all children of a specified Node. If there are no
 434    * children, this is a NodeList containing no nodes. The children
 435    * from the NodeList are put into a LinkedList
 436    *
 437    * @param nodes a list of child elements of a specified Node.
 438    * @return a list of child elements of a specified Node
 439    */
 440  0 private List <Node> childElements(NodeList nodes) {
 441  0 List <Node> list = new LinkedList <Node>();
 442  0 for (int i = 0; i < nodes.getLength(); i++) {
 443  0 if (nodes.item(i)instanceof Element) {
 444  0 list.add(nodes.item(i));
 445    }
 446    }
 447  0 return list;
 448    }
 449    }