Introduction to N3 rules - 2



Tim Berners-Lee



Decentralized Information Group
MIT Computer Science and Artificial Intelligence Laboratory

Reaching out onto the Web

Semantic Web Boot Camp

Derived from Semantic Web Tutorial

These slides:


A built-in function for fetching RDF content off the web for processing.

{ <foo.rdf> log:semantics ?f } 
{ ?f a :InterestingFormula }.

For cwm, the named pages have to be in RDF/XML or N3. It uses Content-Type header and some heuristics.

Try squid and setenv http_proxy, if network performance is a problem.


Once we have the formula, how do we process it?

{ :a :b :c.   :d :e :f. } 
log:includes { :a :b :c. }.

If someone's home page says that they are a vegetarian, then we believe that they are a vegetarian:

@forAll :x.
{ [is :homePage of :x] log:includes 
            { :x a :Vegetarian }}
=> { :x a :Vegetarian}. 

Note: This is a case where we need explicit quantification. With ?x, we would be talking about pages saying "everyone is a vegetarian".


Formulas are just sets of triples, so we can test for what is not contained.

If someone's page does not say they are vegetarian, let's assume they are an omnivore:

@forAll :x.
{:x :homePage log:notIncludes 
                  { :x a :Vegetarian }}
=> { :x a :Omnivore }. 

In the open web, you can never just have a default for a value.
You have to give a default with respect to some dataset.

More using defaults

If the specification for a car doesn't say what color it is then define it as black:

@forAll :car.
{ :car!auto:specification 
      log:notIncludes {:car auto:color []}}
    => {:car auto:color auto:black}.


What if we could only infer the car was red?

{ ?car a auto:SuperSportyModel. }
{ ?car auto:color auto:red. }

Our defaults rule won't use that fact unless we ask cwm to do another level of rules processing.

@forAll :car.
        log:notIncludes {:car auto:color []}}
=> {:car auto:color auto:black}.


If you want to build a formula out of different peices, you contruct their conjunction

( { :a :b :c. } { :d :e :f. } ) 
{ :a :b :c.  :d :e :f. }.

Use with log:semantics, etc

( { :a :b :c. } <foo.rdf>.log:semantics ) 
{ :a :b :c.  (whatever foo.rdf said). }.

Combined Example

{   ( <>!log:semantics 
      <system-rules.n3>!log:semantics ) 
                    log:conjunction ?f.
    ?f log:conclusion ?g.
    ?g log:notIncludes 
                { ?request a :ValidRequest }
} => {
    ?request a InvalidRequest

This means: if what you get by taking the input data, the axioms and the system rules together and thinking about it doesn't tell you that the request is valid, then it is invalid.

N3 text <--> formulas

log:parsedAsN3: For a string, the formula you get by parsing it as a Notation3 document.

"<a> <b> <c>" log:parsedAsN3 {<a> <b> <c>}.

log:N3String: For a formula, a string which expressed that formula in N3.

{<a> <b> <c>} log:N3String "<a> <b> <c>".

(roughly.... The actual string generated may be a bit different.)


log:content: For a document, the string which was returned as the document contents when the URI of the resource was looked up.

<> log:content """<?xml version="1.0" encoding"utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="" lang="en-US"
   and so on...


           log:uri "".

Pulls aside the curtain, letting rules look at the actual text of the URI used to name something.

Automatic Fetch and Merge

The option -mode r tells cwm to automatically incorporate certain remote documents.

:capitalCities log:definitiveDocument 

tells cwm to use that document for information about the :capitalCities predicate

Automatic Schema Fetch and Merge

-mode s tells cwm to fetch "schema" documents.

For each term like <foo#bar>, the document <foo> is loaded and merged

Combined Example: States

We have this file:

    :MA     a :USState;
         :borderstate :CT,
         :capital uscity:bostonma;
         :code "MA";
         :name "Massachusetts";
         :region :NewEngland .

Combined Example: Query

This query:

{"MA"^state:code.state:borderstate^city:state city:name ?n}

Command line:

cwm --mode=rse --think

Combined Example: Results


More log:definitiveDocument

:capitalCities log:definitiveDocument 

also says that all true statements using :capitalCities are made in that document.

We could use this to query "what states are NOT adjacent to MA", which requires knowing our adjacency knowledge is complete.


More Information

Creative Commons License
This work is licensed under a Creative Commons Attribution - Non-Commercial - No Derivatives 2.5 License.