Clover Coverage Report
Coverage timestamp: Fri Feb 14 2014 01:47:57 UTC
../../../../../../img/srcFileCovDistChart8.png 45% of files have more coverage
326   723   97   14.17
130   524   0.3   23
23     4.22  
1    
 
  DynamicDirectInterpreter       Line # 40 326 97 76.6% 0.76617956
 
  (52)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2014, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.kernel.bo.logic.model;
17   
18    import java.util.ArrayList;
19    import java.util.List;
20   
21    import org.qedeq.base.trace.Trace;
22    import org.qedeq.kernel.bo.logic.common.FunctionConstant;
23    import org.qedeq.kernel.bo.logic.common.FunctionKey;
24    import org.qedeq.kernel.bo.logic.common.Operators;
25    import org.qedeq.kernel.bo.logic.common.PredicateConstant;
26    import org.qedeq.kernel.bo.logic.common.PredicateKey;
27    import org.qedeq.kernel.bo.logic.common.SubjectVariable;
28    import org.qedeq.kernel.bo.module.KernelQedeqBo;
29    import org.qedeq.kernel.bo.service.unicode.Latex2UnicodeParser;
30    import org.qedeq.kernel.se.base.list.Element;
31    import org.qedeq.kernel.se.base.list.ElementList;
32    import org.qedeq.kernel.se.common.ModuleContext;
33   
34   
35    /**
36    * This class calculates a new truth value for a given formula for a given interpretation.
37    *
38    * @author Michael Meyling
39    */
 
