📄 testgenericrules.java
字号:
/******************************************************************
* File: TestGenericRules.java
* Created by: Dave Reynolds
* Created on: 08-Jun-2003
*
* (c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
* [See end of file]
* $Id: TestGenericRules.java,v 1.23 2007/01/02 11:50:31 andy_seaborne Exp $
*****************************************************************/
package com.hp.hpl.jena.reasoner.rulesys.test;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.reasoner.*;
import com.hp.hpl.jena.reasoner.rulesys.*;
import com.hp.hpl.jena.reasoner.test.TestUtil;
import com.hp.hpl.jena.util.PrintUtil;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;
import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;
import com.hp.hpl.jena.graph.*;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import java.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Test the packaging of all the reasoners into the GenericRuleReasoner.
* The other tests check out this engine. These tests just need to touch
* enough to validate the packaging.
*
* @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
* @version $Revision: 1.23 $ on $Date: 2007/01/02 11:50:31 $
*/
public class TestGenericRules extends TestCase {
protected static Log logger = LogFactory.getLog(TestFBRules.class);
// Useful constants
Node p = Node.createURI("p");
Node q = Node.createURI("q");
Node r = Node.createURI("r");
Node s = Node.createURI("s");
Node t = Node.createURI("t");
Node a = Node.createURI("a");
Node b = Node.createURI("b");
Node c = Node.createURI("c");
Node d = Node.createURI("d");
Node C1 = Node.createURI("C1");
Node C2 = Node.createURI("C2");
Node C3 = Node.createURI("C3");
Node ty = RDF.Nodes.type;
Node sC = RDFS.Nodes.subClassOf;
List ruleList = Rule.parseRules("[r1: (?a p ?b), (?b p ?c) -> (?a p ?c)]" +
"[r2: (?a q ?b) -> (?a p ?c)]" +
"-> table(p). -> table(q).");
Triple[] ans = new Triple[] { new Triple(a, p, b),
new Triple(b, p, c),
new Triple(a, p, c) };
/**
* Boilerplate for junit
*/
public TestGenericRules( String name ) {
super( name );
}
/**
* Boilerplate for junit.
* This is its own test suite
*/
public static TestSuite suite() {
return new TestSuite( TestGenericRules.class );
// TestSuite suite = new TestSuite();
// suite.addTest(new TestGenericRules( "testAddRemove2" ));
// return suite;
}
/**
* Minimal rule tester to check basic pattern match, forward style.
*/
public void testForward() {
Graph test = Factory.createGraphMem();
test.add(new Triple(a, p, b));
test.add(new Triple(b, p, c));
GenericRuleReasoner reasoner = (GenericRuleReasoner)GenericRuleReasonerFactory.theInstance().create(null);
reasoner.setRules(ruleList);
reasoner.setMode(GenericRuleReasoner.FORWARD);
// Check data bind version
InfGraph infgraph = reasoner.bind(test);
TestUtil.assertIteratorValues(this, infgraph.find(null, p, null), ans);
// Check schema bind version
infgraph = reasoner.bindSchema(test).bind(Factory.createGraphMem());
TestUtil.assertIteratorValues(this, infgraph.find(null, p, null), ans);
}
/**
* Minimal rule tester to check basic pattern match, backward style.
*/
public void testBackward() {
Graph test = Factory.createGraphMem();
test.add(new Triple(a, p, b));
test.add(new Triple(b, p, c));
GenericRuleReasoner reasoner = (GenericRuleReasoner)GenericRuleReasonerFactory.theInstance().create(null);
reasoner.setRules(ruleList);
reasoner.setMode(GenericRuleReasoner.BACKWARD);
// Check data bind version
InfGraph infgraph = reasoner.bind(test);
TestUtil.assertIteratorValues(this, infgraph.find(null, p, null), ans);
// Check schema bind version
infgraph = reasoner.bindSchema(test).bind(Factory.createGraphMem());
TestUtil.assertIteratorValues(this, infgraph.find(null, p, null), ans);
}
/**
* Test example hybrid rule.
*/
public void testHybrid() {
Graph data = Factory.createGraphMem();
data.add(new Triple(a, r, b));
data.add(new Triple(p, ty, s));
List rules = Rule.parseRules(
"[a1: -> (a rdf:type t)]" +
"[r0: (?x r ?y) -> (?x p ?y)]" +
"[r1: (?p rdf:type s) -> [r1b: (?x ?p ?y) <- (?y ?p ?x)]]" +
"[r2: (?p rdf:type s) -> [r2b: (?x ?p ?x) <- (?x rdf:type t)]]" +
"-> tableAll()."
);
GenericRuleReasoner reasoner = (GenericRuleReasoner)GenericRuleReasonerFactory.theInstance().create(null);
reasoner.setRules(rules);
reasoner.setMode(GenericRuleReasoner.HYBRID);
InfGraph infgraph = reasoner.bind(data);
infgraph.setDerivationLogging(true);
TestUtil.assertIteratorValues(this,
infgraph.find(null, p, null), new Object[] {
new Triple(a, p, a),
new Triple(a, p, b),
new Triple(b, p, a)
} );
// Check derivation tracing as well
Iterator di = infgraph.getDerivation(new Triple(b, p, a));
assertTrue(di.hasNext());
RuleDerivation d = (RuleDerivation)di.next();
// java.io.PrintWriter out = new java.io.PrintWriter(System.out);
// d.printTrace(out, true);
// out.close();
assertTrue(d.getRule().getName().equals("r1b"));
TestUtil.assertIteratorValues(this, d.getMatches().iterator(), new Object[] { new Triple(a, p, b) });
assertTrue(! di.hasNext());
}
/**
* Test early detection of illegal backward rules.
*/
public void testBRuleErrorHandling() {
Graph data = Factory.createGraphMem();
List rules = Rule.parseRules(
"[a1: -> [(?x eg:p ?y) (?x eg:q ?y) <- (?x eg:r ?y)]]"
);
boolean foundException = false;
try {
GenericRuleReasoner reasoner = (GenericRuleReasoner)GenericRuleReasonerFactory.theInstance().create(null);
reasoner.setRules(rules);
reasoner.setMode(GenericRuleReasoner.HYBRID);
InfGraph infgraph = reasoner.bind(data);
infgraph.prepare();
} catch (ReasonerException e) {
foundException = true;
}
assertTrue("Catching use of multi-headed brules", foundException);
}
/**
* Test example parameter setting
*/
public void testParameters() {
Graph data = Factory.createGraphMem();
data.add(new Triple(a, r, b));
data.add(new Triple(p, ty, s));
Model m = ModelFactory.createDefaultModel();
Resource configuration= m.createResource(GenericRuleReasonerFactory.URI);
configuration.addProperty(ReasonerVocabulary.PROPderivationLogging, "true");
configuration.addProperty(ReasonerVocabulary.PROPruleMode, "hybrid");
configuration.addProperty(ReasonerVocabulary.PROPruleSet, "testing/reasoners/genericRuleTest.rules");
GenericRuleReasoner reasoner = (GenericRuleReasoner)GenericRuleReasonerFactory.theInstance().create(configuration);
InfGraph infgraph = reasoner.bind(data);
TestUtil.assertIteratorValues(this,
infgraph.find(null, p, null), new Object[] {
new Triple(a, p, a),
new Triple(a, p, b),
new Triple(b, p, a)
} );
// Check derivation tracing as well
Iterator di = infgraph.getDerivation(new Triple(b, p, a));
assertTrue(di.hasNext());
RuleDerivation d = (RuleDerivation)di.next();
assertTrue(d.getRule().getName().equals("r1b"));
TestUtil.assertIteratorValues(this, d.getMatches().iterator(), new Object[] { new Triple(a, p, b) });
assertTrue(! di.hasNext());
// Check retrieval of configuration
Model m2 = ModelFactory.createDefaultModel();
Resource newConfig = m2.createResource();
reasoner.addDescription(m2, newConfig);
TestUtil.assertIteratorValues(this, newConfig.listProperties(), new Statement[] {
m2.createStatement(newConfig, ReasonerVocabulary.PROPderivationLogging, "true"),
m2.createStatement(newConfig, ReasonerVocabulary.PROPruleMode, "hybrid"),
m2.createStatement(newConfig, ReasonerVocabulary.PROPruleSet, "testing/reasoners/genericRuleTest.rules")
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -