Search in sources :

Example 16 with InternalAgenda

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

the class AgendaRuleFlowGroupsTest method testAgendaGroupAndRuleFlowGroup.

@Test
public void testAgendaGroupAndRuleFlowGroup() throws Exception {
    CommandBasedStatefulKnowledgeSession ksession = createSession(-1, "agenda-groups.drl", "ruleflow-groups.drl");
    org.drools.core.spi.AgendaGroup[] groups = ((InternalAgenda) stripSession(ksession).getAgenda()).getAgendaGroups();
    // only main is available
    assertEquals(1, groups.length);
    assertEquals("MAIN", groups[0].getName());
    long id = ksession.getIdentifier();
    List<String> list = new ArrayList<String>();
    list.add("Test");
    ksession.insert(list);
    ksession.execute(new ActivateAgendaGroupCommand("agenda-group"));
    ksession.execute(new ActivateRuleFlowCommand("ruleflow-group"));
    ksession.dispose();
    ksession = createSession(id, "agenda-groups.drl", "ruleflow-groups.drl");
    groups = ((InternalAgenda) stripSession(ksession).getAgenda()).getAgendaGroups();
    // main and agenda group is now on the agenda
    assertEquals(3, groups.length);
    assertEquals("MAIN", groups[0].getName());
    assertEquals("ruleflow-group", groups[1].getName());
    assertEquals("agenda-group", groups[2].getName());
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda) ArrayList(java.util.ArrayList) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) Test(org.junit.Test)

Example 17 with InternalAgenda

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

the class ActiveActivationsIteratorTest method testActiveActivationsIteratorTest.

@Test
public void testActiveActivationsIteratorTest() {
    String str = "package org.kie.test \n" + "\n" + "rule rule0 @Propagation(EAGER) agenda-group 'a1' salience ( Integer.parseInt('1'+$s) ) when\n" + "    $s : String( this != 'xx' )\n" + "then\n" + "end\n" + "rule rule1 @Propagation(EAGER) agenda-group 'a2' salience ( Integer.parseInt('1'+$s)) when\n" + "    $s : String( this != 'xx' )\n" + "    eval( Integer.parseInt( $s ) <= 2 ) \n" + "then\n" + "end\n" + "rule rule2 @Propagation(EAGER) agenda-group 'a3' salience ( Integer.parseInt('1'+$s)) when\n" + "    $s : String( this != 'xx' )\n" + "    eval( Integer.parseInt( $s ) <= 2 ) \n" + "then\n" + "    kcontext.getKieRuntime().halt();\n" + "end\n" + "rule rule3 @Propagation(EAGER) ruleflow-group 'r1' salience ( Integer.parseInt('1'+$s)) when\n" + "    $s : String( this != 'xx' )\n" + "    eval( Integer.parseInt( $s ) > 2 ) \n" + "then\n" + "end\n" + "rule rule4 @Propagation(EAGER) ruleflow-group 'r1' salience ( Integer.parseInt('1'+$s) ) when\n" + "    $s : String( this != 'xx' )\n" + "    eval( Integer.parseInt( $s ) > 2 ) \n" + "    eval( Integer.parseInt( $s ) > 3 ) \n" + "then\n" + "end\n" + "rule rule6 @Propagation(EAGER) when\n" + "     java.util.Map()\n" + "then\n" + "end\n" + "\n" + "rule rule7 @Propagation(EAGER) when\n" + "    $s : String( this != 'xx' )\n" + "then\n" + "end\n" + "\n";
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
    for (int i = 0; i < 3; i++) {
        ksession.insert(new String("" + i));
    }
    ((InternalWorkingMemory) ksession).flushPropagations();
    ((InternalAgenda) ksession.getAgenda()).evaluateEagerList();
    Iterator it = ActiveActivationIterator.iterator(ksession);
    List list = new ArrayList();
    for (AgendaItem act = (AgendaItem) it.next(); act != null; act = (AgendaItem) it.next()) {
        list.add(act.getRule().getName() + ":" + act.getDeclarationValue("$s") + ":" + act.isQueued());
    }
    assertContains(new String[] { "rule7:2:true", "rule7:0:true", "rule7:1:true", "rule0:2:true", "rule0:0:true", "rule0:1:true", "rule1:2:true", "rule1:0:true", "rule1:1:true", "rule2:2:true", "rule2:0:true", "rule2:1:true" }, list);
    ksession.fireAllRules();
    it = ActiveActivationIterator.iterator(ksession);
    list = new ArrayList();
    for (AgendaItem act = (AgendaItem) it.next(); act != null; act = (AgendaItem) it.next()) {
        list.add(act.getRule().getName() + ":" + act.getDeclarationValue("$s") + ":" + act.isQueued());
    }
    assertContains(new String[] { "rule0:2:true", "rule0:0:true", "rule0:1:true", "rule1:2:true", "rule1:0:true", "rule1:1:true", "rule2:2:true", "rule2:0:true", "rule2:1:true" }, list);
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) InternalAgenda(org.drools.core.common.InternalAgenda) ActiveActivationIterator(org.drools.core.common.ActiveActivationIterator) Iterator(org.drools.core.util.Iterator) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) AgendaItem(org.drools.core.common.AgendaItem) Test(org.junit.Test)

