Search in sources :

Example 86 with KieBase

use of org.kie.api.KieBase in project drools by kiegroup.

the class StreamsTest method testMultipleWindows.

@Test(timeout = 10000)
public void testMultipleWindows() throws Exception {
    String drl = "package org.drools.compiler\n" + "declare StockTick\n" + "    @role(event)\n" + "end\n" + "rule FaultsCoincide\n" + "when\n" + "   f1 : StockTick( company == \"RHT\" ) over window:length( 1 )\n" + "   f2 : StockTick( company == \"JBW\" ) over window:length( 1 )\n" + "then\n" + "end";
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(kconf, drl);
    KieSession ksession = kbase.newKieSession();
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    StockTick st1 = new StockTick(1, "RHT", 10, 1000);
    ksession.insert(st1);
    StockTick st2 = new StockTick(2, "JBW", 10, 1000);
    ksession.insert(st2);
    ksession.fireAllRules();
    ArgumentCaptor<org.kie.api.event.rule.AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
    verify(ael, times(1)).afterMatchFired(captor.capture());
    AfterMatchFiredEvent aafe = captor.getValue();
    assertThat((StockTick) aafe.getMatch().getDeclarationValue("f1"), is(st1));
    assertThat((StockTick) aafe.getMatch().getDeclarationValue("f2"), is(st2));
}
Also used : AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 87 with KieBase

use of org.kie.api.KieBase in project drools by kiegroup.

the class SubnetworkTest method testSubNetworks.

@Test
public void testSubNetworks() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_SubNetworks.drl"));
    final KieSession session = createKnowledgeSession(kbase);
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 88 with KieBase

use of org.kie.api.KieBase in project drools by kiegroup.

the class ShadowProxyTest method testShadowProxyOnCollections.

@Test
public void testShadowProxyOnCollections() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_ShadowProxyOnCollections.drl"));
    final KieSession ksession = createKnowledgeSession(kbase);
    final List results = new ArrayList();
    ksession.setGlobal("results", results);
    final Cheesery cheesery = new Cheesery();
    ksession.insert(cheesery);
    ksession.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(1, cheesery.getCheeses().size());
    assertEquals(results.get(0), cheesery.getCheeses().get(0));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Cheesery(org.drools.compiler.Cheesery) Test(org.junit.Test)

Example 89 with KieBase

use of org.kie.api.KieBase in project drools by kiegroup.

the class StrEvaluatorTest method testStrStartsWith.

@Test
public void testStrStartsWith() throws Exception {
    KieBase kbase = readKnowledgeBase();
    KieSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    RoutingMessage m = new RoutingMessage();
    m.setRoutingValue("R1:messageBody");
    ksession.insert(m);
    ksession.fireAllRules();
    assertTrue(list.size() == 4);
    assertTrue(((String) list.get(0)).equals("Message starts with R1"));
    assertTrue(((String) list.get(1)).equals("Message length is not 17"));
    assertTrue(((String) list.get(2)).equals("Message does not start with R2"));
    assertTrue(((String) list.get(3)).equals("Message does not end with R1"));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) RoutingMessage(org.drools.compiler.RoutingMessage) Test(org.junit.Test)

Example 90 with KieBase

use of org.kie.api.KieBase in project drools by kiegroup.

the class StrEvaluatorTest method testStrWithLogicalOr.

@Test
public void testStrWithLogicalOr() {
    String drl = "package org.drools.compiler.integrationtests\n" + "import org.drools.compiler.RoutingMessage\n" + "rule R1\n" + " when\n" + " RoutingMessage( routingValue == \"R2\" || routingValue str[startsWith] \"R1\" )\n" + " then\n" + "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(drl);
    KieSession ksession = kbase.newKieSession();
    try {
        for (String msgValue : new String[] { "R1something", "R2something", "R2" }) {
            RoutingMessage msg = new RoutingMessage();
            msg.setRoutingValue(msgValue);
            ksession.insert(msg);
        }
        assertEquals("Wrong number of rules fired", 2, ksession.fireAllRules());
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) RoutingMessage(org.drools.compiler.RoutingMessage) Test(org.junit.Test)

Aggregations

KieBase (org.kie.api.KieBase)1272 Test (org.junit.Test)1191 KieSession (org.kie.api.runtime.KieSession)1011 ArrayList (java.util.ArrayList)592 List (java.util.List)392 Person (org.drools.compiler.Person)214 FactHandle (org.kie.api.runtime.rule.FactHandle)176 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)168 KieHelper (org.kie.internal.utils.KieHelper)156 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)154 Cheese (org.drools.compiler.Cheese)139 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)99 Arrays.asList (java.util.Arrays.asList)87 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)86 QueryResults (org.kie.api.runtime.rule.QueryResults)78 KieServices (org.kie.api.KieServices)74 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)67 Model (org.drools.model.Model)64 Rule (org.drools.model.Rule)64 ModelImpl (org.drools.model.impl.ModelImpl)64