Search in sources :

Example 21 with InternalAgenda

use of org.drools.core.common.InternalAgenda in project drools by kiegroup.

the class AddRemoveRule method forceFlushLeftTuple.

public static void forceFlushLeftTuple(PathMemory pmem, SegmentMemory sm, InternalWorkingMemory wm, TupleSets<LeftTuple> leftTupleSets) {
    SegmentMemory[] smems = pmem.getSegmentMemories();
    LeftTupleNode node;
    Memory mem;
    long bit = 1;
    if (sm.getRootNode().getType() == NodeTypeEnums.LeftInputAdapterNode && sm.getTipNode().getType() != NodeTypeEnums.LeftInputAdapterNode) {
        // The segment is the first and it has the lian shared with other nodes, the lian must be skipped, so adjust the bit and sink
        node = sm.getRootNode().getSinkPropagator().getFirstLeftTupleSink();
        mem = sm.getNodeMemories().get(1);
        // adjust bit to point to next node
        bit = 2;
    } else {
        node = sm.getRootNode();
        mem = sm.getNodeMemories().get(0);
    }
    PathMemory rtnPmem = NodeTypeEnums.isTerminalNode(pmem.getPathEndNode()) ? pmem : wm.getNodeMemory((AbstractTerminalNode) pmem.getPathEndNode().getPathEndNodes()[0]);
    InternalAgenda agenda = pmem.getActualAgenda(wm);
    new RuleNetworkEvaluator().outerEval(pmem, node, bit, mem, smems, sm.getPos(), leftTupleSets, agenda, new LinkedList<StackEntry>(), true, rtnPmem.getOrCreateRuleAgendaItem(agenda).getRuleExecutor());
}
Also used : AbstractTerminalNode(org.drools.core.reteoo.AbstractTerminalNode) InternalAgenda(org.drools.core.common.InternalAgenda) SegmentMemory(org.drools.core.reteoo.SegmentMemory) Memory(org.drools.core.common.Memory) PathMemory(org.drools.core.reteoo.PathMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentNodeMemory(org.drools.core.reteoo.SegmentNodeMemory) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) RiaNodeMemory(org.drools.core.reteoo.RightInputAdapterNode.RiaNodeMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) TupleMemory(org.drools.core.reteoo.TupleMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) LeftTupleNode(org.drools.core.reteoo.LeftTupleNode) PathMemory(org.drools.core.reteoo.PathMemory)

Example 22 with InternalAgenda

use of org.drools.core.common.InternalAgenda in project drools by kiegroup.

the class AgendaEventSupportTest method testAgendaEventListener.

