Search in sources :

Example 81 with KieBase

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

the class SegmentCreationTest method testMultiSharedPattern.

@Test
public void testMultiSharedPattern() throws Exception {
    KieBase kbase = buildKnowledgeBase("   A() \n", "   A() B() \n", "   A() B() C() \n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    ObjectTypeNode aotn = getObjectTypeNode(kbase, LinkingTest.A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn1 = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[1];
    RuleTerminalNode rtn2 = (RuleTerminalNode) bNode.getSinkPropagator().getSinks()[0];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[1];
    RuleTerminalNode rtn3 = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
    wm.insert(new LinkingTest.A());
    wm.insert(new LinkingTest.B());
    wm.insert(new LinkingTest.C());
    wm.flushPropagations();
    // LiaNode  is in it's own segment
    LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory smem = liaMem.getSegmentMemory();
    assertEquals(liaNode, smem.getRootNode());
    assertEquals(liaNode, smem.getTipNode());
    SegmentMemory rtnSmem1 = smem.getFirst();
    assertEquals(rtn1, rtnSmem1.getRootNode());
    assertEquals(rtn1, rtnSmem1.getTipNode());
    SegmentMemory bSmem = rtnSmem1.getNext();
    assertEquals(bNode, bSmem.getRootNode());
    assertEquals(bNode, bSmem.getTipNode());
    // child segment is not yet initialised, so null
    assertNull(bSmem.getFirst());
    // there is no next
    assertNull(bSmem.getNext());
    // child segments should now be initialised
    wm.fireAllRules();
    wm.flushPropagations();
    SegmentMemory rtnSmem2 = bSmem.getFirst();
    assertEquals(rtn2, rtnSmem2.getRootNode());
    assertEquals(rtn2, rtnSmem2.getTipNode());
    SegmentMemory cSmem = rtnSmem2.getNext();
    assertEquals(cNode, cSmem.getRootNode());
    // note rtn3 is in the same segment as C
    assertEquals(rtn3, cSmem.getTipNode());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) KieBase(org.kie.api.KieBase) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 82 with KieBase

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

the class StatelessStressTest method testLotsOfStateless.

@Test
@Ignore
public void testLotsOfStateless() throws Exception {
    final KieBase kbase = loadKnowledgeBase("thread_class_test.drl");
    int numThreads = 100;
    Thread[] ts = new Thread[numThreads];
    for (int i = 0; i < numThreads; i++) {
        Runnable run = new Runnable() {

            public void run() {
                long start = 0;
                long previous = 0;
                while (true) {
                    start = System.currentTimeMillis();
                    StatelessKieSession sess = kbase.newStatelessKieSession();
                    try {
                        sess = SerializationHelper.serializeObject(sess);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    Person p = new Person();
                    p.setName("Michael");
                    Address add1 = new Address();
                    add1.setStreet("High");
                    Address add2 = new Address();
                    add2.setStreet("Low");
                    List l = new ArrayList();
                    l.add(add1);
                    l.add(add2);
                    p.setAddresses(l);
                    sess.execute(p);
                    long current = System.currentTimeMillis() - start;
                    if (previous != 0) {
                        float f = current / previous;
                        if (f > 1) {
                            System.err.println("SLOWDOWN");
                        }
                    }
                    previous = current;
                }
            }
        };
        Thread t = new Thread(run);
        t.start();
        ts[i] = t;
    }
    for (int i = 0; i < ts.length; i++) {
        ts[i].join();
    }
}
Also used : Address(org.drools.compiler.Address) ArrayList(java.util.ArrayList) KieBase(org.kie.api.KieBase) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) List(java.util.List) ArrayList(java.util.ArrayList) Person(org.drools.compiler.Person) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 83 with KieBase

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

the class StreamsTest method testModifyOnEntryPointFacts.

// (timeout=10000)
@Test
public void testModifyOnEntryPointFacts() throws Exception {
    String str = "package org.drools.compiler\n" + "declare StockTick\n" + "        @role ( event )\n" + "end\n" + "rule R1 salience 100\n" + "    when\n" + "        $s1 : StockTick( company == 'RHT', price == 10 ) from entry-point ep1\n" + "    then\n" + "        StockTick s = $s1;\n" + "        modify( s ) { setPrice( 50 ) };\n" + "end\n" + "rule R2 salience 90\n" + "    when\n" + "        $s1 : StockTick( company == 'RHT', price == 10 ) from entry-point ep2\n" + "    then\n" + "        StockTick s = $s1;\n" + "        modify( s ) { setPrice( 50 ) };\n" + "end\n" + "rule R3 salience 80\n" + "    when\n" + "        $s1 : StockTick( company == 'RHT', price == 10 ) from entry-point ep3\n" + "    then\n" + "        StockTick s = $s1;\n" + "        modify( s ) { setPrice( 50 ) };\n" + "end\n";
    // read in the source
    KieBase kbase = loadKnowledgeBaseFromString((KieBaseConfiguration) null, str);
    KieSession ksession = createKnowledgeSession(kbase);
    org.kie.api.event.rule.AgendaEventListener ael = mock(org.kie.api.event.rule.AgendaEventListener.class);
    ksession.addEventListener(ael);
    EntryPoint ep1 = ksession.getEntryPoint("ep1");
    EntryPoint ep2 = ksession.getEntryPoint("ep2");
    EntryPoint ep3 = ksession.getEntryPoint("ep3");
    ep1.insert(new StockTick(1, "RHT", 10, 1000));
    ep2.insert(new StockTick(1, "RHT", 10, 1000));
    ep3.insert(new StockTick(1, "RHT", 10, 1000));
    int rulesFired = ksession.fireAllRules();
    assertEquals(3, rulesFired);
    ArgumentCaptor<org.kie.api.event.rule.AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
    verify(ael, times(3)).afterMatchFired(captor.capture());
    List<org.kie.api.event.rule.AfterMatchFiredEvent> aafe = captor.getAllValues();
    assertThat(aafe.get(0).getMatch().getRule().getName(), is("R1"));
    assertThat(aafe.get(1).getMatch().getRule().getName(), is("R2"));
    assertThat(aafe.get(2).getMatch().getRule().getName(), is("R3"));
}
Also used : AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) EntryPoint(org.kie.api.runtime.rule.EntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 84 with KieBase

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

the class StreamsTest method testAtomicActivationFiring.

@Test(timeout = 10000)
public void testAtomicActivationFiring() throws Exception {
    // JBRULES-3383
    String str = "package org.drools.compiler.test\n" + "declare Event\n" + "   @role(event)\n" + "   name : String\n" + "end\n" + "declare Monitor\n" + "   @role(event)\n" + "   event : Event\n" + "   name : String\n" + "end\n" + "\n" + "rule \"start monitoring\"\n" + "when\n" + "    $e : Event( $in : name )\n" + "    not Monitor( name == $in )\n" + "then\n" + "    Monitor m = new Monitor( $e, $in );\n" + "    insert( m );\n" + "end\n" + "\n" + "rule \"stop monitoring\"\n" + "timer( int: 1s )\n" + "when\n" + "    $m : Monitor( $in : name )\n" + "    $e : Event( name == $in )\n" + "then\n" + "    retract( $m );\n" + "    retract( $m.getEvent() );\n" + "end\n" + "rule \"halt\"\n" + "salience -1\n" + "when\n" + "    not Event( )\n" + "then\n" + "    drools.halt();\n" + "end\n";
    KieBaseConfiguration kBaseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kBaseConfig.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(kBaseConfig, str);
    KieSession ksession = createKnowledgeSession(kbase);
    ksession.addEventListener(new org.kie.api.event.rule.DebugAgendaEventListener());
    FactType eventType = kbase.getFactType("org.drools.compiler.test", "Event");
    Object event = eventType.newInstance();
    eventType.set(event, "name", "myName");
    ksession.insert(event);
    ksession.fireUntilHalt();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 85 with KieBase

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

the class StreamsTest method testDeclaredEntryPoint.

@Test(timeout = 10000)
public void testDeclaredEntryPoint() {
    final String drl = "package org.jboss.qa.brms.declaredep\n" + "declare entry-point UnusedEntryPoint\n" + "end\n" + "rule HelloWorld\n" + "    when\n" + "        String( ) from entry-point UsedEntryPoint\n" + "    then\n" + "        // consequences\n" + "end\n";
    KieBase kbase = loadKnowledgeBaseFromString((KieBaseConfiguration) null, drl);
    KieSession ksession = kbase.newKieSession();
    assertNotNull(ksession.getEntryPoint("UsedEntryPoint"));
    assertNotNull(ksession.getEntryPoint("UnusedEntryPoint"));
    ksession.dispose();
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) 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