001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
004 *
005 * "Hilbert II" is free software; you can redistribute
006 * it and/or modify it under the terms of the GNU General Public
007 * License as published by the Free Software Foundation; either
008 * version 2 of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 */
015
016 package org.qedeq.kernel.bo.logic.model;
017
018 import java.util.ArrayList;
019 import java.util.HashMap;
020 import java.util.List;
021 import java.util.Map;
022 import java.util.Vector;
023
024 /**
025 * A model for our mathematical world. It has entities, functions and predicates.
026 * There are also predicate and function constants.
027 *
028 * @author Michael Meyling
029 */
030 public abstract class DynamicModel implements Model {
031
032 /** Always return false. */
033 public static final Predicate FALSE = new Predicate(0, 99, "F", "always false") {
034 public boolean calculate(final Entity[] entities) {
035 return false;
036 }
037 };
038
039 /** Always return true. */
040 public static final Predicate TRUE = new Predicate(0, 99, "T", "always true") {
041 public boolean calculate(final Entity[] entities) {
042 return true;
043 }
044 };
045
046 /** Return true if value is even. */
047 public static final Predicate EVEN = new Predicate(0, 99, "| 2", "is even") {
048 public boolean calculate(final Entity[] entities) {
049 boolean result = true;
050 for (int i = 0; i < entities.length; i++) {
051 result &= entities[i].getValue() % 2 == 0;
052 }
053 return result;
054 }
055 };
056
057 /** Are the entities ordered by < ? */
058 public static final Predicate LESS = new Predicate(0, 99, "<", "less") {
059 public boolean calculate(final Entity[] entities) {
060 boolean result = true;
061 for (int i = 0; i < entities.length - 1; i++) {
062 result &= entities[i].getValue() < entities[i + 1].getValue();
063 }
064 return result;
065 }
066 };
067
068 /** Are the entities not ordered by < ? */
069 public static final Predicate NOT_LESS = Predicate.not(LESS);
070
071 /** Are the entities are all the same? */
072 public static final Predicate EQUAL = new Predicate(0, 99, "=", "equal") {
073 public boolean calculate(final Entity[] entities) {
074 boolean result = true;
075 for (int i = 0; i < entities.length - 1; i++) {
076 result &= entities[i].getValue() == entities[i + 1].getValue();
077 }
078 return result;
079 }
080 };
081
082 /** Are the entities are not all the same? */
083 public static final Predicate NOT_EQUAL = Predicate.not(EQUAL);
084
085 /** Name of model. */
086 private final String name;
087
088 /** List of all entities in this model. */
089 private final List entities;
090
091 /** List of functions for different argument numbers. */
092 private final Vector functionPool;
093
094 /** List of predicates for different argument numbers. */
095 private final Vector predicatePool;
096
097 /** Map of predicate constants. */
098 private final Map predicateConstants;
099
100 /** Map of function constants. */
101 private final Map functionConstants;
102
103 /**
104 * Constructor.
105 *
106 * @param name Model name.
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 * Get model name.
124 *
125 * @return Model name.
126 */
127 public String getName() {
128 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 public void addPredicateConstant(final ModelPredicateConstant constant, final Predicate predicate) {
145 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 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 // System.out.println("added entity " + entity);
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 * Add a predicate for interpreting predicate variables.
193 *
194 * @param size Add for this function argument number.
195 * @param predicate This interpretation.
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 * Add a function for interpreting function variables.
231 *
232 * @param size Add for this function argument number.
233 * @param function This interpretation.
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 }
|