Search in sources :

Example 56 with EntryPoint

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

the class TimerAndCalendarTest method testExpiredPropagations.

@Test
public void testExpiredPropagations() throws InterruptedException {
    // DROOLS-244
    String drl = "package org.drools.test;\n" + "\n" + "import org.drools.compiler.StockTick;\n" + "global java.util.List list;\n" + "\n" + "declare StockTick\n" + "\t@role( event )\n" + "\t@timestamp( time )\n" + "end\n" + "\n" + "declare window ATicks\n" + " StockTick( company == \"AAA\" ) over window:time( 1s ) " + " from entry-point \"AAA\"\n" + "end\n" + "\n" + "declare window BTicks\n" + " StockTick( company == \"BBB\" ) over window:time( 1s ) " + " from entry-point \"BBB\"\n" + "end\n" + "\n" + "rule Ticks \n" + " when\n" + " String()\n" + " accumulate( $x : StockTick() from window ATicks, $a : count( $x ) )\n" + " accumulate( $y : StockTick() from window BTicks, $b : count( $y ) )\n" + " accumulate( $z : StockTick() over window:time( 1s ), $c : count( $z ) )\n" + " then\n" + " list.add( $a );\n" + " list.add( $b );\n" + " list.add( $c );\n" + "end";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get("pseudo"));
    KieSession ksession = kbase.newKieSession(conf, null);
    ArrayList list = new ArrayList();
    ksession.setGlobal("list", list);
    SessionPseudoClock clock = (SessionPseudoClock) ksession.getSessionClock();
    clock.advanceTime(1100, TimeUnit.MILLISECONDS);
    StockTick tick = new StockTick(0, "AAA", 1.0, 0);
    StockTick tock = new StockTick(1, "BBB", 1.0, 2500);
    StockTick tack = new StockTick(1, "CCC", 1.0, 2700);
    EntryPoint epa = ksession.getEntryPoint("AAA");
    EntryPoint epb = ksession.getEntryPoint("BBB");
    epa.insert(tick);
    epb.insert(tock);
    ksession.insert(tack);
    FactHandle handle = ksession.insert("go1");
    ksession.fireAllRules();
    System.out.println("***** " + list + " *****");
    assertEquals(asList(0L, 1L, 1L), list);
    list.clear();
    ksession.retract(handle);
    clock.advanceTime(2550, TimeUnit.MILLISECONDS);
    handle = ksession.insert("go2");
    ksession.fireAllRules();
    System.out.println("***** " + list + " *****");
    assertEquals(asList(0L, 0L, 1L), list);
    list.clear();
    ksession.retract(handle);
    clock.advanceTime(500, TimeUnit.MILLISECONDS);
    handle = ksession.insert("go3");
    ksession.fireAllRules();
    System.out.println("***** " + list + " *****");
    assertEquals(asList(0L, 0L, 0L), list);
    list.clear();
    ksession.retract(handle);
    ksession.dispose();
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StockTick(org.drools.compiler.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 57 with EntryPoint

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

the class KieBaseUpdater method clearInstancesOfModifiedClass.

protected void clearInstancesOfModifiedClass(Class<?> cls) {
    // remove all ObjectTypeNodes for the modified classes
    ClassObjectType objectType = new ClassObjectType(cls);
    for (EntryPointNode epn : ctx.kBase.getRete().getEntryPointNodes().values()) {
        epn.removeObjectType(objectType);
    }
    // remove all instance of the old class from the object stores
    for (InternalWorkingMemory wm : ctx.kBase.getWorkingMemories()) {
        for (EntryPoint ep : wm.getEntryPoints()) {
            InternalWorkingMemoryEntryPoint wmEp = (InternalWorkingMemoryEntryPoint) wm.getWorkingMemoryEntryPoint(ep.getEntryPointId());
            ClassAwareObjectStore store = ((ClassAwareObjectStore) wmEp.getObjectStore());
            if (store.clearClassStore(cls)) {
                log.warn("Class " + cls.getName() + " has been modified and therfore its old instances will no longer match");
            }
        }
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) EntryPointNode(org.drools.core.reteoo.EntryPointNode) ClassObjectType(org.drools.core.base.ClassObjectType) InternalWorkingMemoryEntryPoint(org.drools.core.common.InternalWorkingMemoryEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) ClassAwareObjectStore(org.drools.core.common.ClassAwareObjectStore) InternalWorkingMemoryEntryPoint(org.drools.core.common.InternalWorkingMemoryEntryPoint)

Example 58 with EntryPoint

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

the class UpdateCommand method execute.

public Void execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    EntryPoint ep = ksession.getEntryPoint(handle.getEntryPointId());
    if (modifiedProperties != null) {
        ep.update(handle, object, modifiedProperties);
    } else {
        ep.update(handle, object);
    }
    return null;
}
Also used : EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext)

Example 59 with EntryPoint

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

the class GetEntryPointsCommand method execute.

public Collection<? extends EntryPoint> execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    Collection<? extends EntryPoint> eps = ksession.getEntryPoints();
    EntryPointCreator epCreator = (EntryPointCreator) context.get(EntryPointCreator.class.getName());
    if (epCreator == null) {
        return eps;
    }
    Collection<EntryPoint> result = new ArrayList<EntryPoint>();
    for (EntryPoint ep : eps) {
        result.add(epCreator.getEntryPoint(ep.getEntryPointId()));
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) EntryPointCreator(org.drools.core.command.EntryPointCreator) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext)

Example 60 with EntryPoint

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

the class GetFactHandleInEntryPointCommand method execute.

public FactHandle execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    EntryPoint ep = ksession.getEntryPoint(entryPoint);
    InternalFactHandle factHandle = (InternalFactHandle) ep.getFactHandle(object);
    if (factHandle != null) {
        InternalFactHandle handle = factHandle.clone();
        if (disconnected) {
            handle.disconnect();
        }
        return handle;
    }
    return null;
}
Also used : EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) InternalFactHandle(org.drools.core.common.InternalFactHandle)

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