Class OntModels

java.lang.Object
org.apache.jena.ontapi.utils.OntModels

public class OntModels extends Object
A collection of utilities for working with OWL Model and all related objects: Ontology Object, Ontology Entity, Node List, Ontology Statement.
  • Constructor Details

    • OntModels

      public OntModels()
  • Method Details

    • getOntType

      public static <O extends OntObject> Class<O> getOntType(O object)
      Determines the actual ontology object type.
      Type Parameters:
      O - any subtype of OntObject
      Parameters:
      object - instance of O
      Returns:
      Class-type of O
    • asAnonymousIndividual

      public static OntIndividual.Anonymous asAnonymousIndividual(org.apache.jena.rdf.model.RDFNode inModel)
      Creates an anonymous individual for the given RDF Node, that must be associated with a model. The result anonymous individual could be true (i.e. instance of some owl class) or fake (any blank node can be represented as it).
      Parameters:
      inModel - RDFNode, not null
      Returns:
      OntIndividual.Anonymous
      Throws:
      OntJenaException - if the node cannot be present as anonymous individual
    • listImports

      public static org.apache.jena.util.iterator.ExtendedIterator<OntModel> listImports(OntModel model)
      Lists all imported models from the given one.
      Parameters:
      model - OntModel
      Returns:
      a ExtendedIterator of OntModels
      See Also:
    • listLocalObjects

      public static <O extends OntObject> org.apache.jena.util.iterator.ExtendedIterator<O> listLocalObjects(OntModel model, Class<? extends O> type)
      Lists all ontology objects with the given type that are defined in the base graph. See also listLocalStatements(OntModel, Resource, Property, RDFNode) description.
      Type Parameters:
      O - subclass of OntObject
      Parameters:
      model - OntModel
      type - Class-type
      Returns:
      ExtendedIterator of ontology objects of the type O that are local to the base graph
      See Also:
    • listLocalEntities

      public static org.apache.jena.util.iterator.ExtendedIterator<OntEntity> listLocalEntities(OntModel model)
      Lists all OWL entities that are defined in the base graph. See also listLocalStatements(OntModel, Resource, Property, RDFNode) description.
      Parameters:
      model - OntModel
      Returns:
      ExtendedIterator of OntEntitys that are local to the base graph
      See Also:
    • listMembers

      public static <R extends org.apache.jena.rdf.model.RDFNode> org.apache.jena.util.iterator.ExtendedIterator<R> listMembers(RDFNodeList<R> list)
      Lists all members from Ontology List.
      Type Parameters:
      R - RDFNode, a type of list members
      Parameters:
      list - RDFNodeList
      Returns:
      ExtendedIterator of R
    • listClasses

      public static org.apache.jena.util.iterator.ExtendedIterator<OntClass> listClasses(OntIndividual i)
      Lists all class-types for the given individual.
      Parameters:
      i - an OntIndividual, not null
      Returns:
      an ExtendedIterator over all direct class-types
    • listLocalStatements

      public static org.apache.jena.util.iterator.ExtendedIterator<OntStatement> listLocalStatements(OntModel model, org.apache.jena.rdf.model.Resource s, org.apache.jena.rdf.model.Property p, org.apache.jena.rdf.model.RDFNode o)
      Lists all model statements, which belong to the base graph, using the given SPO.

      It is placed here because there is no certainty that methods for working with ExtendedIterator (like OntGraphModelImpl.listLocalStatements(Resource, Property, RDFNode)) should be placed in the public interfaces: Stream-based analogues are almost the same but more functional. But the ability to work with ExtendedIterator is sometimes needed, since it is more lightweight and works a bit faster than Stream-API.

      Parameters:
      model - OntModel, not null
      s - Resource, can be null for any
      p - Property, can be null for any
      o - RDFNode, can be null for any
      Returns:
      an ExtendedIterator of OntStatements local to the base model graph
      See Also:
    • listAnnotations

      public static org.apache.jena.util.iterator.ExtendedIterator<OntStatement> listAnnotations(OntStatement s)
      Returns an iterator over all direct annotations of the given ontology statement.
      Parameters:
      s - OntStatement
      Returns:
      ExtendedIterator over OntStatements
    • listAnnotations

      public static org.apache.jena.util.iterator.ExtendedIterator<OntStatement> listAnnotations(OntObject o)
      Lists all direct object's annotations.
      Parameters:
      o - OntObject, not null
      Returns:
      ExtendedIterator over OntStatements
    • annotations

      public static Stream<OntStatement> annotations(OntStatement statement)
      Recursively lists all annotations for the given Ontology Statement in the form of a flat stream.
      Parameters:
      statement - OntStatement, not null
      Returns:
      a Stream of OntStatements, each of them is annotation property assertion
      See Also:
    • listAllAnnotations

      public static org.apache.jena.util.iterator.ExtendedIterator<OntStatement> listAllAnnotations(OntStatement statement)
      For the specified Statement lists all its annotation assertions recursively including their sub-annotations.
      Parameters:
      statement - OntStatement, not null
      Returns:
      an ExtendedIterator of OntStatements
      See Also:
    • getAllAnnotations

      public static Set<OntStatement> getAllAnnotations(OntStatement statement)
      For the specified Statement gets all its annotation assertions recursively including their sub-annotations.

      For example, for the following snippet

      
       [ a                      owl:Annotation ;
         rdfs:label             "label2" ;
         owl:annotatedProperty  rdfs:label ;
         owl:annotatedSource    [ a                      owl:Axiom ;
                                  rdfs:label             "label1" ;
                                  owl:annotatedProperty  rdfs:comment ;
                                  owl:annotatedSource    [ a             owl:Ontology ;
                                                           rdfs:comment  "comment"
                                                         ] ;
                                  owl:annotatedTarget    "comment"
                                ] ;
         owl:annotatedTarget    "label1"
       ] .
       
      there would be three annotations: _:b0 rdfs:comment "comment", _:b1 rdfs:label "label1", _:b2 rdfs:label "label2".
      Parameters:
      statement - OntStatement, not null
      Returns:
      an Set of OntStatements
    • listSplitStatements

      public static org.apache.jena.util.iterator.ExtendedIterator<OntStatement> listSplitStatements(OntStatement statement)
      Returns an ExtendedIterator over all Ontology Statements, which are obtained from splitting the given statement into several equivalent ones but with disjoint annotations. Each of the returned statements is equal to the given, the difference is only in the related annotations.

      This method can be used in case there are several typed b-nodes for each annotation assertions instead of a single one. Such situation is not a canonical way and should not be widely used, since it is redundant. So usually the result stream contains only a single element: the same OntStatement instance as the input.

      The following code demonstrates that non-canonical way of writing annotations with two or more b-nodes:

      
       s A t .
       _:b0  a                     owl:Axiom .
       _:b0  A1                    t1 .
       _:b0  owl:annotatedSource   s .
       _:b0  owl:annotatedProperty A .
       _:b0  owl:annotatedTarget   t .
       _:b1  a                     owl:Axiom .
       _:b1  A2                    t2 .
       _:b1  owl:annotatedSource   s .
       _:b1  owl:annotatedProperty A .
       _:b1  owl:annotatedTarget   t .
       
      Here the statement s A t has two annotations, but they are spread over different resources (statements _:b0 A1 t1 and _:b1 A2 t2). For this example, the method returns stream of two OntStatements, and each of them has only one annotation. For generality, below is an example of the correct and equivalent way to write these annotations, which is the preferred since it is more compact:
      
       s A t .
       [ a                      owl:Axiom ;
       A1                     t1 ;
       A2                     t2 ;
       owl:annotatedProperty  A ;
       owl:annotatedSource    s ;
       owl:annotatedTarget    t
       ]  .
       
      Parameters:
      statement - OntStatement, not null
      Returns:
      ExtendedIterator of OntStatements
    • toOntStatement

      public static OntStatement toOntStatement(org.apache.jena.graph.Triple triple, OntModel model)
      Answers an Ontology Statement in the specified model that wraps the given triple. This method differs from the method OntModel.asStatement(Triple) in that it provides main statement if it is possible.
      Parameters:
      triple - SPO, not null
      model - OntModel, not null
      Returns:
      OntStatement
      See Also:
    • config

      public static OntConfig config(OntModel m)
      Gets model's config.
      Parameters:
      m - OntModel
      Returns:
      OntConfig or null if model is not OntEnhGraph
    • namedHierarchyRoots

      public static Stream<OntClass.Named> namedHierarchyRoots(OntModel m)
      Answers a stream of the named hierarchy roots of a given OntModel. This will be similar to the results of OntModel.hierarchyRoot(), with the added constraint that every member of the returned stream will be a named class, not an anonymous class expression. The named root classes are calculated from the root classes by recursively replacing every anonymous class with its direct subclasses. Thus, it can be seen that the values in the stream consist of the shallowest fringe of named classes in the hierarchy.
      Parameters:
      m - OntModel
      Returns:
      a Stream of OntClass.Named
      See Also:
    • getLCA

      public static OntClass getLCA(OntClass u, OntClass v)
      Answers the lowest common ancestor of two classes. This is the class that is farthest from the root concept (defaulting to owl:Thing which is a superclass of both u and v). The algorithm is based on Tarjan's off-line LCA. The current implementation expects that the given model:

      • is transitively closed over the subClassOf relation
      • can cheaply determine direct sub-class relations

      Both of these conditions are true of the built-in Jena OWL reasoners, such as OntSpecification.OWL2_FULL_MEM_MICRO_RULES_INF, and external DL reasoners such as Pellet.

      Parameters:
      u - OntClass
      v - OntClass
      Returns:
      the LCA of u and v
      Throws:
      org.apache.jena.shared.JenaException - if the language profile of the given model does not define a top concept owl:Thing
    • getLCA

      public static OntClass getLCA(OntClass root, OntClass u, OntClass v)
      Answers the lowest common ancestor of two classes, assuming that the given class is the root concept to start searching from. See getLCA(OntClass, OntClass) for details.
      Parameters:
      root - OntClass, the root concept, which will be the starting point for the algorithm
      u - OntClass, an ontology class
      v - OntClass, an ontology class
      Returns:
      the LCA of u and v