4

I was unable to find some decent simple code examples of using SWRL and Jena with Pellet, or at least using SWRL? I have studied some examples in Pellet documentation, but there is no example about using SWRL. Most examples on the web are incomplete and confusing.

The only solution I found was with the Jess Rule Engine but it is not free and is under commercial license. I found that Pellet support SWRL rules but could not find running example.

The only example I found is this, but I do not understand it:

OWLOntologyManager m = create();
OWLOntology o = m.createOntology(example_iri);
// Get hold of references to class A and class B.
OWLClass clsA = df.getOWLClass( IRI.create(example_iri +    "#A" ));
OWLClass clsB = df.getOWLClass(IRI.create(example_iri +    "#B"    ));
SWRLVariable var = df.getSWRLVariable(IRI.create(example_iri + "#x" ));
SWRLClassAtom body = df.getSWRLClassAtom(clsA, var);
SWRLClassAtom head = df.getSWRLClassAtom(clsB, var);
SWRLRule rule = df.getSWRLRule(Collections.singleton(body),
Collections.singleton(head));
m.applyChange(new AddAxiom(o, rule));
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Ali Ahmad
  • 1,055
  • 5
  • 27
  • 47

2 Answers2

8

Pellet Rules and Jena Rules are Very Different™

The short answer is that Pellet supports SWRL rules. If you have an ontology that contains SWRL rules and ask Pellet to reason over it, it will take them into consideration.

Jena has its own rules language, which is described in the documentation page, Reasoners and rule engines: Jena inference support. It supports both forward and backward chaining rules.

However, though both Pellet and Jena support a notion of rules, the intended domains of SWRL rules and Jena rules are very different. SWRL rules are OWL-level constructs; the unary predicates in a SWRL rule are class expressions, and the binary predicates are object and data properties. Additionally, SWRL rules only match on named individuals; they don't match for individuals whose existence is only inferred. Jena rules, on the other hand, are RDF-level, and designed to work on RDF-graphs. While RDF and OWL are often used together, (e.g., OWL data is serialized in RDF), the two are conceptually distinct. An OWL reasoner could be implemented that makes no use of RDF, and a SWRL engine could be built that make no use of RDF graphs.

Jena or OWL API?

The code that you've shown, based on the presence of an OWLOntologyManager, is based on the OWL API, not on Jena's API. The OWL API will have more direct functionality for working with OWL and SWRL rules, while Jena will not. (Jena's OntModels work well with OWL1, but support for OWL2 is not complete (and still “open for contributors”).

Rather than using the OWL API or trying to use Jena's API, you'll probably find it easier to create rules using an editor such as Protégé. Martin Kuba has written a very nice OWL2 and SWRL Tutorial that can help you here.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • I have tested some SWRL rules with Protege 4.3 does it will work with Pellet. Secondly what is an alternative for SWRL – Ali Ahmad Jun 28 '13 at 12:46
  • @AliAhmad Protégé 4.3 works with Pellet, as far as I know. There are [SPIN](http://spinrdf.org/) [rules](http://www.w3.org/Submission/2011/SUBM-spin-overview-20110222/) that are based on SPARQL. (I haven't used these, and can't really comment on them.) – Joshua Taylor Jun 28 '13 at 13:45
  • @alex Off hand, I don't recall for sure whether protege 4 supports SWRL, but I think it. However, I don't think the appropriate tab is shown by default. It needs to be turned on (a menu like "Show View" or something), and it doesn't have an obvious name, it's just called "Rules". – Joshua Taylor Jul 25 '16 at 12:34
  • 1
    @Alex Ah, yes, found it. See the last entry in the [Protege 4 OWL FAQ](http://protegewiki.stanford.edu/wiki/Protege-OWL_4_FAQ], [**How do I edit/use SWRL rules in Protege?**](http://protegewiki.stanford.edu/wiki/Protege-OWL_4_FAQ#How_do_I_edit.2Fuse_SWRL_rules_in_Protege). It starts with the process "Create a new tab (Window -> Views -> Create new tab...) called something like Rules, SWRL or whatever you prefer". – Joshua Taylor Jul 25 '16 at 12:37
  • @JoshuaTaylor but that rule editor tab seems to be inconsistent with SWRL rules .please take a look [here](http://stackoverflow.com/questions/38590871/swrl-not-work-on-protege-4-3) – alex Jul 26 '16 at 17:21
6

SWRL rules work with Pellet API. I created my ontology and SWRL rules using Protégé and I was able to create OWL individuals dynamically using Java code. This whole ontology is used as aggregatedOwl in the following code. This code loads ontology (base OWL + individuals if any + SWRL rules) and runs the Pellet reasoner on it and saves the inferred result in a string.

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.StringDocumentTarget;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import org.semanticweb.owlapi.util.InferredAxiomGenerator;
import org.semanticweb.owlapi.util.InferredOntologyGenerator;
import org.semanticweb.owlapi.util.InferredPropertyAssertionGenerator;
import com.clarkparsia.pellet.owlapiv3.PelletReasoner;
import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory;

try {
    manager = OWLManager.createOWLOntologyManager();

    InputStream owlInputStream = new ByteArrayInputStream(aggregatedOwl.getBytes("UTF-8"));
    inferredOntology = manager.loadOntologyFromOntologyDocument(owlInputStream);

    PelletReasoner reasoner = PelletReasonerFactory.getInstance().createReasoner(inferredOntology);
    reasoner.getKB().realize();

    List<InferredAxiomGenerator<? extends OWLAxiom>> axiomGenerators = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
    axiomGenerators.add( new InferredPropertyAssertionGenerator() );

    InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner,axiomGenerators);
    iog.fillOntology(manager, inferredOntology);

    // Save the new ontology
    OutputStream owlOutputStream = new ByteArrayOutputStream();
    manager.saveOntology(inferredOntology, owlOutputStream);
    inferredData = owlOutputStream.toString();
}
catch ( Exception e ) {
    throw new Exception("Exception occurred in applying reasoner");
}

Hope this is helpful to you.

Swamy
  • 771
  • 1
  • 8
  • 24
  • 3
    I find your code confusing! You are mixing Jena and OwlApi in your imports, while you are not using any of the Jena imports. – PCoder Jun 24 '14 at 15:32
  • 1
    Thank you for pointing out, I did not remove unnecessary imports from my code snippet. I edited my answer. hope it is OK now. – Swamy Jun 25 '14 at 07:14