SWRLRuleEngineAPI

Martin O'Connor edited this page Sep 14, 2016 · 32 revisions

The SWRL Rule Engine API is a SWRLAPI component for creating and manipulating SWRL rules.

A factory class called SWRLAPIFactory can be used to create instances of a rule engine. We must first create an OWLAPI-based instance of that ontology, which is represented by the OWLOntology interface.

 // Create OWLOntology instance using the OWLAPI
 OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
 OWLOntology ontology 
   = ontologyManager.loadOntologyFromOntologyDocument(new File("/ont/Ont1.owl"));
 
 // Create a SWRL rule engine using the SWRLAPI  
 SWRLRuleEngine ruleEngine = SWRLAPIFactory.createSWRLRuleEngine(ontology);
 
 // Run the rule engine
 ruleEngine.infer();
  

An exception called SWRLRuleEngineException will be thrown if an error occurs on rule engine creation.

Once a rule engine has been created, it can be used to execute SWRL rules. The SWRLRuleEngine interface provides the following methods to control this process:

  • reset() - Clear all knowledge from the rule engine.
  • importAssertedOWLAxioms() - Import all SWRL rules and relevant asserted OWL axioms from the associated OWL ontology into the rule engine. All existing rules and knowledge will first be cleared and the associated rule engine will be reset.
  • run() - Invoke the rule engine.
  • exportInferredOWLAxioms() - Transfer any OWL axioms asserted by a rule engine to the associated OWL ontology.
  • infer() - Load rules and asserted OWL axioms into the rule engine, run the rule engine, and write any inferred axioms back to the OWL ontology. This method is a wrapper around the four preceding methods.

None of these methods has a return value and all methods throw SWRLRuleEngineException.

The public interface to a SWRL rule engine is designed to be the same irrespective of the particular rule engine implementation. Users of a SWRL rule engine may not necessarily know or care about the underlying rule engine being used.

The OWLAPI can be used to programmatically create SWRL rules for use with a rule engine. The SWRLAPI also provides a parser to parse a textual representation of a SWRL rule. Using this parser, a rule (which is represented by a class called SWRLAPIRule can be created as follows:

 SWRLAPIRule rule = ruleEngine.createSWRLRule("IsAdult-Rule", 
   "Person(?p) ^ hasAge(?p, ?age) ^ swrlb:greaterThan(?age, 17) -> Adult(?p)");

A SWRLParseException will be thrown if the rule text is not valid.

So, to create a rule and execute it using a SWRL rule engine one can do the following:

 // Create OWLOntology instance using the OWLAPI
 OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
 OWLOntology ontology 
   = ontologyManager.loadOntologyFromOntologyDocument(new File("/ont/Ont1.owl"));
 
 // Create a SWRL rule engine using the SWRLAPI  
 SWRLRuleEngine ruleEngine = SWRLAPIFactory.createSWRLRuleEngine(ontology);
 
 // Create a SWRL rule  
 SWRLAPIRule rule = ruleEngine.createSWRLRule("IsAdult-Rule",
   "Person(?p) ^ hasAge(?p, ?age) ^ swrlb:greaterThan(?age, 17) -> Adult(?p)");
 
 // Run the rule engine
 ruleEngine.infer();