📄 testbugs.java
字号:
OntModel m = ModelFactory.createOntologyModel(s, null);
m.read( "file:testing/ontology/bugs/test_oh_01.owl");
// System.out.println( prompt );
OntClass r = m.getOntClass( NS + "Reiseliv" );
List q = new ArrayList();
Set seen = new HashSet();
q.add( r );
while (!q.isEmpty()) {
OntClass c = (OntClass) q.remove( 0 );
seen.add( c );
for (Iterator i = c.listSubClasses( true ); i.hasNext(); ) {
OntClass sub = (OntClass) i.next();
if (!seen.contains( sub )) {
q.add( sub );
}
}
// System.out.println( " Seen class " + c );
}
// check we got all classes
int mask = (1 << expected.length) - 1;
for (int j = 0; j < expected.length; j++) {
if (seen.contains( expected[j] )) {
mask &= ~(1 << j);
}
else {
// System.out.println( "Expected but did not see " + expected[j] );
}
}
for (Iterator k = seen.iterator(); k.hasNext(); ) {
Resource res = (Resource) k.next();
boolean isExpected = false;
for (int j = 0; !isExpected && j < expected.length; j++) {
isExpected = expected[j].equals( res );
}
if (!isExpected) {
// System.out.println( "Got unexpected result " + res );
}
}
assertEquals( "Some expected results were not seen", 0, mask );
}
/**
* Bug report from David A Bigwood
*/
public void test_domainInf() {
// create an OntModel
OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF, null );
// populate the model with stuff
String NS = "http://m3t4.com/ont/#";
OntClass c1 = m.createClass( NS + "c1" );
OntClass c2 = m.createClass( NS + "c2" );
OntClass c3 = m.createClass( NS + "c3" );
OntProperty p1 = m.createObjectProperty( NS + "p1" );
// create a union class to contain the union operands
UnionClass uc = m.createUnionClass(null, null);
// add an operand
uc.addOperand( c1 );
assertEquals( "Size should be 1", 1, uc.getOperands().size() );
assertTrue( "uc should have c1 as union member", uc.getOperands().contains( c1 ) );
// add another operand
uc.addOperand( c2 );
assertEquals( "Size should be 2", 2, uc.getOperands().size() );
TestUtil.assertIteratorValues(this, uc.listOperands(), new Object[] { c1, c2 } );
// add a third operand
uc.addOperand( c3 );
assertEquals( "Size should be 3", 3, uc.getOperands().size() );
TestUtil.assertIteratorValues(this, uc.listOperands(), new Object[] { c1, c2, c3} );
// add union class as domain of a property
p1.addDomain(uc);
}
/**
* Bug report on bad conflict resolution between two non-monotonic rules.
*/
public void testNonmonotonicCR() {
String ruleSrc = "(eg:IndA eg:scoreA ?score), sum(?score 40 ?total), noValue(eg:IndA eg:flag_1 'true') -> drop(0), (eg:IndA eg:scoreA ?total), (eg:IndA eg:flag_1 'true')." +
"(eg:IndA eg:scoreA ?score), sum(?score 33 ?total), noValue(eg:IndA eg:flag_2 'true') -> drop(0), (eg:IndA eg:scoreA ?total), (eg:IndA eg:flag_2 'true').";
List rules = Rule.parseRules(ruleSrc);
Model data = ModelFactory.createDefaultModel();
String NS = PrintUtil.egNS;
Resource i = data.createResource(NS + "IndA");
Property scoreA = data.createProperty(NS, "scoreA");
i.addProperty(scoreA, data.createTypedLiteral(100));
GenericRuleReasoner reasoner = new GenericRuleReasoner(rules);
InfModel inf = ModelFactory.createInfModel(reasoner, data);
Iterator values = inf.listObjectsOfProperty(i, scoreA);
TestUtil.assertIteratorValues(this, values, new Object[] { data.createTypedLiteral(173)});
}
/**
* Bug report - intersection processing does not work incrementally.
*/
public void testIncrementalIU() {
OntModel ontmodel = ModelFactory.createOntologyModel(
OntModelSpec.OWL_MEM_MINI_RULE_INF );
String homeuri = "http://abc/bcd/";
Individual ind[] = new Individual[6];
OntClass classb = ontmodel.createClass(homeuri + "C");
for (int i = 0; i < 6; i++){
ind[i] = classb.createIndividual(homeuri + String.valueOf(i));
}
Individual subind[] = new Individual[] {ind[0], ind[1], ind[2]};
EnumeratedClass class1 = ontmodel.createEnumeratedClass(
homeuri+"C1", ontmodel.createList(subind) );
EnumeratedClass class2 = ontmodel.createEnumeratedClass(
homeuri+"C2", ontmodel.createList(ind));
RDFList list = ontmodel.createList(new RDFNode[] { class1, class2 });
IntersectionClass classI = ontmodel.createIntersectionClass(null, list);
UnionClass classU = ontmodel.createUnionClass(null, list);
// Works with rebind, bug is that it doesn't work without rebind
// ontmodel.rebind();
TestUtil.assertIteratorValues(this, classI.listInstances(), subind);
TestUtil.assertIteratorValues(this, classU.listInstances(), ind);
}
/**
* Fact rules with non-empty bodyies failed to fire.
*/
public void testFactRules() {
Model facts = ModelFactory.createDefaultModel();
String NS = PrintUtil.egNS;
Property p = facts.createProperty(NS + "p");
List rules = Rule.parseRules("makeTemp(?x) -> (?x, eg:p, eg:z). " +
"makeTemp(?x) makeTemp(?y) -> (?x, eg:p, ?y) . " +
"(?x, eg:p, eg:z) -> (?a, eg:p, eg:b). " +
"-> [ (eg:a eg:p eg:y) <- ]."
);
GenericRuleReasoner reasoner = new GenericRuleReasoner(rules);
InfModel inf = ModelFactory.createInfModel(reasoner, facts);
inf.prepare();
TestUtil.assertIteratorLength(inf.listStatements(null, p, (RDFNode)null), 4);
}
/**
* Test chainging rules from axioms which broke while trying to
* fix about test case.
*/
public void testFactChainRules() {
Model facts = ModelFactory.createDefaultModel();
String NS = PrintUtil.egNS;
Property mother = facts.createProperty(NS + "mother");
Resource female = facts.createProperty(NS + "Female");
mother.addProperty(RDFS.range, female);
List rules = Rule.parseRules(
"-> tableAll(). \n" +
"[rdfs6: (?p rdfs:subPropertyOf ?q), notEqual(?p,?q) -> [ (?a ?q ?b) <- (?a ?p ?b)] ] \n" +
"-> (eg:range rdfs:subPropertyOf rdfs:range). \n" +
"-> (rdfs:range rdfs:subPropertyOf eg:range). \n" );
GenericRuleReasoner reasoner = new GenericRuleReasoner(rules);
reasoner.setTransitiveClosureCaching(true);
InfModel inf = ModelFactory.createInfModel(reasoner, facts);
Property egRange = inf.createProperty(NS + "range");
TestUtil.assertIteratorValues(this,
inf.listStatements(null, egRange, (RDFNode)null),
new Object[] {inf.createStatement(mother, egRange, female)} );
}
/**
* test remove operator in case with empty data.
*/
public void testEmptyRemove() {
List rules = Rule.parseRules(
"-> (eg:i eg:prop eg:foo) ." +
"(?X eg:prop ?V) -> (?X eg:prop2 ?V) ." +
"(?X eg:prop eg:foo) noValue(?X eg:guard 'done') -> remove(0) (?X eg:guard 'done') ." );
GenericRuleReasoner reasoner = new GenericRuleReasoner(rules);
InfModel im = ModelFactory.createInfModel(reasoner, ModelFactory.createDefaultModel());
Resource i = im.createResource(PrintUtil.egNS + "i");
Property guard = im.createProperty(PrintUtil.egNS + "guard");
TestUtil.assertIteratorValues(this,
im.listStatements(), new Object[] {im.createStatement(i, guard, "done")});
}
/**
* test duplicate removal when using pure backward rules
*/
public void testBackwardDupRemoval() {
String NS = PrintUtil.egNS;
Model base = ModelFactory.createDefaultModel();
Resource i = base.createResource(NS + "i");
Resource a = base.createResource(NS + "a");
Property p = base.createProperty(NS, "p");
Property q = base.createProperty(NS, "q");
Property r = base.createProperty(NS, "r");
base.add(i, p, a);
base.add(i, q, a);
List rules = Rule.parseRules(
"(eg:i eg:r eg:a) <- (eg:i eg:p eg:a). (eg:i eg:r eg:a) <- (eg:i eg:q eg:a).");
GenericRuleReasoner reasoner = new GenericRuleReasoner(rules);
reasoner.setMode(GenericRuleReasoner.BACKWARD);
InfModel im = ModelFactory.createInfModel(reasoner, base);
TestUtil.assertIteratorLength(im.listStatements(i, r, a), 1);
}
/**
* Test closure of grounded choice points
*/
public void testGroundClosure() {
Flag myFlag = new Flag();
BuiltinRegistry.theRegistry.register(myFlag);
String NS = "http://ont.com/";
PrintUtil.registerPrefix("ns", NS);
String rules =
"[r1: (ns:a ns:p ns:b) <- (ns:a ns:p ns:a)] " +
"[r2: (ns:a ns:p ns:b) <- flag()] " +
"[rt: (?a ns:q ?b) <- (?a ns:p ?b)] ";
Model m = ModelFactory.createDefaultModel();
Resource a = m.createResource(NS + "a");
Resource b = m.createResource(NS + "b");
Property p = m.createProperty(NS + "p");
Property q = m.createProperty(NS + "q");
m.add(a, p, a);
GenericRuleReasoner reasoner = new GenericRuleReasoner(Rule.parseRules(rules));
InfModel infModel = ModelFactory.createInfModel(reasoner, m);
assertTrue( infModel.contains(a, q, b) );
assertTrue( ! myFlag.fired );
}
/**
* Test case for a reported CME bug in the transitive reasoner
*/
public void testCMEInTrans() {
OntModel model =
ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_TRANS_INF);
model.read("file:testing/reasoners/bugs/tgcCMEbug.owl");
}
/**
* Builtin which just records whether it has been called.
* Used in implementing testGroundClosure.
*/
private static class Flag extends BaseBuiltin {
public String getName() { return "flag"; }
public boolean fired = false;
public boolean bodyCall(Node[] args, int length, RuleContext context) {
fired = true;
return true;
}
}
// debug assistant
// private void tempList(Model m, Resource s, Property p, RDFNode o) {
// System.out.println("Listing of " + PrintUtil.print(s) + " " + PrintUtil.print(p) + " " + PrintUtil.print(o));
// for (StmtIterator i = m.listStatements(s, p, o); i.hasNext(); ) {
// System.out.println(" - " + i.next());
// }
// }
}
/*
(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 + -