Short Tutorial on using Pellet + OWLAPI + SWRL Rules

I’ve been looking through Pellet and OWLAPI documentation over the past few days, looking for a good example of running existing SWRL rules via the OWLAPI using Pellet’s built-in DL-safe SWRL support. SWRL is used in ontology mappping, and is a powerful tool. Up until now, I’ve just used the SWRLTab, but needed to start running my rules via plain Java programs, and so needed to code the running of the mapping rules in the OWLAPI (which I’m more familiar with than Jena). Once I clean up the test code, I’ll link it from here so others can take a look if they feel like it.

This example uses the following versions of the software:

Pre-existing Examples

Pellet provides a SWRL rule example ( in the Pellet download), but only for Jena, and not the OWLAPI. The OWLAPI covers the creation of SWRL rules, but not their running. Therefore, to help others who may be walking the same path as I, a short example of OWLAPI + Pellet + SWRL follows.

New Example

This example assumes that you already have the classes, individuals, and rules mentioned below in an OWL file or files. Here is how the test ontology looks, before running the rule (you can use reasoner.getKB().printClassTree() to get this sort of output):

- (source:indvSourceA)
source:SourceB - (source:indvSourceB)

The example SWRL rule is this (the rule.toString() method prints this kind of output, while iterating over ontology.getRules()):

Rule( antecedent(SourceA(?x)) consequent(TargetA(?x)) )

Please note that if you want to modularise your OWL files, as I do (I have different files for the source classes, the target classes, the source individuals, the target individuals, and the rules) then make sure your owl:imports in the primary OWL ontology are correct, and that you’ve mapped them correctly with the SimpleURIMapper class and the manager.addURIMapper(mapper)method. I will update this post with some unit tests of this setup once I’ve cleaned up the code for public consumption.

Once you have your ontology properly loaded into an OWLAPI OWLOntology class, you should simply realize the ontology with the following command to run the SWRL rules:


After this command, all that’s left to do is save the new inferences. In this simple case, one individual is asserted to also be a child of the TargetA class, as follows:

source:SourceA - (source:indvSourceA)
source:SourceB - (source:indvSourceB)
target:TargetA - (source:indvSourceA)

You can do this by using the following code to explicitly save the new inferences to a separate ontology file. You can modify InferredOntologyGenerator to just save a subset of the inferences, if you like. Have a look in the OWLAPI code or javadoc for more information. Alternatively, you could just iterate over the ABox and just save the new individuals to a file. Here’s the code for saving the ontology to a new location:

OWLOntology exportedOntology = manager.createOntology( URI.create( outputLogicalUri ) );
InferredOntologyGenerator generator = new InferredOntologyGenerator( reasoner );
generator.fillOntology( manager, exportedOntology );
manager.saveOntology( exportedOntology, new RDFXMLOntologyFormat(), URI.create( outputPhysicalUri ) );

I hope this helps some people!

