How to extend Pellet2.2.2’s SWRL rule support with your custom built-in ?

Broadly, there are 3 steps:

1. create your built-in class ;

2. register it to pellet;

3. use your built-in in your rule;

0) set up your environment the  maven way

add dependency and repository to your pom.xml as listed below:

<dependency>
<groupId>com.owldl</groupId>
<artifactId>pellet</artifactId>
<version>2.2.2</version>
<scope>runtime</scope>
</dependency>
<repository>
<id>on.cs.unibas.ch</id>
<name>DBIS Maven Releases Repository</name>
<url>http://on.cs.unibas.ch/maven/repository</url&gt;
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>

1) create your built-in class:

package cbob.builtin;
import static org.mindswap.pellet.utils.Namespaces.XSD;
import com.clarkparsia.pellet.rules.builtins.GeneralFunction;
import aterm.ATermAppl;
import org.mindswap.pellet.ABox;
import org.mindswap.pellet.Literal;
import org.mindswap.pellet.utils.ATermUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MoreDateTimeOperators {

private static class Today implements GeneralFunction {
static Log lg = LogFactory.getLog(Today.class);
public boolean apply(ABox abox, Literal[] args) {
lg.debug(“here we are in custom builtIn”);
Calendar calendar = Calendar.getInstance();
SimpleDateFormat isoFormat = new SimpleDateFormat(“yyyy-MM-dd”);
ATermAppl tt = ATermUtils.makeTypedLiteral(isoFormat.format(calendar.getTime())
, XSD+”date”);
Literal result = abox.addLiteral(tt);
if ( result != null ) {
if ( args[0] == null )
args[0] = result;
return true;
}
return false;
}

public boolean isApplicable(boolean[] boundPositions) {
return true;
}
}
public final static GeneralFunction today = new Today();
}

2) register your built-in to pellet

The code below should be inserted anywhere before loading pellet.

BuiltInRegistry.instance.registerBuiltIn( “urn:cbob:builtIn#today“, new GeneralFunctionBuiltIn( MoreDateTimeOperators.today ) );

3) Use your built-in in your ontology(Manchester Syntax)

Prefix: xsd: <http://www.w3.org/2001/XMLSchema#&gt;
Prefix: owl: <http://www.w3.org/2002/07/owl#&gt;
Prefix: : <urn:cbob:event#>
Prefix: xml: <http://www.w3.org/XML/1998/namespace&gt;
Prefix: rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;
Prefix: rdfs: <http://www.w3.org/2000/01/rdf-schema#&gt;
Prefix: skos: <http://www.w3.org/2004/02/skos/core#&gt;

Ontology: <urn:cbob:event>

Class: RelativeTime

Individual: today

Types:
RelativeTime

DataProperty: hasDate

SubPropertyOf:
owl:topDataProperty

Rule:
RelativeTime(today), <urn:cbob:builtIn#today>(?<t>) -> hasDate(today, ?<t>)

3+) you can load this ontology and now pellet can answer “what date is today?” in SPARQL,

PREFIX : <urn:cbob:event#>

SELECT ?when WHERE { today hasDate ?when}

Advertisements
Post a comment or leave a trackback: Trackback URL.

Comments

  • Ico80  On 2011/08/24 at 10:54 am

    Could you please translate Manchester Syntax to the RDFS/OWL? Sorry I didn’t understand how to translate (?) so to replicate your try,
    Thanks in advance!

    • semwebguy  On 2011/08/25 at 8:44 am

      I think you can translate between lots of syntax using protege tool.

  • John Hebeler  On 2011/10/22 at 11:57 am

    Could you please provide an RDF/XML translation of the just the rule (Rule:
    RelativeTime(today), (?) -> hasDate(today, ?)) – the rest I can handle. What I have tried has not been accepted by Pellet.

    Thank you!!!

  • Monia  On 2011/12/03 at 9:49 am

    if my built-in’s name is xxx, I don’t understand how to construct urn:cbob:builtIn#xxx in my java code as well as in my SWRL rule included in my ontology (in RDF/XML syntax).

    Would you please help me!

    • semwebguy  On 2012/01/30 at 9:36 am

      You can use your own namespace and name of the builtin, just register it right is ok.

  • Monia  On 2011/12/03 at 9:51 am

    what is urn and cbob?

    • semwebguy  On 2012/01/30 at 9:35 am

      urn is for Unique Resource Name(its brother is URL), cbob is my company’s prefix.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: