📄 testfbrules.java
字号:
TestUtil.assertIteratorValues(this, infgraph.find(n1, null, n2),
new Triple[] {
new Triple(n1, ne, n2),
new Triple(n1, le, n2),
new Triple(n1, lt, n2),
});
TestUtil.assertIteratorValues(this, infgraph.find(n2, null, n3),
new Triple[] {
new Triple(n2, ne, n3),
new Triple(n2, le, n3),
new Triple(n2, lt, n3),
});
TestUtil.assertIteratorValues(this, infgraph.find(n2, null, n1),
new Triple[] {
new Triple(n2, ne, n1),
new Triple(n2, ge, n1),
new Triple(n2, gt, n1),
});
TestUtil.assertIteratorValues(this, infgraph.find(n3, null, n2),
new Triple[] {
new Triple(n3, ne, n2),
new Triple(n3, ge, n2),
new Triple(n3, gt, n2),
});
// Arithmetic
rules =
"[r1: (?x p ?a), (?x q ?b), sum(?a, ?b, ?c) -> (?x s ?c)]" +
"[r2: (?x p ?a), (?x q ?b), product(?a, ?b, ?c) -> (?x t ?c)]" +
"[r3: (?x p ?a), (?x q ?b), difference(?b, ?a, ?c) -> (?x u ?c)]" +
"[r4: (?x p ?a), (?x q ?b), quotient(?b, ?a, ?c) -> (?x v ?c)]" +
"[r4: (?x p ?a), (?x q ?b), min(?b, ?a, ?c) -> (?x r ?c)]" +
"[r4: (?x p ?a), (?x q ?b), max(?b, ?a, ?c) -> (?x x ?c)]" +
"";
ruleList = Rule.parseRules(rules);
data = Factory.createGraphMem();
data.add(new Triple(n1, p, Util.makeIntNode(3)) );
data.add(new Triple(n1, q, Util.makeIntNode(5)) );
infgraph = createReasoner(ruleList).bind(data);
TestUtil.assertIteratorValues(this, infgraph.find(n1, null, null),
new Triple[] {
new Triple(n1, p, Util.makeIntNode(3)),
new Triple(n1, q, Util.makeIntNode(5)),
new Triple(n1, s, Util.makeIntNode(8)),
new Triple(n1, t, Util.makeIntNode(15)),
new Triple(n1, u, Util.makeIntNode(2)),
new Triple(n1, v, Util.makeIntNode(1)),
new Triple(n1, r, Util.makeIntNode(3)),
new Triple(n1, x, Util.makeIntNode(5)),
});
// Note type checking
rules =
"[r1: (?x p ?y), isLiteral(?y) -> (?x s 'literal')]" +
"[r1: (?x p ?y), notLiteral(?y) -> (?x s 'notLiteral')]" +
"[r1: (?x p ?y), isBNode(?y) -> (?x s 'bNode')]" +
"[r1: (?x p ?y), notBNode(?y) -> (?x s 'notBNode')]" +
"";
ruleList = Rule.parseRules(rules);
data = Factory.createGraphMem();
data.add(new Triple(n1, p, Util.makeIntNode(3)) );
data.add(new Triple(n2, p, res));
data.add(new Triple(n3, p, Node.createAnon()));
infgraph = createReasoner(ruleList).bind(data);
TestUtil.assertIteratorValues(this, infgraph.find(n1, s, null),
new Triple[] {
new Triple(n1, s, Node.createLiteral("literal", "", null)),
new Triple(n1, s, Node.createLiteral("notBNode", "", null)),
});
TestUtil.assertIteratorValues(this, infgraph.find(n2, s, null),
new Triple[] {
new Triple(n2, s, Node.createLiteral("notLiteral", "", null)),
new Triple(n2, s, Node.createLiteral("notBNode", "", null)),
});
TestUtil.assertIteratorValues(this, infgraph.find(n3, s, null),
new Triple[] {
new Triple(n3, s, Node.createLiteral("notLiteral", "", null)),
new Triple(n3, s, Node.createLiteral("bNode", "", null)),
});
// Data type checking
rules =
"[r1: (?x p ?y), isDType(?y, rdfs:Literal) -> (?x s 'isLiteral')]" +
"[r1: (?x p ?y), isDType(?y, http://www.w3.org/2001/XMLSchema#int) -> (?x s 'isXSDInt')]" +
"[r1: (?x p ?y), isDType(?y, http://www.w3.org/2001/XMLSchema#string) -> (?x s 'isXSDString')]" +
"[r1: (?x p ?y), notDType(?y, rdfs:Literal) -> (?x s 'notLiteral')]" +
"[r1: (?x p ?y), notDType(?y, http://www.w3.org/2001/XMLSchema#int) -> (?x s 'notXSDInt')]" +
"[r1: (?x p ?y), notDType(?y, http://www.w3.org/2001/XMLSchema#string) -> (?x s 'notXSDString')]" +
"";
ruleList = Rule.parseRules(rules);
data = Factory.createGraphMem();
data.add(new Triple(n1, p, Util.makeIntNode(3)) );
data.add(new Triple(n2, p, Node.createLiteral("foo", "", null)) );
data.add(new Triple(n3, p, Node.createLiteral("foo", "", XSDDatatype.XSDstring)) );
data.add(new Triple(n4, p, n4));
data.add(new Triple(n5, p, Node.createLiteral("-1", "", XSDDatatype.XSDnonNegativeInteger)) );
infgraph = createReasoner(ruleList).bind(data);
TestUtil.assertIteratorValues(this, infgraph.find(null, s, null),
new Triple[] {
new Triple(n1, s, Node.createLiteral("isLiteral", "", null)),
new Triple(n1, s, Node.createLiteral("isXSDInt", "", null)),
new Triple(n1, s, Node.createLiteral("notXSDString", "", null)),
new Triple(n2, s, Node.createLiteral("isLiteral", "", null)),
new Triple(n2, s, Node.createLiteral("notXSDInt", "", null)),
new Triple(n2, s, Node.createLiteral("isXSDString", "", null)),
new Triple(n3, s, Node.createLiteral("isLiteral", "", null)),
new Triple(n3, s, Node.createLiteral("notXSDInt", "", null)),
new Triple(n3, s, Node.createLiteral("isXSDString", "", null)),
new Triple(n4, s, Node.createLiteral("notLiteral", "", null)),
new Triple(n4, s, Node.createLiteral("notXSDInt", "", null)),
new Triple(n4, s, Node.createLiteral("notXSDString", "", null)),
new Triple(n5, s, Node.createLiteral("notLiteral", "", null)),
new Triple(n5, s, Node.createLiteral("notXSDInt", "", null)),
new Triple(n5, s, Node.createLiteral("notXSDString", "", null)),
});
// Literal counting
rules = "[r1: (?x p ?y), countLiteralValues(?x, p, ?c) -> (?x s ?c)]";
ruleList = Rule.parseRules(rules);
data = Factory.createGraphMem();
data.add(new Triple(n1, p, Util.makeIntNode(2)) );
data.add(new Triple(n1, p, Util.makeIntNode(2)) );
data.add(new Triple(n1, p, Util.makeIntNode(3)) );
data.add(new Triple(n1, p, n2) );
infgraph = createReasoner(ruleList).bind(data);
TestUtil.assertIteratorValues(this, infgraph.find(n1, s, null),
new Triple[] {
new Triple(n1, s, Util.makeIntNode(2)),
});
// Map list operation
rules = "[r1: (n1 p ?l) -> listMapAsSubject(?l, q, C1)]" +
"[r2: (n1 p ?l) -> listMapAsObject ( a, q, ?l)]";
ruleList = Rule.parseRules(rules);
data = Factory.createGraphMem();
data.add(new Triple(n1, p, Util.makeList(new Node[]{b, c, d}, data) ));
infgraph = createReasoner(ruleList).bind(data);
TestUtil.assertIteratorValues(this, infgraph.find(null, q, null),
new Triple[] {
new Triple(b, q, C1),
new Triple(c, q, C1),
new Triple(d, q, C1),
new Triple(a, q, b),
new Triple(a, q, c),
new Triple(a, q, d),
});
}
/**
* Check string manipulation builtins, new at 2.5.
*/
public void testStringBuiltins() {
String rules =
"[r1: (?x p ?y) strConcat(?y, rdf:type, 'foo', ?z) -> (?x q ?z) ] \n" +
"[r1: (?x p ?y) strConcat(?z) -> (?x q ?z) ] \n" +
"[r2: (?x p ?y) uriConcat('http://jena.hpl.hp.com/test#', ?y, ?z) -> (?x q ?z) ]";
List ruleList = Rule.parseRules(rules);
Graph data = Factory.createGraphMem();
data.add(new Triple(n1, p, Node.createLiteral("test")) );
InfGraph infgraph = createReasoner(ruleList).bind(data);
TestUtil.assertIteratorValues(this, infgraph.find(null, q, null),
new Triple[] {
new Triple(n1, q, Node.createLiteral("testhttp://www.w3.org/1999/02/22-rdf-syntax-ns#typefoo")),
new Triple(n1, q, Node.createLiteral("")),
new Triple(n1, q, Node.createURI("http://jena.hpl.hp.com/test#test")),
});
rules =
"[r1: (?x p ?y) regex(?y, '(.*)\\\\s(.*) (f.*)') -> (?x q 'ok') ] \n" +
"[r2: (?x p ?y) regex(?y, '(.*)\\\\s(.*) (f.*)', ?m1, ?m2, ?m3) -> (?x r ?m2) ] \n" +
"";
ruleList = Rule.parseRules(rules);
data = Factory.createGraphMem();
data.add(new Triple(n1, p, Node.createLiteral("foo bar foo")) );
data.add(new Triple(n2, p, Node.createLiteral("foo bar baz")) );
infgraph = createReasoner(ruleList).bind(data);
TestUtil.assertIteratorValues(this, infgraph.find(null, q, null),
new Triple[] {
new Triple(n1, q, Node.createLiteral("ok")),
});
TestUtil.assertIteratorValues(this, infgraph.find(null, r, null),
new Triple[] {
new Triple(n1, r, Node.createLiteral("bar")),
});
}
/**
* More extensive check of arithmetic which checks that binding to an
* expected answer also works
*/
public void testArithmetic() {
doTestArithmetic("sum", 3, 5, 8);
doTestArithmetic("difference", 5, 3, 2);
doTestArithmetic("product", 3, 5, 15);
doTestArithmetic("quotient", 12, 3, 4);
doTestArithmetic("min", 3, 5, 3);
doTestArithmetic("max", 3, 5, 5);
}
/**
* Internals of testArithmetic which sets up a rule
* and executes it with expected and illegal answers.
*/
private void doTestArithmetic(String op, int arg1, int arg2, int expected) {
String rules =
"[r1: (?x p ?a), (?x q ?b), (?x r ?c) " + op + "(?a, ?b, ?c) -> (?x s ?c)]\n " +
"[r2: (?x p ?a), (?x q ?b), (?x t ?c) " + op + "(?a, ?b, ?c) -> (?x u ?c)]";
List ruleList = Rule.parseRules(rules);
Graph data = Factory.createGraphMem();
data.add(new Triple(n1, p, Util.makeIntNode(arg1)) );
data.add(new Triple(n1, q, Util.makeIntNode(arg2)) );
data.add(new Triple(n1, r, Util.makeIntNode(expected)) );
data.add(new Triple(n1, t, Util.makeIntNode(expected+1)) );
InfGraph infgraph = createReasoner(ruleList).bind(data);
assertTrue( infgraph.contains(n1, s, Util.makeIntNode(expected)));
assertFalse( infgraph.contains(n1, u, Node.ANY) );
}
/**
* Helper - returns the single object value for an s/p pair, asserts an error
* if there is more than one.
*/
private Node getValue(Graph g, Node s, Node p) {
ExtendedIterator i = g.find(s, p, null);
assertTrue(i.hasNext());
Node result = ((Triple)i.next()).getObject();
if (i.hasNext()) {
assertTrue("multiple values not expected", false);
i.close();
}
return result;
}
/**
* Investigate a suspicious case in the OWL ruleset, is the backchainer
* returning duplicate values?
*/
public void testDuplicatesEC4() throws IOException {
boolean prior = JenaParameters.enableFilteringOfHiddenInfNodes;
try {
JenaParameters.enableFilteringOfHiddenInfNodes = false;
Model premisesM = FileManager.get().loadModel("file:testing/wg/equivalentClass/premises004.rdf");
Graph data = premisesM.getGraph();
Reasoner reasoner = new OWLFBRuleReasoner(OWLFBRuleReasonerFactory.theInstance());
InfGraph infgraph = reasoner.bind(data);
Node rbPrototypeProp = Node.createURI(ReasonerVocabulary.RBNamespace+"prototype");
int count = 0;
for (Iterator i = infgraph.find(null, rbPrototypeProp, null); i.hasNext(); ) {
Object t = i.next();
// System.out.println(" - " + PrintUtil.print(t));
count++;
}
// listFBGraph("direct databind case", (FBRuleInfGraph)infgraph);
assertEquals(5, count);
infgraph = reasoner.bindSchema(data).bind(Factory.createGraphMem());
count = 0;
for (Iterator i = infgraph.find(null, rbPrototypeProp, null); i.hasNext(); ) {
Object t = i.next();
// System.out.println(" - " + PrintUtil.print(t));
count++;
}
// listFBGraph("bindSchema case", (FBRuleInfGraph)infgraph);
assertEquals(5, count);
} finally {
JenaParameters.enableFilteringOfHiddenInfNodes = prior;
}
}
/**
* Check cost of creating an empty OWL closure.
*/
public void temp() {
Graph data = Factory.createGraphMem();
Graph data2 = Factory.createGraphMem();
Reasoner reasoner = new OWLFBRuleReasoner(OWLFBRuleReasonerFactory.theInstance());
FBRuleInfGraph infgraph = (FBRuleInfGraph)reasoner.bind(data);
FBRuleInfGraph infgraph2 = (FBRuleInfGraph)reasoner.bind(data2);
long t1 = System.currentTimeMillis();
infgraph.prepare();
long t2 = System.currentTimeMillis();
System.out.println("Prepare on empty graph = " + (t2-t1) +"ms");
t1 = System.currentTimeMillis();
infgraph2.prepare();
t2 = System.currentTimeMillis();
System.out.println("Prepare on empty graph = " + (t2-t1) +"ms");
}
/**
* Helper function to list a graph out to logger.info
*/
public void listGraph(Graph g) {
for (Iterator i = g.find(null,null,null); i.hasNext();) {
Triple t = (Triple)i.next();
logger.info(PrintUtil.print(t));
}
logger.info(" -------- ");
}
/**
* Helper function to list the interesting parts of an FBInfGraph.
*/
public void listFBGraph(String message, FBRuleInfGraph graph) {
logger.info(message);
logger.info("Raw graph data");
listGraph(graph.getRawGraph());
logger.info("Static deductions");
listGraph(graph.getDeductionsGraph());
}
}
/*
(c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -