public static void writeTruthMaintenanceSystem(
      MarshallerWriteContext context, EntryPoint wmep, ProtobufMessages.EntryPoint.Builder _epb)
      throws IOException {
    TruthMaintenanceSystem tms = ((NamedEntryPoint) wmep).getTruthMaintenanceSystem();
    ObjectHashMap justifiedMap = tms.getEqualityKeyMap();

    if (!justifiedMap.isEmpty()) {
      EqualityKey[] keys = new EqualityKey[justifiedMap.size()];
      org.drools.core.util.Iterator it = justifiedMap.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.size() > 1) {
          // add all the other key's if they exist
          FastIterator keyIter = key.fastIterator();
          for (DefaultFactHandle handle = key.getFirst().getNext();
              handle != null;
              handle = (DefaultFactHandle) keyIter.next(handle)) {
            _key.addOtherHandle(handle.getId());
          }
        }

        if (key.getBeliefSet() != null) {
          writeBeliefSet(context, key.getBeliefSet(), _key);
        }

        _tms.addKey(_key.build());
      }

      _epb.setTms(_tms.build());
    }
  }
  public static void readTruthMaintenanceSystem(
      MarshallerReaderContext context,
      SessionEntryPoint wmep,
      ProtobufMessages.EntryPoint _ep,
      List<PropagationContextImpl> pctxs)
      throws IOException, ClassNotFoundException {
    TruthMaintenanceSystem tms = ((NamedEntryPoint) wmep).getTruthMaintenanceSystem();

    ProtobufMessages.TruthMaintenanceSystem _tms = _ep.getTms();

    for (ProtobufMessages.EqualityKey _key : _tms.getKeyList()) {
      InternalFactHandle handle = (InternalFactHandle) context.handles.get(_key.getHandleId());

      // ObjectTypeConf state is not marshalled, so it needs to be re-determined
      ObjectTypeConf typeConf =
          context
              .wm
              .getObjectTypeConfigurationRegistry()
              .getObjectTypeConf(
                  ((NamedEntryPoint) handle.getEntryPoint()).getEntryPoint(), handle.getObject());
      if (!typeConf.isTMSEnabled()) {
        typeConf.enableTMS();
      }

      EqualityKey key = new EqualityKey(handle, _key.getStatus());
      handle.setEqualityKey(key);

      if (key.getStatus() == EqualityKey.JUSTIFIED) {
        // not yet added to the object stores
        ((NamedEntryPoint) handle.getEntryPoint())
            .getObjectStore()
            .addHandle(handle, handle.getObject());
        // add handle to object type node
        assertHandleIntoOTN(context, context.wm, handle, pctxs);
      }

      for (Integer factHandleId : _key.getOtherHandleList()) {
        handle = (InternalFactHandle) context.handles.get(factHandleId.intValue());
        key.addFactHandle(handle);
        handle.setEqualityKey(key);
      }
      tms.put(key);

      readBeliefSet(context, tms, key, _key.getBeliefSet());
    }
  }
  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);
    }
  }