Clover Coverage Report
Coverage timestamp: Fri Feb 14 2014 07:28:57 UTC
../../../../../../img/srcFileCovDistChart7.png 74% of files have more coverage
258   665   83   10.75
110   424   0.32   24
24     3.46  
1    
 
  DynamicInterpreter       Line # 42 258 83 70.2% 0.70153064
 
  (90)
 
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.se.base.list.Element;
30    import org.qedeq.kernel.se.base.list.ElementList;
31    import org.qedeq.kernel.se.base.module.FunctionDefinition;
32    import org.qedeq.kernel.se.base.module.PredicateDefinition;
33    import org.qedeq.kernel.se.common.DefaultModuleAddress;
34    import org.qedeq.kernel.se.common.ModuleContext;
35   
36   
37    /**
38    * This class calculates a new truth value for a given formula for a given interpretation.
39    *
40    * @author Michael Meyling
41    */
 
42    public class DynamicInterpreter {
43   
44    /** This class. */
45    private static final Class CLASS = DynamicInterpreter.class;
46   
47    /** Model contains entities, functions, predicates. */
48    private final DynamicModel model;
49   
50    /** Interpret subject variables. */
51    private final SubjectVariableInterpreter subjectVariableInterpreter;
52   
53    /** Interpret predicate variables. */
54    private final PredicateVariableInterpreter predicateVariableInterpreter;
55   
56    /** Interpret function variables. */
57    private final FunctionVariableInterpreter functionVariableInterpreter;
58   
59    /** Module context. Here were are currently. */
60    private ModuleContext moduleContext;
61   
62    /** For formatting debug trace output. */
63    private final StringBuffer deepness = new StringBuffer();
64   
65    /** QEDEQ module we check. */
66    private final KernelQedeqBo qedeq;
67   
68    /**
69    * Constructor.
70    *
71    * @param model We work with this model.
72    * @param qedeq And we work within this module.
73    */
 
74  93 toggle public DynamicInterpreter(final DynamicModel model, final KernelQedeqBo qedeq) {
75  93 this.model = model;
76  93 subjectVariableInterpreter = new SubjectVariableInterpreter(model);
77  93 predicateVariableInterpreter = new PredicateVariableInterpreter(model);
78  93 functionVariableInterpreter = new FunctionVariableInterpreter(model);
79  93 this.qedeq = qedeq;
80    }
81   
82    /**
83    * Get model.
84    *
85    * @return Model we work with.
86    */
 
87  0 toggle public DynamicModel getModel() {
88  0 return model;
89    }
90   
91    /**
92    * Add new predicate constant to this model.
93    *
94    * @param constant This is the predicate constant.
95    * @param variables Variables to use.
96    * @param formula Formula to evaluate for that predicate.
97    */
 
98  3 toggle public void addPredicateConstant(final ModelPredicateConstant constant,
99    final List variables, final ElementList formula) {
100  3 model.addPredicateConstant(constant, new Predicate(constant.getArgumentNumber(),
101    constant.getArgumentNumber(), "", "") {
 
102  55 toggle public boolean calculate(final Entity[] entities) {
103  108 for (int i = 0; i < entities.length; i++) {
104  53 final SubjectVariable var = (SubjectVariable) variables.get(i);
105  53 subjectVariableInterpreter.forceAddSubjectVariable(var, entities[i].getValue());
106    }
107  55 boolean result;
108  55 try {
109  55 result = calculateValue(new ModuleContext(new DefaultModuleAddress()), formula);
110    } catch (HeuristicException e) {
111  0 throw new RuntimeException(e); // LATER 20101014 m31: improve error handling
112    }
113  108 for (int i = entities.length - 1; i >= 0; i--) {
114  53 final SubjectVariable var = (SubjectVariable) variables.get(i);
115  53 subjectVariableInterpreter.forceRemoveSubjectVariable(var);
116    }
117    // System.out.print(constant.getName() + "(");
118    // for (int i = 0; i < entities.length; i++) {
119    // if (i > 0) {
120    // System.out.print(", ");
121    // }
122    // System.out.print(entities[i]);
123    // }
124    // System.out.println(") = " + result);
125  55 return result;
126    }
127    });
128    }
129   
130    /**
131    * Calculate predicate value.
132    *
133    * @param qedeq Predicate constant is defined in this QEDEQ module.
134    * @param constant This is the predicate definition.
135    * @param entities Predicate arguments.
136    * @return Result of calculation;
137    * @throws HeuristicException Calculation failed.
138    */
 
139  0 toggle public boolean calculatPredicateValue(final KernelQedeqBo qedeq, final PredicateDefinition constant,
140    final Entity[] entities) throws HeuristicException {
141  0 DynamicDirectInterpreter inter = new DynamicDirectInterpreter(qedeq, model,
142    subjectVariableInterpreter, predicateVariableInterpreter, functionVariableInterpreter);
143  0 return inter.calculatePredicateValue(
144    new PredicateConstant(new PredicateKey(constant.getName(), constant.getName()),
145    constant.getFormula().getElement().getList(),
146    new ModuleContext(qedeq.getModuleAddress())), entities);
147    }
148   
149    /**
150    * Add new function constant to this model.
151    *
152    * @param constant This is the predicate constant.
153    * @param variables Subject variables to use.
154    * @param term Formula to evaluate for that predicate.
155    */
 
156  0 toggle public void addFunctionConstant(final ModelFunctionConstant constant,
157    final List variables, final ElementList term) {
158  0 model.addFunctionConstant(constant, new Function(constant.getArgumentNumber(),
159    constant.getArgumentNumber(), "", "") {
 
160  0 toggle public Entity map(final Entity[] entities) {
161  0 for (int i = 0; i < entities.length; i++) {
162  0 final SubjectVariable var = (SubjectVariable) variables.get(i);
163  0 subjectVariableInterpreter.forceAddSubjectVariable(var, entities[i].getValue());
164    }
165  0 Entity result;
166  0 try {
167  0 result = calculateTerm(new ModuleContext(new DefaultModuleAddress()), term);
168    } catch (HeuristicException e) {
169  0 throw new RuntimeException(e); // LATER 20101014 m31: improve error handling
170    }
171  0 for (int i = entities.length - 1; i >= 0; i--) {
172  0 final SubjectVariable var = (SubjectVariable) variables.get(i);
173  0 subjectVariableInterpreter.forceRemoveSubjectVariable(var);
174    }
175  0 return result;
176    }
177    });
178    }
179   
180    /**
181    * Calculate function value.
182    *
183    * @param qedeq Function constant is defined in this QEDEQ module.
184    * @param constant This is the function definition.
185    * @param entities Function arguments.
186    * @return Result of calculation;
187    * @throws HeuristicException Calculation failed.
188    */
 
189  0 toggle public Entity calculateFunctionValue(final KernelQedeqBo qedeq, final FunctionDefinition constant,
190    final Entity[] entities) throws HeuristicException {
191  0 DynamicDirectInterpreter inter = new DynamicDirectInterpreter(qedeq, model,
192    subjectVariableInterpreter, predicateVariableInterpreter, functionVariableInterpreter);
193  0 return inter.calculateFunctionValue(
194    new FunctionConstant(new FunctionKey(constant.getName(), constant.getName()),
195    constant.getFormula().getElement().getList(),
196    new ModuleContext(qedeq.getModuleAddress())), entities);
197    }
198   
199    /**
200    * Is the given predicate constant already defined?
201    *
202    * @param constant Predicate constant to check for.
203    * @return Is the given predicate constant already defined?
204    */
 
205  0 toggle public boolean hasPredicateConstant(final ModelPredicateConstant constant) {
206  0 return null != model.getPredicateConstant(constant);
207    }
208   
209    /**
210    * Is the given function constant already defined?
211    *
212    * @param constant Function constant to check for.
213    * @return Is the given function constant already defined?
214    */
 
215  0 toggle public boolean hasFunctionConstant(final ModelFunctionConstant constant) {
216  0 return null != model.getFunctionConstant(constant);
217    }
218   
219    /**
220    * Calculate the truth value of a given formula is a tautology. This is done by checking with
221    * a model and certain variable values.
222    *
223    * @param moduleContext Where we are within an module.
224    * @param formula Formula.
225    * @return Truth value of formula.
226    * @throws HeuristicException We couldn't calculate the value.
227    */
 
228  5280 toggle public boolean calculateValue(final ModuleContext moduleContext, final Element formula)
229    throws HeuristicException {
230    // this.startElement = formula;
231  5280 this.moduleContext = moduleContext;
232    // this.startContext = new ModuleContext(moduleContext);
233  5280 return calculateValue(formula);
234    }
235   
236    /**
237    * Calculate the truth value of a given formula is a tautology. This is done by checking with
238    * a model and certain variable values.
239    *
240    * @param formula Formula.
241    * @return Truth value of formula.
242    * @throws HeuristicException We couldn't calculate the value.
243    */
 
244  483041 toggle private boolean calculateValue(final Element formula) throws HeuristicException {
245  483041 final String method = "calculateValue(Element)";
246    // System.out.println(deepness.toString() + Latex2Utf8Parser.transform(null,
247    // qedeq.getElement2Latex().getLatex(formula), 0));
248    // deepness.append("-");
249  483041 if (Trace.isDebugEnabled(CLASS)) {
250  0 Trace.param(CLASS, this, method, deepness.toString() + "formula", formula);
251  0 deepness.append("-");
252    }
253  483041 if (formula.isAtom()) {
254  0 throw new HeuristicException(HeuristicErrorCodes.WRONG_CALLING_CONVENTION_CODE,
255    HeuristicErrorCodes.WRONG_CALLING_CONVENTION_TEXT, moduleContext);
256    }
257  483041 final String context = getLocationWithinModule();
258  483041 setLocationWithinModule(context + ".getList()");
259  483041 final ElementList list = formula.getList();
260  483041 final String op = list.getOperator();
261  483041 boolean result;
262  483041 if (Operators.CONJUNCTION_OPERATOR.equals(op)) {
263  20372 result = true;
264  83964 for (int i = 0; i < list.size(); i++) {
265  63592 setLocationWithinModule(context + ".getList().getElement(" + i + ")");
266  63592 result &= calculateValue(list.getElement(i));
267    }
268  462669 } else if (Operators.DISJUNCTION_OPERATOR.equals(op)) {
269  22184 result = false;
270  82936 for (int i = 0; i < list.size(); i++) {
271  60752 setLocationWithinModule(context + ".getList().getElement(" + i + ")");
272  60752 result |= calculateValue(list.getElement(i));
273    }
274  440485 } else if (Operators.EQUIVALENCE_OPERATOR.equals(op)) {
275  26592 result = true;
276  26592 boolean value = false;
277  79776 for (int i = 0; i < list.size(); i++) {
278  53184 if (i > 0) {
279  26592 setLocationWithinModule(context + ".getList().getElement(" + i + ")");
280  26592 if (value != calculateValue(list.getElement(i))) {
281  4708 result = false;
282    }
283    } else {
284  26592 setLocationWithinModule(context + ".getList().getElement(" + i + ")");
285  26592 value = calculateValue(list.getElement(i));
286    }
287    }
288  413893 } else if (Operators.IMPLICATION_OPERATOR.equals(op)) {
289  49890 result = false;
290  149670 for (int i = 0; i < list.size(); i++) {
291  99780 setLocationWithinModule(context + ".getList().getElement(" + i + ")");
292  99780 if (i < list.size() - 1) {
293  49890 result |= !calculateValue(list.getElement(i));
294    } else {
295  49890 result |= calculateValue(list.getElement(i));
296    }
297    }
298  364003 } else if (Operators.NEGATION_OPERATOR.equals(op)) {
299  372 result = true;
300  744 for (int i = 0; i < list.size(); i++) {
301  372 setLocationWithinModule(context + ".getList().getElement(" + i + ")");
302  372 result &= !calculateValue(list.getElement(i));
303    }
304  363631 } else if (Operators.PREDICATE_VARIABLE.equals(op)) {
305  283599 final PredicateVariable var = new PredicateVariable(list.getElement(0).getAtom().getString(),
306    list.size() - 1);
307  283599 setLocationWithinModule(context + ".getList()");
308  283599 result = predicateVariableInterpreter.getPredicate(var)
309    .calculate(getEntities(list));
310  80032 } else if (Operators.UNIVERSAL_QUANTIFIER_OPERATOR.equals(op)) {
311  52071 result = handleUniversalQuantifier(list);
312  27961 } else if (Operators.EXISTENTIAL_QUANTIFIER_OPERATOR.equals(op)) {
313  27194 result = handleExistentialQuantifier(list);
314  767 } else if (Operators.UNIQUE_EXISTENTIAL_QUANTIFIER_OPERATOR.equals(op)) {
315  11 result = handleUniqueExistentialQuantifier(list);
316  756 } else if (Operators.PREDICATE_CONSTANT.equals(op)) {
317  756 final String text = stripReference(list.getElement(0).getAtom().getString());
318  756 final ModelPredicateConstant var = new ModelPredicateConstant(text,
319    list.size() - 1);
320  756 Predicate predicate = model.getPredicateConstant(var);
321  756 if (predicate == null) {
322   
323  0 setLocationWithinModule(context + ".getList().getOperator()");
324  0 throw new HeuristicException(HeuristicErrorCodes.UNKNOWN_PREDICATE_CONSTANT_CODE,
325    HeuristicErrorCodes.UNKNOWN_PREDICATE_CONSTANT_TEXT + var, moduleContext);
326    }
327  756 setLocationWithinModule(context + ".getList()");
328  756 result = predicate.calculate(getEntities(list));
329    } else {
330  0 setLocationWithinModule(context + ".getList().getOperator()");
331  0 throw new HeuristicException(HeuristicErrorCodes.UNKNOWN_OPERATOR_CODE,
332    HeuristicErrorCodes.UNKNOWN_OPERATOR_TEXT + op, moduleContext);
333    }
334  483041 setLocationWithinModule(context);
335  483041 if (Trace.isDebugEnabled(CLASS)) {
336  0 deepness.setLength(deepness.length() > 0 ? deepness.length() - 1 : 0);
337  0 Trace.param(CLASS, this, method, deepness.toString() + "result ", result);
338    }
339    // deepness.setLength(deepness.length() > 0 ? deepness.length() - 1 : 0);
340    // System.out.print(deepness.toString() + Latex2Utf8Parser.transform(null,
341    // qedeq.getElement2Latex().getLatex(formula), 0));
342    // System.out.println("=" + result);
343  483041 return result;
344    }
345   
346    /**
347    * Handle universal quantifier operator.
348    *
349    * @param list Work on this formula.
350    * @return result Calculated truth value.
351    * @throws HeuristicException Calculation not possible.
352    */
 
353  52071 toggle private boolean handleUniversalQuantifier(final ElementList list) throws HeuristicException {
354  52071 final String context = getLocationWithinModule();
355  52071 boolean result = true;
356  52071 final ElementList variable = list.getElement(0).getList();
357  52071 final SubjectVariable var = new SubjectVariable(variable.getElement(0).getAtom().getString());
358  52071 subjectVariableInterpreter.addSubjectVariable(var);
359  127735 for (int i = 0; i < model.getEntitiesSize(); i++) {
360    // System.out.print(deepness.toString() + Latex2Utf8Parser.transform(null,
361    // qedeq.getElement2Latex().getLatex(variable), 0));
362    // System.out.println("=" + model.getEntity(i));
363   
364  115901 if (list.size() == 2) {
365  115330 setLocationWithinModule(context + ".getList().getElement(1)");
366  115330 result &= calculateValue(list.getElement(1));
367    } else { // must be 3
368  571 setLocationWithinModule(context + ".getList().getElement(1)");
369  571 final boolean result1 = calculateValue(list.getElement(1));
370  571 setLocationWithinModule(context + ".getList().getElement(2)");
371  571 final boolean result2 = calculateValue(list.getElement(2));
372  571 result &= !result1 || result2;
373    }
374  115901 if (!result) {
375  40237 break;
376    }
377  75664 subjectVariableInterpreter.increaseSubjectVariableSelection(var);
378    }
379  52071 subjectVariableInterpreter.removeSubjectVariable(var);
380  52071 return result;
381    }
382   
383    /**
384    * Handle existential quantifier operator.
385    *
386    * @param list Work on this formula.
387    * @return result Calculated truth value.
388    * @throws HeuristicException Calculation not possible.
389    */
 
390  27194 toggle private boolean handleExistentialQuantifier(final ElementList list) throws HeuristicException {
391  27194 final String context = getLocationWithinModule();
392  27194 boolean result = false;
393  27194 final ElementList variable = list.getElement(0).getList();
394  27194 final SubjectVariable var = new SubjectVariable(variable.getElement(0).getAtom().getString());
395  27194 subjectVariableInterpreter.addSubjectVariable(var);
396  91792 for (int i = 0; i < model.getEntitiesSize(); i++) {
397    // System.out.print(deepness.toString() + Latex2Utf8Parser.transform(null,
398    // qedeq.getElement2Latex().getLatex(variable), 0));
399    // System.out.println("=" + model.getEntity(i));
400  83521 if (list.size() == 2) {
401  83521 setLocationWithinModule(context + ".getList().getElement(1)");
402  83521 result |= calculateValue(list.getElement(1));
403    } else { // must be 3
404  0 setLocationWithinModule(context + ".getList().getElement(1)");
405  0 final boolean result1 = calculateValue(list.getElement(1));
406  0 setLocationWithinModule(context + ".getList().getElement(2)");
407  0 final boolean result2 = calculateValue(list.getElement(2));
408  0 result |= result1 && result2;
409    }
410  83521 if (result) {
411  18923 break;
412    }
413  64598 subjectVariableInterpreter.increaseSubjectVariableSelection(var);
414    }
415  27194 subjectVariableInterpreter.removeSubjectVariable(var);
416  27194 return result;
417    }
418   
419    /**
420    * Handle unique existential quantifier operator.
421    *
422    * @param list Work on this formula.
423    * @return result Calculated truth value.
424    * @throws HeuristicException Calculation not possible.
425    */
 
426  11 toggle private boolean handleUniqueExistentialQuantifier(final ElementList list) throws HeuristicException {
427  11 final String context = getLocationWithinModule();
428  11 boolean result = false;
429  11 final ElementList variable = list.getElement(0).getList();
430  11 final SubjectVariable var = new SubjectVariable(variable.getElement(0).getAtom().getString());
431  11 subjectVariableInterpreter.addSubjectVariable(var);
432  58 for (int i = 0; i < model.getEntitiesSize(); i++) {
433  49 boolean val;
434  49 if (list.size() == 2) {
435  49 setLocationWithinModule(context + ".getList().getElement(1)");
436  49 val = calculateValue(list.getElement(1));
437    } else { // must be 3
438  0 setLocationWithinModule(context + ".getList().getElement(1)");
439  0 final boolean result1 = calculateValue(list.getElement(1));
440  0 setLocationWithinModule(context + ".getList().getElement(2)");
441  0 final boolean result2 = calculateValue(list.getElement(2));
442  0 val = result1 && result2;
443    }
444  49 if (val) {
445  13 if (result) {
446  2 result = false;
447  2 break;
448    }
449  11 result = true;
450    }
451  47 subjectVariableInterpreter.increaseSubjectVariableSelection(var);
452    }
453  11 subjectVariableInterpreter.removeSubjectVariable(var);
454  11 return result;
455    }
456   
457    /**
458    * Interpret terms.
459    *
460    * @param terms Interpret these terms. The first entry is stripped.
461    * @return Values.
462    * @throws HeuristicException evaluation failed.
463    */
 
464  284723 toggle private Entity[] getEntities(final ElementList terms)
465    throws HeuristicException {
466  284723 final String context = getLocationWithinModule();
467  284723 final Entity[] result = new Entity[terms.size() - 1]; // strip first argument
468  561547 for (int i = 0; i < result.length; i++) {
469  276824 setLocationWithinModule(context + ".getElement(" + (i + 1) + ")");
470  276824 result[i] = calculateTerm(terms.getElement(i + 1));
471    }
472  284723 setLocationWithinModule(context);
473  284723 return result;
474    }
475   
476    /**
477    * Calculate the term value of a given term. This is done by checking with
478    * a model and certain variable values.
479    *
480    * @param moduleContext Where we are within an module.
481    * @param term Term.
482    * @return Entity of model.
483    * @throws HeuristicException We couldn't calculate the value.
484    */
 
485  0 toggle public Entity calculateTerm(final ModuleContext moduleContext, final Element term)
486    throws HeuristicException {
487    // this.startElement = formula;
488  0 this.moduleContext = moduleContext;
489    // this.startContext = new ModuleContext(moduleContext);
490  0 return calculateTerm(term);
491    }
492   
493    /**
494    * Interpret term.
495    *
496    * @param term Interpret this term.
497    * @return Value.
498    * @throws HeuristicException evaluation failed.
499    */
 
500  276824 toggle private Entity calculateTerm(final Element term)
501    throws HeuristicException {
502  276824 final String method = "calculateTerm(Element) ";
503  276824 if (Trace.isDebugEnabled(CLASS)) {
504  0 Trace.param(CLASS, this, method, deepness.toString() + "term ", term);
505  0 deepness.append("-");
506    }
507  276824 if (!term.isList()) {
508  0 throw new RuntimeException("a term should be a list: " + term);
509    }
510  276824 final String context = getLocationWithinModule();
511  276824 final ElementList termList = term.getList();
512  276824 final String op = termList.getOperator();
513  276824 Entity result = null;
514  276824 if (Operators.SUBJECT_VARIABLE.equals(op)) {
515  276443 final String text = termList.getElement(0).getAtom().getString();
516  276443 final SubjectVariable var = new SubjectVariable(text);
517  276443 result = subjectVariableInterpreter.getEntity(var);
518  381 } else if (Operators.FUNCTION_VARIABLE.equals(op)) {
519  368 final FunctionVariable var = new FunctionVariable(termList.getElement(0).getAtom().getString(),
520    termList.size() - 1);
521  368 Function function = functionVariableInterpreter.getFunction(var);
522  368 setLocationWithinModule(context + ".getList()");
523  368 result = function.map(getEntities(termList));
524  13 } else if (Operators.FUNCTION_CONSTANT.equals(op)) {
525  0 final String label = termList.getElement(0).getAtom().getString();
526  0 if (label.indexOf(".") >= 0) {
527  0 final String shortName = label.substring(label.indexOf(".") + 1);
528  0 final String external = label.substring(0, label.indexOf("."));
529  0 if (qedeq.getKernelRequiredModules() != null) {
530  0 final KernelQedeqBo newProp = qedeq.getKernelRequiredModules().getKernelQedeqBo(external);
531  0 if (newProp != null) {
532  0 FunctionDefinition definition = newProp.getLabels().getFunction(shortName,
533    termList.size() - 1);
534  0 if (definition != null) {
535  0 setLocationWithinModule(context + ".getList()");
536  0 result = calculateFunctionValue(qedeq, definition, getEntities(termList));
537    }
538    } else {
539  0 setLocationWithinModule(context + ".getList().getOperator()");
540  0 throw new HeuristicException(HeuristicErrorCodes.UNKNOWN_FUNCTION_CONSTANT_CODE,
541    HeuristicErrorCodes.UNKNOWN_FUNCTION_CONSTANT_TEXT + shortName, moduleContext);
542    }
543    } else {
544  0 setLocationWithinModule(context + ".getList().getOperator()");
545  0 throw new HeuristicException(HeuristicErrorCodes.UNKNOWN_FUNCTION_CONSTANT_CODE,
546    HeuristicErrorCodes.UNKNOWN_FUNCTION_CONSTANT_TEXT + label, moduleContext);
547    }
548    } else {
549  0 final ModelFunctionConstant var = new ModelFunctionConstant(label,
550    termList.size() - 1);
551  0 Function function = model.getFunctionConstant(var);
552  0 if (function == null) {
553  0 setLocationWithinModule(context + ".getList().getOperator()");
554  0 throw new HeuristicException(HeuristicErrorCodes.UNKNOWN_FUNCTION_CONSTANT_CODE,
555    HeuristicErrorCodes.UNKNOWN_FUNCTION_CONSTANT_TEXT + var, moduleContext);
556    }
557  0 setLocationWithinModule(context + ".getList()");
558  0 result = function.map(getEntities(termList));
559    }
560  13 } else if (Operators.CLASS_OP.equals(op)) {
561  13 List fullfillers = new ArrayList();
562  13 ElementList variable = termList.getElement(0).getList();
563  13 final SubjectVariable var = new SubjectVariable(variable.getElement(0).getAtom().getString());
564  13 subjectVariableInterpreter.addSubjectVariable(var);
565  13 final ModelPredicateConstant isSetPredicate = new ModelPredicateConstant("isSet", 1);
566  13 Predicate isSet = model.getPredicateConstant(isSetPredicate);
567  13 if (isSet == null) {
568  0 throw new HeuristicException(HeuristicErrorCodes.UNKNOWN_TERM_OPERATOR_CODE,
569    HeuristicErrorCodes.UNKNOWN_TERM_OPERATOR_TEXT + "isSet(*)", moduleContext);
570    }
571  52 for (int i = 0; i < model.getEntitiesSize(); i++) {
572  39 setLocationWithinModule(context + ".getList().getElement(1)");
573  39 if (calculateValue(termList.getElement(1))
574    && isSet.calculate(new Entity[] {model.getEntity(i)})) {
575  13 fullfillers.add(model.getEntity(i));
576    }
577  39 subjectVariableInterpreter.increaseSubjectVariableSelection(var);
578    }
579  13 result = model.comprehension((Entity[]) fullfillers.toArray(new Entity[] {}));
580  13 subjectVariableInterpreter.removeSubjectVariable(var);
581    } else {
582  0 setLocationWithinModule(context + ".getList().getOperator()");
583  0 throw new HeuristicException(HeuristicErrorCodes.UNKNOWN_TERM_OPERATOR_CODE,
584    HeuristicErrorCodes.UNKNOWN_TERM_OPERATOR_TEXT + op, moduleContext);
585    }
586  276824 setLocationWithinModule(context);
587  276824 if (Trace.isDebugEnabled(CLASS)) {
588  0 deepness.setLength(deepness.length() > 0 ? deepness.length() - 1 : 0);
589  0 Trace.param(CLASS, this, method, deepness.toString() + "result ", result);
590    }
591  276824 return result;
592    }
593   
 
594  1123864 toggle private String getLocationWithinModule() {
595  1123864 return moduleContext.getLocationWithinModule();
596    }
597   
 
598  2566937 toggle private void setLocationWithinModule(final String localContext) {
599  2566937 moduleContext.setLocationWithinModule(localContext);
600    // if (localContext.equals(startContext.getLocationWithinModule())) {
601    // return;
602    // }
603    // String position
604    // = moduleContext.getLocationWithinModule().substring(startContext.getLocationWithinModule().length());
605    // if (position.startsWith(".")) {
606    // position = position.substring(1);
607    // }
608    // try {
609    // DynamicGetter.get(startElement, position);
610    // } catch (IllegalAccessException e) {
611    // System.out.println(position);
612    // e.printStackTrace(System.out);
613    // } catch (InvocationTargetException e) {
614    // System.out.println(position);
615    // e.printStackTrace(System.out);
616    // } catch (RuntimeException e) {
617    // System.out.println(position);
618    // e.printStackTrace(System.out);
619    // throw e;
620    // }
621    }
622   
623    /**
624    * Change to next valuation.
625    *
626    * @return Is there a next new valuation?
627    */
 
628  5185 toggle public boolean next() {
629  5185 return subjectVariableInterpreter.next() || predicateVariableInterpreter.next()
630    || functionVariableInterpreter.next();
631    }
632   
 
633  0 toggle public String toString() {
634  0 final StringBuffer buffer = new StringBuffer();
635  0 buffer.append("Current interpretation:\n");
636  0 buffer.append("\t" + predicateVariableInterpreter + "\n");
637  0 buffer.append("\t" + subjectVariableInterpreter + "\n");
638  0 buffer.append("\t" + functionVariableInterpreter);
639  0 return buffer.toString();
640    }
641   
642    /**
643    * Strips the reference to external modules.
644    *
645    * @param operator Might have reference to an external module.
646    * @return Operator as local reference.
647    */
 
648  756 toggle public String stripReference(final String operator) {
649  756 final int index = operator.lastIndexOf(".");
650  756 if (index >= 0 && index + 1 < operator.length()) {
651  9 return operator.substring(index + 1);
652    }
653  747 return operator;
654    }
655   
656    /**
657    * Clear all variable settings.
658    */
 
659  45 toggle public void clearVariables() {
660  45 subjectVariableInterpreter.clear();
661  45 predicateVariableInterpreter.clear();
662  45 functionVariableInterpreter.clear();
663    }
664   
665    }