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());
}
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);
}
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);
}
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;
}
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;
}
}
}
}
Aggregations