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.zkb;
019:
020: import java.lang.reflect.Method;
021: import java.sql.SQLException;
022: import javax.sql.DataSource;
023: import org.mandarax.kernel.*;
024: import org.mandarax.kernel.meta.*;
025: import org.mandarax.kernel.validation.*;
026: import org.mandarax.reference.AdvancedKnowledgeBase;
027: import org.mandarax.reference.validation.TestCaseImpl;
028: import org.mandarax.sql.*;
029: import org.mandarax.util.LogicFactorySupport;
030: import org.mandarax.util.logging.LogCategories;
031: import java.util.*;
032:
033: /**
034: * Utility providing all kind of test data for ZKB test cases.
035: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
036: * @version 3.4 <7 March 05>
037: * @since 2.2
038: */
039: public class TestData implements LogCategories {
040:
041: static LogicFactory lfactory = LogicFactory.getDefaultFactory();
042: static LogicFactorySupport lfs = new LogicFactorySupport(lfactory);
043:
044: /**
045: * Get a simple predicate associating Strings.
046: * @return a simple predicate
047: */
048: public static SimplePredicate simplePredicate1() {
049: Class[] struct = { String.class, String.class };
050: SimplePredicate p = new SimplePredicate("is father of", struct);
051: return p;
052: }
053:
054: /**
055: * Get a simple predicate associating beans (=instances of Person).
056: * @return a simple predicate
057: */
058: public static SimplePredicate simplePredicate2() {
059: Class[] struct = { Person.class, Person.class };
060: // if this is renamed to "is father of" there will be failed test cases in
061: // the zkb module - but only due to the compare mathod used
062: // (equals would work, but compare also compares properties)
063: // by Jens, 26 Sept 03
064: SimplePredicate p = new SimplePredicate("is father of 2",
065: struct);
066: return p;
067: }
068:
069: /**
070: * Get a simple predicate associating Strings.
071: * @return a simple predicate
072: */
073: public static SimplePredicate simplePredicate3() {
074: Class[] struct = { Integer.class, Integer.class };
075: SimplePredicate p = new SimplePredicate(
076: "<numeric dummy predicate>", struct);
077:
078: return p;
079: }
080:
081: /**
082: * Get a j predicate
083: * @return a j predicate
084: */
085: public static JPredicate jPredicate() {
086: try {
087: Class clazz = Object.class;
088: Class[] par = { clazz };
089: Method m = clazz.getMethod("equals", par);
090: JPredicate p = new JPredicate(m);
091: p.setName("two objects are equal");
092: return p;
093: } catch (Exception x) {
094: LOG_TEST.error("Error building jpredicate", x);
095: }
096:
097: return null;
098: }
099:
100: /**
101: * Get a jfunction returning a string.
102: * @return a simple predicate
103: */
104: public static JFunction jFunction() {
105: try {
106: Class clazz = String.class;
107: Class[] par = { clazz };
108: Method m = clazz.getMethod("concat", par);
109: JFunction f = new JFunction(m);
110:
111: f.setName("concatenate two strings");
112:
113: return f;
114: } catch (Exception x) {
115: LOG_TEST.error("Error building jpredicate", x);
116: }
117:
118: return null;
119: }
120:
121: /**
122: * Get a SQL function returning a string.
123: * @return an sql function
124: */
125: public static SQLFunction sqlFunction1() {
126: SQLFunction f = new SQLFunction();
127:
128: f.setDataSource(dataSource());
129: f.setName("sample sql function");
130: f.setQuery("SELECT NAME FROM PEOPLE WHERE ID = ?");
131:
132: Class[] struct = { Integer.class };
133:
134: f.setStructure(struct);
135: f
136: .setObjectRelationalMapping(new OneColumnMapping(
137: String.class));
138:
139: return f;
140: }
141:
142: /**
143: * Get an SQL predicate associating two strings.
144: * @return an sql predicate
145: */
146: public static SQLPredicate sqlPredicate() {
147: SQLPredicate p = new SQLPredicate();
148:
149: p.setDataSource(dataSource());
150: p.setName("sample sql predicate");
151: p.setQuery("SELECT NAME,FATHER FROM FAMILY");
152: Class[] struct = { String.class, String.class };
153: p.setStructure(struct);
154: p.setTypeMapping(new DefaultTypeMapping());
155:
156: return p;
157: }
158:
159: /**
160: * Get a data source.
161: * @return a data source
162: */
163: public static DataSource dataSource() {
164: DummyDataSource ds = new DummyDataSource();
165:
166: try {
167: ds.setConnectString("jdbc:odbc:test");
168: ds.setDriver(sun.jdbc.odbc.JdbcOdbcDriver.class);
169: ds.setUserName("scott");
170: ds.setPassword("tiger");
171: ds.setLoginTimeout(5000);
172: } catch (SQLException x) {
173: LOG_TEST.error("Cannot initialize dummy data source", x);
174: }
175:
176: return ds;
177: }
178:
179: /**
180: * Get a simple rule.
181: * @return a rule
182: */
183: public static Rule rule1() {
184: return lfs.rule(prereq1(), fact1());
185: }
186:
187: /**
188: * Get a rule with two prerequisites connected by AND
189: * @return a rule
190: */
191: public static Rule andRule() {
192: Rule r = lfs.rule(prereq1(), prereq2(), fact3());
193: r.setBodyOrConnected(false);
194: return r;
195: }
196:
197: /**
198: * Get a rule with two prerequisites connected by AND
199: * @return a rule
200: */
201: public static Rule orRule() {
202: Rule r = lfs.rule(prereq1(), prereq2(), fact3());
203: r.setBodyOrConnected(true);
204: return r;
205: }
206:
207: /**
208: * Get a rule with a negated and an unnegated prerequisite connected by AND
209: * @return a rule
210: */
211: public static Rule negRule() {
212: Term[] terms = { lfs.variable("x", String.class),
213: lfs.variable("y", String.class) };
214: Prerequisite prereq1 = lfs.prereq(simplePredicate1(), terms,
215: true);
216: Prerequisite prereq2 = lfs.prereq(simplePredicate1(), terms,
217: false);
218: Fact concl = lfs.fact(simplePredicate1(), terms);
219: Rule r = lfs.rule(prereq1, prereq2, concl);
220: return r;
221: }
222:
223: /**
224: * Get a fact with a simple predicate and variable terms
225: * @return a fact
226: */
227: public static Fact fact1() {
228: Term[] terms = { lfs.variable("x", String.class),
229: lfs.variable("y", String.class) };
230: return lfs.fact(simplePredicate1(), terms);
231: }
232:
233: /**
234: * Get a fact with a simple predicate and variable terms
235: * @return a fact
236: */
237: public static Fact fact2() {
238: Term[] terms = { lfs.variable("a person", Person.class),
239: lfs.variable("another person", Person.class) };
240: return lfs.fact(simplePredicate2(), terms);
241: }
242:
243: /**
244: * Get a fact with a simple predicate and variable terms
245: * @return a fact
246: */
247: public static Fact fact3() {
248: Term[] terms = { lfs.variable("x", Integer.class),
249: lfs.variable("y", Integer.class) };
250: return lfs.fact(simplePredicate3(), terms);
251: }
252:
253: /**
254: * Get a prerequisite with a simple predicate and variable terms
255: * @return a prerequisite
256: */
257: public static Prerequisite prereq1() {
258: Term[] terms = { lfs.variable("x", String.class),
259: lfs.variable("y", String.class) };
260: return lfs.prereq(simplePredicate1(), terms);
261: }
262:
263: /**
264: * Get a prerequisite with a simple predicate and variable terms
265: * @return a prerequisite
266: */
267: public static Prerequisite prereq2() {
268: Term[] terms = { lfs.variable("a person", Person.class),
269: lfs.variable("another person", Person.class) };
270: return lfs.prereq(simplePredicate2(), terms);
271: }
272:
273: /**
274: * Get a prerequisite with a simple predicate and variable terms
275: * @return a prerequisite
276: */
277: public static Prerequisite prereq3() {
278: Term[] terms = { lfs.variable("x", Integer.class),
279: lfs.variable("y", Integer.class) };
280: return lfs.prereq(simplePredicate3(), terms);
281: }
282:
283: /**
284: * Get a fact with a simple predicate and constant terms
285: * @return a fact
286: */
287: public static Fact fact4() {
288: Term[] terms = { lfs.cons("Jens", String.class),
289: lfs.cons("Max", String.class) };
290:
291: return lfs.fact(simplePredicate1(), terms);
292: }
293:
294: /**
295: * Get a fact with a simple predicate and constant terms
296: * @return a fact
297: */
298: public static Fact fact5() {
299: Person p1 = new Person(1, "Jens", 1966, 0, 12);
300: Person p2 = new Person(2, "Max", 1993, 10, 10);
301: Term[] terms = { lfs.cons(p1, Person.class),
302: lfs.cons(p2, Person.class) };
303: return lfs.fact(simplePredicate2(), terms);
304: }
305:
306: /**
307: * Get a fact with a simple predicate and constant terms
308: * @return a fact
309: */
310: public static Fact fact6() {
311: Term[] terms = { lfs.cons(new Integer(3), Integer.class),
312: lfs.cons(new Integer(42), Integer.class) };
313: return lfs.fact(simplePredicate3(), terms);
314: }
315:
316: /**
317: * Get a fact with a jpredicate and variable terms.
318: * @return a fact
319: */
320: public static Fact fact7() {
321: Term[] terms = { lfs.variable("x", Object.class),
322: lfs.variable("y", Object.class) };
323: return lfs.fact(jPredicate(), terms);
324: }
325:
326: /**
327: * Get a fact with a simple predicate, a jfunction and variable terms.
328: * @return a fact
329: */
330: public static Fact fact8() {
331: Term[] terms = {
332: lfs.cplx(jFunction(), lfs.variable("x", String.class),
333: lfs.variable("y", String.class)),
334: lfs.variable("z", String.class) };
335: return lfs.fact(simplePredicate1(), terms);
336: }
337:
338: /**
339: * Get a fact with a sql predicate and variable terms.
340: * @return a fact
341: */
342: public static Fact fact9() {
343: Term[] terms = { lfs.variable("x", String.class),
344: lfs.variable("y", String.class) };
345: return lfs.fact(sqlPredicate(), terms);
346: }
347:
348: /**
349: * Get a fact with a simple predicate, a sql function and variable terms.
350: * @return a fact
351: */
352: public static Fact fact10() {
353: Term[] terms = {
354: lfs.cplx(sqlFunction1(), lfs.variable("x",
355: Integer.class)),
356: lfs.variable("z", String.class) };
357: return lfs.fact(simplePredicate1(), terms);
358: }
359:
360: /**
361: * Get a SQL clause set.
362: * @return a fact
363: */
364: public static SQLClauseSet sqlClauseSet() {
365: return new SQLClauseSet(sqlPredicate(), "", 1000000);
366: }
367:
368: /**
369: * Get a query.
370: * @return a query
371: */
372: public static Query query1() {
373: return lfs.query(fact1(), "a query");
374: }
375:
376: /**
377: * Get a query.
378: * @return a query
379: */
380: public static Query query2() {
381: return lfs.query(fact1(), fact2(), "a query");
382: }
383:
384: /**
385: * Get a query.
386: * @return a query
387: */
388: public static Query query3() {
389: return lfs.query(fact3(), "a query");
390: }
391:
392: /**
393: * Get a test case.
394: * @return a test case.
395: */
396: public static TestCase testCase1() {
397: // very basic
398: Query query = query1();
399: ClauseSet[] assumptions = { fact1() };
400: Map[] expectedReplacements = new Map[0];
401: return lfactory.createTestCase(query, assumptions,
402: TestCase.AT_BOTTOM, -1, true, expectedReplacements);
403: }
404:
405: /**
406: * Get a test case.
407: * @return a test case.
408: */
409: public static TestCase testCase2() {
410: // very basic
411: Query query = query1();
412: ClauseSet[] assumptions = { fact1() };
413: Map[] expectedReplacements = new Map[0];
414: return lfactory.createTestCase(query, assumptions,
415: TestCase.AT_BOTTOM, -1, false, expectedReplacements);
416: }
417:
418: /**
419: * Get a test case.
420: * @return a test case.
421: */
422: public static TestCase testCase3() {
423: Query query = query2();
424: ClauseSet[] assumptions = { fact1() };
425: Map[] expectedReplacements = new Map[0];
426: return lfactory.createTestCase(query, assumptions,
427: TestCase.AT_BOTTOM, 42, true, expectedReplacements);
428: }
429:
430: /**
431: * Get a test case.
432: * @return a test case.
433: */
434: public static TestCase testCase4() {
435: Query query = query2();
436: ClauseSet[] assumptions = { fact1(), fact2(), andRule(),
437: orRule(), sqlClauseSet() };
438: Map[] expectedReplacements = new Map[0];
439: return lfactory.createTestCase(query, assumptions,
440: TestCase.AT_BOTTOM, 42, true, expectedReplacements);
441: }
442:
443: /**
444: * Get a test case.
445: * @return a test case.
446: */
447: public static TestCase testCase5() {
448: Query query = query2();
449: ClauseSet[] assumptions = { fact1() };
450: Map[] expectedReplacements = new Map[1];
451: expectedReplacements[0] = new Hashtable();
452: expectedReplacements[0].put(lfactory.createVariableTerm("x",
453: Integer.TYPE), new Integer(42));
454: return lfactory.createTestCase(query, assumptions,
455: TestCase.ON_TOP, 42, true, expectedReplacements);
456: }
457:
458: /**
459: * Get a test case.
460: * @return a test case.
461: */
462: public static TestCase testCase6() {
463: Query query = query2();
464: ClauseSet[] assumptions = { fact1(), fact2(), andRule(),
465: orRule(), sqlClauseSet() };
466: Map[] expectedReplacements = new Map[2];
467: expectedReplacements[0] = new Hashtable();
468: expectedReplacements[0].put(lfactory.createVariableTerm("x",
469: Integer.TYPE), new Integer(42));
470: expectedReplacements[0].put(lfactory.createVariableTerm("dob",
471: java.util.Date.class), new Date());
472: expectedReplacements[1] = new Hashtable();
473: expectedReplacements[1].put(lfactory.createVariableTerm("x",
474: String.class), "test");
475: return lfactory.createTestCase(query, assumptions,
476: TestCase.ON_TOP, 42, true, expectedReplacements);
477: }
478:
479: }
|