View Javadoc

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      public VisitorSerializeWSML(TopEntity nsC) {
51          visitor = new VisitorSerializeWSMLTerms(nsC);
52          visitor.setAtoms2ConstructedTerms(atoms2Rewrite);
53          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      public void visitAtom(Atom expr) {
62          boolean infix = false;
63          Term t = expr.getIdentifier();
64          t.accept(visitor);
65          String s = visitor.getSerializedObject();
66          String realIRI = ConstantTransformer.getInstance().findIri(s);
67          
68          //check for infix notion e.g. 1+2
69          if (realIRI != null && 
70                  ConstantTransformer.getInstance().isInfix(realIRI) &&
71                  expr.getArity()==2){
72              expr.getParameter(0).accept(visitor);
73              expr.getParameter(1).accept(visitor);
74              s = visitor.getSerializedObject() +
75                  " " + s + " " +
76                  visitor.getSerializedObject();
77          }
78          else {
79              //avoid +(1,2)
80              if (realIRI!= null) {
81                  s=visitor.resolveIRI(realIRI);
82              }
83              int nbParams = expr.getArity();
84              if (nbParams > 0) {
85                  s = s + "(";
86                  for (int i = 0; i < nbParams; i++) {
87                      expr.getParameter(i).accept(visitor);
88                      s = s + visitor.getSerializedObject();
89                      if (i + 1 < nbParams) {
90                          s = s + ",";
91                      }
92                  }
93                  s = s + ")";
94              }
95          }
96          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     public void visitNegationAsFailure(NegationAsFailure expr) {
105         expr.getOperand().accept(this);
106         if (expr.getOperand()instanceof AtomicExpression) {
107             stack.add("naf " + (String)stack.remove(stack.size() - 1));
108         }
109         else {
110             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     public void visitNegation(Negation expr) {
120         expr.getOperand().accept(this);
121         if (expr.getOperand()instanceof AtomicExpression) {
122             stack.add("neg " + (String)stack.remove(stack.size() - 1));
123         }
124         else {
125             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     public void visitConstraint(Constraint expr) {
135         expr.getOperand().accept(this);
136         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     public void visitConjunction(Conjunction expr) {
145         //check functionsymbols to rewrite
146         if (expr.getRightOperand() instanceof Atom){
147             Atom a = (Atom)expr.getRightOperand();
148             if (a.getArity()>0 && a.getParameter(0) instanceof TempVariable){
149                 List <Term> params = new LinkedList <Term>(a.listParameters());
150                 params.remove(0);
151                 Term key = a.getParameter(0);
152                 Term subst = new ConstructedTermImpl((IRI)a.getIdentifier(), params);
153                 atoms2Rewrite.put(a.getParameter(0),subst);
154                 expr.getLeftOperand().accept(this);
155                 return;
156             }
157         }
158         parentOperator = expr;
159         expr.getLeftOperand().accept(this);
160         expr.getRightOperand().accept(this);
161         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     public void visitEquivalence(Equivalence expr) {
170         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         parentOperator = expr;
177         expr.getLeftOperand().accept(this);
178         expr.getRightOperand().accept(this);
179         if (brackets) {
180             stack.add("(" + (String)stack.remove(stack.size() - 2) + "\nequivalent\n" +
181                       (String)stack.remove(stack.size() - 1) + ")");
182         }
183         else {
184             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     public void visitInverseImplication(InverseImplication expr) {
194         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         parentOperator = expr;
201         expr.getLeftOperand().accept(this);
202         expr.getRightOperand().accept(this);
203         if (brackets) {
204             stack.add("(" + (String)stack.remove(stack.size() - 2) + "\nimpliedBy\n" +
205                       (String)stack.remove(stack.size() - 1) + ")");
206         }
207         else {
208             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     public void visitImplication(Implication expr) {
218         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         parentOperator = expr;
225         expr.getLeftOperand().accept(this);
226         expr.getRightOperand().accept(this);
227         if (brackets) {
228             stack.add("(" + (String)stack.remove(stack.size() - 2) + "\nimplies\n" +
229                       (String)stack.remove(stack.size() - 1) + ")");
230         }
231         else {
232             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     public void visitLogicProgrammingRule(LogicProgrammingRule expr) {
242         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         parentOperator = expr;
249         expr.getLeftOperand().accept(this);
250         expr.getRightOperand().accept(this);
251         if (brackets) {
252             stack.add("(" + (String)stack.remove(stack.size() - 2) + "\n:-\n"
253                       + (String)stack.remove(stack.size() - 1) + ")");
254         }
255         else {
256             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     public void visitDisjunction(Disjunction expr) {
267         boolean bracket = parentOperator instanceof Conjunction;
268         parentOperator = expr;
269         expr.getLeftOperand().accept(this);
270         expr.getRightOperand().accept(this);
271         if (bracket) {
272             stack.add("(" + (String)stack.remove(stack.size() - 2) + "\n  or\n" + (String)stack.remove(stack.size() - 1) +
273                       ")");
274         }
275         else {
276             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     public void visitUniversalQuantification(UniversalQuantification expr) {
286         String res = helpQuantified(expr);
287         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     public void visitExistentialQuantification(ExistentialQuantification expr) {
296         String res = helpQuantified(expr);
297         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     public String getSerializedObject() {
307         Object o = stack.remove(0);
308         parentOperator = null;
309         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     private String helpQuantified(Quantified expr) {
320         String res = "";
321         Set s = expr.listVariables();
322         Iterator i = s.iterator();
323         if (s.size() > 1) {
324             res = res + "{";
325             while (i.hasNext()) {
326                 ((Term)i.next()).accept(visitor);
327                 res = res + visitor.getSerializedObject();
328                 if (i.hasNext()) {
329                     res = res + ",";
330                 }
331             }
332             res = res + "}";
333         }
334         else {
335             ((Term)i.next()).accept(visitor);
336             res = res + visitor.getSerializedObject();
337         }
338         expr.getOperand().accept(this);
339         return res;
340     }
341 
342     public void visitAttributeContraintMolecule(AttributeConstraintMolecule expr) {
343         expr.getLeftParameter().accept(visitor);
344         String id = visitor.getSerializedObject();
345         expr.getRightParameter().accept(visitor);
346         String value = visitor.getSerializedObject();
347         expr.getAttribute().accept(visitor);
348         String attributName = visitor.getSerializedObject();
349         stack.add(id+"["+attributName+" ofType "+ value+"]");
350     }
351 
352     public void visitAttributeInferenceMolecule(AttributeInferenceMolecule expr) {
353         expr.getLeftParameter().accept(visitor);
354         String id = visitor.getSerializedObject();
355         expr.getRightParameter().accept(visitor);
356         String value = visitor.getSerializedObject();
357         expr.getAttribute().accept(visitor);
358         String attributName = visitor.getSerializedObject();
359         stack.add(id+"["+attributName+" impliesType "+ value+"]");
360     }
361 
362     public void visitAttributeValueMolecule(AttributeValueMolecule expr) {
363         expr.getLeftParameter().accept(visitor);
364         String id = visitor.getSerializedObject();
365         expr.getRightParameter().accept(visitor);
366         String value = visitor.getSerializedObject();
367         expr.getAttribute().accept(visitor);
368         String attributName = visitor.getSerializedObject();
369         stack.add(id+"["+attributName+" hasValue "+ value+"]");
370     }
371 
372     public void visitCompoundMolecule(CompoundMolecule expr) {
373         Term termId = ((Molecule) expr.listOperands().iterator().next()).getLeftParameter(); 
374         termId.accept(visitor);
375         String str = visitor.getSerializedObject();
376         
377         List isA = new Vector();
378         String isAType = "";
379         Set attrNames = new HashSet();
380         Iterator i = expr.listOperands().iterator();
381         int n = 0;
382         while (i.hasNext()){
383             n++;
384             Molecule m = (Molecule)i.next();
385             m.getRightParameter().accept(visitor);
386             String right= visitor.getSerializedObject();
387             if (m instanceof AttributeMolecule){
388                 attrNames.add(((AttributeMolecule)m).getAttribute());
389             }
390             else if (m instanceof SubConceptMolecule){
391                 isA.add(right);
392                 isAType = " subConceptOf ";
393             }
394             else if (m instanceof MembershipMolecule){
395                 isA.add(right);
396                 isAType = " memberOf ";
397             }
398         }
399         if (attrNames.size()!=0) {
400             str += "["; 
401         }
402         i = attrNames.iterator();
403         int count = 0;
404         while(i.hasNext()){
405             Term t = (Term)i.next();
406             str += helpAttributes(expr.listAttributeValueMolecules(t),t, " hasValue ");
407             if (expr.listAttributeValueMolecules(t).size()!=0 &&
408                     (expr.listAttributeConstraintMolecules(t).size()!=0l ||
409                     expr.listAttributeInferenceMolecules(t).size()!=0)){
410                 str += ", ";
411             }
412             str += helpAttributes(expr.listAttributeConstraintMolecules(t),t, " ofType ");
413             if (expr.listAttributeConstraintMolecules(t).size()!=0 &&
414                     expr.listAttributeInferenceMolecules(t).size()!=0){
415                 str += ", ";
416             }
417             str += helpAttributes(expr.listAttributeInferenceMolecules(t),t, " impliesType ");
418             if (++count < attrNames.size()){
419                 str += ", ";
420             }
421         }
422         if (attrNames.size()!=0) {
423             str += "]"; 
424         }
425         String isAString="";
426         i = isA.iterator();
427         count = 0;
428         while(i.hasNext()){
429             isAString += (String)i.next();
430             if (++count<isA.size()){
431                 isAString += ",";
432             }
433         }
434         if (isA.size()>1){
435             isAString = "{" + isAString + "}";
436         }
437         stack.add(str + isAType + isAString);
438     }
439     
440     private String helpAttributes(List attributeMolecules, Term t, String operator){
441         if (attributeMolecules == null || attributeMolecules.size()==0){
442             return "";
443         }
444         t.accept(visitor);
445         String attributName = visitor.getSerializedObject();
446         String right = "";
447         int count = 0;
448         Iterator it = attributeMolecules.iterator();
449         while (it.hasNext()){
450             Molecule m = (Molecule)it.next();
451             m.getRightParameter().accept(visitor);
452             right += visitor.getSerializedObject();
453             if (++count < attributeMolecules.size()) {
454                 right += ", ";
455             }
456         }
457         if (count>1){
458             right = "{"+right+"}";
459         }
460         return attributName + operator + right;
461     }
462 
463     public void visitMemberShipMolecule(MembershipMolecule expr) {
464         expr.getLeftParameter().accept(visitor);
465         String id = visitor.getSerializedObject();
466         expr.getRightParameter().accept(visitor);
467         String type = visitor.getSerializedObject();
468         stack.add(id +" memberOf " + type);
469     }
470 
471     public void visitSubConceptMolecule(SubConceptMolecule expr) {
472         expr.getLeftParameter().accept(visitor);
473         String id = visitor.getSerializedObject();
474         expr.getRightParameter().accept(visitor);
475         String type = visitor.getSerializedObject();
476         stack.add(id +" subConceptOf " + type);
477     }
478 }