1
2
3
4
5
6
7
8
9
10
11
12
13
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
25
26
27
28 public final class SubjectVariableInterpreter {
29
30
31 private List subjectVariableAllocations;
32
33
34 private Model model;
35
36
37
38
39
40
41 public SubjectVariableInterpreter(final Model model) {
42 this.model = model;
43 subjectVariableAllocations = new ArrayList();
44 }
45
46
47
48
49
50
51 public synchronized boolean next() {
52 boolean next = true;
53 for (int i = subjectVariableAllocations.size() - 1; i >= -1; i--) {
54 if (i < 0) {
55 next = false;
56 break;
57 }
58 final SubjectVariableAllocation allocation
59 = (SubjectVariableAllocation) subjectVariableAllocations.get(i);
60 if (allocation.getValue() + 1 < model.getEntitiesSize()) {
61 allocation.increaseNumber();
62 break;
63 }
64 allocation.resetNumber();
65 }
66 return next;
67 }
68
69
70
71
72
73
74 public synchronized void addSubjectVariable(final SubjectVariable var) {
75
76
77
78
79
80 subjectVariableAllocations.add(new SubjectVariableAllocation(var));
81 }
82
83
84
85
86
87
88
89 public synchronized void forceAddSubjectVariable(final SubjectVariable var, final int value) {
90 subjectVariableAllocations.add(new SubjectVariableAllocation(var, value));
91 }
92
93
94
95
96
97
98 public synchronized void forceRemoveSubjectVariable(final SubjectVariable var) {
99 int index = getIndex(var);
100 if (index < 0) {
101 throw new RuntimeException("variable does not exist: " + var);
102 }
103 final SubjectVariableAllocation current
104 = (SubjectVariableAllocation) subjectVariableAllocations.get(index);
105 if (!current.isFixed()) {
106 throw new RuntimeException("trying to remove not fixed allocation: " + current);
107 }
108 subjectVariableAllocations.remove(index);
109
110 }
111
112
113
114
115
116 public synchronized void removeSubjectVariable(final SubjectVariable var) {
117 int index = getIndex(var);
118 if (index < 0) {
119 throw new RuntimeException("variable does not exist: " + var);
120 }
121 final SubjectVariableAllocation current
122 = (SubjectVariableAllocation) subjectVariableAllocations.get(index);
123 if (current.isFixed()) {
124 throw new RuntimeException("trying to remove fixed allocation: " + current);
125 }
126 subjectVariableAllocations.remove(index);
127
128 }
129
130 private synchronized int getSubjectVariableSelection(final SubjectVariable var) {
131 int selection = 0;
132 int index = getIndex(var);
133 if (index >= 0) {
134 selection = ((SubjectVariableAllocation) subjectVariableAllocations.get(index))
135 .getValue();
136 } else {
137 addSubjectVariable(var);
138 }
139 return selection;
140 }
141
142
143
144
145
146
147
148 public synchronized Entity getEntity(final SubjectVariable var) {
149 return model.getEntity(getSubjectVariableSelection(var));
150 }
151
152 private int getIndex(final SubjectVariable var) {
153 int index = -1;
154 for (index = subjectVariableAllocations.size() - 1; index >= 0; index--) {
155 final SubjectVariableAllocation current
156 = (SubjectVariableAllocation) subjectVariableAllocations.get(index);
157 if (var.equals(current.getVariable())) {
158 break;
159 }
160 }
161 return index;
162 }
163
164
165
166
167
168
169 public synchronized void increaseSubjectVariableSelection(final SubjectVariable var) {
170 ((SubjectVariableAllocation) subjectVariableAllocations.get(getIndex(var))).increaseNumber();
171 }
172
173 public synchronized String toString() {
174 final StringBuffer buffer = new StringBuffer();
175 buffer.append("subject variables {");
176 for (int i = 0; i < subjectVariableAllocations.size(); i++) {
177 if (i > 0) {
178 buffer.append(", ");
179 }
180 SubjectVariableAllocation var = (SubjectVariableAllocation) subjectVariableAllocations.get(i);
181 buffer.append(var.getVariable());
182 buffer.append("=");
183 buffer.append(model.getEntity(var.getValue()));
184 }
185 buffer.append("}");
186 return buffer.toString();
187 }
188
189
190
191
192 public synchronized void clear() {
193 subjectVariableAllocations.clear();
194 }
195
196 }