Clover coverage report - Maven Clover report
Coverage timestamp: Tue Sep 16 2008 01:16:37 EEST
file stats: LOC: 389   Methods: 23
NCLOC: 307   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AtomicExpressionAnalysis.java 0% 0% 0% 0%
coverage
 1    /*
 2    wsmo4j - a WSMO API and Reference Implementation
 3    Copyright (c) 2004, University of Innsbruck, Institute of Computer Science
 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.wsml;
 17   
 18    import java.util.*;
 19   
 20    import org.deri.wsmo4j.io.parser.*;
 21    import org.omwg.logicalexpression.*;
 22    import org.omwg.logicalexpression.terms.*;
 23    import org.omwg.ontology.*;
 24    import org.wsmo.common.*;
 25    import org.wsmo.factory.*;
 26    import org.wsmo.wsml.*;
 27    import org.wsmo.wsml.compiler.node.*;
 28   
 29    import com.ontotext.wsmo4j.common.*;
 30    import com.ontotext.wsmo4j.parser.wsml.*;
 31   
 32   
 33   
 34    /**
 35    *
 36    * <pre>
 37    * Created on 21.11.2005
 38    * Committed by $Author$
 39    * $Source$,
 40    * </pre>
 41    *
 42    * @author Holger Lausen
 43    * @version $Revision$ $Date$
 44    */
 45    public class AtomicExpressionAnalysis extends ASTAnalysis {
 46    private LogicalExpressionFactory lFactory;
 47    private WsmoFactory factory;
 48    private ASTAnalysisContainer container;
 49   
 50    private Stack logexps; // handle to logexp stack
 51    private Stack terms; // handle to terms stack
 52    private Stack termLists; // handle to term lists stack
 53    private Stack molecules;
 54   
 55    private int tempVarCounter=0;
 56    private Stack mathAtomsToRewrite=new Stack();
 57   
 58   
 59  0 public AtomicExpressionAnalysis(ASTAnalysisContainer container,
 60    WsmoFactory factory,
 61    LogicalExpressionFactory lFactory) {
 62   
 63  0 if (container == null || lFactory == null || factory == null) {
 64  0 throw new IllegalArgumentException();
 65    }
 66  0 this.container = container;
 67  0 this.lFactory = lFactory;
 68  0 this.factory = factory;
 69   
 70    // register the handled nodes
 71  0 container.registerNodeHandler(AAtomSimple.class, this);
 72  0 container.registerNodeHandler(AComparison.class, this);
 73  0 container.registerNodeHandler(AAttrDefAttrRelation.class, this);
 74  0 container.registerNodeHandler(AAttrValAttrRelation.class, this);
 75  0 container.registerNodeHandler(AAttributeMoleculeMolecule.class, this);
 76  0 container.registerNodeHandler(AConceptMoleculeNonpreferredMolecule.class, this);
 77  0 container.registerNodeHandler(AConceptMoleculePreferredMolecule.class, this);
 78  0 container.registerNodeHandler(AMoleculeSimple.class, this);
 79   
 80  0 container.registerNodeHandler(AMultVal.class, this);
 81  0 container.registerNodeHandler(AMultiplicationMultVal.class, this);
 82  0 container.registerNodeHandler(ASimpleAdditionArithVal.class, this);
 83  0 container.registerNodeHandler(ASemisimple2AdditionArithVal.class, this);
 84  0 container.registerNodeHandler(ASemisimple1AdditionArithVal.class, this);
 85  0 container.registerNodeHandler(AAdditionArithVal.class, this);
 86   
 87    //shortcuts
 88  0 logexps = container.getStack(LogicalExpression.class);
 89  0 terms = container.getStack(Term.class);
 90  0 termLists = container.getStack(Term[].class);
 91  0 molecules = container.getStack(Molecule.class);
 92   
 93    }
 94   
 95   
 96  0 public void outAAtomSimple(AAtomSimple node) {
 97  0 Term term =null;
 98  0 if (!terms.isEmpty()){
 99  0 term = (Term)terms.pop();
 100    }
 101  0 if (term instanceof ConstructedTerm){
 102  0 ConstructedTerm t = (ConstructedTerm)term;
 103  0 Vector v = new Vector();
 104  0 for(int i=0; i<t.getArity(); i++){
 105  0 v.add(t.getParameter(i));
 106    }
 107  0 logexps.add(lFactory.createAtom(
 108    (IRI) t.getFunctionSymbol(),v));
 109  0 rewriteMathAtoms();
 110  0 return;
 111    }
 112  0 if (term instanceof IRIImpl){
 113  0 logexps.add(lFactory.createAtom(
 114    (IRI) term,
 115    new Vector()));
 116  0 rewriteMathAtoms();
 117  0 return;
 118    }
 119  0 ParserException e = new ParserException("Found Invalid Atom: "+node, null);
 120  0 Token t = null;
 121  0 if (node.getTerm() instanceof AVarTerm){
 122  0 t=((AVarTerm)node.getTerm()).getVariable();
 123    }
 124  0 if (node.getTerm() instanceof ANbAnonymousTerm){
 125  0 t=((ANbAnonymousTerm)node.getTerm()).getNbAnonymous();
 126    }
 127  0 if (node.getTerm() instanceof ADataTerm){
 128  0 PValue pv =((ADataTerm)node.getTerm()).getValue();
 129  0 if (pv instanceof AStringValue){
 130  0 t=((AStringValue)pv).getString();
 131    }
 132  0 if (pv instanceof ANumericValue){
 133  0 PNumber pn = ((ANumericValue)pv).getNumber();
 134  0 if (pn instanceof ADecimalNumber){
 135  0 t=((ADecimal)((ADecimalNumber)pn).getDecimal()).getPosDecimal();
 136    }
 137  0 if (pn instanceof AIntegerNumber){
 138  0 t=((AInteger)((AIntegerNumber)pn).getInteger()).getPosInteger();
 139    }
 140    }
 141  0 if (pv instanceof ADatatypeValue){
 142  0 PFunctionsymbol pf = ((ADatatypeValue)pv).getFunctionsymbol();
 143  0 if (pf instanceof AMathFunctionsymbol){
 144  0 t = ((AMathFunctionsymbol)pf).getLpar();
 145    }
 146  0 if (pf instanceof AParametrizedFunctionsymbol){
 147  0 t = ((AParametrizedFunctionsymbol)pf).getLpar();
 148    }
 149    }
 150    }
 151  0 if (t!=null){
 152  0 e.setErrorLine(t.getLine());
 153  0 e.setErrorPos(t.getPos());
 154    }
 155  0 e.setFoundToken(node.toString());
 156  0 e.setExpectedToken("IRI or sQName");
 157  0 throw new WrappedParsingException(e);
 158    }
 159   
 160  0 public void outAComparison(AComparison node) {
 161  0 Vector v = new Vector();
 162  0 Term te2 = (Term)terms.pop();
 163  0 Term te1 = (Term)terms.pop();
 164  0 v.add(te1);
 165  0 v.add(te2);
 166  0 Identifier op = determineCompOp(node.getCompOp(),te1,te2);
 167  0 logexps.add(lFactory.createAtom(op, v));
 168  0 rewriteMathAtoms();
 169    }
 170   
 171  0 public void outAAttrDefAttrRelation(AAttrDefAttrRelation node) {
 172  0 Term attid = (Term) terms.pop();
 173  0 Term mId = (Term) terms.peek();
 174  0 Term[] values = (Term[]) termLists.pop();
 175   
 176  0 if (node.getAttrDefOp() instanceof AImpliestypeAttrDefOp){
 177  0 for (int i=0; i<values.length; i++){
 178  0 molecules.add(lFactory.createAttributeInference(
 179    mId, attid,values[i]));
 180    }
 181    }
 182    else{
 183  0 for (int i=0; i<values.length; i++){
 184  0 molecules.add(lFactory.createAttributeConstraint(
 185    mId, attid,values[i]));
 186    }
 187    }
 188    }
 189   
 190  0 public void outAAttrValAttrRelation(AAttrValAttrRelation node) {
 191  0 Term attid = (Term) terms.pop();
 192  0 Term mId = (Term) terms.peek();
 193  0 Term[] values = (Term[]) termLists.pop();
 194  0 for (int i=0; i<values.length; i++){
 195  0 molecules.add(lFactory.createAttributeValue(
 196    mId, attid,values[i]));
 197    }
 198    }
 199   
 200  0 public void outAAttributeMoleculeMolecule(AAttributeMoleculeMolecule node) {
 201    //clean termstack from id
 202  0 terms.pop();
 203    }
 204   
 205   
 206  0 public void outAConceptMoleculeNonpreferredMolecule(AConceptMoleculeNonpreferredMolecule node) {
 207  0 moleculeCptHelper(node.getCptOp());
 208    }
 209   
 210  0 public void outAConceptMoleculePreferredMolecule(AConceptMoleculePreferredMolecule node) {
 211  0 moleculeCptHelper(node.getCptOp());
 212    }
 213   
 214  0 private void moleculeCptHelper(PCptOp node){
 215  0 Term [] conceptOrMemberOfTerms = (Term[])termLists.pop();
 216  0 Term molId = (Term) terms.pop();
 217  0 if (node instanceof AMemberofCptOp){
 218  0 for (int i=0; i<conceptOrMemberOfTerms.length; i++){
 219  0 molecules.add(lFactory.createMemberShipMolecule(
 220    molId, conceptOrMemberOfTerms[i]));
 221    }
 222    }
 223    else{
 224  0 for (int i=0; i<conceptOrMemberOfTerms.length; i++){
 225  0 molecules.add(lFactory.createSubConceptMolecule(
 226    molId, conceptOrMemberOfTerms[i]));
 227    }
 228    }
 229    }
 230   
 231  0 public void outAMoleculeSimple(AMoleculeSimple node) {
 232  0 if (molecules.size()==1){
 233  0 logexps.add(molecules.pop());
 234    }
 235    else{
 236  0 logexps.add(lFactory.createCompoundMolecule(new Vector(molecules)));
 237  0 molecules.clear();
 238    }
 239  0 rewriteMathAtoms();
 240    }
 241   
 242    //[arith] +- [mul]
 243  0 public void outAAdditionArithVal(AAdditionArithVal node) {
 244  0 Term t2 = (Term)terms.pop();
 245  0 createPredicate(getFSIRI(node.getArithOp()),
 246    (Term)terms.pop(),t2);
 247    }
 248   
 249    //term +- [mul]
 250  0 public void outASemisimple1AdditionArithVal(ASemisimple1AdditionArithVal node) {
 251  0 Term t2 = (Term)terms.pop();
 252  0 createPredicate(getFSIRI(node.getArithOp()),
 253    (Term)terms.pop(),t2);
 254    }
 255   
 256    //[arith] +- term
 257  0 public void outASemisimple2AdditionArithVal(ASemisimple2AdditionArithVal node) {
 258  0 Term t2 = (Term)terms.pop();
 259  0 createPredicate(getFSIRI(node.getArithOp()),
 260    (Term)terms.pop(),t2);
 261    }
 262    //term +- term
 263  0 public void outASimpleAdditionArithVal(ASimpleAdditionArithVal node) {
 264  0 Term t2 = (Term)terms.pop();
 265  0 createPredicate(getFSIRI(node.getArithOp()),
 266    (Term)terms.pop(),t2);
 267    }
 268   
 269    //[mul] */ term
 270  0 public void outAMultiplicationMultVal(AMultiplicationMultVal node) {
 271  0 Term t2 = (Term)terms.pop();
 272  0 createPredicate(getFSIRI(node.getMulOp()),
 273    (Term)terms.pop(),t2);
 274    }
 275   
 276    //term */ term
 277  0 public void outAMultVal(AMultVal node) {
 278  0 Term t2 = (Term)terms.pop();
 279  0 createPredicate(getFSIRI(node.getMulOp()),
 280    (Term)terms.pop(),t2);
 281    }
 282   
 283  0 private void createPredicate(IRI iri, Term left, Term right){
 284  0 Variable v = new TempVariable(tempVarCounter++);
 285  0 List params = new Vector();
 286  0 params.add(v);
 287  0 params.add(left);
 288  0 params.add(right);
 289  0 mathAtomsToRewrite.push(lFactory.createAtom(iri,params));
 290  0 terms.add(v);
 291    }
 292   
 293  0 private void rewriteMathAtoms() {
 294  0 while (!mathAtomsToRewrite.isEmpty()){
 295  0 LogicalExpression left = (LogicalExpression) logexps.pop();
 296  0 Atom right = (Atom) mathAtomsToRewrite.pop();
 297  0 logexps.push(lFactory.createConjunction(left, right));
 298    }
 299    }
 300   
 301  0 private IRI getFSIRI(PArithOp op) {
 302  0 if (op instanceof AAddArithOp) {
 303    //add
 304  0 return factory.createIRI(Constants.NUMERIC_ADD);
 305    }
 306    //sub
 307  0 return factory.createIRI(Constants.NUMERIC_SUB);
 308    }
 309   
 310  0 private IRI getFSIRI(PMulOp op) {
 311  0 if (op instanceof AMulMulOp) {
 312    //mult
 313  0 return factory.createIRI(Constants.NUMERIC_MUL);
 314    }
 315    //div
 316  0 return factory.createIRI(Constants.NUMERIC_DIV);
 317    }
 318   
 319   
 320  0 private Identifier determineCompOp(PCompOp cOp, Term te1, Term te2) {
 321  0 Identifier op;
 322  0 if (cOp instanceof AEqualCompOp) {
 323    //equal, stringEqual, numericEqual
 324  0 if(isNumeric(te1) && isNumeric(te2)) {
 325  0 op = factory.createIRI(Constants.NUMERIC_EQUAL);
 326  0 } else if (isString(te1) && isString(te2)){
 327  0 op = factory.createIRI(Constants.STRING_EQUAL);
 328    } else {
 329  0 op = factory.createIRI(Constants.EQUAL);
 330    }
 331    }
 332  0 else if (cOp instanceof AGtCompOp) {
 333  0 op = factory.createIRI(Constants.GREATER_THAN);
 334    }
 335  0 else if (cOp instanceof AGteCompOp) {
 336  0 op = factory.createIRI(Constants.GREATER_EQUAL);
 337    }
 338  0 else if (cOp instanceof ALtCompOp) {
 339  0 op = factory.createIRI(Constants.LESS_THAN);
 340    }
 341  0 else if (cOp instanceof ALteCompOp) {
 342  0 op = factory.createIRI(Constants.LESS_EQUAL);
 343    }
 344  0 else if (cOp instanceof AStrongEqualCompOp) {
 345  0 op = factory.createIRI(Constants.STRONG_EQUAL);
 346    }
 347    else {
 348    //inequal, stringInequal, numericInequal
 349  0 if(isNumeric(te1) && isNumeric(te2)) {
 350  0 op = factory.createIRI(Constants.NUMERIC_INEQUAL);
 351  0 } else if (isString(te1) && isString(te2)){
 352  0 op = factory.createIRI(Constants.STRING_INEQUAL);
 353    } else {
 354  0 op = factory.createIRI(Constants.INEQUAL);
 355    }
 356    }
 357  0 return op;
 358    }
 359   
 360    //TODO more clever analysis (e.g. for variable)
 361  0 private boolean isNumeric(Term t) {
 362  0 if(t instanceof DataValue ) {
 363  0 String s = ((DataValue)t).getType().getIRI().toString();
 364  0 if( s.equals(WsmlDataType.WSML_DOUBLE)
 365    || s.equals(WsmlDataType.WSML_FLOAT)
 366    || s.equals(WsmlDataType.WSML_DECIMAL)
 367    || s.equals(WsmlDataType.WSML_INTEGER) ) {
 368  0 return true;
 369    } else {
 370  0 return false;
 371    }
 372    } else {
 373  0 return false;
 374    }
 375    }
 376   
 377  0 private boolean isString(Term t) {
 378  0 if(t instanceof DataValue ) {
 379  0 String s = ((DataValue)t).getType().getIRI().toString();
 380  0 if( s.equals(WsmlDataType.WSML_STRING)) {
 381  0 return true;
 382    } else {
 383  0 return false;
 384    }
 385    } else {
 386  0 return false;
 387    }
 388    }
 389    }