graphic with four colored squares
Cover page images (keys)

Introduction to N3 Rules

Sandro Hawke, sandro@w3.org

Naming Things

Using:

... to name people, physical objects, abstract classes of things, etc

File-Scope Names (Blank Nodes)

_:fred
_:x
_:node3442

Global-Scope Names: URIs

<http://web.mit.edu>                           a web page
<http://www.w3.org/People/Sandro>              a web page
<http://www.w3.org/People/Sandro/data#sandro>  a person
<http://www.w3.org/People/Connolly/#me>        a person
<http://purl.org/dc/elements/1.1/title         a property
<http://xmlns.com/foaf/0.1/firstName           a property
<http://xmlns.com/foaf/0.1/Person              a class

Global-Scope Names: QNames

Assign some prefixes

@prefix dc: "http://purl.org/dc/elements/1.1/"
@prefix foaf: "http://xmlns.com/foaf/0.1/"
@prefix danc: "http://www.w3.org/People/Connolly/#"

and then just say

foaf:firstName
foaf:Person
danc:me

Variables

Used as placeholders inside rules

?x
?PERSON
?_v21

Other kinds of names can be used as variables if quantified!

Literals

Strings

"strings like this"
"""long "weird" strings like
       this
   one"""

Numbers

21

Arbitrary Datatypes

"2002-09-24Z"^^xs:date
"2002-05-30T09:30:10Z"^^xs:dateTime
"P5Y"^^xs:duration
"XII"^^my:romanNumeral

Expressing Statements (Propositions)

Boil everything down to: <subject> <predicate> <object>.

friends:Sue bio:age 21.
friends:Sue fam:sister _:Betty.

Skip repeated subject using semi-colon:

sandat:sandro foaf:firstName "Sandro";
              foaf:lastName "Hawke".

Expressing Rules

Say that foaf:knows is symmetric (probably not true)

{ ?x foaf:knows ?y }
=>
{ ?y foaf:knows ?x }.
{ ?x sec228:childSupportBalance ?bal.
  ?bal math:greaterThan 1000 
}
=>
{ ?x a sec228:Deadbeat }.

Using N3 with cwm

Give it files of data and/or rules

$ cwm inputs --think

If you're looking for something in particular, use --filter:

$ cwm inputs --think --filter=my_query.n3

Lists

Use lists to say this is all there is. You could do:

sd:sandro fam:sister sd:Maya, sd:Brenna

But that doesn't say there are no more. So:

sd:sandro fam:allSisters (sd:Maya sd:Brenna)

Lists are Syntactic Sugar

:a :b (1 2 3).

is the same as:

:a :b _:list123.
_:list123 rdf:first 1;
          rdf:rest _:list23.
_:list23 rdf:first 2;
         rdf:rest _:list3.
_:list3 rdf:first 3;
        rdf:rest rdf:nil.

This is similar to lists in LISP.

Square Brackets ("something-which-has ...")

sd:sandro foaf:knows [ foaf:firstName "Tim";
                       foaf:lastName "Berners-Lee" ].

In some contexts, this is the same as:

sd:sandro foaf:knows _:tim.
_:tim foaf:firstName "Tim";
      foaf:lastName "Berners-Lee".

But if it's in the "then"-clause of a rule, think of it being a new thing each time.

N-Ary

See Defining N-ary Relations from SWBP WG

How do you say "Serenity is showing at 7:15 at Loews Copley"?

[ a movie:Showing;
    movie:time "7:15";
    movie:title movie:serenity
    movie:theater movie:bostonLoewsCopley
].

Built-In Predicates

See cwm built-ins.

{ (1 2 3 4) math:sum ?x }
=>
{ ... do something with ?x, which will be 10 }

Example: Section 228

See 18 USC § 228

# 18usc228(a)(1) - second branch ($5K limit)
{  ?person s228:residesInDifferentStateFrom ?child.
   [ a s228:UnpaidObligation;
     s228:obligatedPerson ?person;
     s228:supportedPerson ?child;
     s228:amount ?amt
   ].
   ?amt math:greaterThan 5000. 
}
=> 
{ [ a law:ApparentViolation;
      law:alleged_violator ?person;
      law:law usc:18_228;
   ]
}

Ignores temporal aspect....

Needs several supporting rules, defining residesInDifferentStateFrom, etc

Note that human's probably can't accurately follow complex rules.