Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1.     @Test
  2.     public void testRecursiveNotQuery() {
  3.         String drl =
  4.                 "import org.drools.compiler.xpath.Thing;\n" +
  5.                 "global java.util.List list\n" +
  6.                 "\n" +
  7.                 "rule \"Print all things contained in the Office\" when\n" +
  8.                 "    $office : Thing( name == \"office\" )\n" +
  9.                 "    isNotContainedIn( $office, thing; )\n" +
  10.                 "then\n" +
  11.                 "    list.add( thing.getName() );\n" +
  12.                 "end\n" +
  13.                 "\n" +
  14.                 "query isContainedIn( Thing $x, Thing $y )\n" +
  15.                 "    $y := Thing() from $x.getChildren()\n" +
  16.                 "or\n" +
  17.                 "    ( $z := Thing() from $x.getChildren() and isContainedIn( $z, $y; ) )\n" +
  18.                 "end\n" +
  19.                 "\n" +
  20.                 "query isNotContainedIn( Thing $x, Thing $y )\n" +
  21.                 "    not( isContainedIn( $x, $y; ) )\n" +
  22.                 "end\n";
  23.  
  24.  
  25.         Thing house = new Thing( "house" );
  26.         Thing office = new Thing( "office" );
  27.         house.addChild( office );
  28.         Thing kitchen = new Thing( "kitchen" );
  29.         house.addChild( kitchen );
  30.  
  31.         Thing knife = new Thing( "knife" );
  32.         kitchen.addChild( knife );
  33.         Thing cheese = new Thing( "cheese" );
  34.         kitchen.addChild( cheese );
  35.  
  36.         Thing desk = new Thing( "desk" );
  37.         office.addChild( desk );
  38.         Thing chair = new Thing( "chair" );
  39.         office.addChild( chair );
  40.  
  41.         Thing computer = new Thing( "computer" );
  42.         desk.addChild( computer );
  43.         Thing draw = new Thing( "draw" );
  44.         desk.addChild( draw );
  45.         Thing key = new Thing( "key" );
  46.         draw.addChild( key );
  47.  
  48.         KieSession ksession = new KieHelper().addContent( drl, ResourceType.DRL )
  49.                                              .build()
  50.                                              .newKieSession();
  51.  
  52.         List<String> list = new ArrayList<String>();
  53.         ksession.setGlobal( "list", list );
  54.  
  55.         ksession.insert(house);
  56.         ksession.insert(office);
  57.         ksession.insert(kitchen);
  58.         ksession.insert(knife);
  59.         ksession.insert(cheese);
  60.         ksession.insert(desk);
  61.         ksession.insert(chair);
  62.         ksession.insert(computer);
  63.         ksession.insert(draw);
  64.         ksession.insert(key);
  65.  
  66.         ksession.fireAllRules();
  67.         System.out.println(list);
  68.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement