public boolean accept(
     Activation activation,
     PropagationContext context,
     InternalWorkingMemory workingMemory,
     TerminalNode rtn) {
   if (activation.isRuleAgendaItem()) {
     ActivationKey key =
         PersisterHelper.createActivationKey(
             activation.getRule().getPackageName(),
             activation.getRule().getName(),
             activation.getTuple());
     if (!this.rneActivations.containsKey(key)) {
       rneaToFire.add((RuleAgendaItem) activation);
     }
     return true;
   } else {
     ActivationKey key =
         PersisterHelper.createActivationKey(
             rtn.getRule().getPackageName(), rtn.getRule().getName(), activation.getTuple());
     // add the tuple to the cache for correlation
     this.tuplesCache.put(key, activation.getTuple());
     // check if there was an active activation for it
     return !this.dormantActivations.containsKey(key);
   }
 }
  private static void evaluateRuleActivations(StatefulKnowledgeSessionImpl wm) {
    // ET: NOTE: initially we were only resolving partially evaluated rules
    // but some tests fail because of that. Have to resolve all rule agenda items
    // in order to fix the tests

    // find all partially evaluated rule activations
    //        ActivationIterator it = ActivationIterator.iterator( wm );
    //        Set<String> evaluated = new HashSet<String>();
    //        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.isRuleAgendaItem() ) {
    //                evaluated.add( item.getRule().getPackageName()+"."+item.getRule().getName() );
    //            }
    //        }
    // need to evaluate all lazy partially evaluated activations before serializing
    boolean dirty = true;
    while (dirty) {
      for (Activation activation : ((InternalAgenda) wm.getAgenda()).getActivations()) {
        if (activation
            .isRuleAgendaItem() /*&& evaluated.contains( activation.getRule().getPackageName()+"."+activation.getRule().getName() )*/) {
          // evaluate it
          ((RuleAgendaItem) activation).getRuleExecutor().reEvaluateNetwork(wm, null, false);
          ((RuleAgendaItem) activation).getRuleExecutor().removeRuleAgendaItemWhenEmpty(wm);
        }
      }
      dirty = false;
      if (wm.getKnowledgeBase().getConfiguration().isPhreakEnabled()) {
        // network evaluation with phreak and TMS may make previous processed rules dirty again, so
        // need to reprocess until all is flushed.
        for (Activation activation : ((InternalAgenda) wm.getAgenda()).getActivations()) {
          if (activation.isRuleAgendaItem()
              && ((RuleAgendaItem) activation).getRuleExecutor().isDirty()) {
            dirty = true;
            break;
          }
        }
      }
    }
  }
  private static void writeAgenda(
      MarshallerWriteContext context, ProtobufMessages.RuleData.Builder _ksb) throws IOException {
    InternalWorkingMemory wm = context.wm;
    InternalAgenda agenda = (InternalAgenda) wm.getAgenda();

    org.drools.core.marshalling.impl.ProtobufMessages.Agenda.Builder _ab =
        ProtobufMessages.Agenda.newBuilder();

    AgendaGroup[] agendaGroups =
        (AgendaGroup[])
            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 (Iterator<AgendaGroup> it = focusStack.iterator(); it.hasNext(); ) {
      AgendaGroup group = it.next();
      _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());
  }