Ejemplo n.º 1
0
  public void testEntry(KProjectTestClass testClass, String jarName) {
    List<String> list = new ArrayList<String>();

    StatelessKnowledgeSession stlsKsession = testClass.getKBase1KSession1();
    stlsKsession.setGlobal("list", list);
    stlsKsession.execute("dummy");
    assertEquals(2, list.size());
    assertTrue(list.contains(jarName + ".test1:rule1"));
    assertTrue(list.contains(jarName + ".test1:rule2"));

    list.clear();
    StatefulKnowledgeSession stflKsession = testClass.getKBase1KSession2();
    stflKsession.setGlobal("list", list);
    stflKsession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.contains(jarName + ".test1:rule1"));
    assertTrue(list.contains(jarName + ".test1:rule2"));

    list.clear();
    stflKsession = testClass.getKBase2KSession3();
    stflKsession.setGlobal("list", list);
    stflKsession.fireAllRules();
    assertEquals(2, list.size());

    assertTrue(list.contains(jarName + ".test2:rule1"));
    assertTrue(list.contains(jarName + ".test2:rule2"));

    list.clear();
    stlsKsession = testClass.getKBase3KSession4();
    stlsKsession.setGlobal("list", list);
    stlsKsession.execute("dummy");
    assertEquals(4, list.size());
    assertTrue(list.contains(jarName + ".test1:rule1"));
    assertTrue(list.contains(jarName + ".test1:rule2"));
    assertTrue(list.contains(jarName + ".test2:rule1"));
    assertTrue(list.contains(jarName + ".test2:rule2"));
  }
  private String insertMessageAndFire(String message, boolean stateful)
      throws IllegalAccessException, InstantiationException {
    System.out.println("********** Firing rules");

    String result = null;

    FactType testFactType = kagent.getKnowledgeBase().getFactType("test", "TestFact");
    Object fact = testFactType.newInstance();
    testFactType.set(fact, "message", message);

    if (stateful) {
      StatefulKnowledgeSession session = kagent.getKnowledgeBase().newStatefulKnowledgeSession();
      session.insert(fact);
      session.fireAllRules();
      result = (String) testFactType.get(fact, "message");
      session.dispose();
    } else {
      StatelessKnowledgeSession session = kagent.getKnowledgeBase().newStatelessKnowledgeSession();
      session.execute(fact);
      result = (String) testFactType.get(fact, "message");
    }

    return result;
  }