Search in sources :

Example 66 with EntryPoint

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

the class ReteooWorkingMemoryTest method testDifferentEntryPointsOnSameFact.

@Test
public void testDifferentEntryPointsOnSameFact() {
    // JBRULES-2971
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    Rete rete = kBase.getRete();
    NodeFactory nFacotry = kBase.getConfiguration().getComponentFactory().getNodeFactoryService();
    EntryPointNode epn = nFacotry.buildEntryPointNode(kBase.getReteooBuilder().getIdGenerator().getNextId(), RuleBasePartitionId.MAIN_PARTITION, kBase.getConfiguration().isMultithreadEvaluation(), rete, new EntryPointId("xxx"));
    kBase.getRete().addObjectSink(epn);
    KieSession ksession = kBase.newKieSession();
    FactHandle f1 = ksession.insert("f1");
    EntryPoint ep = ksession.getEntryPoint("xxx");
    try {
        ep.update(f1, "s1");
        fail("Should throw an exception");
    } catch (IllegalArgumentException e) {
    }
    try {
        ep.retract(f1);
        fail("Should throw an exception");
    } catch (IllegalArgumentException e) {
    }
    ksession.update(f1, "s1");
    assertNotNull(ksession.getObject(f1));
    ksession.retract(f1);
    ksession.retract(f1);
    assertNull(ksession.getObject(f1));
}
Also used : NodeFactory(org.drools.core.reteoo.builder.NodeFactory) EntryPointId(org.drools.core.rule.EntryPointId) FactHandle(org.kie.api.runtime.rule.FactHandle) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 67 with EntryPoint

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

the class TraitHelper method lookupHandleForWrapper.

private <K> InternalFactHandle lookupHandleForWrapper(K core) {
    for (EntryPoint ep : workingMemory.getEntryPoints()) {
        ObjectStore store = ((WorkingMemoryEntryPoint) ep).getObjectStore();
        Iterator<InternalFactHandle> iter = store.iterateFactHandles();
        while (iter.hasNext()) {
            InternalFactHandle handle = iter.next();
            if (handle.isTraitable() && handle.getObject() instanceof CoreWrapper && ((CoreWrapper) handle.getObject()).getCore() == core) {
                return handle;
            }
        }
    }
    return null;
}
Also used : CoreWrapper(org.drools.core.factmodel.traits.CoreWrapper) ObjectStore(org.drools.core.common.ObjectStore) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) InternalWorkingMemoryEntryPoint(org.drools.core.common.InternalWorkingMemoryEntryPoint) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) InternalFactHandle(org.drools.core.common.InternalFactHandle) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) InternalWorkingMemoryEntryPoint(org.drools.core.common.InternalWorkingMemoryEntryPoint)

Example 68 with EntryPoint

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

the class DescrBuilderTest method testFromEntryPoint.

@Test
public void testFromEntryPoint() throws InstantiationException, IllegalAccessException {
    PackageDescr pkg = DescrFactory.newPackage().name("org.drools").newRule().name("from rule").lhs().pattern("String").id("s", false).from().entryPoint("EventStream").end().end().rhs("//System.out.println(s);").end().getDescr();
    KiePackage kpkg = compilePkgDescr(pkg, "org.drools");
    assertEquals("org.drools", kpkg.getName());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Collections.singletonList(kpkg));
    KieSession ksession = createKnowledgeSession(kbase);
    EntryPoint ep = ksession.getEntryPoint("EventStream");
    ep.insert("Hello World!");
    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : KiePackage(org.kie.api.definition.KiePackage) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 69 with EntryPoint

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

the class MarshallingTest method testMarshallEntryPointsWithSlidingTimeWindow.

@Test
@Ignore("beta4 phreak")
public void testMarshallEntryPointsWithSlidingTimeWindow() throws Exception {
    String str = "package org.domain.test \n" + "import " + getClass().getCanonicalName() + ".*\n" + "import java.util.List\n" + "global java.util.List list\n" + "declare A\n" + " @role( event )\n" + " @expires( 10m )\n" + "end\n" + "declare B\n" + "" + " @role( event )\n" + " @expires( 10m )\n" + "end\n" + "" + "rule a1\n" + "when\n" + "   $l : List() from collect( A()  over window:time(30s) from entry-point 'a-ep') \n" + "then\n" + "   list.add( $l );" + "end\n";
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBaseFromString(conf, str);
    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get("pseudo"));
    ksconf.setOption(TimerJobFactoryOption.get("trackable"));
    KieSession ksession = createKnowledgeSession(kbase, ksconf);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    EntryPoint aep = ksession.getEntryPoint("a-ep");
    aep.insert(new A());
    ksession = marsallStatefulKnowledgeSession(ksession);
    aep = ksession.getEntryPoint("a-ep");
    aep.insert(new A());
    ksession = marsallStatefulKnowledgeSession(ksession);
    list.clear();
    ksession.fireAllRules();
    ksession = marsallStatefulKnowledgeSession(ksession);
    assertEquals(2, ((List) list.get(0)).size());
    PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    timeService.advanceTime(15, TimeUnit.SECONDS);
    ksession = marsallStatefulKnowledgeSession(ksession);
    aep = ksession.getEntryPoint("a-ep");
    aep.insert(new A());
    ksession = marsallStatefulKnowledgeSession(ksession);
    aep = ksession.getEntryPoint("a-ep");
    aep.insert(new A());
    ksession = marsallStatefulKnowledgeSession(ksession);
    list.clear();
    ksession.fireAllRules();
    ksession = marsallStatefulKnowledgeSession(ksession);
    assertEquals(4, ((List) list.get(0)).size());
    timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    timeService.advanceTime(20, TimeUnit.SECONDS);
    ksession = marsallStatefulKnowledgeSession(ksession);
    list.clear();
    ksession.fireAllRules();
    assertEquals(2, ((List) list.get(0)).size());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) FactA(org.drools.compiler.FactA) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) SessionClock(org.kie.api.time.SessionClock) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 70 with EntryPoint

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

the class FromTest method testNetworkBuildErrorAcrossEntryPointsAndFroms.

@Test
public void testNetworkBuildErrorAcrossEntryPointsAndFroms() throws Exception {
    String rule1 = "package org.drools.compiler\n";
    rule1 += "global java.util.List list\n";
    rule1 += "rule rule1\n";
    rule1 += "when\n";
    rule1 += "         Cheese() from entry-point \"testep\"\n";
    rule1 += "    $p : Person() from list\n";
    rule1 += "then \n";
    rule1 += "  list.add( \"rule1\" ) ;\n";
    rule1 += "  insert( $p );\n";
    rule1 += "end\n";
    rule1 += "rule rule2\n";
    rule1 += "when\n";
    rule1 += "  $p : Person() \n";
    rule1 += "then \n";
    rule1 += "  list.add( \"rule2\" ) ;\n";
    rule1 += "end\n";
    final KieBase kbase = loadKnowledgeBaseFromString(rule1);
    final KieSession ksession = createKnowledgeSession(kbase);
    final EntryPoint ep = ksession.getEntryPoint("testep");
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    list.add(new Person("darth"));
    ep.insert(new Cheese("cheddar"));
    ksession.fireAllRules();
    assertEquals(3, list.size());
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Aggregations

EntryPoint (org.kie.api.runtime.rule.EntryPoint)70 Test (org.junit.Test)50 KieSession (org.kie.api.runtime.KieSession)46 ArrayList (java.util.ArrayList)26 KieBase (org.kie.api.KieBase)26 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)19 StockTick (org.drools.compiler.StockTick)18 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)16 FactHandle (org.kie.api.runtime.rule.FactHandle)15 List (java.util.List)13 InternalFactHandle (org.drools.core.common.InternalFactHandle)13 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)13 RegistryContext (org.drools.core.command.impl.RegistryContext)12 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)8 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)7 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)7 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)6 EventFactHandle (org.drools.core.common.EventFactHandle)6 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)6 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)6