Title: Object Oriented Rules and Inheritance
1Object Oriented Rules and Inheritance
- FabrÃcio Teles Marcos Aurélio, August 2006
2Outline
- Purely Relational Rule Languages x Purely Object
Oriented Languages - Hybrid Object Oriented Rule Languages
- Inheritance Taxonomy
- Yangs Inheritance Postulates
- CHORD Extending CHR? with High-Order Object
Oriented Constraints - Case Study Triangram
3Knowledge Representation and Rule Based Languages
- Strong points
- provides built-in FOL deduction with negation as
failure - the most powerful and versatile inference
mechanism - with formal semantics
- provides bases for implementation of other
mechanisms like abduction, induction, planning,
non-monotonic reasoning - uses rules that
- are intuitive and modular
- preserve the truth (detachment)
- can be acquired by machine learning
- Weak points
- non-intuitive codification of the terminological
and procedural knowledge - poor facility to structure complex entities
- limitations of the tools and methodologies of
distributed development on a large scale - poor standardization
- few non-AI computational services, libraries
available for reuse - limited interoperability with other languages
4Knowledge Representation and Rule Based Languages
- Rules often represent expert knowledge in a
natural way (If...Then) - Rules represent modular knowledge
X
Y (? ? X) ? Y ? Y (? ? X) ? Y X
5Knowledge Representation and Object Oriented
Languages
- Strong points
- intuitive codification of the terminological and
procedural knowledge - facilities to structure complex entities
- tools and methodologies of distributed
development on a large scale consolidated and
very spread out - API for the most varied computational services
and languages - emphasis in the standardization, interoperability
and reuse
- Weak points
- no built-in general purpose inference machine
- any mechanism of inference beyond inheritance
must be implemented from scratch - codifies behaviors procedurally
- no techniques to learn objects from data
- neither complete nor standard declarative formal
semantics
6Hybrid Object OrientedRule Languages
- OO Requirements
- Object identity
- Complex Objects
- Classes
- Encapsulation
- Inheritance
- Overriding and Overloading
- Reasoning Requirements
- Declarative Language
- Predicates with logical variables
- Automatic deduction
- Formal semantics
- Computational completeness
Hybrid Rules Objects
7Rules and objects how integrate?
Object Oriented Systems
Rule Based Systems
8Integrating rules with objectssyntactic
alternatives
- Hybrid system object oriented system in which
- procedural methods of the classes are substituted
by encapsulated rules bases, or - Hybrid system rule based system in which
- instances of logical terms in the facts base are
substituted by - objects instances of a hierarchy of classes
- logical terms in the premises and conclusions of
the rules are substituted by - object patterns with logical variables in the
place of - object names and attribute values
- possibly also name of classes and attributes
9Substitute procedural methodsby encapsulated
rule bases
Classes Hierarchy
Object Base
10Substitutefact base byobject base
11F-Logic
- Frame Logic is a deductive object oriented
database language - Syntactical integration between Object Oriented
Paradigm and Rules - Combines rules expressivity and declarative
semantics with object oriented concepts. - Implements Structural and Behavioral Inheritance
with support for Single-source Multiple
Inheritance - Flora is an implementation for F-logic over XSB
Prolog
12Flora Metamodel
Sequential Transaction Frame Logic Programming
Frame Logic Programming
Sequential Transaction Logic Programming
General Logic Programming
Hilog Logic Programming
Definite Logic Programming
13HiLog Metamodel
closure(R)(X,Y) - R(X,Y). closure(R)(X,Y) -
R(X,Z), closure(R)(Z,Y).
14F-Logic Metamodel
john person, johnage -gt 31, johnchildren -gtgt
bob,mary
john personage -gt 31, children -gtgt bob,mary
15F-Logic Metamodel
john.age 31, john..children bob,
john..children mary
16F-Logic Metamodel
17F-Logic Metamodel
18F-Logic Metamodel
19F-Logic Metamodel
20F-Logic Metamodel
21F-Logic Metamodel
22F-Logic OO Concepts
23F-Logic Program Example with Rules
Triangram Rules - Poligon Regularity
XirregularPolygon - Xtriangleside-gtgtS1,S2
and tnot(S1 S2) and S1.length \
S2.length. Xhypotenuse-gtHypotenuse -
Xtriangle and X..triangleSideposition-gtb
ase, side-gtBase and X..triangleSideposit
ion-gtleft, side-gtLeft and
X..triangleSideposition-gtright, side-gtRight
and if (Base.length gt Left.length and
Base.length gt Right.length) then
Hypotenuse Base else if Left.length
gt Right.length then Hypotenuse
Left else Hypotenuse
Right.
Triangram Rule Segment Adjacency
Sadjacent-gtgtX - XsegmentLine, Xadjacent-gtgtS.
24Inheritance Taxonomy (1/4)
Inheritance
Structural
Behavioral
engineersalary(2009)?5000 ? johnengineer ? joh
nsalary(2009)?5000
flowerpetalColor?color ? mary
flower ? marypetalColor?color
code engineersalary(X)?V-VX2 ?
johnengineer ? johnsalary(X)?V-VX2
Values
Code
Signatures
Non-monotonic reasoning in FLORA-2, Kifer, 2005
25Code Inheritance Example
R1 _at_ softDeptbonus()-gtB -
softDepttotal-gtT, B T 100. R2 _at_ code
softDeptbonus1()-gtB - softDepttotal-gtT, B
T 100.
?- johnbonus()-gtB.B 1000.
?- johnbonus1()-gtB.B 3000.
26Inheritance Taxonomy (2/4)
class1att1?color ? class2att1?tree
? class2class1 ? objclass2att1?value ? value
color ? value tree
Inheritance
class1att1?color ? class2att1?integer
? class2class1 ? objclass2 ? objatt1?integer
Monotonic
Non-monotonic
27Inheritance Taxonomy (3/4)
Inheritance
- humanlegs?2 ? policehasGun?true ?
shaftpolice ? shafthuman - ?
- shaftlegs?2 ? shafthasGun?true
flowerpetalColor?color ? mary
flower ? marypetalColor?color
Simple
Multiple
quakerpolicy?pacifist ? republicanpolicy?paci
fist ? nixon quaker ? nixon republican
? nixonpolicy-gtpacifist
quakerpolicy?pacifist ? republicanpolicy?hawk
? nixon quaker ? nixon republican
? nixon.policy unknown
Single Source
Multiple Source
Source Based
Value Based
28Inheritance Taxonomy (4/4)
Inheritance
elephantcolor?gray ? royalElephantcolor?whit
e ? royalElephant elephant ? clyde
royalElephant ? clydecolor?white
elephantcolor?gray ? royalElephantcolor?white
? royalElephant elephant ? clyde
royalElephant ? clydecolor?white ?
clydecolor?gray ? white gray
elephantcolor?gray ? royalElephantcolor?white
? royalElephant elephant ? clyde
royalElephant ? false
w/ Overriding
wo/ Overriding
29Source x Value Based Multiple Inheritance
c1
- Source-based c2..m and c3..m cancel each other
regardless of the set of values - Value-based c2..m and c3..m do not cancel each
other when they return the same set of values
(since there is no real conflict)
30Inheritance and Negation
- Non-Monotonic Inheritance (NMI) is a form of
default reasoning, - Thus, in order to provide semantics and implement
inheritance we can reuse another form of default
reasoning Negation As Failure (NAF) - F-Logic programs inheritance semantics reuses
General Logic Programs' Well-Founded Semantics
for NAF - Flora implements F-logic inheritance by reusing
SLG resolution of XSB Prolog
31Yangs Postulates
- Embodies the common intuition behind
non-monotonic multiple inheritance - An object model for some F-logic program is an
interpretation for every F-atom under a
three-valued semantics
32Main Concepts
- Inheritance Contexts
- cm is an inheritance context for o if o is a
member of class cand m?v is locally defined at c
for some value v - Local context
- sm is a local context if is a fact or if it is
derived through some rule - Overriding
- the inheritance context sm overrides cm for o
if c?s and sc - Inheritance Candidates
- cm is an inheritance candidate for o if it is
an inheritance context which is not overriden by
any other inheritance context - Inheritance Conflict
- An inheritance conflict occurs when there is more
than one inheritance candidate for inheriting
some field m to o
33Main Concepts
- Each concept previously defined can be
- Strong
- Some concept is strong if all of its
preconditions are true. - Weak
- Some concept is weak if some of its
preconditions is undefined.
34Example
- quakerpolicy?pacifist ? republicanpolicy?hawk
? - partypolicy-gtpacifist
- quaker party ? republican party
- nixon quaker ? nixon republican
- __________________________________________________
______
35Translation Schema
- Transforms every FLP F into a GLP G such that
- WFS(G) ? OptimisticObjectModel (F)
- Two parts
- Transformation rules, each one rewriting an FLP
fragment into a corresponding GLP fragment - Trailer GLP rules, concatenated at the end of the
transformation result
36Translation Schema
37Translation Schema Trailer Rules
38 XirregularPolygon - Xtriangleside-gtgtS1
length-gtL1, side-gtgtS2length-gtL12,
S1\S2, L1 \ L2.
isa(X,irregularPolygon) - isa(X,triangle),
mvd(X,side,S1), mvd(X,side,S2), fd(S1,length,
L1), fd(S2, length, L2), S1\S2, L1\L2.
39CHORDExtending CHR? with High-Order Object
Oriented Constraints
40CHORD Metamodel
41Reusing Yangs Approach on CHORD
- Translate the initial set of CHORD rules into a
set of CHR rules, transforming each F-atom in
some special CHR predicate - Attach some trailer rules implementing OO
semantics
42Current Implementation
- Current implementation supports only
- F-Atoms and conjunctive F-molecules
- F-Atoms are only allowed as rule defined
constraints
43Trailer Rules
- Alter normal CHR execution
- insert inheritance steps between constraint
simplification steps
Normal CHR
Constraint Simplification!
44CHORD Runtime State Machine
Process Overriding
Constraint Simplification
Remove Candidates
Block Multiple-source Inheritance
Propagate Class Structure
Inherit Candidates
Propagate Inheritance Candidates
45Example
- GOAL
- quakerpolicy?pacifist, republicanpolicy?
hawk, count?7, - partypolicy-gtpacifist,
- quaker party, republican party, nixon
quaker, nixon republican
46CHORD Runtime State Machine
CONSTRAINT STORE
quakerpolicy?pacifist, republicanpolicy?ha
wk, republicancount?7,
partypolicy-gtpacifist, quaker party,
republican party, nixon quaker, nixon
republican
Process Overriding
Constraint Simplification
Remove Candidates
Applies user rules, as in CHR? There are no rules
to fire
Block Multiple-source Inheritance
Propagate Class Structure
Inherit Candidates
Propagate Inheritance Candidates
47CHORD Runtime State Machine
CONSTRAINT STORE quakerpolicy?paci
fist, republicanpolicy?hawk,
republicancount?7, partypolicy-gtpacifist,
quaker party, republican party,
nixon quaker, nixon republican nixon
party
Process Overriding
Constraint Simplification
Remove Candidates
Block Multiple-source Inheritance
Propagate Class Structure
Propagates class structures, i.e. ab, bc ?
ac
Inherit Candidates
Propagate Inheritance Candidates
48CHORD Runtime State Machine
CONSTRAINT STORE quakerpolicy?paci
fist, republicanpolicy?hawk,
republicancount?7, partypolicy-gtpacifist,
quaker party, republican party,
nixon quaker, nixon republican nixon
party candidate_ifd(nixon,republican,count)
candidate_ifd(nixon,quaker,policy)
candidate_ifd(nixon,republican,policy)
candidate_ifd(nixon,party,policy)
candidate_ifd(republican,party,policy)
candidate_ifd(quaker,party,policy)
Process Overriding
Constraint Simplification
Remove Candidates
Block Multiple-source Inheritance
Propagate Class Structure
Calculate exhaustive list of inheritance
candidates, without taking overriding into
consideration
Inherit Candidates
Propagate Inheritance Candidates
49CHORD Runtime State Machine
Removes overriden inheritance candidates
Process Overriding
Constraint Simplification
Remove Candidates
CONSTRAINT STORE quakerpolicy?pacifi
st, republicanpolicy?hawk,
republicancount?7, partypolicy-gtpacifist,
quaker party, republican party, nixon
quaker, nixon republican nixon
party candidate_ifd(nixon,republican,count) candid
ate_ifd(nixon,quaker,policy) candidate_ifd(nixon,r
epublican,policy) candidate_ifd(nixon,party,policy
) candidate_ifd(republican,party,policy) candidate
_ifd(quaker,party,policy)
Block Multiple-source Inheritance
Propagate Class Structure
Inherit Candidates
Propagate Inheritance Candidates
50CHORD Runtime State Machine
Process Overriding
Constraint Simplification
Remove Candidates
CONSTRAINT STORE quakerpolicy?pacifist
, republicanpolicy?hawk, republicancount?7
, partypolicy-gtpacifist, quaker party,
republican party, nixon quaker, nixon
republican nixon party candidate_ifd(nixon,repu
blican,count) candidate_ifd(nixon,quaker,policy) c
andidate_ifd(nixon,republican,policy)
Block Multiple-source Inheritance
Propagate Class Structure
Removes inheritance Candidates causing multiple
inheritance conflicts
Inherit Candidates
Propagate Inheritance Candidates
51CHORD Runtime State Machine
Process Overriding
Constraint Simplification
Remove Candidates
Block Multiple-source Inheritance
CONSTRAINT STORE quakerpolicy?pacifist,
republicanpolicy?hawk, republicancount?7,
partypolicy-gtpacifist, quaker party,
republican party, nixon quaker, nixon
republican nixon party nixoncount?7
Propagate Class Structure
Define remaining candidates as local Definitions
Inherit Candidates
Propagate Inheritance Candidates
52CHORD Runtime State Machine
CONSTRAINT STORE quakerpolicy?pacifist,
republicanpolicy?hawk, republicancount?7,
partypolicy-gtpacifist, quaker party,
republican party, nixon quaker, nixon
republican nixon party nixoncount?7
Process Overriding
Constraint Simplification
Remove Candidates
Block Multiple-source Inheritance
Remove remaining intermediate helper constraints
Propagate Class Structure
Inherit Candidates
Propagate Inheritance Candidates
53CHORD Runtime State Machine
- Each state is associated to a sub-rule base
associated with a part of the trailer - Constraint Simplification state is associated to
user rules and goal - Since we can not control the order of the
execution of the rules, we need some special
strategy in order to implement state machines in
CHR
54Implementing State Machines in CHRD
- Associate each state to an unique constraint name
55Implementing State Machines in CHRD
- 2) Attach to each rule head of each state rule
base the constraint associated to the state
Propagate Class Structure
XZ, ZY gt X ? Y XY. SC, OS gt
OC. OC, CMgtR gt OMgtR.
XZ, ZY, class_structure() gt X ? Y
XY. SC, OS, class_structure() gt
OC. OC, CMgtR, class_structure() gt
OMgtR.
56Implementing State Machines in CHRD
- 3) For each state rule base, add an
simplification rule to simulate state
transitions(this rule should be the last one in
state rule base)
class_structure() ltgt propagate_candidates().
57Implementing State Machines in CHRD
XZ, ZY, class_structure() gt X ? Y
XY. SC, OS, class_structure() gt
OC. OC, CMgtR, class_structure() gt
OMgtR. class_structure() ltgt
propagate_candidates().
58Case Study Triangram
59Case Study - Triangram
- Simplification of the known Chinese game, the
Tangram - Tangram
- form complex figures using only 7 geometric 2D
figures, without having overlapping between them - It considers only the following types of
different parts - 5 isosceles triangles (2 small ones, 1 medium and
2 great ones) - 1 square
- 1 parallelogram
60Case Study - Triangram
61Case Study - Triangram
- Triangram
- Basic parts are only of a type triangles
- Uses three types of triangles
62Case Study - Triangram
- Triangram
- The objective is to form geometric figures from
the basic types of triangles - It's only allowed to form a restricted number of
types of figures the straight hexagons,
pentagons and the quadrilaterals
63Case Study - Triangram
- UML/OCL
- Representation of the world convex polygons
64Case Study - Triangram
- UML/OCL
- Representation of the world triangle
65Case Study - Triangram
- Implementation
- 28 CHORD rules
- The largest one has 19 contraints on its head
- About 125 constraints on goal
- Average instances of the problem last about 1h to
run
66Case Study - Triangram
- Implementation
- Goal
- Scenary being tested
- Class hierarchy
- Classes signatures
- Rules
- Invariants
- Methods
- Query ? propagation rules with the F-Atom of the
method in the body of the rule and pre-conditions
in the head - Transactional ? simpagation rules with
pre-conditions in the head, removals in
simplificateHead and pos-conditions in the body
67Case Study - Triangram
CHORD Implementation - Goal
main() ltgt true ...
Class Hierarchy
scaleneTriangletriangle, isoscelesTriangletr
iangle, equilateralTriangleisoscelesTriangle,
...
Scenary being tested
tri1 equilateralTriangle side -gtgt s11
adjacent -gtgt s12, adjacent -gtgt s13,
length -gt 3 , ... , ...
Class signatures
triangleside gtgt segmentLine,
triangleSide gtgt triangleSide,
hypotenuse gt segmentLine, ...
68Case Study - Triangram
Invariants
... XsegmentLineadjacent-gtgtS gt true
SsegmentLineadjacent-gtgtX. Xtriangle,
Xside-gtgtS1, Xside-gtgtS2, S1length-gtL1,
S2length-gtL2 gt ne(S1,S2), L1 L2
XregularPolygon. ...
69Case Study - Triangram
Query Methods
... Pol1 rectangle side -gtgt SidePol1 length
-gt L, adjacent -gtgt A1 length -gt LA1
, parts -gtgt P1, parts -gtgt P2 , Pol2
isoscelesTriangle triangleSide -gtgt
TS1 side -gt SidePol2 length
-gt L, adjacent -gtgt A2
length -gt LA1 , position -gt base
gt ne(P1,Pol2), ne(P2,Pol2)
Pol1isCompatible(Pol2, SidePol1,
SidePol2)-gtvoid. ...
70Case Study - Triangram
Transactional Methods
... Pol11triangleisCompatible(Pol2, SidePol1,
SidePol2)-gtvoid, Pol2 triangle,
... \ A11adjacent -gtgt SidePol1, A12adjacent
-gtgt SidePol1, A21adjacent -gtgt SidePol2,
A22adjacent -gtgt SidePol2 gt A11 ! A12, A21
! A22 A11adjacent -gtgt A22, A12adjacent -gtgt
A21, A21adjacent -gtgt A12, A22adjacent -gtgt
A11, Pol3 rectangleside -gtgt
A11,A12,A21,A22, parts -gtgt
Pol1,Pol2, ...