Example 18 with InternalAgenda

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

the class ProtobufInputMarshaller method readAgenda.

public static void readAgenda(MarshallerReaderContext context, RuleData _ruleData, InternalAgenda agenda) {
    ProtobufMessages.Agenda _agenda = _ruleData.getAgenda();
    for (org.drools.core.marshalling.impl.ProtobufMessages.Agenda.AgendaGroup _agendaGroup : _agenda.getAgendaGroupList()) {
        AgendaGroupQueueImpl group = (AgendaGroupQueueImpl) agenda.getAgendaGroup(_agendaGroup.getName(), context.kBase);
        group.setActive(_agendaGroup.getIsActive());
        group.setAutoDeactivate(_agendaGroup.getIsAutoDeactivate());
        group.setClearedForRecency(_agendaGroup.getClearedForRecency());
        group.hasRuleFlowListener(_agendaGroup.getHasRuleFlowLister());
        group.setActivatedForRecency(_agendaGroup.getActivatedForRecency());
        for (org.drools.core.marshalling.impl.ProtobufMessages.Agenda.AgendaGroup.NodeInstance _nodeInstance : _agendaGroup.getNodeInstanceList()) {
            group.addNodeInstance(_nodeInstance.getProcessInstanceId(), _nodeInstance.getNodeInstanceId());
        }
        agenda.getAgendaGroupsMap().put(group.getName(), group);
    }
    for (String _groupName : _agenda.getFocusStack().getGroupNameList()) {
        agenda.addAgendaGroupOnStack(agenda.getAgendaGroup(_groupName));
    }
    for (ProtobufMessages.Agenda.RuleFlowGroup _ruleFlowGroup : _agenda.getRuleFlowGroupList()) {
        AgendaGroupQueueImpl group = (AgendaGroupQueueImpl) agenda.getAgendaGroup(_ruleFlowGroup.getName(), context.kBase);
        group.setActive(_ruleFlowGroup.getIsActive());
        group.setAutoDeactivate(_ruleFlowGroup.getIsAutoDeactivate());
        for (org.drools.core.marshalling.impl.ProtobufMessages.Agenda.RuleFlowGroup.NodeInstance _nodeInstance : _ruleFlowGroup.getNodeInstanceList()) {
            group.addNodeInstance(_nodeInstance.getProcessInstanceId(), _nodeInstance.getNodeInstanceId());
        }
        agenda.getAgendaGroupsMap().put(group.getName(), group);
        if (group.isActive()) {
            agenda.addAgendaGroupOnStack(agenda.getAgendaGroup(group.getName()));
        }
    }
    readActivations(context, _agenda.getMatchList(), _agenda.getRuleActivationList());
    agenda.setActivationsFilter(context.filter);
}
Also used : AgendaGroupQueueImpl(org.drools.core.common.AgendaGroupQueueImpl) InternalAgenda(org.drools.core.common.InternalAgenda)

Example 19 with InternalAgenda

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

the class ProtobufInputMarshaller method createAndInitializeSession.

private static StatefulKnowledgeSessionImpl createAndInitializeSession(MarshallerReaderContext context, int id, Environment environment, SessionConfiguration config, ProtobufMessages.KnowledgeSession _session) throws IOException {
    FactHandleFactory handleFactory = context.kBase.newFactHandleFactory(_session.getRuleData().getLastId(), _session.getRuleData().getLastRecency());
    InternalAgenda agenda = context.kBase.getConfiguration().getComponentFactory().getAgendaFactory().createAgenda(context.kBase, false);
    readAgenda(context, _session.getRuleData(), agenda);
    WorkingMemoryFactory wmFactory = context.kBase.getConfiguration().getComponentFactory().getWorkingMemoryFactory();
    StatefulKnowledgeSessionImpl session = (StatefulKnowledgeSessionImpl) wmFactory.createWorkingMemory(id, context.kBase, handleFactory, // pCTx starts at 1, as InitialFact is 0
    1, config, agenda, environment);
    return session;
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) WorkingMemoryFactory(org.drools.core.common.WorkingMemoryFactory) FactHandleFactory(org.drools.core.spi.FactHandleFactory)

Example 20 with InternalAgenda

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

the class RuleUnitExecutorSession method switchToRuleUnit.

@Override
public void switchToRuleUnit(RuleUnit ruleUnit, Activation activation) {
    String activateUnitName = activation.getRule().getRuleUnitClassName();
    boolean isActiveUnitCurrent = currentRuleUnit != null && currentRuleUnit.getClass().getName().equals(activateUnitName);
    if (isActiveUnitCurrent) {
        if (currentRuleUnit != null) {
            currentRuleUnit.onYield(ruleUnit);
        }
        session.getPropagationList().flush();
        InternalAgenda agenda = session.getAgenda();
        agenda.getStackList().remove(agenda.getAgendaGroup(currentRuleUnit.getClass().getName()));
        unitsStack.push(currentRuleUnit);
        bindRuleUnit(ruleUnit);
    } else {
        for (int i = 0; i < unitsStack.size(); i++) {
            if (unitsStack.get(i).getClass().getName().equals(activateUnitName)) {
                unitsStack.add(i, ruleUnit);
                break;
            }
        }
    }
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda)

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