Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../../../img/srcFileCovDistChart9.png 45% of files have more coverage
62   253   34   3.1
28   145   0.55   20
20     1.7  
1    
 
  DynamicModel       Line # 30 62 34 87.3% 0.8727273
 
No Tests
 
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    import java.util.Vector;
23   
24    /**
25    * A model for our mathematical world. It has entities, functions and predicates.
26    * There are also predicate and function constants.
27    *
28    * @author Michael Meyling
29    */
 
30    public abstract class DynamicModel implements Model {
31   
32    /** Always return false. */
33    public static final Predicate FALSE = new Predicate(0, 99, "F", "always false") {
 
34  88588 toggle public boolean calculate(final Entity[] entities) {
35  88588 return false;
36    }
37    };
38   
39    /** Always return true. */
40    public static final Predicate TRUE = new Predicate(0, 99, "T", "always true") {
 
41  99113 toggle public boolean calculate(final Entity[] entities) {
42  99113 return true;
43    }
44    };
45   
46    /** Return true if value is even. */
47    public static final Predicate EVEN = new Predicate(0, 99, "| 2", "is even") {
 
48  27897 toggle public boolean calculate(final Entity[] entities) {
49  27897 boolean result = true;
50  58192 for (int i = 0; i < entities.length; i++) {
51  30295 result &= entities[i].getValue() % 2 == 0;
52    }
53  27897 return result;
54    }
55    };
56   
57    /** Are the entities ordered by < ? */
58    public static final Predicate LESS = new Predicate(0, 99, "<", "less") {
 
59  3448 toggle public boolean calculate(final Entity[] entities) {
60  3448 boolean result = true;
61  6896 for (int i = 0; i < entities.length - 1; i++) {
62  3448 result &= entities[i].getValue() < entities[i + 1].getValue();
63    }
64  3448 return result;
65    }
66    };
67   
68    /** Are the entities not ordered by < ? */
69    public static final Predicate NOT_LESS = Predicate.not(LESS);
70   
71    /** Are the entities are all the same? */
72    public static final Predicate EQUAL = new Predicate(0, 99, "=", "equal") {
 
73  8960 toggle public boolean calculate(final Entity[] entities) {
74  8960 boolean result = true;
75  17920 for (int i = 0; i < entities.length - 1; i++) {
76  8960 result &= entities[i].getValue() == entities[i + 1].getValue();
77    }
78  8960 return result;
79    }
80    };
81   
82    /** Are the entities are not all the same? */
83    public static final Predicate NOT_EQUAL = Predicate.not(EQUAL);
84   
85    /** Name of model. */
86    private final String name;
87   
88    /** List of all entities in this model. */
89    private final List entities;
90   
91    /** List of functions for different argument numbers. */
92    private final Vector functionPool;
93   
94    /** List of predicates for different argument numbers. */
95    private final Vector predicatePool;
96   
97    /** Map of predicate constants. */
98    private final Map predicateConstants;
99   
100    /** Map of function constants. */
101    private final Map functionConstants;
102   
103    /**
104    * Constructor.
105    *
106    * @param name Model name.
107    */
 
108  193 toggle public DynamicModel(final String name) {
109  193 this.name = name;
110  193 entities = new ArrayList();
111  193 functionPool = new Vector();
112  193 predicatePool = new Vector();
113   
114  193 predicateConstants = new HashMap();
115  193 predicateConstants.put(new ModelPredicateConstant("TRUE", 0), TRUE);
116  193 predicateConstants.put(new ModelPredicateConstant("FALSE", 0), FALSE);
117  193 predicateConstants.put(new ModelPredicateConstant("equal", 2), EQUAL);
118  193 predicateConstants.put(new ModelPredicateConstant("notEqual", 2), NOT_EQUAL);
119  193 functionConstants = new HashMap();
120    }
121   
122    /**
123    * Get model name.
124    *
125    * @return Model name.
126    */
 
127  0 toggle public String getName() {
128  0 return name;
129    }
130   
131    /**
132    * Returns the description for the concrete model.
133    *
134    * @return Description of concrete model.
135    */
136    public abstract String getDescription();
137   
138    /**
139    * Add a predicate constant.
140    *
141    * @param constant Add for this constant.
142    * @param predicate This interpretation.
143    */
 
144  196 toggle public void addPredicateConstant(final ModelPredicateConstant constant, final Predicate predicate) {
145  196 predicateConstants.put(constant, predicate);
146    }
147   
148    /**
149    * Add a function constant.
150    *
151    * @param constant Add for this constant.
152    * @param function This interpretation.
153    */
 
154  0 toggle public void addFunctionConstant(final ModelFunctionConstant constant, final Function function) {
155  0 functionConstants.put(constant, function);
156    }
157   
 
158  675 toggle protected void addEntity(final Entity entity) {
159  675 if (entities.size() != entity.getValue()) {
160  0 throw new RuntimeException("entity value should have been " + entities.size()
161    + " but was " + entity.getValue());
162    }
163    // System.out.println("added entity " + entity);
164  675 entities.add(entity);
165    }
166   
 
167  347609 toggle public int getEntitiesSize() {
168  347609 return entities.size();
169    }
170   
 
171  491419 toggle public Entity getEntity(final int number) {
172  491419 return (Entity) entities.get(number);
173    }
174   
 
175  14020 toggle public int getPredicateSize(final int size) {
176  14020 if (predicatePool.size() <= size) {
177  626 return 0;
178    }
179  13394 final List list = (List) predicatePool.get(size);
180  13394 if (list == null) {
181  0 return 0;
182    }
183  13394 return list.size();
184    }
185   
 
186  367983 toggle public Predicate getPredicate(final int size, final int number) {
187  367983 final List predicateForSize = (List) predicatePool.get(size);
188  367983 return (Predicate) predicateForSize.get(number);
189    }
190   
191    /**
192    * Add a predicate for interpreting predicate variables.
193    *
194    * @param size Add for this function argument number.
195    * @param predicate This interpretation.
196    */
 
197  3000 toggle public void addPredicate(final int size, final Predicate predicate) {
198  3000 if (getPredicateSize(size) == 0) {
199  626 if (predicatePool.size() <= size) {
200  1252 for (int i = predicatePool.size(); i <= size; i++) {
201  626 predicatePool.add(new ArrayList());
202    }
203    }
204    }
205  3000 List list = (List) predicatePool.get(size);
206  3000 list.add(predicate);
207    }
208   
 
209  54897 toggle public Predicate getPredicateConstant(final ModelPredicateConstant con) {
210  54897 return (Predicate) predicateConstants.get(con);
211    }
212   
 
213  2034 toggle public int getFunctionSize(final int size) {
214  2034 if (functionPool.size() <= size) {
215  438 return 0;
216    }
217  1596 final List list = (List) functionPool.get(size);
218  1596 if (list == null) {
219  0 return 0;
220    }
221  1596 return list.size();
222    }
223   
 
224  512 toggle public Function getFunction(final int size, final int number) {
225  512 final List functionForSize = (List) functionPool.get(size);
226  512 return (Function) functionForSize.get(number);
227    }
228   
229    /**
230    * Add a function for interpreting function variables.
231    *
232    * @param size Add for this function argument number.
233    * @param function This interpretation.
234    */
 
235  2004 toggle public void addFunction(final int size, final Function function) {
236  2004 if (getFunctionSize(size) == 0) {
237  438 if (functionPool.size() <= size) {
238  876 for (int i = functionPool.size(); i <= size; i++) {
239  438 functionPool.add(new ArrayList());
240    }
241    }
242    }
243  2004 final List list = (List) functionPool.get(size);
244  2004 list.add(function);
245    }
246   
 
247  0 toggle public Function getFunctionConstant(final ModelFunctionConstant con) {
248  0 return (Function) functionConstants.get(con);
249    }
250   
251    public abstract Entity comprehension(final Entity[] array);
252   
253    }