📄 testfbrules.java
字号:
/******************************************************************
* File: TestFBRules.java
* Created by: Dave Reynolds
* Created on: 29-May-2003
*
* (c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
* [See end of file]
* $Id: TestFBRules.java,v 1.50 2007/09/08 11:27:49 der Exp $
*****************************************************************/
package com.hp.hpl.jena.reasoner.rulesys.test;
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.datatypes.xsd.*;
import com.hp.hpl.jena.graph.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.shared.ClosedException;
import com.hp.hpl.jena.shared.impl.JenaParameters;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.util.PrintUtil;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.vocabulary.*;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import java.io.IOException;
import java.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Test suite for the hybrid forward/backward rule system.
*
* @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
* @version $Revision: 1.50 $ on $Date: 2007/09/08 11:27:49 $
*/
public class TestFBRules extends TestCase {
protected static Log logger = LogFactory.getLog(TestFBRules.class);
// Useful constants
protected Node p = Node.createURI("p");
protected Node q = Node.createURI("q");
protected Node n1 = Node.createURI("n1");
protected Node n2 = Node.createURI("n2");
protected Node n3 = Node.createURI("n3");
protected Node n4 = Node.createURI("n4");
protected Node n5 = Node.createURI("n5");
protected Node res = Node.createURI("res");
protected Node r = Node.createURI("r");
protected Node s = Node.createURI("s");
protected Node t = Node.createURI("t");
protected Node u = Node.createURI("u");
protected Node v = Node.createURI("v");
protected Node w = Node.createURI("w");
protected Node x = Node.createURI("x");
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;
/**
* Boilerplate for junit
*/
public TestFBRules( String name ) {
super( name );
}
/**
* Boilerplate for junit.
* This is its own test suite
*/
public static TestSuite suite() {
return new TestSuite( TestFBRules.class );
// TestSuite suite = new TestSuite();
// suite.addTest(new TestFBRules( "testNumericFunctors" ));
// return suite;
}
/**
* Override in subclasses to test other reasoners.
*/
public Reasoner createReasoner(List rules) {
FBRuleReasoner reasoner = new FBRuleReasoner(rules);
reasoner.tablePredicate(RDFS.Nodes.subClassOf);
reasoner.tablePredicate(RDF.Nodes.type);
reasoner.tablePredicate(p);
return reasoner;
}
/**
* Assemble a test infGraph from a rule source and starting data
*/
public InfGraph createInfGraph(String rules, Graph data) {
return createReasoner( Rule.parseRules(rules) ).bind(data);
}
/**
* Assemble a test infGraph from a rule source and empty data
*/
public InfGraph createInfGraph(String rules) {
return createReasoner( Rule.parseRules(rules) ).bind(Factory.createGraphMem());
}
/**
* Check parser extension for f/b distinction.
*/
public void testParser() {
String rf = "(?a rdf:type ?t) -> (?t rdf:type rdfs:Class).";
String rb = "(?t rdf:type rdfs:Class) <- (?a rdf:type ?t).";
assertTrue( ! Rule.parseRule(rf).isBackward() );
assertTrue( Rule.parseRule(rb).isBackward() );
}
/**
* Minimal rule tester to check basic pattern match, forward style.
*/
public void testRuleMatcher() {
String rules = "[r1: (?a p ?b), (?b q ?c) -> (?a, q, ?c)]" +
"[r2: (?a p ?b), (?b p ?c) -> (?a, p, ?c)]" +
"[r3: (?a p ?a), (n1 p ?c), (n1, p, ?a) -> (?a, p, ?c)]" +
"[r4: (n4 ?p ?a) -> (n4, ?a, ?p)]";
InfGraph infgraph = createInfGraph(rules);
infgraph.add(new Triple(n1, p, n2));
infgraph.add(new Triple(n2, p, n3));
infgraph.add(new Triple(n2, q, n3));
infgraph.add(new Triple(n4, p, n4));
TestUtil.assertIteratorValues(this, infgraph.find(null, null, null),
new Triple[] {
new Triple(n1, p, n2),
new Triple(n2, p, n3),
new Triple(n2, q, n3),
new Triple(n4, p, n4),
new Triple(n1, p, n3),
new Triple(n1, q, n3),
new Triple(n4, n4, p),
});
}
/**
* Test functor handling
*/
public void testEmbeddedFunctors() {
String rules = "(?C rdf:type owl:Restriction), (?C owl:onProperty ?P), (?C owl:allValuesFrom ?D) -> (?C rb:restriction all(?P, ?D))." +
"(?C rb:restriction all(eg:p, eg:D)) -> (?C rb:restriction 'allOK')." +
"[ -> (eg:foo eg:prop functor(eg:bar, 1)) ]" +
"[ (?x eg:prop functor(eg:bar, ?v)) -> (?x eg:propbar ?v) ]" +
"[ (?x eg:prop functor(?v, ?*)) -> (?x eg:propfunc ?v) ]" +
"";
Model data = ModelFactory.createDefaultModel();
Resource R1 = data.createResource(PrintUtil.egNS + "R1");
Resource D = data.createResource(PrintUtil.egNS + "D");
Property p = data.createProperty(PrintUtil.egNS, "p");
Property propbar = data.createProperty(PrintUtil.egNS, "propbar");
Property rbr = data.createProperty(ReasonerVocabulary.RBNamespace, "restriction");
R1.addProperty(RDF.type, OWL.Restriction)
.addProperty(OWL.onProperty, p)
.addProperty(OWL.allValuesFrom, D);
InfGraph infgraph = createInfGraph(rules, data.getGraph());
Model infModel = ModelFactory.createModelForGraph(infgraph);
Resource foo = infModel.createResource(PrintUtil.egNS + "foo");
RDFNode flit = infModel.getResource(R1.getURI()).getRequiredProperty(rbr).getObject();
assertNotNull(flit);
assertEquals(flit.toString(), "allOK");
// assertTrue(flit instanceof Literal);
// Functor func = (Functor)((Literal)flit).getValue();
// assertEquals("all", func.getName());
// assertEquals(p.getNode(), func.getArgs()[0]);
// assertEquals(D.getNode(), func.getArgs()[1]);
Literal one = (Literal)foo.getRequiredProperty(propbar).getObject();
assertEquals(new Integer(1), one.getValue());
}
/**
* The the minimal machinery for supporting builtins
*/
public void testBuiltins() {
String rules = //"[testRule1: (n1 ?p ?a) -> print('rule1test', ?p, ?a)]" +
"[r1: (n1 p ?x), addOne(?x, ?y) -> (n1 q ?y)]" +
"[r2: (n1 p ?x), lessThan(?x, 3) -> (n2 q ?x)]" +
"[axiom1: -> (n1 p 1)]" +
"[axiom2: -> (n1 p 4)]" +
"";
InfGraph infgraph = createInfGraph(rules);
TestUtil.assertIteratorValues(this, infgraph.find(n1, q, null),
new Triple[] {
new Triple(n1, q, Util.makeIntNode(2)),
new Triple(n1, q, Util.makeIntNode(5))
});
TestUtil.assertIteratorValues(this, infgraph.find(n2, q, null),
new Triple[] {
new Triple(n2, q, Util.makeIntNode(1))
});
}
/**
* Test schmea partial binding machinery, forward subset.
*/
public void testSchemaBinding() {
String rules = "[testRule1: (n1 p ?a) -> (n2, p, ?a)]" +
"[testRule2: (n1 q ?a) -> (n2, q, ?a)]" +
"[testRule3: (n2 p ?a), (n2 q ?a) -> (res p ?a)]" +
"[testBRule4: (n3 p ?a) <- (n1, p, ?a)]";
List ruleList = Rule.parseRules(rules);
Graph schema = Factory.createGraphMem();
schema.add(new Triple(n1, p, n3));
Graph data = Factory.createGraphMem();
data.add(new Triple(n1, q, n4));
data.add(new Triple(n1, q, n3));
Reasoner reasoner = createReasoner(ruleList);
Reasoner boundReasoner = reasoner.bindSchema(schema);
InfGraph infgraph = boundReasoner.bind(data);
TestUtil.assertIteratorValues(this, infgraph.find(null, null, null),
new Triple[] {
new Triple(n1, p, n3),
new Triple(n2, p, n3),
new Triple(n3, p, n3),
new Triple(n1, q, n4),
new Triple(n2, q, n4),
new Triple(n1, q, n3),
new Triple(n2, q, n3),
new Triple(res, p, n3)
});
}
/**
* The the "remove" builtin
*/
public void testRemoveBuiltin() {
String rules =
"[rule1: (?x p ?y), (?x q ?y) -> remove(0)]" +
"";
InfGraph infgraph = createInfGraph(rules);
infgraph.add(new Triple(n1, p, Util.makeIntNode(1)));
infgraph.add(new Triple(n1, p, Util.makeIntNode(2)));
infgraph.add(new Triple(n1, q, Util.makeIntNode(2)));
TestUtil.assertIteratorValues(this, infgraph.find(n1, null, null),
new Triple[] {
new Triple(n1, p, Util.makeIntNode(1)),
new Triple(n1, q, Util.makeIntNode(2))
});
}
/**
* Test the rebind operation.
*/
public void testRebind() {
String rules = "[rule1: (?x p ?y) -> (?x q ?y)]";
Graph data = Factory.createGraphMem();
data.add(new Triple(n1, p, n2));
InfGraph infgraph = createInfGraph(rules, data);
TestUtil.assertIteratorValues(this, infgraph.find(n1, null, null),
new Triple[] {
new Triple(n1, p, n2),
new Triple(n1, q, n2)
});
Graph ndata = Factory.createGraphMem();
ndata.add(new Triple(n1, p, n3));
infgraph.rebind(ndata);
TestUtil.assertIteratorValues(this, infgraph.find(n1, null, null),
new Triple[] {
new Triple(n1, p, n3),
new Triple(n1, q, n3)
});
}
/**
* Test that reset does actually clear out all the data.
* We use the RDFS configuration because uses both TGC, forward and backward
* rules and so is a good check.
*/
public void testRebind2() {
String NS = "http://jena.hpl.hp.com/test#";
Model base = ModelFactory.createDefaultModel();
Resource A = base.createResource(NS + "A");
Resource B = base.createResource(NS + "B");
Resource I = base.createResource(NS + "i");
A.addProperty(RDFS.subClassOf, B);
I.addProperty(RDF.type, A);
InfModel inf = ModelFactory.createInfModel(ReasonerRegistry.getRDFSReasoner(), base);
assertTrue(inf.containsResource(A) && inf.containsResource(I));
base.removeAll();
inf.rebind();
assertFalse(inf.containsResource(A) || inf.containsResource(I));
}
/**
* Test rebindAll reconsults a changed ruleset
*/
public void testRebindAll() {
String NS = "http://jena.hpl.hp.com/example#";
List rules1 = Rule.parseRules( "(?x http://jena.hpl.hp.com/example#p ?y) -> (?x http://jena.hpl.hp.com/example#q ?y)." );
List rules2 = Rule.parseRules( "(?x http://jena.hpl.hp.com/example#q ?y) -> (?x http://jena.hpl.hp.com/example#r ?y)." );
Model m = ModelFactory.createDefaultModel();
Property p = m.createProperty(NS + "p");
Property q = m.createProperty(NS + "q");
Property r = m.createProperty(NS + "r");
Resource a = m.createResource(NS + "a");
Resource b = m.createResource(NS + "b");
Statement s1 = m.createStatement(a, p, b);
Statement s2 = m.createStatement(a, q, b);
Statement s3 = m.createStatement(a, r, b);
m.add(s1);
GenericRuleReasoner reasoner = new GenericRuleReasoner(rules1);
InfModel infModel = ModelFactory.createInfModel(reasoner, m);
reasoner.addRules(rules2);
TestUtil.assertIteratorValues(this, infModel.listStatements(a, null, (RDFNode)null),
new Object[] {s1, s2});
((FBRuleInfGraph)infModel.getGraph()).rebindAll();
TestUtil.assertIteratorValues(this, infModel.listStatements(a, null, (RDFNode)null),
new Object[] {s1, s2, s3});
}
/**
* Test the close operation.
*/
public void testClose() {
String rules = "[rule1: (?x p ?y) -> (?x q ?y)]";
Graph data = Factory.createGraphMem();
data.add(new Triple(n1, p, n2));
InfGraph infgraph = createInfGraph(rules, data);
TestUtil.assertIteratorValues(this, infgraph.find(n1, null, null),
new Triple[] {
new Triple(n1, p, n2),
new Triple(n1, q, n2)
});
infgraph.close();
boolean foundException = false;
try {
infgraph.find(n1, null, null);
} catch (ClosedException e) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -