Recently, I'm trying to approach semantic web using Jena to create RDF and make query. Now, I have successfully made a owl based RDF file in Jena. However, when I trying to use different ontologies (such as: cidoc-crm), I do not know how to import those ontologies to Jena. Does anyone know how to import them to Jena? Do I need to create a new ontology model?
问题:
回答1:
Jena's OntModel interface provides a convenient way of working with ontologies (including RDFS ontologies). The following code shows how you can get an OntModel that contains the data of the ontology. In this code, I then create another OntModel with inference support that includes the CIDOC model that I created. With the inferencing model, it is easy to create individuals and see computed types for them, or see subclass relationships derived by the inference model.
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
public class CIDOCExample {
final static String CIDOCNS = "http://www.cidoc-crm.org/cidoc-crm/";
public static void main( String[] args ) {
// Load the CIDOC-CRM into an OntModel. We won't add any reasoning capability to this model, but
// we'll use it as a submodel of OntModels that do. Jena can pull the document from the web, but
// you could also download a local copy and read that. It would certainly a bit quicker than
// downloading it every time.
final OntModel cidocModel = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM );
cidocModel.read( "http://www.cidoc-crm.org/rdfs/cidoc_crm_v5.1-draft-2013May.rdfs" );
// Create an OntModel that imports the cidocModel, and give it inference support.
final OntModel model = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM_RDFS_INF );
model.addSubModel( cidocModel );
// Retrieve a class from the OntModel and shows its subclasses.
final OntClass e5_event = model.getOntClass( CIDOCNS+"E5_Event" );
System.out.println( "Subclasses of E5_Event:" );
for ( final ExtendedIterator<OntClass> it = e5_event.listSubClasses(); it.hasNext() ;) {
System.out.println( "\t* "+it.next() );
}
// Create your own instance data in an OntModel that imports the cidocModel
final Individual someJoining = model.createIndividual( "http://example.org/someJoining", cidocModel.getOntClass( CIDOCNS+"E85_Joining"));
System.out.println( "Types of "+someJoining );
for ( final ExtendedIterator<Resource> types = someJoining.listRDFTypes(false); types.hasNext(); ) {
System.out.println( "\t* "+types.next() );
}
}
}
Having to obtain the classes using OntModel.getOntClass(CIDCOCNS+"...")
is a bit clumsy, and it's very easy to make a typo. Jena provides an excellent schemagen tool that takes an ontology and generates a class of constants that represent the classes, properties, and individuals declared in the ontology. For instance, you could use schemagen
to create a CIDOC
class with, e.g., a constant OntClass
object for E5_Event
so that instead of
final OntClass e5_event = model.getOntClass( CIDOCNS+"E5_Event" );
System.out.println( "Subclasses of E5_Event:" );
for ( final ExtendedIterator<OntClass> it = e5_event.listSubClasses(); it.hasNext() ;) {
System.out.println( "\t* "+it.next() );
}
you could do
System.out.println( "Subclasses of E5_Event:" );
for ( final ExtendedIterator<OntClass> it = CIDOC.E5_Event.inModel( model ).listSubClasses(); it.hasNext() ;) {
System.out.println( "\t* "+it.next() );
}