Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric...

38
Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium

Transcript of Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric...

Page 1: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Using Semantic Web Data: Inference

Lalana Kagal

Decentralized Information Group

MIT CSAIL

Eric Prud'hommeaux

Engineer

World Wide Web Consortium

Page 2: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Why do we need Rules on the Web?

• The Semantic Web provides a unifying representation of richly structured data

• Adding logic to the Web implies the use of rules to make inferences, choose courses of action, and answer questions

• This logic must be powerful enough to describe complex properties of objects but not so powerful that agents can be tricked by being asked to consider a paradox

Page 3: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

N3Logic: A Logic For the Web

• N3Logic is a logic that allows rules to be expressed in a Web environment.

• It extends RDF with syntax for – nested graphs– quantified variables – predicates for implication– accessing resources on the Web– functions including cryptographic, string, math

• The main goal of N3Logic is to be a minimal extension to the RDF data model such that the same language can be used for logic and data

Page 4: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

N3 Versus N3Logic

• N3 is a compact and readable alternative to RDF's XML syntax • N3 adds quoting to Turtle

– Turtle : <#SuperMan> <#can> <#fly>– N3: <#LoisLane> <#believes> { <#ClarkKent> <#is>

<#Superman> }– Does not imply that <#ClarkKent> <#can> <#fly>

• N3Logic uses N3 syntax and extends RDF with a vocabulary of predicates

• N3Logic allows rules to be integrated smoothly with RDF and provides certain essential built-in functions

Page 5: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

N3Logic Tools

• N3Logic reasoner – cwm

Page 6: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Installing cwm

• Prerequisites– Python: http://www.python.org/– cwm needs Python2.2 or later

• Download– http://www.w3.org/2000/10/swap/cwm.tar.gz

• Alias to executable– Set up an alias (.bat file, etc) to make the cwm command– In bash this is cwm="python /whereverYouPutThem/cwm.py"

Page 7: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Testing cwm• At the command prompt

> cwm http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/test1.n3

Page 8: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Executing Cwm• You can get a list of these by typing cwm --help

--rdf Input & Output in RDF/XML instead of n3

--n3 Input & Output in N3 from now on (Default)

--think as -rules but continue until no more rule matches

--filter=foo Read rules from foo, apply to store, replacing store with conclusions

• Examples– Combine data and find all deductions

• cwm foo.n3 bar.n3 --think – Convert from rdf/xml to rdf/n3

• cwm --rdf foo.rdf --n3

Page 9: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

If installed correctly• At the command prompt

> cwm --n3 http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/test1.n3

Page 10: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Scenario: Decentralized Access Control

• The DIG group has several online resources including pictures, papers, presentations, and proposals that are only accessible to members of the group and to people that members trust. The group has a webpage that lists its members using N3. The page identifies members by their Friend Of A Friend (FOAF) pages. Authentication in this scenario is via OpenID. DIG members provide their OpenID URI (usually the same as their work homepages) in their FOAF pages.

• Open http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/code/data.n3 for this data about the DIG group

• Policy: Only group members are allowed to access group resources or people that group members know

Page 11: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Scenario: Decentralized Access Control

• For simplicity, the data that we check our demo policies against also include a test request for a DIG resource

:Req1 a air:Request;

foaf:openid <http://auth.mit.edu/oshani>;

air:resource <http://dig.csail.mit.edu/proposals/nsf.tex/>.

Page 12: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

N3 Overview (RDF Extension)• Quoted formulae

– b:mary says { j:Joe foaf:member :DIG } .

• Universally quantified formulas– @forAll X. { X a Man } log:implies { X a Mortal }.– shorthand notation ?X

{ ?X a Man } log:implies { ?X a Mortal }.

• Existential variables or blank nodes– @forSome :X. j:Joe foaf:knows :X. :X foaf:name "Fred" .– shorthand notation

j:Joe foaf:knows [ foaf:name "Fred" ] .

Page 13: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

N3 Overview (RDF Extension)• Additional builtins with URI references defined in

– log: http:// www.w3.org/2000/10/swap/log#

– crypto: http://www.w3.org/2000/10/swap/crypto#

– list: http://www.w3.org/2000/10/swap/list#

– math: http://www.w3.org/2000/10/swap/math#

– os: http://www.w3.org/2000/10/swap/os#

– string: http://www.w3.org/2000/10/swap/string#

– time: http://www.w3.org/2000/10/swap/time#

• N3Logic extends RDF with variables and nested graphs to enable the descriptions of rules

• shorthand notation for log:implies is => • { A } => { B } where A and B are sets of triples

Page 14: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Developing First Rule

• RULE: A member of DIG is a user in the memberlist of DIG@forAll :MEMBER.

{ :DIG foaf:member :MemberList.

:MEMBER air:in :MemberList. } => {:MEMBER foaf:member :DIG}.

Page 15: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Visualising First Rule

• Open http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/rule1.n3

in any editor

Page 16: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Executing First Rule

• Execute cwm with rule1.n3 and data.n3> cwm http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-

code/data.n3 http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/rule1.n3 --think

Page 17: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Executing First Rule

• Produces too much information• Need to filter results• Only display those results generated by the rule

> cwm http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/data.n3 http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/rule1.n3 --think --filter=”http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/rule1.n3”

Page 18: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Executing First Rule

Page 19: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Graph Functions

• access a file and store its N3 representation in a variable

– <file> log:semantics <variable>

• check if one formula can be derived from another formula

– <formula> log:includes <formula>

• scoped negation as failure of log:includes

– <formula> log:notIncludes <formula>

• thinking inside

– <formula> log:conclusion <formula>

• Merging a list of formulae

– ( <formula1> <formula2> ) log:conjunction <formula3>

Page 20: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Developing Second Rule

• Read information from http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/data.n3

• Print all instances of foaf:Organizations and their foaf:names

Page 21: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Developing Second Rule

@prefix log: <http://www.w3.org/2000/10/swap/log#> .

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .@prefix foaf: <http://xmlns.com/foaf/0.1/> .@prefix : <http://dig.csail.mit.edu/data#> .

@forAll :F, :G, :N.

{ <http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/data.n3> log:semantics :F. :F log:includes { :G a foaf:Organization; foaf:name :N }.} => { :G a foaf:Organization; foaf:name :N }.

Page 22: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Executing Second Rule

• Execute this rule and see if it works> cwm http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/rule2.n3 --think --filter="http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/rule2.n3"

Page 23: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Executing Second Rule

Page 24: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Developing Third Rule

• Access the data file and see all attributes passed along with the Request

• A ValidRequest is one that has both the requester's openID URI as well as a requested resource that is a DIG resource

Page 25: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Developing Third Rule

@forAll :F, :REQ, :REQUESTER, :RESOURCE.

{ <http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/data.n3> log:semantics :F.

:F log:includes

{ :REQ a air:Request;

foaf:openid :REQUESTER;

air:resource :RESOURCE }.

:F log:includes { :DIG :owns :RESOURCE }.

} => { :REQ a air:ValidRequest }.

Page 26: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Executing Third Rule

Page 27: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Developing Fourth Rule

• Add another rule to the policy; if the Request is valid, check if the requester is a member of the DIG group

• If the requester is a member of the DIG group, the request is compatible with the DIGPolicy

@forAll :F, :REQ, :REQUESTER, :RESOURCE, :MEMBERLIST, :MEMBER.

{ <http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/data.n3> log:semantics :F.

:REQ a air:ValidRequest.

:F log:includes { :REQ foaf:openid :REQUESTER.

:DIG foaf:member :MEMBERLIST.

:MEMBER air:in :MEMBERLIST.

:MEMBER a foaf:Person;

foaf:openid :REQUESTER }

} => { :REQ air:compatible-with :DIGPolicy }.

Page 28: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Executing Fourth Rule

> cwm http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/rule4.n3 --think --filter="http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/rule4.n3"

Page 29: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Executing Fourth Rule

Page 30: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Developing Fifth Rule

– Write a rule that • that merges two rules and data

– rule1.n3 that infers the list of members of DIG– rule6.n3 that infers that a request is valid if it for a DIG

resource and if it includes the openID URI of the requester

– data.n3 that includes the data of the DIG group and an example request

• and uses log:conclusion to think and execute rule1.n3 and rule6.n3 against the data to infer that the request is a PermittedRequest if it is valid and the requester is from the DIG group

Page 31: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Developing Fifth Rule

@forAll :D, :R1, :R6, :F, :G, :MEMBER, :REQ, :REQUESTER.

{ <http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/data.n3> log:semantics :D.

<http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/rule1.n3> log:semantics :R1.

<http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/rule6.n3> log:semantics :R6.

( :D :R1 :R6 ) log:conjunction :F.

:F log:conclusion :G.

:G log:includes { :REQ a air:ValidRequest;

air:requester :REQUESTER.

:MEMBER foaf:member :DIG.

:MEMBER foaf:openid :REQUESTER }

} => { :REQ a air:PermittedRequest }.

Page 32: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Executing Fifth Rule

> cwm http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/rule5.n3 --think --filter="http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/rule5.n3"

Page 33: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Executing Fifth Rule

Page 34: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

More N3Logic Builtins– crypto functions – md5, sign, and verify– math functions- cos, sin, greaterThan,

notGreaterThan, and difference– os functions for retrieving environment information –

argv, baseAbsolute, and baseRelative– string functions – concatenation, matches, and

startsWith– time functions – dayOfWeek, gmTime, and localTime

Page 35: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Developing Seventh Rule– Write a rule that when given a temperature in F

degrees as a commandline argument, converts it into Celcius degrees

– Use os functions to retrieve the commandline argument

• <arg number> os:argv <variable>• arguments passed using “--with”

– Use math functions to convert to celcius• ( <num1> <num2> ) math:difference <var1>• ( <num1> <num2> ) math:product <var1>

Page 36: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Developing Seventh Rule

@forAll :TF, :T1, :TC.{ "1" os:argv :TF. ( :TF 32 ) math:difference :T1. (:T1 "0.5555" ) math:product :TC} => { :TC :tempInCOf :TF }.

Page 37: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Executing Seventh Rule

> cwm http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/rule7.n3 --think --filter="http://dig.csail.mit.edu/2008/Talks/0518-SemTech-eplk/cwm-code/rule7.n3" --with 40

Page 38: Using Semantic Web Data: Inference Lalana Kagal Decentralized Information Group MIT CSAIL Eric Prud'hommeaux Engineer World Wide Web Consortium.

Cwm Resources

• Cwm tutorial– http://www.w3.org/2000/10/swap/doc/

• Commandline arguments– http://www.w3.org/2000/10/swap/doc/CwmHelp

• Builtin functions– http://www.w3.org/2000/10/swap/doc/Built-In

• N3Logic: A Logical Framework for the World Wide Web", Journal of Theory and Practice of Logic Programming (TPLP), Special Issue on Logic Programming and the Web– http://arxiv.org/abs/0711.1533