// public void testIsSerializable() {
// assertTrue( Serializable.class.isAssignableFrom( AgendaEventSupport.class ) );
// }
@Test
@Ignore
public void testAgendaEventListener() throws Exception {
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    // create a simple package with one rule to test the events
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools.test");
    final RuleImpl rule = new RuleImpl("test1");
    rule.setEager(true);
    rule.setAgendaGroup("test group");
    final ClassObjectType cheeseObjectType = new ClassObjectType(Cheese.class);
    final Pattern pattern = new Pattern(0, cheeseObjectType);
    pkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    pkg.getClassFieldAccessorStore().setEagerWire(true);
    final ClassFieldReader extractor = pkg.getClassFieldAccessorStore().getReader(Cheese.class, "type");
    final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar");
    final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor);
    pattern.addConstraint(constraint);
    rule.addPattern(pattern);
    rule.setConsequence(new Consequence() {

        public void evaluate(final KnowledgeHelper knowledgeHelper, final WorkingMemory workingMemory) throws Exception {
        }

        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        }

        public void writeExternal(ObjectOutput out) throws IOException {
        }

        public String getName() {
            // TODO Auto-generated method stub
            return null;
        }
    });
    pkg.addRule(rule);
    kbase.addPackages(Collections.singleton(pkg));
    // create a new working memory and add an AgendaEventListener
    KieSession ksession = kbase.newKieSession();
    final List agendaList = new ArrayList();
    final AgendaEventListener agendaEventListener = new AgendaEventListener() {

        public void matchCancelled(MatchCancelledEvent event) {
            assertNotNull(event.getKieRuntime());
            agendaList.add(event);
        }

        public void matchCreated(MatchCreatedEvent event) {
            assertNotNull(event.getKieRuntime());
            agendaList.add(event);
        }

        public void afterMatchFired(AfterMatchFiredEvent event) {
            assertNotNull(event.getKieRuntime());
            agendaList.add(event);
        }

        public void agendaGroupPopped(AgendaGroupPoppedEvent event) {
            assertNotNull(event.getKieRuntime());
            agendaList.add(event);
        }

        public void agendaGroupPushed(AgendaGroupPushedEvent event) {
            assertNotNull(event.getKieRuntime());
            agendaList.add(event);
        }

        public void beforeMatchFired(BeforeMatchFiredEvent event) {
            assertNotNull(event.getKieRuntime());
            agendaList.add(event);
        }

        public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
            assertNotNull(event.getKieRuntime());
            agendaList.add(event);
        }

        public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
            assertNotNull(event.getKieRuntime());
            agendaList.add(event);
        }

        public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
            assertNotNull(event.getKieRuntime());
            agendaList.add(event);
        }

        public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
            assertNotNull(event.getKieRuntime());
            agendaList.add(event);
        }
    };
    ksession.addEventListener(agendaEventListener);
    assertEquals(1, ksession.getAgendaEventListeners().size());
    // assert the cheese fact
    final Cheese cheddar = new Cheese("cheddar", 15);
    FactHandle cheddarHandle = ksession.insert(cheddar);
    InternalAgenda agenda = (InternalAgenda) ksession.getAgenda();
    agenda.evaluateEagerList();
    // should be one MatchCreatedEvent
    assertEquals(1, agendaList.size());
    MatchCreatedEvent createdEvent = (MatchCreatedEvent) agendaList.get(0);
    assertSame(cheddarHandle, createdEvent.getMatch().getFactHandles().toArray()[0]);
    // clear the agenda to check CLEAR events occur
    ksession.getAgenda().clear();
    MatchCancelledEvent cancelledEvent = (MatchCancelledEvent) agendaList.get(1);
    assertEquals(MatchCancelledCause.CLEAR, cancelledEvent.getCause());
    agendaList.clear();
    // update results in an MatchCreatedEvent
    cheddar.setPrice(14);
    ksession.update(cheddarHandle, cheddar);
    agenda.evaluateEagerList();
    assertEquals(1, agendaList.size());
    createdEvent = (MatchCreatedEvent) agendaList.get(0);
    assertSame(cheddarHandle, createdEvent.getMatch().getFactHandles().toArray()[0]);
    agendaList.clear();
    // update should not result in cancelation+activation events
    cheddar.setPrice(14);
    ksession.update(cheddarHandle, cheddar);
    assertEquals(0, agendaList.size());
    // cancelledEvent = (ActivationCancelledEvent) agendaList.get( 0 );
    // assertEquals( ActivationCancelledCause.WME_MODIFY, cancelledEvent.getCause() );
    // assertSame( cheddarHandle,
    // cancelledEvent.getActivation().toFactHandles().toArray()[0] );
    // createdEvent = (ActivationCreatedEvent) agendaList.get( 1 );
    // assertSame( cheddarHandle,
    // createdEvent.getActivation().toFactHandles().toArray()[0] );
    // agendaList.clear();
    // retract results in a ActivationCancelledEvent, note the object is not resolveable now as it no longer exists
    ksession.retract(cheddarHandle);
    assertEquals(1, agendaList.size());
    cancelledEvent = (MatchCancelledEvent) agendaList.get(0);
    // invalidated handles no longer set the object to null
    assertNotNull(((InternalFactHandle) cancelledEvent.getMatch().getFactHandles().toArray()[0]).getObject());
    // re-assert the fact so we can test the agenda group events
    cheddarHandle = ksession.insert(cheddar);
    agendaList.clear();
    // setFocus results in an AgendaGroupPushedEvent
    ksession.getAgenda().getAgendaGroup("test group").setFocus();
    assertEquals(1, agendaList.size());
    final AgendaGroupPushedEvent pushedEvent = (AgendaGroupPushedEvent) agendaList.get(0);
    assertEquals("test group", pushedEvent.getAgendaGroup().getName());
    agendaList.clear();
    // fireAllRules results in a BeforeActivationFiredEvent and an AfterActivationFiredEvent
    // the AgendaGroup becomes empty, which results in a popped event.
    ksession.fireAllRules();
    assertEquals(3, agendaList.size());
    final BeforeMatchFiredEvent beforeEvent = (BeforeMatchFiredEvent) agendaList.get(0);
    assertSame(cheddarHandle, beforeEvent.getMatch().getFactHandles().toArray()[0]);
    final AfterMatchFiredEvent afterEvent = (AfterMatchFiredEvent) agendaList.get(1);
    assertSame(cheddarHandle, afterEvent.getMatch().getFactHandles().toArray()[0]);
    final AgendaGroupPoppedEvent poppedEvent = (AgendaGroupPoppedEvent) agendaList.get(2);
    assertEquals("test group", poppedEvent.getAgendaGroup().getName());
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) WorkingMemory(org.drools.core.WorkingMemory) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) ArrayList(java.util.ArrayList) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Cheese(org.drools.core.test.model.Cheese) RuleFlowGroupDeactivatedEvent(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil) InternalAgenda(org.drools.core.common.InternalAgenda) ClassFieldReader(org.drools.core.base.ClassFieldReader) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) BeforeMatchFiredEvent(org.kie.api.event.rule.BeforeMatchFiredEvent) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) FieldValue(org.drools.core.spi.FieldValue) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Pattern(org.drools.core.rule.Pattern) ObjectOutput(java.io.ObjectOutput) Consequence(org.drools.core.spi.Consequence) IOException(java.io.IOException) IOException(java.io.IOException) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) AgendaGroupPoppedEvent(org.kie.api.event.rule.AgendaGroupPoppedEvent) RuleFlowGroupActivatedEvent(org.kie.api.event.rule.RuleFlowGroupActivatedEvent) AgendaGroupPushedEvent(org.kie.api.event.rule.AgendaGroupPushedEvent) ObjectInput(java.io.ObjectInput) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 23 with InternalAgenda

