1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| package org.deri.wsmo4j.io.parser.xml; |
17 |
| |
18 |
| |
19 |
| import java.math.*; |
20 |
| import java.util.*; |
21 |
| |
22 |
| import org.deri.wsmo4j.logicalexpression.*; |
23 |
| import org.omwg.logicalexpression.*; |
24 |
| import org.omwg.logicalexpression.terms.*; |
25 |
| import org.omwg.ontology.Variable; |
26 |
| import org.w3c.dom.*; |
27 |
| import org.wsmo.common.*; |
28 |
| import org.wsmo.factory.*; |
29 |
| import org.wsmo.wsml.*; |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| public class XMLExprParser { |
37 |
| |
38 |
| DataFactory df; |
39 |
| LogicalExpressionFactory leFactory; |
40 |
| WsmoFactory factory; |
41 |
| |
42 |
0
| public XMLExprParser(WsmoFactory factory,
|
43 |
| LogicalExpressionFactory leFactory, DataFactory df){ |
44 |
0
| this.df=df;
|
45 |
0
| this.leFactory=leFactory;
|
46 |
0
| this.factory=factory;
|
47 |
| } |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
0
| public LogicalExpression evaluateXML(Element exprNode)
|
59 |
| throws ParserException { |
60 |
0
| String exprName = exprNode.getNodeName();
|
61 |
0
| LogicalExpression logExpr = null;
|
62 |
| |
63 |
| |
64 |
0
| List exprs = null;
|
65 |
0
| if (exprName.equals("and")) {
|
66 |
0
| exprs = childElements(exprNode.getChildNodes());
|
67 |
0
| logExpr = evaluateXMLAnd(exprs);
|
68 |
| } |
69 |
0
| else if (exprName.equals("or")) {
|
70 |
0
| exprs = childElements(exprNode.getChildNodes());
|
71 |
0
| logExpr = evaluateXMLOr(exprs);
|
72 |
| } |
73 |
0
| else if (exprName.equals("forall")) {
|
74 |
0
| exprs = childElements(exprNode.getChildNodes());
|
75 |
0
| logExpr = evaluateXMLForAll(exprs);
|
76 |
| } |
77 |
0
| else if (exprName.equals("exists")) {
|
78 |
0
| exprs = childElements(exprNode.getChildNodes());
|
79 |
0
| logExpr = evaluateXMLExists(exprs);
|
80 |
| } |
81 |
0
| else if (exprName.equals("equivalent")) {
|
82 |
0
| exprs = childElements(exprNode.getChildNodes());
|
83 |
0
| logExpr = evaluateXMLEquals(exprs);
|
84 |
| } |
85 |
0
| else if (exprName.equals("impliedBy")) {
|
86 |
0
| exprs = childElements(exprNode.getChildNodes());
|
87 |
0
| logExpr = evaluateXMLImpliedBy(exprs);
|
88 |
| } |
89 |
0
| else if (exprName.equals("implies")) {
|
90 |
0
| exprs = childElements(exprNode.getChildNodes());
|
91 |
0
| logExpr = evaluateXMLImplies(exprs);
|
92 |
| } |
93 |
0
| else if (exprName.equals("impliedByLP")) {
|
94 |
0
| exprs = childElements(exprNode.getChildNodes());
|
95 |
0
| logExpr = evaluateXMLImpliesLP(exprs);
|
96 |
| } |
97 |
0
| else if (exprName.equals("naf")) {
|
98 |
0
| exprs = childElements(exprNode.getChildNodes());
|
99 |
0
| logExpr = evaluateXMLNaf(exprs);
|
100 |
| } |
101 |
0
| else if (exprName.equals("neg")) {
|
102 |
0
| exprs = childElements(exprNode.getChildNodes());
|
103 |
0
| logExpr = evaluateXMLNeg(exprs);
|
104 |
| } |
105 |
0
| else if (exprName.equals("constraint")) {
|
106 |
0
| exprs = childElements(exprNode.getChildNodes());
|
107 |
0
| logExpr = evaluateXMLConstraing(exprs);
|
108 |
| } |
109 |
0
| else if (exprName.equals("molecule")) {
|
110 |
0
| NodeList cptE = exprNode.getElementsByTagName("isa");
|
111 |
0
| NodeList attrValue = exprNode.getElementsByTagName("attributeValue");
|
112 |
0
| NodeList attrDef = exprNode.getElementsByTagName("attributeDefinition");
|
113 |
| |
114 |
0
| Term t = evaluateXMLTerm((Element)exprNode.getElementsByTagName("term").item(0));
|
115 |
0
| List <Molecule> molecules = new Vector <Molecule>();
|
116 |
| |
117 |
0
| if (attrValue.getLength() > 0 || attrDef.getLength() > 0) {
|
118 |
0
| if (attrValue.getLength() > 0) {
|
119 |
0
| molecules.addAll(evaluateXMLAttrValue(attrValue, t));
|
120 |
| } |
121 |
0
| if (attrDef.getLength() > 0) {
|
122 |
0
| molecules.addAll(evaluateXMLAttrDef(attrDef, t));
|
123 |
| } |
124 |
| } |
125 |
0
| if (cptE.getLength() > 0) {
|
126 |
0
| Node isa = cptE.item(0);
|
127 |
0
| String type = isa.getAttributes().getNamedItem("type").getNodeValue();
|
128 |
0
| if (type.equals("memberOf")) {
|
129 |
0
| Iterator types = evaluateXMLIsa(cptE).iterator();
|
130 |
0
| while (types.hasNext()){
|
131 |
0
| molecules.add(leFactory.createMemberShipMolecule(
|
132 |
| t,(Term)types.next())); |
133 |
| } |
134 |
| } |
135 |
0
| else if (type.equals("subConceptOf")) {
|
136 |
0
| Iterator types = evaluateXMLIsa(cptE).iterator();
|
137 |
0
| while (types.hasNext()){
|
138 |
0
| molecules.add(leFactory.createSubConceptMolecule(
|
139 |
| t,(Term)types.next())); |
140 |
| } |
141 |
| } |
142 |
| } |
143 |
0
| if (molecules.size()==0){
|
144 |
0
| throw new ParserException("XML Parse Error: Molecule element without content detected! ("+t+")", null);
|
145 |
| } |
146 |
0
| else if (molecules.size()==1){
|
147 |
0
| logExpr = molecules.remove(0);
|
148 |
| } |
149 |
| else { |
150 |
0
| logExpr = leFactory.createCompoundMolecule(molecules);
|
151 |
| } |
152 |
| } |
153 |
0
| else if (exprName.equals("atom")) {
|
154 |
0
| logExpr = evaluateXMLConstructed(exprNode, factory.createIRI(exprNode.getAttribute("name")));
|
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| } |
160 |
| else { |
161 |
0
| throw new ParserException("XML Parse Error... : " + exprName, null);
|
162 |
| } |
163 |
0
| return logExpr;
|
164 |
| } |
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
| |
171 |
| |
172 |
0
| private Term evaluateXMLTerm(Element exprNode)
|
173 |
| throws ParserException { |
174 |
0
| String exprName = exprNode.getNodeName();
|
175 |
0
| Term t = null;
|
176 |
0
| if (exprName.equals("variable")) {
|
177 |
0
| try {
|
178 |
0
| t = leFactory.createVariable(exprNode.getAttribute("name").substring(1));
|
179 |
| } |
180 |
| catch (IllegalArgumentException e) { |
181 |
0
| throw new ParserException("XML Parse Error: Zero-length variable not allowed.", e);
|
182 |
| } |
183 |
| |
184 |
| } |
185 |
0
| else if (exprName.equals("term") ||
|
186 |
| exprName.equals("name") || |
187 |
| exprName.equals("arg") || |
188 |
| exprName.equals("value")) { |
189 |
0
| String iriStr = exprNode.getAttribute("name");
|
190 |
0
| String notation = ConstantTransformer.getInstance().findNotation(iriStr);
|
191 |
0
| if (notation != null && notation.equals("_integer")) {
|
192 |
0
| t = df.createWsmlInteger(new BigInteger(exprNode.getFirstChild().getNodeValue()));
|
193 |
| } |
194 |
0
| else if (notation != null && notation.equals("_decimal")) {
|
195 |
0
| t = df.createWsmlDecimal(new BigDecimal(exprNode.getFirstChild().getNodeValue()));
|
196 |
| } |
197 |
0
| else if (notation != null && notation.equals("_string")) {
|
198 |
0
| t = df.createWsmlString(exprNode.getFirstChild().getNodeValue());
|
199 |
| } |
200 |
0
| else if (iriStr.equals(Constants.ANONYMOUS_ID)) {
|
201 |
0
| t = factory.createAnonymousID();
|
202 |
| } |
203 |
0
| else if (iriStr.startsWith(Constants.ANONYMOUS_ID)) {
|
204 |
| |
205 |
0
| t = leFactory.createAnonymousID(Byte.parseByte(iriStr.substring(iriStr.lastIndexOf("ID") + 2)));
|
206 |
| } |
207 |
| else { |
208 |
0
| t = helpEvaluateTerm(exprNode, iriStr);
|
209 |
| } |
210 |
| } |
211 |
0
| else if (exprName.equals("type")) {
|
212 |
0
| t = factory.createIRI(exprNode.getAttributes().getNamedItem("name").getNodeValue());
|
213 |
| } |
214 |
| else { |
215 |
0
| throw new ParserException("XML Parse Error: wrong element name: " + exprName + " for a term", null);
|
216 |
| } |
217 |
0
| return t;
|
218 |
| } |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
0
| private Term helpEvaluateTerm(Element exprNode, String iriStr)
|
229 |
| throws ParserException { |
230 |
0
| Term t;
|
231 |
0
| if (iriStr.startsWith("?")) {
|
232 |
0
| try {
|
233 |
0
| t = leFactory.createVariable(iriStr.substring(1));
|
234 |
| } |
235 |
| catch (IllegalArgumentException e) { |
236 |
0
| throw new ParserException("XML Parse Error: Zero-length variable not allowed.", e);
|
237 |
| } |
238 |
| } |
239 |
| else { |
240 |
0
| IRI iri = factory.createIRI(iriStr);
|
241 |
0
| if (exprNode.getChildNodes().getLength() > 1) {
|
242 |
0
| Element e;
|
243 |
0
| Vector <Term> ll = new Vector <Term>();
|
244 |
0
| NodeList nl = exprNode.getChildNodes();
|
245 |
0
| for (int i = 0; i < nl.getLength(); i++) {
|
246 |
0
| Node n = nl.item(i);
|
247 |
0
| if (n instanceof Element) {
|
248 |
0
| e = (Element)n;
|
249 |
0
| ll.add(evaluateXMLTerm(e));
|
250 |
| } |
251 |
| } |
252 |
0
| t = leFactory.createConstructedTerm(iri, ll);
|
253 |
| } |
254 |
| else { |
255 |
0
| t = iri;
|
256 |
| } |
257 |
| } |
258 |
0
| return t;
|
259 |
| } |
260 |
| |
261 |
| |
262 |
| |
263 |
| |
264 |
| |
265 |
| |
266 |
| |
267 |
| |
268 |
| |
269 |
0
| private LogicalExpression evaluateXMLExists(List exprs)
|
270 |
| throws ParserException { |
271 |
0
| HashSet <Variable> hs = new HashSet <Variable>();
|
272 |
0
| for (int i = 0; i < exprs.size() - 1; i++) {
|
273 |
0
| hs.add((Variable) evaluateXMLTerm((Element)exprs.get(i)));
|
274 |
| } |
275 |
0
| return leFactory.createExistentialQuantification(hs, evaluateXML((Element)exprs.get(exprs.size() - 1)));
|
276 |
| } |
277 |
| |
278 |
0
| private LogicalExpression evaluateXMLForAll(List exprs)
|
279 |
| throws ParserException { |
280 |
0
| HashSet <Variable> hs = new HashSet <Variable> ();
|
281 |
0
| for (int i = 0; i < exprs.size() - 1; i++) {
|
282 |
0
| hs.add((Variable) evaluateXMLTerm((Element)exprs.get(i)));
|
283 |
| } |
284 |
0
| return leFactory.createUniversalQuantification(hs, evaluateXML((Element)exprs.get(exprs.size() - 1)));
|
285 |
| } |
286 |
| |
287 |
| |
288 |
| |
289 |
| |
290 |
| |
291 |
| |
292 |
| |
293 |
| |
294 |
| |
295 |
0
| private LogicalExpression evaluateXMLAnd(List exprs)
|
296 |
| throws ParserException { |
297 |
0
| return leFactory.createConjunction(evaluateXML((Element)exprs.get(0)), evaluateXML((Element)exprs.get(1)));
|
298 |
| } |
299 |
| |
300 |
0
| private LogicalExpression evaluateXMLOr(List exprs)
|
301 |
| throws ParserException { |
302 |
0
| return leFactory.createDisjunction(evaluateXML((Element)exprs.get(0)), evaluateXML((Element)exprs.get(1)));
|
303 |
| } |
304 |
| |
305 |
0
| private LogicalExpression evaluateXMLImplies(List exprs)
|
306 |
| throws ParserException { |
307 |
0
| return leFactory.createImplication(evaluateXML((Element)exprs.get(0)), evaluateXML((Element)exprs.get(1)));
|
308 |
| } |
309 |
| |
310 |
0
| private LogicalExpression evaluateXMLImpliesLP(List exprs)
|
311 |
| throws ParserException { |
312 |
0
| return leFactory.createLogicProgrammingRule(evaluateXML((Element)exprs.get(0)),
|
313 |
| evaluateXML((Element)exprs.get(1))); |
314 |
| } |
315 |
| |
316 |
0
| private LogicalExpression evaluateXMLImpliedBy(List exprs)
|
317 |
| throws ParserException { |
318 |
0
| return leFactory.createInverseImplication(evaluateXML((Element)exprs.get(0)), evaluateXML((Element)exprs.get(1)));
|
319 |
| } |
320 |
| |
321 |
0
| private LogicalExpression evaluateXMLEquals(List exprs)
|
322 |
| throws ParserException { |
323 |
0
| return leFactory.createEquivalence(evaluateXML((Element)exprs.get(0)), evaluateXML((Element)exprs.get(1)));
|
324 |
| } |
325 |
| |
326 |
| |
327 |
| |
328 |
| |
329 |
| |
330 |
| |
331 |
| |
332 |
| |
333 |
| |
334 |
0
| private LogicalExpression evaluateXMLNeg(List exprs)
|
335 |
| throws ParserException { |
336 |
0
| return leFactory.createNegation(evaluateXML((Element)exprs.get(0)));
|
337 |
| } |
338 |
| |
339 |
0
| private LogicalExpression evaluateXMLNaf(List exprs)
|
340 |
| throws ParserException { |
341 |
0
| return leFactory.createNegationAsFailure(evaluateXML((Element)exprs.get(0)));
|
342 |
| } |
343 |
| |
344 |
0
| private LogicalExpression evaluateXMLConstraing(List exprs)
|
345 |
| throws ParserException { |
346 |
0
| return leFactory.createConstraint(evaluateXML((Element)exprs.get(0)));
|
347 |
| } |
348 |
| |
349 |
0
| private Set <Molecule> evaluateXMLAttrValue(NodeList attValue, Term id)
|
350 |
| throws ParserException { |
351 |
0
| Set <Molecule> molecules = new HashSet <Molecule> ();
|
352 |
0
| for (int i = 0; i < attValue.getLength(); i++) {
|
353 |
0
| Node singleAttVal = attValue.item(i);
|
354 |
0
| Term attributeName = evaluateXMLTerm((Element)childElements(((Element)singleAttVal).getElementsByTagName("name")).get(0));
|
355 |
0
| List attValContent = childElements(((Element)singleAttVal).getElementsByTagName("value"));
|
356 |
0
| for (int j = 0; j < attValContent.size(); j++) {
|
357 |
0
| Term value = evaluateXMLTerm((Element)attValContent.get(j));
|
358 |
0
| molecules.add(leFactory.createAttributeValue(id, attributeName, value));
|
359 |
| } |
360 |
| } |
361 |
0
| return molecules;
|
362 |
| } |
363 |
| |
364 |
| |
365 |
| |
366 |
| |
367 |
| |
368 |
| |
369 |
| |
370 |
| |
371 |
| |
372 |
| |
373 |
0
| private Set <Molecule> evaluateXMLAttrDef(NodeList attDef, Term id)
|
374 |
| throws ParserException { |
375 |
0
| Set <Molecule> molecules = new HashSet <Molecule> ();
|
376 |
0
| for (int i = 0; i < attDef.getLength(); i++) {
|
377 |
0
| Node singleAttDef = attDef.item(i);
|
378 |
0
| Term attributeName = evaluateXMLTerm((Element)childElements(((Element)singleAttDef).getElementsByTagName("name")).get(0));
|
379 |
0
| List attDefContent = childElements(((Element)singleAttDef).getElementsByTagName("type"));
|
380 |
0
| for (int j = 0; j < attDefContent.size(); j++) {
|
381 |
0
| Term type = evaluateXMLTerm((Element)attDefContent.get(j));
|
382 |
0
| if (singleAttDef.getAttributes().getNamedItem("type").getNodeValue().equals("constraining")) {
|
383 |
0
| molecules.add(leFactory.createAttributeConstraint(
|
384 |
| id, attributeName, type)); |
385 |
| }else{ |
386 |
0
| molecules.add(leFactory.createAttributeInference(
|
387 |
| id, attributeName, type)); |
388 |
| } |
389 |
| } |
390 |
| } |
391 |
0
| return molecules;
|
392 |
| } |
393 |
| |
394 |
0
| private List <Term> evaluateXMLIsa(NodeList cptE)
|
395 |
| throws ParserException { |
396 |
0
| NodeList isaTerms;
|
397 |
0
| List <Term> set = new Vector <Term> ();
|
398 |
0
| for (int i = 0; i < cptE.getLength(); i++) {
|
399 |
0
| isaTerms = ((Element)cptE.item(i)).getElementsByTagName("term");
|
400 |
0
| for (int j = 0; j < isaTerms.getLength(); j++) {
|
401 |
0
| set.add(evaluateXMLTerm((Element)isaTerms.item(j)));
|
402 |
| } |
403 |
| } |
404 |
0
| return set;
|
405 |
| } |
406 |
| |
407 |
| |
408 |
| |
409 |
| |
410 |
| |
411 |
| |
412 |
| |
413 |
| |
414 |
| |
415 |
| |
416 |
0
| private LogicalExpression evaluateXMLConstructed(Node exprNode, IRI identifier)
|
417 |
| throws ParserException { |
418 |
0
| Element e;
|
419 |
0
| Vector <Term> ll = new Vector <Term> ();
|
420 |
0
| NodeList nl = exprNode.getChildNodes();
|
421 |
0
| for (int i = 0; i < nl.getLength(); i++) {
|
422 |
0
| Node n = nl.item(i);
|
423 |
| |
424 |
0
| if (n instanceof Element) {
|
425 |
0
| e = (Element)n;
|
426 |
0
| ll.add(evaluateXMLTerm(e));
|
427 |
| } |
428 |
| } |
429 |
0
| return leFactory.createAtom(identifier, ll);
|
430 |
| } |
431 |
| |
432 |
| |
433 |
| |
434 |
| |
435 |
| |
436 |
| |
437 |
| |
438 |
| |
439 |
| |
440 |
0
| private List <Node> childElements(NodeList nodes) {
|
441 |
0
| List <Node> list = new LinkedList <Node>();
|
442 |
0
| for (int i = 0; i < nodes.getLength(); i++) {
|
443 |
0
| if (nodes.item(i)instanceof Element) {
|
444 |
0
| list.add(nodes.item(i));
|
445 |
| } |
446 |
| } |
447 |
0
| return list;
|
448 |
| } |
449 |
| } |