| 1 | /* This file is part of the project "Hilbert II" - http://www.qedeq.org |
| 2 | * |
| 3 | * Copyright 2000-2014, 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 | public boolean calculate(final Entity[] entities) { |
| 35 | return false; |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | /** Always return true. */ |
| 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 | /** Return true if value is even. */ |
| 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 | /** Are the entities ordered by < ? */ |
| 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 | /** 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 | 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 | /** 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 | 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 | } |