use of org.drools.core.common.InternalAgenda in project drools by kiegroup.

the class DefaultKnowledgeHelper method unblockAllMatches.

public void unblockAllMatches(Match act) {
    AgendaItem targetMatch = (AgendaItem) act;
    boolean wasBlocked = (targetMatch.getBlockers() != null && !targetMatch.getBlockers().isEmpty());
    for (LinkedListEntry entry = (LinkedListEntry) targetMatch.getBlockers().getFirst(); entry != null; ) {
        LinkedListEntry tmp = (LinkedListEntry) entry.getNext();
        LogicalDependency dep = (LogicalDependency) entry.getObject();
        ((AgendaItem) dep.getJustifier()).removeBlocked(dep);
        entry = tmp;
    }
    if (wasBlocked) {
        RuleAgendaItem ruleAgendaItem = targetMatch.getRuleAgendaItem();
        InternalAgenda agenda = workingMemory.getAgenda();
        agenda.stageLeftTuple(ruleAgendaItem, targetMatch);
    }
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda) RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) LinkedListEntry(org.drools.core.util.LinkedListEntry) LogicalDependency(org.drools.core.common.LogicalDependency) SimpleLogicalDependency(org.drools.core.beliefsystem.simple.SimpleLogicalDependency) RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) AgendaItem(org.drools.core.common.AgendaItem)

Aggregations

InternalAgenda (org.drools.core.common.InternalAgenda)23 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)14 KieSession (org.kie.api.runtime.KieSession)11 List (java.util.List)9 KieBase (org.kie.api.KieBase)8 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)6 Cheese (org.drools.compiler.Cheese)5 AgendaItem (org.drools.core.common.AgendaItem)4 RuleAgendaItem (org.drools.core.phreak.RuleAgendaItem)4 Person (org.drools.compiler.Person)3 CommandBasedStatefulKnowledgeSession (org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 AgendaGroup (org.drools.core.spi.AgendaGroup)3 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 AgendaGroupQueueImpl (org.drools.core.common.AgendaGroupQueueImpl)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)2 MatchCreatedEvent (org.kie.api.event.rule.MatchCreatedEvent)2