Thursday, July 16, 2009

An Introduction to Inference Rules

Intellidimension’s SPARQL extensions for inference rules are a powerful tool when developing a semantics based application. In this article I will show you a few simple examples to get you started. Intellidiemsion has added a SPARQL extension called a rulebase for defining inference rules to be used in any SPARQL query. When an SPARQL query is executed all the rules in all the rulebases are evaluated. A rulebase can be loaded into a model and referenced by uri or declared right inline with your SPARQL query. In this article let’s take a looks at an inline rulebase.

prefix rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#>
prefix x:<http://example.org/>

rulebase(
# create some facts
construct {x:D rdfs:subClassOf x:B}
construct {x:C rdfs:subClassOf x:B}
construct {x:B rdfs:subClassOf x:A}

#transitive subclass rule
construct {?class rdfs:subClassOf ?super} where {
?class rdfs:subClassOf ?x.
?x rdfs:subClassOf ?super }

# root class relation
select ?class ?root where {
?class rdfs:subClassOf ?root.
not(?root) {?root rdfs:subClassOf ?super}
} as :rootClass

# aggregation relation
select ?class :count(?sub) as ?count where {
?sub rdfs:subClassOf ?class.
} group by ?class as :numberOfSubClasses

)

select ?super where {x:C rdfs:subClassOf ?super}
#select ?root where {:rootClass(x:C, ?root)}
#select ?count where {:numberOfSubClasses(x:B, ?count)}



The example about creates a rulebase with some facts about subclass relationships and some inference rules to derive some additional information about those class relationships. The rule:

construct {?class rdfs:subClassOf ?super} where {
?class rdfs:subClassOf ?x.
?x rdfs:subClassOf ?super }

Recursively generates all the subclass relationships so when the following query is run:


select ?super where {x:C rdfs:subClassOf ?super}

it results in:

<http://example.org/A> 
<http://example.org/B>

The rulebase also contains a relation rule for finding the root class (the top base class) of any class. Note that this rule actually makes use of our previous rule for recursively traversing the class hierarchy.


select ?class ?root where {
?class rdfs:subClassOf ?root.
not(?root) {?root rdfs:subClassOf ?super}
} as :rootClass

When we execute this rule in a query by replacing the SELECT query in the example with the following:


select ?root where {:rootClass(x:C, ?root)}

We get:


<http://example.org/A>

The last rule in the rulebase provides both an example of the use of a SPARQL function as well as an aggregation. This rule calculates how many subclasses a class has.

select ?class :count(?sub) as ?count where { 
?sub rdfs:subClassOf ?class.
} group by ?class as :numberOfSubClasses

When we execute this rule in a query (replace the SELECT in the example):


select ?count where {:numberOfSubClasses(x:B, ?count)}

We get:


"2"^^<http://www.w3.org/2001/XMLSchema#int>

Hopefully this is enough to get you started with inference rules. I will post some more complicated use of rules in the future.

0 comments:

Post a Comment