Beispiel #1
0
  private static void smallBlobCompile() throws DroolsParserException, IOException, Exception {

    /* love you */ long time = System.currentTimeMillis();
    PackageBuilder b = new PackageBuilder();
    b.addPackageFromDrl(new StringReader(getHeader()));
    for (int i = 0; i < RULE_COUNT; i++) {
      String name = "x" + i;
      int status = i;

      String r = getTemplate2(name, i, status);
      b.addPackageFromDrl(new StringReader(r));
      if (i % 1000 == 0) System.err.println("Rule #" + i);
    }

    assertFalse(b.getErrors().toString(), b.hasErrors());

    System.err.println("Time taken for compiling: " + (System.currentTimeMillis() - time));
    time = System.currentTimeMillis();

    Package p = b.getPackage();
    RuleBase rb = RuleBaseFactory.newRuleBase();

    rb.addPackage(p);

    System.err.println("Time taken rete building: " + (System.currentTimeMillis() - time));
  }
  @Test
  public void testNotWithBindings() throws Exception {
    final PackageBuilder builder = new PackageBuilder();
    builder.addPackageFromDrl(
        new InputStreamReader(getClass().getResourceAsStream("not_with_bindings_rule_test.drl")));
    final Package pkg = builder.getPackage();

    final Rule rule = pkg.getRules()[0];
    assertTrue(rule.isValid());
    assertEquals(0, builder.getErrors().getErrors().length);
    RuleBase ruleBase = getSinglethreadRuleBase();
    ruleBase.addPackage(pkg);
    ruleBase = SerializationHelper.serializeObject(ruleBase);
    final WorkingMemory workingMemory = ruleBase.newStatefulSession();

    final List list = new ArrayList();
    workingMemory.setGlobal("list", list);

    final Cheese stilton = new Cheese("stilton", 5);
    final FactHandle stiltonHandle = workingMemory.insert(stilton);
    final Cheese cheddar = new Cheese("cheddar", 7);
    final FactHandle cheddarHandle = workingMemory.insert(cheddar);

    final PersonInterface paul = new Person("paul", "stilton", 12);
    workingMemory.insert(paul);
    workingMemory.fireAllRules();

    assertEquals(0, list.size());

    workingMemory.retract(stiltonHandle);

    workingMemory.fireAllRules();

    assertEquals(1, list.size());
  }
  @Test
  public void testNot() throws Exception {
    final PackageBuilder builder = new PackageBuilder();
    builder.addPackageFromDrl(
        new InputStreamReader(getClass().getResourceAsStream("not_rule_test.drl")));
    if (builder.hasErrors()) {
      fail(builder.getErrors().toString());
    }
    final Package pkg = builder.getPackage();

    final RuleBase ruleBase = getSinglethreadRuleBase();
    ruleBase.addPackage(pkg);
    final WorkingMemory workingMemory = ruleBase.newStatefulSession();

    final List list = new ArrayList();
    workingMemory.setGlobal("list", list);

    final Cheese stilton = new Cheese("stilton", 5);
    final FactHandle stiltonHandle = workingMemory.insert(stilton);
    final Cheese cheddar = new Cheese("cheddar", 7);
    final FactHandle cheddarHandle = workingMemory.insert(cheddar);
    workingMemory.fireAllRules();

    assertEquals(0, list.size());

    workingMemory.retract(stiltonHandle);

    workingMemory.fireAllRules();

    assertEquals(4, list.size());
    assertTrue(list.contains(new Integer(5)));
    assertTrue(list.contains(new Integer(6)));
    assertTrue(list.contains(new Integer(7)));
    assertTrue(list.contains(new Integer(8)));
  }
Beispiel #4
0
  private static void bigBlobCompile() throws DroolsParserException, IOException, Exception {
    StringBuilder buf = new StringBuilder();
    buf.append(getHeader());

    for (int i = 0; i < RULE_COUNT; i++) {
      String name = "x" + i;
      int status = i;

      String r = getTemplate1(name, status);
      buf.append(r);
    }

    /* love you */ long time = System.currentTimeMillis();

    DrlParser ps = new DrlParser(LanguageLevelOption.DRL5);
    PackageDescr pkg = ps.parse(new StringReader(buf.toString()));

    System.err.println("Time taken for parsing: " + (System.currentTimeMillis() - time));

    time = System.currentTimeMillis();
    PackageBuilder b = new PackageBuilder();
    b.addPackage(pkg);
    assertFalse(b.getErrors().toString(), b.hasErrors());

    System.err.println("Time taken for compiling: " + (System.currentTimeMillis() - time));
    time = System.currentTimeMillis();

    Package p = b.getPackage();
    RuleBase rb = RuleBaseFactory.newRuleBase();

    rb.addPackage(p);

    System.err.println("Time taken rete building: " + (System.currentTimeMillis() - time));

    File f = new File("foo.rulebase");
    if (f.exists()) f.delete();

    time = System.currentTimeMillis();
    ObjectOutput out = new DroolsObjectOutputStream(new FileOutputStream(f));
    out.writeObject(rb);
    out.flush();
    out.close();
    System.err.println("Time taken serializing rulebase: " + (System.currentTimeMillis() - time));

    time = System.currentTimeMillis();
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
    RuleBase rb_ = (RuleBase) in.readObject();
    System.err.println(
        "Time taken de-serializing rulebase: " + (System.currentTimeMillis() - time));
  }