Successfully reported this slideshow.
1
Reasoning with Graphs
House
Location("Office",
"House ")
Location("Kitchen",
"House")
Location("Desk",
"Office")
Locatio...
2
Backward Chaining
query isContainedIn( String x, String y )
Location( x, y; )
or
( Location( z, y; ) and isContainedIn( ...
Backward Chaining
3
ksession.insert( new Location("Office", "House") );
ksession.insert( new Location("Kitchen", "House") ...
Backward Chaining
4
rule "go" salience 10
when
$s : String( )
then
System.out.println( $s );
end
House
Location("Office",
...
Backward Chaining
5
rule "go1"
when
String( this == "go1" )
isContainedIn("Office", "House"; )
then
System.out.println( "o...
Backward Chaining
6
rule "go1"
when
String( this == "go1" )
isContainedIn("Office", "House"; )
then
System.out.println( "o...
Backward Chaining
7
rule "go1"
when
String( this == "go1" )
isContainedIn("Office", "House"; )
then
System.out.println( "o...
Backward Chaining
8
rule "go1"
when
String( this == "go1" )
isContainedIn("Office", "House"; )
then
System.out.println( "o...
Backward Chaining
9
rule "go1"
when
String( this == "go1" )
isContainedIn("Office", "House"; )
then
System.out.println( "o...
Backward Chaining
10
rule "go2"
when
String( this == "go2" )
isContainedIn("Draw", "House"; )
then
System.out.println( "Dr...
Backward Chaining
11
rule "go2"
when
String( this == "go2" )
isContainedIn("Draw", "House"; )
then
System.out.println( "Dr...
Backward Chaining
12
rule "go2"
when
String( this == "go2" )
isContainedIn("Draw", "House"; )
then
System.out.println( "Dr...
Backward Chaining
13
rule "go2"
when
String( this == "go2" )
isContainedIn("Draw", "House"; )
then
System.out.println( "Dr...
Backward Chaining
14
rule "go2"
when
String( this == "go2" )
isContainedIn("Draw", "House"; )
then
System.out.println( "Dr...
Backward Chaining
15
rule "go2"
when
String( this == "go2" )
isContainedIn("Draw", "House"; )
then
System.out.println( "Dr...
Backward Chaining
16
rule "go2"
when
String( this == "go2" )
isContainedIn("Draw", "House"; )
then
System.out.println( "Dr...
Backward Chaining
17
rule "go2"
when
String( this == "go2" )
isContainedIn("Draw", "House"; )
then
System.out.println( "Dr...
Backward Chaining
18
rule "go2"
when
String( this == "go2" )
isContainedIn("Draw", "House"; )
then
System.out.println( "Dr...
Backward Chaining
19
rule "go2"
when
String( this == "go2" )
isContainedIn("Draw", "House"; )
then
System.out.println( "Dr...
Backward Chaining
20
rule "go2"
when
String( this == "go2" )
isContainedIn("Draw", "House"; )
then
System.out.println( "Dr...
Backward Chaining
21
rule "go2"
when
String( this == "go2" )
isContainedIn("Draw", "House"; )
then
System.out.println( "Dr...
Backward Chaining
22
rule "go3"
when
String( this == "go3" )
isContainedIn("Key", "Office"; )
then
System.out.println( "Ke...
Backward Chaining
23
rule "go3"
when
String( this == "go3" )
isContainedIn("Key", "Office"; )
then
System.out.println( "Ke...
Backward Chaining
24
rule "go3"
when
String( this == "go3" )
isContainedIn("Key", "Office"; )
then
System.out.println( "Ke...
Backward Chaining
25
rule "go4"
when
String( this == "go4" )
isContainedIn(thing, "Office"; )
then
System.out.println( "th...
Backward Chaining
26
rule "go4"
when
String( this == "go4" )
isContainedIn(thing, "Office"; )
then
System.out.println( "th...
Backward Chaining
27
rule "go4"
when
String( this == "go4" )
isContainedIn(thing, "Office"; )
then
System.out.println( "th...
Backward Chaining
28
rule "go5"
when
String( this == "go5" )
isContainedIn(thing, location; )
then
System.out.println( "th...
Backward Chaining
29
rule "go5"
when
String( this == "go5" )
isContainedIn(thing, location; )
then
System.out.println( "th...
Backward Chaining
30
rule "go5"
when
String( this == "go5" )
isContainedIn(thing, location; )
then
System.out.println( "th...
Backward Chaining
31
rule "go5"
when
String( this == "go5" )
isContainedIn(thing, location; )
then
System.out.println( "th...
Upcoming SlideShare
Loading in …5
×

Reactive Transitive Closures with Drools (Backward Chaining)

2,381 views

Published on

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.

  • Be the first to comment

  • Be the first to like this

Reactive Transitive Closures with Drools (Backward Chaining)

  1. 1. 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
  2. 2. 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
  3. 3. Backward Chaining 3 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
  4. 4. Backward Chaining 4 rule "go" salience 10 when $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
  5. 5. Backward Chaining 5 rule "go1" when String( this == "go1" ) isContainedIn("Office", "House"; ) then System.out.println( "office is in the house" ); end rule "go" salience 10 when $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
  6. 6. Backward Chaining 6 rule "go1" when String( this == "go1" ) isContainedIn("Office", "House"; ) then System.out.println( "office is in the house" ); end rule "go" salience 10 when $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
  7. 7. Backward Chaining 7 rule "go1" when String( this == "go1" ) isContainedIn("Office", "House"; ) then System.out.println( "office is in the house" ); end rule "go" salience 10 when $s : String( ) then System.out.println( $s ); end ksession.insert( "go1" ); ksession.fireAllRules(); --- go1 office 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
  8. 8. Backward Chaining 8 rule "go1" when String( this == "go1" ) isContainedIn("Office", "House"; ) then System.out.println( "office is in the house" ); end rule "go" salience 10 when $s : String( ) then System.out.println( $s ); end ksession.insert( "go1" ); ksession.fireAllRules(); --- go1 office 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
  9. 9. Backward Chaining 9 rule "go1" when String( this == "go1" ) isContainedIn("Office", "House"; ) then System.out.println( "office is in the house" ); end rule "go" salience 10 when $s : String( ) then System.out.println( $s ); end ksession.insert( "go1" ); ksession.fireAllRules(); --- go1 office 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
  10. 10. Backward Chaining 10 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
  11. 11. Backward Chaining 11 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
  12. 12. Backward Chaining 12 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(); --- go2 Draw 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
  13. 13. Backward Chaining 13 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(); --- go2 Draw 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
  14. 14. Backward Chaining 14 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(); --- go2 Draw 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
  15. 15. Backward Chaining 15 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(); --- go2 Draw 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
  16. 16. Backward Chaining 16 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(); --- go2 Draw 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
  17. 17. Backward Chaining 17 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(); --- go2 Draw 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
  18. 18. Backward Chaining 18 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(); --- go2 Draw 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
  19. 19. Backward Chaining 19 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(); --- go2 Draw 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
  20. 20. Backward Chaining 20 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(); --- go2 Draw 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
  21. 21. Backward Chaining 21 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(); --- go2 Draw 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
  22. 22. Backward Chaining 22 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
  23. 23. Backward Chaining 23 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
  24. 24. Backward Chaining 24 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
  25. 25. Backward Chaining 25 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
  26. 26. Backward Chaining 26 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
  27. 27. Backward Chaining 27 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(); --- go4 thing Key is in the Office thing Computer is in the Office thing Draw is in the Office thing Desk is in the Office thing 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
  28. 28. Backward Chaining 28 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
  29. 29. Backward Chaining 29 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
  30. 30. Backward Chaining 30 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
  31. 31. Backward Chaining 31 rule "go5" when String( this == "go5" ) isContainedIn(thing, location; ) then System.out.println( "thing " + thing + " is in " + location ); end ksession.insert( "go5" ); ksession.fireAllRules(); --- go5 thing Knife is in House thing Cheese is in House thing Key is in House thing Computer is in House thing Draw is in House thing Desk is in House thing Chair is in House thing Key is in Office thing Computer is in Office thing Draw is in Office thing Key is in Desk thing Office is in House Out Var (unbound) Out Var (unbound) thing Computer is in Desk thing Knife is in Kitchen thing Cheese is in Kitchen thing Kitchen is in House thing Key is in Draw thing Draw is in Desk thing Desk is in Office thing 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

×