40    public class DynamicDirectInterpreter {
41   
42    /** This class. */
43    private static final Class CLASS = DynamicDirectInterpreter.class;
44   
45    /** We work with this module. */
46    private KernelQedeqBo qedeq;
47   
48    /** Module context. Here were are currently. */
49    private ModuleContext moduleContext;
50   
51    /** For formatting debug trace output. */
52    private final StringBuffer deepness = new StringBuffer();
53   
54    /** Model contains entities, functions, predicates. */
55    private final DynamicModel model;
56   
57    /** Interpret subject variables. */
58    private final SubjectVariableInterpreter subjectVariableInterpreter;
59   
60    /** Interpret predicate variables. */
61    private final PredicateVariableInterpreter predicateVariableInterpreter;
62   
63    /** Interpret function variables. */
64    private final FunctionVariableInterpreter functionVariableInterpreter;
65   
66    /**
67    * Constructor.
68    *
69    * @param qedeq We work with this module.
70    * @param model We work with this model.
71    */
 
72  52 toggle public DynamicDirectInterpreter(final KernelQedeqBo qedeq, final DynamicModel model) {
73  52 this(qedeq, model, new SubjectVariableInterpreter(model), new PredicateVariableInterpreter(
74    model), new FunctionVariableInterpreter(model));
75    }
76   
77    /**
78    * Constructor.
79    *
80    * @param qedeq We work with this module.
81    * @param model We work with this model.
82    * @param subjectVariableInterpreter Subject variable interpreter.
83    * @param predicateVariableInterpreter Predicate variable interpreter.
84    * @param functionVariableInterpreter Function variable interpreter.
85    */
 
86  52 toggle public DynamicDirectInterpreter(final KernelQedeqBo qedeq, final DynamicModel model,
87    final SubjectVariableInterpreter subjectVariableInterpreter,
88    final PredicateVariableInterpreter predicateVariableInterpreter,
89    final FunctionVariableInterpreter functionVariableInterpreter) {
90  52 this.qedeq = qedeq;
91  52 this.model = model;
92  52 this.subjectVariableInterpreter = subjectVariableInterpreter;
93  52 this.predicateVariableInterpreter = predicateVariableInterpreter;
94  52 this.functionVariableInterpreter = functionVariableInterpreter;
95    }
96   
97    /**
98    * Calculate function value.
99    *
100    * @param constant This is the function definition.
101    * @param entities Function arguments.
102    * @return Result of calculation;
103    * @throws HeuristicException Calculation of function value failed.
104    */
 
105  6504 toggle public Entity calculateFunctionValue(final FunctionConstant constant,
106    final Entity[] entities) throws HeuristicException {
107  6504 final List params = constant.getSubjectVariables();
108  17404 for (int i = 0; i < entities.length; i++) {
109  10900 final SubjectVariable var = (SubjectVariable) params.get(i);
110  10900 subjectVariableInterpreter.forceAddSubjectVariable(var, entities[i].getValue());
111    }
112  6504 Entity result;
113  6504 try {
114  6504 result = calculateTerm(constant.getDefiningTerm());
115    } finally {
116  17404 for (int i = entities.length - 1; i >= 0; i--) {
117  10900 final SubjectVariable var = (SubjectVariable) params.get(i);
118  10900 subjectVariableInterpreter.forceRemoveSubjectVariable(var);
119    }
120    }
121  6504 return result;
122    }
123   
124    /**
125    * Calculate function value.
126    *
127    * @param constant This is the predicate definition.
128    * @param entities Function arguments.
129    * @return Result of calculation;
130    * @throws HeuristicException Calculation failed.
131    */
 
132  12842 toggle public boolean calculatePredicateValue(final PredicateConstant constant,
133    final Entity[] entities) throws HeuristicException {
134  12842 final List params = constant.getSubjectVariables();
135  31646 for (int i = 0; i < entities.length; i++) {
136  18804 final SubjectVariable var = (SubjectVariable) params.get(i);
137  18804 subjectVariableInterpreter.forceAddSubjectVariable(var, entities[i].getValue());
138    }
139  12842 boolean result;
140  12842 try {
141  12842 result = calculateValue(constant.getDefiningFormula());
142    } finally {
143  31646 for (int i = entities.length - 1; i >= 0; i--) {
144  18804 final SubjectVariable var = (SubjectVariable) params.get(i);
145  18804 subjectVariableInterpreter.forceRemoveSubjectVariable(var);
146    }
147    }
148  12842 return result;
149    }
150   
151    /**
152    * Calculate the truth value of a given formula is a tautology. This is done by checking with
153    * a model and certain variable values.
154    *
155    * @param moduleContext Where we are within an module.
156    * @param formula Formula.
157    * @return Truth value of formula.
158    * @throws HeuristicException We couldn't calculate the value.
159    */
 
160  1583 toggle public boolean calculateValue(final ModuleContext moduleContext, final Element formula)
161    throws HeuristicException {
162    // this.startElement = formula;
163  1583 this.moduleContext = new ModuleContext(moduleContext);
164    // this.startContext = new ModuleContext(moduleContext);
165  1583 return calculateValue(formula);
166    }
167   
168    /**
169    * Calculate the truth value of a given formula is a tautology. This is done by checking with
170    * a model and certain variable values.
171    *
172    * @param formula Formula.
173    * @return Truth value of formula.
174    * @throws HeuristicException We couldn't calculate the value.
175    */
 
176  236342 toggle private boolean calculateValue(final Element formula) throws HeuristicException {
177  236342 final String method = "calculateValue(Element)";
178  236342 if (Trace.isDebugEnabled(CLASS)) {
179  0 Trace.param(CLASS, this, method, deepness.toString() + "formula",
180    Latex2UnicodeParser.transform(null, qedeq.getElement2Latex().getLatex(formula), 0));
181  0 deepness.append("-");
182    }
183  236342 if (formula.isAtom()) {
184  0 throw new HeuristicException(HeuristicErrorCodes.WRONG_CALLING_CONVENTION_CODE,
185    HeuristicErrorCodes.WRONG_CALLING_CONVENTION_TEXT, moduleContext);
186    }
187  236342 final KernelQedeqBo qedeqOld = qedeq;
188  236342 final ModuleContext moduleContextOld = new ModuleContext(moduleContext);
189  236342 final String context = getLocationWithinModule();
190  236342 boolean result;
191  236342 try {
192  236342 final ElementList list = formula.getList();
193  236342 setLocationWithinModule(context + ".getList()");
194  236342 final String op = list.getOperator();
195  236342 if (Operators.CONJUNCTION_OPERATOR.equals(op)) {
196  28721 result = true;
197  77925 for (int i = 0; i < list.size() && result; i++) {
198  49204 setLocationWithinModule(context + ".getList().getElement(" + i + ")");
199  49204 result &= calculateValue(list.getElement(i));
200    }
201  207621 } else if (Operators.DISJUNCTION_OPERATOR.equals(op)) {
202  8254 result = false;
203  21658 for (int i = 0; i < list.size() && !result; i++) {
204  13404 setLocationWithinModule(context + ".getList().getElement(" + i + ")");
205  13404 result |= calculateValue(list.getElement(i));
206    }
207  199367 } else if (Operators.EQUIVALENCE_OPERATOR.equals(op)) {
208  11079 result = true;
209  11079 boolean value = false;
210  33237 for (int i = 0; i < list.size() && result; i++) {
211  22158 setLocationWithinModule(context + ".getList().getElement(" + i + ")");
212  22158 if (i > 0) {
213  11079 if (value != calculateValue(list.getElement(i))) {
214  1601 result = false;
215    }
216    } else {
217  11079 value = calculateValue(list.getElement(i));
218    }
219    }
220  188288 } else if (Operators.IMPLICATION_OPERATOR.equals(op)) {
221  19572 result = false;
222  45950 for (int i = 0; i < list.size() && !result; i++) {
223  26378 setLocationWithinModule(context + ".getList().getElement(" + i + ")");
224  26378 if (i < list.size() - 1) {
225  19572 result |= !calculateValue(list.getElement(i));
226    } else {
227  6806 result |= calculateValue(list.getElement(i));
228    }
229    }
230  168716 } else if (Operators.NEGATION_OPERATOR.equals(op)) {
231  1847 result = true;
232  3694 for (int i = 0; i < list.size() && result; i++) {
233  1847 setLocationWithinModule(context + ".getList().getElement(" + i + ")");
234  1847 result &= !calculateValue(list.getElement(i));
235    }
236  166869 } else if (Operators.PREDICATE_VARIABLE.equals(op)) {
237  70997 setLocationWithinModule(context + ".getList()");
238  70997 final Entity[] arguments = getEntities(list);
239  70997 final PredicateVariable var = new PredicateVariable(
240    list.getElement(0).getAtom().getString(), list.size() - 1);
241  70997 result = predicateVariableInterpreter.getPredicate(var)
242    .calculate(arguments);
243  95872 } else if (Operators.UNIVERSAL_QUANTIFIER_OPERATOR.equals(op)) {
244  18502 result = handleUniversalQuantifier(list);
245  77370 } else if (Operators.EXISTENTIAL_QUANTIFIER_OPERATOR.equals(op)) {
246  16913 result = handleExistentialQuantifier(list);
247  60457 } else if (Operators.UNIQUE_EXISTENTIAL_QUANTIFIER_OPERATOR.equals(op)) {
248  5 result = handleUniqueExistentialQuantifier(list);
249  60452 } else if (Operators.PREDICATE_CONSTANT.equals(op)) {
250  60452 final String label = list.getElement(0).getAtom().getString();
251  60452 String name = label;
252    // System.out.println(label);
253  60452 KernelQedeqBo newProp = qedeq;
254  66944 while (name.indexOf(".") >= 0) {
255  6492 name = name.substring(label.indexOf(".") + 1);
256  6492 final String external = label.substring(0, label.indexOf("."));
257  6492 newProp = null;
258  6492 if (qedeq.getKernelRequiredModules() != null) {
259  6492 newProp = qedeq.getKernelRequiredModules().getKernelQedeqBo(external);
260    }
261  6492 if (newProp == null) {
262  0 setLocationWithinModule(context + ".getList().getOperator()");
263  0 throw new HeuristicException(HeuristicErrorCodes.UNKNOWN_IMPORT_MODULE_CODE,
264    HeuristicErrorCodes.UNKNOWN_IMPORT_MODULE_TEXT + "\"" + external + "\""
265    + HeuristicErrorCodes.UNKNOWN_IMPORT_MODULE_TEXT_2 + "\"" + external
266    + "." + name + "\"",
267    moduleContext);
268    }
269    }
270  60452 final PredicateKey predicateKey = new PredicateKey(name, "" + (list.size() - 1));
271  60452 final PredicateConstant constant = (newProp.getExistenceChecker() != null
272    ? newProp.getExistenceChecker().get(predicateKey) : null);
273  60452 if (constant != null) {
274  6478 setLocationWithinModule(context + ".getList()");
275  6478 final Entity[] arguments = getEntities(list);
276  6478 setModuleContext(newProp);
277  6478 moduleContext = new ModuleContext(constant.getContext());
278    // we must get the second argument of equivalence
279  6478 moduleContext.setLocationWithinModule(moduleContext.getLocationWithinModule()
280    + ".getElement(1)");
281  6478 try {
282  6478 result = calculatePredicateValue(constant, arguments);
283    } catch (HeuristicException e) {
284  0 setModuleContext(qedeqOld);
285  0 moduleContext = moduleContextOld;
286  0 setLocationWithinModule(context + ".getList().getElement(1)");
287  0 throw new HeuristicException(
288    HeuristicErrorCodes.PREDICATE_CALCULATION_FAILED_CODE,
289    HeuristicErrorCodes.PREDICATE_CALCULATION_FAILED_TEXT + predicateKey,
290    moduleContext, e.getContext());
291    }
292    } else { // should be initial predicate, must be in the model
293  53974 final ModelPredicateConstant var = new ModelPredicateConstant(name, list.size()
294    - 1);
295  53974 final Predicate predicate = model.getPredicateConstant(var);
296  53974 if (predicate == null) {
297  0 setLocationWithinModule(context + ".getList().getOperator()");
298  0 throw new HeuristicException(
299    HeuristicErrorCodes.UNKNOWN_PREDICATE_CONSTANT_CODE,
300    HeuristicErrorCodes.UNKNOWN_PREDICATE_CONSTANT_TEXT + var,
301    moduleContext);
302    }
303  53974 setLocationWithinModule(context + ".getList()");
304  53974 final Entity[] arguments = getEntities(list);
305  53974 result = predicate.calculate(arguments);
306    }
307    } else {
308  0 setLocationWithinModule(context + ".getList().getOperator()");
309  0 throw new HeuristicException(HeuristicErrorCodes.UNKNOWN_OPERATOR_CODE,
310    HeuristicErrorCodes.UNKNOWN_OPERATOR_TEXT + op, moduleContext);
311    }
312    } finally {
313    // qedeq = qedeqOld;
314  236342 setModuleContext(qedeqOld);
315  236342 moduleContext = moduleContextOld;
316  236342 setLocationWithinModule(context);
317    }
318  236342 if (Trace.isDebugEnabled(CLASS)) {
319  0 deepness.setLength(deepness.length() > 0 ? deepness.length() - 1 : 0);
320  0 Trace.param(CLASS, this, method, deepness.toString() + Latex2UnicodeParser.transform(
321    null, qedeq.getElement2Latex().getLatex(formula), 0), result);
322    }
323  236342 return result;
324    }
325   
326    /**
327    * Handle universal quantifier operator.
328    *
329    * @param list Work on this formula.
330    * @return result Calculated truth value.
331    * @throws HeuristicException Calculation not possible.
332    */
 
333  18502 toggle private boolean handleUniversalQuantifier(final ElementList list)
334    throws HeuristicException {
335  18502 final String method = "handleUniversalQuantifier(ElementList)";
336  18502 final String context = getLocationWithinModule();
337  18502 boolean result = true;
338  18502 final ElementList variable = list.getElement(0).getList();
339  18502 final SubjectVariable var = new SubjectVariable(variable.getElement(0).getAtom().getString());
340  18502 subjectVariableInterpreter.addSubjectVariable(var);
341  42311 for (int i = 0; i < model.getEntitiesSize(); i++) {
342  37704 if (Trace.isDebugEnabled(CLASS)) {
343  0 Trace.param(CLASS, this, method, deepness.toString()
344    + Latex2UnicodeParser.transform(null, qedeq.getElement2Latex()
345    .getLatex(variable), 0), model.getEntity(i));
346    }
347  37704 if (list.size() == 2) {
348  37478 setLocationWithinModule(context + ".getElement(1)");
349  37478 result &= calculateValue(list.getElement(1));
350    } else { // must be 3
351  226 setLocationWithinModule(context + ".getElement(1)");
352  226 final boolean result1 = calculateValue(list.getElement(1));
353  226 setLocationWithinModule(context + ".getElement(2)");
354  226 final boolean result2 = calculateValue(list.getElement(2));
355  226 result &= !result1 || result2;
356    }
357  37704 if (!result) {
358  13895 break;
359    }
360  23809 subjectVariableInterpreter.increaseSubjectVariableSelection(var);
361    }
362  18502 subjectVariableInterpreter.removeSubjectVariable(var);
363  18502 return result;
364    }
365   
366    /**
367    * Handle existential quantifier operator.
368    *
369    * @param list Work on this formula.
370    * @return result Calculated truth value.
371    * @throws HeuristicException Calculation not possible.
372    */
 
373  16913 toggle private boolean handleExistentialQuantifier(final ElementList list)
374    throws HeuristicException {
375  16913 final String method = "handleExistentialQuantifier(ElementList)";
376  16913 final String context = getLocationWithinModule();
377  16913 boolean result = false;
378  16913 final ElementList variable = list.getElement(0).getList();
379  16913 final SubjectVariable var = new SubjectVariable(variable.getElement(0).getAtom().getString());
380  16913 subjectVariableInterpreter.addSubjectVariable(var);
381  49952 for (int i = 0; i < model.getEntitiesSize(); i++) {
382  44834 if (Trace.isDebugEnabled(CLASS)) {
383  0 Trace.param(CLASS, this, method, deepness.toString() + Latex2UnicodeParser
384    .transform(null, qedeq.getElement2Latex().getLatex(variable), 0),
385    model.getEntity(i));
386    }
387  44834 if (list.size() == 2) {
388  44834 setLocationWithinModule(context + ".getElement(1)");
389  44834 result |= calculateValue(list.getElement(1));
390    } else { // must be 3
391  0 setLocationWithinModule(context + ".getElement(1)");
392  0 final boolean result1 = calculateValue(list.getElement(1));
393  0 setLocationWithinModule(context + ".getElement(2)");
394  0 final boolean result2 = calculateValue(list.getElement(2));
395  0 result |= result1 && result2;
396    }
397  44834 if (result) {
398  11795 break;
399    }
400  33039 subjectVariableInterpreter.increaseSubjectVariableSelection(var);
401    }
402  16913 subjectVariableInterpreter.removeSubjectVariable(var);
403  16913 return result;
404    }
405   
406    /**
407    * Handle unique existential quantifier operator.
408    *
409    * @param list Work on this formula.
410    * @return result Calculated truth value.
411    * @throws HeuristicException Calculation not possible.
412    */
 
413  5 toggle private boolean handleUniqueExistentialQuantifier(final ElementList list)
414    throws HeuristicException {
415  5 final String method = "handleUniqueExistentialQuantifier(ElementList)";
416  5 final String context = getLocationWithinModule();
417  5 boolean result = false;
418  5 final ElementList variable = list.getElement(0).getList();
419  5 final SubjectVariable var = new SubjectVariable(variable.getElement(0).getAtom().getString());
420  5 subjectVariableInterpreter.addSubjectVariable(var);
421  22 for (int i = 0; i < model.getEntitiesSize(); i++) {
422  18 if (Trace.isDebugEnabled(CLASS)) {
423  0 Trace.param(CLASS, this, method, deepness.toString() + Latex2UnicodeParser.transform(null,
424    qedeq.getElement2Latex().getLatex(variable), 0), model.getEntity(i));
425    }
426  18 boolean val;
427  18 if (list.size() == 2) {
428  18 setLocationWithinModule(context + ".getList().getElement(1)");
429  18 val = calculateValue(list.getElement(1));
430    } else { // must be 3
431  0 setLocationWithinModule(context + ".getList().getElement(1)");
432  0 final boolean result1 = calculateValue(list.getElement(1));
433  0 setLocationWithinModule(context + ".getList().getElement(2)");
434  0 final boolean result2 = calculateValue(list.getElement(2));
435  0 val = result1 && result2;
436    }
437  18 if (val) {
438  6 if (result) {
439  1 result = false;
440  1 break;
441    }
442  5 result = true;
443    }
444  17 subjectVariableInterpreter.increaseSubjectVariableSelection(var);
445    }
446  5 subjectVariableInterpreter.removeSubjectVariable(var);
447  5 return result;
448    }
449   
450    /**
451    * Interpret terms.
452    *
453    * @param terms Interpret these terms. The first entry is stripped.
454    * @return Values.
455    * @throws HeuristicException evaluation failed.
456    */
 
457  138097 toggle private Entity[] getEntities(final ElementList terms)
458    throws HeuristicException {
459    // System.out.println(terms);
460  138097 final String context = getLocationWithinModule();
461  138097 final Entity[] result = new Entity[terms.size() - 1]; // strip first argument
462  347464 for (int i = 0; i < result.length; i++) {
463    // System.out.println(i + " " + terms.getElement(i + 1));
464  209367 setLocationWithinModule(context + ".getElement(" + (i + 1) + ")");
465  209367 result[i] = calculateTerm(terms.getElement(i + 1));
466    }
467  138097 setLocationWithinModule(context);
468  138097 return result;
469    }
470   
471    /**
472    * Calculate the term value of a given term. This is done by checking with
473    * a model and certain variable values.
474    *
475    * @param moduleContext Where we are within an module.
476    * @param term Term.
477    * @return Entity of model.
478    * @throws HeuristicException We couldn't calculate the value.
479    */
 
480  0 toggle public Entity calculateTerm(final ModuleContext moduleContext, final Element term)
481    throws HeuristicException {
482  0 this.moduleContext = moduleContext;
483  0 return calculateTerm(term);
484    }
485   
486    /**
487    * Interpret term.
488    *
489    * @param term Interpret this term.
490    * @return Value.
491    * @throws HeuristicException evaluation failed.
492    */
 
493  215871 toggle private Entity calculateTerm(final Element term)
494    throws HeuristicException {
495  215871 final String method = "calculateTerm(Element) ";
496  215871 if (Trace.isDebugEnabled(CLASS)) {
497  0 Trace.param(CLASS, this, method, deepness.toString() + "term ", Latex2UnicodeParser.transform(null,
498    qedeq.getElement2Latex().getLatex(term), 0));
499  0 deepness.append("-");
500    }
501  215871 if (!term.isList()) {
502  0 throw new RuntimeException("a term should be a list: " + term);
503    }
504  215871 final KernelQedeqBo qedeqOld = qedeq;
505  215871 final ModuleContext moduleContextOld = new ModuleContext(moduleContext);
506  215871 final String context = getLocationWithinModule();
507  215871 Entity result = null;
508  215871 try {
509  215871 final ElementList termList = term.getList();
510  215871 final String op = termList.getOperator();
511  215871 if (Operators.SUBJECT_VARIABLE.equals(op)) {
512  202687 final String text = termList.getElement(0).getAtom().getString();
513  202687 final SubjectVariable var = new SubjectVariable(text);
514  202687 result = subjectVariableInterpreter.getEntity(var);
515  13184 } else if (Operators.FUNCTION_VARIABLE.equals(op)) {
516  144 final FunctionVariable var = new FunctionVariable(termList.getElement(0).getAtom().getString(),
517    termList.size() - 1);
518  144 Function function = functionVariableInterpreter.getFunction(var);
519  144 setLocationWithinModule(context + ".getList()");
520  144 result = function.map(getEntities(termList));
521  13040 } else if (Operators.FUNCTION_CONSTANT.equals(op)) {
522  6504 final String label = termList.getElement(0).getAtom().getString();
523  6504 String name = label;
524  6504 KernelQedeqBo newProp = qedeq;
525  6504 if (label.indexOf(".") >= 0) {
526  0 name = label.substring(label.indexOf(".") + 1);
527  0 final String external = label.substring(0, label.indexOf("."));
528  0 newProp = null;
529  0 if (qedeq.getKernelRequiredModules() != null) {
530  0 newProp = qedeq.getKernelRequiredModules().getKernelQedeqBo(external);
531    }
532  0 if (newProp == null) {
533  0 setLocationWithinModule(context + ".getList().getOperator()");
534  0 throw new HeuristicException(HeuristicErrorCodes.UNKNOWN_IMPORT_MODULE_CODE,
535    HeuristicErrorCodes.UNKNOWN_IMPORT_MODULE_TEXT + "\"" + external + "\""
536    + HeuristicErrorCodes.UNKNOWN_IMPORT_MODULE_TEXT_2 + "\"" + label + "\"",
537    moduleContext);
538    }
539    }
540  6504 final FunctionKey functionKey = new FunctionKey(name, "" + (termList.size() - 1));
541  6504 FunctionConstant constant
542    = newProp.getExistenceChecker().get(functionKey);
543  6504 if (constant != null) {
544  6504 setLocationWithinModule(context + ".getList()");
545  6504 final Entity[] arguments = getEntities(termList);
546  6504 setModuleContext(newProp);
547  6504 moduleContext = new ModuleContext(constant.getContext());
548    // we must get the second argument of equal relation
549  6504 moduleContext.setLocationWithinModule(moduleContext.getLocationWithinModule() + ".getElement(2)");
550  6504 try {
551  6504 result = calculateFunctionValue(constant, arguments);
552    } catch (HeuristicException e) {
553  0 setModuleContext(qedeqOld);
554  0 moduleContext = moduleContextOld;
555  0 setLocationWithinModule(context + ".getList().getElement(1)");
556  0 throw new HeuristicException(HeuristicErrorCodes.PREDICATE_CALCULATION_FAILED_CODE,
557    HeuristicErrorCodes.PREDICATE_CALCULATION_FAILED_TEXT + functionKey,
558    moduleContext, e.getContext());
559    }
560    } else {
561  0 final ModelFunctionConstant var = new ModelFunctionConstant(name,
562    termList.size() - 1);
563  0 Function function = model.getFunctionConstant(var);
564  0 if (function == null) {
565  0 setLocationWithinModule(context + ".getList().getOperator()");
566  0 throw new HeuristicException(HeuristicErrorCodes.UNKNOWN_FUNCTION_CONSTANT_CODE,
567    HeuristicErrorCodes.UNKNOWN_FUNCTION_CONSTANT_TEXT + var, moduleContext);
568    }
569  0 setLocationWithinModule(context + ".getList()");
570  0 final Entity[] arguments = getEntities(termList);
571  0 result = function.map(arguments);
572    }
573  6536 } else if (Operators.CLASS_OP.equals(op)) {
574  6536 List fullfillers = new ArrayList();
575  6536 ElementList variable = termList.getElement(0).getList();
576  6536 final SubjectVariable var = new SubjectVariable(variable.getElement(0).getAtom().getString());
577  6536 subjectVariableInterpreter.addSubjectVariable(var);
578  6536 KernelQedeqBo newProp = qedeq;
579  6536 if (qedeq.getExistenceChecker() != null) {
580  6536 newProp = qedeq.getExistenceChecker().getClassOperatorModule();
581    }
582  6536 final PredicateConstant isSet = newProp.getExistenceChecker().getPredicate("isSet", 1);
583  6536 if (isSet == null) {
584  0 throw new HeuristicException(HeuristicErrorCodes.UNKNOWN_TERM_OPERATOR_CODE,
585    HeuristicErrorCodes.UNKNOWN_TERM_OPERATOR_TEXT + "isSet(*)", moduleContext);
586    }
587  32680 for (int i = 0; i < model.getEntitiesSize(); i++) {
588  26144 setModuleContext(qedeqOld);
589  26144 moduleContext = moduleContextOld;
590  26144 setLocationWithinModule(context + ".getList().getElement(1)");
591  26144 if (calculateValue(termList.getElement(1))) {
592  6364 setModuleContext(newProp);
593  6364 moduleContext = newProp.getLabels().getPredicateContext("isSet", 1);
594  6364 setLocationWithinModule(moduleContext.getLocationWithinModule()
595    + ".getFormula().getElement().getList().getElement(1)");
596  6364 try {
597  6364 if (calculatePredicateValue(isSet, new Entity[] {model.getEntity(i)})) {
598  4880 fullfillers.add(model.getEntity(i));
599    }
600    } catch (HeuristicException e) {
601  0 setModuleContext(qedeqOld);
602  0 moduleContext = moduleContextOld;
603  0 setLocationWithinModule(context + ".getList().getElement(1)");
604  0 throw new HeuristicException(HeuristicErrorCodes.PREDICATE_CALCULATION_FAILED_CODE,
605    HeuristicErrorCodes.PREDICATE_CALCULATION_FAILED_TEXT + isSet,
606    moduleContext, e.getContext());
607    }
608    }
609  26144 subjectVariableInterpreter.increaseSubjectVariableSelection(var);
610    }
611  6536 result = model.comprehension((Entity[]) fullfillers.toArray(new Entity[] {}));
612  6536 subjectVariableInterpreter.removeSubjectVariable(var);
613    } else {
614  0 setLocationWithinModule(context + ".getList().getOperator()");
615  0 throw new HeuristicException(HeuristicErrorCodes.UNKNOWN_TERM_OPERATOR_CODE,
616    HeuristicErrorCodes.UNKNOWN_TERM_OPERATOR_TEXT + op, moduleContext);
617    }
618    } finally {
619    // qedeq = qedeqOld;
620  215871 setModuleContext(qedeqOld);
621  215871 moduleContext = moduleContextOld;
622    // System.out.println("module context old: " + moduleContextOld);
623  215871 setLocationWithinModule(context);
624    }
625  215871 if (Trace.isDebugEnabled(CLASS)) {
626  0 deepness.setLength(deepness.length() > 0 ? deepness.length() - 1 : 0);
627  0 Trace.param(CLASS, this, method, deepness.toString() + Latex2UnicodeParser.transform(null,
628    qedeq.getElement2Latex().getLatex(term), 0), result);
629    }
630  215871 return result;
631    }
632   
 
633  625730 toggle private String getLocationWithinModule() {
634  625730 return moduleContext.getLocationWithinModule();
635    }
636   
 
637  1397904 toggle protected ModuleContext getModuleContext() {
638  1397904 return moduleContext;
639    }
640   
 
641  497703 toggle protected void setModuleContext(final KernelQedeqBo qedeq) {
642  497703 this.qedeq = qedeq;
643  497703 moduleContext = new ModuleContext(qedeq.getModuleAddress());
644    }
645   
 
646  1402397 toggle protected void setLocationWithinModule(final String localContext) {
647  1402397 moduleContext.setLocationWithinModule(localContext);
648    }
649   
650    /**
651    * Change to next valuation.
652    *
653    * @return Is there a next new valuation?
654    */
 
655  1564 toggle public boolean next() {
656  1564 return subjectVariableInterpreter.next() || predicateVariableInterpreter.next()
657    || functionVariableInterpreter.next();
658    }
659   
 
660  0 toggle public String toString() {
661  0 final StringBuffer buffer = new StringBuffer();
662  0 buffer.append("Current interpretation:\n");
663  0 buffer.append("\t" + predicateVariableInterpreter + "\n");
664  0 buffer.append("\t" + subjectVariableInterpreter + "\n");
665  0 buffer.append("\t" + functionVariableInterpreter);
666  0 return buffer.toString();
667    }
668   
669    /**
670    * Strips the reference to external modules.
671    *
672    * @param operator Might have reference to an external module.
673    * @return Operator as local reference.
674    */
 
675  0 toggle public String stripReference(final String operator) {
676  0 final int index = operator.lastIndexOf(".");
677  0 if (index >= 0 && index + 1 < operator.length()) {
678  0 return operator.substring(index + 1);
679    }
680  0 return operator;
681    }
682   
683   
684    /**
685    * Is the given predicate constant already defined?
686    *
687    * @param constant Predicate constant to check for.
688    * @return Is the given predicate constant already defined?
689    */
 
690  1 toggle public boolean hasPredicateConstant(final ModelPredicateConstant constant) {
691  1 return null != model.getPredicateConstant(constant);
692    }
693   
694    /**
695    * Is the given function constant already defined?
696    *
697    * @param constant Function constant to check for.
698    * @return Is the given function constant already defined?
699    */
 
700  0 toggle public boolean hasFunctionConstant(final ModelFunctionConstant constant) {
701  0 return null != model.getFunctionConstant(constant);
702    }
703   
704    /**
705    * Clear all variable settings.
706    */
 
707  3 toggle public void clearVariables() {
708  3 subjectVariableInterpreter.clear();
709  3 predicateVariableInterpreter.clear();
710  3 functionVariableInterpreter.clear();
711    }
712   
713    /**
714    * Get model.
715    *
716    * @return Model we work with.
717    */
 
718  0 toggle public DynamicModel getModel() {
719  0 return model;
720    }
721   
722   
723    }