Class SimpleReasoner

  • All Implemented Interfaces:
    ChangeListener, org.apache.jena.rdf.model.ModelChangedListener

    public class SimpleReasoner
    extends org.apache.jena.rdf.listeners.StatementListener
    implements org.apache.jena.rdf.model.ModelChangedListener, ChangeListener
    Allows for real-time incremental materialization or retraction of RDFS- style class and property subsumption based ABox inferences as statements are added to or removed from the (ABox or TBox) knowledge base.
    Author:
    sjm222
    • Constructor Summary

      Constructors 
      Constructor Description
      SimpleReasoner​(org.apache.jena.ontology.OntModel tboxModel, RDFService rdfService, org.apache.jena.rdf.model.Model inferenceModel, org.apache.jena.rdf.model.Model inferenceRebuildModel, org.apache.jena.rdf.model.Model scratchpadModel, SearchIndexer searchIndexer)  
      SimpleReasoner​(org.apache.jena.ontology.OntModel tboxModel, org.apache.jena.ontology.OntModel aboxModel, org.apache.jena.rdf.model.Model inferenceModel)
      This constructor is used for the unit tests only
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void addedInverseProperty​(org.apache.jena.ontology.OntProperty prop1, org.apache.jena.ontology.OntProperty prop2, org.apache.jena.rdf.model.Model inferenceModel)
      If it is added that P is an inverse of Q, then: 1.
      void addedStatement​(org.apache.jena.rdf.model.Statement stmt)  
      protected void addedSubClass​(org.apache.jena.ontology.OntClass subClass, org.apache.jena.ontology.OntClass superClass, org.apache.jena.rdf.model.Model inferenceModel)
      If it is added that B is a subClass of A, then for each individual that is typed as B, either in the ABox or in the inferred model, infer that it is of type A.
      void addedTBoxStatement​(org.apache.jena.rdf.model.Statement stmt)
      Performs incremental ABox reasoning based on changes to the class hierarchy.
      void addInference​(org.apache.jena.rdf.model.Statement infStmt, org.apache.jena.rdf.model.Model inferenceModel)
      Add an inference from the inference model Adds the inference to the inference model if it is not already in the inference model and not in the abox model.
      protected void addInference​(org.apache.jena.rdf.model.Statement infStmt, org.apache.jena.rdf.model.Model inferenceModel, boolean handleSameAs)  
      protected void addInference​(org.apache.jena.rdf.model.Statement infStmt, org.apache.jena.rdf.model.Model inferenceModel, boolean handleSameAs, boolean checkRedundancy)  
      protected void changedTBoxStatement​(org.apache.jena.rdf.model.Statement stmt, boolean add)
      Performs incremental ABox reasoning based on changes to the class hierarchy.
      protected void doPlugins​(ModelUpdate.Operation op, org.apache.jena.rdf.model.Statement stmt)  
      protected boolean entailedStatement​(org.apache.jena.rdf.model.Statement stmt)
      Returns true if the triple is entailed by inverse property reasoning or sameAs reasoning; otherwise returns false.
      protected boolean entailedType​(org.apache.jena.rdf.model.Resource subject, org.apache.jena.rdf.model.Resource cls)
      Returns true if it is entailed by class subsumption that subject is of type cls; otherwise returns false.
      protected boolean entailedType​(org.apache.jena.rdf.model.Resource subject, org.apache.jena.rdf.model.Resource cls, List<String> remainingTypeURIs)
      Returns true if it is entailed by class subsumption that subject is of type cls; otherwise returns false.
      protected org.apache.jena.rdf.model.Model generateInverseInferences​(org.apache.jena.ontology.OntProperty prop, org.apache.jena.ontology.OntProperty inverseProp)  
      protected List<org.apache.jena.ontology.OntProperty> getInverseProperties​(org.apache.jena.rdf.model.Statement stmt)
      Returns a list of properties that are inverses of the property in the given statement.
      protected List<org.apache.jena.rdf.model.Resource> getParents​(org.apache.jena.rdf.model.Resource cls, org.apache.jena.ontology.OntModel tboxModel)  
      List<ReasonerPlugin> getPluginList()  
      protected List<String> getRemainingAssertedTypeURIs​(org.apache.jena.rdf.model.Resource resource)  
      boolean getSameAsEnabled()  
      protected List<org.apache.jena.rdf.model.Resource> getSameIndividuals​(org.apache.jena.rdf.model.Resource ind, org.apache.jena.rdf.model.Model inferenceModel)
      Get a list of individuals the same as the given individual
      protected List<org.apache.jena.rdf.model.Resource> getSubClasses​(org.apache.jena.rdf.model.Resource cls)  
      protected List<org.apache.jena.rdf.model.Resource> getSuperClasses​(org.apache.jena.rdf.model.Resource cls)  
      boolean isABoxReasoningAsynchronous()
      Asynchronous reasoning mode (DeltaComputer) no longer used in the case of batch removals.
      protected boolean isInterestedInRemovedStatement​(org.apache.jena.rdf.model.Statement stmt)  
      boolean isRecomputing()
      Returns true if the reasoner is in the process of recomputing all inferences.
      void notifyEvent​(String string, Object event)
      Override this to listen to events pertaining to the given graphURI.
      void notifyEvent​(org.apache.jena.rdf.model.Model model, Object event)  
      void notifyModelChange​(ModelChange modelChange)
      Override this to listen to each model change
      void recompute()  
      protected void removedInverseProperty​(org.apache.jena.ontology.OntProperty prop1, org.apache.jena.ontology.OntProperty prop2, org.apache.jena.rdf.model.Model inferenceModel)
      If it is removed that P is an inverse of Q, then: 1.
      void removedStatement​(org.apache.jena.rdf.model.Statement stmt)  
      protected void removedSubClass​(org.apache.jena.ontology.OntClass subClass, org.apache.jena.ontology.OntClass superClass, org.apache.jena.rdf.model.Model inferenceModel)
      If removed that B is a subclass of A, then for each individual that is of type B, either inferred or in the ABox, remove the assertion that it is of type A from the inferred model, UNLESS the individual is of some type C that is a subClass of A (including A itself)
      void removedTBoxStatement​(org.apache.jena.rdf.model.Statement stmt)
      Performs incremental ABox reasoning based on changes to the class hierarchy.
      void removeInference​(org.apache.jena.rdf.model.Statement infStmt, org.apache.jena.rdf.model.Model inferenceModel)
      Remove an inference from the inference model Removes the inference if it is not entailed by the abox model and if the inference model contains it.
      protected void removeInference​(org.apache.jena.rdf.model.Statement infStmt, org.apache.jena.rdf.model.Model inferenceModel, boolean handleSameAs, boolean checkEntailment)  
      protected void setMostSpecificTypes​(org.apache.jena.rdf.model.Resource individual, HashSet<String> typeURIs, org.apache.jena.rdf.model.Model inferenceModel)  
      protected void setMostSpecificTypes​(org.apache.jena.rdf.model.Resource individual, org.apache.jena.rdf.model.Model inferenceModel, HashSet<String> unknownTypes)
      Find the most specific types (classes) of an individual and infer them for the individual with the mostSpecificType annotation.
      void setPluginList​(List<ReasonerPlugin> pluginList)  
      void setSameAsEnabled​(boolean tf)  
      void setStopRequested()
      This is called when the application shuts down.
      static String stmtString​(org.apache.jena.rdf.model.Statement statement)
      Utility method for logging
      • Methods inherited from class org.apache.jena.rdf.listeners.StatementListener

        addedStatements, addedStatements, addedStatements, addedStatements, removedStatements, removedStatements, removedStatements, removedStatements
      • Methods inherited from interface org.apache.jena.rdf.model.ModelChangedListener

        addedStatements, addedStatements, addedStatements, addedStatements, removedStatements, removedStatements, removedStatements, removedStatements
    • Constructor Detail

      • SimpleReasoner

        public SimpleReasoner​(org.apache.jena.ontology.OntModel tboxModel,
                              RDFService rdfService,
                              org.apache.jena.rdf.model.Model inferenceModel,
                              org.apache.jena.rdf.model.Model inferenceRebuildModel,
                              org.apache.jena.rdf.model.Model scratchpadModel,
                              SearchIndexer searchIndexer)
        Parameters:
        tboxModel - - input. This model contains both asserted and inferred TBox axioms
        rdfService - - input. An RDF Service
        inferenceModel - - output. This is the model in which inferred (materialized) ABox statements are maintained (added or retracted).
        inferenceRebuildModel - - output. This the model is temporarily used when the whole ABox inference model is rebuilt
        scratchpadModel - - output. This the model is temporarily used when the whole ABox inference model is rebuilt
        searchIndexer - - output. If not null, the indexer will be paused before the ABox inference model is rebuilt and unpaused when the rebuild is complete.
      • SimpleReasoner

        public SimpleReasoner​(org.apache.jena.ontology.OntModel tboxModel,
                              org.apache.jena.ontology.OntModel aboxModel,
                              org.apache.jena.rdf.model.Model inferenceModel)
        This constructor is used for the unit tests only
        Parameters:
        tboxModel - - input. This model contains both asserted and inferred TBox axioms
        aboxModel - - input. This model contains asserted ABox statements
        inferenceModel - - output. This is the model in which inferred (materialized) ABox statements are maintained (added or retracted).
    • Method Detail

      • setSameAsEnabled

        public void setSameAsEnabled​(boolean tf)
      • getSameAsEnabled

        public boolean getSameAsEnabled()
      • notifyModelChange

        public void notifyModelChange​(ModelChange modelChange)
        Description copied from interface: ChangeListener
        Override this to listen to each model change
        Specified by:
        notifyModelChange in interface ChangeListener
        Parameters:
        modelChange - - the object representing the model change
      • addedStatement

        public void addedStatement​(org.apache.jena.rdf.model.Statement stmt)
        Specified by:
        addedStatement in interface org.apache.jena.rdf.model.ModelChangedListener
        Overrides:
        addedStatement in class org.apache.jena.rdf.listeners.StatementListener
      • removedStatement

        public void removedStatement​(org.apache.jena.rdf.model.Statement stmt)
        Specified by:
        removedStatement in interface org.apache.jena.rdf.model.ModelChangedListener
        Overrides:
        removedStatement in class org.apache.jena.rdf.listeners.StatementListener
      • changedTBoxStatement

        protected void changedTBoxStatement​(org.apache.jena.rdf.model.Statement stmt,
                                            boolean add)
        Performs incremental ABox reasoning based on changes to the class hierarchy. addedTBoxStatement and removedTBoxStatement use the same tests so the are merged into this method. Handles rdfs:subclassOf, owl:equivalentClass, and owl:inverseOf
      • addedTBoxStatement

        public void addedTBoxStatement​(org.apache.jena.rdf.model.Statement stmt)
        Performs incremental ABox reasoning based on changes to the class hierarchy. Handles rdfs:subclassOf, owl:equivalentClass, and owl:inverseOf
      • removedTBoxStatement

        public void removedTBoxStatement​(org.apache.jena.rdf.model.Statement stmt)
        Performs incremental ABox reasoning based on changes to the class hierarchy. Handles rdfs:subclassOf, owl:equivalentClass, and owl:inverseOf
      • addedSubClass

        protected void addedSubClass​(org.apache.jena.ontology.OntClass subClass,
                                     org.apache.jena.ontology.OntClass superClass,
                                     org.apache.jena.rdf.model.Model inferenceModel)
        If it is added that B is a subClass of A, then for each individual that is typed as B, either in the ABox or in the inferred model, infer that it is of type A.
      • removedSubClass

        protected void removedSubClass​(org.apache.jena.ontology.OntClass subClass,
                                       org.apache.jena.ontology.OntClass superClass,
                                       org.apache.jena.rdf.model.Model inferenceModel)
        If removed that B is a subclass of A, then for each individual that is of type B, either inferred or in the ABox, remove the assertion that it is of type A from the inferred model, UNLESS the individual is of some type C that is a subClass of A (including A itself)
      • addedInverseProperty

        protected void addedInverseProperty​(org.apache.jena.ontology.OntProperty prop1,
                                            org.apache.jena.ontology.OntProperty prop2,
                                            org.apache.jena.rdf.model.Model inferenceModel)
        If it is added that P is an inverse of Q, then: 1. For each statement involving predicate P in the assertions model add the inverse statement to the inference model if that inverse is in the assertions model. 2. Repeat the same for predicate Q.
      • removedInverseProperty

        protected void removedInverseProperty​(org.apache.jena.ontology.OntProperty prop1,
                                              org.apache.jena.ontology.OntProperty prop2,
                                              org.apache.jena.rdf.model.Model inferenceModel)
        If it is removed that P is an inverse of Q, then: 1. For each statement involving predicate P in the abox assertions model remove the inverse statement from the inference model unless that statement is otherwise entailed. 2. Repeat the same for predicate Q.
      • getSameIndividuals

        protected List<org.apache.jena.rdf.model.Resource> getSameIndividuals​(org.apache.jena.rdf.model.Resource ind,
                                                                              org.apache.jena.rdf.model.Model inferenceModel)
        Get a list of individuals the same as the given individual
      • entailedType

        protected boolean entailedType​(org.apache.jena.rdf.model.Resource subject,
                                       org.apache.jena.rdf.model.Resource cls)
        Returns true if it is entailed by class subsumption that subject is of type cls; otherwise returns false.
      • entailedType

        protected boolean entailedType​(org.apache.jena.rdf.model.Resource subject,
                                       org.apache.jena.rdf.model.Resource cls,
                                       List<String> remainingTypeURIs)
        Returns true if it is entailed by class subsumption that subject is of type cls; otherwise returns false. remainingTypeURIs is an optional list of asserted type URIs for the subject resource, and may be null. Supplying a precompiled list can yield performance improvement when this method is called repeatedly for the same subject.
      • getRemainingAssertedTypeURIs

        protected List<String> getRemainingAssertedTypeURIs​(org.apache.jena.rdf.model.Resource resource)
      • getSubClasses

        protected List<org.apache.jena.rdf.model.Resource> getSubClasses​(org.apache.jena.rdf.model.Resource cls)
      • getSuperClasses

        protected List<org.apache.jena.rdf.model.Resource> getSuperClasses​(org.apache.jena.rdf.model.Resource cls)
      • entailedStatement

        protected boolean entailedStatement​(org.apache.jena.rdf.model.Statement stmt)
        Returns true if the triple is entailed by inverse property reasoning or sameAs reasoning; otherwise returns false.
      • getInverseProperties

        protected List<org.apache.jena.ontology.OntProperty> getInverseProperties​(org.apache.jena.rdf.model.Statement stmt)
        Returns a list of properties that are inverses of the property in the given statement.
      • generateInverseInferences

        protected org.apache.jena.rdf.model.Model generateInverseInferences​(org.apache.jena.ontology.OntProperty prop,
                                                                            org.apache.jena.ontology.OntProperty inverseProp)
      • addInference

        public void addInference​(org.apache.jena.rdf.model.Statement infStmt,
                                 org.apache.jena.rdf.model.Model inferenceModel)
        Add an inference from the inference model Adds the inference to the inference model if it is not already in the inference model and not in the abox model.
      • addInference

        protected void addInference​(org.apache.jena.rdf.model.Statement infStmt,
                                    org.apache.jena.rdf.model.Model inferenceModel,
                                    boolean handleSameAs)
      • addInference

        protected void addInference​(org.apache.jena.rdf.model.Statement infStmt,
                                    org.apache.jena.rdf.model.Model inferenceModel,
                                    boolean handleSameAs,
                                    boolean checkRedundancy)
      • removeInference

        public void removeInference​(org.apache.jena.rdf.model.Statement infStmt,
                                    org.apache.jena.rdf.model.Model inferenceModel)
        Remove an inference from the inference model Removes the inference if it is not entailed by the abox model and if the inference model contains it. Removes the corresponding inference for each same individual if that inference is not entailed by the abox model.
      • removeInference

        protected void removeInference​(org.apache.jena.rdf.model.Statement infStmt,
                                       org.apache.jena.rdf.model.Model inferenceModel,
                                       boolean handleSameAs,
                                       boolean checkEntailment)
      • setMostSpecificTypes

        protected void setMostSpecificTypes​(org.apache.jena.rdf.model.Resource individual,
                                            org.apache.jena.rdf.model.Model inferenceModel,
                                            HashSet<String> unknownTypes)
        Find the most specific types (classes) of an individual and infer them for the individual with the mostSpecificType annotation.
      • setMostSpecificTypes

        protected void setMostSpecificTypes​(org.apache.jena.rdf.model.Resource individual,
                                            HashSet<String> typeURIs,
                                            org.apache.jena.rdf.model.Model inferenceModel)
      • getParents

        protected List<org.apache.jena.rdf.model.Resource> getParents​(org.apache.jena.rdf.model.Resource cls,
                                                                      org.apache.jena.ontology.OntModel tboxModel)
      • isInterestedInRemovedStatement

        protected boolean isInterestedInRemovedStatement​(org.apache.jena.rdf.model.Statement stmt)
      • doPlugins

        protected void doPlugins​(ModelUpdate.Operation op,
                                 org.apache.jena.rdf.model.Statement stmt)
      • isRecomputing

        public boolean isRecomputing()
        Returns true if the reasoner is in the process of recomputing all inferences.
      • recompute

        public void recompute()
      • setStopRequested

        public void setStopRequested()
        This is called when the application shuts down.
      • isABoxReasoningAsynchronous

        public boolean isABoxReasoningAsynchronous()
        Asynchronous reasoning mode (DeltaComputer) no longer used in the case of batch removals.
      • notifyEvent

        public void notifyEvent​(String string,
                                Object event)
        Description copied from interface: ChangeListener
        Override this to listen to events pertaining to the given graphURI.
        Specified by:
        notifyEvent in interface ChangeListener
        Parameters:
        string - - the graph to which the event pertains
        event - - the event that occurred.
      • notifyEvent

        public void notifyEvent​(org.apache.jena.rdf.model.Model model,
                                Object event)
        Specified by:
        notifyEvent in interface org.apache.jena.rdf.model.ModelChangedListener
        Overrides:
        notifyEvent in class org.apache.jena.rdf.listeners.StatementListener
      • stmtString

        public static String stmtString​(org.apache.jena.rdf.model.Statement statement)
        Utility method for logging