Reasoning with Rules SWRL as Example Jan Pettersen Nytun, UIA 1
JPN, UiA 2
What is a rule? Consist of premise and a conclusion. Meaning: In any situation where the premise applies the conclusion must also hold. premise conclusion JPN, UiA 3
Agenda Different Types of Reasoning Inductive reasoning Deductive reasoning Abductive reasoning Why rules? The Semantic Web Rule Language (SWRL) SWRL Types of Atoms SWRL Example SWRL Exercise 4
Inductive Reasoning From Wikipedia, the free encyclopedia the premises are viewed as supplying strong evidence for the truth of the conclusion. conclusion of a deductive argument is certain, the truth of the conclusion of an inductive argument is probable, based upon the evidence given. premise conclusion Not as strong as for deductive reasoning JPN, UiA 5
P S O Inductive Reasoning From Wikipedia, the free encyclopedia the premises of an inductive logical argument indicate some degree of support for the conclusion but do not entail it... derives general principles from specific observations based on observations premise conclusion Likely to be true JPN, UiA 6
Inductive Reasoning Example Many observations indicate that humans eventually dies, i.e., humans are mortals. Human(x) Mortal(x) JPN, UiA 7
Deductive Reasoning also Called Deductive Logic, Logical Deduction From Wikipedia, the free encyclopedia If all premises are true, and the rules of deductive logic are followed, then the conclusion reached is necessarily true. In inductive reasoning, the conclusion is reached by generalizing or extrapolating from specific cases to general rules, i.e., there is uncertainty. However, induction used in mathematical proofs is actually a form of deductive reasoning. JPN, UiA 8
Deductive Reasoning Example All men are mortal. Socrates is a man. ------------------------------ Therefore, Socrates is mortal. JPN, UiA 9
Deductive Reasoning Example in First Order Predicate Logic All men are mortal. Socrates is a man. ------------------------------ Therefore, Socrates is mortal. x.man(x) Mortal(x) -- All men are mortal Man(Socrates) -- Socrates is a man -------------------------------------------------------------------------- Man(Socrates) Mortal(Socrates). -- Socrates is mortal JPN, UiA 10
From Wikipedia, the free encyclopedia JPN, UiA 11
[https://explorable.com/inductive-reasoning] Theories have to be tested and hypotheses answered before the scientific community accepts them as truth. JPN, UiA 12
JPN, UiA 13 Abductive Reasoning From Wikipedia, the free encyclopedia Example: The grass is wet; if it rained last night, then it would be unsurprising that the grass is wet. Therefore, by abductive reasoning, the possibility that it rained last night is reasonable. RaindLastNight GrassIsWeet Some other process could have also resulted in a wet grass, such as sprinklers. Consequently, abducing that it rained last night from the observation of wet grass can lead to a false conclusion. SprinklerWasOn GrassIsWeet
Abductive Reasoning Continues Inference to the best explanation. Given a true conclusion and a rule, it attempts to select some possible premises that, if true also, can support the conclusion, though not uniquely. RaindLastNight GrassIsWeet SprinklerWasOn GrassIsWeet Can be used to develop a hypothesis, which in turn can be tested by additional reasoning or data. JPN, UiA 14
Abductive Reasoning Example ref.: https://www.quora.com/what-is-a-good-example-of-abductive-reasoning The doctor hears her patients symptoms, including the regular shortness of breath on cold days and when exercising and abduces that the best explanation of these symptoms is that her patient is an asthma sufferer. The scientist observes the test tube and sees the chemical turn purple. She abduces that either there is potassium in the sample or her colleague is playing yet another prank on her. JPN, UiA 15
Agenda Different Types of Reasoning Inductive reasoning Deductive reasoning Abductive reasoning Why rules? The Semantic Web Rule Language (SWRL) SWRL Types of Atoms SWRL Example SWRL Exercise 16
In some cases we need both Structure and Rules JPN, UiA 17
Example of rule using The Semantic Web Rule Language (SWRL): hasparent(?x,?parent) hasbrother(?parent,?uncle) hasuncle(?x,?uncle) Some statements cannot be expressed in OWL. Modeling constructs of OWL not always adequate or most desirable. Knowledge Representation, Part II, JPN, UiA 18
Agenda Different Types of Reasoning Inductive reasoning Deductive reasoning Abductive reasoning Why rules? The Semantic Web Rule Language (SWRL) SWRL Types of Atoms SWRL Example SWRL Exercise 19
Ref.: https://github.com/protegeproject/swrlapi/wiki/swrllanguagefaq The Semantic Web Rule Language (SWRL) An expressive OWL-based rule language. SWRL allows users to write rules that can be expressed in terms of OWL concepts to provide more powerful deductive reasoning capabilities than OWL alone. JPN, UiA 20
Ref.: https://github.com/protegeproject/swrlapi/wiki/swrllanguagefaq SWRL Rule head body atom ^ atom... atom ^ atom body and head consist of positive conjunctions of atoms (only AND between atoms) Atom p(arg1, arg2,... argn) p is a predicate symbol; arg1, arg2,..., argn are the terms of the expression. JPN, UiA 21
Ref.: https://github.com/protegeproject/swrlapi/wiki/swrllanguagefaq All variables in SWRL are treated as universally quantified ( ), with their scope limited to a given rule. E.g., given: hasparent(?x,?parent) hasbrother(?parent,?uncle) hasuncle(?x,?uncle) This rule applies for all?x, all?parent and all?uncle. JPN, UiA 22
Agenda Different Types of Reasoning Inductive reasoning Deductive reasoning Abductive reasoning Why rules? The Semantic Web Rule Language (SWRL) SWRL Types of Atoms SWRL Example SWRL Exercise 23
Ref.: https://github.com/protegeproject/swrlapi/wiki/swrllanguagefaq SWRL provides seven types of atoms: Class Atoms Individual Property atoms Data Valued Property atoms Different Individuals atoms Same Individual atoms Built-in atoms Data Range atoms JPN, UiA 24
Ref.: https://github.com/protegeproject/swrlapi/wiki/swrllanguagefaq Class Atom OWL named class or class expression and a single argument representing an OWL individual Examples: Person(?p) Man(Fred) Man(?p) -> Person(?p) JPN, UiA 25
Example of Class Expression (haschild >= 1)(?x) -> Parent(?x) JPN, UiA 26
Ref.: https://github.com/protegeproject/swrlapi/wiki/swrllanguagefaq Individual Property Atom OWL object property and two arguments representing OWL individuals. Examples: hasbrother(?x,?y) hassibling(fred,?y) Person(?p) ^ hassibling(?p,?s) ^ Man(?s) -> hasbrother(?p,?s) JPN, UiA 27
Ref.: https://github.com/protegeproject/swrlapi/wiki/swrllanguagefaq Data Valued Property OWL data property and two arguments, the first representing an OWL individual, and the second a data value. Examples: hasage(?x,?age) hasheight(fred,?h) hasage(?x, 232) hasname(?x, "Fred") Person(?p) ^ hascar(?p, true) -> Driver(?p) Person(Fred) ^ hascar(fred, true) -> Driver(Fred) JPN, UiA 28
Ref.: https://github.com/protegeproject/swrlapi/wiki/swrllanguagefaq Different Individuals Atom Arguments representing OWL individuals. Examples: differentfrom(?x,?y) differentfrom(fred, Joe) Same Individual Atom Arguments representing OWL individuals. Examples: sameas(?x,?y) sameas(fred, Freddy) JPN, UiA 29
Ref.: https://github.com/protegeproject/swrlapi/wiki/swrllanguagefaq Data Range Atom A datatype name or a set of literals and a single argument representing a data value. Examples: xsd:int(?x) [3, 4, 5](?x)?x is a variable representing a data value. JPN, UiA 30
Ref.: https://github.com/protegeproject/swrlapi/wiki/swrllanguagefaq Built-In Atom SWRL support user-defined built-ins. A built-in is a predicate that takes one or more arguments and evaluates to true if the arguments satisfy the predicate. SWRL contained many built-ins. Example - Person with an age of greater than 17 is an adult is: : Person(?p) ^ hasage(?p,?age) ^ swrlb:greaterthan(?age, 17) -> Adult(?p) (swrlb is a namespace) JPN, UiA 31
Ref.: https://github.com/protegeproject/swrlapi/wiki/swrllanguagefaq A rule that uses a core SWRL string built-in to determine if a person's telephone number starts with the international access code "+" can be written as follows: Person(?p) ^ hasnumber(?p,?number) ^ swrlb:startswith(?number, "+") hasinternationalnumber(?p, true) JPN, UiA 32
Ref.: https://github.com/protegeproject/swrlapi/wiki/swrllanguagefaq Rectangle(?r) ^ haswidthinmeters(?r,?w) ^ hasheightinmeters(?r,?h) ^ swrlb:multiply(?areainsquaremeters,?w,?h) hasareainsquaremeters(?r,?areainsquaremeters) JPN, UiA 33
Agenda Different Types of Reasoning Inductive reasoning Deductive reasoning Abductive reasoning Why rules? The Semantic Web Rule Language (SWRL) SWRL Types of Atoms SWRL Example SWRL Exercise 34
DL and SWRL has a big overlap Example: If a person is the author of a book then she is a (member of the class) book author. First Order Predicate Logic: x.person(x) y.authorof(x,y) Book(y) Bookauthor(x) Description Logic: Person and authorof some Book SWRL: Person(?x) ^ authorof(?x,?y) ^Book(?y) -> BookAuthor(?x) JPN, UiA 35
Example in Protégé JPN, UiA 36
:authorof rdf:type owl:objectproperty. :Book rdf:type owl:class. :Person rdf:type owl:class. :BookAuthor rdf:type owl:class ; owl:equivalentclass [ owl:intersectionof ( :Person [ rdf:type owl:restriction ; owl:onproperty :authorof ; owl:somevaluesfrom :Book ] ) ; rdf:type owl:class ]. :adollshouse rdf:type owl:namedindividual, :Book. :ibsen rdf:type owl:namedindividual, :Person ; :authorof :adollshouse, :peergynt. Ontology Used :notanauthorperson rdf:type owl:namedindividual, :Person. :peergynt rdf:type owl:namedindividual, :Book. JPN, UiA 37
DL reasoner infer that ibsen is a book author JPN, UiA 38
Make SWRL rule in Protégé JPN, UiA 39
JPN, UiA 40
Transfer SWRL rule to rule engine After pressing JPN, UiA 41
Run SWRL rule JPN, UiA 42
See result of reasoning JPN, UiA 43
JPN, UiA 44 Result of SWRL reasoning
Agenda Different Types of Reasoning Inductive reasoning Deductive reasoning Abductive reasoning Why rules? The Semantic Web Rule Language (SWRL) SWRL Types of Atoms SWRL Example SWRL Exercise 45
@prefix : <http://www.uia.no/veggi#>. @prefix owl: <http://www.w3.org/2002/07/owl#>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. @prefix xml: <http://www.w3.org/xml/1998/namespace>. @prefix xsd: <http://www.w3.org/2001/xmlschema#>. @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>. @base <http://www.uia.no/veggi>. <http://www.uia.no/veggi> rdf:type owl:ontology. ################################################################# # Object Properties ################################################################# ### http://www.uia.no/veggi#contains :contains rdf:type owl:objectproperty. ### http://www.uia.no/veggi#dislikes :dislikes rdf:type owl:objectproperty. ### http://www.uia.no/veggi#ordered :ordered rdf:type owl:objectproperty. ################################################################# # Classes ################################################################# ### http://www.uia.no/veggi#bakedsalmon :BakedSalmon rdf:type owl:class ; rdfs:subclassof :Dish, [ rdf:type owl:restriction ; owl:onproperty :contains ; owl:somevaluesfrom :SalmonProduct ]. ### http://www.uia.no/veggi#chickpeas :Chickpeas rdf:type owl:class ; rdfs:subclassof :VeggiProduct. ### http://www.uia.no/veggi#dish :Dish rdf:type owl:class. ### http://www.uia.no/veggi#falafel :Falafel rdf:type owl:class ; rdfs:subclassof :Dish, [ rdf:type owl:restriction ; owl:onproperty :contains ; owl:somevaluesfrom :Chickpeas ]. Exercise this ontology is given ### http://www.uia.no/veggi#fishproduct :FishProduct rdf:type owl:class ; rdfs:subclassof :Product. ### http://www.uia.no/veggi#person :Person rdf:type owl:class. ### http://www.uia.no/veggi#product :Product rdf:type owl:class. ### http://www.uia.no/veggi#salmonproduct :SalmonProduct rdf:type owl:class ; rdfs:subclassof :FishProduct. ### http://www.uia.no/veggi#unhappy :Unhappy rdf:type owl:class. ### http://www.uia.no/veggi#vegetarian :Vegetarian rdf:type owl:class. ### http://www.uia.no/veggi#veggiproduct :VeggiProduct rdf:type owl:class ; rdfs:subclassof :Product. ################################################################# # Individuals ################################################################# ### http://www.uia.no/veggi#chickpeas1 :chickpeas1 rdf:type owl:namedindividual, :Chickpeas. ### http://www.uia.no/veggi#dish1bakedsalmon :dish1bakedsalmon rdf:type owl:namedindividual, :BakedSalmon ; :contains :salmon1. ### http://www.uia.no/veggi#disk2bakedsalmon :disk2bakedsalmon rdf:type owl:namedindividual, :BakedSalmon ; :contains :salmon2. ### http://www.uia.no/veggi#disk3falafel :disk3falafel rdf:type owl:namedindividual, :Falafel ; :contains :chickpeas1. ### http://www.uia.no/veggi#janenotveggi :janenotveggi rdf:type owl:namedindividual, :Person ; :ordered :dish1bakedsalmon. ### http://www.uia.no/veggi#salmon1 :salmon1 rdf:type owl:namedindividual, :SalmonProduct. ### http://www.uia.no/veggi#salmon2 :salmon2 rdf:type owl:namedindividual, :SalmonProduct. ### http://www.uia.no/veggi#tomveggi :tomveggi rdf:type owl:namedindividual, :Person, :Vegetarian ; :ordered :disk2bakedsalmon, :disk3falafel. ### Generated by the OWL API (version 4.2.5.20160517-0735) https://github.com/owlcs/owlapi JPN, UiA 46
Ontology loaded into Protégé JPN, UiA 47
JPN, UiA 48
JPN, UiA 49
JPN, UiA 50
Even if no rule is specified the inference engine will still do some inferencing: JPN, UiA 51
Task 1: Every vegetarian dislikes all fish products. In effect all individuals of type Vegetarian will dislike all individuals of type FishProduct (and all its subclasses). E.g.: JPN, UiA 52
Task 1 Solution: (1) Every vegetarian dislikes all fish products. Vegetarian(?x) ^ FishProduct(?y) -> dislikes(?x,?y) JPN, UiA 53
Task 2: Anyone who ordered a dish that contains something he or she dislikes is unhappy. JPN, UiA 54
Task 2 solution: Anyone who ordered a dish that contains something he or she dislikes is unhappy. JPN, UiA 55
Task 3: Everything that can be ordered as a dish actually is a dish. Add the following to test: JPN, UiA 56
Task Solution 3: Everything that can be ordered as a dish actually is a dish. JPN, UiA 57
References Foundations of Semantic Web Technologies, Pascal Hitzler, Markus Krötzsch, Sebastian Rudolph, Chapman & Hall/CRC, 2009 http://www.powershow.com/view4/5a7f2a- OGRlM/SWRL_Semantic_Web_Rule_Language_powerpoint_ppt_presentation Help SWRL Protégé 5: https://github.com/protegeproject/swrlapi/wiki Jan Pettersen Nytun, UiA, page 58