Search in sources :

Example 1 with AgendaGroup

use of org.drools.core.spi.AgendaGroup in project drools by kiegroup.

the class ProtobufOutputMarshaller method writeAgenda.

private static void writeAgenda(MarshallerWriteContext context, ProtobufMessages.RuleData.Builder _ksb) throws IOException {
    InternalWorkingMemory wm = context.wm;
    InternalAgenda agenda = wm.getAgenda();
    org.drools.core.marshalling.impl.ProtobufMessages.Agenda.Builder _ab = ProtobufMessages.Agenda.newBuilder();
    AgendaGroup[] agendaGroups = agenda.getAgendaGroupsMap().values().toArray(new AgendaGroup[agenda.getAgendaGroupsMap().size()]);
    Arrays.sort(agendaGroups, AgendaGroupSorter.instance);
    for (AgendaGroup ag : agendaGroups) {
        AgendaGroupQueueImpl group = (AgendaGroupQueueImpl) ag;
        org.drools.core.marshalling.impl.ProtobufMessages.Agenda.AgendaGroup.Builder _agb = ProtobufMessages.Agenda.AgendaGroup.newBuilder();
        _agb.setName(group.getName()).setIsActive(group.isActive()).setIsAutoDeactivate(group.isAutoDeactivate()).setClearedForRecency(group.getClearedForRecency()).setHasRuleFlowLister(group.isRuleFlowListener()).setActivatedForRecency(group.getActivatedForRecency());
        Map<Long, String> nodeInstances = group.getNodeInstances();
        for (Map.Entry<Long, String> entry : nodeInstances.entrySet()) {
            org.drools.core.marshalling.impl.ProtobufMessages.Agenda.AgendaGroup.NodeInstance.Builder _nib = ProtobufMessages.Agenda.AgendaGroup.NodeInstance.newBuilder();
            _nib.setProcessInstanceId(entry.getKey());
            _nib.setNodeInstanceId(entry.getValue());
            _agb.addNodeInstance(_nib.build());
        }
        _ab.addAgendaGroup(_agb.build());
    }
    org.drools.core.marshalling.impl.ProtobufMessages.Agenda.FocusStack.Builder _fsb = ProtobufMessages.Agenda.FocusStack.newBuilder();
    LinkedList<AgendaGroup> focusStack = agenda.getStackList();
    for (AgendaGroup group : focusStack) {
        _fsb.addGroupName(group.getName());
    }
    _ab.setFocusStack(_fsb.build());
    // serialize all dormant activations
    org.drools.core.util.Iterator it = ActivationIterator.iterator(wm);
    List<org.drools.core.spi.Activation> dormant = new ArrayList<org.drools.core.spi.Activation>();
    for (org.drools.core.spi.Activation item = (org.drools.core.spi.Activation) it.next(); item != null; item = (org.drools.core.spi.Activation) it.next()) {
        if (!item.isQueued()) {
            dormant.add(item);
        }
    }
    Collections.sort(dormant, ActivationsSorter.INSTANCE);
    for (org.drools.core.spi.Activation activation : dormant) {
        _ab.addMatch(writeActivation(context, (AgendaItem) activation));
    }
    // serialize all network evaluator activations
    for (Activation activation : agenda.getActivations()) {
        if (activation.isRuleAgendaItem()) {
            // serialize it
            _ab.addRuleActivation(writeActivation(context, (AgendaItem) activation));
        }
    }
    _ksb.setAgenda(_ab.build());
}
Also used : AgendaGroup(org.drools.core.spi.AgendaGroup) ArrayList(java.util.ArrayList) AgendaGroupQueueImpl(org.drools.core.common.AgendaGroupQueueImpl) Activation(org.drools.core.spi.Activation) ByteString(com.google.protobuf.ByteString) RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) AgendaItem(org.drools.core.common.AgendaItem) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) InternalAgenda(org.drools.core.common.InternalAgenda) Activation(org.drools.core.spi.Activation) InternalAgenda(org.drools.core.common.InternalAgenda) Map(java.util.Map) ObjectHashMap(org.drools.core.util.ObjectHashMap)

Example 2 with AgendaGroup

use of org.drools.core.spi.AgendaGroup in project drools by kiegroup.

the class DefaultAgenda method setFocus.

public void setFocus(final PropagationContext ctx, final String name) {
    AgendaGroup agendaGroup = getAgendaGroup(name);
    agendaGroup.setAutoFocusActivator(ctx);
    setFocus(agendaGroup);
}
Also used : AgendaGroup(org.drools.core.spi.AgendaGroup)

Example 3 with AgendaGroup

use of org.drools.core.spi.AgendaGroup in project drools by kiegroup.

the class ExecutionFlowControlTest method testLockOnActive.

@Test
public void testLockOnActive() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LockOnActive.drl");
    KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    // AgendaGroup "group1" is not active, so should receive activation
    final Cheese brie12 = new Cheese("brie", 12);
    ksession.insert(brie12);
    ((InternalWorkingMemory) ksession).flushPropagations();
    InternalAgenda agenda = ((InternalAgenda) ksession.getAgenda());
    final AgendaGroup group1 = agenda.getAgendaGroup("group1");
    assertEquals(1, group1.size());
    ksession.getAgenda().getAgendaGroup("group1").setFocus();
    // AgendaqGroup "group1" is now active, so should not receive activations
    final Cheese brie10 = new Cheese("brie", 10);
    ksession.insert(brie10);
    assertEquals(1, group1.size());
    final Cheese cheddar20 = new Cheese("cheddar", 20);
    ksession.insert(cheddar20);
    final AgendaGroup group2 = agenda.getAgendaGroup("group1");
    assertEquals(1, group2.size());
    agenda.setFocus(group2);
    final Cheese cheddar17 = new Cheese("cheddar", 17);
    ksession.insert(cheddar17);
    assertEquals(1, group2.size());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) InternalAgenda(org.drools.core.common.InternalAgenda) AgendaGroup(org.drools.core.spi.AgendaGroup) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) Test(org.junit.Test)

Example 4 with AgendaGroup

use of org.drools.core.spi.AgendaGroup in project drools by kiegroup.

the class ExecutionFlowControlTest method testLockOnActiveWithModify.

@Test
public void testLockOnActiveWithModify() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LockOnActiveWithUpdate.drl");
    KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Cheese brie = new Cheese("brie", 13);
    final Person bob = new Person("bob");
    bob.setCheese(brie);
    final Person mic = new Person("mic");
    mic.setCheese(brie);
    final Person mark = new Person("mark");
    mark.setCheese(brie);
    final FactHandle brieHandle = (FactHandle) ksession.insert(brie);
    ksession.insert(bob);
    ksession.insert(mic);
    ksession.insert(mark);
    InternalWorkingMemory wm = (InternalWorkingMemory) ksession;
    wm.flushPropagations();
    final InternalAgenda agenda = (InternalAgenda) ksession.getAgenda();
    final AgendaGroup group1 = agenda.getAgendaGroup("group1");
    agenda.setFocus(group1);
    assertEquals(1, group1.size());
    RuleAgendaItem ruleItem1 = (RuleAgendaItem) group1.getActivations()[0];
    ruleItem1.getRuleExecutor().evaluateNetwork(wm.getAgenda());
    assertEquals(3, ruleItem1.getRuleExecutor().getLeftTupleList().size());
    agenda.fireNextItem(null, 0, 0);
    assertEquals(1, group1.size());
    assertEquals(2, ruleItem1.getRuleExecutor().getLeftTupleList().size());
    ksession.update(brieHandle, brie);
    assertEquals(1, group1.size());
    ruleItem1.getRuleExecutor().evaluateNetwork(wm.getAgenda());
    assertEquals(2, ruleItem1.getRuleExecutor().getLeftTupleList().size());
    AgendaGroup group2 = agenda.getAgendaGroup("group2");
    agenda.setFocus(group2);
    assertEquals(1, group2.size());
    RuleAgendaItem ruleItem2 = (RuleAgendaItem) group2.getActivations()[0];
    ruleItem2.getRuleExecutor().evaluateNetwork(wm.getAgenda());
    assertEquals(3, ruleItem2.getRuleExecutor().getLeftTupleList().size());
    agenda.fireNextItem(null, 0, 0);
    assertEquals(1, group2.size());
    assertEquals(2, ruleItem2.getRuleExecutor().getLeftTupleList().size());
    ksession.update(brieHandle, brie);
    assertEquals(1, group2.size());
    assertEquals(2, ruleItem2.getRuleExecutor().getLeftTupleList().size());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) InternalAgenda(org.drools.core.common.InternalAgenda) AgendaGroup(org.drools.core.spi.AgendaGroup) RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Aggregations

AgendaGroup (org.drools.core.spi.AgendaGroup)4 ArrayList (java.util.ArrayList)3 InternalAgenda (org.drools.core.common.InternalAgenda)3 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)3 List (java.util.List)2 Cheese (org.drools.compiler.Cheese)2 RuleAgendaItem (org.drools.core.phreak.RuleAgendaItem)2 Test (org.junit.Test)2 KieBase (org.kie.api.KieBase)2 KieSession (org.kie.api.runtime.KieSession)2 ByteString (com.google.protobuf.ByteString)1 Map (java.util.Map)1 Person (org.drools.compiler.Person)1 AgendaGroupQueueImpl (org.drools.core.common.AgendaGroupQueueImpl)1 AgendaItem (org.drools.core.common.AgendaItem)1 Activation (org.drools.core.spi.Activation)1 ObjectHashMap (org.drools.core.util.ObjectHashMap)1 FactHandle (org.kie.api.runtime.rule.FactHandle)1