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.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Vector;
23
24
25
26
27
28
29
30 public abstract class DynamicModel implements Model {
31
32
33 public static final Predicate FALSE = new Predicate(0, 99, "F", "always false") {
34 public boolean calculate(final Entity[] entities) {
35 return false;
36 }
37 };
38
39
40 public static final Predicate TRUE = new Predicate(0, 99, "T", "always true") {
41 public boolean calculate(final Entity[] entities) {
42 return true;
43 }
44 };
45
46
47 public static final Predicate EVEN = new Predicate(0, 99, "| 2", "is even") {
48 public boolean calculate(final Entity[] entities) {
49 boolean result = true;
50 for (int i = 0; i < entities.length; i++) {
51 result &= entities[i].getValue() % 2 == 0;
52 }
53 return result;
54 }
55 };
56
57
58 public static final Predicate LESS = new Predicate(0, 99, "<", "less") {
59 public boolean calculate(final Entity[] entities) {
60 boolean result = true;
61 for (int i = 0; i < entities.length - 1; i++) {
62 result &= entities[i].getValue() < entities[i + 1].getValue();
63 }
64 return result;
65 }
66 };
67
68
69 public static final Predicate NOT_LESS = Predicate.not(LESS);
70
71
72 public static final Predicate EQUAL = new Predicate(0, 99, "=", "equal") {
73 public boolean calculate(final Entity[] entities) {
74 boolean result = true;
75 for (int i = 0; i < entities.length - 1; i++) {
76 result &= entities[i].getValue() == entities[i + 1].getValue();
77 }
78 return result;
79 }
80 };
81
82
83 public static final Predicate NOT_EQUAL = Predicate.not(EQUAL);
84
85
86 private final String name;
87
88
89 private final List entities;
90
91
92 private final Vector functionPool;
93
94
95 private final Vector predicatePool;
96
97
98 private final Map predicateConstants;
99
100
101 private final Map functionConstants;
102
103
104
105
106
107
108 public DynamicModel(final String name) {
109 this.name = name;
110 entities = new ArrayList();
111 functionPool = new Vector();
112 predicatePool = new Vector();
113
114 predicateConstants = new HashMap();
115 predicateConstants.put(new ModelPredicateConstant("TRUE", 0), TRUE);
116 predicateConstants.put(new ModelPredicateConstant("FALSE", 0), FALSE);
117 predicateConstants.put(new ModelPredicateConstant("equal", 2), EQUAL);
118 predicateConstants.put(new ModelPredicateConstant("notEqual", 2), NOT_EQUAL);
119 functionConstants = new HashMap();
120 }
121
122
123
124
125
126
127 public String getName() {
128 return name;
129 }
130
131
132
133
134
135
136 public abstract String getDescription();
137
138
139
140
141
142
143
144 public void addPredicateConstant(final ModelPredicateConstant constant, final Predicate predicate) {
145 predicateConstants.put(constant, predicate);
146 }
147
148
149
150
151
152
153
154 public void addFunctionConstant(final ModelFunctionConstant constant, final Function function) {
155 functionConstants.put(constant, function);
156 }
157
158 protected void addEntity(final Entity entity) {
159 if (entities.size() != entity.getValue()) {
160 throw new RuntimeException("entity value should have been " + entities.size()
161 + " but was " + entity.getValue());
162 }
163
164 entities.add(entity);
165 }
166
167 public int getEntitiesSize() {
168 return entities.size();
169 }
170
171 public Entity getEntity(final int number) {
172 return (Entity) entities.get(number);
173 }
174
175 public int getPredicateSize(final int size) {
176 if (predicatePool.size() <= size) {
177 return 0;
178 }
179 final List list = (List) predicatePool.get(size);
180 if (list == null) {
181 return 0;
182 }
183 return list.size();
184 }
185
186 public Predicate getPredicate(final int size, final int number) {
187 final List predicateForSize = (List) predicatePool.get(size);
188 return (Predicate) predicateForSize.get(number);
189 }
190
191
192
193
194
195
196
197 public void addPredicate(final int size, final Predicate predicate) {
198 if (getPredicateSize(size) == 0) {
199 if (predicatePool.size() <= size) {
200 for (int i = predicatePool.size(); i <= size; i++) {
201 predicatePool.add(new ArrayList());
202 }
203 }
204 }
205 List list = (List) predicatePool.get(size);
206 list.add(predicate);
207 }
208
209 public Predicate getPredicateConstant(final ModelPredicateConstant con) {
210 return (Predicate) predicateConstants.get(con);
211 }
212
213 public int getFunctionSize(final int size) {
214 if (functionPool.size() <= size) {
215 return 0;
216 }
217 final List list = (List) functionPool.get(size);
218 if (list == null) {
219 return 0;
220 }
221 return list.size();
222 }
223
224 public Function getFunction(final int size, final int number) {
225 final List functionForSize = (List) functionPool.get(size);
226 return (Function) functionForSize.get(number);
227 }
228
229
230
231
232
233
234
235 public void addFunction(final int size, final Function function) {
236 if (getFunctionSize(size) == 0) {
237 if (functionPool.size() <= size) {
238 for (int i = functionPool.size(); i <= size; i++) {
239 functionPool.add(new ArrayList());
240 }
241 }
242 }
243 final List list = (List) functionPool.get(size);
244 list.add(function);
245 }
246
247 public Function getFunctionConstant(final ModelFunctionConstant con) {
248 return (Function) functionConstants.get(con);
249 }
250
251 public abstract Entity comprehension(final Entity[] array);
252
253 }