1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.deri.wsmo4j.io.serializer.xml;
17
18
19 import java.util.*;
20
21 import org.deri.wsmo4j.logicalexpression.*;
22 import org.w3c.dom.*;
23
24 import org.omwg.logicalexpression.*;
25 import org.omwg.logicalexpression.terms.*;
26
27
28
29
30
31
32
33
34
35 public class VisitorSerializeXML
36 extends AbstractVisitor {
37
38 private Vector stack;
39
40 private VisitorSerializeXMLTerms visitor;
41
42 private Document doc;
43
44
45
46
47
48 public VisitorSerializeXML(Document doc) {
49 this.doc = doc;
50 visitor = new VisitorSerializeXMLTerms(doc);
51 stack = new Vector();
52 }
53
54
55
56
57
58
59
60
61 public void visitAtom(Atom expr) {
62 int nbParams = expr.getArity();
63 visitor.setName("atom");
64 expr.getIdentifier().accept(visitor);
65 Node newExpr = (Node)visitor.getSerializedObject();
66 if (nbParams > 0) {
67 for (int i = 0; i < nbParams; i++) {
68 visitor.setName("arg");
69 expr.getParameter(i).accept(visitor);
70 newExpr.appendChild((Node)visitor.getSerializedObject());
71 }
72 }
73 stack.add(newExpr);
74 }
75
76 public void visitCompoundMolecule(CompoundMolecule expr) {
77 Element molecule = doc.createElement("molecule");
78 visitor.setName("term");
79
80 ((Molecule) expr.listOperands().iterator().next()).getLeftParameter().accept(visitor);
81 molecule.appendChild((Node)visitor.getSerializedObject());
82 List isa = expr.listMemberShipMolecules();
83 int isaType = 0;
84 if (isa != null && isa.size() !=0) {
85 isaType = 1;
86 }
87 else {
88 isa = expr.listSubConceptMolecules();
89 if (isa != null && isa.size() !=0) {
90 isaType = 2;
91 }
92 }
93 if (isaType != 0) {
94 molecule.appendChild(helpSerializeIsa(isa, isaType, doc));
95 }
96
97
98 Set attrNames = new HashSet();
99 Iterator i = expr.listOperands().iterator();
100 while (i.hasNext()){
101 Object o = i.next();
102 if (o instanceof AttributeMolecule){
103 attrNames.add(((AttributeMolecule)o).getAttribute());
104 }
105 }
106
107 Iterator attrNamesIt = attrNames.iterator();
108 while (attrNamesIt.hasNext()){
109 Term t = (Term)attrNamesIt.next();
110 List l = expr.listAttributeConstraintMolecules(t);
111 if (l.size()>0){
112 molecule.appendChild(helpSerializeAttrSpecification(l, doc));
113 }
114 l = expr.listAttributeInferenceMolecules(t);
115 if (l.size()>0){
116 molecule.appendChild(helpSerializeAttrSpecification(l, doc));
117 }
118 l = expr.listAttributeValueMolecules(t);
119 if (l.size()>0){
120 molecule.appendChild(helpSerializeAttrSpecification(l, doc));
121 }
122 }
123 stack.add(molecule);
124 }
125
126
127
128
129
130
131
132
133 public void visitNegationAsFailure(NegationAsFailure expr) {
134 Node newExpr = doc.createElement("naf");
135 expr.getOperand().accept(this);
136 newExpr.appendChild((Node)stack.remove(stack.size() - 1));
137 stack.add(newExpr);
138 }
139
140
141
142
143
144
145
146
147 public void visitNegation(Negation expr) {
148 Node newExpr = doc.createElement("neg");
149 expr.getOperand().accept(this);
150 newExpr.appendChild((Node)stack.remove(stack.size() - 1));
151 stack.add(newExpr);
152 }
153
154
155
156
157
158
159
160
161 public void visitConstraint(Constraint expr) {
162 Node newExpr = doc.createElement("constraint");
163 expr.getOperand().accept(this);
164 newExpr.appendChild((Node)stack.remove(stack.size() - 1));
165 stack.add(newExpr);
166 }
167
168
169
170
171
172
173
174
175 public void visitConjunction(Conjunction expr) {
176 Node newExpr = doc.createElement("and");
177 finishBinary(expr, newExpr);
178 }
179
180
181
182
183
184
185
186
187 public void visitEquivalence(Equivalence expr) {
188 Node newExpr = doc.createElement("equivalent");
189 finishBinary(expr, newExpr);
190 }
191
192
193
194
195
196
197
198
199 public void visitInverseImplication(InverseImplication expr) {
200 Node newExpr = doc.createElement("impliedBy");
201 finishBinary(expr, newExpr);
202 }
203
204
205
206
207
208
209
210
211 public void visitImplication(Implication expr) {
212 Node newExpr = doc.createElement("implies");
213 finishBinary(expr, newExpr);
214 }
215
216
217
218
219
220
221
222
223 public void visitLogicProgrammingRule(LogicProgrammingRule expr) {
224 Node newExpr = doc.createElement("impliedByLP");
225 finishBinary(expr, newExpr);
226 }
227
228
229
230
231
232
233
234
235 public void visitDisjunction(Disjunction expr) {
236 Node newExpr = doc.createElement("or");
237 finishBinary(expr, newExpr);
238 }
239
240
241
242
243
244
245
246
247
248 private void finishBinary(Binary expr, Node newExpr) {
249 expr.getLeftOperand().accept(this);
250 expr.getRightOperand().accept(this);
251 newExpr.appendChild((Node)stack.remove(stack.size() - 2));
252 newExpr.appendChild((Node)stack.remove(stack.size() - 1));
253 stack.add(newExpr);
254 }
255
256
257
258
259
260
261
262
263 public void visitUniversalQuantification(UniversalQuantification expr) {
264 Node newExpr = doc.createElement("forall");
265 addVariables(newExpr, expr);
266 expr.getOperand().accept(this);
267 newExpr.appendChild((Node)stack.remove(stack.size() - 1));
268 stack.add(newExpr);
269 }
270
271
272
273
274
275
276
277
278 public void visitExistentialQuantification(ExistentialQuantification expr) {
279 Node newExpr = doc.createElement("exists");
280 addVariables(newExpr, expr);
281 expr.getOperand().accept(this);
282 newExpr.appendChild((Node)stack.remove(stack.size() - 1));
283 stack.add(newExpr);
284 }
285
286
287
288
289
290
291
292 public Object getSerializedObject() {
293 return stack.remove(0);
294 }
295
296
297
298
299
300
301
302
303
304
305 private Node helpSerializeIsa(List isaSet, int isaType, Document doc) {
306 Element isa = doc.createElement("isa");
307 Iterator it = isaSet.iterator();
308 switch (isaType) {
309 case 1:
310 isa.setAttribute("type", "memberOf");
311 break;
312 case 2:
313 isa.setAttribute("type", "subConceptOf");
314 break;
315 }
316 visitor.setName("term");
317 while (it.hasNext()) {
318 ((Molecule)it.next()).getRightParameter().accept(visitor);
319 isa.appendChild((Node)visitor.getSerializedObject());
320 }
321 return isa;
322 }
323
324
325
326 private Node helpSerializeAttrSpecification(List attrMolecules, Document doc) {
327 Element attrSpec = null;
328 AttributeMolecule am = (AttributeMolecule)attrMolecules.iterator().next();
329 if (am instanceof AttributeConstraintMolecule) {
330 attrSpec = doc.createElement("attributeDefinition");
331 attrSpec.setAttribute("type", "constraining");
332 }
333 else if (am instanceof AttributeInferenceMolecule) {
334 attrSpec = doc.createElement("attributeDefinition");
335 attrSpec.setAttribute("type", "inferring");
336 }
337 else if (am instanceof AttributeValueMolecule) {
338 attrSpec = doc.createElement("attributeValue");
339 }
340 visitor.setName("name");
341 am.getAttribute().accept(visitor);
342 attrSpec.appendChild((Node)visitor.getSerializedObject());
343 Iterator it = attrMolecules.iterator();
344 if (am instanceof AttributeConstraintMolecule || am instanceof AttributeInferenceMolecule) {
345 visitor.setName("type");
346 while (it.hasNext()) {
347 ((AttributeMolecule)it.next()).getRightParameter().accept(visitor);
348 attrSpec.appendChild((Node)visitor.getSerializedObject());
349 }
350 }
351 else if (am instanceof AttributeValueMolecule) {
352 visitor.setName("value");
353 while (it.hasNext()) {
354 ((AttributeMolecule)it.next()).getRightParameter().accept(visitor);
355 attrSpec.appendChild((Node)visitor.getSerializedObject());
356 }
357 }
358 return attrSpec;
359 }
360
361
362
363
364
365
366
367
368
369
370
371 private void addVariables(Node q, LogicalExpression log) {
372 Set s = ((Quantified)log).listVariables();
373 Iterator i = s.iterator();
374 visitor.setName("variable");
375 while (i.hasNext()) {
376 ((Term)i.next()).accept(visitor);
377 q.appendChild((Node)visitor.getSerializedObject());
378 }
379 }
380
381 public void visitSubConceptMolecule(SubConceptMolecule expr) {
382 visitConceptMolecule(expr,2);
383 }
384
385 private void visitConceptMolecule(Molecule expr, int type){
386 Element molecule = doc.createElement("molecule");
387 visitor.setName("term");
388 expr.getLeftParameter().accept(visitor);
389 molecule.appendChild((Node)visitor.getSerializedObject());
390 List isa = new Vector();
391 isa.add(expr);
392 molecule.appendChild(helpSerializeIsa(isa, type, doc));
393 stack.add(molecule);
394 }
395
396
397
398
399 public void visitMemberShipMolecule(MembershipMolecule expr) {
400 visitConceptMolecule(expr,1);
401 }
402
403
404
405
406 public void visitAttributeValueMolecule(AttributeValueMolecule expr) {
407 visitAttributeMolecule(expr);
408 }
409
410 private void visitAttributeMolecule(AttributeMolecule expr){
411 Element molecule = doc.createElement("molecule");
412 visitor.setName("term");
413 expr.getLeftParameter().accept(visitor);
414 molecule.appendChild((Node)visitor.getSerializedObject());
415
416 List l = new Vector();
417 l.add(expr);
418 molecule.appendChild(helpSerializeAttrSpecification(l, doc));
419 stack.add(molecule);
420 }
421
422
423
424
425 public void visitAttributeContraintMolecule(AttributeConstraintMolecule expr) {
426 visitAttributeMolecule(expr);
427 }
428
429
430
431
432 public void visitAttributeInferenceMolecule(AttributeInferenceMolecule expr) {
433 visitAttributeMolecule(expr);
434 }
435 }