Notation3 Logic: A Practical Introduction
Dörthe Arndt and William Van Woensel
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 1
Outline
N3 and the Web
Reasoning with N3
Applications
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 2
Outline
N3 and the Web
Reasoning with N3
Applications
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 3
Semantic Web
Idea: provide a machine-understandable version of the Web
Why? A Semantic Web enables computers to use the Web for you.
How? Logical Representation of knowledge in graphs.
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 4
Resource Description Framework (RDF)
Simple triples:
:William :likes :spaghetti. -> “William likes spaghetti”
Blank nodes:
_:x :likes :spaghetti. -> “Someone likes spaghetti.”
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 5
Notation3 Logic
Extension of RDF
Rules
Quotations
Built-ins
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 6
Rules
{:William :likes :spaghetti.}=>{:William :likes :pizza.}.
“If William likes spaghetti, he also likes pizza.”
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 7
Universal Variables
{?x :likes :spaghetti.}=>{?x :likes :pizza.}.
“If someone likes spaghetti, this person also likes pizza.”
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 8
Quotations of formulae
:Doerthe :thinks {:William :likes spaghetti, :pizza}.
“Doerthe thinks that William likes spaghetti and pizza.”
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 9
N3 quotation vs RDF-star
N3 quotation is for graphs:
:Doerthe :thinks {:William :likes spaghetti, :pizza}.
RDF-star is for triples:
:Doerthe :thinks <<:William :likes spaghetti>>.
RDF-star and N3 quotation are compatible, some reasoners support both.
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 10
Built-ins
Predicates with special meanings
“Traditional” N3 built-ins:
https://www.w3.org/2000/10/swap/doc/CwmBuiltins
“New” built-ins are currently discussed.
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 11
Outline
N3 and the Web
Reasoning with N3
Applications
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 12
Reasoners
Classical Reasoners: EYE and Cwm
Recent developments: jen3, N03, …
We use EYE in our examples
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 13
Queries and Closure
N3 reasoners can give either
the deductive closure of an N3 graph or the result of a query
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 14
Deductive Closure
Given:
:William :likes :spaghetti.
{?x :likes :spaghetti.} => {?x :likes :pizza}.
Closure:
:William :likes :spaghetti.
{?x :likes :spaghetti.} => {?x :likes :pizza}.
:William :likes :pizza.
The deductive closure is the set of all triples which can be derived from a dataset
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 15
Query result
Given:
Facts and rules: William :likes :spaghetti.
Query: {?x :likes :spaghetti.} => {?x :likes :pizza}.
Result:
:William :likes :pizza.
A query is a special rule. Query reasoning provides all the results of the rule marked as query. The reasoning process can include other rules as well.
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 16
Example
Example execution in EYE (https://github.com/josd/eye)
From now on we will only consider deductive closures.
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 17
N3 Online editor
We provide an N3 online editor: http://ppr.cs.dal.ca:3002/n3/editor/
Spaghetti-example: http://ppr.cs.dal.ca:3002/n3/editor/s/4yxZnYnt
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 18
Existential rules
:William :likes :spaghetti.
{?x :likes :spaghetti.} => {?x :loves _:z}.
:William :loves _:z.
Link to example: http://ppr.cs.dal.ca:3002/n3/editor/s/E303UvWf
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 19
Rule-producing rules
:Researcher rdfs:subClassOf :Person.
{?x rdfs:subClassOf ?y.} => {{?z a ?x}=>{?z a ?y}}.
{?z a :Researcher}=>{?z a :Person}.
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 20
Forward vs. Backward Chaining (in EYE)
In N3 you can indicate how a rule should be applied:
Forward-chaining:
{?x :likes :Spaghetti}=>{?x :likes :Pizza}.
Backward-chaining:
{?x :likes :Pizza}<={?x :likes :Spaghetti}.
Link: http://ppr.cs.dal.ca:3002/n3/editor/s/rQEyElOC
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 21
Negation predicates
Different predicates which can express (scoped) negation as failure.
Example: log:collectAllIn
Built-in that collects all occurrences of a pattern in a list.
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 22
Link traversal
The built-in log:semantics allows us, to access the content behind a uri.
:doerthe foaf:knows git:william.n3 .
{ ?x foaf:knows ?y.
?y log:semantics ?content.
}=>{
?x :friendInfo ?content
}.
Try it: http://ppr.cs.dal.ca:3002/n3/editor/s/uNpYRH12
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 23
Graph operations
The predicate log:includes allows us to search for patterns in graphs.
{:william :likes :Spaghetti, :Pizza, :Fish.}
log:includes {:william :likes :Pizza}.
Examples:
http://ppr.cs.dal.ca:3002/n3/editor/s/HPX9ZCKY
http://ppr.cs.dal.ca:3002/n3/editor/s/BFg3hpTT
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 24
Proofs
[] a r:Proof, r:Conjunction;
r:component <#lemma1>;
r:gives {:William :likes :pizza.}.
<#lemma1> a r:Inference;
r:gives { :William :likes :pizza.};
r:evidence ( <#lemma2> );
r:rule <#lemma3>.
<#lemma2> a r:Extraction;
r:gives { :William :likes :spaghetti.};
r:because [ a r:Parsing; r:source <William.n3>].
<#lemma3> a r:Extraction;
r:gives { {?x_0_1 :likes :spaghetti} => {?x_0_1 :likes :pizza}.};
r:because [ a r:Parsing; r:source <spaghetti_rule.n3>].
N3 reasoners
produce proofs.
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 25
Outline
N3 and the Web
Reasoning with N3
Applications
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 26
Using N3 as Lingua Franca for Clinical Decision Support
Interoperable Electronic Health Records (EHR)
⇒ N3 does not suffer impedance mismatch
⇒ Use quoted graphs in N3
{ :patientY :has :viral_pneumonia }
:diagnosed_by :doctorX ;
:diagnosed_on "22-02-2022" .
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 27
Model-driven UIs using N3 + HL7 FHIR
⇒ Generate UI for health data input1
diary_observations a fhir:PlanDefinition ;
fhir:PlanDefinition.action ( [
fhir:PlanDefinition.action.definitionUri :diagnose_cough_wheezing_stridor ] ... ) ]
:diagnose_cough_wheezing_stridor a fhir:ActivityDefinition ;
fhir:ActivityDefinition.title "Is your cough, wheezing or stridor less, the same ... " ;
fhir:ActivityDefinition.observationResultRequirement [
fhir:ObservationDefinition.code :code_cough ; …
fhir:ObservationDefinition.permittedDataType :dt_int, :1_10_scale ] .
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 28
Model-driven UIs using N3 + HL7 FHIR (2)
:tpl-int_range_field a tpl:UiTemplate ;
tpl:select :select-int_range_field
tpl:generate [
tpl:ui :ui-int_input ;
tpl:placeholders ('_code_' '_prefix_' '_id_' '_min_' '_max_' '_step_' '_suffix_’);
tpl:values ( ?code ?label ?id ?min ?max ?step ?range )
] .
{ ?req fhir:permittedDataType :dt_int .
?range!fhir:low fhir:value ?min .
?range!fhir:high fhir:value ?max .. }
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 29
Introspection and Rule Generation
{ ?tpl a tpl:UiTemplate ;
tpl:select [ rdf:value ?selection ] ;
tpl:generate [
tpl:ui ?element ; tpl:code ?ui_code ;
tpl:placeholders ?placeholders ; tpl:values ?values ] .
( ?selection
{ ?element tpl:code ?ui_code .
( ?ui_code ?placeholders ?values ) string:replaceAll ?output }
) log:conjunction ?premise .
} => { ?premise => { ?element tpl:generated ?output } } .
replace placeholders with selector values
pattern-match with UiTemplate
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 30
Evidence-based CDS using N3
⇒ Use N3 rules to define a Finite State Machine
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 31
Evidence-based CDS: Finite State Machine
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 32
High-level State Transition Formalism
{ { ?composite a :CompositeTask ; state:in :Active ; :subTask ?sub .
<> log:notIncludes { ?prev :next ?sub }
} a state:Guard .
?sub state:in :Inactive
} state:transit { ?sub state:in :Ready } .
{ ?e1 :next ?e2 . ?e1 state:in :Completed } rdf:type state:Guard .
?e2 state:in :Inactive
} state:transit { ?e2 state:in :Ready } ;
state:reason :readyNextOfCompletedEntity .
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 33
Using Linear Logic for State Transitions
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 34
Introspection and Rule Generation (2)
{ ?premise state:transit ?conclusion ;
log:includes { ?target state:in ?oldState . ?guard a state:Guard } .
?conclusion log:includes { ?target state:in ?newState } .
?premise state:reason ?reason .
( { ?guard a log:StableTruth } { ?target state:in ?oldState } )
log:conjunction ?newPremise .
( ?conclusion
{ [ a state:Log ; state:target ?target ; state:reason ?reason ;
state:from ?oldState ; state:to ?newState ; state:time ?now ] }
) log:conjunction ?newConclusion
} => { ?newPremise log:becomes ?newConclusion } .
pattern-match with state-transition rule
construct new premise
construct new conclusion
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 35
Outline
N3 and the Web
Reasoning with N3
Applications
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 36
Can your use case benefit from N3?
Try it: http://ppr.cs.dal.ca:3002/n3/editor/
Join the W3C Community group: https://www.w3.org/community/n3-dev/
Notation3 Logic: A practical Introduction
Dörthe Arndt and William Van Woensel
RuleML Webinar 23.02.2022
�Slide 37