예제 #1
0
  @Test
  public void testDuplicateLocalVariableMVELConsequence() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(
        ResourceFactory.newInputStreamResource(
            getClass().getResourceAsStream("test_DuplicateLocalVariableMVELConsequence.drl")),
        ResourceType.DRL);

    assertTrue(kbuilder.hasErrors());
  }
예제 #2
0
  public static void setup() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(
        ResourceFactory.newInputStreamResource(
            ExampleScenario.class.getResourceAsStream(FILE_NAME_RULES)),
        ResourceType.DRL);

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    wm = kbase.newStatefulKnowledgeSession();
  }
예제 #3
0
  @Test
  public void testFromWithOr() throws Exception {
    KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    builder.add(
        ResourceFactory.newInputStreamResource(
            getClass().getResourceAsStream("test_FromWithOr.drl")),
        ResourceType.DRL);

    if (builder.hasErrors()) {
      logger.info(builder.getErrors().toString());
    }
    assertFalse(builder.hasErrors());

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(builder.getKnowledgePackages());

    final StatefulKnowledgeSession session = createKnowledgeSession(kbase);

    final List<Address> results = new ArrayList<Address>();
    session.setGlobal("results", results);

    Address a1 = new Address();
    a1.setZipCode("12345");
    Address a2 = new Address();
    a2.setZipCode("54321");
    Address a3 = new Address();
    a3.setZipCode("99999");

    Person p = new Person();
    p.addAddress(a1);
    p.addAddress(a2);
    p.addAddress(a3);

    session.insert(p);
    session.fireAllRules();

    assertEquals(2, results.size());
    assertTrue(results.contains(a1));
    assertTrue(results.contains(a2));
  }
예제 #4
0
  @Test
  public void testForall2() throws Exception {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(
        ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("test_Forall2.drl")),
        ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    final StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);

    final List<String> list = new ArrayList<String>();
    ksession.setGlobal("results", list);

    final State state = new State("SP");
    ksession.insert(state);

    final Person bob = new Person("Bob");
    bob.setStatus(state.getState());
    bob.setAlive(true);
    ksession.insert(bob);

    ksession.fireAllRules();

    assertEquals(0, list.size());

    final State qc = new State("QC");
    ksession.insert(qc);
    final Person john = new Person("John");
    john.setStatus(qc.getState());
    john.setAlive(false);

    ksession.fireAllRules();

    assertEquals(1, list.size());
  }
예제 #5
0
  public static void main(final String[] args) throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(
        ResourceFactory.newInputStreamResource(
            MannersBenchmark.class.getResourceAsStream("manners.drl")),
        ResourceType.DRL);

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    String filename;
    if (args.length != 0) {
      String arg = args[0];
      filename = arg;
    } else {
      filename = "manners5.dat";
    }

    for (int i = 0; i < 3; ++i) {
      InputStream is = MannersBenchmark.class.getResourceAsStream(filename);
      List list = getInputObjects(is);
      KieSession session = kbase.newStatefulKnowledgeSession();

      for (Iterator it = list.iterator(); it.hasNext(); ) {
        Object object = it.next();
        session.insert(object);
      }

      session.insert(new Count(1));

      long start = System.currentTimeMillis();
      session.fireAllRules();
      System.err.println(System.currentTimeMillis() - start);
      session.dispose();
    }
  }
예제 #6
0
  @Test
  @Ignore("This test requires us to fix the propagation order")
  public void testForallWithSlidingWindow() throws Exception {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(
        ResourceFactory.newInputStreamResource(
            getClass().getResourceAsStream("test_ForallSlidingWindow.drl")),
        ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());

    final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    final KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(conf, null);
    final SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    List<String> results = new ArrayList<String>();
    ksession.setGlobal("results", results);

    // advance time... no events, so forall should fire
    clock.advanceTime(60, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, results.size());

    int seq = 1;
    // advance time... there are matching events now, but forall still not fire
    ksession.insert(new StockTick(seq++, "RHT", 10, clock.getCurrentTime())); // 60
    clock.advanceTime(5, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, results.size());
    ksession.insert(new StockTick(seq++, "RHT", 10, clock.getCurrentTime())); // 65
    clock.advanceTime(5, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, results.size());

    // advance time... there are non-matching events now, so forall de-activates
    ksession.insert(new StockTick(seq++, "IBM", 10, clock.getCurrentTime())); // 70
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, results.size());

    // advance time... there are non-matching events now, so forall is still deactivated
    ksession.insert(new StockTick(seq++, "RHT", 10, clock.getCurrentTime())); // 80
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, results.size());

    // advance time... non-matching event expires now, so forall should fire
    ksession.insert(new StockTick(seq++, "RHT", 10, clock.getCurrentTime())); // 90
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(2, results.size());

    // advance time... forall still matches and should not fire
    ksession.insert(new StockTick(seq++, "RHT", 10, clock.getCurrentTime())); // 100
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(2, results.size());

    // advance time... forall still matches and should not fire
    clock.advanceTime(60, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(2, results.size());
  }