Search in sources :

Example 1 with InternalAgenda

use of org.drools.core.common.InternalAgenda 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 InternalAgenda

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

the class ProtobufInputMarshaller method readSession.

/**
 * Stream the data into an existing session
 *
 * @param session
 * @param context
 * @return
 * @throws IOException
 * @throws ClassNotFoundException
 */
public static StatefulKnowledgeSessionImpl readSession(StatefulKnowledgeSessionImpl session, MarshallerReaderContext context) throws IOException, ClassNotFoundException {
    ProtobufMessages.KnowledgeSession _session = loadAndParseSession(context);
    InternalAgenda agenda = resetSession(session, context, _session);
    readSession(_session, session, agenda, context);
    return session;
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda)

Example 3 with InternalAgenda

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

the class ProtobufInputMarshaller method resetSession.

private static InternalAgenda resetSession(StatefulKnowledgeSessionImpl session, MarshallerReaderContext context, ProtobufMessages.KnowledgeSession _session) {
    session.reset(_session.getRuleData().getLastId(), _session.getRuleData().getLastRecency(), 1);
    InternalAgenda agenda = (InternalAgenda) session.getAgenda();
    readAgenda(context, _session.getRuleData(), agenda);
    return agenda;
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda)

Example 4 with InternalAgenda

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

the class MarshallingTest method testRuleFlowGroups.

@Test
public void testRuleFlowGroups() throws Exception {
    String rule1 = "package org.drools.compiler.test;\n";
    rule1 += "import org.drools.compiler.Cheese\n";
    rule1 += "global java.util.List list\n";
    rule1 += "rule \"Rule 1\"\n";
    rule1 += "    ruleflow-group \"ruleflow-group-1\"\n";
    rule1 += "when\n";
    rule1 += "    $c : Cheese( ) \n";
    rule1 += "then\n";
    rule1 += "    list.add( \"rule1\" );\n";
    rule1 += "    drools.halt();\n";
    rule1 += "end";
    String rule2 = "package org.drools.compiler.test;\n";
    rule2 += "import org.drools.compiler.Cheese\n";
    rule2 += "global java.util.List list\n";
    rule2 += "rule \"Rule 2\"\n";
    rule2 += "    salience 10\n";
    rule2 += "    ruleflow-group \"ruleflow-group-1\"\n";
    rule2 += "when\n";
    rule2 += "    $c : Cheese( ) \n";
    rule2 += "then\n";
    rule2 += "    list.add( \"rule2\" );\n";
    rule2 += "    drools.halt();\n";
    rule2 += "end";
    String rule3 = "package org.drools.compiler.test;\n";
    rule3 += "import org.drools.compiler.Cheese\n";
    rule3 += "global java.util.List list\n";
    rule3 += "rule \"Rule 3\"\n";
    rule3 += "    salience 10\n";
    rule3 += "    ruleflow-group \"ruleflow-group-2\"\n";
    rule3 += "    activation-group \"activation-group-2\"\n";
    rule3 += "when\n";
    rule3 += "    $c : Cheese( ) \n";
    rule3 += "then\n";
    rule3 += "    list.add( \"rule3\" );\n";
    rule3 += "    drools.halt();\n";
    rule3 += "end";
    String rule4 = "package org.drools.compiler.test;\n";
    rule4 += "import org.drools.compiler.Cheese\n";
    rule4 += "global java.util.List list\n";
    rule4 += "rule \"Rule 4\"\n";
    rule4 += "    ruleflow-group \"ruleflow-group-2\"\n";
    rule4 += "    activation-group \"activation-group-2\"\n";
    rule4 += "when\n";
    rule4 += "    $c : Cheese( ) \n";
    rule4 += "then\n";
    rule4 += "    list.add( \"rule4\" );\n";
    rule4 += "    drools.halt();\n";
    rule4 += "end";
    KieBase kbase = loadKnowledgeBaseFromString(rule1, rule2, rule3, rule4);
    KieSession ksession = getSerialisedStatefulKnowledgeSession(kbase.newKieSession(), true);
    kbase = SerializationHelper.serializeObject(kbase);
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Cheese brie = new Cheese("brie", 12);
    ksession.insert(brie);
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ((InternalAgenda) ksession.getAgenda()).activateRuleFlowGroup("ruleflow-group-1");
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals("rule2", list.get(0));
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ((InternalAgenda) ksession.getAgenda()).activateRuleFlowGroup("ruleflow-group-2");
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals("rule3", list.get(1));
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals("rule1", list.get(2));
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) Cheese(org.drools.compiler.Cheese) Test(org.junit.Test)

Example 5 with InternalAgenda

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

the class RuleFlowGroupTest method testRuleFlowGroupWithLockOnActivate.

@Test
public void testRuleFlowGroupWithLockOnActivate() {
    // JBRULES-3590
    final String str = "import org.drools.compiler.Person;\n" + "import org.drools.compiler.Cheese;\n" + "rule R1\n" + "ruleflow-group \"group1\"\n" + "lock-on-active true\n" + "when\n" + "   $p : Person()\n" + "then\n" + "   $p.setName(\"John\");\n" + "   update ($p);\n" + "end\n" + "rule R2\n" + "ruleflow-group \"group1\"\n" + "lock-on-active true\n" + "when\n" + "   $p : Person( name == null )\n" + "   forall ( Cheese ( type == \"cheddar\" ))\n" + "then\n" + "end";
    final KieBase kbase = loadKnowledgeBaseFromString(str);
    final KieSession ksession = kbase.newKieSession();
    ksession.insert(new Person());
    ksession.insert(new Cheese("gorgonzola"));
    ((InternalAgenda) ksession.getAgenda()).activateRuleFlowGroup("group1");
    assertEquals(1, ksession.fireAllRules());
    ksession.dispose();
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

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