📄 testbasiclp.java
字号:
new Triple(C1, sC, C2),
new Triple(C2, sC, C3),
new Triple(a, ty, C1)
};
Graph data = Factory.createGraphMem();
for (int i = 0; i < triples.length; i++) {
data.add(triples[i]);
}
InfGraph infgraph = makeInfGraph(rules, data, tabled);
ExtendedIterator it = infgraph.find(a, ty, null);
Triple result = (Triple)it.next();
assertEquals(result.getSubject(), a);
assertEquals(result.getPredicate(), ty);
it.close();
// Make sure if we start again we get the full listing.
TestUtil.assertIteratorValues(this,
infgraph.find(a, ty, null),
new Object[] {
new Triple(a, ty, C1),
new Triple(a, ty, C2),
new Triple(a, ty, C3)
} );
}
/**
* A problem from the original backchainer tests - RDFS example which failed.
* Was due to unsupported multi-head statement.
*/
public void testProblem8() {
String ruleSrc =
"[rdfs9: (?a rdf:type ?y) <- bound(?y) (?x rdfs:subClassOf ?y) (?a rdf:type ?x)]" +
"[restriction4: (?C owl:equivalentClass max(?P, ?X)) <- (?C rdf:type owl:Restriction), (?C owl:onProperty ?P), (?C owl:maxCardinality ?X)]" +
"[restrictionProc11: (?X rdf:type max(?P, 1)) <- (?P rdf:type owl:FunctionalProperty), (?X rdf:type owl:Thing)]" +
"[equivalentClass1: (?Q rdfs:subClassOf ?P) <- (?P owl:equivalentClass ?Q) ]" +
"[equivalentClass1: (?P rdfs:subClassOf ?Q) <- (?P owl:equivalentClass ?Q) ]" +
"[restrictionSubclass1: (?X rdf:type ?D) <- bound(?D) (?D owl:equivalentClass ?R), isFunctor(?R) (?X rdf:type ?R)]";
doTest( ruleSrc,
new Node[] { ty, sC, OWL.equivalentClass.asNode() },
new Triple[] {
new Triple(a, ty, OWL.Thing.asNode()),
new Triple(p, ty, OWL.FunctionalProperty.asNode()),
new Triple(c, OWL.equivalentClass.asNode(), C1),
new Triple(C1, ty, OWL.Restriction.asNode()),
new Triple(C1, OWL.onProperty.asNode(), p),
new Triple(C1, OWL.maxCardinality.asNode(), Util.makeIntNode(1)),
},
new Triple(a, ty, c),
new Object[] {
new Triple(a, ty, c)
} );
}
/**
* Test derivation machinery
*/
public void testRuleDerivations() {
String rules = "[testRule1: (C2, p, ?a) <- (C1 p ?a)]" +
"[testRule2: (C2, q, ?a) <- (C1 q ?a)]" +
"[testRule3: (a p ?a) <- (C2 p ?a), (C2 q ?a)]";
List ruleList = Rule.parseRules(rules);
Graph data = Factory.createGraphMem();
data.add(new Triple(C1, p, C3));
data.add(new Triple(C1, q, C4));
data.add(new Triple(C1, q, C3));
InfGraph infgraph = makeInfGraph(ruleList, data, new Node[]{p, q});
infgraph.setDerivationLogging(true);
TestUtil.assertIteratorValues(this, infgraph.find(a, null, null),
new Triple[] {
new Triple(a, p, C3)
});
Iterator derivs = infgraph.getDerivation(new Triple(a, p, C3));
StringWriter outString = new StringWriter(250);
PrintWriter out = new PrintWriter(outString);
while (derivs.hasNext()) {
Derivation d = (Derivation) derivs.next();
d.printTrace(out, true);
}
out.flush();
String testString = TestUtil.normalizeWhiteSpace("Rule testRule3 concluded (a p C3) <-\n" +
" Rule testRule1 concluded (C2 p C3) <-\n" +
" Fact (C1 p C3)\r\n" +
" Rule testRule2 concluded (C2 q C3) <-\n" +
" Fact (C1 q C3)\r\n");
assertEquals(testString, TestUtil.normalizeWhiteSpace(outString.getBuffer().toString()));
}
/**
* A suspect problem, originally derived from the OWL rules - risk of unbound variables escaping.
* Not managed to isolate or reproduce the problem yet.
*/
public void testProblem9() {
String ruleSrc =
"[test: (?x owl:sameAs ?x) <- (?x rdf:type owl:Thing) ]" +
"[sameIndividualAs6: (?X rdf:type owl:Thing) <- (?X owl:sameAs ?Y) ]" +
"[ans: (?x p C1) <- (?y owl:sameAs ?x)]";
Node sI = OWL.sameAs.asNode();
doTest( ruleSrc,
new Node[] { ty, sI }, // Tabled predicates
new Triple[] { // init data
new Triple(a, ty, OWL.Thing.asNode()),
new Triple(b, sI, c),
},
new Triple(Node.ANY, p, Node.ANY), // query
new Object[] { // result
new Triple(a, p, C1),
new Triple(b, p, C1),
new Triple(c, p, C1),
} );
// new Triple(Node.ANY, ty, Node.ANY), // query
// new Object[] { // result
// new Triple(a, ty, OWL.Thing.asNode()),
// new Triple(b, ty, OWL.Thing.asNode())
// } );
}
/**
* Test 3-arg builtins such as arithmetic.
*/
public void testArithBuiltins() {
doBuiltinTest(
"[(a,r,0) <- (a,p,?x), (a,q,?y), lessThan(?x,?y)]" +
"[(a,r,1) <- (a,p,?x), (a,q,?y), ge(?x, ?y)]",
Util.makeIntNode(2),Util.makeIntNode(3), Util.makeIntNode(0)
);
doBuiltinTest(
"[(a,r,0) <- (a,p,?x), (a,q,?y), lessThan(?x,?y)]" +
"[(a,r,1) <- (a,p,?x), (a,q,?y), ge(?x, ?y)]",
Util.makeIntNode(3),Util.makeIntNode(3), Util.makeIntNode(1)
);
doBuiltinTest(
"[(a,r,0) <- (a,p,?x), (a,q,?y), le(?x,?y)]" +
"[(a,r,1) <- (a,p,?x), (a,q,?y), greaterThan(?x, ?y)]",
Util.makeIntNode(3),Util.makeIntNode(3), Util.makeIntNode(0)
);
doBuiltinTest(
"[(a,r,?z) <- (a,p,?x), (a,q,?y), min(?x,?y,?z)]",
Util.makeIntNode(2),Util.makeIntNode(3), Util.makeIntNode(2)
);
doBuiltinTest(
"[(a,r,?z) <- (a,p,?x), (a,q,?y), min(?x,?y,?z)]",
Util.makeIntNode(4),Util.makeIntNode(3), Util.makeIntNode(3)
);
doBuiltinTest(
"[(a,r,?z) <- (a,p,?x), (a,q,?y), max(?x,?y,?z)]",
Util.makeIntNode(2),Util.makeIntNode(3), Util.makeIntNode(3)
);
doBuiltinTest(
"[(a,r,?z) <- (a,p,?x), (a,q,?y), max(?x,?y,?z)]",
Util.makeIntNode(4),Util.makeIntNode(3), Util.makeIntNode(4)
);
}
/**
* Test the temporary list builtins
*/
public void testListBuiltins() {
String ruleSrc = "[(a r ?n) <- (a p ?l), listLength(?l, ?n)]" +
"[(a s ?e) <- (a p ?l), listEntry(?l, 1, ?e)]";
List rules = Rule.parseRules(ruleSrc);
Graph data = Factory.createGraphMem();
data.add(new Triple(a, p, Util.makeList(new Node[]{C1,C2,C3},data)));
InfGraph infgraph = makeInfGraph(rules, data);
TestUtil.assertIteratorValues(this,
infgraph.find(new Triple(a, r, Node.ANY)),
new Triple[] {
new Triple(a, r, Util.makeIntNode(3))
});
TestUtil.assertIteratorValues(this,
infgraph.find(new Triple(a, s, Node.ANY)),
new Triple[] {
new Triple(a, s, C2)
});
rules = Rule.parseRules(
"[(a s b) <- (a p ?l), (a, q, ?j) listEqual(?l, ?j)]" +
"[(a s c) <- (a p ?l), (a, q, ?j) listNotEqual(?l, ?j)]" +
"[(a s d) <- (a p ?l), (a, r, ?j) listEqual(?l, ?j)]" +
"[(a s e) <- (a p ?l), (a, r, ?j) listNotEqual(?l, ?j)]"
);
data = Factory.createGraphMem();
data.add(new Triple(a, p,
Util.makeList( new Node[]{C1, Util.makeIntNode(3), C3}, data) ));
data.add(new Triple(a, q,
Util.makeList( new Node[]{C3, C1, Util.makeLongNode(3)}, data) ));
data.add(new Triple(a, r,
Util.makeList( new Node[]{C3, C1, Util.makeLongNode(2)}, data) ));
infgraph = makeInfGraph(rules, data);
TestUtil.assertIteratorValues(this,
infgraph.find(new Triple(a, s, Node.ANY)),
new Triple[] {
new Triple(a, s, b),
new Triple(a, s, e),
});
rules = Rule.parseRules(
"[(b r ?j) <- (a p ?l), (a, q, ?j) listContains(?l, ?j)]" +
"[(b s ?j) <- (a p ?l), (a, q, ?j) listNotContains(?l, ?j)]"
);
data = Factory.createGraphMem();
data.add(new Triple(a, p,
Util.makeList( new Node[]{C1, Util.makeIntNode(3), C3}, data) ));
data.add(new Triple(a, q, C1));
data.add(new Triple(a, q, Util.makeLongNode(3)));;
data.add(new Triple(a, q, C2));
infgraph = makeInfGraph(rules, data);
TestUtil.assertIteratorValues(this,
infgraph.find(new Triple(b, Node.ANY, Node.ANY)),
new Triple[] {
new Triple(b, r, C1),
new Triple(b, r, Util.makeIntNode(3)),
new Triple(b, s, C2),
});
}
/**
* Test that we detect concurrent modification of LP graphs with
* non-closed iterators.
*/
public void testCME() {
String ruleSrc = "(?a p 1) <- (?a p 0). (?a p 2) <- (?a p 0).";
List rules = Rule.parseRules(ruleSrc);
Graph data = Factory.createGraphMem();
data.add(new Triple(a, p, Util.makeIntNode(0)));
InfGraph infgraph = makeInfGraph(rules, data);
// Check the base case works
TestUtil.assertIteratorValues(this,
infgraph.find(new Triple(a, p, Node.ANY)),
new Triple[] {
new Triple(a, p, Util.makeIntNode(0)),
new Triple(a, p, Util.makeIntNode(1)),
new Triple(a, p, Util.makeIntNode(2)),
});
// Now force a CME
boolean ok = false;
ExtendedIterator i = infgraph.find(new Triple(a, p, Node.ANY));
try {
i.next();
infgraph.add( new Triple(a, p, Util.makeIntNode(4)) );
i.next();
} catch (ConcurrentModificationException e) {
ok = true;
} finally {
i.close();
}
assertTrue("Expect CME on unclosed iterators", ok);
}
/**
* Generic test operation.
* @param ruleSrc the source of the rules
* @param triples a set of triples to insert in the graph before the query
* @param query the TripleMatch to search for
* @param results the array of expected results
*/
private void doTest(String ruleSrc, Triple[] triples, TripleMatch query, Object[] results) {
List rules = Rule.parseRules(ruleSrc);
Graph data = Factory.createGraphMem();
for (int i = 0; i < triples.length; i++) {
data.add(triples[i]);
}
InfGraph infgraph = makeInfGraph(rules, data);
TestUtil.assertIteratorValues(this, infgraph.find(query), results);
}
/**
* Generic test operation.
* @param ruleSrc the source of the rules
* @param tabled the predicates that should be tabled
* @param triples a set of triples to insert in the graph before the query
* @param query the TripleMatch to search for
* @param results the array of expected results
*/
private void doTest(String ruleSrc, Node[] tabled, Triple[] triples, TripleMatch query, Object[] results) {
List rules = Rule.parseRules(ruleSrc);
Graph data = Factory.createGraphMem();
for (int i = 0; i < triples.length; i++) {
data.add(triples[i]);
}
InfGraph infgraph = makeInfGraph(rules, data, tabled);
TestUtil.assertIteratorValues(this, infgraph.find(query), results);
}
/**
* Generic base test operation on a graph with the single triple (a, p, b)
* @param ruleSrc the source of the rules
* @param query the TripleMatch to search for
* @param results the array of expected results
*/
private void doBasicTest(String ruleSrc, TripleMatch query, Object[] results) {
doTest(ruleSrc, new Triple[]{new Triple(a,p,b)}, query, results);
}
/**
* Generic test operation.
* @param rule to test a simple builtin operation
* @param param1 value to bind to first parameter by (a,p,_)
* @param param2 value to bind to first parameter by (a,q,_)
* @param result the expected result to be found by (a,r,_)
*/
private void doBuiltinTest(String ruleSrc, Node param1, Node param2, Node result) {
doTest(ruleSrc,
new Triple[] {
new Triple(a, p, param1),
new Triple(a, q, param2)
},
new Triple(a, r, Node.ANY),
new Triple[] {
new Triple(a, r, result)
});
}
}
/*
(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 + -