📄 testbackchainer.java
字号:
new Triple(a, r, b)
} );
}
/**
* Test troublesome rdfs rules
*/
public void testRDFSProblemsb() {
Graph data = Factory.createGraphMem();
data.add(new Triple(C1, sC, C2));
data.add(new Triple(C2, sC, C3));
data.add(new Triple(C1, ty, RDFS.Class.asNode()));
data.add(new Triple(C2, ty, RDFS.Class.asNode()));
data.add(new Triple(C3, ty, RDFS.Class.asNode()));
List rules = Rule.parseRules(
"[rdfs8: (?a rdfs:subClassOf ?b), (?b rdfs:subClassOf ?c) -> (?a rdfs:subClassOf ?c)]" +
"[rdfs7: (?a rdf:type rdfs:Class) -> (?a rdfs:subClassOf ?a)]"
);
Reasoner reasoner = createReasoner(rules);
InfGraph infgraph = reasoner.bind(data);
TestUtil.assertIteratorValues(this,
infgraph.find(null, sC, null),
new Object[] {
new Triple(C1, sC, C2),
new Triple(C1, sC, C3),
new Triple(C1, sC, C1),
new Triple(C2, sC, C3),
new Triple(C2, sC, C2),
new Triple(C3, sC, C3),
} );
}
/**
* Test troublesome rdfs rules
*/
public void testRDFSProblems() {
Graph data = Factory.createGraphMem();
data.add(new Triple(p, sP, q));
data.add(new Triple(q, sP, r));
data.add(new Triple(C1, sC, C2));
data.add(new Triple(C2, sC, C3));
data.add(new Triple(a, ty, C1));
List rules = Rule.parseRules(
"[rdfs8: (?a rdfs:subClassOf ?b), (?b rdfs:subClassOf ?c) -> (?a rdfs:subClassOf ?c)]" +
"[rdfs9: (?x rdfs:subClassOf ?y), (?a rdf:type ?x) -> (?a rdf:type ?y)]" +
// "[-> (rdf:type rdfs:range rdfs:Class)]" +
"[rdfs3: (?x ?p ?y), (?p rdfs:range ?c) -> (?y rdf:type ?c)]" +
"[rdfs7: (?a rdf:type rdfs:Class) -> (?a rdfs:subClassOf ?a)]"
);
Reasoner reasoner = createReasoner(rules);
InfGraph infgraph = reasoner.bind(data);
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)
} );
TestUtil.assertIteratorValues(this,
infgraph.find(C1, sC, a),
new Object[] {
} );
}
/**
* Test complex rule head unification
*/
public void testHeadUnify() {
Graph data = Factory.createGraphMem();
data.add(new Triple(c, q, d));
List rules = Rule.parseRules(
"[r1: (c r ?x) <- (?x p f(?x b))]" +
"[r2: (?y p f(a ?y)) <- (c q ?y)]"
);
Reasoner reasoner = createReasoner(rules);
InfGraph infgraph = reasoner.bind(data);
TestUtil.assertIteratorValues(this,
infgraph.find(c, r, null), new Object[] { } );
data.add(new Triple(c, q, a));
rules = Rule.parseRules(
"[r1: (c r ?x) <- (?x p f(?x a))]" +
"[r2: (?y p f(a ?y)) <- (c q ?y)]"
);
reasoner = createReasoner(rules);
infgraph = reasoner.bind(data);
TestUtil.assertIteratorValues(this,
infgraph.find(c, r, null),
new Object[] {
new Triple(c, r, a)
} );
data = Factory.createGraphMem();
data.add(new Triple(a, q, a));
data.add(new Triple(a, q, b));
data.add(new Triple(a, q, c));
data.add(new Triple(b, q, d));
data.add(new Triple(b, q, b));
rules = Rule.parseRules(
"[r1: (c r ?x) <- (?x p ?x)]" +
"[r2: (?x p ?y) <- (a q ?x), (b q ?y)]"
);
reasoner = createReasoner(rules);
infgraph = reasoner.bind(data);
TestUtil.assertIteratorValues(this,
infgraph.find(c, r, null),
new Object[] {
new Triple(c, r, b)
} );
rules = Rule.parseRules(
"[r1: (c r ?x) <- (?x p ?x)]" +
"[r2: (a p ?x) <- (a q ?x)]"
);
reasoner = createReasoner(rules);
infgraph = reasoner.bind(data);
TestUtil.assertIteratorValues(this,
infgraph.find(c, r, null),
new Object[] {
new Triple(c, r, a)
} );
}
/**
* Test restriction example
*/
public void testRestriction1() {
Graph data = Factory.createGraphMem();
data.add(new Triple(a, ty, r));
data.add(new Triple(a, p, b));
data.add(new Triple(r, sC, C1));
data.add(new Triple(C1, ty, OWL.Restriction.asNode()));
data.add(new Triple(C1, OWL.onProperty.asNode(), p));
data.add(new Triple(C1, OWL.allValuesFrom.asNode(), c));
List rules = Rule.parseRules(
"[rdfs9: (?x rdfs:subClassOf ?y) (?a rdf:type ?x) -> (?a rdf:type ?y)]" +
"[restriction2: (?C rdf:type owl:Restriction), (?C owl:onProperty ?P), (?C owl:allValuesFrom ?D) -> (?C owl:equivalentClass all(?P, ?D))]" +
"[rs2: (?D owl:equivalentClass all(?P,?C)), (?X rdf:type ?D) -> (?X rdf:type all(?P,?C))]" +
"[rp4: (?X rdf:type all(?P, ?C)), (?X ?P ?Y) -> (?Y rdf:type ?C)]"
);
Reasoner reasoner = createReasoner(rules);
InfGraph infgraph = reasoner.bind(data);
TestUtil.assertIteratorValues(this,
infgraph.find(b, ty, c), new Object[] {
new Triple(b, ty, c)
} );
}
/**
* Test restriction example. The rules are more than the minimum required
* to solve the query and they interact to given run away seaches if there
* is a problem.
*/
public void testRestriction2() {
Graph data = Factory.createGraphMem();
data.add(new Triple(a, ty, OWL.Thing.asNode()));
data.add(new Triple(p, ty, OWL.FunctionalProperty.asNode()));
data.add(new Triple(c, OWL.equivalentClass.asNode(), C1));
data.add(new Triple(C1, ty, OWL.Restriction.asNode()));
data.add(new Triple(C1, OWL.onProperty.asNode(), p));
data.add(new Triple(C1, OWL.maxCardinality.asNode(), Util.makeIntNode(1)));
List rules = Rule.parseRules(
// these ones are required for the inference.
"[rdfs9: bound(?y) (?x rdfs:subClassOf ?y) (?a rdf:type ?x) -> (?a rdf:type ?y)]" +
"[restriction4: (?C rdf:type owl:Restriction), (?C owl:onProperty ?P), (?C owl:maxCardinality ?X) -> (?C owl:equivalentClass max(?P, ?X))]" +
"[restrictionProc11: (?P rdf:type owl:FunctionalProperty), (?X rdf:type owl:Thing) -> (?X rdf:type max(?P, 1))]" +
// "[equivalentClass1: (?P owl:equivalentClass ?Q) -> (?P rdfs:subClassOf ?Q), (?Q rdfs:subClassOf ?P) ]" +
"[equivalentClass1: (?P owl:equivalentClass ?Q) -> (?P rdfs:subClassOf ?Q) ]" +
"[equivalentClass1: (?P owl:equivalentClass ?Q) -> (?Q rdfs:subClassOf ?P) ]" +
"[restrictionSubclass1: bound(?D) (?D owl:equivalentClass ?R), isFunctor(?R) (?X rdf:type ?R)-> (?X rdf:type ?D)]" +
// these ones are noise which can cause run aways or failures if there are bugs
"[rdfs8: unbound(?c) (?a rdfs:subClassOf ?b) (?b rdfs:subClassOf ?c) -> (?a rdfs:subClassOf ?c)]" +
"[rdfs8: bound(?c) (?b rdfs:subClassOf ?c) (?a rdfs:subClassOf ?b) -> (?a rdfs:subClassOf ?c)]" +
"[rdfs9: unbound(?y) (?a rdf:type ?x) (?x rdfs:subClassOf ?y) -> (?a rdf:type ?y)]" +
"[-> (rdf:type rdfs:range rdfs:Class)]" +
"[rdfs3: bound(?c) (?p rdfs:range ?c) (?x ?p ?y) -> (?y rdf:type ?c)]" +
"[rdfs7: (?a rdf:type rdfs:Class) -> (?a rdfs:subClassOf ?a)]" +
"[restrictionProc13: (owl:Thing rdfs:subClassOf all(?P, ?C)) -> (?P rdfs:range ?C)]" +
"[restrictionSubclass1: unbound(?D) (?X rdf:type ?R), isFunctor(?R) (?D owl:equivalentClass ?R) -> (?X rdf:type ?D)]" +
"[restrictionSubclass2: bound(?R), isFunctor(?R), (?D owl:equivalentClass ?R),(?X rdf:type ?D) -> (?X rdf:type ?R)]" +
"[restrictionSubclass2: unbound(?R), (?X rdf:type ?D), (?D owl:equivalentClass ?R) isFunctor(?R) -> (?X rdf:type ?R)]" +
"" );
Reasoner reasoner = createReasoner(rules);
InfGraph infgraph = reasoner.bind(data);
TestUtil.assertIteratorValues(this,
infgraph.find(a, ty, C1), new Object[] {
new Triple(a, ty, C1)
} );
TestUtil.assertIteratorValues(this,
infgraph.find(a, ty, c), new Object[] {
new Triple(a, ty, c)
} );
}
/**
* Test restriction example
*/
public void testRestriction3() {
Graph data = Factory.createGraphMem();
data.add(new Triple(a, ty, r));
data.add(new Triple(r, sC, C1));
data.add(new Triple(C1, ty, OWL.Restriction.asNode()));
data.add(new Triple(C1, OWL.onProperty.asNode(), p));
data.add(new Triple(C1, OWL.allValuesFrom.asNode(), c));
List rules = Rule.parseRules(
"[-> (rdfs:subClassOf rdfs:range rdfs:Class)]" +
// "[-> (owl:Class rdfs:subClassOf rdfs:Class)]" +
"[rdfs3: bound(?c) (?p rdfs:range ?c) (?x ?p ?y) -> (?y rdf:type ?c)]" +
"[rdfs3: unbound(?c) (?x ?p ?y), (?p rdfs:range ?c) -> (?y rdf:type ?c)]" +
"[rdfs7: (?a rdf:type rdfs:Class) -> (?a rdfs:subClassOf ?a)]" +
"[rdfs8: (?a rdfs:subClassOf ?b) (?b rdfs:subClassOf ?c) -> (?a rdfs:subClassOf ?c)]" +
"[restrictionProc4b: bound(?Y) (?X ?P ?Y), notEqual(?P, rdf:type), (?X rdf:type all(?P, ?C)),-> (?Y rdf:type ?C)]" +
"[restrictionProc4b: unbound(?Y), (?X rdf:type all(?P, ?C)), (?X ?P ?Y), notEqual(?P, rdf:type),-> (?Y rdf:type ?C)]" +
"" );
Reasoner reasoner = createReasoner(rules);
InfGraph infgraph = reasoner.bind(data);
TestUtil.assertIteratorValues(this,
infgraph.find(null, ty, c), new Object[] {
} );
}
/**
* Test close and halt operation.
*/
public void testClose() {
Graph data = Factory.createGraphMem();
data.add(new Triple(p, sP, q));
data.add(new Triple(q, sP, r));
data.add(new Triple(C1, sC, C2));
data.add(new Triple(C2, sC, C3));
data.add(new Triple(a, ty, C1));
data.add(new Triple(ty, RDFS.range.asNode(), RDFS.Class.asNode()));
List rules = Rule.parseRules(
"[rdfs8: (?a rdfs:subClassOf ?b), (?b rdfs:subClassOf ?c) -> (?a rdfs:subClassOf ?c)]" +
"[rdfs9: (?x rdfs:subClassOf ?y), (?a rdf:type ?x) -> (?a rdf:type ?y)]" +
// "[-> (rdf:type rdfs:range rdfs:Class)]" +
"[rdfs3: (?x ?p ?y), (?p rdfs:range ?c) -> (?y rdf:type ?c)]" +
"[rdfs7: (?a rdf:type rdfs:Class) -> (?a rdfs:subClassOf ?a)]"
);
Reasoner reasoner = createReasoner(rules);
InfGraph infgraph = reasoner.bind(data);
// Get just one result
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)
} );
}
/**
* Test problematic rdfs case
*/
public void testBug1() throws IOException {
Graph data = Factory.createGraphMem();
Node p = Node.createURI("http://www.hpl.hp.com/semweb/2003/eg#p");
Node r = Node.createURI("http://www.hpl.hp.com/semweb/2003/eg#r");
Node C1 = Node.createURI("http://www.hpl.hp.com/semweb/2003/eg#C1");
data.add(new Triple(a, p, b));
List rules = Rule.parseRules(Util.loadRuleParserFromResourceFile("testing/reasoners/bugs/rdfs-error1.brules"));
Reasoner reasoner = createReasoner(rules);
InfGraph infgraph = reasoner.bind(data);
TestUtil.assertIteratorValues(this,
infgraph.find(b, ty, C1),
new Object[] {
new Triple(b, ty, C1)
} );
}
}
/*
(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 + -