📄 testbackchainer.java
字号:
/******************************************************************
* File: TestBackchainer.java
* Created by: Dave Reynolds
* Created on: 04-May-2003
*
* (c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
* [See end of file]
* $Id: TestBackchainer.java,v 1.36 2007/01/02 11:50:31 andy_seaborne Exp $
*****************************************************************/
package com.hp.hpl.jena.reasoner.rulesys.test;
import com.hp.hpl.jena.graph.*;
import com.hp.hpl.jena.reasoner.*;
import com.hp.hpl.jena.reasoner.rulesys.*;
import com.hp.hpl.jena.reasoner.rulesys.impl.*;
import com.hp.hpl.jena.reasoner.test.TestUtil;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.RDFS;
import com.hp.hpl.jena.vocabulary.RDF;
import java.io.IOException;
import java.util.*;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Test harness for the backward chainer.
* Parameterizable in subclasses by overriding createReasoner.
* The original version was developed for the original backchaining interpeter.
* That has now been obsoleted at this is now used to double check the
* LP engine, though the bulk of such tests are really done by TestBasicLP.
*
* @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
* @version $Revision: 1.36 $ on $Date: 2007/01/02 11:50:31 $
*/
public class TestBackchainer extends TestCase {
// Maximum size of binding environment needed in the tests
private static final int MAX_VARS = 10;
// Useful constants
protected Node p = Node.createURI("p");
protected Node q = Node.createURI("q");
protected Node r = Node.createURI("r");
protected Node s = Node.createURI("s");
protected Node t = Node.createURI("t");
protected Node a = Node.createURI("a");
protected Node b = Node.createURI("b");
protected Node c = Node.createURI("c");
protected Node d = Node.createURI("d");
protected Node C1 = Node.createURI("C1");
protected Node C2 = Node.createURI("C2");
protected Node C3 = Node.createURI("C3");
protected Node sP = RDFS.Nodes.subPropertyOf;
protected Node sC = RDFS.Nodes.subClassOf;
protected Node ty = RDF.Nodes.type;
String testRules1 =
"(?x ?q ?y) <- (?p rdfs:subPropertyOf ?q)(?x ?p ?y). " +
"(?a rdfs:subPropertyOf ?c) <- (?a rdfs:subPropertyOf ?b)(?b rdfs:subPropertyOf ?c). ";
String testRuleAxioms = "[ -> (p rdfs:subPropertyOf q)]" +
"[ -> (q rdfs:subPropertyOf r) ]" +
"[ -> (a p b) ]";
Triple[] dataElts = new Triple[] {
new Triple(p, sP, q),
new Triple(q, sP, r),
new Triple(a, p, b)
};
/**
* Boilerplate for junit
*/
public TestBackchainer( String name ) {
super( name );
}
/**
* Boilerplate for junit.
* This is its own test suite
*/
public static TestSuite suite() {
return new TestSuite( TestBackchainer.class );
// TestSuite suite = new TestSuite();
// suite.addTest(new TestBackchainer( "testRDFSProblemsb" ));
// return suite;
}
/**
* Override in subclasses to test other reasoners.
*/
public Reasoner createReasoner(List rules) {
LPBackwardRuleReasoner reasoner = new LPBackwardRuleReasoner(rules);
reasoner.tablePredicate(sP);
reasoner.tablePredicate(sC);
reasoner.tablePredicate(ty);
reasoner.tablePredicate(p);
reasoner.tablePredicate(a);
reasoner.tablePredicate(b);
return reasoner;
}
/**
* Test parser modes to support backarrow notation are working
*/
public void testParse() {
List rules = Rule.parseRules(testRules1);
assertEquals("BRule parsing",
"[ (?x ?q ?y) <- (?p rdfs:subPropertyOf ?q) (?x ?p ?y) ]",
rules.get(0).toString());
assertEquals("BRule parsing",
"[ (?a rdfs:subPropertyOf ?c) <- (?a rdfs:subPropertyOf ?b) (?b rdfs:subPropertyOf ?c) ]",
rules.get(1).toString());
}
/**
* Test goal/head unify operation.
*/
public void testUnify() {
Node_RuleVariable xg = new Node_RuleVariable("?x", 0);
Node_RuleVariable yg = new Node_RuleVariable("?y", 1);
Node_RuleVariable zg = new Node_RuleVariable("?z", 2);
Node_RuleVariable xh = new Node_RuleVariable("?x", 0);
Node_RuleVariable yh = new Node_RuleVariable("?y", 1);
Node_RuleVariable zh = new Node_RuleVariable("?z", 2);
TriplePattern g1 = new TriplePattern(xg, p, yg);
TriplePattern g2 = new TriplePattern(xg, p, xg);
TriplePattern g3 = new TriplePattern( a, p, xg);
TriplePattern g4 = new TriplePattern( a, p, b);
TriplePattern h1 = new TriplePattern(xh, p, yh);
TriplePattern h2 = new TriplePattern(xh, p, xh);
TriplePattern h3 = new TriplePattern( a, p, xh);
TriplePattern h4 = new TriplePattern( a, p, b);
TriplePattern h5 = new TriplePattern(xh, p, a);
doTestUnify(g1, h1, true, new Node[] {null, null});
doTestUnify(g1, h2, true, new Node[] {null, null});
doTestUnify(g1, h3, true, new Node[] {null, null});
doTestUnify(g1, h4, true, new Node[] {null, null});
doTestUnify(g1, h5, true, new Node[] {null, null});
doTestUnify(g2, h1, true, new Node[] {null, xh});
doTestUnify(g2, h2, true, new Node[] {null, null});
doTestUnify(g2, h3, true, new Node[] {a, null});
doTestUnify(g2, h4, false, null);
doTestUnify(g2, h5, true, new Node[] {a, null});
doTestUnify(g3, h1, true, new Node[] {a, null});
doTestUnify(g3, h2, true, new Node[] {a, null});
doTestUnify(g3, h3, true, new Node[] {null, null});
doTestUnify(g3, h4, true, new Node[] {null, null});
doTestUnify(g3, h5, true, new Node[] {a, null});
doTestUnify(g4, h1, true, new Node[] {a, b});
doTestUnify(g4, h2, false, null);
doTestUnify(g4, h3, true, new Node[] {b});
doTestUnify(g4, h4, true, null);
doTestUnify(g4, h5, false, null);
// Recursive case
doTestUnify(h1, h1, true, new Node[] {null, null});
// Wildcard case
doTestUnify(new TriplePattern(null, null, null), h2, true, new Node[] {null, null});
// Test functor cases as well!
TriplePattern gf = new TriplePattern(xg, p,
Functor.makeFunctorNode("f", new Node[]{xg, b}));
TriplePattern hf1 = new TriplePattern(yh, p,
Functor.makeFunctorNode("f", new Node[]{zh, b}));
TriplePattern hf2 = new TriplePattern(yh, p,
Functor.makeFunctorNode("f", new Node[]{a, yh}));
TriplePattern hf3 = new TriplePattern(yh, p,
Functor.makeFunctorNode("f", new Node[]{b, yh}));
doTestUnify(gf, hf1, true, new Node[] {null, null, yh});
doTestUnify(gf, hf2, false, null);
doTestUnify(gf, hf3, true, new Node[] {null, b});
// Check binding environment use
BindingVector env = BindingVector.unify(g2, h1, MAX_VARS);
env.bind(xh, c);
assertEquals(env.getBinding(yh), c);
env = BindingVector.unify(g2, h1, MAX_VARS);
env.bind(yh, c);
assertEquals(env.getBinding(xh), c);
}
/**
* Helper for testUnify.
* @param goal goal triple pattern
* @param head head triple pattern
* @param succeed whether match should succeeed or fail
* @param env list list of expected environment bindings
*
*/
private void doTestUnify(TriplePattern goal, TriplePattern head, boolean succeed, Node[] env) {
BindingVector result = BindingVector.unify(goal, head, MAX_VARS);
if (succeed) {
assertNotNull(result);
if (env != null) {
for (int i = 0; i < env.length; i++) {
Node n = result.getEnvironment()[i];
if (env[i] != null) {
assertEquals(env[i], n);
} else {
assertNull(n);
}
}
}
} else {
assertNull(result);
}
}
/**
* Check that a reasoner over an empty rule set accesses
* the raw data successfully.
*/
public void testListData() {
Graph data = Factory.createGraphMem();
for (int i = 0; i < dataElts.length; i++) {
data.add(dataElts[i]);
}
Graph schema = Factory.createGraphMem();
schema.add(new Triple(c, p, c));
// Case of schema and data but no rule axioms
Reasoner reasoner = createReasoner(new ArrayList());
InfGraph infgraph = reasoner.bindSchema(schema).bind(data);
TestUtil.assertIteratorValues(this,
infgraph.find(null, null, null),
new Object[] {
new Triple(p, sP, q),
new Triple(q, sP, r),
new Triple(a, p, b),
new Triple(c, p, c)});
// Case of data and rule axioms but no schema
List rules = Rule.parseRules("-> (d p d).");
reasoner = createReasoner(rules);
infgraph = reasoner.bind(data);
TestUtil.assertIteratorValues(this,
infgraph.find(null, null, null),
new Object[] {
new Triple(p, sP, q),
new Triple(q, sP, r),
new Triple(a, p, b),
new Triple(d, p, d)});
// Case of data and rule axioms and schema
infgraph = reasoner.bindSchema(schema).bind(data);
TestUtil.assertIteratorValues(this,
infgraph.find(null, null, null),
new Object[] {
new Triple(p, sP, q),
new Triple(q, sP, r),
new Triple(a, p, b),
new Triple(c, p, c),
new Triple(d, p, d)});
}
/**
* Test basic rule operations - simple AND rule
*/
public void testBaseRules1() {
List rules = Rule.parseRules("[r1: (?a r ?c) <- (?a p ?b),(?b p ?c)]");
Graph data = Factory.createGraphMem();
data.add(new Triple(a, p, b));
data.add(new Triple(b, p, c));
data.add(new Triple(b, p, d));
Reasoner reasoner = createReasoner(rules);
InfGraph infgraph = reasoner.bind(data);
TestUtil.assertIteratorValues(this,
infgraph.find(null, r, null),
new Object[] {
new Triple(a, r, c),
new Triple(a, r, d)
} );
}
/**
* Test basic rule operations - simple OR rule
*/
public void testBaseRules2() {
List rules = Rule.parseRules(
"[r1: (?a r ?b) <- (?a p ?b)]" +
"[r2: (?a r ?b) <- (?a q ?b)]" +
"[r3: (?a r ?b) <- (?a s ?c), (?c s ?b)]"
);
Graph data = Factory.createGraphMem();
data.add(new Triple(a, p, b));
data.add(new Triple(b, q, c));
data.add(new Triple(a, s, b));
data.add(new Triple(b, s, d));
Reasoner reasoner = createReasoner(rules);
InfGraph infgraph = reasoner.bind(data);
TestUtil.assertIteratorValues(this,
infgraph.find(null, r, null),
new Object[] {
new Triple(a, r, b),
new Triple(b, r, c),
new Triple(a, r, d)
} );
}
/**
* Test basic rule operations - simple OR rule with chaining
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -