graphic with four colored squares
Cover page images (keys)

Introduction to N3 Rules

Sandro Hawke,

Naming Things


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

File-Scope Names (Blank Nodes)


Global-Scope Names: URIs

<>                           a web page
<>              a web page
<>  a person
<>        a person
<         a property
<           a property
<              a class

Global-Scope Names: QNames

Assign some prefixes

@prefix dc: ""
@prefix foaf: ""
@prefix danc: ""

and then just say



Used as placeholders inside rules


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



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



Arbitrary Datatypes


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


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.


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.