private static BetaMemory createSegmentMemory(BetaNode node, InternalWorkingMemory wm) {
   BetaMemory betaMemory = (BetaMemory) wm.getNodeMemory(node);
   if (betaMemory.getSegmentMemory() == null) {
     SegmentUtilities.createSegmentMemory(node, wm);
   }
   return betaMemory;
 }
  @Test
  public void testLiaNodeInitialisation() {
    setUp(JOIN_NODE);
    // Initialise from lian
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(RuleEngineOption.PHREAK);
    AbstractWorkingMemory wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));
    SegmentUtilities.createSegmentMemory(liaNode, wm);
    liaNode.assertObject((InternalFactHandle) wm.insert("str"), context, wm);

    LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    assertEquals(1, liaMem.getNodePosMaskBit());
    assertEquals(3, liaMem.getSegmentMemory().getAllLinkedMaskTest());

    BetaMemory bm1 = (BetaMemory) wm.getNodeMemory(n1);
    assertEquals(2, bm1.getNodePosMaskBit());
    assertEquals(3, bm1.getSegmentMemory().getAllLinkedMaskTest());

    // Initialise from n1
    wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));
    n1.assertObject((InternalFactHandle) wm.insert("str"), context, wm);

    liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    assertEquals(1, liaMem.getNodePosMaskBit());
    assertEquals(3, liaMem.getSegmentMemory().getAllLinkedMaskTest());

    bm1 = (BetaMemory) wm.getNodeMemory(n1);
    assertEquals(2, bm1.getNodePosMaskBit());
    assertEquals(3, bm1.getSegmentMemory().getAllLinkedMaskTest());
  }
  @Test
  public void testLiaNodeLinking() {
    setUp(JOIN_NODE);
    // Initialise from lian
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(RuleEngineOption.PHREAK);
    AbstractWorkingMemory wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));

    SegmentUtilities.createSegmentMemory(liaNode, wm);

    InternalFactHandle fh1 = (InternalFactHandle) wm.insert("str1");
    n1.assertObject(fh1, context, wm);

    LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    assertEquals(1, liaMem.getNodePosMaskBit());
    assertEquals(3, liaMem.getSegmentMemory().getAllLinkedMaskTest());

    BetaMemory bm1 = (BetaMemory) wm.getNodeMemory(n1);
    assertEquals(2, bm1.getNodePosMaskBit());
    assertEquals(3, bm1.getSegmentMemory().getAllLinkedMaskTest());

    // still unlinked
    assertFalse(liaMem.getSegmentMemory().isSegmentLinked());

    // now linked
    InternalFactHandle fh2 = (InternalFactHandle) wm.insert("str2");
    liaNode.assertObject(fh2, context, wm);
    assertTrue(liaMem.getSegmentMemory().isSegmentLinked());

    // test unlink after one retract
    liaNode.retractLeftTuple(fh2.getFirstLeftTuple(), context, wm);
    assertFalse(liaMem.getSegmentMemory().isSegmentLinked());

    // check counter, after multiple asserts
    InternalFactHandle fh3 = (InternalFactHandle) wm.insert("str3");
    InternalFactHandle fh4 = (InternalFactHandle) wm.insert("str4");
    liaNode.assertObject(fh3, context, wm);
    liaNode.assertObject(fh4, context, wm);

    assertTrue(liaMem.getSegmentMemory().isSegmentLinked());

    liaNode.retractLeftTuple(fh3.getFirstLeftTuple(), context, wm);
    assertTrue(liaMem.getSegmentMemory().isSegmentLinked());

    liaNode.retractLeftTuple(fh4.getFirstLeftTuple(), context, wm);
    assertFalse(liaMem.getSegmentMemory().isSegmentLinked());
  }
  private BaseNode[] evalQuery(
      String queryName,
      DroolsQuery queryObject,
      InternalFactHandle handle,
      PropagationContext pCtx) {
    BaseNode[] tnodes = (BaseNode[]) ruleBase.getReteooBuilder().getTerminalNodes(queryName);
    if (this.ruleBase.getConfiguration().isPhreakEnabled()) {
      if (tnodes == null) {
        throw new RuntimeException("Query '" + queryName + "' does not exist");
      }

      QueryTerminalNode tnode = (QueryTerminalNode) tnodes[0];
      LeftTupleSource lts = tnode.getLeftTupleSource();
      while (lts.getType() != NodeTypeEnums.LeftInputAdapterNode) {
        lts = lts.getLeftTupleSource();
      }
      LeftInputAdapterNode lian = (LeftInputAdapterNode) lts;
      LiaNodeMemory lmem = (LiaNodeMemory) getNodeMemory((MemoryFactory) lts);
      SegmentMemory lsmem = lmem.getSegmentMemory();
      if (lsmem == null) {
        lsmem = SegmentUtilities.createSegmentMemory(lts, this);
      }
      LeftInputAdapterNode.doInsertObject(
          handle, pCtx, lian, this, lmem, false, queryObject.isOpen());

      List<PathMemory> rmems = lmem.getSegmentMemory().getPathMemories();
      for (int i = 0, length = rmems.size(); i < length; i++) {
        PathMemory rm = rmems.get(i);

        RuleNetworkEvaluatorActivation evaluator =
            agenda.createRuleNetworkEvaluatorActivation(
                Integer.MAX_VALUE, rm, (TerminalNode) rm.getNetworkNode());
        evaluator.evaluateNetwork(this, 0, -1);
      }
    } else {
      // no need to call retract, as no leftmemory used.
      getEntryPointNode().assertQuery(handle, pCtx, this);

      pCtx.evaluateActionQueue(this);
    }
    return tnodes;
  }