|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| package org.qedeq.kernel.bo.logic; |
|
19 |
| |
|
20 |
| import org.qedeq.kernel.base.list.Atom; |
|
21 |
| import org.qedeq.kernel.base.list.Element; |
|
22 |
| import org.qedeq.kernel.base.list.ElementList; |
|
23 |
| import org.qedeq.kernel.bo.module.ModuleContext; |
|
24 |
| import org.qedeq.kernel.dto.list.ElementSet; |
|
25 |
| import org.qedeq.kernel.trace.Trace; |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| public final class FormulaChecker implements Operators, FormulaBasicErrors { |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| private final ModuleContext currentContext; |
|
53 |
| |
|
54 |
| |
|
55 |
| private final ExistenceChecker existenceChecker; |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
737
| private FormulaChecker(final ModuleContext context,
|
|
67 |
| final ExistenceChecker existenceChecker) { |
|
68 |
737
| this.existenceChecker = existenceChecker;
|
|
69 |
737
| if (existenceChecker.equalityOperatorExists()
|
|
70 |
| && !existenceChecker.predicateExists(existenceChecker.getEqualityOperator(), 2)) { |
|
71 |
0
| throw new IllegalArgumentException("equality predicate should exist, but it doesn't");
|
|
72 |
| } |
|
73 |
737
| currentContext = new ModuleContext(context);
|
|
74 |
| } |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
51
| public static final void checkFormula(final Element element, final ModuleContext context)
|
|
84 |
| throws LogicalCheckException { |
|
85 |
51
| checkFormula(element, context, EverythingExists.getInstance());
|
|
86 |
| } |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
594
| public static final void checkFormula(final Element element, final ModuleContext context,
|
|
97 |
| final ExistenceChecker existenceChecker) throws LogicalCheckException { |
|
98 |
594
| final FormulaChecker checker = new FormulaChecker(context, existenceChecker);
|
|
99 |
594
| checker.checkFormula(element);
|
|
100 |
| } |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
21
| public static final void checkTerm(final Element element, final ModuleContext context)
|
|
110 |
| throws LogicalCheckException { |
|
111 |
21
| checkTerm(element, context, EverythingExists.getInstance());
|
|
112 |
| } |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
143
| public static final void checkTerm(final Element element, final ModuleContext context,
|
|
123 |
| final ExistenceChecker existenceChecker) throws LogicalCheckException { |
|
124 |
143
| final FormulaChecker checker = new FormulaChecker(context, existenceChecker);
|
|
125 |
143
| checker.checkTerm(element);
|
|
126 |
| } |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
4670
| private final void checkFormula(final Element element)
|
|
135 |
| throws LogicalCheckException { |
|
136 |
4670
| final String method = "checkFormula";
|
|
137 |
4670
| Trace.begin(this, method);
|
|
138 |
4670
| Trace.param(this, method, "element", element);
|
|
139 |
4670
| final String context = getCurrentContext().getLocationWithinModule();
|
|
140 |
4670
| Trace.param(this, method, "context", context);
|
|
141 |
4670
| checkList(element);
|
|
142 |
4663
| final ElementList list = element.getList();
|
|
143 |
4663
| final String listContext = context + ".getList()";
|
|
144 |
4663
| setLocationWithinModule(listContext);
|
|
145 |
4663
| final String operator = list.getOperator();
|
|
146 |
4663
| if (operator.equals(CONJUNCTION_OPERATOR)
|
|
147 |
| || operator.equals(DISJUNCTION_OPERATOR) |
|
148 |
| || operator.equals(IMPLICATION_OPERATOR) |
|
149 |
| || operator.equals(EQUIVALENCE_OPERATOR)) { |
|
150 |
1453
| Trace.trace(this, method, "one of (and, or, implication, equivalence) operator found");
|
|
151 |
1453
| if (list.size() <= 1) {
|
|
152 |
9
| throw new FormulaCheckException(MORE_THAN_ONE_ARGUMENT_EXPECTED,
|
|
153 |
| MORE_THAN_ONE_ARGUMENT_EXPECTED_TEXT + "\"" |
|
154 |
| + operator + "\"", element, getCurrentContext()); |
|
155 |
| } |
|
156 |
1444
| if (operator.equals(IMPLICATION_OPERATOR) && list.size() != 2) {
|
|
157 |
1
| throw new FormulaCheckException(EXACTLY_TWO_ARGUMENTS_EXPECTED,
|
|
158 |
| EXACTLY_TWO_ARGUMENTS_EXPECTED_TEXT + "\"" |
|
159 |
| + operator + "\"", element, getCurrentContext()); |
|
160 |
| } |
|
161 |
1443
| for (int i = 0; i < list.size(); i++) {
|
|
162 |
3290
| setLocationWithinModule(listContext + ".getElement(" + i + ")");
|
|
163 |
3290
| checkFormula(list.getElement(i));
|
|
164 |
| } |
|
165 |
1434
| setLocationWithinModule(listContext);
|
|
166 |
1434
| checkFreeAndBoundDisjunct(0, list);
|
|
167 |
3210
| } else if (operator.equals(NEGATION_OPERATOR)) {
|
|
168 |
159
| Trace.trace(this, method, "negation operator found");
|
|
169 |
159
| setLocationWithinModule(listContext);
|
|
170 |
159
| if (list.size() != 1) {
|
|
171 |
2
| throw new FormulaCheckException(EXACTLY_ONE_ARGUMENT_EXPECTED,
|
|
172 |
| EXACTLY_ONE_ARGUMENT_EXPECTED_TEXT + "\"" + operator + "\"", |
|
173 |
| element, getCurrentContext()); |
|
174 |
| } |
|
175 |
157
| setLocationWithinModule(listContext + ".getElement(0)");
|
|
176 |
157
| checkFormula(list.getElement(0));
|
|
177 |
3051
| } else if (operator.equals(PREDICATE_VARIABLE)
|
|
178 |
| || operator.equals(PREDICATE_CONSTANT)) { |
|
179 |
2670
| Trace.trace(this, method, "predicate operator found");
|
|
180 |
2670
| setLocationWithinModule(listContext);
|
|
181 |
2670
| if (list.size() < 1) {
|
|
182 |
2
| throw new FormulaCheckException(AT_LEAST_ONE_ARGUMENT_EXPECTED,
|
|
183 |
| AT_LEAST_ONE_ARGUMENT_EXPECTED_TEXT + "\"" + operator + "\"", |
|
184 |
| element, getCurrentContext()); |
|
185 |
| } |
|
186 |
| |
|
187 |
2668
| setLocationWithinModule(listContext + ".getElement(0)");
|
|
188 |
2668
| checkAtomFirst(list.getElement(0));
|
|
189 |
| |
|
190 |
| |
|
191 |
2660
| for (int i = 1; i < list.size(); i++) {
|
|
192 |
3112
| setLocationWithinModule(listContext + ".getElement(" + i + ")");
|
|
193 |
3112
| checkTerm(list.getElement(i));
|
|
194 |
| } |
|
195 |
| |
|
196 |
2658
| setLocationWithinModule(listContext);
|
|
197 |
2658
| checkFreeAndBoundDisjunct(1, list);
|
|
198 |
| |
|
199 |
| |
|
200 |
2654
| if (PREDICATE_CONSTANT.equals(operator) && !existenceChecker.predicateExists(
|
|
201 |
| list.getElement(0).getAtom().getString(), list.size() - 1)) { |
|
202 |
4
| throw new FormulaCheckException(UNKNOWN_PREDICATE_CONSTANT,
|
|
203 |
| UNKNOWN_PREDICATE_CONSTANT_TEXT + "\"" |
|
204 |
| + list.getElement(0).getAtom().getString() + "\"", element, |
|
205 |
| getCurrentContext()); |
|
206 |
| } |
|
207 |
| |
|
208 |
381
| } else if (operator.equals(EXISTENTIAL_QUANTIFIER_OPERATOR)
|
|
209 |
| || operator.equals(UNIQUE_EXISTENTIAL_QUANTIFIER_OPERATOR) |
|
210 |
| || operator.equals(UNIVERSAL_QUANTIFIER_OPERATOR)) { |
|
211 |
376
| Trace.trace(this, method, "quantifier found");
|
|
212 |
376
| setLocationWithinModule(context);
|
|
213 |
376
| checkQuantifier(element);
|
|
214 |
| } else { |
|
215 |
5
| setLocationWithinModule(listContext + ".getOperator()");
|
|
216 |
5
| throw new FormulaCheckException(UNKNOWN_LOGICAL_OPERATOR,
|
|
217 |
| UNKNOWN_LOGICAL_OPERATOR_TEXT + "\"" + operator + "\"", |
|
218 |
| element, getCurrentContext()); |
|
219 |
| } |
|
220 |
| |
|
221 |
4562
| setLocationWithinModule(context);
|
|
222 |
4562
| Trace.end(this, method);
|
|
223 |
| } |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
376
| private void checkQuantifier(final Element element) throws LogicalCheckException {
|
|
234 |
376
| final String method = "checkQuantifier";
|
|
235 |
376
| Trace.begin(this, method);
|
|
236 |
376
| Trace.param(this, method, "element", element);
|
|
237 |
| |
|
238 |
376
| final String context = getCurrentContext().getLocationWithinModule();
|
|
239 |
376
| Trace.param(this, method, "context", context);
|
|
240 |
376
| checkList(element);
|
|
241 |
376
| final ElementList list = element.getList();
|
|
242 |
376
| final String listContext = context + ".getList()";
|
|
243 |
376
| setLocationWithinModule(listContext);
|
|
244 |
376
| final String operator = list.getOperator();
|
|
245 |
376
| if (!operator.equals(EXISTENTIAL_QUANTIFIER_OPERATOR)
|
|
246 |
| && operator.equals(UNIQUE_EXISTENTIAL_QUANTIFIER_OPERATOR) |
|
247 |
| && operator.equals(UNIVERSAL_QUANTIFIER_OPERATOR)) { |
|
248 |
0
| throw new IllegalArgumentException("quantifier element expected but found: "
|
|
249 |
| + element.toString()); |
|
250 |
| } |
|
251 |
376
| if (list.size() < 2 || list.size() > 3) {
|
|
252 |
0
| throw new FormulaCheckException(EXACTLY_TWO_OR_THREE_ARGUMENTS_EXPECTED,
|
|
253 |
| EXACTLY_TWO_OR_THREE_ARGUMENTS_EXPECTED_TEXT, element, getCurrentContext()); |
|
254 |
| } |
|
255 |
| |
|
256 |
| |
|
257 |
376
| if (operator.equals(UNIQUE_EXISTENTIAL_QUANTIFIER_OPERATOR)
|
|
258 |
| && !existenceChecker.equalityOperatorExists()) { |
|
259 |
0
| setLocationWithinModule(listContext + ".getOperator()");
|
|
260 |
0
| throw new FormulaCheckException(EQUALITY_PREDICATE_NOT_YET_DEFINED,
|
|
261 |
| EQUALITY_PREDICATE_NOT_YET_DEFINED_TEXT, element, getCurrentContext()); |
|
262 |
| } |
|
263 |
| |
|
264 |
| |
|
265 |
376
| setLocationWithinModule(listContext + ".getElement(" + 0 + ")");
|
|
266 |
376
| checkSubjectVariable(list.getElement(0));
|
|
267 |
| |
|
268 |
| |
|
269 |
373
| setLocationWithinModule(listContext + ".getElement(" + 1 + ")");
|
|
270 |
373
| checkFormula(list.getElement(1));
|
|
271 |
| |
|
272 |
368
| setLocationWithinModule(listContext);
|
|
273 |
| |
|
274 |
368
| if (FormulaChecker.getBoundSubjectVariables(list.getElement(1)).contains(
|
|
275 |
| list.getElement(0))) { |
|
276 |
11
| throw new FormulaCheckException(SUBJECT_VARIABLE_ALREADY_BOUND_IN_FORMULA,
|
|
277 |
| SUBJECT_VARIABLE_ALREADY_BOUND_IN_FORMULA_TEXT, list.getElement(1), |
|
278 |
| getCurrentContext()); |
|
279 |
| } |
|
280 |
357
| if (list.size() > 3) {
|
|
281 |
0
| throw new FormulaCheckException(EXACTLY_TWO_OR_THREE_ARGUMENTS_EXPECTED,
|
|
282 |
| EXACTLY_TWO_OR_THREE_ARGUMENTS_EXPECTED_TEXT, list, |
|
283 |
| getCurrentContext()); |
|
284 |
| } |
|
285 |
357
| if (list.size() > 2) {
|
|
286 |
| |
|
287 |
47
| setLocationWithinModule(listContext + ".getElement(" + 2 + ")");
|
|
288 |
47
| checkFormula(list.getElement(2));
|
|
289 |
| |
|
290 |
| |
|
291 |
45
| setLocationWithinModule(listContext);
|
|
292 |
45
| if (FormulaChecker.getBoundSubjectVariables(list.getElement(2)).contains(
|
|
293 |
| list.getElement(0))) { |
|
294 |
3
| throw new FormulaCheckException(SUBJECT_VARIABLE_ALREADY_BOUND_IN_FORMULA,
|
|
295 |
| SUBJECT_VARIABLE_ALREADY_BOUND_IN_FORMULA_TEXT, list.getElement(2), |
|
296 |
| getCurrentContext()); |
|
297 |
| } |
|
298 |
42
| setLocationWithinModule(listContext);
|
|
299 |
42
| checkFreeAndBoundDisjunct(1, list);
|
|
300 |
| } |
|
301 |
| |
|
302 |
340
| setLocationWithinModule(context);
|
|
303 |
340
| Trace.end(this, method);
|
|
304 |
| } |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
4704
| private void checkTerm(final Element element) throws LogicalCheckException {
|
|
313 |
4704
| final String method = "checkTerm";
|
|
314 |
4704
| Trace.begin(this, method);
|
|
315 |
4704
| Trace.param(this, method, "element", element);
|
|
316 |
| |
|
317 |
4704
| final String context = getCurrentContext().getLocationWithinModule();
|
|
318 |
4704
| Trace.param(this, method, "context", context);
|
|
319 |
4704
| checkList(element);
|
|
320 |
4700
| final ElementList list = element.getList();
|
|
321 |
4700
| final String listContext = context + ".getList()";
|
|
322 |
4700
| setLocationWithinModule(listContext);
|
|
323 |
4700
| final String operator = list.getOperator();
|
|
324 |
4700
| if (operator.equals(SUBJECT_VARIABLE)) {
|
|
325 |
3241
| checkSubjectVariable(element);
|
|
326 |
1459
| } else if (operator.equals(FUNCTION_CONSTANT)
|
|
327 |
| || operator.equals(FUNCTION_VARIABLE)) { |
|
328 |
| |
|
329 |
| |
|
330 |
1236
| if (operator.equals(FUNCTION_CONSTANT) && list.size() < 1) {
|
|
331 |
1
| throw new TermCheckException(AT_LEAST_ONE_ARGUMENT_EXPECTED,
|
|
332 |
| AT_LEAST_ONE_ARGUMENT_EXPECTED_TEXT, element, getCurrentContext()); |
|
333 |
| } |
|
334 |
| |
|
335 |
| |
|
336 |
1235
| if (operator.equals(FUNCTION_VARIABLE) && list.size() < 2) {
|
|
337 |
2
| throw new TermCheckException(MORE_THAN_ONE_ARGUMENT_EXPECTED,
|
|
338 |
| MORE_THAN_ONE_ARGUMENT_EXPECTED_TEXT, element, getCurrentContext()); |
|
339 |
| } |
|
340 |
| |
|
341 |
| |
|
342 |
1233
| setLocationWithinModule(listContext + ".getElement(0)");
|
|
343 |
1233
| checkAtomFirst(list.getElement(0));
|
|
344 |
| |
|
345 |
| |
|
346 |
1226
| setLocationWithinModule(listContext);
|
|
347 |
1226
| for (int i = 1; i < list.size(); i++) {
|
|
348 |
1449
| setLocationWithinModule(listContext + ".getElement(" + i + ")");
|
|
349 |
1449
| checkTerm(list.getElement(i));
|
|
350 |
| } |
|
351 |
| |
|
352 |
1225
| setLocationWithinModule(listContext);
|
|
353 |
1225
| checkFreeAndBoundDisjunct(1, list);
|
|
354 |
| |
|
355 |
| |
|
356 |
1221
| setLocationWithinModule(listContext);
|
|
357 |
1221
| if (FUNCTION_CONSTANT.equals(operator) && !existenceChecker.functionExists(
|
|
358 |
| list.getElement(0).getAtom().getString(), list.size() - 1)) { |
|
359 |
2
| throw new FormulaCheckException(UNKNOWN_FUNCTION_CONSTANT,
|
|
360 |
| UNKNOWN_FUNCTION_CONSTANT_TEXT + "\"" |
|
361 |
| + list.getElement(0).getAtom().getString() + "\"", element, |
|
362 |
| getCurrentContext()); |
|
363 |
| } |
|
364 |
| |
|
365 |
223
| } else if (operator.equals(CLASS)) {
|
|
366 |
| |
|
367 |
212
| if (list.size() != 2) {
|
|
368 |
2
| throw new TermCheckException(EXACTLY_TWO_ARGUMENTS_EXPECTED,
|
|
369 |
| EXACTLY_TWO_ARGUMENTS_EXPECTED_TEXT, element, getCurrentContext()); |
|
370 |
| } |
|
371 |
| |
|
372 |
210
| setLocationWithinModule(listContext + ".getElement(" + 0 + ")");
|
|
373 |
210
| if (!isSubjectVariable(list.getElement(0))) {
|
|
374 |
1
| throw new TermCheckException(SUBJECT_VARIABLE_EXPECTED,
|
|
375 |
| SUBJECT_VARIABLE_EXPECTED_TEXT, element, getCurrentContext()); |
|
376 |
| } |
|
377 |
| |
|
378 |
| |
|
379 |
209
| setLocationWithinModule(listContext + ".getElement(" + 1 + ")");
|
|
380 |
209
| checkFormula(list.getElement(1));
|
|
381 |
| |
|
382 |
| |
|
383 |
209
| setLocationWithinModule(listContext);
|
|
384 |
209
| if (!existenceChecker.classOperatorExists()) {
|
|
385 |
2
| throw new FormulaCheckException(CLASS_OPERATOR_STILL_UNKNOWN,
|
|
386 |
| CLASS_OPERATOR_STILL_UNKNOWN_TEXT, element, getCurrentContext()); |
|
387 |
| } |
|
388 |
| |
|
389 |
| |
|
390 |
207
| setLocationWithinModule(listContext + ".getElement(" + 0 + ")");
|
|
391 |
207
| if (FormulaChecker.getBoundSubjectVariables(list.getElement(1)).contains(
|
|
392 |
| list.getElement(0))) { |
|
393 |
1
| throw new TermCheckException(SUBJECT_VARIABLE_ALREADY_BOUND_IN_FORMULA,
|
|
394 |
| SUBJECT_VARIABLE_ALREADY_BOUND_IN_FORMULA_TEXT, list.getElement(0), |
|
395 |
| getCurrentContext()); |
|
396 |
| } |
|
397 |
| |
|
398 |
| } else { |
|
399 |
11
| setLocationWithinModule(listContext + ".getOperator()");
|
|
400 |
11
| throw new TermCheckException(UNKNOWN_TERM_OPERATOR,
|
|
401 |
| UNKNOWN_TERM_OPERATOR_TEXT + "\"" + operator + "\"", element, getCurrentContext()); |
|
402 |
| } |
|
403 |
| |
|
404 |
4663
| setLocationWithinModule(context);
|
|
405 |
4663
| Trace.end(this, method);
|
|
406 |
| } |
|
407 |
| |
|
408 |
| |
|
409 |
| |
|
410 |
| |
|
411 |
| |
|
412 |
| |
|
413 |
| |
|
414 |
| |
|
415 |
| |
|
416 |
| |
|
417 |
5359
| private void checkFreeAndBoundDisjunct(final int start,
|
|
418 |
| final ElementList list) throws LogicalCheckException { |
|
419 |
| |
|
420 |
5359
| final String context = getCurrentContext().getLocationWithinModule();
|
|
421 |
5359
| final ElementSet free = new ElementSet();
|
|
422 |
5359
| final ElementSet bound = new ElementSet();
|
|
423 |
5359
| for (int i = start; i < list.size(); i++) {
|
|
424 |
7917
| setLocationWithinModule(context + ".getElement(" + i + ")");
|
|
425 |
7917
| final ElementSet newFree
|
|
426 |
| = getFreeSubjectVariables(list.getElement(i)); |
|
427 |
7917
| final ElementSet newBound
|
|
428 |
| = FormulaChecker.getBoundSubjectVariables(list.getElement(i)); |
|
429 |
7917
| final ElementSet interBound = newFree.newIntersection(bound);
|
|
430 |
7917
| if (!interBound.isEmpty()) {
|
|
431 |
21
| throw new FormulaCheckException(FREE_VARIABLE_ALREADY_BOUND,
|
|
432 |
| FREE_VARIABLE_ALREADY_BOUND_TEXT |
|
433 |
| + interBound, list.getElement(i), getCurrentContext()); |
|
434 |
| } |
|
435 |
7896
| final ElementSet interFree = newBound.newIntersection(free);
|
|
436 |
7896
| if (!interFree.isEmpty()) {
|
|
437 |
18
| throw new FormulaCheckException(BOUND_VARIABLE_ALREADY_FREE,
|
|
438 |
| BOUND_VARIABLE_ALREADY_FREE_TEXT |
|
439 |
| + interFree, list.getElement(i), getCurrentContext()); |
|
440 |
| } |
|
441 |
7878
| bound.union(newBound);
|
|
442 |
7878
| |