Jena Rules Examples
From Wiki
Basic Rule Examples
Example 1 : Using Plain and Typed Literals
Any driver living in New York and having training driver certificate is eligible for insurance.
<ex:Driver rdf:about="http://example.com/John">
<ex:state>New York</ex:state>
<ex:hasTrainingCertificate rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">true</ex:hasTrainingCertificate>
</ex:Driver>
@prefix rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
@prefix ex: http://example.com/
@prefix xs: http://www.w3.org/2001/XMLSchema#
[eligibleDriver: (?d rdf:type ex:EligibleDriver)
<-
(?d rdf:type ex:Driver)
(?d ex:state "New York")
(?d ex:hasTrainingCertificate "true"^^xs:boolean)]
Example 2 : Using Built-ins
A driver is young if has between 18 and 25 years old.
<ex:Driver rdf:about="http://example.com/John"><br>
<ex:age rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">21</ex:age><br>
</ex:Driver>
@prefix rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
@prefix ex: http://example.com/
@prefix xs: http://www.w3.org/2001/XMLSchema#
[youngDriver: (?d rdf:type ex:YoungDriver)
<-
(?d rdf:type ex:Driver)
(?d ex:age ?a)
greaterThan(?a,18)
lessThan(?a,25)]
Example 3 : Using Individuals
Any young driver having driver's school certificate and no accidents is eligible for insurance.
<ex:Driver rdf:about="http://example.com/John">
<ex:certificateType rdf:resource="http://example.com/DriverSchoolCertificate" />
<ex:accidentsNumber rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">0</ex:accidentsNumber>
</ex:Driver>
@prefix rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
@prefix ex: http://example.com/
@prefix xs: http://www.w3.org/2001/XMLSchema#
[eligibleDriver: (?d rdf:type ex:EligibleDriver)
<-
(?d rdf:type ex:Driver)
(?d ex:certificateType ex:DriverSchoolCertificate)
(?d ex:accidentsNumber "0"^^xs:integer)]
Advanced Rule Examples
Example 1 : Using Negation
A driver is typical if he is neither young driver or senior driver.
<ex:Driver rdf:about="http://example.com/John">
<ex:name>Jojn Smith</ex:name>
</ex:Driver>
@prefix rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
@prefix ex: http://example.com/
[eligibleDriver: (?d rdf:type ex:TypicalDriver)
<-
(?d rdf:type ex:Driver)
noValue(?d rdf:type ex:YoungDriver)
noValue(?d rdf:type ex:SeniorDriver)]
Example 1 : Split disjunction in multiple rules
A male driver living in New York or Vancouver and having less than 2 accidents is eligible for insurance.
<ex:Driver rdf:about="http://example.com/John">
<ex:state>Vancouver</ex:state>
<ex:accidentsNumber rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">1</ex:accidentsNumber>
</ex:Driver>
@prefix rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
@prefix ex: http://example.com/
[eligibleDriver_1: (?d rdf:type ex:EligibleDriver)
<-
(?d rdf:type ex:Driver)
(?d ex:state "New York")
(?d ex:accidentsNumber ?an)
lessThan(?an,2)]
[eligibleDriver_2: (?d rdf:type ex:EligibleDriver)
<-
(?d rdf:type ex:Driver)
(?d ex:state "Vancouver")
(?d ex:accidentsNumber ?an)
lessThan(?an,2)]

