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.service.logic; |
17 | |
18 | import java.util.HashMap; |
19 | import java.util.Map; |
20 | |
21 | import org.qedeq.base.trace.Trace; |
22 | import org.qedeq.kernel.bo.logic.common.ExistenceChecker; |
23 | import org.qedeq.kernel.bo.logic.common.FunctionConstant; |
24 | import org.qedeq.kernel.bo.logic.common.FunctionKey; |
25 | import org.qedeq.kernel.bo.logic.common.PredicateConstant; |
26 | import org.qedeq.kernel.bo.logic.common.PredicateKey; |
27 | import org.qedeq.kernel.se.base.module.InitialFunctionDefinition; |
28 | import org.qedeq.kernel.se.base.module.InitialPredicateDefinition; |
29 | import org.qedeq.kernel.se.base.module.Rule; |
30 | import org.qedeq.kernel.se.common.RuleKey; |
31 | |
32 | |
33 | /** |
34 | * Checks if all predicate and function constants exist already. |
35 | * |
36 | * @author Michael Meyling |
37 | */ |
38 | public class DefaultExistenceChecker implements ExistenceChecker { |
39 | |
40 | /** This class. */ |
41 | private static final Class CLASS = DefaultExistenceChecker.class; |
42 | |
43 | /** Maps {@link PredicateKey} identifiers to {@link InitialPredicateDefinitions}s. */ |
44 | private final Map initialPredicateDefinitions = new HashMap(); |
45 | |
46 | /** Maps {@link PredicateKey} identifiers to {@link PredicateConstant}s. */ |
47 | private final Map predicateDefinitions = new HashMap(); |
48 | |
49 | /** Maps {@link FunctionKey} identifiers to {@link InitialFunctionDefinition}s. */ |
50 | private final Map initialFunctionDefinitions = new HashMap(); |
51 | |
52 | /** Maps {@link FunctionKey} identifiers to {@link FunctionConstant}s. */ |
53 | private final Map functionDefinitions = new HashMap(); |
54 | |
55 | /** Maps {@link RuleKey}s to {@link Rules}s. */ |
56 | private final Map ruleDefinitions = new HashMap(); |
57 | |
58 | /** Is the class operator already defined? */ |
59 | private boolean setDefinitionByFormula; |
60 | |
61 | /** Identity operator. */ |
62 | private String identityOperator; |
63 | |
64 | |
65 | /** |
66 | * Constructor. |
67 | */ |
68 | public DefaultExistenceChecker() { |
69 | clear(); |
70 | } |
71 | |
72 | /** |
73 | * Empty all definitions. |
74 | */ |
75 | public void clear() { |
76 | Trace.trace(CLASS, this, "setClassOperatorExists", "clear"); |
77 | initialPredicateDefinitions.clear(); |
78 | predicateDefinitions.clear(); |
79 | initialFunctionDefinitions.clear(); |
80 | ruleDefinitions.clear(); |
81 | functionDefinitions.clear(); |
82 | identityOperator = null; |
83 | setDefinitionByFormula = false; |
84 | } |
85 | |
86 | public boolean predicateExists(final PredicateKey predicate) { |
87 | final InitialPredicateDefinition initialDefinition |
88 | = (InitialPredicateDefinition) initialPredicateDefinitions.get(predicate); |
89 | if (initialDefinition != null) { |
90 | return true; |
91 | } |
92 | return null != predicateDefinitions.get(predicate); |
93 | } |
94 | |
95 | public boolean predicateExists(final String name, final int arguments) { |
96 | final PredicateKey predicate = new PredicateKey(name, "" + arguments); |
97 | return predicateExists(predicate); |
98 | } |
99 | |
100 | public boolean isInitialPredicate(final PredicateKey predicate) { |
101 | final InitialPredicateDefinition initialDefinition |
102 | = (InitialPredicateDefinition) initialPredicateDefinitions.get(predicate); |
103 | return initialDefinition != null; |
104 | } |
105 | |
106 | /** |
107 | * Add unknown predicate constant definition. If the predicate constant is already known a |
108 | * runtime exception is thrown. |
109 | * |
110 | * @param initialDefinition Predicate constant definition that is not already known. Must not be |
111 | * <code>null</code>. |
112 | * @throws IllegalArgumentException Predicate constant is already defined. |
113 | */ |
114 | public void add(final InitialPredicateDefinition initialDefinition) { |
115 | final PredicateKey predicate = new PredicateKey(initialDefinition.getName(), |
116 | initialDefinition.getArgumentNumber()); |
117 | if (predicateExists(predicate)) { |
118 | throw new IllegalArgumentException(LogicErrors.PREDICATE_ALREADY_DEFINED_TEXT |
119 | + predicate); |
120 | } |
121 | initialPredicateDefinitions.put(predicate, initialDefinition); |
122 | } |
123 | |
124 | /** |
125 | * Add unknown predicate constant definition. If the predicate constant is already known a |
126 | * runtime exception is thrown. |
127 | * |
128 | * @param constant Predicate constant definition that is not already known. Must not be |
129 | * <code>null</code>. |
130 | * @throws IllegalArgumentException Predicate constant is already defined. |
131 | */ |
132 | public void add(final PredicateConstant constant) { |
133 | final PredicateKey predicate = constant.getKey(); |
134 | if (predicateExists(predicate)) { |
135 | throw new IllegalArgumentException(LogicErrors.PREDICATE_ALREADY_DEFINED_TEXT |
136 | + predicate); |
137 | } |
138 | predicateDefinitions.put(predicate, constant); |
139 | } |
140 | |
141 | /** |
142 | * Get predicate constant definition. |
143 | * |
144 | * @param predicate Get definition of this predicate. |
145 | * @return Definition. |
146 | */ |
147 | public PredicateConstant get(final PredicateKey predicate) { |
148 | return (PredicateConstant) predicateDefinitions.get(predicate); |
149 | } |
150 | |
151 | /** |
152 | * Get predicate constant definition. |
153 | * |
154 | * @param name Name of predicate. |
155 | * @param arguments Arguments of predicate. |
156 | * @return Definition. Might be <code>null</code>. |
157 | */ |
158 | public PredicateConstant getPredicate(final String name, final int arguments) { |
159 | final PredicateKey predicate = new PredicateKey(name, "" + arguments); |
160 | return get(predicate); |
161 | } |
162 | |
163 | public boolean functionExists(final FunctionKey function) { |
164 | final InitialFunctionDefinition initialDefinition |
165 | = (InitialFunctionDefinition) initialFunctionDefinitions.get(function); |
166 | if (initialDefinition != null) { |
167 | return true; |
168 | } |
169 | return null != functionDefinitions.get(function); |
170 | } |
171 | |
172 | public boolean functionExists(final String name, final int arguments) { |
173 | final FunctionKey function = new FunctionKey(name, "" + arguments); |
174 | return functionExists(function); |
175 | } |
176 | |
177 | /** |
178 | * Add unknown function constant definition. If the function constant is already known a |
179 | * runtime exception is thrown. |
180 | * |
181 | * @param definition Function constant definition that is not already known. Must not be |
182 | * <code>null</code>. |
183 | * @throws IllegalArgumentException Function constant is already defined. |
184 | */ |
185 | public void add(final FunctionConstant definition) { |
186 | final FunctionKey function = definition.getKey(); |
187 | if (functionDefinitions.get(function) != null) { |
188 | throw new IllegalArgumentException(LogicErrors.FUNCTION_ALREADY_DEFINED_TEXT |
189 | + function); |
190 | } |
191 | functionDefinitions.put(function, definition); |
192 | } |
193 | |
194 | /** |
195 | * Add unknown function constant definition. If the function constant is already known a |
196 | * runtime exception is thrown. |
197 | * |
198 | * @param initialDefinition Function constant definition that is not already known. Must not be |
199 | * <code>null</code>. |
200 | * @throws IllegalArgumentException Function constant is already defined. |
201 | */ |
202 | public void add(final InitialFunctionDefinition initialDefinition) { |
203 | final FunctionKey predicate = new FunctionKey(initialDefinition.getName(), |
204 | initialDefinition.getArgumentNumber()); |
205 | if (functionExists(predicate)) { |
206 | throw new IllegalArgumentException(LogicErrors.FUNCTION_ALREADY_DEFINED_TEXT |
207 | + predicate); |
208 | } |
209 | initialFunctionDefinitions.put(predicate, initialDefinition); |
210 | } |
211 | |
212 | /** |
213 | * Get function constant definition. |
214 | * |
215 | * @param function Get definition of this predicate. |
216 | * @return Definition. Might be <code>null</code>. |
217 | */ |
218 | public FunctionConstant get(final FunctionKey function) { |
219 | return (FunctionConstant) functionDefinitions.get(function); |
220 | } |
221 | |
222 | /** |
223 | * Get function constant definition. |
224 | * |
225 | * @param name Name of function. |
226 | * @param arguments Arguments of function. |
227 | * @return Definition. Might be <code>null</code>. |
228 | */ |
229 | public FunctionConstant getFunction(final String name, final int arguments) { |
230 | final FunctionKey function = new FunctionKey(name, "" + arguments); |
231 | return get(function); |
232 | } |
233 | |
234 | public boolean isInitialFunction(final FunctionKey function) { |
235 | final InitialFunctionDefinition initialDefinition |
236 | = (InitialFunctionDefinition) initialFunctionDefinitions.get(function); |
237 | return initialDefinition != null; |
238 | } |
239 | |
240 | |
241 | public boolean ruleExists(final RuleKey ruleKey) { |
242 | return null != get(ruleKey); |
243 | } |
244 | |
245 | /** |
246 | * Add unknown rule definition. If the rule is already known a runtime exception is thrown. |
247 | * |
248 | * @param ruleKey Key for rule. |
249 | * @param definition Rule definition that is not already known. Must not be |
250 | * <code>null</code>. Rule key might be different! |
251 | * @throws IllegalArgumentException Rule is already defined (for given version). |
252 | */ |
253 | public void add(final RuleKey ruleKey, final Rule definition) { |
254 | if (ruleDefinitions.get(ruleKey) != null) { |
255 | throw new IllegalArgumentException(LogicErrors.RULE_ALREADY_DEFINED_TEXT |
256 | + ruleKey); |
257 | } |
258 | ruleDefinitions.put(ruleKey, definition); |
259 | } |
260 | |
261 | /** |
262 | * Get rule definition. |
263 | * |
264 | * @param ruleKey Get definition of this key. |
265 | * @return Definition. Might be <code>null</code>. |
266 | */ |
267 | public Rule get(final RuleKey ruleKey) { |
268 | return (Rule) ruleDefinitions.get(ruleKey); |
269 | } |
270 | |
271 | public boolean classOperatorExists() { |
272 | return setDefinitionByFormula; |
273 | } |
274 | |
275 | // /** |
276 | // * Set if the class operator is already defined. |
277 | // * |
278 | // * @param existence Class operator is defined. |
279 | // */ |
280 | // TODO m31 20100820: write some tests that use this feature |
281 | // public void setClassOperatorExists(final boolean existence) { |
282 | // Trace.param(CLASS, this, "setClassOperatorExists", "existence", existence); |
283 | // setDefinitionByFormula = existence; |
284 | // } |
285 | |
286 | public boolean identityOperatorExists() { |
287 | return this.identityOperator != null; |
288 | } |
289 | |
290 | public String getIdentityOperator() { |
291 | return this.identityOperator; |
292 | } |
293 | |
294 | /** |
295 | * Set the identity operator. |
296 | * |
297 | * @param identityOperator Operator name. Might be <code>null</code>. |
298 | */ |
299 | public void setIdentityOperatorDefined(final String identityOperator) { |
300 | Trace.param(CLASS, this, "setIdentityOperatorDefined", "identityOperator", identityOperator); |
301 | this.identityOperator = identityOperator; |
302 | } |
303 | |
304 | } |