Skip to content

owlcs/ont-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ONT-API (ver. 4.x.x)

Maven Central Javadoc JetBrains open source

Summary

ONT-API is an RDF-centric Java library to work with OWL2.

For more info about the library see the project wiki.

Dependencies

Requirements

  • Java 17+

License

  • Apache License Version 2.0
  • GNU LGPL Version 3.0

Example

import com.github.owlcs.ontapi.OntManagers;
import com.github.owlcs.ontapi.Ontology;
import com.github.owlcs.ontapi.OntologyManager;
import org.apache.jena.ontapi.OntSpecification;
import org.apache.jena.ontapi.model.OntModel;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.ResultSet;
import org.apache.jena.vocabulary.OWL;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.parameters.Imports;

public class Examples {
    public static void main(String... args) {
        String iri = "https://github.com/owlcs/ont-api";

        OWLDataFactory df = OntManagers.getDataFactory();
        OntologyManager om = OntManagers.createDirectManager();

        // set ontology specification, see jena-owl2 for more details
        om.getOntologyConfigurator().setSpecification(OntSpecification.OWL2_EL_MEM_RULES_INF);

        // create OWLAPI impl
        Ontology owlapi = om.createOntology(IRI.create(iri));
        // add OWL class declaration
        owlapi.addAxiom(df.getOWLDeclarationAxiom(df.getOWLClass(iri + "#Class1")));

        // view as Jena impl
        OntModel jena = owlapi.asGraphModel();
        // add OWL class declaration
        jena.createResource(iri + "#Class2", OWL.Class);

        // lists all axioms inferred by Jena Reasoner
        owlapi.axioms(Imports.INCLUDED).forEach(System.out::println);

        // and print to stdout in turtle format
        jena.write(System.out, "ttl");

        // play with InfModel representation
        System.out.println(jena.asInferenceModel().validate().isValid());

        // SPARQL (finds all statements including inferred ones)
        try (QueryExecution exec = QueryExecutionFactory.create(
                QueryFactory.create(
                        "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                                "SELECT ?s ?o WHERE { ?s a ?o }"
                ), jena)) {
            ResultSet res = exec.execSelect();
            while (res.hasNext()) {
                System.out.println(res.next());
            }
        }
    }
}