001: /******************************************************************
002: * File: TestBasics.java
003: * Created by: Dave Reynolds
004: * Created on: 30-Mar-03
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: TestBasics.java,v 1.41 2008/01/02 12:08:20 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.reasoner.rulesys.test;
010:
011: import com.hp.hpl.jena.rdf.model.*;
012: import com.hp.hpl.jena.reasoner.*;
013: import com.hp.hpl.jena.reasoner.rulesys.*;
014: import com.hp.hpl.jena.reasoner.rulesys.impl.*;
015: import com.hp.hpl.jena.reasoner.test.TestUtil;
016: import com.hp.hpl.jena.graph.*;
017: import com.hp.hpl.jena.util.PrintUtil;
018: import com.hp.hpl.jena.vocabulary.*;
019:
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022: import java.util.*;
023: import java.io.*;
024:
025: /**
026: * Unit tests for simple infrastructure pieces of the rule systems.
027: *
028: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
029: * @version $Revision: 1.41 $ on $Date: 2008/01/02 12:08:20 $
030: */
031: public class TestBasics extends TestCase {
032:
033: // Maximum size of binding environment needed in the tests
034: private static final int MAX_VARS = 10;
035:
036: // Useful constants
037: Node p = Node.createURI("p");
038: Node q = Node.createURI("q");
039: Node r = Node.createURI("r");
040: Node s = Node.createURI("s");
041: Node n1 = Node.createURI("n1");
042: Node n2 = Node.createURI("n2");
043: Node n3 = Node.createURI("n3");
044: Node n4 = Node.createURI("n4");
045: Node n5 = Node.createURI("n5");
046: Node res = Node.createURI("res");
047:
048: /**
049: * Boilerplate for junit
050: */
051: public TestBasics(String name) {
052: super (name);
053: }
054:
055: /**
056: * Boilerplate for junit.
057: * This is its own test suite
058: */
059: public static TestSuite suite() {
060: return new TestSuite(TestBasics.class);
061: }
062:
063: /**
064: * Test the internal rule parser
065: */
066: public void testRuleParser() {
067: String[] testRules = new String[] {
068: "(?a rdf:type ?_) -> (?a rdf:type ?b).",
069: "(?a rdf:type ?_), (?a rdf:type ?_) -> (?a rdf:type ?b).",
070: "(?a rdf:type max(?a,1)) -> (?a rdf:type 'foo').",
071: "(?a rdf:type ?_) -> addOne(?a).",
072: "(?a rdf:type ?_) -> [(?a rdf:type ?_) -> addOne(?a)].",
073: "(?a rdf:type ?_) -> (?a rdf:type '42').",
074: "(?a rdf:type ?_) -> (?a rdf:type 4.2).",
075: "(?a rdf:type ?_) -> (?a rdf:type ' fool that,I(am)').",
076: "[rule1: (?a rdf:type ?_) -> (?a rdf:type a)]",
077: "-> print(' ').",
078: "-> print(' literal with embedded \\' characters ').",
079: "-> print(\" literal characters \").",
080: "-> print(42). ",
081: "-> print('42'^^xsd:byte). ",
082: "-> print('42'^^http://www.w3.org/2001/XMLSchema#int). ",
083: "-> print('42'^^foobar:byte). ",
084: "-> print(<foo://a/file>). " };
085: String[] testResults = new String[] {
086: "[ (?a rdf:type ?_) -> (?a rdf:type ?b) ]",
087: "[ (?a rdf:type ?_) (?a rdf:type ?_) -> (?a rdf:type ?b) ]",
088: "[ (?a rdf:type 'max(?a '1'^^http://www.w3.org/2001/XMLSchema#int)'^^urn:x-hp-jena:Functor) -> (?a rdf:type 'foo') ]",
089: "[ (?a rdf:type ?_) -> addOne(?a) ]",
090: "[ (?a rdf:type ?_) -> [ (?a rdf:type ?_) -> addOne(?a) ] ]",
091: "[ (?a rdf:type ?_) -> (?a rdf:type '42') ]",
092: "[ (?a rdf:type ?_) -> (?a rdf:type '4.2'^^http://www.w3.org/2001/XMLSchema#float) ]",
093: "[ (?a rdf:type ?_) -> (?a rdf:type ' fool that,I(am)') ]",
094: "[ rule1: (?a rdf:type ?_) -> (?a rdf:type a) ]",
095: "[ -> print(' ') ]",
096: "[ -> print(' literal with embedded ' characters ') ]",
097: "[ -> print(' literal characters ') ]",
098: "[ -> print('42'^^http://www.w3.org/2001/XMLSchema#int) ]",
099: "[ -> print('42'^^http://www.w3.org/2001/XMLSchema#byte) ]",
100: "[ -> print('42'^^http://www.w3.org/2001/XMLSchema#int) ]",
101: "[ -> print('42'^^http://foobar#byte) ]",
102: "[ -> print(foo://a/file) ]", };
103:
104: PrintUtil.registerPrefix("foobar", "http://foobar#");
105: for (int i = 0; i < testRules.length; i++) {
106: Rule r = Rule.parseRule(testRules[i]);
107: assertEquals(testResults[i], r.toString());
108: }
109:
110: // Test for an illegal rule format
111: String[] testBadRules = new String[] {
112: "(foo(?A) eg:p ?B) <- (?a, eg:p, ?B).",
113: "(foo(?A) eg:p ?B) -> (?a, eg:p, ?B)." };
114: for (int i = 0; i < testBadRules.length; i++) {
115: boolean foundError = false;
116: try {
117: Rule r = Rule.parseRule(testBadRules[i]);
118: } catch (Rule.ParserException e) {
119: foundError = true;
120: }
121: assertTrue("Failed to find illegal rule", foundError);
122: }
123: }
124:
125: /**
126: * Test rule equality operations.
127: */
128: public void testRuleEquality() {
129: Rule r1 = Rule.parseRule("(?a p ?b) -> (?a q ?b).");
130: Rule r2 = Rule.parseRule("(?a p ?b) -> (?b q ?a).");
131: Rule r1b = Rule.parseRule("(?x p ?y) -> (?x q ?y).");
132: Rule r3 = Rule.parseRule("(?a p ?b), addOne(?a) -> (?a q ?b).");
133: Rule r3b = Rule
134: .parseRule("(?c p ?d), addOne(?c) -> (?c q ?d).");
135: Rule r4 = Rule
136: .parseRule("(?a p ?b), makeTemp(?a) -> (?a q ?b).");
137: Rule r5 = Rule.parseRule("(?a p ?b), addOne(?b) -> (?a q ?b).");
138: Rule r6 = Rule.parseRule("(?a p ?b), addOne(p) -> (?a q ?b).");
139: assertTrue(!r1.equals(r2));
140: assertTrue(r1.equals(r1b));
141: assertTrue(!r1.equals(r3));
142: assertTrue(r3.equals(r3b));
143: assertTrue(!r3.equals(r4));
144: assertTrue(!r3.equals(r5));
145: assertTrue(!r3.equals(r6));
146: }
147:
148: /**
149: * Test the BindingEnvironment machinery
150: */
151: public void testBindingEnvironment() {
152: BindingStack env = new BindingStack();
153: env.reset(MAX_VARS);
154:
155: env.bind(3, n1);
156: assertEquals(n1, env.getEnvironment()[3]);
157: env.push();
158: env.bind(2, n2);
159: assertEquals(n2, env.getEnvironment()[2]);
160: env.unwind();
161: assertEquals(null, env.getEnvironment()[2]);
162: assertEquals(n1, env.getEnvironment()[3]);
163: env.push();
164: env.bind(1, n3);
165: assertEquals(null, env.getEnvironment()[2]);
166: assertEquals(n1, env.getEnvironment()[3]);
167: assertEquals(n3, env.getEnvironment()[1]);
168: env.unwind();
169: assertEquals(null, env.getEnvironment()[2]);
170: assertEquals(n1, env.getEnvironment()[3]);
171: assertEquals(null, env.getEnvironment()[1]);
172: env.push();
173: env.bind(1, n3);
174: assertEquals(null, env.getEnvironment()[2]);
175: assertEquals(n1, env.getEnvironment()[3]);
176: assertEquals(n3, env.getEnvironment()[1]);
177: env.commit();
178: assertEquals(null, env.getEnvironment()[2]);
179: assertEquals(n1, env.getEnvironment()[3]);
180: assertEquals(n3, env.getEnvironment()[1]);
181: try {
182: env.unwind();
183: assertTrue("Failed to catch end of stack", false);
184: } catch (IndexOutOfBoundsException e) {
185: }
186: }
187:
188: /**
189: * Test simple single clause binding
190: */
191: public void testClauseMaching() {
192: BindingStack env = new BindingStack();
193: env.reset(MAX_VARS);
194: List rules = new ArrayList();
195: BasicForwardRuleInfGraph inf = new BasicForwardRuleInfGraph(
196: new BasicForwardRuleReasoner(rules), rules, null);
197: TriplePattern p1 = new TriplePattern(new Node_RuleVariable(
198: "?a", 0), n1, new Node_RuleVariable("?b", 1));
199: TriplePattern p2 = new TriplePattern(new Node_RuleVariable(
200: "?b", 1), new Node_RuleVariable("?c", 2), n2);
201:
202: // Should fail with no bindings
203: boolean match = FRuleEngine.match(p1, new Triple(n1, n2, n3),
204: env);
205: assertTrue(!match);
206: assertEquals(null, env.getEnvironment()[0]);
207: assertEquals(null, env.getEnvironment()[1]);
208: assertEquals(null, env.getEnvironment()[2]);
209:
210: // Should succeed with two bindings
211: match = FRuleEngine.match(p1, new Triple(n2, n1, n3), env);
212: assertTrue(match);
213: assertEquals(n2, env.getEnvironment()[0]);
214: assertEquals(n3, env.getEnvironment()[1]);
215: assertEquals(null, env.getEnvironment()[2]);
216:
217: // should fail but leave prior bindings intact
218: match = FRuleEngine.match(p2, new Triple(n1, n2, n2), env);
219: assertTrue(!match);
220: assertEquals(n2, env.getEnvironment()[0]);
221: assertEquals(n3, env.getEnvironment()[1]);
222: assertEquals(null, env.getEnvironment()[2]);
223:
224: // should succeed with full binding set
225: match = FRuleEngine.match(p2, new Triple(n3, n1, n2), env);
226: assertTrue(match);
227: assertEquals(n2, env.getEnvironment()[0]);
228: assertEquals(n3, env.getEnvironment()[1]);
229: assertEquals(n1, env.getEnvironment()[2]);
230: }
231:
232: /**
233: * Minimal rule tester to check basic pattern match
234: */
235: public void testRuleMatcher() {
236: String rules = "[r1: (?a p ?b), (?b q ?c) -> (?a, q, ?c)]"
237: + "[r2: (?a p ?b), (?b p ?c) -> (?a, p, ?c)]"
238: + "[r3: (?a p ?a), (n1 p ?c), (n1, p, ?a) -> (?a, p, ?c)]"
239: + "[r4: (n4 ?p ?a) -> (n4, ?a, ?p)]";
240: List ruleList = Rule.parseRules(rules);
241:
242: InfGraph infgraph = new BasicForwardRuleReasoner(ruleList)
243: .bind(Factory.createGraphMem());
244: infgraph.add(new Triple(n1, p, n2));
245: infgraph.add(new Triple(n2, p, n3));
246: infgraph.add(new Triple(n2, q, n3));
247: infgraph.add(new Triple(n4, p, n4));
248:
249: TestUtil.assertIteratorValues(this , infgraph.find(null, null,
250: null), new Triple[] { new Triple(n1, p, n2),
251: new Triple(n2, p, n3), new Triple(n2, q, n3),
252: new Triple(n4, p, n4), new Triple(n1, p, n3),
253: new Triple(n1, q, n3), new Triple(n4, n4, p), });
254: }
255:
256: /**
257: * Test derivation machinery
258: */
259: public void testRuleDerivations() {
260: String rules = "[testRule1: (n1 p ?a) -> (n2, p, ?a)]"
261: + "[testRule2: (n1 q ?a) -> (n2, q, ?a)]"
262: + "[testRule3: (n2 p ?a), (n2 q ?a) -> (res p ?a)]";
263: List ruleList = Rule.parseRules(rules);
264:
265: InfGraph infgraph = new BasicForwardRuleReasoner(ruleList)
266: .bind(Factory.createGraphMem());
267: infgraph.setDerivationLogging(true);
268: infgraph.add(new Triple(n1, p, n3));
269: infgraph.add(new Triple(n1, q, n4));
270: infgraph.add(new Triple(n1, q, n3));
271:
272: TestUtil.assertIteratorValues(this , infgraph.find(null, null,
273: null), new Triple[] { new Triple(n1, p, n3),
274: new Triple(n2, p, n3), new Triple(n1, q, n4),
275: new Triple(n2, q, n4), new Triple(n1, q, n3),
276: new Triple(n2, q, n3), new Triple(res, p, n3) });
277:
278: Iterator derivs = infgraph
279: .getDerivation(new Triple(res, p, n3));
280: StringWriter outString = new StringWriter(250);
281: PrintWriter out = new PrintWriter(outString);
282: while (derivs.hasNext()) {
283: Derivation d = (Derivation) derivs.next();
284: d.printTrace(out, true);
285: }
286: out.flush();
287:
288: String testString = TestUtil
289: .normalizeWhiteSpace("Rule testRule3 concluded (res p n3) <-\n"
290: + " Rule testRule1 concluded (n2 p n3) <-\n"
291: + " Fact (n1 p n3)\r\n"
292: + " Rule testRule2 concluded (n2 q n3) <-\n"
293: + " Fact (n1 q n3)\r\n");
294: assertEquals(testString, TestUtil.normalizeWhiteSpace(outString
295: .getBuffer().toString()));
296: }
297:
298: /**
299: * Test axiom handling machinery
300: */
301: public void testAxiomHandling() {
302: String rules = "[testRule1: (n1 p ?a) -> (n2, p, ?a)]"
303: + "[testRule2: (n1 q ?a) -> (n2, q, ?a)]"
304: + "[testRule3: (n2 p ?a), (n2 q ?a) -> (res p ?a)]"
305: + "[axiom1: -> (n1 p n3)]";
306: List ruleList = Rule.parseRules(rules);
307:
308: InfGraph infgraph = new BasicForwardRuleReasoner(ruleList)
309: .bind(Factory.createGraphMem());
310: TestUtil.assertIteratorValues(this , infgraph.find(null, null,
311: null), new Triple[] { new Triple(n1, p, n3),
312: new Triple(n2, p, n3), });
313:
314: infgraph.add(new Triple(n1, q, n4));
315: infgraph.add(new Triple(n1, q, n3));
316:
317: TestUtil.assertIteratorValues(this , infgraph.find(null, null,
318: null), new Triple[] { new Triple(n1, p, n3),
319: new Triple(n2, p, n3), new Triple(n1, q, n4),
320: new Triple(n2, q, n4), new Triple(n1, q, n3),
321: new Triple(n2, q, n3), new Triple(res, p, n3) });
322:
323: }
324:
325: /**
326: * Test schema partial binding machinery
327: */
328: public void testSchemaBinding() {
329: String rules = "[testRule1: (n1 p ?a) -> (n2, p, ?a)]"
330: + "[testRule2: (n1 q ?a) -> (n2, q, ?a)]"
331: + "[testRule3: (n2 p ?a), (n2 q ?a) -> (res p ?a)]";
332: List ruleList = Rule.parseRules(rules);
333: Graph schema = Factory.createGraphMem();
334: schema.add(new Triple(n1, p, n3));
335: Graph data = Factory.createGraphMem();
336: data.add(new Triple(n1, q, n4));
337: data.add(new Triple(n1, q, n3));
338:
339: Reasoner reasoner = new BasicForwardRuleReasoner(ruleList);
340: Reasoner boundReasoner = reasoner.bindSchema(schema);
341: InfGraph infgraph = boundReasoner.bind(data);
342:
343: TestUtil.assertIteratorValues(this , infgraph.find(null, null,
344: null), new Triple[] { new Triple(n1, p, n3),
345: new Triple(n2, p, n3), new Triple(n1, q, n4),
346: new Triple(n2, q, n4), new Triple(n1, q, n3),
347: new Triple(n2, q, n3), new Triple(res, p, n3) });
348: }
349:
350: /**
351: * Test functor handling
352: */
353: public void testEmbeddedFunctors() {
354: String rules = "(?C owl:onProperty ?P), (?C owl:allValuesFrom ?D) -> (?C rb:restriction all(?P, ?D))."
355: + "(?C rb:restriction all(eg:p, eg:D)) -> (?C rb:restriction 'allOK')."
356: + "[ -> (eg:foo eg:prop functor(eg:bar, 1)) ]"
357: + "[ (?x eg:prop functor(eg:bar, ?v)) -> (?x eg:propbar ?v) ]"
358: + "[ (?x eg:prop functor(?v, ?*)) -> (?x eg:propfunc ?v) ]"
359: + "";
360: List ruleList = Rule.parseRules(rules);
361:
362: Model data = ModelFactory.createDefaultModel();
363: Resource R1 = data.createResource(PrintUtil.egNS + "R1");
364: Resource D = data.createResource(PrintUtil.egNS + "D");
365: Property p = data.createProperty(PrintUtil.egNS, "p");
366: Property prop = data.createProperty(PrintUtil.egNS, "prop");
367: Property propbar = data.createProperty(PrintUtil.egNS,
368: "propbar");
369: Property propfunc = data.createProperty(PrintUtil.egNS,
370: "propfunc");
371: Property rbr = data.createProperty(
372: ReasonerVocabulary.RBNamespace, "restriction");
373: R1.addProperty(OWL.onProperty, p).addProperty(
374: OWL.allValuesFrom, D);
375:
376: Reasoner reasoner = new BasicForwardRuleReasoner(ruleList);
377: InfGraph infgraph = reasoner.bind(data.getGraph());
378: Model infModel = ModelFactory.createModelForGraph(infgraph);
379: Resource foo = infModel.createResource(PrintUtil.egNS + "foo");
380: Resource bar = infModel.createResource(PrintUtil.egNS + "bar");
381:
382: RDFNode flit = infModel.getResource(R1.getURI())
383: .getRequiredProperty(rbr).getObject();
384: assertNotNull(flit);
385: assertEquals(flit.toString(), "allOK");
386: // assertTrue(flit instanceof Literal);
387: // Functor func = (Functor)((Literal)flit).getValue();
388: // assertEquals("all", func.getName());
389: // assertEquals(p.getNode(), func.getArgs()[0]);
390: // assertEquals(D.getNode(), func.getArgs()[1]);
391:
392: Literal one = (Literal) foo.getRequiredProperty(propbar)
393: .getObject();
394: assertEquals(new Integer(1), one.getValue());
395: }
396:
397: /**
398: * The the minimal machinery for supporting builtins
399: */
400: public void testBuiltins() {
401: String rules = //"[testRule1: (n1 ?p ?a) -> print('rule1test', ?p, ?a)]" +
402: "[r1: (n1 p ?x), addOne(?x, ?y) -> (n1 q ?y)]"
403: + "[r2: (n1 p ?x), lessThan(?x, 3) -> (n2 q ?x)]"
404: + "[axiom1: -> (n1 p 1)]" + "[axiom2: -> (n1 p 4)]"
405: + "";
406: List ruleList = Rule.parseRules(rules);
407:
408: InfGraph infgraph = new BasicForwardRuleReasoner(ruleList)
409: .bind(Factory.createGraphMem());
410: TestUtil.assertIteratorValues(this , infgraph.find(n1, q, null),
411: new Triple[] { new Triple(n1, q, Util.makeIntNode(2)),
412: new Triple(n1, q, Util.makeIntNode(5)) });
413: TestUtil
414: .assertIteratorValues(this , infgraph.find(n2, q, null),
415: new Triple[] { new Triple(n2, q, Util
416: .makeIntNode(1)) });
417:
418: }
419:
420: /**
421: * The the "remove" builtin
422: */
423: public void testRemoveBuiltin() {
424: String rules = "[rule1: (?x p ?y), (?x q ?y) -> remove(0)]"
425: + "";
426: List ruleList = Rule.parseRules(rules);
427:
428: InfGraph infgraph = new BasicForwardRuleReasoner(ruleList)
429: .bind(Factory.createGraphMem());
430: infgraph.add(new Triple(n1, p, Util.makeIntNode(1)));
431: infgraph.add(new Triple(n1, p, Util.makeIntNode(2)));
432: infgraph.add(new Triple(n1, q, Util.makeIntNode(2)));
433:
434: TestUtil.assertIteratorValues(this , infgraph.find(n1, null,
435: null), new Triple[] {
436: new Triple(n1, p, Util.makeIntNode(1)),
437: new Triple(n1, q, Util.makeIntNode(2)) });
438:
439: }
440:
441: /**
442: * The the "drop" builtin
443: */
444: public void testDropBuiltin() {
445: String rules = "[rule1: (?x p ?y) -> drop(0)]" + "";
446: List ruleList = Rule.parseRules(rules);
447:
448: InfGraph infgraph = new BasicForwardRuleReasoner(ruleList)
449: .bind(Factory.createGraphMem());
450: infgraph.add(new Triple(n1, p, Util.makeIntNode(1)));
451: infgraph.add(new Triple(n1, p, Util.makeIntNode(2)));
452: infgraph.add(new Triple(n1, q, Util.makeIntNode(2)));
453:
454: TestUtil.assertIteratorValues(this , infgraph.find(n1, null,
455: null), new Triple[] { new Triple(n1, q, Util
456: .makeIntNode(2)) });
457:
458: }
459:
460: /**
461: * Test the rebind operation.
462: */
463: public void testRebind() {
464: String rules = "[rule1: (?x p ?y) -> (?x q ?y)]";
465: List ruleList = Rule.parseRules(rules);
466: Graph data = Factory.createGraphMem();
467: data.add(new Triple(n1, p, n2));
468: InfGraph infgraph = new BasicForwardRuleReasoner(ruleList)
469: .bind(data);
470: TestUtil.assertIteratorValues(this , infgraph.find(n1, null,
471: null), new Triple[] { new Triple(n1, p, n2),
472: new Triple(n1, q, n2) });
473: Graph ndata = Factory.createGraphMem();
474: ndata.add(new Triple(n1, p, n3));
475: infgraph.rebind(ndata);
476: TestUtil.assertIteratorValues(this , infgraph.find(n1, null,
477: null), new Triple[] { new Triple(n1, p, n3),
478: new Triple(n1, q, n3) });
479: }
480:
481: /**
482: * Test size bug, used to blow up if size was called before any queries.
483: */
484: public void testSize() {
485: String rules = "[rule1: (?x p ?y) -> (?x q ?y)]";
486: List ruleList = Rule.parseRules(rules);
487: Graph data = Factory.createGraphMem();
488: data.add(new Triple(n1, p, n2));
489: InfGraph infgraph = new BasicForwardRuleReasoner(ruleList)
490: .bind(data);
491: assertEquals(infgraph.size(), 2);
492: }
493:
494: /**
495: * Check validity report implementation, there had been a stupid bug here.
496: */
497: public void testValidityReport() {
498: StandardValidityReport report = new StandardValidityReport();
499: report.add(false, "dummy", "dummy1");
500: report.add(false, "dummy", "dummy3");
501: assertTrue(report.isValid());
502: report.add(true, "dummy", "dummy2");
503: assertTrue(!report.isValid());
504:
505: report = new StandardValidityReport();
506: report.add(false, "dummy", "dummy1");
507: report.add(true, "dummy", "dummy2");
508: report.add(false, "dummy", "dummy3");
509: assertTrue(!report.isValid());
510:
511: report = new StandardValidityReport();
512: report.add(new ValidityReport.Report(false, "dummy", "dummy1"));
513: report.add(new ValidityReport.Report(true, "dummy", "dummy2"));
514: report.add(new ValidityReport.Report(false, "dummy", "dummy3"));
515: assertTrue(!report.isValid());
516: }
517:
518: /**
519: * Test the list conversion utility that is used in some of the builtins.
520: */
521: public void testConvertList() {
522: Graph data = Factory.createGraphMem();
523: Node first = RDF.Nodes.first;
524: Node rest = RDF.Nodes.rest;
525: Node nil = RDF.Nodes.nil;
526: data.add(new Triple(n1, first, p));
527: data.add(new Triple(n1, rest, n2));
528: data.add(new Triple(n2, first, q));
529: data.add(new Triple(n2, rest, nil));
530:
531: data.add(new Triple(n3, first, p));
532: data.add(new Triple(n3, rest, n4));
533: data.add(new Triple(n4, rest, n5));
534: data.add(new Triple(n5, first, q));
535: data.add(new Triple(n5, rest, nil));
536:
537: String rules = "[rule1: (?x p ?y) -> (?x q ?y)]";
538: List ruleList = Rule.parseRules(rules);
539: InfGraph infgraph = new BasicForwardRuleReasoner(ruleList)
540: .bind(data);
541:
542: RuleContext context = new BFRuleContext(
543: (ForwardRuleInfGraphI) infgraph);
544: List result = Util.convertList(n1, context);
545: assertEquals(result.size(), 2);
546: assertEquals(result.get(0), p);
547: assertEquals(result.get(1), q);
548:
549: List result2 = Util.convertList(n3, context);
550: assertEquals(result2.size(), 1);
551: assertEquals(result2.get(0), p);
552: }
553:
554: }
555:
556: /*
557: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
558: All rights reserved.
559:
560: Redistribution and use in source and binary forms, with or without
561: modification, are permitted provided that the following conditions
562: are met:
563:
564: 1. Redistributions of source code must retain the above copyright
565: notice, this list of conditions and the following disclaimer.
566:
567: 2. Redistributions in binary form must reproduce the above copyright
568: notice, this list of conditions and the following disclaimer in the
569: documentation and/or other materials provided with the distribution.
570:
571: 3. The name of the author may not be used to endorse or promote products
572: derived from this software without specific prior written permission.
573:
574: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
575: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
576: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
577: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
578: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
579: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
580: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
581: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
582: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
583: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
584: */
|