Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. @Test
  2.     public void testFireUntilHalt() throws InterruptedException, IOException {
  3.  
  4.         String drl = "package org.jboss.qa.jbpm.functional\n" +
  5.                 "\n" +
  6.                 "import org.jboss.qa.brms.domain.Person;\n" +
  7.                 "\n" +
  8.                 "rule \"person detector\"\n" +
  9.                 "    when\n" +
  10.                 "        Person( )\n" +
  11.                 "    then\n" +
  12.                 "        System.out.println(\"There is a person.\");\n" +
  13.                 "end";
  14.  
  15.         RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get()
  16.                 .newEmptyBuilder()
  17.                 .addAsset(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL)
  18.                 .get();
  19.  
  20.         RuntimeManager runtimeManager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
  21.         // ksession for process instance #1
  22.         // since there is no process instance yet we need to get new session
  23.         RuntimeEngine runtime = runtimeManager.getRuntimeEngine(EmptyContext.get());
  24.         final KieSession ksessionLocal = runtime.getKieSession();
  25.  
  26.         TrackingAgendaEventListener listener = new TrackingAgendaEventListener();
  27.         ksessionLocal.addEventListener(listener);
  28.  
  29.         // thread for firing until halt
  30.         ExecutorService thread = Executors.newSingleThreadExecutor();
  31.         thread.submit(new Runnable() {
  32.  
  33.             @Override
  34.             public void run() {
  35.                 ksessionLocal.fireUntilHalt();
  36.             }
  37.         });
  38.  
  39.         int wantedPersonsNum = 3;
  40.         int unwantedPersonsNum = 2;
  41.  
  42.         Person p;
  43.         // insert 3 wanted persons
  44.         for (int i = 0; i < wantedPersonsNum; i++) {
  45.             p = new Person("wanted person");
  46.             p.setId(i);
  47.             ksessionLocal.insert(p);
  48.         }
  49.         // insert 2 unwanted persons
  50.         for (int i = 0; i < unwantedPersonsNum; i++) {
  51.             p = new Person("unwanted person");
  52.             p.setId(i + 50);
  53.             ksessionLocal.insert(p);
  54.         }
  55.         // wait for rule to fire
  56.         Thread.sleep(1000);
  57.         // 8 persons should be acknowledged - person detector rule fired
  58.         assertEquals(wantedPersonsNum + unwantedPersonsNum, listener.ruleFiredCount("person detector"));
  59.         ksessionLocal.halt();
  60.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement