Search in sources :

Example 31 with StatelessKieSession

use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.

the class MVELTest method testArrays.

@Test
public void testArrays() {
    String text = "package test_mvel;\n";
    text += "import " + TestObject.class.getCanonicalName() + ";\n";
    text += "import function " + TestObject.class.getCanonicalName() + ".array;\n";
    text += "no-loop true\n";
    text += "dialect \"mvel\"\n";
    text += "rule \"1\"\n";
    text += "salience 1\n";
    text += "when\n";
    text += "    $fact: TestObject()\n";
    text += "    eval($fact.checkHighestPriority(\"mvel\", 2))\n";
    text += "    eval($fact.stayHasDaysOfWeek(\"mvel\", false, new String[][]{{\"2008-04-01\", \"2008-04-10\"}}))\n";
    text += "then\n";
    text += "    $fact.applyValueAddPromo(1,2,3,4,\"mvel\");\n";
    text += "end";
    final KieBase kieBase = loadKnowledgeBaseFromString(text.replaceAll("mvel", "java"), text);
    final StatelessKieSession statelessKieSession = kieBase.newStatelessKieSession();
    final List<String> list = new ArrayList<String>();
    statelessKieSession.execute(new TestObject(list));
    assertEquals(6, list.size());
    assertTrue(list.containsAll(Arrays.asList("TestObject.checkHighestPriority: java|2", "TestObject.stayHasDaysOfWeek: java|false|[2008-04-01, 2008-04-10]", "TestObject.checkHighestPriority: mvel|2", "TestObject.stayHasDaysOfWeek: mvel|false|[2008-04-01, 2008-04-10]", "TestObject.applyValueAddPromo: 1|2|3|4|mvel", "TestObject.applyValueAddPromo: 1|2|3|4|java")));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Test(org.junit.Test)

Example 32 with StatelessKieSession

use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.

the class KieContainerDefaultsTest method testTwoKieSessionsOneStatelessDefault.

/**
 * This test checks if default KieSessions behave as expected even if one of them is Stateless.
 */
@Test
public void testTwoKieSessionsOneStatelessDefault() {
    final KieModuleModel kieModule = kieServices.newKieModuleModel();
    kieModule.newKieBaseModel("firstKBase").setDefault(true).newKieSessionModel("firstKSession").setType(KieSessionModel.KieSessionType.STATELESS).setDefault(true);
    kieModule.newKieBaseModel("secondKBase").setDefault(true).newKieSessionModel("secondKSession").setDefault(false);
    buildKieModule(kieModule);
    final KieContainer kieContainer = kieServices.newKieContainer(RELEASE_ID);
    final StatelessKieSession firstKSession = kieContainer.newStatelessKieSession("firstKSession");
    final KieSession secondKSession = kieContainer.newKieSession("secondKSession");
    try {
        Assertions.assertThat(firstKSession).isEqualTo(((InternalKieContainer) kieContainer).getStatelessKieSession());
        Assertions.assertThat(secondKSession).isNotEqualTo(((InternalKieContainer) kieContainer).getStatelessKieSession());
    } finally {
        secondKSession.dispose();
    }
}
Also used : KieModuleModel(org.kie.api.builder.model.KieModuleModel) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) InternalKieContainer(org.drools.core.impl.InternalKieContainer) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 33 with StatelessKieSession

use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.

the class MultipleSheetsLoadingTest method test.

@Test
public void test() {
    final KieBuilder kbuilder = this.buildResources();
    final Collection<Message> results = kbuilder.getResults().getMessages(Level.ERROR, Level.WARNING);
    if (results.size() > 0) {
        LOGGER.error(results.toString());
    }
    Assertions.assertThat(results).as("Some errors/warnings found").isEmpty();
    final KieBase kbase = KieBaseUtil.getDefaultKieBaseFromKieBuilder(kieBaseTestConfiguration, kbuilder);
    final StatelessKieSession ksession = kbase.newStatelessKieSession();
    final Set<String> resultSet = new HashSet<String>();
    ksession.execute((Object) resultSet);
    Assertions.assertThat(resultSet.size()).as("Wrong number of rules was fired").isEqualTo(2);
    for (String ruleName : new String[] { "rule1", "rule2" }) {
        Assertions.assertThat(resultSet.contains(ruleName)).as("Rule " + ruleName + " was not fired!").isTrue();
    }
}
Also used : Message(org.kie.api.builder.Message) KieBase(org.kie.api.KieBase) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieBuilder(org.kie.api.builder.KieBuilder) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 34 with StatelessKieSession

use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.

the class Misc2Test method testListnersOnStatlessKieSession.

@Test
public void testListnersOnStatlessKieSession() {
    // DROOLS-141
    // BZ-999491
    String str = "rule R when\n" + "  String()\n" + "then\n" + "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    StatelessKieSession ksession = kbase.newStatelessKieSession();
    final List<String> firings = new ArrayList<String>();
    AgendaEventListener agendaEventListener = new AgendaEventListener() {

        public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
        }

        public void matchCancelled(org.kie.api.event.rule.MatchCancelledEvent event) {
        }

        public void beforeMatchFired(org.kie.api.event.rule.BeforeMatchFiredEvent event) {
        }

        public void afterMatchFired(org.kie.api.event.rule.AfterMatchFiredEvent event) {
            firings.add("Fired!");
        }

        public void agendaGroupPopped(org.kie.api.event.rule.AgendaGroupPoppedEvent event) {
        }

        public void agendaGroupPushed(org.kie.api.event.rule.AgendaGroupPushedEvent event) {
        }

        public void beforeRuleFlowGroupActivated(org.kie.api.event.rule.RuleFlowGroupActivatedEvent event) {
        }

        public void afterRuleFlowGroupActivated(org.kie.api.event.rule.RuleFlowGroupActivatedEvent event) {
        }

        public void beforeRuleFlowGroupDeactivated(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent event) {
        }

        public void afterRuleFlowGroupDeactivated(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent event) {
        }
    };
    ksession.addEventListener(agendaEventListener);
    ksession.execute("1");
    ksession.execute("2");
    assertEquals(2, firings.size());
    ksession.removeEventListener(agendaEventListener);
    ksession.execute("3");
    assertEquals(2, firings.size());
}
Also used : ArrayList(java.util.ArrayList) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieBase(org.kie.api.KieBase) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) Test(org.junit.Test)

