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:

<name>DBIS Maven Releases Repository</name>

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( ) );

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

Prefix: xsd: <;
Prefix: owl: <;
Prefix: : <urn:cbob:event#>
Prefix: xml: <;
Prefix: rdf: <;
Prefix: rdfs: <;
Prefix: skos: <;

Ontology: <urn:cbob:event>

Class: RelativeTime

Individual: today


DataProperty: hasDate


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}

Post a comment or leave a trackback: Trackback URL.


  • 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: 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

%d bloggers like this: