Ejemplo n.º 1
0
  @Test
  public void testStatelessWithCommands() throws Exception {
    File f1 = fileManager.write("rule1.drl", createDefaultRule("rule1"));

    File f2 = fileManager.write("rule2.drl", createDefaultRule("rule2"));

    String xml = "";
    xml += "<change-set xmlns='http://drools.org/drools-5.0/change-set'";
    xml += "    xmlns:xs='http://www.w3.org/2001/XMLSchema-instance'";
    xml +=
        "    xs:schemaLocation='http://drools.org/drools-5.0/change-set http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-api/src/main/resources/change-set-1.0.0.xsd' >";
    xml += "    <add> ";
    xml += "        <resource source='" + f1.getParentFile().toURI().toURL() + "' type='DRL' />";
    xml += "    </add> ";
    xml += "</change-set>";
    File fxml = fileManager.write("changeset", "changeset.xml", xml);

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();

    KnowledgeAgent kagent = this.createKAgent(kbase);

    applyChangeSet(kagent, ResourceFactory.newUrlResource(fxml.toURI().toURL()));

    StatelessKnowledgeSession ksession = kagent.newStatelessKnowledgeSession();

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

    ksession.execute(new InsertObjectCommand("hello"));

    assertEquals(2, list.size());
    assertTrue(list.contains("rule1"));
    assertTrue(list.contains("rule2"));
  }
Ejemplo n.º 2
0
  private void testRulesFirst(StatelessKnowledgeSession session) {
    RulesInvocationCounter counter = new RulesInvocationCounter();
    session.addEventListener(counter);

    EasyMock.reset(service);

    session.setGlobal("itemService", service);

    Item item1 = new Item();
    item1.setName("My test item1 name");
    item1.setPrice(1);
    service.equal(item1, 1);

    Item item5 = new Item();
    item5.setName("My test item5 name");
    item5.setPrice(5);
    service.equal(item5, 5);

    Item item6 = new Item();
    item6.setName("My test item6 name");
    item6.setPrice(12);

    EasyMock.replay(service);

    session.execute(item1);
    Assert.assertEquals(counter.getCounter(), 1);
    session.execute(item5);
    Assert.assertEquals(counter.getCounter(), 2);
    session.execute(item6);
    Assert.assertEquals(counter.getCounter(), 2);

    EasyMock.verify(service);
  }
Ejemplo n.º 3
0
  private StatelessKnowledgeSession getSession2(Resource resource) throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(resource, ResourceType.DRL);

    if (kbuilder.hasErrors()) {
      System.out.println(kbuilder.getErrors());
    }

    assertFalse(kbuilder.hasErrors());
    Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages();

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();

    kbase.addKnowledgePackages(pkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    StatelessKnowledgeSession session = kbase.newStatelessKnowledgeSession();

    session.setGlobal("list", this.list);
    session.setGlobal("cheesery", this.cheesery);
    return session;
  }