13 thoughts on “Short Tutorial on using Pellet + OWLAPI + SWRL Rules”

  1. Hey,
    I have been doing the same over the last couple of days. Have you looked into using a relational DB with OWL API? I would need some help on this.


    1. There are a number of options for Jena (e.g. TDB), but there is also an option for the OWLAPI. It’s called OWLDB ( I haven’t had the chance to try it out yet, but there has been some discussion of it on the owlapi developers list (e.g. Both the Jena and OWLAPI database backends are things I plan to try out in the very near future.

      1. Hi,
        I looked into owldb, but I see that it automatically creates the database schema based on ontology schema. However, I couldn’t try it out as I couldn’t find any jar with its distribution.
        My requirements are a little different. My database schema and ontology schema aren’t quite the same. I am currently looking into populating an ontology querying a relational database, loading the ontology into Pellet, do the reasoning and populating the database again with the obtained entailments.
        It would be great if you can comment on this or please let me know if you have come across anything similar to what I have been looking at.


  2. Well, I don’t know of any directly, but there has been a limited amount of chatter about this recently on the owlapi-developers mailing list here: . However, there doesn’t seem to be an answer there. Perhaps if you were to respond to that thread, asking for what you might need? Good luck!

  3. Thank you for sharing useful info.
    I have rule like this below
    Rule( antecedent(Person(?p) hasAent(?p ?a)) consequent(Person(?p)) )

    You mentioned above that “you could just iterate over the ABox and just save the new individuals to a file”

    I don’t understand how do use that? can you please tell me bit detailed?
    Thanks a lot.

    1. I’m not quite sure what you’re trying to accomplish with the above rule – it seems that you wish to assert that individuals of Person who have an hasAgent role should also be individuals of Person? That does not seem to give you any extra information. However, there are a number of ways to save an ontology as a separate file, many of which are shown in the OWLAPI documents ( As for just pulling out the ABox, some of this can be done with the InferredOntologyGenerator, as below if you use the default axiom generators:

      InferredOntologyGenerator generator = new InferredOntologyGenerator( getReasoner() );
      inferredInstanceOntology = getManager().createOntology( URI.create( getOutputLogicalUri() ) );
      generator.fillOntology( getManager(), inferredInstanceOntology );

      getManager().saveOntology( inferredInstanceOntology, new RDFXMLOntologyFormat(),URI.create( getOutputPhysicalUri() ) );

      Or, you can replace the first line above with the following to select only those axiom generators you wish to use, e.g.:

      List<InferredAxiomGenerator> axiomGenerators = new ArrayList<InferredAxiomGenerator>();
      axiomGenerators.add( new InferredClassAssertionAxiomGenerator() );
      axiomGenerators.add( new InferredSubDataPropertyAxiomGenerator() );
      axiomGenerators.add( new InferredSubObjectPropertyAxiomGenerator() );
      InferredOntologyGenerator generator = new InferredOntologyGenerator( getReasoner(), axiomGenerators );

      However, I have not yet got these to behave exactly as I wish them to, and am currently working on getting the output exactly as I wish: namely, the individuals, and all associated object/data properties, either all individuals or only those who were modified via the SWRL rules.

  4. hi, i am new to pellet and owlapi and i am trying to get to know the latest versions of them. Could you tell me where can i get a good and updated tutorial (like yours but with more topics), because everything i have found until now seems old.

  5. Hello,
    I am trying to get the inferred Information from my Ontology just like you did it my Code looks as following

    public class Checker {
    public void infer(OWLOntology onto) …

    Reasoner reasoner = prf.createReasoner(manager);


    OWLOntology inferred = manager.createOntology( URI.create( “http://myBeleg/ontology/inferred.owl” ) );
    InferredOntologyGenerator generator = new InferredOntologyGenerator( reasoner );
    [1] generator.fillOntology( manager, inferred );
    manager.saveOntology(inferred, new RDFXMLOntologyFormat(), URI.create(“file:/data/inferred.owl”));


    until [1] everything appears to work correctly, i can see the inferred class tree, but when the program executes [1] i become the following exception, any ideas on how to solve this or where the mistake could be?

    at java.util.Collections$SingletonSet$
    at org.mindswap.pellet.datatypes.BaseAtomicDatatype.getValue(
    at org.mindswap.pellet.tableau.branch.LiteralValueBranch.tryBranch(
    at org.mindswap.pellet.tableau.branch.Branch.tryNext(
    at org.mindswap.pellet.tableau.completion.CompletionStrategy.applyLiteralRule(
    at com.clarkparsia.pellet.rules.ContinuousRulesStrategy.complete(
    at org.mindswap.pellet.ABox.isConsistent(
    at org.mindswap.pellet.ABox.isType(
    at org.mindswap.pellet.ABox.getDataPropertyValues(
    at org.mindswap.pellet.ABox.getDataPropertyValues(
    at org.mindswap.pellet.KnowledgeBase.getDataPropertyValues(
    at org.mindswap.pellet.KnowledgeBase.getDataPropertyValues(
    at org.mindswap.pellet.owlapi.Reasoner.getRelatedValues(
    at org.mindswap.pellet.owlapi.Reasoner.getDataPropertyRelationships(
    at org.semanticweb.owl.util.InferredPropertyAssertionGenerator.addAxioms(
    at org.semanticweb.owl.util.InferredPropertyAssertionGenerator.addAxioms(
    at org.semanticweb.owl.util.InferredEntityAxiomGenerator.createAxioms(
    at org.semanticweb.owl.util.InferredOntologyGenerator.fillOntology(
    at de.rodo.test.Checker.infer(
    at de.rodo.test.Main.main(

    Thanks in advanced

  6. Hi, thanks for a great tutorial. Though old, I find it useful even today. I found the links to is no more valid. It would also be great to have a complete source code for the enitre exercise.

  7. Thanks for your comment. is an example provided by the OWLAPI itself, so if it’s no longer valid that might be because the API has been updated and perhaps files have been moved or renamed. Have a look and the OWLAPI and see if you can find something similar. I no longer have the example code, but I agree that the source code for the exercise would be useful – next time I will include it, thanks!

    1. Thanks for the comment. I’ve not used the OWL API recently, and so my knowledge is a bit out of date. However, I’m sure the good people at would be very happy to help you out. You can definitely create SWRL rules with the OWLAPI and then save it to your ontology, but you’ll need to look into the OWL API itself, and perhaps ask the mailing list, to find out more specifics. Good luck!

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s