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