Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../../../img/srcFileCovDistChart9.png 45% of files have more coverage
203   421   68   7.25
38   316   0.33   28
28     2.43  
1    
 
  ThreeModel       Line # 29 203 68 90.7% 0.9070632
 
  (49)
 
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.HashMap;
20    import java.util.List;
21    import java.util.Map;
22   
23    /**
24    * A model for our mathematical world. It has entities, functions and predicates.
25    * There are also predicate and function constants.
26    *
27    * @author Michael Meyling
28    */
 
29    public final class ThreeModel implements Model {
30   
31    /** "Zero" or empty set. */
32    public static final Entity ZERO = new Entity(0, "0", "{} or empty set");
33   
34    /** "One" or set that contains the empty set. */
35    public static final Entity ONE = new Entity(1, "1", "{{}} or {0}");
36   
37    /** "Two" or set that contains "zero" and "one". */
38    public static final Entity TWO = new Entity(2, "2", "{{}, {{}}} or {0, 1}");
39   
40    /** Map to zero. */
41    public static final Function FUNCTION_ZERO = new Function(0, 99, "->0", "always 0") {
 
42  243 toggle public Entity map(final Entity[] entities) {
43  243 return ZERO;
44    }
45    };
46   
47    /** Map to one. */
48    public static final Function FUNCTION_ONE = new Function(0, 99, "->1", "always 1") {
 
49  48 toggle public Entity map(final Entity[] entities) {
50  48 return ONE;
51    }
52    };
53   
54    /** Map to two. */
55    public static final Function FUNCTION_TWO = new Function(0, 99, "->2", "always 2") {
 
56  181 toggle public Entity map(final Entity[] entities) {
57  181 return TWO;
58    }
59    };
60   
61    /** Modulo 3. */
62    public static final Function FUNCTION_MOD = new Function(0, 99, "% 3", "modulo 3") {
 
63  40 toggle public Entity map(final Entity[] entities) {
64  40 int result = 0;
65  80 for (int i = 0; i < entities.length; i++) {
66  40 result += entities[i].getValue() % 3;
67    }
68  40 result = result % 3;
69  40 switch (result) {
70  15 case 0: return ZERO;
71  13 case 1: return ONE;
72  12 case 2: return TWO;
73  0 default: return null;
74    }
75    }
76    };
77   
78    /** +1 Modulo 3. */
79    public static final Function FUNCTION_PLUS = new Function(0, 99, "+1 % 3", "plus 1 modulo 3") {
 
80  36 toggle public Entity map(final Entity[] entities) {
81  36 int result = 1;
82  72 for (int i = 0; i < entities.length; i++) {
83  36 result += entities[i].getValue() % 3;
84    }
85  36 result = result % 3;
86  36 switch (result) {
87  12 case 0: return ZERO;
88  12 case 1: return ONE;
89  12 case 2: return TWO;
90  0 default: return null;
91    }
92    }
93    };
94   
95    /** Minimum. */
96    public static final Function FUNCTION_MIN = new Function(0, 99, "min", "minimum") {
 
97  375 toggle public Entity map(final Entity[] entities) {
98  375 int result = 2;
99  1125 for (int i = 0; i < entities.length; i++) {
100  750 if (result > entities[i].getValue()) {
101  384 result = entities[i].getValue();
102    }
103    }
104  375 switch (result) {
105  285 case 0: return ZERO;
106  64 case 1: return ONE;
107  26 case 2: return TWO;
108  0 default: return null;
109    }
110    }
111    };
112   
113    /** Maximum. */
114    public static final Function FUNCTION_MAX = new Function(0, 99, "max", "maximum") {
 
115  355 toggle public Entity map(final Entity[] entities) {
116  355 int result = 0;
117  1065 for (int i = 0; i < entities.length; i++) {
118  710 if (result < entities[i].getValue()) {
119  302 result = entities[i].getValue();
120    }
121    }
122  355 switch (result) {
123  75 case 0: return ZERO;
124  92 case 1: return ONE;
125  188 case 2: return TWO;
126  0 default: return null;
127    }
128    }
129    };
130   
131    /** Maximum. */
132    private final Function functionClassList = new Function(0, 99, "{..}", "classList") {
 
133  143 toggle public Entity map(final Entity[] entities) {
134  143 Entity result = ZERO;
135  385 for (int i = 0; i < entities.length; i++) {
136  242 switch (entities[i].getValue()) {
137  106 case 0:
138  106 if (result.getValue() == 0) {
139  66 result = ONE;
140    }
141  106 break;
142  76 case 1:
143  76 result = TWO;
144  76 break;
145  60 case 2:
146  60 result = TWO;
147  60 break;
148  0 default: throw new RuntimeException("unknown value for entity " + entities[i]);
149    }
150    }
151  143 return result;
152    }
153    };
154   
155    /** Always return false. */
156    public static final Predicate FALSE = new Predicate(0, 99, "F", "always false") {
 
157  33903 toggle public boolean calculate(final Entity[] entities) {
158  33903 return false;
159    }
160    };
161   
162    /** Always return true. */
163    public static final Predicate TRUE = new Predicate(0, 99, "T", "always true") {
 
164  38421 toggle public boolean calculate(final Entity[] entities) {
165  38421 return true;
166    }
167    };
168   
169    /** Return true if value is even. */
170    public static final Predicate EVEN = new Predicate(0, 99, "| 2", "is even") {
 
171  16666 toggle public boolean calculate(final Entity[] entities) {
172  16666 boolean result = true;
173  34290 for (int i = 0; i < entities.length; i++) {
174  17624 result &= entities[i].getValue() % 2 == 0;
175    }
176  16666 return result;
177    }
178    };
179   
180    /** Are the entities ordered by < ? */
181    public static final Predicate LESS = new Predicate(0, 99, "<", "less") {
 
182  4028 toggle public boolean calculate(final Entity[] entities) {
183  4028 boolean result = true;
184  8056 for (int i = 0; i < entities.length - 1; i++) {
185  4028 result &= entities[i].getValue() < entities[i + 1].getValue();
186    }
187  4028 return result;
188    }
189    };
190   
191    /** Are the entities not ordered by < ? */
192    public static final Predicate NOT_LESS = Predicate.not(LESS);
193   
194    /** Are the entities are all the same? */
195    public static final Predicate EQUAL = new Predicate(0, 99, "=", "equal") {
 
196  3990 toggle public boolean calculate(final Entity[] entities) {
197  3990 boolean result = true;
198  7980 for (int i = 0; i < entities.length - 1; i++) {
199  3990 result &= entities[i].getValue() == entities[i + 1].getValue();
200    }
201  3990 return result;
202    }
203    };
204   
205    /** Are the entities are not all the same? */
206    public static final Predicate NOT_EQUAL = Predicate.not(EQUAL);
207   
208    /** Return true if all values are zero. */
209    public static final Predicate IS_ZERO = new Predicate(0, 99, "=0", "is_zero") {
 
210  16599 toggle public boolean calculate(final Entity[] entities) {
211  16599 boolean result = true;
212  34116 for (int i = 0; i < entities.length; i++) {
213  17517 result &= entities[i].getValue() == ZERO.getValue();
214    }
215  16599 return result;
216    }
217    };
218   
219    /** Are the entities are all equal to 1? */
220    public static final Predicate IS_ONE = new Predicate(0, 99, "=1", "is_one") {
 
221  14830 toggle public boolean calculate(final Entity[] entities) {
222  14830 boolean result = true;
223  31442 for (int i = 0; i < entities.length; i++) {
224  16612 result &= entities[i].getValue() == ONE.getValue();
225    }
226  14830 return result;
227    }
228    };
229   
230    /** Are the entities are all equal to 2? */
231    public static final Predicate IS_TWO = new Predicate(0, 99, "= 2", "is_two") {
 
232  21297 toggle public boolean calculate(final Entity[] entities) {
233  21297 boolean result = true;
234  45528 for (int i = 0; i < entities.length; i++) {
235  24231 result &= entities[i].getValue() == TWO.getValue();
236    }
237  21297 return result;
238    }
239    };
240   
241    /** Are the entities are not all equal to 2? */
242    public static final Predicate NOT_IS_TWO = Predicate.not(IS_TWO);
243   
244   
245   
246    /** List of all entities in this model. */
247    private final List entities;
248   
249    /** List of functions for different argument numbers. */
250    private final List functionPool;
251   
252    /** List of predicates for different argument numbers. */
253    private final List predicatePool;
254   
255    /** Map of predicate constants. */
256    private final Map predicateConstants;
257   
258    /** Map of function constants. */
259    private final Map functionConstants;
260   
261    /**
262    * Constructor.
263    */
 
264  51 toggle public ThreeModel() {
265  51 entities = new ArrayList();
266  51 entities.add(ZERO);
267  51 entities.add(ONE);
268  51 entities.add(TWO);
269   
270  51 functionPool = new ArrayList();
271   
272  51 final List function0 = new ArrayList();
273  51 functionPool.add(function0);
274  51 function0.add(FUNCTION_ZERO);
275  51 function0.add(FUNCTION_ONE);
276   
277  51 final List function1 = new ArrayList();
278  51 functionPool.add(function1);
279  51 function1.add(FUNCTION_ZERO);
280  51 function1.add(FUNCTION_ONE);
281  51 function1.add(FUNCTION_MOD);
282  51 function1.add(FUNCTION_PLUS);
283   
284  51 final List function2 = new ArrayList();
285  51 functionPool.add(function2);
286  51 function2.add(FUNCTION_ZERO);
287  51 function2.add(FUNCTION_ONE);
288  51 function2.add(FUNCTION_MOD);
289  51 function2.add(FUNCTION_PLUS);
290   
291  51 predicatePool = new ArrayList();
292   
293  51 final List predicate0 = new ArrayList();
294  51 predicatePool.add(predicate0);
295  51 predicate0.add(FALSE);
296  51 predicate0.add(TRUE);
297   
298  51 final List predicate1 = new ArrayList();
299  51 predicatePool.add(predicate1);
300  51 predicate1.add(FALSE);
301  51 predicate1.add(TRUE);
302  51 predicate1.add(EVEN);
303  51 predicate1.add(IS_ZERO);
304  51 predicate1.add(IS_ONE);
305  51 predicate1.add(IS_TWO);
306   
307  51 final List predicate2 = new ArrayList();
308  51 predicatePool.add(predicate2);
309  51 predicate2.add(FALSE);
310  51 predicate2.add(TRUE);
311  51 predicate2.add(EVEN);
312  51 predicate2.add(LESS);
313  51 predicate2.add(EQUAL);
314  51 predicate2.add(IS_ZERO);
315  51 predicate2.add(IS_ONE);
316  51 predicate2.add(IS_TWO);
317   
318  51 predicateConstants = new HashMap();
319  51 predicateConstants.put(new ModelPredicateConstant("TRUE", 0), TRUE);
320  51 predicateConstants.put(new ModelPredicateConstant("FALSE", 0), FALSE);
321  51 predicateConstants.put(new ModelPredicateConstant("equal", 2), EQUAL);
322  51 predicateConstants.put(new ModelPredicateConstant("notEqual", 2), NOT_EQUAL);
323  51 predicateConstants.put(new ModelPredicateConstant("in", 2), LESS);
324  51 predicateConstants.put(new ModelPredicateConstant("notIn", 2), NOT_LESS);
325  51 predicateConstants.put(new ModelPredicateConstant("isSet", 1), Predicate.not(IS_TWO));
326  51 predicateConstants.put(new ModelPredicateConstant("subclass", 2), Predicate.or(LESS, EQUAL));
327   
328  51 functionConstants = new HashMap();
329  51 functionConstants.put(new ModelFunctionConstant("emptySet", 0), FUNCTION_ZERO);
330  51 functionConstants.put(new ModelFunctionConstant("RussellClass", 0), FUNCTION_TWO);
331  51 functionConstants.put(new ModelFunctionConstant("intersection", 2), FUNCTION_MIN);
332  51 functionConstants.put(new ModelFunctionConstant("union", 2), FUNCTION_MAX);
333  51 functionConstants.put(new ModelFunctionConstant("universalClass", 0), FUNCTION_TWO);
334  51 functionConstants.put(new ModelFunctionConstant("complement", 1), new Function(1, 1, "compement",
335    "complement") {
 
336  212 toggle public Entity map(final Entity[] entities) {
337  212 if (entities.length != 1) {
338  0 return ZERO;
339    }
340  212 switch (entities[0].getValue()) {
341  125 case 0: return TWO;
342  39 case 1: return TWO;
343  48 case 2: return ZERO;
344  0 default: throw new IllegalArgumentException("unknown entity value");
345    }
346    }
347    });
348  51 functionConstants.put(new ModelFunctionConstant("classList", 0), functionClassList);
349  51 functionConstants.put(new ModelFunctionConstant("classList", 1), functionClassList);
350  51 functionConstants.put(new ModelFunctionConstant("classList", 2), functionClassList);
351  51 functionConstants.put(new ModelFunctionConstant("classList", 3), functionClassList);
352  51 functionConstants.put(new ModelFunctionConstant("classList", 4), functionClassList);
353   
354    }
355   
 
356  0 toggle public String getDescription() {
357  0 return "This model has three entities. The first is element of the second, "
358    + "the second element of the third.";
359    }
360   
 
361  114241 toggle public int getEntitiesSize() {
362  114241 return entities.size();
363    }
364   
 
365  138334 toggle public Entity getEntity(final int number) {
366  138334 return (Entity) entities.get(number);
367    }
368   
 
369  4289 toggle public int getPredicateSize(final int size) {
370  4289 if (predicatePool.size() <= size) {
371  0 return 0;
372    }
373  4289 return ((List) predicatePool.get(size)).size();
374    }
375   
 
376  139793 toggle public Predicate getPredicate(final int size, final int number) {
377  139793 final List predicateForSize = (List) predicatePool.get(size);
378  139793 return (Predicate) predicateForSize.get(number);
379    }
380   
 
381  8360 toggle public Predicate getPredicateConstant(final ModelPredicateConstant con) {
382  8360 return (Predicate) predicateConstants.get(con);
383    }
384   
 
385  14 toggle public int getFunctionSize(final int size) {
386  14 if (functionPool.size() <= size) {
387  0 return 0;
388    }
389  14 return ((List) functionPool.get(size)).size();
390    }
391   
 
392  172 toggle public Function getFunction(final int size, final int number) {
393  172 final List functionForSize = (List) functionPool.get(size);
394  172 return (Function) functionForSize.get(number);
395    }
396   
 
397  1497 toggle public Function getFunctionConstant(final ModelFunctionConstant con) {
398  1497 return (Function) functionConstants.get(con);
399    }
400   
 
401  2232 toggle public Entity comprehension(final Entity[] array) {
402  2232 Entity result = ZERO;
403  4479 for (int i = 0; i < array.length; i++) {
404  2247 switch (array[i].getValue()) {
405  1496 case 0:
406  1496 if (result.getValue() == 0) {
407  1496 result = ONE;
408    }
409  1496 break;
410  751 case 1:
411  751 result = TWO;
412  751 break;
413  0 case 2:
414  0 break;
415  0 default: throw new RuntimeException("unknown value for entity " + array[i]);
416    }
417    }
418  2232 return result;
419    }
420   
421    }