Example 35 with StatelessKieSession

use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.

the class GlobalTest method testGlobalAccess.

@Test
public void testGlobalAccess() {
    final String drl = "import org.drools.core.base.MapGlobalResolver;\n" + "global java.lang.String myGlobal;\n" + "global String unused; \n";
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    final KieSession session1 = kbase.newKieSession();
    final String sample = "default string";
    // Testing 1.
    System.out.println("Start testing 1.");
    session1.setGlobal("myGlobal", "Testing 1");
    session1.insert(sample);
    session1.fireAllRules();
    final Map.Entry[] entries1 = ((MapGlobalResolver) session1.getGlobals()).getGlobals();
    assertEquals(1, entries1.length);
    assertEquals(entries1[0].getValue(), "Testing 1");
    assertEquals(1, session1.getGlobals().getGlobalKeys().size());
    assertTrue(session1.getGlobals().getGlobalKeys().contains("myGlobal"));
    session1.dispose();
    // Testing 2.
    System.out.println("Start testing 2.");
    final StatelessKieSession session2 = session1.getKieBase().newStatelessKieSession();
    session2.setGlobal("myGlobal", "Testing 2");
    session2.execute(sample);
    final Map.Entry[] entries2 = ((MapGlobalResolver) session2.getGlobals()).getGlobals();
    assertEquals(1, entries2.length);
    assertEquals(entries2[0].getValue(), "Testing 2");
    assertEquals(1, session2.getGlobals().getGlobalKeys().size());
    assertTrue(session2.getGlobals().getGlobalKeys().contains("myGlobal"));
    // Testing 3.
    System.out.println("Start testing 3.");
    final StatefulKnowledgeSession session3 = ((StatelessKnowledgeSessionImpl) session2).newWorkingMemory();
    session3.insert(sample);
    session3.fireAllRules();
    Map.Entry[] entries3 = ((MapGlobalResolver) session3.getGlobals()).getGlobals();
    assertEquals(1, entries3.length);
    assertEquals(entries3[0].getValue(), "Testing 2");
    assertEquals(1, session3.getGlobals().getGlobalKeys().size());
    assertTrue(session3.getGlobals().getGlobalKeys().contains("myGlobal"));
    session3.setGlobal("myGlobal", "Testing 3 Over");
    entries3 = ((MapGlobalResolver) session3.getGlobals()).getGlobals();
    assertEquals(1, entries3.length);
    assertEquals(entries3[0].getValue(), "Testing 3 Over");
    assertEquals(1, session3.getGlobals().getGlobalKeys().size());
    assertTrue(session3.getGlobals().getGlobalKeys().contains("myGlobal"));
    session3.dispose();
    // Testing 4.
    System.out.println("Start testing 4.");
    final StatefulKnowledgeSession session4 = ((StatelessKnowledgeSessionImpl) session2).newWorkingMemory();
    session4.setGlobal("myGlobal", "Testing 4");
    session4.insert(sample);
    session4.fireAllRules();
    final Map.Entry[] entries4 = ((MapGlobalResolver) session4.getGlobals()).getGlobals();
    assertEquals(1, entries4.length);
    assertEquals(entries4[0].getValue(), "Testing 4");
    assertEquals(1, session4.getGlobals().getGlobalKeys().size());
    assertTrue(session4.getGlobals().getGlobalKeys().contains("myGlobal"));
    session4.dispose();
}
Also used : StatelessKnowledgeSessionImpl(org.drools.core.impl.StatelessKnowledgeSessionImpl) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) MapGlobalResolver(org.drools.core.base.MapGlobalResolver) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

StatelessKieSession (org.kie.api.runtime.StatelessKieSession)55 Test (org.junit.Test)42 ArrayList (java.util.ArrayList)28 KieBase (org.kie.api.KieBase)20 List (java.util.List)14 Cheese (org.drools.compiler.Cheese)14 KieContainer (org.kie.api.runtime.KieContainer)13 Command (org.kie.api.command.Command)12 KieServices (org.kie.api.KieServices)10 DynamicRulesTest (org.drools.compiler.integrationtests.DynamicRulesTest)9 ExecutionResults (org.kie.api.runtime.ExecutionResults)9 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)7 KieSession (org.kie.api.runtime.KieSession)7 Message (org.drools.compiler.Message)6 Person (org.drools.compiler.Person)6 FireAllRulesCommand (org.drools.core.command.runtime.rule.FireAllRulesCommand)6 KieModuleModel (org.kie.api.builder.model.KieModuleModel)6 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)6 KieBuilder (org.kie.api.builder.KieBuilder)5 KieFileSystem (org.kie.api.builder.KieFileSystem)5