Advertisement
Guest User

Untitled

a guest
Apr 19th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. package org.test;
  2.  
  3. import org.drools.factmodel.traits.Traitable;
  4. import org.drools.factmodel.traits.Entity;
  5. import org.drools.factmodel.traits.Thing;
  6.  
  7. global java.util.List list;
  8.  
  9.  
  10. declare Place
  11.     @Traitable
  12.     name    : String        @key
  13. end
  14.  
  15.  
  16. declare trait Location
  17. end
  18.  
  19. declare trait Lake extends Location
  20.     name    : String
  21. end
  22.  
  23. declare trait Country extends Location
  24.     name    : String
  25. end
  26.  
  27. declare trait City extends Location
  28.     name    : String
  29. end
  30.  
  31.  
  32. declare IsIn
  33.     src : Place
  34.     tgt : Place
  35. end
  36.  
  37. query is_in( Place $p, Class $type, Location $q )
  38.     ( IsIn( $p, $x ; ) and $q := Location( core == $x, this isA $type ) )
  39.     or
  40.     ( IsIn( $p, $x ; ) and is_in( $x, $type, $q ; ) )
  41. end
  42.  
  43.  
  44. rule "Init"
  45. when
  46. then
  47.     Place p1 = new Place( "Italy" );
  48.     insert( p1 );
  49.     don( p1, Country.class );
  50.  
  51.     Place p2 = new Place( "LakeOfGarda" );
  52.     insert( p2 );
  53.     don( p2, Lake.class );
  54.  
  55.     Place p3 = new Place( "LakeOfComo" );
  56.     insert( p3 );
  57.     don( p3, Lake.class );
  58.  
  59.     Place p4 = new Place( "Como" );
  60.     insert( p4 );
  61.     don( p4, City.class );
  62.  
  63.     insert( new IsIn( p2, p1 ) );
  64.     insert( new IsIn( p3, p1 ) );
  65.     insert( new IsIn( p4, p3 ) );
  66. end
  67.  
  68.  
  69. rule "PlacesInItaly"
  70. when
  71.     $s : String()
  72.     $p : Place( name == $s )
  73.  
  74.     is_in( $p, Country.class, $q ; )
  75.  
  76.     $q := Country( $name : name == "Italy" )
  77. then
  78.     System.out.println( "Checking " + $p + " in " + $q );
  79.     list.add( $name );
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement