Skip to content

Instantly share code, notes, and snippets.

@ijdickinson
Created April 16, 2013 13:12
Show Gist options
  • Save ijdickinson/5395756 to your computer and use it in GitHub Desktop.
Save ijdickinson/5395756 to your computer and use it in GitHub Desktop.
Simple example of class assertions in OWL using Jena
package examples;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.*;
public class EmployeeExample
{
public static final String ONT1 = "http://example.com/ontologies/ont1#";
public static final String ONT2 = "http://example.com/ontologies/ont2#";
private OntClass ont1Employee;
private OntClass ont2Employee;
private Individual john;
public static void main( String[] args ) {
new EmployeeExample().run();
}
public void run() {
System.out.println( "Baseline model - no inference" );
OntModel m = resetBaseModel( false );
printDescription( john );
System.out.println( "\nModel with inference" );
m = resetBaseModel( true );
printDescription( john );
System.out.println( "\nont1:Employee rdfs:subClassOf ont2:Employee" );
m = resetBaseModel( true );
ont1Employee.addSuperClass( ont2Employee );
printDescription( john );
System.out.println( "\nont2:Employee rdfs:subClassOf ont1:Employee" );
m = resetBaseModel( true );
ont1Employee.addSubClass( ont2Employee );
printDescription( john );
System.out.println( "\nont2:Employee owl:sameAs ont1:Employee" );
m = resetBaseModel( true );
ont1Employee.addEquivalentClass( ont2Employee );
printDescription( john );
}
private OntModel resetBaseModel( boolean withInference ) {
OntModelSpec spec = withInference ? OntModelSpec.OWL_MEM_MICRO_RULE_INF : OntModelSpec.OWL_MEM;
OntModel m = ModelFactory.createOntologyModel( spec );
m.setNsPrefix( "ont1", ONT1 );
m.setNsPrefix( "ont2", ONT2 );
ont1Employee = m.createClass( ONT1 + "Employee" );
ont2Employee = m.createClass( ONT2 + "Employee" );
john = m.createIndividual( ONT1 + "john", ont1Employee );
return m;
}
private void printDescription( Resource r ) {
System.out.println( r.getURI() + " has properties:" );
for (StmtIterator i = r.listProperties(); i.hasNext(); ) {
Statement s = i.next();
System.out.println( " " + r.getModel().shortForm( s.getPredicate().getURI() ) +
" = " + r.getModel().shortForm( s.getObject().toString() ));
}
}
}
Baseline model - no inference
http://example.com/ontologies/ont1#john has properties:
rdf:type = ont1:Employee
Model with inference
http://example.com/ontologies/ont1#john has properties:
rdf:type = ont1:Employee
rdf:type = owl:Thing
ont1:Employee rdfs:subClassOf ont2:Employee
http://example.com/ontologies/ont1#john has properties:
rdf:type = ont1:Employee
rdf:type = ont2:Employee
rdf:type = owl:Thing
ont2:Employee rdfs:subClassOf ont1:Employee
http://example.com/ontologies/ont1#john has properties:
rdf:type = ont1:Employee
rdf:type = owl:Thing
ont2:Employee owl:sameAs ont1:Employee
http://example.com/ontologies/ont1#john has properties:
rdf:type = ont1:Employee
rdf:type = ont2:Employee
rdf:type = owl:Thing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment