001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package test.org.mandarax.xkb;
019:
020: import java.lang.reflect.Method;
021: import java.sql.SQLException;
022: import javax.sql.DataSource;
023: import org.mandarax.kernel.Fact;
024: import org.mandarax.kernel.LogicFactory;
025: import org.mandarax.kernel.Prerequisite;
026: import org.mandarax.kernel.Query;
027: import org.mandarax.kernel.Rule;
028: import org.mandarax.kernel.SimplePredicate;
029: import org.mandarax.kernel.Term;
030: import org.mandarax.kernel.meta.DynaBeanFunction;
031: import org.mandarax.kernel.meta.JFunction;
032: import org.mandarax.kernel.meta.JPredicate;
033: import org.mandarax.sql.DefaultTypeMapping;
034: import org.mandarax.sql.OneColumnMapping;
035: import org.mandarax.sql.SQLClauseSet;
036: import org.mandarax.sql.SQLFunction;
037: import org.mandarax.sql.SQLPredicate;
038: import org.mandarax.util.LogicFactorySupport;
039: import org.mandarax.util.logging.LogCategories;
040:
041: /**
042: * Utility providing all kind of test data for XKB test cases.
043: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
044: * @version 3.4 <7 March 05>
045: * @since 1.6
046: */
047: public class TestData implements LogCategories {
048:
049: static LogicFactory lfactory = LogicFactory.getDefaultFactory();
050: static LogicFactorySupport lfs = new LogicFactorySupport(lfactory);
051:
052: /**
053: * Get a simple predicate associating Strings.
054: * @return a simple predicate
055: */
056: public static SimplePredicate simplePredicate1() {
057: Class[] struct = { String.class, String.class };
058: SimplePredicate p = new SimplePredicate("is father of", struct);
059: return p;
060: }
061:
062: /**
063: * Get a simple predicate associating beans (=instances of Person).
064: * @return a simple predicate
065: */
066: public static SimplePredicate simplePredicate2() {
067: Class[] struct = { Person.class, Person.class };
068: SimplePredicate p = new SimplePredicate("is father of", struct);
069: return p;
070: }
071:
072: /**
073: * Get a simple predicate associating Strings.
074: * @return a simple predicate
075: */
076: public static SimplePredicate simplePredicate3() {
077: Class[] struct = { Integer.class, Integer.class };
078: SimplePredicate p = new SimplePredicate(
079: "<numeric dummy predicate>", struct);
080:
081: return p;
082: }
083:
084: /**
085: * Get a jpredicate
086: * @return a jpredicate
087: */
088: public static JPredicate jPredicate() {
089: try {
090: Class clazz = Object.class;
091: Class[] par = { clazz };
092: Method m = clazz.getMethod("equals", par);
093: JPredicate p = new JPredicate(m);
094: p.setName("two objects are equal");
095: return p;
096: } catch (Exception x) {
097: LOG_TEST.error("Error building jpredicate", x);
098: }
099:
100: return null;
101: }
102:
103: /**
104: * Get a jfunction returning a string.
105: * @return a jfunction
106: */
107: public static JFunction jFunction() {
108: try {
109: Class clazz = String.class;
110: Class[] par = { clazz };
111: Method m = clazz.getMethod("concat", par);
112: JFunction f = new JFunction(m);
113:
114: f.setName("concatenate two strings");
115:
116: return f;
117: } catch (Exception x) {
118: LOG_TEST.error("Error building jpredicate", x);
119: }
120:
121: return null;
122: }
123:
124: /**
125: * Get a dynabeanfunction returning a string.
126: * @return a dyna bean function
127: */
128: public static DynaBeanFunction dynaBeanFunction() {
129: try {
130: Class clazz = String.class;
131: Class[] par = { clazz };
132: Method m = clazz.getMethod("concat", par);
133: DynaBeanFunction f = new DynaBeanFunction(m,
134: "an extension", "concat");
135:
136: f.setName("concatenate two strings");
137:
138: return f;
139: } catch (Exception x) {
140: LOG_TEST.error("Error building dyna bean predicate", x);
141: }
142:
143: return null;
144: }
145:
146: /**
147: * Get a SQL function returning a string.
148: * @return an sql function
149: */
150: public static SQLFunction sqlFunction1() {
151: SQLFunction f = new SQLFunction();
152:
153: f.setDataSource(dataSource());
154: f.setName("sample sql function");
155: f.setQuery("SELECT NAME FROM PEOPLE WHERE ID = ?");
156:
157: Class[] struct = { Integer.class };
158:
159: f.setStructure(struct);
160: f
161: .setObjectRelationalMapping(new OneColumnMapping(
162: String.class));
163:
164: return f;
165: }
166:
167: /**
168: * Get an SQL predicate associating two strings.
169: * @return an sql predicate
170: */
171: public static SQLPredicate sqlPredicate() {
172: SQLPredicate p = new SQLPredicate();
173:
174: p.setDataSource(dataSource());
175: p.setName("sample sql predicate");
176: p.setQuery("SELECT NAME,FATHER FROM FAMILY");
177: Class[] struct = { String.class, String.class };
178: p.setStructure(struct);
179: p.setTypeMapping(new DefaultTypeMapping());
180:
181: return p;
182: }
183:
184: /**
185: * Get a data source.
186: * @return a data source
187: */
188: public static DataSource dataSource() {
189: DummyDataSource ds = new DummyDataSource();
190:
191: try {
192: ds.setConnectString("jdbc:odbc:test");
193: ds.setDriver(sun.jdbc.odbc.JdbcOdbcDriver.class);
194: ds.setUserName("scott");
195: ds.setPassword("tiger");
196: ds.setLoginTimeout(5000);
197: } catch (SQLException x) {
198: LOG_TEST.error("Cannot initialize dummy data source", x);
199: }
200:
201: return ds;
202: }
203:
204: /**
205: * Get a simple rule.
206: * @return a rule
207: */
208: public static Rule rule1() {
209: return lfs.rule(prereq1(), fact1());
210: }
211:
212: /**
213: * Get a rule with two prerequisites connected by AND
214: * @return a rule
215: */
216: public static Rule andRule() {
217: Rule r = lfs.rule(prereq1(), prereq2(), fact3());
218: r.setBodyOrConnected(false);
219: return r;
220: }
221:
222: /**
223: * Get a rule with two prerequisites connected by AND
224: * @return a rule
225: */
226: public static Rule orRule() {
227: Rule r = lfs.rule(prereq1(), prereq2(), fact3());
228: r.setBodyOrConnected(true);
229: return r;
230: }
231:
232: /**
233: * Get a rule with a negated and an unnegated prerequisite connected by AND
234: * @return a rule
235: */
236: public static Rule negRule() {
237: Term[] terms = { lfs.variable("x", String.class),
238: lfs.variable("y", String.class) };
239: Prerequisite prereq1 = lfs.prereq(simplePredicate1(), terms,
240: true);
241: Prerequisite prereq2 = lfs.prereq(simplePredicate1(), terms,
242: false);
243: Fact concl = lfs.fact(simplePredicate1(), terms);
244: Rule r = lfs.rule(prereq1, prereq2, concl);
245: return r;
246: }
247:
248: /**
249: * Get a fact with a simple predicate and variable terms
250: * @return a fact
251: */
252: public static Fact fact1() {
253: Term[] terms = { lfs.variable("x", String.class),
254: lfs.variable("y", String.class) };
255: return lfs.fact(simplePredicate1(), terms);
256: }
257:
258: /**
259: * Get a fact with a simple predicate and variable terms
260: * @return a fact
261: */
262: public static Fact fact2() {
263: Term[] terms = { lfs.variable("a person", Person.class),
264: lfs.variable("another person", Person.class) };
265: return lfs.fact(simplePredicate2(), terms);
266: }
267:
268: /**
269: * Get a fact with a simple predicate and variable terms
270: * @return a fact
271: */
272: public static Fact fact3() {
273: Term[] terms = { lfs.variable("x", Integer.class),
274: lfs.variable("y", Integer.class) };
275: return lfs.fact(simplePredicate3(), terms);
276: }
277:
278: /**
279: * Get a prerequisite with a simple predicate and variable terms
280: * @return a prerequisite
281: */
282: public static Prerequisite prereq1() {
283: Term[] terms = { lfs.variable("x", String.class),
284: lfs.variable("y", String.class) };
285: return lfs.prereq(simplePredicate1(), terms);
286: }
287:
288: /**
289: * Get a prerequisite with a simple predicate and variable terms
290: * @return a prerequisite
291: */
292: public static Prerequisite prereq2() {
293: Term[] terms = { lfs.variable("a person", Person.class),
294: lfs.variable("another person", Person.class) };
295: return lfs.prereq(simplePredicate2(), terms);
296: }
297:
298: /**
299: * Get a prerequisite with a simple predicate and variable terms
300: * @return a prerequisite
301: */
302: public static Prerequisite prereq3() {
303: Term[] terms = { lfs.variable("x", Integer.class),
304: lfs.variable("y", Integer.class) };
305: return lfs.prereq(simplePredicate3(), terms);
306: }
307:
308: /**
309: * Get a fact with a simple predicate and constant terms
310: * @return a fact
311: */
312: public static Fact fact4() {
313: Term[] terms = { lfs.cons("Jens", String.class),
314: lfs.cons("Max", String.class) };
315:
316: return lfs.fact(simplePredicate1(), terms);
317: }
318:
319: /**
320: * Get a fact with a simple predicate and constant terms
321: * @return a fact
322: */
323: public static Fact fact5() {
324: Person p1 = new Person(1, "Jens", 1966, 0, 12);
325: Person p2 = new Person(2, "Max", 1993, 10, 10);
326: Term[] terms = { lfs.cons(p1, Person.class),
327: lfs.cons(p2, Person.class) };
328: return lfs.fact(simplePredicate2(), terms);
329: }
330:
331: /**
332: * Get a fact with a simple predicate and constant terms
333: * @return a fact
334: */
335: public static Fact fact6() {
336: Term[] terms = { lfs.cons(new Integer(3), Integer.class),
337: lfs.cons(new Integer(42), Integer.class) };
338: return lfs.fact(simplePredicate3(), terms);
339: }
340:
341: /**
342: * Get a fact with a jpredicate and variable terms.
343: * @return a fact
344: */
345: public static Fact fact7() {
346: Term[] terms = { lfs.variable("x", Object.class),
347: lfs.variable("y", Object.class) };
348: return lfs.fact(jPredicate(), terms);
349: }
350:
351: /**
352: * Get a fact with a simple predicate, a jfunction and variable terms.
353: * @return a fact
354: */
355: public static Fact fact8() {
356: Term[] terms = {
357: lfs.cplx(jFunction(), lfs.variable("x", String.class),
358: lfs.variable("y", String.class)),
359: lfs.variable("z", String.class) };
360: return lfs.fact(simplePredicate1(), terms);
361: }
362:
363: /**
364: * Get a fact with a sql predicate and variable terms.
365: * @return a fact
366: */
367: public static Fact fact9() {
368: Term[] terms = { lfs.variable("x", String.class),
369: lfs.variable("y", String.class) };
370: return lfs.fact(sqlPredicate(), terms);
371: }
372:
373: /**
374: * Get a fact with a simple predicate, a sql function and variable terms.
375: * @return a fact
376: */
377: public static Fact fact10() {
378: Term[] terms = {
379: lfs.cplx(sqlFunction1(), lfs.variable("x",
380: Integer.class)),
381: lfs.variable("z", String.class) };
382: return lfs.fact(simplePredicate1(), terms);
383: }
384:
385: /**
386: * Get a fact with a simple predicate, a dyna bean function and variable terms.
387: * @return a fact
388: */
389: public static Fact fact11() {
390: Term[] terms = {
391: lfs.cplx(dynaBeanFunction(), lfs.variable("x",
392: String.class)), lfs.variable("z", String.class) };
393: return lfs.fact(simplePredicate1(), terms);
394: }
395:
396: /**
397: * Get a SQL clause set.
398: * @return a fact
399: */
400: public static SQLClauseSet sqlClauseSet() {
401: return new SQLClauseSet(sqlPredicate(), "", 1000000);
402: }
403:
404: /**
405: * Get a query.
406: * @return a query
407: */
408: public static Query query1() {
409: return lfs.query(fact1(), "a query");
410: }
411:
412: /**
413: * Get a query.
414: * @return a query
415: */
416: public static Query query2() {
417: return lfs.query(fact1(), fact2(), "a query");
418: }
419:
420: /**
421: * Get a query.
422: * @return a query
423: */
424: public static Query query3() {
425: return lfs.query(fact3(), "a query");
426: }
427: }
|