📄 webonttestharness.java
字号:
* and error reporting.
*/
public void runTest(Resource test) {
System.out.println("Running " + test);
boolean success = false;
boolean fail = false;
try {
success = doRunTest(test);
} catch (Exception e) {
fail = true;
System.out.print("\nException: " + e);
e.printStackTrace();
}
testCount++;
if (success) {
System.out.print( (testCount % 40 == 0) ? ".\n" : ".");
System.out.flush();
passCount++;
} else {
System.out.println("\nFAIL: " + test);
}
Resource resultType = null;
if (fail) {
resultType = OWLResults.FailingRun;
} else {
if (test.hasProperty(RDF.type, OWLTest.NegativeEntailmentTest)
|| test.hasProperty(RDF.type, OWLTest.ConsistencyTest)) {
resultType = success ? OWLResults.PassingRun : OWLResults.FailingRun;
} else {
resultType = success ? OWLResults.PassingRun : OWLResults.IncompleteRun;
}
}
// log to the rdf result format
Resource result = testResults.createResource()
.addProperty(RDF.type, OWLResults.TestRun)
.addProperty(RDF.type, resultType)
.addProperty(OWLResults.test, test)
.addProperty(OWLResults.system, jena2);
}
/**
* Run a single test of any sort, return true if the test succeeds.
*/
public boolean doRunTest(Resource test) throws IOException {
if (test.hasProperty(RDF.type, OWLTest.PositiveEntailmentTest)
|| test.hasProperty(RDF.type, OWLTest.NegativeEntailmentTest)
|| test.hasProperty(RDF.type, OWLTest.OWLforOWLTest)
|| test.hasProperty(RDF.type, OWLTest.ImportEntailmentTest)
|| test.hasProperty(RDF.type, OWLTest.TrueTest) ) {
// Entailment tests
boolean processImports = test.hasProperty(RDF.type, OWLTest.ImportEntailmentTest);
Model premises = getDoc(test, RDFTest.premiseDocument, processImports);
Model conclusions = getDoc(test, RDFTest.conclusionDocument);
comprehensionAxioms(premises, conclusions);
long t1 = System.currentTimeMillis();
InfGraph graph = reasoner.bind(premises.getGraph());
if (printProfile) {
((FBRuleInfGraph)graph).resetLPProfile(true);
}
Model result = ModelFactory.createModelForGraph(graph);
boolean correct = testEntailment(conclusions.getGraph(), result.getGraph());
long t2 = System.currentTimeMillis();
lastTestDuration = t2 - t1;
if (printProfile) {
((FBRuleInfGraph)graph).printLPProfile();
}
if (test.hasProperty(RDF.type, OWLTest.NegativeEntailmentTest)) {
correct = !correct;
}
return correct;
} else if (test.hasProperty(RDF.type, OWLTest.InconsistencyTest)) {
// System.out.println("Starting: " + test);
Model input = getDoc(test, RDFTest.inputDocument);
long t1 = System.currentTimeMillis();
InfGraph graph = reasoner.bind(input.getGraph());
boolean correct = ! graph.validate().isValid();
long t2 = System.currentTimeMillis();
lastTestDuration = t2 - t1;
return correct;
} else if (test.hasProperty(RDF.type, OWLTest.ConsistencyTest)) {
// Not used normally becase we are not complete enough to prove consistency
// System.out.println("Starting: " + test);
Model input = getDoc(test, RDFTest.inputDocument);
long t1 = System.currentTimeMillis();
InfGraph graph = reasoner.bind(input.getGraph());
boolean correct = graph.validate().isValid();
long t2 = System.currentTimeMillis();
lastTestDuration = t2 - t1;
return correct;
} else {
for (StmtIterator i = test.listProperties(RDF.type); i.hasNext(); ) {
System.out.println("Test type = " + i.nextStatement().getObject());
}
throw new ReasonerException("Unknown test type");
}
}
/**
* Load the premises or conclusions for the test, optional performing
* import processing.
*/
public Model getDoc(Resource test, Property docType, boolean processImports) throws IOException {
if (processImports) {
Model result = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, null);
StmtIterator si = test.listProperties(docType);
while ( si.hasNext() ) {
String fname = si.nextStatement().getObject().toString() + ".rdf";
loadFile(fname, result);
}
return result;
} else {
return getDoc(test, docType);
}
}
/**
* Load the premises or conclusions for the test.
*/
public Model getDoc(Resource test, Property docType) throws IOException {
Model result = ModelFactory.createDefaultModel();
StmtIterator si = test.listProperties(docType);
while ( si.hasNext() ) {
String fname = si.nextStatement().getObject().toString() + ".rdf";
loadFile(fname, result);
}
return result;
}
/**
* Utility to load a file into a model a Model.
* Files are assumed to be relative to the BASE_URI.
* @param file the file name, relative to baseDir
* @return the loaded Model
*/
public static Model loadFile(String file, Model model) throws IOException {
String langType = "RDF/XML";
if (file.endsWith(".nt")) {
langType = "N-TRIPLE";
} else if (file.endsWith("n3")) {
langType = "N3";
}
String fname = file;
if (fname.startsWith(BASE_URI)) {
fname = fname.substring(BASE_URI.length());
}
Reader reader = new BufferedReader(new FileReader(BASE_TESTDIR + fname));
model.read(reader, BASE_URI + fname, langType);
return model;
}
/**
* Test a conclusions graph against a result graph. This works by
* translating the conclusions graph into a find query which contains one
* variable for each distinct bNode in the conclusions graph.
*/
public boolean testEntailment(Graph conclusions, Graph result) {
QueryHandler qh = result.queryHandler();
Query query = WGReasonerTester.graphToQuery(conclusions);
Iterator i = qh.prepareBindings(query, new Node[] {}).executeBindings();
return i.hasNext();
}
/**
* Example the conclusions graph for introduction of restrictions which
* require a comprehension rewrite and declare new (anon) classes
* for those restrictions.
*/
public void comprehensionAxioms(Model premises, Model conclusions) {
// Comprehend all restriction declarations and note them in a map
Map comprehension = new HashMap();
StmtIterator ri = conclusions.listStatements(null, RDF.type, OWL.Restriction);
while (ri.hasNext()) {
Resource restriction = ri.nextStatement().getSubject();
StmtIterator pi = restriction.listProperties(OWL.onProperty);
while (pi.hasNext()) {
Resource prop = (Resource)pi.nextStatement().getObject();
StmtIterator vi = restriction.listProperties();
while (vi.hasNext()) {
Statement rs = vi.nextStatement();
if ( ! rs.getPredicate().equals(OWL.onProperty)) {
// Have a restriction on(prop) of type rs in the conclusions
// So assert a premise that such a restriction could exisit
Resource comp = premises.createResource()
.addProperty(RDF.type, OWL.Restriction)
.addProperty(OWL.onProperty, prop)
.addProperty(rs.getPredicate(), rs.getObject());
comprehension.put(restriction, comp);
}
}
}
}
// Comprehend any intersectionOf lists. Introduce anon class which has the form
// of the intersection expression.
// Rewrite queries of the form (X intersectionOf Y) to the form
// (X equivalentClass ?CC) (?CC intersectionOf Y)
StmtIterator ii = conclusions.listStatements(null, OWL.intersectionOf, (RDFNode)null);
List intersections = new ArrayList();
while (ii.hasNext()) {
intersections.add(ii.next());
}
for (Iterator i = intersections.iterator(); i.hasNext(); ) {
Statement is = (Statement)i.next();
// Declare in the premises that such an intersection exists
Resource comp = premises.createResource()
.addProperty(RDF.type, OWL.Class)
.addProperty(OWL.intersectionOf, mapList(premises, (Resource)is.getObject(), comprehension));
// Rewrite the conclusions to be a test for equivalence between the class being
// queried and the comprehended interesection
conclusions.remove(is);
conclusions.add(is.getSubject(), OWL.equivalentClass, comp);
}
// Comprehend any oneOf lists
StmtIterator io = conclusions.listStatements(null, OWL.oneOf, (RDFNode)null);
while (io.hasNext()) {
Statement s = io.nextStatement();
Resource comp = premises.createResource()
.addProperty(OWL.oneOf, s.getObject());
}
}
/**
* Helper. Adds to the target model a translation of the given RDF list
* with each element replaced according to the map.
*/
private Resource mapList(Model target, Resource list, Map map) {
if (list.equals(RDF.nil)) {
return RDF.nil;
} else {
Resource head = (Resource) list.getRequiredProperty(RDF.first).getObject();
Resource rest = (Resource) list.getRequiredProperty(RDF.rest).getObject();
Resource mapElt = target.createResource();
Resource mapHead = (Resource) map.get(head);
if (mapHead == null) mapHead = head;
mapElt.addProperty(RDF.first, mapHead);
mapElt.addProperty(RDF.rest, mapList(target, rest, map));
return mapElt;
}
}
// =======================================================================
// Internal helper functions
/** Return a list of all tests of the given type, according to the current filters */
public List findTestsOfType(Resource testType) {
ArrayList result = new ArrayList();
StmtIterator si = testDefinitions.listStatements(null, RDF.type, testType);
while (si.hasNext()) {
Resource test = si.nextStatement().getSubject();
boolean accept = true;
// Check test status
Literal status = (Literal) test.getProperty(RDFTest.status).getObject();
if (approvedOnly) {
accept = status.getString().equals(STATUS_FLAGS[0]);
} else {
accept = false;
for (int i = 0; i < STATUS_FLAGS.length; i++) {
if (status.getString().equals(STATUS_FLAGS[i])) {
accept = true;
break;
}
}
}
// Check for blocked tests
for (int i = 0; i < BLOCKED_TESTS.length; i++) {
if (BLOCKED_TESTS[i].equals(test.toString())) {
accept = false;
}
}
// End of filter tests
if (accept) {
result.add(test);
}
}
return 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 + -