001: package org.mandarax.reference;
002:
003: /*
004: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: import java.util.Map;
021:
022: import org.mandarax.kernel.ClauseSet;
023: import org.mandarax.kernel.ComplexTerm;
024: import org.mandarax.kernel.ConstantTerm;
025: import org.mandarax.kernel.Fact;
026: import org.mandarax.kernel.Function;
027: import org.mandarax.kernel.LogicFactory;
028: import org.mandarax.kernel.Predicate;
029: import org.mandarax.kernel.Prerequisite;
030: import org.mandarax.kernel.Query;
031: import org.mandarax.kernel.Rule;
032: import org.mandarax.kernel.Term;
033: import org.mandarax.kernel.VariableTerm;
034: import org.mandarax.kernel.validation.TestCase;
035: import org.mandarax.lib.Cut;
036: import org.mandarax.reference.validation.*;
037:
038: /**
039: * Factory object for creating logical entities objects.
040: * The classes with the postfix <it>Impl</it> should never be instanciated
041: * directly. Instead use instances of this class. To use alternative
042: * implementations, subclass this class. <br> To install an instance of this
043: * class to be the default factory use the following code:
044: * <code>(new DefaultLogicFactory()).install()</code>. If this is done, a
045: * reference to the factory can be obtained using
046: * <code>LogicFactory.getDefaultFactory()</code>.
047: * <br>
048: * As from 1.9, queries are supported.
049: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
050: * @version 3.4 <7 March 05>
051: * @since 1.1
052: */
053: public class DefaultLogicFactory extends LogicFactory {
054: private Prerequisite cut = null;
055:
056: /**
057: * Constructor.
058: */
059: public DefaultLogicFactory() {
060: super ();
061: }
062:
063: /**
064: * Create a new complex term.
065: * @return the created complex term
066: * @param anFunction a function
067: * @param terms an array of terms
068: */
069: public ComplexTerm createComplexTerm(Function anFunction,
070: Term[] terms) {
071: return new ComplexTermImpl(anFunction, terms);
072: }
073:
074: /**
075: * Create a new constant term.
076: * @return the created constant term
077: * @param obj the object that is to be wrapped by the constant term
078: */
079: public ConstantTerm createConstantTerm(Object obj) {
080: return new ConstantTermImpl(obj);
081: }
082:
083: /**
084: * Create a new constant term.
085: * @return a new constant term
086: * @param obj the wrapped object
087: * @param type the type of the object
088: * @throws throws an IllegalArgumentException if object and type are inconsistent,
089: * i.e. if type object is not an instance of type
090: */
091: public ConstantTerm createConstantTerm(Object obj, Class type) {
092: return new ConstantTermImpl(obj, type);
093: }
094:
095: /**
096: * Create a new fact.
097: * @return the created fact
098: * @param aPredicate the predicate
099: * @param terms the terms
100: */
101: public Fact createFact(Predicate aPredicate, Term[] terms) {
102: return new FactImpl(aPredicate, terms);
103: }
104:
105: /**
106: * Create a new prerequisite.
107: * @return a new prerequisite
108: * @param aPredicate a predicate
109: * @param terms an array of terms
110: * @param negatedAF whether the prerequisite is negated (as failure)
111: */
112: public Prerequisite createPrerequisite(Predicate aPredicate,
113: Term[] terms, boolean negatedAF) {
114: return new PrerequisiteImpl(aPredicate, terms, negatedAF);
115: }
116:
117: /**
118: * Create a cut prerequisite.
119: * @return a prerequisite
120: */
121: public Prerequisite createCut() {
122: // 2.2: Each cut has to be a unique instance
123: // if (cut==null) {
124: cut = new PrerequisiteImpl(new Cut(), new Term[] {}, false) {
125: /**
126: * Indicates whether the prerequisite is negated (negation as failure)
127: * @return true if the prerequisite is negated, false otherwise
128: */
129: public boolean isNegatedAF() {
130: return false;
131: }
132:
133: /**
134: * Set the negated (as failure) flag.
135: * @param value true or false
136: */
137: public void setNegatedAF(boolean value) {
138: // ignored
139: }
140: };
141: // }
142: return cut;
143: }
144:
145: /**
146: * Create a new rule.
147: * @return the created rule
148: * @param body a list of facts that will become the body of the rule
149: * @param head a fact that will become the head of the rule
150: */
151: public Rule createRule(java.util.List body, Fact head) {
152: return new RuleImpl(body, head);
153: }
154:
155: /**
156: * Create a new rule.
157: * @return a new rule
158: * @param body a list facts becoming the body of the rules
159: * @param head the head of the rule
160: * @param or indicates whether the prerequisites are connected by OR
161: */
162: public Rule createRule(java.util.List body, Fact head, boolean or) {
163: return new RuleImpl(body, head, or);
164: }
165:
166: /**
167: * Create a new rule with an empty body.
168: * @return the created rule
169: * @param head the fact that will become the head of the rule
170: */
171: public Rule createRule(Fact head) {
172: return createRule(new java.util.ArrayList(), head);
173: }
174:
175: /**
176: * Create a new variable term.
177: * @return the created variable term
178: * @param aName the name of the variable
179: * @param aType the type of the variable
180: */
181: public VariableTerm createVariableTerm(String aName, Class aType) {
182: return new VariableTermImpl(aName, aType);
183: }
184:
185: /**
186: * Create a new query.
187: * @return a new query
188: * @param fact aFact
189: * @param name the name of the query
190: */
191: public Query createQuery(Fact fact, String name) {
192: Fact[] facts = { fact };
193: return createQuery(facts, name);
194: }
195:
196: /**
197: * Create a new query.
198: * @return a new query
199: * @param facts an array of facts
200: * @param name the name of the query
201: */
202: public Query createQuery(Fact[] facts, String name) {
203: return new QueryImpl(facts, name);
204: }
205:
206: /**
207: * Create a new test case.
208: * @return a new test case
209: * @param aQuery a <strong>ground</strong> query
210: * @param assumptions some assumptions
211: * @param policyToAddAssumptionsToKB one of the integers in TestCase (TestCase.ON_TOP, TestCase.AT_BOTTOM)
212: * @param expectedResult true or false
213: * @see org.mandarax.kernel.validation.TestCase
214: */
215: public TestCase createTestCase(Query aQuery,
216: ClauseSet[] assumptions, int policyToAddAssumptionsToKB,
217: boolean expectedResult) {
218: return new TestCaseImpl(assumptions,
219: policyToAddAssumptionsToKB, aQuery, expectedResult);
220: }
221:
222: /**
223: * Create a new test case.
224: * @return a new test case
225: * @param aQuery a <strong>ground</strong> query
226: * @param assumptions some assumptions
227: * @param policyToAddAssumptionsToKB one of the integers in TestCase (TestCase.ON_TOP, TestCase.AT_BOTTOM)
228: * @param expectedNumberOfResults the expected number of results
229: */
230: public TestCase createTestCase(Query aQuery,
231: ClauseSet[] assumptions, int policyToAddAssumptionsToKB,
232: int expectedNumberOfResults) {
233: return new TestCaseImpl(assumptions,
234: policyToAddAssumptionsToKB, aQuery,
235: expectedNumberOfResults);
236: }
237:
238: /**
239: * Create a new test case.
240: * @return a new test case
241: * @param aQuery a <strong>ground</strong> query
242: * @param assumptions some assumptions
243: * @param policyToAddAssumptionsToKB one of the integers in TestCase (TestCase.ON_TOP, TestCase.AT_BOTTOM)
244: * @param expectedReplacements an array of expected replacements, each map contains VariableTerm -> Object associations
245: */
246: public TestCase createTestCase(Query aQuery,
247: ClauseSet[] assumptions, int policyToAddAssumptionsToKB,
248: Map[] expectedReplacements) {
249: return new TestCaseImpl(assumptions,
250: policyToAddAssumptionsToKB, aQuery,
251: expectedReplacements);
252: }
253:
254: /**
255: * Create a new test case.
256: * Note that the parameters are somehow redundant, expectedResult only makes sense if the query is ground while
257: * expectedReplacements only makes sense if the query contains variables. This is a general purpose
258: * creator that can be used in modules such as ZKB.
259: * @return a new test case
260: * @param aQuery a <strong>ground</strong> query
261: * @param assumptions some assumptions
262: * @param policyToAddAssumptionsToKB one of the integers in TestCase (TestCase.ON_TOP, TestCase.AT_BOTTOM)
263: * @param expectedNumberOfResults the expected number of results
264: * @param expectedResult true or false
265: * @param expectedReplacements an array of expected replacements, each map contains VariableTerm -> Object associations
266: */
267: public TestCase createTestCase(Query aQuery,
268: ClauseSet[] assumptions, int policyToAddAssumptionsToKB,
269: int expectedNumberOfResults, boolean expectedResult,
270: Map[] expectedReplacements) {
271: return new TestCaseImpl(assumptions,
272: policyToAddAssumptionsToKB, aQuery,
273: expectedNumberOfResults, expectedReplacements,
274: expectedResult);
275: }
276:
277: /**
278: * Return the implementation class for facts.
279: * @return a class
280: */
281: public Class getFactImplementationClass() {
282: return FactImpl.class;
283: }
284:
285: /**
286: * Return the implementation class for rules.
287: * @return a class
288: */
289: public Class getRuleImplementationClass() {
290: return RuleImpl.class;
291: }
292:
293: /**
294: * Return the implementation class for prerequisites.
295: * @return a class
296: */
297: public Class getPrerequisiteImplementationClass() {
298: return PrerequisiteImpl.class;
299: }
300:
301: /**
302: * Return the implementation class for queries.
303: * @return a class
304: */
305: public Class getQueryImplementationClass() {
306: return QueryImpl.class;
307: }
308:
309: /**
310: * Return the implementation class for constant terms.
311: * @return a class
312: */
313: public Class getConstantTermImplementationClass() {
314: return ConstantTermImpl.class;
315: }
316:
317: /**
318: * Return the implementation class for variable terms.
319: * @return a class
320: */
321: public Class getVariableTermImplementationClass() {
322: return VariableTermImpl.class;
323: }
324:
325: /**
326: * Return the implementation class for complex terms.
327: * @return a class
328: */
329: public Class getComplexTermImplementationClass() {
330: return ComplexTermImpl.class;
331: }
332: }
|