@Test
  public void testAllLinkedInWithNotNodesOnly() {
    setUp(NOT_NODE);

    assertEquals(NotNode.class, n3.getClass()); // make sure it created NotNodes

    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(RuleEngineOption.PHREAK);
    AbstractWorkingMemory wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));

    BetaMemory bm = (BetaMemory) wm.getNodeMemory(n3);
    createSegmentMemory(n3, wm);
    assertTrue(bm.getSegmentMemory().isSegmentLinked()); // not nodes start off linked

    DefaultFactHandle f1 = (DefaultFactHandle) wm.insert("test1"); // unlinked after first assertion
    n3.assertObject(f1, context, wm);

    // this doesn't unlink on the assertObject, as the node's memory must be processed. So use the
    // helper method the main network evaluator uses.
    PhreakNotNode.unlinkNotNodeOnRightInsert((NotNode) n3, bm, wm);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());

    n3.retractRightTuple(f1.getFirstRightTuple(), context, wm);
    assertTrue(bm.getSegmentMemory().isSegmentLinked());
    // assertFalse( bm.getSegmentMemory().isSigmentLinked() ); // check retraction unlinks again
  }
  @Test
  public void testAllLinkedInWithExistsNodesOnly() {
    setUp(EXISTS_NODE);

    assertEquals(ExistsNode.class, n3.getClass()); // make sure it created ExistsNodes

    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(RuleEngineOption.PHREAK);
    AbstractWorkingMemory wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));
    DefaultFactHandle f1 = (DefaultFactHandle) wm.insert("test1");
    n3.assertObject(f1, context, wm);

    BetaMemory bm = (BetaMemory) wm.getNodeMemory(n3);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());

    n4.assertObject(f1, context, wm);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());

    n5.assertObject(f1, context, wm);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());

    n6.assertObject(f1, context, wm);
    assertTrue(
        bm.getSegmentMemory()
            .isSegmentLinked()); // only after all 4 nodes are populated, is the segment linked in

    n6.retractRightTuple(f1.getLastRightTuple(), context, wm);
    assertFalse(bm.getSegmentMemory().isSegmentLinked()); // check retraction unlinks again
  }
  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());
    }
  }