Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../../../img/srcFileCovDistChart9.png 45% of files have more coverage
57   304   30   2.48
14   144   0.53   23
23     1.3  
1    
 
  DefaultExistenceChecker       Line # 38 57 30 81.9% 0.81914896
 
  (95)
 
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.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  305 toggle public DefaultExistenceChecker() {
69  305 clear();
70    }
71   
72    /**
73    * Empty all definitions.
74    */
 
75  610 toggle public void clear() {
76  610 Trace.trace(CLASS, this, "setClassOperatorExists", "clear");
77  610 initialPredicateDefinitions.clear();
78  610 predicateDefinitions.clear();
79  610 initialFunctionDefinitions.clear();
80  610 ruleDefinitions.clear();
81  610 functionDefinitions.clear();
82  610 identityOperator = null;
83  610 setDefinitionByFormula = false;
84    }
85   
 
86  37061 toggle public boolean predicateExists(final PredicateKey predicate) {
87  37061 final InitialPredicateDefinition initialDefinition
88    = (InitialPredicateDefinition) initialPredicateDefinitions.get(predicate);
89  37061 if (initialDefinition != null) {
90  22251 return true;
91    }
92  14810 return null != predicateDefinitions.get(predicate);
93    }
94   
 
95  34687 toggle public boolean predicateExists(final String name, final int arguments) {
96  34687 final PredicateKey predicate = new PredicateKey(name, "" + arguments);
97  34687 return predicateExists(predicate);
98    }
99   
 
100  2 toggle public boolean isInitialPredicate(final PredicateKey predicate) {
101  2 final InitialPredicateDefinition initialDefinition
102    = (InitialPredicateDefinition) initialPredicateDefinitions.get(predicate);
103  2 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  205 toggle public void add(final InitialPredicateDefinition initialDefinition) {
115  205 final PredicateKey predicate = new PredicateKey(initialDefinition.getName(),
116    initialDefinition.getArgumentNumber());
117  205 if (predicateExists(predicate)) {
118  0 throw new IllegalArgumentException(LogicErrors.PREDICATE_ALREADY_DEFINED_TEXT
119    + predicate);
120    }
121  205 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  652 toggle public void add(final PredicateConstant constant) {
133  652 final PredicateKey predicate = constant.getKey();
134  652 if (predicateExists(predicate)) {
135  0 throw new IllegalArgumentException(LogicErrors.PREDICATE_ALREADY_DEFINED_TEXT
136    + predicate);
137    }
138  652 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  66991 toggle public PredicateConstant get(final PredicateKey predicate) {
148  66991 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  6538 toggle public PredicateConstant getPredicate(final String name, final int arguments) {
159  6538 final PredicateKey predicate = new PredicateKey(name, "" + arguments);
160  6538 return get(predicate);
161    }
162   
 
163  18673 toggle public boolean functionExists(final FunctionKey function) {
164  18673 final InitialFunctionDefinition initialDefinition
165    = (InitialFunctionDefinition) initialFunctionDefinitions.get(function);
166  18673 if (initialDefinition != null) {
167  0 return true;
168    }
169  18673 return null != functionDefinitions.get(function);
170    }
171   
 
172  17621 toggle public boolean functionExists(final String name, final int arguments) {
173  17621 final FunctionKey function = new FunctionKey(name, "" + arguments);
174  17621 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  1044 toggle public void add(final FunctionConstant definition) {
186  1044 final FunctionKey function = definition.getKey();
187  1044 if (functionDefinitions.get(function) != null) {
188  0 throw new IllegalArgumentException(LogicErrors.FUNCTION_ALREADY_DEFINED_TEXT
189    + function);
190    }
191  1044 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  0 toggle public void add(final InitialFunctionDefinition initialDefinition) {
203  0 final FunctionKey predicate = new FunctionKey(initialDefinition.getName(),
204    initialDefinition.getArgumentNumber());
205  0 if (functionExists(predicate)) {
206  0 throw new IllegalArgumentException(LogicErrors.FUNCTION_ALREADY_DEFINED_TEXT
207    + predicate);
208    }
209  0 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  6508 toggle public FunctionConstant get(final FunctionKey function) {
219  6508 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  1 toggle public FunctionConstant getFunction(final String name, final int arguments) {
230  1 final FunctionKey function = new FunctionKey(name, "" + arguments);
231  1 return get(function);
232    }
233   
 
234  2 toggle public boolean isInitialFunction(final FunctionKey function) {
235  2 final InitialFunctionDefinition initialDefinition
236    = (InitialFunctionDefinition) initialFunctionDefinitions.get(function);
237  2 return initialDefinition != null;
238    }
239   
240   
 
241  1655 toggle public boolean ruleExists(final RuleKey ruleKey) {
242  1655 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  1655 toggle public void add(final RuleKey ruleKey, final Rule definition) {
254  1655 if (ruleDefinitions.get(ruleKey) != null) {
255  1 throw new IllegalArgumentException(LogicErrors.RULE_ALREADY_DEFINED_TEXT
256    + ruleKey);
257    }
258  1654 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  5487 toggle public Rule get(final RuleKey ruleKey) {
268  5487 return (Rule) ruleDefinitions.get(ruleKey);
269    }
270   
 
271  0 toggle public boolean classOperatorExists() {
272  0 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  19819 toggle public boolean identityOperatorExists() {
287  19819 return this.identityOperator != null;
288    }
289   
 
290  9352 toggle public String getIdentityOperator() {
291  9352 return this.identityOperator;
292    }
293   
294    /**
295    * Set the identity operator.
296    *
297    * @param identityOperator Operator name. Might be <code>null</code>.
298    */
 
299  211 toggle public void setIdentityOperatorDefined(final String identityOperator) {
300  211 Trace.param(CLASS, this, "setIdentityOperatorDefined", "identityOperator", identityOperator);
301  211 this.identityOperator = identityOperator;
302    }
303   
304    }