Reactive Transitive Closures with Drools (Backward Chaining)

31
1 Reasoning with Graphs House Location("Office", "House ") Location("Kitchen", "House") Location("Desk", "Office") Location("Chair", "Office") Location("Computer", "Desk") Location("Draw", "Desk") Location("Knife", "Kitchen") Location("Cheese", "Kitchen") Location("Key", "Draw") Tuesday, 2 July 13

description

Slides to go with the video here: http://www.youtube.com/watch?v=fCjIRVSRFvA This video shows backward chaining with Drools. It also demonstrates the concepts of reactive transitive closures.

Transcript of Reactive Transitive Closures with Drools (Backward Chaining)

Page 1: Reactive Transitive Closures with Drools (Backward Chaining)

1

Reasoning with Graphs

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 2: Reactive Transitive Closures with Drools (Backward Chaining)

2

Backward Chaining

query isContainedIn( String x, String y ) Location( x, y; ) or ( Location( z, y; ) and isContainedIn( x, z; ) )end

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 3: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining3

ksession.insert( new Location("Office", "House") );ksession.insert( new Location("Kitchen", "House") );ksession.insert( new Location("Knife", "Kitchen") );ksession.insert( new Location("Cheese", "Kitchen") );ksession.insert( new Location("Desk", "Office") );ksession.insert( new Location("Chair", "Office") );ksession.insert( new Location("Computer", "Desk") );ksession.insert( new Location("Draw", "Desk") );

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 4: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining4

rule "go" salience 10when $s : String( )then System.out.println( $s );end

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 5: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining5

rule "go1"when String( this == "go1" ) isContainedIn("Office", "House"; )then System.out.println( "office is in the house" );end

rule "go" salience 10when $s : String( )then System.out.println( $s );end

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 6: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining6

rule "go1"when String( this == "go1" ) isContainedIn("Office", "House"; )then System.out.println( "office is in the house" );end

rule "go" salience 10when $s : String( )then System.out.println( $s );end

query isContainedIn( String x, String y ) Location( x, y; ) or ( Location( z, y; ) and isContainedIn( x, z; ) )end

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 7: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining7

rule "go1"when String( this == "go1" ) isContainedIn("Office", "House"; )then System.out.println( "office is in the house" );end

rule "go" salience 10when $s : String( )then System.out.println( $s );end

ksession.insert( "go1" );ksession.fireAllRules();---go1office is in the house

query isContainedIn( String x, String y ) Location( x, y; ) or ( Location( z, y; ) and isContainedIn( x, z; ) )end

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 8: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining8

rule "go1"when String( this == "go1" ) isContainedIn("Office", "House"; )then System.out.println( "office is in the house" );end

rule "go" salience 10when $s : String( )then System.out.println( $s );end

ksession.insert( "go1" );ksession.fireAllRules();---go1office is in the house

query isContainedIn( String x, String y ) Location( x, y; ) or ( Location( z, y; ) and isContainedIn( x, z; ) )end

isContainedIn(x==Office, y==House)

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 9: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining9

rule "go1"when String( this == "go1" ) isContainedIn("Office", "House"; )then System.out.println( "office is in the house" );end

rule "go" salience 10when $s : String( )then System.out.println( $s );end

ksession.insert( "go1" );ksession.fireAllRules();---go1office is in the house

query isContainedIn( String x, String y ) Location( x, y; ) or ( Location( z, y; ) and isContainedIn( x, z; ) )end

Location(x==Office, y==House)isContainedIn(x==Office, y==House)

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 10: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining10

rule "go2"when String( this == "go2" ) isContainedIn("Draw", "House"; )then System.out.println( "Draw in the House" );end

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 11: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining11

rule "go2"when String( this == "go2" ) isContainedIn("Draw", "House"; )then System.out.println( "Draw in the House" );end

query isContainedIn( String x, String y ) Location( x, y; ) or ( Location( z, y; ) and isContainedIn( x, z; ) )end

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 12: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining12

rule "go2"when String( this == "go2" ) isContainedIn("Draw", "House"; )then System.out.println( "Draw in the House" );end

query isContainedIn( String x, String y ) Location( x, y; ) or ( Location( z, y; ) and isContainedIn( x, z; ) )end

ksession.insert( "go2" );ksession.fireAllRules();---go2Draw in the House

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 13: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining13

rule "go2"when String( this == "go2" ) isContainedIn("Draw", "House"; )then System.out.println( "Draw in the House" );end

query isContainedIn( String x, String y ) Location( x, y; ) or ( Location( z, y; ) and isContainedIn( x, z; ) )end

isContainedIn(x==Draw, y==House)

ksession.insert( "go2" );ksession.fireAllRules();---go2Draw in the House

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 14: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining14

rule "go2"when String( this == "go2" ) isContainedIn("Draw", "House"; )then System.out.println( "Draw in the House" );end

query isContainedIn( String x, String y ) Location( x, y; ) or ( Location( z, y; ) and isContainedIn( x, z; ) )end

Location(z==Office, y==House)isContainedIn(x==Draw, y==House)

ksession.insert( "go2" );ksession.fireAllRules();---go2Draw in the House

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 15: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining15

rule "go2"when String( this == "go2" ) isContainedIn("Draw", "House"; )then System.out.println( "Draw in the House" );end

query isContainedIn( String x, String y ) Location( x, y; ) or ( Location( z, y; ) and isContainedIn( x, z; ) )end

Location(z==Office, y==House)isContainedIn(x==Draw, z==Office)

isContainedIn(x==Draw, y==House)

ksession.insert( "go2" );ksession.fireAllRules();---go2Draw in the House

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 16: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining16

rule "go2"when String( this == "go2" ) isContainedIn("Draw", "House"; )then System.out.println( "Draw in the House" );end

