Skip to content

Instantly share code, notes, and snippets.

@ijdickinson
Created July 19, 2012 11:39
Show Gist options
  • Save ijdickinson/3143188 to your computer and use it in GitHub Desktop.
Save ijdickinson/3143188 to your computer and use it in GitHub Desktop.
Workaround for bug in Jena OntTools.namedHierarchyRoots
package usertests;
import java.io.StringReader;
import java.util.Iterator;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
public class LHRTest
{
public static final String SOURCE = "<?xml version=\"1.0\"?>\n" +
"<rdf:RDF\n" +
" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n" +
" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"\n" +
" xmlns:owl=\"http://www.w3.org/2002/07/owl#\" >\n" +
"<owl:Class rdf:about=\"http://dbpedia.org/class/yago/Album106591815\"/>\n" +
"<owl:Class rdf:about=\"http://dbpedia.org/class/yago/HostCitiesOfTheCommonwealthGames\"/>\n" +
"<owl:ObjectProperty rdf:about=\"http://dbpedia.org/property/after\"> \n" +
"<rdfs:domain>\n" +
"<owl:Class>\n" +
"<owl:unionOf rdf:parseType=\"Collection\">\n" +
"<owl:Class rdf:about=\"http://dbpedia.org/class/yago/Album106591815\"/>\n" +
"</owl:unionOf>\n" +
"</owl:Class>\n" +
"</rdfs:domain>\n" +
"</owl:ObjectProperty>\n" +
"</rdf:RDF>";
public static void main( String[] args ) {
OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_MICRO_RULE_INF );
m.read( new StringReader( SOURCE ), "RDF/XML" );
for (ExtendedIterator<OntClass> i = m.listHierarchyRootClasses(); i.hasNext(); ) {
System.out.println( "LHRC -> " + i.next() );
}
OntModel base = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM, m.getBaseModel() );
for (Iterator<OntClass> i = OntTools.namedHierarchyRoots( base ).iterator(); i.hasNext(); ) {
System.out.println( "NHR -> " + i.next() );
}
}
}
@ijdickinson
Copy link
Author

Output:

LHRC -> 3788676f:1389f06e9d6:-7ffc
LHRC -> http://dbpedia.org/class/yago/HostCitiesOfTheCommonwealthGames
NHR -> http://dbpedia.org/class/yago/HostCitiesOfTheCommonwealthGames
NHR -> http://dbpedia.org/class/yago/Album106591815

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment