1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| package org.deri.wsmo4j.validator; |
17 |
| |
18 |
| |
19 |
| import java.util.List; |
20 |
| |
21 |
| import org.deri.wsmo4j.io.serializer.wsml.LogExprSerializerWSML; |
22 |
| import org.deri.wsmo4j.logicalexpression.ConstantTransformer; |
23 |
| import org.omwg.logicalexpression.Atom; |
24 |
| import org.omwg.logicalexpression.AttributeConstraintMolecule; |
25 |
| import org.omwg.logicalexpression.AttributeInferenceMolecule; |
26 |
| import org.omwg.logicalexpression.AttributeValueMolecule; |
27 |
| import org.omwg.logicalexpression.Binary; |
28 |
| import org.omwg.logicalexpression.CompoundMolecule; |
29 |
| import org.omwg.logicalexpression.Conjunction; |
30 |
| import org.omwg.logicalexpression.Constants; |
31 |
| import org.omwg.logicalexpression.Constraint; |
32 |
| import org.omwg.logicalexpression.Disjunction; |
33 |
| import org.omwg.logicalexpression.Equivalence; |
34 |
| import org.omwg.logicalexpression.ExistentialQuantification; |
35 |
| import org.omwg.logicalexpression.Implication; |
36 |
| import org.omwg.logicalexpression.InverseImplication; |
37 |
| import org.omwg.logicalexpression.LogicProgrammingRule; |
38 |
| import org.omwg.logicalexpression.LogicalExpression; |
39 |
| import org.omwg.logicalexpression.MembershipMolecule; |
40 |
| import org.omwg.logicalexpression.Negation; |
41 |
| import org.omwg.logicalexpression.NegationAsFailure; |
42 |
| import org.omwg.logicalexpression.SubConceptMolecule; |
43 |
| import org.omwg.logicalexpression.Unary; |
44 |
| import org.omwg.logicalexpression.UniversalQuantification; |
45 |
| import org.omwg.logicalexpression.Visitor; |
46 |
| import org.omwg.ontology.Axiom; |
47 |
| import org.wsmo.common.WSML; |
48 |
| import org.wsmo.validator.ValidationError; |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| public class WsmlRuleExpressionValidator |
57 |
| implements Visitor { |
58 |
| |
59 |
| private LogExprSerializerWSML leSerializer = null; |
60 |
| |
61 |
| private WsmlFullValidator validator = null; |
62 |
| |
63 |
| private Axiom axiom = null; |
64 |
| |
65 |
| private List <ValidationError> errors = null; |
66 |
| |
67 |
| private boolean body = false; |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
0
| public WsmlRuleExpressionValidator(Axiom axiom, List <ValidationError> errors, WsmlFullValidator val) {
|
74 |
0
| this.axiom = axiom;
|
75 |
0
| this.errors = errors;
|
76 |
0
| this.validator = val;
|
77 |
0
| leSerializer = validator.leSerializer;
|
78 |
| } |
79 |
| |
80 |
0
| public void setup() {
|
81 |
0
| body = false;
|
82 |
| } |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
0
| public void visitAtom(Atom expr) {
|
90 |
0
| if (!body) {
|
91 |
0
| boolean infix = false;
|
92 |
0
| String iri = expr.getIdentifier().toString();
|
93 |
0
| infix = ConstantTransformer.getInstance().isInfix(iri);
|
94 |
| |
95 |
0
| if (infix) {
|
96 |
0
| if (iri.indexOf(Constants.INEQUAL) != -1) {
|
97 |
0
| addError(expr, ValidationError.AX_HEAD_ERR + ": Must not contain the " +
|
98 |
| "inequality symbol \"!=\":\n" + leSerializer.serialize(expr)); |
99 |
| } |
100 |
0
| else if (iri.indexOf(Constants.EQUAL) != -1) {
|
101 |
0
| addError(expr, ValidationError.AX_HEAD_ERR + ": Must not contain the " +
|
102 |
| "equality symbol \"=\":\n" + leSerializer.serialize(expr)); |
103 |
| } |
104 |
0
| else if (iri.indexOf(Constants.STRONG_EQUAL) != -1) {
|
105 |
0
| addError(expr, ValidationError.AX_HEAD_ERR + ": Must not contain the " +
|
106 |
| "strong equality symbol \":=:\":\n" + |
107 |
| leSerializer.serialize(expr)); |
108 |
| } |
109 |
| } |
110 |
| } |
111 |
| else { |
112 |
0
| boolean infix = false;
|
113 |
0
| String iri = expr.getIdentifier().toString();
|
114 |
0
| infix = ConstantTransformer.getInstance().isInfix(iri);
|
115 |
| |
116 |
0
| if (infix) {
|
117 |
0
| if (iri.indexOf(Constants.STRONG_EQUAL) != -1) {
|
118 |
0
| addError(expr, ValidationError.AX_BODY_ERR + ": Must " +
|
119 |
| "not contain the strong equality symbol \":=:\":\n" + |
120 |
| leSerializer.serialize(expr)); |
121 |
| } |
122 |
| } |
123 |
| } |
124 |
| } |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
0
| public void visitAttributeContraintMolecule(AttributeConstraintMolecule expr) {
|
132 |
| |
133 |
| } |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
0
| public void visitAttributeInferenceMolecule(AttributeInferenceMolecule expr) {
|
141 |
| |
142 |
| } |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
0
| public void visitAttributeValueMolecule(AttributeValueMolecule expr) {
|
150 |
| |
151 |
| } |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
0
| public void visitCompoundMolecule(CompoundMolecule expr) {
|
159 |
| |
160 |
| } |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
0
| public void visitMemberShipMolecule(MembershipMolecule expr) {
|
168 |
| |
169 |
| } |
170 |
| |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
0
| public void visitSubConceptMolecule(SubConceptMolecule expr) {
|
177 |
| |
178 |
| } |
179 |
| |
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
| |
185 |
| |
186 |
0
| public void visitNegationAsFailure(NegationAsFailure expr) {
|
187 |
0
| if (!body) {
|
188 |
0
| addError(expr, ValidationError.AX_HEAD_ERR + ": Must not " +
|
189 |
| "contain a NegationAsFailure:\n" + |
190 |
| leSerializer.serialize(expr)); |
191 |
| } |
192 |
| else { |
193 |
0
| expr.getOperand().accept(this);
|
194 |
| } |
195 |
| } |
196 |
| |
197 |
| |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
0
| public void visitNegation(Negation expr) {
|
203 |
0
| if (!body) {
|
204 |
0
| addError(expr, ValidationError.AX_HEAD_ERR + ": Must not " +
|
205 |
| "contain a Negation:\n" + leSerializer.serialize(expr)); |
206 |
| } |
207 |
| else { |
208 |
0
| addError(expr, ValidationError.AX_BODY_ERR + ": Must not " +
|
209 |
| "contain a Negation:\n" + leSerializer.serialize(expr)); |
210 |
0
| body = false;
|
211 |
| } |
212 |
| } |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
| |
218 |
| |
219 |
0
| public void visitConstraint(Constraint expr) {
|
220 |
0
| if (!body) {
|
221 |
0
| body = true;
|
222 |
0
| expr.getOperand().accept(this);
|
223 |
| } |
224 |
| else { |
225 |
0
| addError(expr, ValidationError.AX_BODY_ERR + ": Must not " +
|
226 |
| "contain a Negation:\n" + leSerializer.serialize(expr)); |
227 |
0
| body = false;
|
228 |
| } |
229 |
| } |
230 |
| |
231 |
| |
232 |
| |
233 |
| |
234 |
| |
235 |
| |
236 |
0
| public void visitConjunction(Conjunction expr) {
|
237 |
0
| expr.getLeftOperand().accept(this);
|
238 |
0
| expr.getRightOperand().accept(this);
|
239 |
| } |
240 |
| |
241 |
| |
242 |
| |
243 |
| |
244 |
| |
245 |
| |
246 |
0
| public void visitDisjunction(Disjunction expr) {
|
247 |
0
| if (!body) {
|
248 |
0
| addError(expr, ValidationError.AX_HEAD_ERR + ": Must not " +
|
249 |
| "contain a Disjunction:\n" + leSerializer.serialize(expr)); |
250 |
| } |
251 |
| else { |
252 |
0
| expr.getLeftOperand().accept(this);
|
253 |
0
| expr.getRightOperand().accept(this);
|
254 |
| } |
255 |
| } |
256 |
| |
257 |
| |
258 |
| |
259 |
| |
260 |
| |
261 |
| |
262 |
0
| public void visitEquivalence(Equivalence expr) {
|
263 |
0
| if (!body) {
|
264 |
0
| if (checkHeads(expr)) {
|
265 |
0
| addError(expr, ValidationError.AX_HEAD_ERR + ": a and b from " +
|
266 |
| "\"a equivalent b\" must not contain {implies, " + |
267 |
| "impliedBy, equivalent):\n" + leSerializer.serialize(expr)); |
268 |
| } |
269 |
| else { |
270 |
0
| expr.getLeftOperand().accept(this);
|
271 |
0
| expr.getRightOperand().accept(this);
|
272 |
| } |
273 |
| } |
274 |
| else { |
275 |
0
| expr.getLeftOperand().accept(this);
|
276 |
0
| expr.getRightOperand().accept(this);
|
277 |
| } |
278 |
| } |
279 |
| |
280 |
| |
281 |
| |
282 |
| |
283 |
| |
284 |
| |
285 |
0
| public void visitInverseImplication(InverseImplication expr) {
|
286 |
0
| if (!body) {
|
287 |
0
| if (checkHeads(expr)) {
|
288 |
0
| addError(expr, ValidationError.AX_HEAD_ERR + ": a and b from " +
|
289 |
| "\"a impliedBy b\" must not contain {implies, " + |
290 |
| "impliedBy, equivalent):\n" + leSerializer.serialize(expr)); |
291 |
| } |
292 |
| else { |
293 |
0
| expr.getLeftOperand().accept(this);
|
294 |
0
| expr.getRightOperand().accept(this);
|
295 |
| } |
296 |
| } |
297 |
| else { |
298 |
0
| expr.getLeftOperand().accept(this);
|
299 |
0
| expr.getRightOperand().accept(this);
|
300 |
| } |
301 |
| } |
302 |
| |
303 |
| |
304 |
| |
305 |
| |
306 |
| |
307 |
| |
308 |
0
| public void visitImplication(Implication expr) {
|
309 |
0
| if (!body) {
|
310 |
0
| if (checkHeads(expr)) {
|
311 |
0
| addError(expr, ValidationError.AX_HEAD_ERR + ": a and b " +
|
312 |
| "from \"a implies b\" must not contain {implies, " + |
313 |
| "impliedBy, equivalent):\n" + leSerializer.serialize(expr)); |
314 |
| } |
315 |
| else { |
316 |
0
| expr.getLeftOperand().accept(this);
|
317 |
0
| expr.getRightOperand().accept(this);
|
318 |
| } |
319 |
| } |
320 |
| else { |
321 |
0
| expr.getLeftOperand().accept(this);
|
322 |
0
| expr.getRightOperand().accept(this);
|
323 |
| } |
324 |
| } |
325 |
| |
326 |
| |
327 |
| |
328 |
| |
329 |
| |
330 |
| |
331 |
0
| public void visitLogicProgrammingRule(LogicProgrammingRule expr) {
|
332 |
0
| body = false;
|
333 |
0
| expr.getLeftOperand().accept(this);
|
334 |
0
| body = true;
|
335 |
0
| expr.getRightOperand().accept(this);
|
336 |
| } |
337 |
| |
338 |
| |
339 |
| |
340 |
| |
341 |
| |
342 |
| |
343 |
0
| public void visitUniversalQuantification(UniversalQuantification expr) {
|
344 |
0
| if (!body) {
|
345 |
0
| addError(expr, ValidationError.AX_HEAD_ERR + ": Must not " +
|
346 |
| "contain an UniversalQuantification:\n" + |
347 |
| leSerializer.serialize(expr)); |
348 |
| } |
349 |
| else { |
350 |
0
| expr.getOperand().accept(this);
|
351 |
| } |
352 |
| |
353 |
| } |
354 |
| |
355 |
| |
356 |
| |
357 |
| |
358 |
| |
359 |
| |
360 |
0
| public void visitExistentialQuantification(ExistentialQuantification expr) {
|
361 |
0
| if (!body) {
|
362 |
0
| addError(expr, ValidationError.AX_HEAD_ERR + ": Must not " +
|
363 |
| "contain an ExistentialQuantification:\n" + |
364 |
| leSerializer.serialize(expr)); |
365 |
| } |
366 |
| else { |
367 |
0
| expr.getOperand().accept(this);
|
368 |
| } |
369 |
| |
370 |
| } |
371 |
| |
372 |
| |
373 |
| |
374 |
| |
375 |
| |
376 |
0
| private boolean checkHeads(LogicalExpression expr) {
|
377 |
0
| boolean flag = false;
|
378 |
0
| if (expr instanceof Binary) {
|
379 |
0
| LogicalExpression lLeft = ((Binary) expr).getLeftOperand();
|
380 |
0
| LogicalExpression lRight = ((Binary) expr).getRightOperand();
|
381 |
0
| if (lLeft instanceof Equivalence || lLeft instanceof Implication
|
382 |
| || lLeft instanceof InverseImplication) { |
383 |
0
| flag = true;
|
384 |
| } |
385 |
| else { |
386 |
0
| checkHeads(lLeft);
|
387 |
| } |
388 |
0
| if (lRight instanceof Equivalence || lRight instanceof Implication
|
389 |
| || lRight instanceof InverseImplication) { |
390 |
0
| flag = true;
|
391 |
| } |
392 |
| else { |
393 |
0
| checkHeads(lRight);
|
394 |
| } |
395 |
| } |
396 |
0
| else if (expr instanceof Unary) {
|
397 |
0
| LogicalExpression l = ((Unary) expr).getOperand();
|
398 |
0
| if (l instanceof Equivalence || l instanceof Implication
|
399 |
| || l instanceof InverseImplication) { |
400 |
0
| flag = true;
|
401 |
| } |
402 |
| else { |
403 |
0
| checkHeads(l);
|
404 |
| } |
405 |
| } |
406 |
0
| return flag;
|
407 |
| } |
408 |
| |
409 |
0
| private void addError(LogicalExpression logexp, String msg) {
|
410 |
0
| LogicalExpressionErrorImpl le = new LogicalExpressionErrorImpl(
|
411 |
| axiom, logexp, msg, WSML.WSML_RULE); |
412 |
0
| if (!errors.contains(le)) {
|
413 |
0
| errors.add(le);
|
414 |
| } |
415 |
| } |
416 |
| } |