📄 testdigreasoner.java
字号:
// list the inconsistent classes
StmtIterator i = m.listStatements( null, OWL.equivalentClass, OWL.Nothing );
int unsatCount = 0;
while (i.hasNext()) {
/*Resource s = */i.nextStatement().getSubject();
//System.out.println( "Class " + s + " is unsatisfiable" );
unsatCount++;
}
assertEquals( "Should be 4 unsatisfiable classes", 5, unsatCount );
}
/* Bug report by Michele Orlando - duplicate results from rdql query */
public void test_bug_mo_1() {
String NS = "http://example.org/foo#";
DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
spec.setReasoner( r );
OntModel m = ModelFactory.createOntologyModel( spec, null );
OntClass a = m.createClass( NS + "A" );
Individual i0 = m.createIndividual( NS + "i0", a );
int iCount = 0;
int nonICount = 0;
for (StmtIterator j = m.listStatements( null, RDF.type, m.getResource(NS+"A")); j.hasNext(); ) {
if (j.nextStatement().getSubject().equals( i0 )) {
iCount++;
}
else {
nonICount++;
}
}
assertEquals( "Should be no non i0's", 0, nonICount );
assertEquals( "Should be only one i0", 1, iCount );
}
/* Bug report from Michele Orlando - failure on getLabel */
public void test_bug_mo_2() {
boolean ex0 = false;
boolean ex1 = false;
String NS = "http://example.org/foo#";
DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
spec.setReasoner( r );
OntModel model = ModelFactory.createOntologyModel( spec, null );
OntClass a = model.createClass( NS + "A" );
Individual b = model.createIndividual( NS + "b", a );
Model base = model.getBaseModel();
Literal lbl = base.createLiteral( "fubar", "en" );
base.add( b, RDFS.label, lbl );
// ensure we have the model we want
//model.write( System.out, "N3" );
for ( Iterator instances = model.listIndividuals(); instances.hasNext(); ) {
Individual inst = (Individual) instances.next();
try {
//System.out.println( "Label try #1: " + " "+inst.getLabel("en"));
inst.getLabel("en");
}
catch (Exception e) {
//System.out.println( "First attempt failed: " + e.getMessage() );
ex0 = true;
}
try {
Resource baseInst = (Resource) inst.inModel( base );
/*String label =*/ baseInst.getProperty( RDFS.label ).getString();
//System.out.println("Label try #2: " + label );
}
catch (Exception e) {
System.out.println( "Second attempt failed " + e.getMessage() );
ex1 = true;
}
}
// Junit
assertTrue( !(ex0 || ex1) );
}
public void test_bug_mo_3() {
DIGReasoner r = (DIGReasoner) ReasonerRegistry.theRegistry().create( DIGReasonerFactory.URI, null );
OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_DL_MEM );
spec.setReasoner( r );
OntModel model = ModelFactory.createOntologyModel( spec );
OntClass c0 = model.createClass( "ns:A" );
c0.addLabel( "this is just a test", "en" );
Model base = model.getBaseModel();
OntClass cls = model.getOntClass("ns:A");
System.out.println(cls.getLocalName());
for ( Iterator instances = cls.listInstances(); instances.hasNext(); ) {
Resource inst = (Resource) instances.next();
// System.out.println(" "+inst.getLabel("it"));
Property p = base.getProperty("http://www.w3.org/2000/01/rdf-schema#","label");
Statement s = inst.getProperty(p);
System.out.println(" "+s.getString());
}
}
// for debuging the basic query tests one at a time
public void xxtestBasicQueryN()
throws Exception
{
String root = "testing/ontology/dig/owl/basicq";
int testN = 24;
File testSource = new File( root + "/test_" + testN + ".source.xml" );
File testQuery = new File( root + "/test_" + testN + ".query.xml" );
File testTarget = new File( root + "/test_" + testN + ".result.xml" );
new DigBasicQueryTest( testSource, testTarget, testQuery, OntModelSpec.OWL_MEM ).runTest();
}
// Internal implementation methods
//////////////////////////////////
private static void buildConceptLangSuite( String root, OntModelSpec spec, TestSuite s ) {
int i = 0;
while (true) {
File testSource = new File( root + "/test_" + i + ".source.xml" );
File testTarget = new File( root + "/test_" + i + ".xml" );
if (!testSource.exists()) {
break;
}
else {
i++;
}
s.addTest( new DigTranslationTest( testSource, testTarget, spec ) );
}
}
private static void buildBasicQuerySuite( String root, OntModelSpec spec, TestSuite s ) {
int i = 0;
while (true) {
File testSource = new File( root + "/test_" + i + ".source.xml" );
File testQuery = new File( root + "/test_" + i + ".query.xml" );
File testTarget = new File( root + "/test_" + i + ".result.xml" );
if (!testSource.exists()) {
break;
}
else {
i++;
}
s.addTest( new DigBasicQueryTest( testSource, testTarget, testQuery, spec ) );
}
}
//==============================================================================
// Inner class definitions
//==============================================================================
private static class AbstractDigTest
extends TestCase
{
private boolean debug = true;
public AbstractDigTest( String name ) {
super( name );
}
/** This is a simple test that test xml structure isomorphism on elements and attributes */
protected void xmlEqualityTest( Document source, Document target ) {
// test both ways round to ensure compatability
boolean test = xmlEqualityTest( source.getDocumentElement(), target.getDocumentElement() );
if (debug && !test) {
PrintWriter out = new PrintWriter( System.err );
out.println( getName() + " expected:" );
new DIGConnection().serialiseDocument( target, out );
out.println();
out.println( "Saw:" );
new DIGConnection().serialiseDocument( source, out );
out.println();
}
assertTrue( "Failed to match source to target documents", test );
test = xmlEqualityTest( target.getDocumentElement(), source.getDocumentElement() );
if (debug && !test) {
PrintWriter out = new PrintWriter( System.err );
out.println( getName() + " expected:" );
new DIGConnection().serialiseDocument( source, out );
out.println();
out.println( "Saw:" );
new DIGConnection().serialiseDocument( target, out );
out.println();
}
assertTrue( "Failed to match target to source documents", test );
}
private boolean xmlEqualityTest( Element source, Element target ) {
boolean match = source.getNodeName().equals( target.getNodeName() );
NodeList children = source.getChildNodes();
for (int i = 0; match && i < children.getLength(); i++) {
Node child = children.item( i );
// we're only looking at structural equivalence - elements and attributes
if (child instanceof Element) {
match = findElementMatch( (Element) child, target );
}
}
NamedNodeMap attrs = source.getAttributes();
for (int i = 0; match && i < attrs.getLength(); i++) {
match = findAttributeMatch( (Attr) attrs.item( i ), target );
}
return match;
}
private boolean findElementMatch( Element sourceChild, Element target ) {
boolean found = false;
NodeList targetChildren = target.getElementsByTagName( sourceChild.getNodeName() );
for (int i = 0; !found && i < targetChildren.getLength(); i++) {
Node targetChild = targetChildren.item( i );
if (targetChild instanceof Element && sourceChild.getNodeName().equals( targetChild.getNodeName() )) {
// we have found an element with the same name - see if it matches
found = xmlEqualityTest( sourceChild, (Element) targetChild );
}
}
return found;
}
private boolean findAttributeMatch( Attr child, Element target ) {
String chValue = child.getValue();
String targetValue = target.getAttribute( child.getName() );
return (chValue.startsWith( DIGAdapter.ANON_MARKER ) && targetValue.startsWith( DIGAdapter.ANON_MARKER)) ||
chValue.equals( targetValue );
}
}
private static class DigTranslationTest
extends AbstractDigTest
{
private File m_source;
private File m_target;
private OntModelSpec m_spec;
DigTranslationTest( File source, File target, OntModelSpec spec ) {
super( "DigTranslationTest " + source.getName() );
m_source = source;
m_target = target;
m_spec = spec;
}
public void runTest()
throws Exception
{
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Model m = ModelFactory.createDefaultModel();
m.read( new FileInputStream( m_source ), null );
DIGAdapter da = new DIGAdapter( m_spec, m.getGraph() );
Document targetD = builder.parse( m_target );
Document sourceD = da.translateKbToDig();
// debug da.serialiseDocument( sourceD, new PrintWriter( System.out ));
xmlEqualityTest( sourceD, targetD );
}
}
private static class DigBasicQueryTest
extends AbstractDigTest
{
private File m_source;
private File m_target;
private File m_query;
private OntModelSpec m_spec;
DigBasicQueryTest( File source, File target, File query, OntModelSpec spec ) {
super( "BasicQueryTest " + source.getName() );
m_source = source;
m_target = target;
m_query = query;
m_spec = spec;
}
public void runTest()
throws Exception
{
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Model m = ModelFactory.createDefaultModel();
m.read( new FileInputStream( m_source ), null );
DIGAdapter da = new DIGAdapter( m_spec, m.getGraph() );
// upload
da.resetKB();
boolean warn = !da.uploadKB();
if (warn) {
System.err.println( "00 Warning!" );
for (Iterator i = da.getConnection().getWarnings(); i.hasNext(); ) {
System.err.println( i.next() );
}
assertFalse( "Should not be upload warnings", warn );
}
Document queryD = builder.parse( m_query );
Document targetD = builder.parse( m_target );
log.debug( "DIG test " + m_source.getPath() );
Document resultD = da.getConnection().sendDigVerb( queryD, da.getProfile() );
da.getConnection().errorCheck( resultD, da.getProfile() );
assertFalse( "Should not be warnings", da.getConnection().warningCheck( resultD ) );
da.close();
xmlEqualityTest( resultD, targetD );
}
}
}
/*
* (c) Copyright 2001, 2002, 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 + -