private static void readActivations(
      MarshallerReaderContext context,
      List<ProtobufMessages.Activation> _dormant,
      List<ProtobufMessages.Activation> _rneas) {

    for (ProtobufMessages.Activation _activation : _dormant) {
      // this is a dormant activation
      context
          .filter
          .getDormantActivationsMap()
          .put(
              PersisterHelper.createActivationKey(
                  _activation.getPackageName(), _activation.getRuleName(), _activation.getTuple()),
              _activation);
    }
    for (ProtobufMessages.Activation _activation : _rneas) {
      // this is an active rule network evaluator
      context
          .filter
          .getRneActivations()
          .put(
              PersisterHelper.createActivationKey(
                  _activation.getPackageName(), _activation.getRuleName(), _activation.getTuple()),
              _activation);
    }
  }
  public static ProtobufMessages.Activation writeActivation(
      MarshallerWriteContext context, AgendaItem agendaItem) {
    ProtobufMessages.Activation.Builder _activation = ProtobufMessages.Activation.newBuilder();

    RuleImpl rule = agendaItem.getRule();
    _activation.setPackageName(rule.getPackage());
    _activation.setRuleName(rule.getName());
    _activation.setTuple(writeTuple(agendaItem.getTuple()));
    _activation.setSalience(agendaItem.getSalience());
    _activation.setIsActivated(agendaItem.isQueued());
    _activation.setEvaluated(agendaItem.isRuleAgendaItem());

    if (agendaItem.getActivationGroupNode() != null) {
      _activation.setActivationGroup(
          agendaItem.getActivationGroupNode().getActivationGroup().getName());
    }

    if (agendaItem.getFactHandle() != null) {
      _activation.setHandleId(agendaItem.getFactHandle().getId());
    }

    org.drools.core.util.LinkedList<LogicalDependency> list = agendaItem.getLogicalDependencies();
    if (list != null && !list.isEmpty()) {
      for (LogicalDependency node = list.getFirst(); node != null; node = node.getNext()) {
        _activation.addLogicalDependency(((BeliefSet) node.getJustified()).getFactHandle().getId());
      }
    }
    return _activation.build();
  }
  private static void writeBeliefSet(
      MarshallerWriteContext context,
      BeliefSet beliefSet,
      org.drools.core.marshalling.impl.ProtobufMessages.EqualityKey.Builder _key)
      throws IOException {

    ProtobufMessages.BeliefSet.Builder _beliefSet = ProtobufMessages.BeliefSet.newBuilder();
    _beliefSet.setHandleId(beliefSet.getFactHandle().getId());

    ObjectMarshallingStrategyStore objectMarshallingStrategyStore =
        context.objectMarshallingStrategyStore;

    // for ( LinkedListEntry node = (LinkedListEntry) beliefSet.getFirst(); node != null; node =
    // (LinkedListEntry) node.getNext() ) {
    FastIterator it = beliefSet.iterator();
    for (LinkedListEntry node = (LinkedListEntry) beliefSet.getFirst();
        node != null;
        node = (LinkedListEntry) it.next(node)) {
      LogicalDependency belief = (LogicalDependency) node.getObject();
      ProtobufMessages.LogicalDependency.Builder _logicalDependency =
          ProtobufMessages.LogicalDependency.newBuilder();
      // _belief.setActivation( value )

      LogicalDependency dependency = (LogicalDependency) node.getObject();
      org.drools.core.spi.Activation activation = dependency.getJustifier();
      ProtobufMessages.Activation _activation =
          ProtobufMessages.Activation.newBuilder()
              .setPackageName(activation.getRule().getPackage())
              .setRuleName(activation.getRule().getName())
              .setTuple(PersisterHelper.createTuple(activation.getTuple()))
              .build();
      _logicalDependency.setActivation(_activation);

      if (belief.getObject() != null) {
        ObjectMarshallingStrategy strategy =
            objectMarshallingStrategyStore.getStrategyObject(belief.getObject());

        Integer index = context.getStrategyIndex(strategy);
        _logicalDependency.setObjectStrategyIndex(index.intValue());
        _logicalDependency.setObject(
            ByteString.copyFrom(
                strategy.marshal(
                    context.strategyContext.get(strategy), context, belief.getObject())));
      }

      if (belief.getValue() != null) {
        ObjectMarshallingStrategy strategy =
            objectMarshallingStrategyStore.getStrategyObject(belief.getValue());

        Integer index = context.getStrategyIndex(strategy);
        _logicalDependency.setValueStrategyIndex(index.intValue());
        _logicalDependency.setValue(
            ByteString.copyFrom(
                strategy.marshal(
                    context.strategyContext.get(strategy), context, belief.getValue())));
      }
      _beliefSet.addLogicalDependency(_logicalDependency.build());
    }
    _key.setBeliefSet(_beliefSet);
  }
  public static ProtobufMessages.Activation writeActivation(
      MarshallerWriteContext context, AgendaItem agendaItem) {
    ProtobufMessages.Activation.Builder _activation = ProtobufMessages.Activation.newBuilder();

    Rule rule = agendaItem.getRule();
    _activation.setPackageName(rule.getPackage());
    _activation.setRuleName(rule.getName());

    ProtobufMessages.Tuple.Builder _tb = ProtobufMessages.Tuple.newBuilder();
    for (LeftTuple entry = agendaItem.getTuple(); entry != null; entry = entry.getParent()) {
      InternalFactHandle handle = entry.getLastHandle();
      _tb.addHandleId(handle.getId());
    }
    _activation.setTuple(_tb.build());

    _activation.setSalience(agendaItem.getSalience());
    _activation.setIsActivated(agendaItem.isActivated());

    if (agendaItem.getActivationGroupNode() != null) {
      _activation.setActivationGroup(
          agendaItem.getActivationGroupNode().getActivationGroup().getName());
    }

    if (agendaItem.getFactHandle() != null) {
      _activation.setHandleId(agendaItem.getFactHandle().getId());
    }

    org.drools.core.util.LinkedList list = agendaItem.getLogicalDependencies();
    if (list != null && !list.isEmpty()) {
      for (LogicalDependency node = (LogicalDependency) list.getFirst();
          node != null;
          node = (LogicalDependency) node.getNext()) {
        _activation.addLogicalDependency(((InternalFactHandle) node.getJustified()).getId());
      }
    }
    return _activation.build();
  }
  public static void writeTruthMaintenanceSystem(
      MarshallerWriteContext context, ProtobufMessages.RuleData.Builder _session)
      throws IOException {
    ObjectHashMap assertMap = context.wm.getTruthMaintenanceSystem().getAssertMap();
    ObjectHashMap justifiedMap = context.wm.getTruthMaintenanceSystem().getJustifiedMap();

    if (!assertMap.isEmpty() || !justifiedMap.isEmpty()) {
      EqualityKey[] keys = new EqualityKey[assertMap.size()];
      org.drools.core.util.Iterator it = assertMap.iterator();
      int i = 0;
      for (org.drools.core.util.ObjectHashMap.ObjectEntry entry =
              (org.drools.core.util.ObjectHashMap.ObjectEntry) it.next();
          entry != null;
          entry = (org.drools.core.util.ObjectHashMap.ObjectEntry) it.next()) {
        EqualityKey key = (EqualityKey) entry.getKey();
        keys[i++] = key;
      }

      Arrays.sort(keys, EqualityKeySorter.instance);

      ProtobufMessages.TruthMaintenanceSystem.Builder _tms =
          ProtobufMessages.TruthMaintenanceSystem.newBuilder();

      // write the assert map of Equality keys
      for (EqualityKey key : keys) {
        ProtobufMessages.EqualityKey.Builder _key = ProtobufMessages.EqualityKey.newBuilder();
        _key.setStatus(key.getStatus());
        _key.setHandleId(key.getFactHandle().getId());
        if (key.getOtherFactHandle() != null && !key.getOtherFactHandle().isEmpty()) {
          for (InternalFactHandle handle : key.getOtherFactHandle()) {
            _key.addOtherHandle(handle.getId());
          }
        }
        _tms.addKey(_key.build());
      }

      it = justifiedMap.iterator();
      i = 0;
      for (org.drools.core.util.ObjectHashMap.ObjectEntry entry =
              (org.drools.core.util.ObjectHashMap.ObjectEntry) it.next();
          entry != null;
          entry = (org.drools.core.util.ObjectHashMap.ObjectEntry) it.next()) {
        ProtobufMessages.Justification.Builder _justification =
            ProtobufMessages.Justification.newBuilder();
        _justification.setHandleId(((Integer) entry.getKey()).intValue());

        org.drools.core.util.LinkedList list = (org.drools.core.util.LinkedList) entry.getValue();
        for (LinkedListEntry node = (LinkedListEntry) list.getFirst();
            node != null;
            node = (LinkedListEntry) node.getNext()) {
          LogicalDependency dependency = (LogicalDependency) node.getObject();
          org.drools.spi.Activation activation = dependency.getJustifier();
          ProtobufMessages.Activation _activation =
              ProtobufMessages.Activation.newBuilder()
                  .setPackageName(activation.getRule().getPackage())
                  .setRuleName(activation.getRule().getName())
                  .setTuple(PersisterHelper.createTuple(activation.getTuple()))
                  .build();
          _justification.addActivation(_activation);
        }
        _tms.addJustification(_justification.build());
      }
      _session.setTms(_tms.build());
    }
  }
  private static void readBeliefSet(
      MarshallerReaderContext context,
      TruthMaintenanceSystem tms,
      EqualityKey key,
      ProtobufMessages.BeliefSet _beliefSet)
      throws IOException, ClassNotFoundException {
    InternalFactHandle handle = (InternalFactHandle) context.handles.get(_beliefSet.getHandleId());
    for (ProtobufMessages.LogicalDependency _logicalDependency :
        _beliefSet.getLogicalDependencyList()) {
      ProtobufMessages.Activation _activation = _logicalDependency.getActivation();
      Activation activation =
          (Activation)
              context
                  .filter
                  .getTuplesCache()
                  .get(
                      PersisterHelper.createActivationKey(
                          _activation.getPackageName(),
                          _activation.getRuleName(),
                          _activation.getTuple()))
                  .getObject();

      Object object = null;
      ObjectMarshallingStrategy strategy = null;
      if (_logicalDependency.hasObjectStrategyIndex()) {
        strategy = context.usedStrategies.get(_logicalDependency.getObjectStrategyIndex());
        object =
            strategy.unmarshal(
                context.strategyContexts.get(strategy),
                context,
                _logicalDependency.getObject().toByteArray(),
                (context.ruleBase == null) ? null : context.ruleBase.getRootClassLoader());
      }

      Object value = null;
      if (_logicalDependency.hasValueStrategyIndex()) {
        strategy = context.usedStrategies.get(_logicalDependency.getValueStrategyIndex());
        value =
            strategy.unmarshal(
                context.strategyContexts.get(strategy),
                context,
                _logicalDependency.getValue().toByteArray(),
                (context.ruleBase == null) ? null : context.ruleBase.getRootClassLoader());
      }

      ObjectTypeConf typeConf =
          context
              .wm
              .getObjectTypeConfigurationRegistry()
              .getObjectTypeConf(
                  ((NamedEntryPoint) handle.getEntryPoint()).getEntryPoint(), handle.getObject());
      tms.readLogicalDependency(
          handle,
          object,
          value,
          activation,
          activation.getPropagationContext(),
          activation.getRule(),
          typeConf);
    }
  }