query isContainedIn( String x, String y ) Location( x, y; ) or ( Location( z, y; ) and isContainedIn( x, z; ) )end

Location(z==Office, y==House)isContainedIn(x==Draw, z==Office)

Location(z==Kitchen, y==House)isContainedIn(x==Draw, z==Kitchen)

isContainedIn(x==Draw, y==House)

ksession.insert( "go2" );ksession.fireAllRules();---go2Draw in the House

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 17: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining17

rule "go2"when String( this == "go2" ) isContainedIn("Draw", "House"; )then System.out.println( "Draw in the House" );end

query isContainedIn( String x, String y ) Location( x, y; ) or ( Location( z, y; ) and isContainedIn( x, z; ) )end

isContainedIn(x==Draw, y==Office)

ksession.insert( "go2" );ksession.fireAllRules();---go2Draw in the House

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 18: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining18

rule "go2"when String( this == "go2" ) isContainedIn("Draw", "House"; )then System.out.println( "Draw in the House" );end

query isContainedIn( String x, String y ) Location( x, y; ) or ( Location( z, y; ) and isContainedIn( x, z; ) )end

Location(z==Desk, y==Office)isContainedIn(x==Draw, y==Office)

ksession.insert( "go2" );ksession.fireAllRules();---go2Draw in the House

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 19: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining19

rule "go2"when String( this == "go2" ) isContainedIn("Draw", "House"; )then System.out.println( "Draw in the House" );end

query isContainedIn( String x, String y ) Location( x, y; ) or ( Location( z, y; ) and isContainedIn( x, z; ) )end

Location(z==Desk, y==Office)isContainedIn(x==Draw, z==Desk)

isContainedIn(x==Draw, y==Office)

ksession.insert( "go2" );ksession.fireAllRules();---go2Draw in the House

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 20: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining20

rule "go2"when String( this == "go2" ) isContainedIn("Draw", "House"; )then System.out.println( "Draw in the House" );end

query isContainedIn( String x, String y ) Location( x, y; ) or ( Location( z, y; ) and isContainedIn( x, z; ) )end

isContainedIn(x==Draw, y==Desk)

ksession.insert( "go2" );ksession.fireAllRules();---go2Draw in the House

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 21: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining21

rule "go2"when String( this == "go2" ) isContainedIn("Draw", "House"; )then System.out.println( "Draw in the House" );end

query isContainedIn( String x, String y ) Location( x, y; ) or ( Location( z, y; ) and isContainedIn( x, z; ) )end

Location(x==Draw, y==Desk)isContainedIn(x==Draw, y==Desk)

ksession.insert( "go2" );ksession.fireAllRules();---go2Draw in the House

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 22: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining22

rule "go3"when String( this == "go3" ) isContainedIn("Key", "Office"; )then System.out.println( "Key in the Office" );end

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 23: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining23

rule "go3"when String( this == "go3" ) isContainedIn("Key", "Office"; )then System.out.println( "Key in the Office" );end

ksession.insert( "go3" );ksession.fireAllRules();---go3

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 24: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining24

rule "go3"when String( this == "go3" ) isContainedIn("Key", "Office"; )then System.out.println( "Key in the Office" );end

ksession.insert( "go3" );ksession.fireAllRules();---go3

ksession.insert( new Location("Key", "Draw") );ksession.fireAllRules();---Key in the Office

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 25: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining25

rule "go4"when String( this == "go4" ) isContainedIn(thing, "Office"; )then System.out.println( "thing " + thing + " is in the Office" );end

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 26: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining26

rule "go4"when String( this == "go4" ) isContainedIn(thing, "Office"; )then System.out.println( "thing " + thing + " is in the Office" );end

Out Var (unbound)

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 27: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining27

rule "go4"when String( this == "go4" ) isContainedIn(thing, "Office"; )then System.out.println( "thing " + thing + " is in the Office" );end

ksession.insert( "go4" );ksession.fireAllRules();---go4thing Key is in the Officething Computer is in the Officething Draw is in the Officething Desk is in the Officething Chair is in the Office

Out Var (unbound)

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 28: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining28

rule "go5"when String( this == "go5" ) isContainedIn(thing, location; )then System.out.println( "thing " + thing + " is in " + location );end

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 29: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining29

rule "go5"when String( this == "go5" ) isContainedIn(thing, location; )then System.out.println( "thing " + thing + " is in " + location );end

Out Var (unbound)

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 30: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining30

rule "go5"when String( this == "go5" ) isContainedIn(thing, location; )then System.out.println( "thing " + thing + " is in " + location );end

Out Var (unbound) Out Var

(unbound)

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13

Page 31: Reactive Transitive Closures with Drools (Backward Chaining)

Backward Chaining31

rule "go5"when String( this == "go5" ) isContainedIn(thing, location; )then System.out.println( "thing " + thing + " is in " + location );end

ksession.insert( "go5" );ksession.fireAllRules();---go5thing Knife is in Housething Cheese is in Housething Key is in Housething Computer is in Housething Draw is in Housething Desk is in Housething Chair is in Housething Key is in Officething Computer is in Officething Draw is in Officething Key is in Deskthing Office is in House

Out Var (unbound) Out Var

(unbound)

thing Computer is in Deskthing Knife is in Kitchenthing Cheese is in Kitchenthing Kitchen is in Housething Key is in Drawthing Draw is in Deskthing Desk is in Officething Chair is in Office

House

Location("Office", "House ")

Location("Kitchen", "House")

Location("Desk", "Office")

Location("Chair", "Office")

Location("Computer", "Desk")

Location("Draw", "Desk")

Location("Knife", "Kitchen")

Location("Cheese", "Kitchen")

Location("Key", "Draw")

Tuesday, 2 July 13