Clover coverage report - Maven Clover report
Coverage timestamp: Tue Sep 16 2008 01:16:37 EEST
file stats: LOC: 478   Methods: 22
NCLOC: 345   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
VisitorSerializeWSML.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.serializer.wsml;
 17   
 18   
 19    import java.util.*;
 20   
 21    import org.deri.wsmo4j.io.parser.wsml.*;
 22    import org.deri.wsmo4j.logicalexpression.*;
 23    import org.deri.wsmo4j.logicalexpression.terms.*;
 24    import org.omwg.logicalexpression.*;
 25    import org.omwg.logicalexpression.terms.*;
 26    import org.omwg.ontology.Parameter;
 27    import org.wsmo.common.*;
 28   
 29   
 30    /**
 31    * Concrete Visitor class. For each visited logical expression, a String is built.
 32    * @see org.deri.wsmo4j.io.serializer.xml.LogExprSerializerXML
 33    * @see org.deri.wsmo4j.logicalexpression.AbstractVisitor
 34    */
 35    public class VisitorSerializeWSML
 36    extends AbstractVisitor {
 37   
 38    private Vector stack;
 39   
 40    private Map atoms2Rewrite = new HashMap();
 41   
 42    private VisitorSerializeWSMLTerms visitor;
 43   
 44    private LogicalExpression parentOperator;
 45   
 46    /**
 47    * @param nsC TopEntity
 48    * @see org.deri.wsmo4j.io.serializer.wsml.LogExprSerializerWSML#LogExprSerializerWSML(TopEntity)
 49    */
 50  0 public VisitorSerializeWSML(TopEntity nsC) {
 51  0 visitor = new VisitorSerializeWSMLTerms(nsC);
 52  0 visitor.setAtoms2ConstructedTerms(atoms2Rewrite);
 53  0 stack = new Vector();
 54    }
 55   
 56    /**
 57    * Builds a String representing the Atom and adds it to a vector.
 58    * @param expr Atom to be serialized
 59    * @see org.deri.wsmo4j.logicalexpression.AbstractVisitor#visitAtom(Atom)
 60    */
 61  0 public void visitAtom(Atom expr) {
 62  0 boolean infix = false;
 63  0 Term t = expr.getIdentifier();
 64  0 t.accept(visitor);
 65  0 String s = visitor.getSerializedObject();
 66  0 String realIRI = ConstantTransformer.getInstance().findIri(s);
 67   
 68    //check for infix notion e.g. 1+2
 69  0 if (realIRI != null &&
 70    ConstantTransformer.getInstance().isInfix(realIRI) &&
 71    expr.getArity()==2){
 72  0 expr.getParameter(0).accept(visitor);
 73  0 expr.getParameter(1).accept(visitor);
 74  0 s = visitor.getSerializedObject() +
 75    " " + s + " " +
 76    visitor.getSerializedObject();
 77    }
 78    else {
 79    //avoid +(1,2)
 80  0 if (realIRI!= null) {
 81  0 s=visitor.resolveIRI(realIRI);
 82    }
 83  0 int nbParams = expr.getArity();
 84  0 if (nbParams > 0) {
 85  0 s = s + "(";
 86  0 for (int i = 0; i < nbParams; i++) {
 87  0 expr.getParameter(i).accept(visitor);
 88  0 s = s + visitor.getSerializedObject();
 89  0 if (i + 1 < nbParams) {
 90  0 s = s + ",";
 91    }
 92    }
 93  0 s = s + ")";
 94    }
 95    }
 96  0 stack.add(s);
 97    }
 98   
 99    /**
 100    * Builds a String representing the Unary expression and adds it to a vector.
 101    * @param expr Unary Expression to be serialized, with operator NAF
 102    * @see org.deri.wsmo4j.logicalexpression.AbstractVisitor#visitNegationAsFailure(NegationAsFailure)
 103    */
 104  0 public void visitNegationAsFailure(NegationAsFailure expr) {
 105  0 expr.getOperand().accept(this);
 106  0 if (expr.getOperand()instanceof AtomicExpression) {
 107  0 stack.add("naf " + (String)stack.remove(stack.size() - 1));
 108    }
 109    else {
 110  0 stack.add("naf (" + (String)stack.remove(stack.size() - 1) + ")");
 111    }
 112    }
 113   
 114    /**
 115    * Builds a String representing the Unary expression and adds it to a vector.
 116    * @param expr Unary Expression to be serialized, with operator NEG
 117    * @see org.deri.wsmo4j.logicalexpression.AbstractVisitor#visitNegation(Negation)
 118    */
 119  0 public void visitNegation(Negation expr) {
 120  0 expr.getOperand().accept(this);
 121  0 if (expr.getOperand()instanceof AtomicExpression) {
 122  0 stack.add("neg " + (String)stack.remove(stack.size() - 1));
 123    }
 124    else {
 125  0 stack.add("neg (" + (String)stack.remove(stack.size() - 1) + ")");
 126    }
 127    }
 128   
 129    /**
 130    * Builds a String representing the Unary expression and adds it to a vector.
 131    * @param expr Unary Expression to be serialized, with operator CONSTRAINT
 132    * @see org.deri.wsmo4j.logicalexpression.AbstractVisitor#visitConstraint(Constraint)
 133    */
 134  0 public void visitConstraint(Constraint expr) {
 135  0 expr.getOperand().accept(this);
 136  0 stack.add("!- " + (String)stack.remove(stack.size() - 1));
 137    }
 138   
 139    /**
 140    * Builds a String representing the Binary expression and adds it to a vector.
 141    * @param expr Binary Expression to be serialized, with operator AND
 142    * @see org.deri.wsmo4j.logicalexpression.AbstractVisitor#visitConjunction(Conjunction)
 143    */
 144  0 public void visitConjunction(Conjunction expr) {
 145    //check functionsymbols to rewrite
 146  0 if (expr.getRightOperand() instanceof Atom){
 147  0 Atom a = (Atom)expr.getRightOperand();
 148  0 if (a.getArity()>0 && a.getParameter(0) instanceof TempVariable){
 149  0 List <Term> params = new LinkedList <Term>(a.listParameters());
 150  0 params.remove(0);
 151  0 Term key = a.getParameter(0);
 152  0 Term subst = new ConstructedTermImpl((IRI)a.getIdentifier(), params);
 153  0 atoms2Rewrite.put(a.getParameter(0),subst);
 154  0 expr.getLeftOperand().accept(this);
 155  0 return;
 156    }
 157    }
 158  0 parentOperator = expr;
 159  0 expr.getLeftOperand().accept(this);
 160  0 expr.getRightOperand().accept(this);
 161  0 stack.add((String)stack.remove(stack.size() - 2) + "\n and " + (String)stack.remove(stack.size() - 1));
 162    }
 163   
 164    /**
 165    * Builds a String representing the Binary expression and adds it to a vector.
 166    * @param expr Binary Expression to be serialized, with operator EQUIVALENT
 167    * @see org.deri.wsmo4j.logicalexpression.AbstractVisitor#visitEquivalence(Equivalence)
 168    */
 169  0 public void visitEquivalence(Equivalence expr) {
 170  0 boolean brackets = (parentOperator instanceof Conjunction
 171    || parentOperator instanceof Disjunction
 172    || parentOperator instanceof InverseImplication
 173    || parentOperator instanceof Implication
 174    || parentOperator instanceof Equivalence
 175    || parentOperator instanceof LogicProgrammingRule);
 176  0 parentOperator = expr;
 177  0 expr.getLeftOperand().accept(this);
 178  0 expr.getRightOperand().accept(this);
 179  0 if (brackets) {
 180  0 stack.add("(" + (String)stack.remove(stack.size() - 2) + "\nequivalent\n" +
 181    (String)stack.remove(stack.size() - 1) + ")");
 182    }
 183    else {
 184  0 stack.add((String)stack.remove(stack.size() - 2) + "\nequivalent\n" + (String)stack.remove(stack.size() - 1));
 185    }
 186    }
 187   
 188    /**
 189    * Builds a String representing the Binary expression and adds it to a vector.
 190    * @param expr Binary Expression to be serialized, with operator IMPLIEDBY
 191    * @see org.deri.wsmo4j.logicalexpression.AbstractVisitor#visitInverseImplication(InverseImplication)
 192    */
 193  0 public void visitInverseImplication(InverseImplication expr) {
 194  0 boolean brackets = (parentOperator instanceof Conjunction
 195    || parentOperator instanceof Disjunction
 196    || parentOperator instanceof InverseImplication
 197    || parentOperator instanceof Implication
 198    || parentOperator instanceof Equivalence
 199    || parentOperator instanceof LogicProgrammingRule);
 200  0 parentOperator = expr;
 201  0 expr.getLeftOperand().accept(this);
 202  0 expr.getRightOperand().accept(this);
 203  0 if (brackets) {
 204  0 stack.add("(" + (String)stack.remove(stack.size() - 2) + "\nimpliedBy\n" +
 205    (String)stack.remove(stack.size() - 1) + ")");
 206    }
 207    else {
 208  0 stack.add((String)stack.remove(stack.size() - 2) + "\nimpliedBy\n" + (String)stack.remove(stack.size() - 1));
 209    }
 210    }
 211   
 212    /**
 213    * Builds a String representing the Binary expression and adds it to a vector.
 214    * @param expr Binary Expression to be serialized, with operator IMPLIES
 215    * @see org.deri.wsmo4j.logicalexpression.AbstractVisitor#visitImplication(Implication)
 216    */
 217  0 public void visitImplication(Implication expr) {
 218  0 boolean brackets = (parentOperator instanceof Conjunction
 219    || parentOperator instanceof Disjunction
 220    || parentOperator instanceof InverseImplication
 221    || parentOperator instanceof Implication
 222    || parentOperator instanceof Equivalence
 223    || parentOperator instanceof LogicProgrammingRule);
 224  0 parentOperator = expr;
 225  0 expr.getLeftOperand().accept(this);
 226  0 expr.getRightOperand().accept(this);
 227  0 if (brackets) {
 228  0 stack.add("(" + (String)stack.remove(stack.size() - 2) + "\nimplies\n" +
 229    (String)stack.remove(stack.size() - 1) + ")");
 230    }
 231    else {
 232  0 stack.add((String)stack.remove(stack.size() - 2) + "\nimplies\n" + (String)stack.remove(stack.size() - 1));
 233    }
 234    }
 235   
 236    /**
 237    * Builds a String representing the Binary expression and adds it to a vector.
 238    * @param expr Binary Expression to be serialized, with operator IMPLIEDBYLP
 239    * @see org.deri.wsmo4j.logicalexpression.AbstractVisitor#visitLogicProgrammingRule(LogicProgrammingRule)
 240    */
 241  0 public void visitLogicProgrammingRule(LogicProgrammingRule expr) {
 242  0 boolean brackets = (parentOperator instanceof Conjunction
 243    || parentOperator instanceof Disjunction
 244    || parentOperator instanceof InverseImplication
 245    || parentOperator instanceof Implication
 246    || parentOperator instanceof Equivalence
 247    || parentOperator instanceof LogicProgrammingRule);
 248  0 parentOperator = expr;
 249  0 expr.getLeftOperand().accept(this);
 250  0 expr.getRightOperand().accept(this);
 251  0 if (brackets) {
 252  0 stack.add("(" + (String)stack.remove(stack.size() - 2) + "\n:-\n"
 253    + (String)stack.remove(stack.size() - 1) + ")");
 254    }
 255    else {
 256  0 stack.add((String)stack.remove(stack.size() - 2) + "\n:-\n"
 257    + (String)stack.remove(stack.size() - 1));
 258    }
 259    }
 260   
 261    /**
 262    * Builds a String representing the Binary expression and adds it to a vector.
 263    * @param expr Binary Expression to be serialized, with operator OR
 264    * @see org.deri.wsmo4j.logicalexpression.AbstractVisitor#visitDisjunction(Disjunction)
 265    */
 266  0 public void visitDisjunction(Disjunction expr) {
 267  0 boolean bracket = parentOperator instanceof Conjunction;
 268  0 parentOperator = expr;
 269  0 expr.getLeftOperand().accept(this);
 270  0 expr.getRightOperand().accept(this);
 271  0 if (bracket) {
 272  0 stack.add("(" + (String)stack.remove(stack.size() - 2) + "\n or\n" + (String)stack.remove(stack.size() - 1) +
 273    ")");
 274    }
 275    else {
 276  0 stack.add((String)stack.remove(stack.size() - 2) + "\n or\n" + (String)stack.remove(stack.size() - 1));
 277    }
 278    }
 279   
 280    /**
 281    * Builds a String representing the Quantified expression and adds it to a vector.
 282    * @param expr Quantified Expression to be serialized, with operator FORALL
 283    * @see org.deri.wsmo4j.logicalexpression.AbstractVisitor#visitUniversalQuantification(UniversalQuantification)
 284    */
 285  0 public void visitUniversalQuantification(UniversalQuantification expr) {
 286  0 String res = helpQuantified(expr);
 287  0 stack.add("forall " + res + "\n ( " + (String)stack.remove(stack.size() - 1) + " )");
 288    }
 289   
 290    /**
 291    * Builds a String representing the Quantified expression and adds it to a vector.
 292    * @param expr Quantified Expression to be serialized, with operator EXISTS
 293    * @see org.deri.wsmo4j.logicalexpression.AbstractVisitor#visitExistentialQuantification(ExistentialQuantification)
 294    */
 295  0 public void visitExistentialQuantification(ExistentialQuantification expr) {
 296  0 String res = helpQuantified(expr);
 297  0 stack.add("exists " + res + "\n ( " + (String)stack.remove(stack.size() - 1) + " )");
 298    }
 299   
 300    /**
 301    * All serialized elements are added to a vector. This method removes the
 302    * first serialized object from this vector and shifts any subsequent
 303    * objects to the left (subtracts one from their indices).
 304    * @return the serialized String object that is the first element in this vector
 305    */
 306  0 public String getSerializedObject() {
 307  0 Object o = stack.remove(0);
 308  0 parentOperator = null;
 309  0 return (String)o;
 310    }
 311   
 312   
 313   
 314    /**
 315    * Builds a String representing the Quantified Expression
 316    * @param expr Quantified Expression to be serialized, with operator EXISTS
 317    * @return String representing serialized Quantified Expression
 318    */
 319  0 private String helpQuantified(Quantified expr) {
 320  0 String res = "";
 321  0 Set s = expr.listVariables();
 322  0 Iterator i = s.iterator();
 323  0 if (s.size() > 1) {
 324  0 res = res + "{";
 325  0 while (i.hasNext()) {
 326  0 ((Term)i.next()).accept(visitor);
 327  0 res = res + visitor.getSerializedObject();
 328  0 if (i.hasNext()) {
 329  0 res = res + ",";
 330    }
 331    }
 332  0 res = res + "}";
 333    }
 334    else {
 335  0 ((Term)i.next()).accept(visitor);
 336  0 res = res + visitor.getSerializedObject();
 337    }
 338  0 expr.getOperand().accept(this);
 339  0 return res;
 340    }
 341   
 342  0 public void visitAttributeContraintMolecule(AttributeConstraintMolecule expr) {
 343  0 expr.getLeftParameter().accept(visitor);
 344  0 String id = visitor.getSerializedObject();
 345  0 expr.getRightParameter().accept(visitor);
 346  0 String value = visitor.getSerializedObject();
 347  0 expr.getAttribute().accept(visitor);
 348  0 String attributName = visitor.getSerializedObject();
 349  0 stack.add(id+"["+attributName+" ofType "+ value+"]");
 350    }
 351   
 352  0 public void visitAttributeInferenceMolecule(AttributeInferenceMolecule expr) {
 353  0 expr.getLeftParameter().accept(visitor);
 354  0 String id = visitor.getSerializedObject();
 355  0 expr.getRightParameter().accept(visitor);
 356  0 String value = visitor.getSerializedObject();
 357  0 expr.getAttribute().accept(visitor);
 358  0 String attributName = visitor.getSerializedObject();
 359  0 stack.add(id+"["+attributName+" impliesType "+ value+"]");
 360    }
 361   
 362  0 public void visitAttributeValueMolecule(AttributeValueMolecule expr) {
 363  0 expr.getLeftParameter().accept(visitor);
 364  0 String id = visitor.getSerializedObject();
 365  0 expr.getRightParameter().accept(visitor);
 366  0 String value = visitor.getSerializedObject();
 367  0 expr.getAttribute().accept(visitor);
 368  0 String attributName = visitor.getSerializedObject();
 369  0 stack.add(id+"["+attributName+" hasValue "+ value+"]");
 370    }
 371   
 372  0 public void visitCompoundMolecule(CompoundMolecule expr) {
 373  0 Term termId = ((Molecule) expr.listOperands().iterator().next()).getLeftParameter();
 374  0 termId.accept(visitor);
 375  0 String str = visitor.getSerializedObject();
 376   
 377  0 List isA = new Vector();
 378  0 String isAType = "";
 379  0 Set attrNames = new HashSet();
 380  0 Iterator i = expr.listOperands().iterator();
 381  0 int n = 0;
 382  0 while (i.hasNext()){
 383  0 n++;
 384  0 Molecule m = (Molecule)i.next();
 385  0 m.getRightParameter().accept(visitor);
 386  0 String right= visitor.getSerializedObject();
 387  0 if (m instanceof AttributeMolecule){
 388  0 attrNames.add(((AttributeMolecule)m).getAttribute());
 389    }
 390  0 else if (m instanceof SubConceptMolecule){
 391  0 isA.add(right);
 392  0 isAType = " subConceptOf ";
 393    }
 394  0 else if (m instanceof MembershipMolecule){
 395  0 isA.add(right);
 396  0 isAType = " memberOf ";
 397    }
 398    }
 399  0 if (attrNames.size()!=0) {
 400  0 str += "[";
 401    }
 402  0 i = attrNames.iterator();
 403  0 int count = 0;
 404  0 while(i.hasNext()){
 405  0 Term t = (Term)i.next();
 406  0 str += helpAttributes(expr.listAttributeValueMolecules(t),t, " hasValue ");
 407  0 if (expr.listAttributeValueMolecules(t).size()!=0 &&
 408    (expr.listAttributeConstraintMolecules(t).size()!=0l ||
 409    expr.listAttributeInferenceMolecules(t).size()!=0)){
 410  0 str += ", ";
 411    }
 412  0 str += helpAttributes(expr.listAttributeConstraintMolecules(t),t, " ofType ");
 413  0 if (expr.listAttributeConstraintMolecules(t).size()!=0 &&
 414    expr.listAttributeInferenceMolecules(t).size()!=0){
 415  0 str += ", ";
 416    }
 417  0 str += helpAttributes(expr.listAttributeInferenceMolecules(t),t, " impliesType ");
 418  0 if (++count < attrNames.size()){
 419  0 str += ", ";
 420    }
 421    }
 422  0 if (attrNames.size()!=0) {
 423  0 str += "]";
 424    }
 425  0 String isAString="";
 426  0 i = isA.iterator();
 427  0 count = 0;
 428  0 while(i.hasNext()){
 429  0 isAString += (String)i.next();
 430  0 if (++count<isA.size()){
 431  0 isAString += ",";
 432    }
 433    }
 434  0 if (isA.size()>1){
 435  0 isAString = "{" + isAString + "}";
 436    }
 437  0 stack.add(str + isAType + isAString);
 438    }
 439   
 440  0 private String helpAttributes(List attributeMolecules, Term t, String operator){
 441  0 if (attributeMolecules == null || attributeMolecules.size()==0){
 442  0 return "";
 443    }
 444  0 t.accept(visitor);
 445  0 String attributName = visitor.getSerializedObject();
 446  0 String right = "";
 447  0 int count = 0;
 448  0 Iterator it = attributeMolecules.iterator();
 449  0 while (it.hasNext()){
 450  0 Molecule m = (Molecule)it.next();
 451  0 m.getRightParameter().accept(visitor);
 452  0 right += visitor.getSerializedObject();
 453  0 if (++count < attributeMolecules.size()) {
 454  0 right += ", ";
 455    }
 456    }
 457  0 if (count>1){
 458  0 right = "{"+right+"}";
 459    }
 460  0 return attributName + operator + right;
 461    }
 462   
 463  0 public void visitMemberShipMolecule(MembershipMolecule expr) {
 464  0 expr.getLeftParameter().accept(visitor);
 465  0 String id = visitor.getSerializedObject();
 466  0 expr.getRightParameter().accept(visitor);
 467  0 String type = visitor.getSerializedObject();
 468  0 stack.add(id +" memberOf " + type);
 469    }
 470   
 471  0 public void visitSubConceptMolecule(SubConceptMolecule expr) {
 472  0 expr.getLeftParameter().accept(visitor);
 473  0 String id = visitor.getSerializedObject();
 474  0 expr.getRightParameter().accept(visitor);
 475  0 String type = visitor.getSerializedObject();
 476  0 stack.add(id +" subConceptOf " + type);
 477    }
 478    }