Skip to content

owlcs/ont-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ONT-API (ver. 3.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 11+

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 com.github.sszuev.jena.ontapi.OntSpecification;
import com.github.sszuev.jena.ontapi.model.OntModel;
import com.github.sszuev.jena.ontapi.vocabulary.OWL;
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.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLDataFactory;

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

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

        // 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 axioms (finds axioms from base graph, inferred by Jena Reasoner are not included)
        owlapi.axioms().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());
            }
        }
    }
}