public static int getOffset(NetworkNode node) {
    LeftTupleSource lt = null;
    int offset = 1;
    if (NodeTypeEnums.isTerminalNode(node)) {
      lt = ((TerminalNode) node).getLeftTupleSource();
      offset++;
    } else if (node.getType() == NodeTypeEnums.RightInputAdaterNode) {
      lt = ((RightInputAdapterNode) node).getLeftTupleSource();
    } else {
      lt = (LeftTupleSource) node;
    }
    while (lt.getType() != NodeTypeEnums.LeftInputAdapterNode) {
      offset++;
      lt = lt.getLeftTupleSource();
    }

    return offset;
  }
  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;
  }
 public static boolean useLeftMemory(LeftTupleSource tupleSource, LeftTuple leftTuple) {
   boolean useLeftMemory = true;
   if (!tupleSource.isLeftTupleMemoryEnabled()) {
     // This is a hack, to not add closed DroolsQuery objects
     Object object = leftTuple.getRootLeftTuple().getLastHandle().getObject();
     if (!(object instanceof DroolsQuery) || !((DroolsQuery) object).isOpen()) {
       useLeftMemory = false;
     }
   }
   return useLeftMemory;
 }