Mulgara 1.0 includes a custom rules
engine known as Krule (pronounced "cruel" by its lead
developer).
The purpose of Krule is to apply a
set of entailment rules to a base set of statements
("facts"), producing new statements based on those facts.
In other words, Krule applies a set of pre-defined rules
to statements in a model. The application of the rules
results in some new RDF statements, which may be added to
that model or stored elsewhere.
Krule is implemented in a manner
consistent with Mulgara. Rule definitions are provided in
RDF files and may be loaded into Mulgara models.
Similarly, Krule acts upon statements in models and
produces statements which may be stored in the same or
different models. Krule is "triples all the way
down".
Krule has been used to entail RDF
Schema rules and ongoing work may lead to a more scalable
method of dealing with Web Ontology Language (OWL)
analysis. Users and developers are encouraged to
investigate this impressive new addition to Mulgara.
Anyone may add new rules and apply them to
models.
Setting up Krule
Some setup is required before one may
use Krule. Please note that this tutorial presumes the
use of ’localhost’ as a machine name,
’server1’ as a database name. If you choose
to change these or use different model names for the
Krule setup, you may have to also change the URLs
embedded in the Krule rules files (called krule.owl and
rdfs-krule.rdf, both of which are located in the
.../mulgaa-1.0/rules/ directory) and in the commands
given below.
First, set up some handy aliases:
# set up aliases
alias <http://mulgara.org/mulgara#> as mulgara;
alias <http://www.w3.org/1999/02/22-rdf-syntax-ns#> as rdf;
alias <http://www.w3.org/2000/01/rdf-schema#> as rdfs;
alias <http://www.w3.org/2002/07/owl#> as owl;
alias <http://mulgara.org/owl/krule/#> as krule;
Next, create some special models
which are used by Krule and presumed to be present:
# create utility models
create <rmi://localhost/server1#type> <mulgara:TypeModel>;
create <rmi://localhost/server1#prefix> <mulgara:PrefixModel>;
Create a model to hold the Krule
statements. Naturally, the URL that you use may vary:
# clear and create the rules model
drop <rmi://localhost/server1#krule>;
create <rmi://localhost/server1#krule>;
Load the Krule ontology into your
krule model. This ontology is used regardless of which
rules are used. You will need to replace "@basedir@" with
the path to your Mulgara v1.0 source code
installation:
# Load the ontology for how the rules data fits together.
load <file:@basedir@/rules/krule.owl>
into <rmi://localhost/server1#krule>;
Load your rules into your krule
model. In this case, we are loading the RDF Schema
entailment rules which are provided in the file
.../mulgara-1.0/rules/rdfs-krule.rdf.
# Load the rules data.
load <file:@basedir@/rules/rdfs-krule.rdf>
into <rmi://localhost/server1#krule>;
Krule is now ready to go. All you
have to do is apply the rules to some RDF statements,
known in the parlance as "base data". This is covered in
the next section.
Please note that the instructions in
this section follow the file
.../mulgara-1.0/rules/example.itql, originally written by
Paul Gearon.
Using Krule
Krule is used by applying a set of
rules to a set of RDF statements. The rules are
themselves RDF statements and are stored in their own
model. The RDF statements upon which those rules are
applied are known as "base data" and are stored in
another model.
The application of the rules to the
base data is accomplished by issuing the "apply" iTQL
command, like this:
apply <rulesmodel> to <basemodel>;
A Mulgara instance may have many
different models containing rules, since the rules are
just a set of RDF statements.
Using apply that way will place the
newly-generated entailed statements into the base
data’s model (<basemodel>). If you want to
put the entailed statements into a separate model, you
may do so with this syntax:
apply <rulesmodel> to <basemodel> <entailedmodel>;
Base data may consist of any RDF
statements stored in a Mulgara model. There are no
restrictions on what kind of data may have Krule rules
run against it.
To try Krule, you first need to
complete the setup steps above. Once that is done, you
need to find some sample data. Fortunately, Mulgara ships
with some sample data. You can create a model and load it
in:
drop <rmi://localhost/server1#input>;
create <rmi://localhost/server1#input>;
load <jar:file:/@basedir@/dist/mulgara-1.0.0.jar!/data/w3c-news.rss>
into <rmi://localhost/server1#input>;
Although Krule allows you to put the
entailed statements into the same model as the base data,
we will separate them so you can see what was created. To
do that, we need to create a model to hold the
output:
drop <rmi://localhost/server1#output>;
create <rmi://localhost/server1#output>;
Now we are ready to apply the rules
to the base data in the input model and store the
resulting entailed statements in the output model:
apply <rmi://localhost/server1#krule>
to <rmi://localhost/server1#input>
<rmi://localhost/server1#output>;
The newly-created entailed statements
may be viewed by querying the output model:
select $subject $predicate $object
from <rmi://localhost/server1#output>
where $subject $predicate $object;
We can show the numbers of statements
in the input and output models by using iTQL’s
count function:
select
count ( select $subject $predicate $object
from <rmi://localhost/server1#input>
where $subject $predicate $object
)
from <rmi://localhost/server1#input>
where $subject $predicate $object;
select
count ( select $subject $predicate $object
from <rmi://localhost/server1#output>
where $subject $predicate $object
)
from <rmi://localhost/server1#output>
where $subject $predicate $object;