Home > Data Integration, Semantics and Ontologies, Software and Tools > Short Tutorial on using Pellet + OWLAPI + SWRL Rules

Short Tutorial on using Pellet + OWLAPI + SWRL Rules

December 1, 2009 Leave a comment Go to comments

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 (RulesExample.java in the Pellet download), but only for Jena, and not the OWLAPI. The OWLAPI Example3.java 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):


owl:Thing
source:SourceA
- (source:indvSourceA)
source:SourceB - (source:indvSourceB)
target:TargetA
target:TargetB

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:


getReasoner().getKB().realize();

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:


owl:Thing
source:SourceA - (source:indvSourceA)
source:SourceB - (source:indvSourceB)
target:TargetA - (source:indvSourceA)
target:TargetB

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!

About these ads
  1. soontobeared
    December 3, 2009 at 18:28 | #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.

    Thanks.

    • December 4, 2009 at 08:59 | #2

      There are a number of options for Jena (e.g. TDB), but there is also an option for the OWLAPI. It’s called OWLDB (http://owldb.sourceforge.net). 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. http://sourceforge.net/mailarchive/message.php?msg_name=33863c3b0911251520x3747291ai408350af705d7bf6%40mail.gmail.com). Both the Jena and OWLAPI database backends are things I plan to try out in the very near future.

      • soontobeared
        December 4, 2009 at 20:14 | #3

        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.

        Thanks.

  2. December 8, 2009 at 15:06 | #4

    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: http://bit.ly/69qa1d . 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. alswamy
    December 18, 2009 at 14:39 | #5

    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.

    alswamy@gmail.com

    • January 4, 2010 at 11:32 | #6

      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 (http://owlapi.sourceforge.net/documentation.html). 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. rodo2024
    January 2, 2010 at 21:25 | #7

    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. rodo2024
    January 18, 2010 at 23:50 | #9

    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);
    reasoner.loadOntology(onto);

    reasoner.classify();
    reasoner.getKB().realize();
    reasoner.getKB().printClassTree();

    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?

    java.util.NoSuchElementException
    at java.util.Collections$SingletonSet$1.next(Collections.java:3083)
    at org.mindswap.pellet.datatypes.BaseAtomicDatatype.getValue(BaseAtomicDatatype.java:128)
    at org.mindswap.pellet.tableau.branch.LiteralValueBranch.tryBranch(LiteralValueBranch.java:87)
    at org.mindswap.pellet.tableau.branch.Branch.tryNext(Branch.java:115)
    at org.mindswap.pellet.tableau.completion.CompletionStrategy.applyLiteralRule(CompletionStrategy.java:1895)
    at com.clarkparsia.pellet.rules.ContinuousRulesStrategy.complete(ContinuousRulesStrategy.java:356)
    at org.mindswap.pellet.ABox.isConsistent(ABox.java:1306)
    at org.mindswap.pellet.ABox.isType(ABox.java:806)
    at org.mindswap.pellet.ABox.getDataPropertyValues(ABox.java:982)
    at org.mindswap.pellet.ABox.getDataPropertyValues(ABox.java:937)
    at org.mindswap.pellet.KnowledgeBase.getDataPropertyValues(KnowledgeBase.java:3711)
    at org.mindswap.pellet.KnowledgeBase.getDataPropertyValues(KnowledgeBase.java:3747)
    at org.mindswap.pellet.owlapi.Reasoner.getRelatedValues(Reasoner.java:542)
    at org.mindswap.pellet.owlapi.Reasoner.getDataPropertyRelationships(Reasoner.java:316)
    at org.semanticweb.owl.util.InferredPropertyAssertionGenerator.addAxioms(InferredPropertyAssertionGenerator.java:51)
    at org.semanticweb.owl.util.InferredPropertyAssertionGenerator.addAxioms(InferredPropertyAssertionGenerator.java:39)
    at org.semanticweb.owl.util.InferredEntityAxiomGenerator.createAxioms(InferredEntityAxiomGenerator.java:51)
    at org.semanticweb.owl.util.InferredOntologyGenerator.fillOntology(InferredOntologyGenerator.java:120)
    at de.rodo.test.Checker.infer(Checker.java:106)
    at de.rodo.test.Main.main(Main.java:30)

    Thanks in advanced
    Rodolfo

  1. No trackbacks yet.
You must be logged in to post a comment.
Follow

Get every new post delivered to your Inbox.

Join 493 other followers

%d bloggers like this: