Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../../../img/srcFileCovDistChart8.png 62% of files have more coverage
55   196   24   4.58
24   105   0.44   12
12     2  
1    
 
  SubjectVariableInterpreter       Line # 28 55 24 72.5% 0.72527474
 
  (284)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, 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.kernel.bo.logic.common.SubjectVariable;
22   
23    /**
24    * This class interpretation.
25    *
26    * @author Michael Meyling
27    */
 
28    public final class SubjectVariableInterpreter {
29   
30    /** List of subject variables allocations. */
31    private List subjectVariableAllocations;
32   
33    /** Model contains entities. */
34    private Model model;
35   
36    /**
37    * Constructor.
38    *
39    * @param model Model we work on.
40    */
 
41  494 toggle public SubjectVariableInterpreter(final Model model) {
42  494 this.model = model;
43  494 subjectVariableAllocations = new ArrayList();
44    }
45   
46    /**
47    * Change to next valuation.
48    *
49    * @return Is there a next new valuation?
50    */
 
51  12611 toggle public synchronized boolean next() {
52  12611 boolean next = true;
53  13663 for (int i = subjectVariableAllocations.size() - 1; i >= -1; i--) {
54  13663 if (i < 0) {
55  10291 next = false;
56  10291 break;
57    }
58  3372 final SubjectVariableAllocation allocation
59    = (SubjectVariableAllocation) subjectVariableAllocations.get(i);
60  3372 if (allocation.getValue() + 1 < model.getEntitiesSize()) {
61  2320 allocation.increaseNumber();
62  2320 break;
63    }
64  1052 allocation.resetNumber();
65    }
66  12611 return next;
67    }
68   
69    /**
70    * Add subject variable. This is usually done for interpreting a quantifier.
71    *
72    * @param var Subject variable to add to our interpretation.
73    */
 
74  177337 toggle public synchronized void addSubjectVariable(final SubjectVariable var) {
75    // if we forbid quantifing with same variable twice, we must activate the following
76    // if (subjectVariables.contains(var)) {
77    // throw new RuntimeException("variable already exists: " + var);
78    // }
79    // System.out.println("added subject variable " + var);
80  177337 subjectVariableAllocations.add(new SubjectVariableAllocation(var));
81    }
82   
83    /**
84    * Add subject variable even if already existing.
85    *
86    * @param var Remove this subject variable.
87    * @param value Set interpretation to this entity number.
88    */
 
89  29757 toggle public synchronized void forceAddSubjectVariable(final SubjectVariable var, final int value) {
90  29757 subjectVariableAllocations.add(new SubjectVariableAllocation(var, value));
91    }
92   
93    /**
94    * Remove existing subject variable interpretation.
95    *
96    * @param var Remove this subject variable.
97    */
 
98  29757 toggle public synchronized void forceRemoveSubjectVariable(final SubjectVariable var) {
99  29757 int index = getIndex(var);
100  29757 if (index < 0) {
101  0 throw new RuntimeException("variable does not exist: " + var);
102    }
103  29757 final SubjectVariableAllocation current
104    = (SubjectVariableAllocation) subjectVariableAllocations.get(index);
105  29757 if (!current.isFixed()) {
106  0 throw new RuntimeException("trying to remove not fixed allocation: " + current);
107    }
108  29757 subjectVariableAllocations.remove(index);
109    // System.out.println("removed subject variable " + var);
110    }
111    /**
112    * Remove existing subject variable interpretation.
113    *
114    * @param var Remove this subject variable.
115    */
 
116  176915 toggle public synchronized void removeSubjectVariable(final SubjectVariable var) {
117  176915 int index = getIndex(var);
118  176915 if (index < 0) {
119  0 throw new RuntimeException("variable does not exist: " + var);
120    }
121  176915 final SubjectVariableAllocation current
122    = (SubjectVariableAllocation) subjectVariableAllocations.get(index);
123  176915 if (current.isFixed()) {
124  0 throw new RuntimeException("trying to remove fixed allocation: " + current);
125    }
126  176915 subjectVariableAllocations.remove(index);
127    // System.out.println("removed subject variable " + var);
128    }
129   
 
130  616767 toggle private synchronized int getSubjectVariableSelection(final SubjectVariable var) {
131  616767 int selection = 0;
132  616767 int index = getIndex(var);
133  616767 if (index >= 0) {
134  616352 selection = ((SubjectVariableAllocation) subjectVariableAllocations.get(index))
135    .getValue();
136    } else {
137  415 addSubjectVariable(var);
138    }
139  616767 return selection;
140    }
141   
142    /**
143    * Get current interpretation of subject variable.
144    *
145    * @param var Subject variable we are interested in.
146    * @return Current entity for subject variable.
147    */
 
148  616767 toggle public synchronized Entity getEntity(final SubjectVariable var) {
149  616767 return model.getEntity(getSubjectVariableSelection(var));
150    }
151   
 
152  1109580 toggle private int getIndex(final SubjectVariable var) {
153  1109580 int index = -1;
154  1288974 for (index = subjectVariableAllocations.size() - 1; index >= 0; index--) {
155  1288559 final SubjectVariableAllocation current
156    = (SubjectVariableAllocation) subjectVariableAllocations.get(index);
157  1288559 if (var.equals(current.getVariable())) {
158  1109165 break;
159    }
160    }
161  1109580 return index;
162    }
163   
164    /**
165    * Set interpretation of subject variable to next entity.
166    *
167    * @param var Switch to next entity for this subject variable.
168    */
 
169  286141 toggle public synchronized void increaseSubjectVariableSelection(final SubjectVariable var) {
170  286141 ((SubjectVariableAllocation) subjectVariableAllocations.get(getIndex(var))).increaseNumber();
171    }
172   
 
173  0 toggle public synchronized String toString() {
174  0 final StringBuffer buffer = new StringBuffer();
175  0 buffer.append("subject variables {");
176  0 for (int i = 0; i < subjectVariableAllocations.size(); i++) {
177  0 if (i > 0) {
178  0 buffer.append(", ");
179    }
180  0 SubjectVariableAllocation var = (SubjectVariableAllocation) subjectVariableAllocations.get(i);
181  0 buffer.append(var.getVariable());
182  0 buffer.append("=");
183  0 buffer.append(model.getEntity(var.getValue()));
184    }
185  0 buffer.append("}");
186  0 return buffer.toString();
187    }
188   
189    /**
190    * Clear variable interpretation.
191    */
 
192  48 toggle public synchronized void clear() {
193  48 subjectVariableAllocations.clear();
194    }
195   
196    }