001: /******************************************************************
002: * File: TestLPDerivation.java
003: * Created by: Dave Reynolds
004: * Created on: 07-Oct-2005
005: *
006: * (c) Copyright 2005, Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: TestLPDerivation.java,v 1.4 2008/01/02 12:08:20 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.reasoner.rulesys.test;
010:
011: import java.util.Arrays;
012: import java.util.Iterator;
013: import java.util.List;
014:
015: import com.hp.hpl.jena.graph.Factory;
016: import com.hp.hpl.jena.graph.Graph;
017: import com.hp.hpl.jena.graph.Node;
018: import com.hp.hpl.jena.graph.Triple;
019: import com.hp.hpl.jena.graph.TripleMatch;
020: import com.hp.hpl.jena.reasoner.InfGraph;
021: import com.hp.hpl.jena.reasoner.rulesys.FBRuleInfGraph;
022: import com.hp.hpl.jena.reasoner.rulesys.FBRuleReasoner;
023: import com.hp.hpl.jena.reasoner.rulesys.Rule;
024: import com.hp.hpl.jena.reasoner.rulesys.RuleDerivation;
025: import com.hp.hpl.jena.util.iterator.ExtendedIterator;
026:
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: /**
031: * Test the derivation tracing of the LP system.
032: *
033: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
034: * @version $Revision: 1.4 $
035: */
036:
037: public class TestLPDerivation extends TestCase {
038:
039: /**
040: * Boilerplate for junit
041: */
042: public TestLPDerivation(String name) {
043: super (name);
044: }
045:
046: /**
047: * Boilerplate for junit.
048: * This is its own test suite
049: */
050: public static TestSuite suite() {
051: return new TestSuite(TestLPDerivation.class);
052: }
053:
054: // Useful constants
055: Node p = Node.createURI("p");
056: Node q = Node.createURI("q");
057: Node r = Node.createURI("r");
058: Node s = Node.createURI("s");
059: Node a = Node.createURI("a");
060: Node b = Node.createURI("b");
061: Node c = Node.createURI("c");
062: Node d = Node.createURI("d");
063: Node e = Node.createURI("e");
064:
065: /**
066: * Return an inference graph working over the given rule set and raw data.
067: * Can be overridden by subclasses of this test class.
068: * @param rules the rule set to use
069: * @param data the graph of triples to process
070: * @param tabled an array of predicates that should be tabled
071: */
072: public static InfGraph makeInfGraph(List rules, Graph data,
073: Node[] tabled) {
074: FBRuleReasoner reasoner = new FBRuleReasoner(rules);
075: FBRuleInfGraph infgraph = (FBRuleInfGraph) reasoner.bind(data);
076: for (int i = 0; i < tabled.length; i++) {
077: infgraph.setTabled(tabled[i]);
078: }
079: // infgraph.setTraceOn(true);
080: infgraph.setDerivationLogging(true);
081: return infgraph;
082: }
083:
084: /**
085: * Run a single derivation test. Only tests the first derivation found.
086: * @param ruleSrc source for a set of rules
087: * @param tabled array of predicate nodes which should be tabled by the rules
088: * @param triples inital array of triple data
089: * @param query the query to be tested the first result will be checked
090: * @param matches the set of triple matches which should occur in the derivation
091: * @param rulenumber the index of the rule in the rule list which should occur in the derivation
092: */
093: private void doTest(String ruleSrc, Node[] tabled,
094: Triple[] triples, TripleMatch query, Triple[] matches,
095: int rulenumber) {
096: List rules = Rule.parseRules(ruleSrc);
097: Graph data = Factory.createGraphMem();
098: for (int i = 0; i < triples.length; i++) {
099: data.add(triples[i]);
100: }
101: InfGraph infgraph = makeInfGraph(rules, data, tabled);
102: ExtendedIterator results = infgraph.find(query);
103: assertTrue(results.hasNext());
104: Triple result = (Triple) results.next();
105: results.close();
106: Rule rule = (Rule) rules.get(rulenumber);
107: List matchList = Arrays.asList(matches);
108: Iterator derivations = infgraph.getDerivation(result);
109: assertTrue(derivations.hasNext());
110: RuleDerivation derivation = (RuleDerivation) derivations.next();
111: // PrintWriter pw = new PrintWriter(System.out);
112: // derivation.printTrace(pw, true);
113: // pw.close();
114: assertEquals(result, derivation.getConclusion());
115: assertEquals(matchList, derivation.getMatches());
116: assertEquals(rule, derivation.getRule());
117: }
118:
119: /**
120: * Test simple rule derivation.
121: */
122: public void testBasic() {
123: doTest("(?x p ?y) <- (?x q ?y).", new Node[] {}, // Rules + tabling
124: new Triple[] { // Data
125: new Triple(a, q, b), }, new Triple(a, p, b), // query
126: new Triple[] { // Expected match list in derivation
127: new Triple(a, q, b) }, 0 // Expected rule in derivation
128: );
129: }
130:
131: /**
132: * Test simple rule derivation from pair
133: */
134: public void testBasic2() {
135: doTest("(?x p ?y) <- (?x q ?y). (?x p ?y) <- (?x r ?y).",
136: new Node[] {}, // Rules + tabling
137: new Triple[] { // Data
138: new Triple(a, r, b), }, new Triple(a, p, b), // query
139: new Triple[] { // Expected match list in derivation
140: new Triple(a, r, b) }, 1 // Expected rule in derivation
141: );
142: }
143:
144: /**
145: * Test composite derivation.
146: */
147: public void testComposite() {
148: doTest("(?x p ?y) <- (?x q ?y) (?x r ?y).",
149: new Node[] {}, // Rules + tabling
150: new Triple[] { // Data
151: new Triple(a, q, b), new Triple(a, r, b), },
152: new Triple(a, p, b), // query
153: new Triple[] { // Expected match list in derivation
154: new Triple(a, q, b), new Triple(a, r, b) }, 0 // Expected rule in derivation
155: );
156: }
157:
158: /**
159: * Test Chain derivation.
160: */
161: public void testChain() {
162: doTest(
163: "(?x s ?y) <- (?x r ?y). (?x p ?y) <- (?x q ?y) (?x s ?y). ",
164: new Node[] {}, // Rules + tabling
165: new Triple[] { // Data
166: new Triple(a, q, b), new Triple(a, r, b), },
167: new Triple(a, p, b), // query
168: new Triple[] { // Expected match list in derivation
169: new Triple(a, q, b), new Triple(a, s, b) }, 1 // Expected rule in derivation
170: );
171: }
172:
173: /**
174: * Test tabled chaining
175: */
176: public void testTabled() {
177: doTest("(?x p ?z) <- (?x p ?y) (?y p ?z).", new Node[] { p }, // Rules + tabling
178: new Triple[] { // Data
179: new Triple(a, p, b), new Triple(a, p, c),
180: new Triple(b, p, d), }, new Triple(a, p, d), // query
181: new Triple[] { // Expected match list in derivation
182: new Triple(a, p, b), new Triple(b, p, d) }, 0 // Expected rule in derivation
183: );
184: }
185:
186: }
187:
188: /*
189: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
190: All rights reserved.
191:
192: Redistribution and use in source and binary forms, with or without
193: modification, are permitted provided that the following conditions
194: are met:
195:
196: 1. Redistributions of source code must retain the above copyright
197: notice, this list of conditions and the following disclaimer.
198:
199: 2. Redistributions in binary form must reproduce the above copyright
200: notice, this list of conditions and the following disclaimer in the
201: documentation and/or other materials provided with the distribution.
202:
203: 3. The name of the author may not be used to endorse or promote products
204: derived from this software without specific prior written permission.
205:
206: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
207: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
208: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
209: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
210: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
211: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
212: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
213: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
214: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
215: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
216: */
|