Program: 12130
Project: 12130
Buggy source:
/** 
 * <p> episodic_memory.cpp:3869:void  epmem_process_query( agent *my_agent,  Symbol *state,  Symbol *pos_query,  Symbol *neg_query,  epmem_time_list& prohibits, epmem_time_id before,  epmem_time_id after,  epmem_symbol_set& currents, soar_module::wme_set& cue_wmes,  soar_module::symbol_triple_list& meta_wmes,  soar_module::symbol_triple_list& retrieval_wmes,  int level=3 )
 * @param state
 * @param query
 * @param neg_query
 * @param prohibit
 * @param before
 * @param after
 * @param currents
 * @param cue_wmes
 * @param meta_wmes
 * @param retrieval_wmes
 * @throws SQLException 
 * @throws SoarException 
 */
private void epmem_process_query(IdentifierImpl state,SymbolImpl pos_query,SymbolImpl neg_query,List<Long> prohibits,long before,long after,Set<SymbolImpl> currents,Set<WmeImpl> cue_wmes,List<SymbolTriple> meta_wmes,List<SymbolTriple> retrieval_wmes,int level) throws SQLException, SoarException {
  if (pos_query == null) {
    epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_status,predefinedSyms.epmem_sym_bad_cmd);
    return;
  }
  if (before != EPMEM_MEMID_NONE && after != EPMEM_MEMID_NONE && before <= after) {
    epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_status,predefinedSyms.epmem_sym_bad_cmd);
    return;
  }
  if (logger.isDebugEnabled()) {
    logger.debug("String_Node_Str");
  }
  stats.last_considered.set(0L);
  stats.last_graph_matches.set(0L);
  if (!prohibits.isEmpty()) {
    Collections.sort(prohibits);
  }
  boolean do_graph_match=(params.graph_match.get() == GraphMatchChoices.on);
  GmOrderingChoices gm_order=params.gm_ordering.get();
  Map<WmeImpl,EpmemLiteral> literal_cache=new LinkedHashMap<WmeImpl,EpmemLiteral>();
  @SuppressWarnings("String_Node_Str") Map<EpmemTriple,EpmemPEdge>[] pedge_caches=new Map[2];
  pedge_caches[0]=new LinkedHashMap<DefaultEpisodicMemory.EpmemTriple,DefaultEpisodicMemory.EpmemPEdge>();
  pedge_caches[1]=new LinkedHashMap<DefaultEpisodicMemory.EpmemTriple,DefaultEpisodicMemory.EpmemPEdge>();
  @SuppressWarnings("String_Node_Str") SortedMap<EpmemTriple,EpmemUEdge> uedge_caches[]=new SortedMap[2];
  uedge_caches[0]=new TreeMap<EpmemTriple,EpmemUEdge>();
  uedge_caches[1]=new TreeMap<EpmemTriple,EpmemUEdge>();
  Set<EpmemInterval> interval_cleanup=new LinkedHashSet<EpmemInterval>();
  EpmemLiteral root_literal=new EpmemLiteral();
  Set<EpmemLiteral> leaf_literals=new LinkedHashSet<EpmemLiteral>();
  PriorityQueue<EpmemPEdge> pedge_pq=new PriorityQueue<EpmemPEdge>(11,new Comparator<EpmemPEdge>(){
    public int compare(    EpmemPEdge a,    EpmemPEdge b){
      if (a.time != b.time) {
        return (a.time < b.time) ? 1 : -1;
      }
 else {
        return (a.triple.compareTo(b.triple));
      }
    }
  }
);
  PriorityQueue<EpmemInterval> interval_pq=new PriorityQueue<EpmemInterval>(11,new Comparator<EpmemInterval>(){
    public int compare(    EpmemInterval a,    EpmemInterval b){
      if (a.time != b.time) {
        return (a.time < b.time) ? 1 : -1;
      }
 else       if (a.is_end_point == b.is_end_point) {
        return (a.uedge.triple.compareTo(b.uedge.triple));
      }
 else {
        return (a.is_end_point == EPMEM_RANGE_START) ? 1 : -1;
      }
    }
  }
);
  Map<SymbolImpl,Integer> symbol_num_incoming=new LinkedHashMap<SymbolImpl,Integer>();
  Map<EpmemSymbolNodePair,Integer> symbol_node_count=new LinkedHashMap<EpmemSymbolNodePair,Integer>();
  long best_episode=EPMEM_MEMID_NONE;
  double best_score=0;
  boolean best_graph_matched=false;
  long best_cardinality=0;
  Map<EpmemLiteral,EpmemNodePair> best_bindings=new LinkedHashMap<EpmemLiteral,EpmemNodePair>();
  double current_score=0;
  long current_cardinality=0;
  LinkedList<EpmemLiteral> gm_ordering=new LinkedList<EpmemLiteral>();
  if (level > 1) {
{
      stats.qry_pos.set(0l);
      stats.qry_neg.set(0l);
      root_literal.id_sym=null;
      root_literal.value_sym=pos_query;
      root_literal.is_neg_q=EPMEM_NODE_POS;
      root_literal.value_is_id=EPMEM_RIT_STATE_EDGE;
      root_literal.is_leaf=false;
      root_literal.is_current=false;
      root_literal.attribute_s_id=EPMEM_NODEID_BAD;
      root_literal.child_n_id=EPMEM_NODEID_ROOT;
      root_literal.weight=0.0;
      root_literal.parents=new LinkedHashSet<EpmemLiteral>();
      root_literal.children=new LinkedHashSet<EpmemLiteral>();
      root_literal.matches=new TreeSet<EpmemNodePair>();
      root_literal.values=new LinkedHashMap<Long,Integer>();
      symbol_num_incoming.put(pos_query,1);
      literal_cache.put(null,root_literal);
      Set<SymbolImpl> visiting=new LinkedHashSet<SymbolImpl>();
      visiting.add(pos_query);
      visiting.add(neg_query);
      for (int query_type=EPMEM_NODE_POS; query_type <= EPMEM_NODE_NEG; query_type++) {
        SymbolImpl query_root=null;
switch (query_type) {
case EPMEM_NODE_POS:
          query_root=pos_query;
        break;
case EPMEM_NODE_NEG:
      query_root=neg_query;
    break;
}
if (query_root == null) {
  continue;
}
List<WmeImpl> children=epmem_get_augs_of_id(query_root,DefaultMarker.create());
for (WmeImpl wme_iter : children) {
  EpmemLiteral child=epmem_build_dnf(wme_iter,literal_cache,leaf_literals,symbol_num_incoming,gm_ordering,currents,query_type,visiting,cue_wmes);
  if (child != null) {
    child.id_sym=pos_query;
    child.parents.add(root_literal);
    root_literal.children.add(child);
  }
}
}
stats.qry_lits.set(stats.qry_pos.get() + stats.qry_neg.get());
}
double perfect_score=0;
int perfect_cardinality=0;
for (EpmemLiteral iter : leaf_literals) {
if (iter.is_neg_q == 0) {
perfect_score+=iter.weight;
perfect_cardinality++;
}
}
if (before == EPMEM_MEMID_NONE) {
before=stats.time.get() - 1;
}
 else {
before=before - 1;
}
long current_episode=before;
long next_episode;
{
EpmemTriple triple=new EpmemTriple(EPMEM_NODEID_BAD,EPMEM_NODEID_BAD,EPMEM_NODEID_ROOT);
EpmemPEdge root_pedge=new EpmemPEdge();
root_pedge.triple=triple.copyEpmemTriple();
root_pedge.value_is_id=EPMEM_RIT_STATE_EDGE;
root_pedge.has_noncurrent=false;
root_pedge.literals=new ConcurrentSkipListSet<EpmemLiteral>();
root_pedge.literals.add(root_literal);
root_pedge.sql=db.pool_dummy.getCopy();
root_pedge.sql.setLong(1,Long.MAX_VALUE);
root_pedge.sqlResults=root_pedge.sql.executeQuery();
root_pedge.time=Long.MAX_VALUE;
pedge_pq.add(root_pedge);
pedge_caches[EPMEM_RIT_STATE_EDGE].put(triple,root_pedge);
EpmemUEdge root_uedge=new EpmemUEdge();
root_uedge.triple=triple.copyEpmemTriple();
root_uedge.value_is_id=EPMEM_RIT_STATE_EDGE;
root_uedge.has_noncurrent=false;
root_uedge.activation_count=0;
root_uedge.pedges=new LinkedHashSet<EpmemPEdge>();
root_uedge.intervals=1;
root_uedge.activated=false;
uedge_caches[EPMEM_RIT_STATE_EDGE].put(triple,root_uedge);
EpmemInterval root_interval=new EpmemInterval();
root_interval.uedge=root_uedge;
root_interval.is_end_point=1;
root_interval.sql=db.pool_dummy.getCopy();
root_interval.sql.setLong(1,before);
root_interval.sqlResult=root_interval.sql.executeQuery();
root_interval.time=before;
interval_pq.add(root_interval);
interval_cleanup.add(root_interval);
}
if (logger.isDebugEnabled()) {
logger.debug(epmem_print_retrieval_state(literal_cache,pedge_caches,uedge_caches));
}
while (pedge_pq.size() != 0 && current_episode > after) {
long next_edge;
long next_interval;
boolean changed_score=false;
next_edge=pedge_pq.peek().time;
while ((pedge_pq.size() != 0) && (pedge_pq.peek().time == next_edge || pedge_pq.peek().time >= current_episode)) {
final EpmemPEdge pedge=pedge_pq.poll();
final EpmemTriple triple=pedge.triple.copyEpmemTriple();
if (pedge.sqlResults.getMetaData().getColumnCount() > 1) {
  triple.child_n_id=pedge.sqlResults.getLong(1 + 1);
}
 else {
  triple.child_n_id=0L;
}
if (logger.isDebugEnabled()) {
  logger.debug("String_Node_Str" + triple.parent_n_id + "String_Node_Str"+ triple.attribute_s_id+ "String_Node_Str"+ triple.child_n_id);
}
if (pedge.value_is_id != 0) {
  @SuppressWarnings("String_Node_Str") boolean created=false;
  for (  EpmemLiteral literal_iter : pedge.literals) {
    EpmemLiteral literal=literal_iter;
    for (    EpmemLiteral child_iter : literal.children) {
      created|=epmem_register_pedges(triple.child_n_id,child_iter,pedge_pq,after,pedge_caches,uedge_caches);
    }
  }
}
Map<EpmemTriple,EpmemUEdge> uedge_cache=uedge_caches[pedge.value_is_id];
EpmemUEdge uedge_iter=uedge_cache.get(triple);
if (uedge_iter == null) {
  EpmemUEdge uedge=new EpmemUEdge();
  uedge.triple=triple.copyEpmemTriple();
  uedge.value_is_id=pedge.value_is_id;
  uedge.has_noncurrent=pedge.has_noncurrent;
  uedge.activation_count=0;
  uedge.pedges=new LinkedHashSet<EpmemPEdge>();
  uedge.intervals=0;
  uedge.activated=false;
  boolean created=false;
  long edge_id=pedge.sqlResults.getLong(0 + 1);
  long promo_time=EPMEM_MEMID_NONE;
  boolean is_lti=(pedge.value_is_id != 0 && pedge.triple.child_n_id != EPMEM_NODEID_BAD && pedge.triple.child_n_id != EPMEM_NODEID_ROOT);
  if (is_lti) {
    db.find_lti_promotion_time.setLong(1,triple.child_n_id);
    ResultSet results=db.find_lti_promotion_time.executeQuery();
    if (results.next()) {
      promo_time=results.getLong(0 + 1);
    }
    results.close();
  }
  for (int interval_type=EPMEM_RANGE_EP; interval_type <= EPMEM_RANGE_POINT; interval_type++) {
    for (int point_type=EPMEM_RANGE_START; point_type <= EPMEM_RANGE_END; point_type++) {
      PreparedStatement interval_sql=null;
      if (is_lti) {
        interval_sql=db.pool_find_lti_queries[point_type][interval_type].getCopy();
      }
 else {
        interval_sql=db.pool_find_interval_queries[pedge.value_is_id][point_type][interval_type].getCopy();
      }
      int bind_pos=1;
      if (point_type == EPMEM_RANGE_END && interval_type == EPMEM_RANGE_NOW) {
        interval_sql.setLong(bind_pos++,current_episode);
      }
      interval_sql.setLong(bind_pos++,edge_id);
      if (is_lti && interval_type == EPMEM_RANGE_EP) {
        interval_sql.setLong(bind_pos++,promo_time);
      }
      interval_sql.setLong(bind_pos++,current_episode);
      ResultSet results=interval_sql.executeQuery();
      if (results.next()) {
        EpmemInterval interval=new EpmemInterval();
        interval.is_end_point=point_type;
        interval.uedge=uedge;
        interval.time=results.getLong(0 + 1);
        if (is_lti && point_type == EPMEM_RANGE_START && interval_type != EPMEM_RANGE_POINT && interval.time < promo_time) {
          interval.time=promo_time - 1;
        }
        interval.sql=interval_sql;
        interval.sqlResult=results;
        interval_pq.add(interval);
        interval_cleanup.add(interval);
        uedge.intervals++;
        created=true;
      }
 else {
        results.close();
        interval_sql.close();
      }
    }
  }
  if (created) {
    if (is_lti) {
      EpmemInterval start_interval=new EpmemInterval();
      start_interval.uedge=uedge;
      start_interval.is_end_point=EPMEM_RANGE_START;
      start_interval.time=promo_time - 1;
      start_interval.sql=null;
      interval_pq.add(start_interval);
      interval_cleanup.add(start_interval);
    }
    uedge.pedges.add(pedge);
    uedge_cache.put(triple.copyEpmemTriple(),uedge);
  }
 else {
    uedge.pedges.clear();
    uedge.pedges=null;
  }
}
 else {
  EpmemUEdge uedge=uedge_iter;
  uedge.pedges.add(pedge);
  if (uedge.activated && uedge.activation_count == 1) {
    for (    EpmemLiteral lit_iter : pedge.literals) {
      EpmemLiteral literal=lit_iter;
      ByRef<Double> curScoreRef=new ByRef<Double>(current_score);
      ByRef<Long> curCardinalityRef=new ByRef<Long>(current_cardinality);
      changed_score|=epmem_satisfy_literal(literal,triple.parent_n_id,triple.child_n_id,curScoreRef,curCardinalityRef,symbol_node_count,uedge_caches,symbol_num_incoming);
      current_score=curScoreRef.value;
      current_cardinality=curCardinalityRef.value;
    }
  }
}
if (pedge.sql != null) {
  if (pedge.sqlResults == null) {
    ResultSet results=pedge.sql.executeQuery();
    pedge.sqlResults=results;
  }
  if (pedge.sqlResults.next()) {
    if (pedge.sqlResults.getMetaData().getColumnCount() > 2) {
      pedge.time=pedge.sqlResults.getLong(2 + 1);
    }
 else {
      pedge.time=0L;
    }
    pedge_pq.add(pedge);
  }
 else {
    pedge.sqlResults.close();
    pedge.sqlResults=null;
    pedge.sql.close();
    pedge.sql=null;
  }
}
}
next_edge=(pedge_pq.isEmpty() ? after : pedge_pq.peek().time);
while (interval_pq.size() != 0 && interval_pq.peek().time > next_edge && current_episode > after) {
if (logger.isDebugEnabled()) {
  logger.debug("String_Node_Str" + current_episode);
}
while (interval_pq.size() != 0 && interval_pq.peek().time >= current_episode) {
  EpmemInterval interval=interval_pq.poll();
  EpmemUEdge uedge=interval.uedge;
  EpmemTriple triple=uedge.triple.copyEpmemTriple();
  if (logger.isDebugEnabled()) {
    logger.debug("String_Node_Str" + (interval.is_end_point != 0 ? "String_Node_Str" : "String_Node_Str") + "String_Node_Str"+ triple.parent_n_id+ "String_Node_Str"+ triple.attribute_s_id+ "String_Node_Str"+ triple.child_n_id);
  }
  if (interval.is_end_point != 0) {
    uedge.activated=true;
    uedge.activation_count++;
    if (uedge.activation_count == 1) {
      for (      EpmemPEdge pedge_iter : uedge.pedges) {
        EpmemPEdge pedge=pedge_iter;
        for (        EpmemLiteral lit_iter : pedge.literals) {
          EpmemLiteral literal=lit_iter;
          ByRef<Double> curScoreRef=new ByRef<Double>(current_score);
          ByRef<Long> curCardinalityRef=new ByRef<Long>(current_cardinality);
          changed_score|=epmem_satisfy_literal(literal,triple.parent_n_id,triple.child_n_id,curScoreRef,curCardinalityRef,symbol_node_count,uedge_caches,symbol_num_incoming);
          current_score=curScoreRef.value;
          current_cardinality=curCardinalityRef.value;
        }
      }
    }
  }
 else {
    uedge.activated=false;
    uedge.activation_count--;
    for (    EpmemPEdge pedge_iter : uedge.pedges) {
      EpmemPEdge pedge=pedge_iter;
      for (      EpmemLiteral lit_iter : pedge.literals) {
        ByRef<Double> curScore=new ByRef<Double>(current_score);
        ByRef<Long> curCardinality=new ByRef<Long>(current_cardinality);
        changed_score|=epmem_unsatisfy_literal(lit_iter,triple.parent_n_id,triple.child_n_id,curScore,curCardinality,symbol_node_count);
        current_score=curScore.value;
        current_cardinality=curCardinality.value;
      }
    }
  }
  if (interval.sql != null) {
    if (interval.sqlResult == null) {
      interval.sqlResult=interval.sql.executeQuery();
    }
    if (interval.uedge.has_noncurrent && interval.sqlResult.next()) {
      interval.time=interval.sqlResult.getInt(0 + 1);
      interval_pq.add(interval);
    }
 else {
      interval.sqlResult.close();
      interval.sqlResult=null;
      interval.sql.close();
      interval.sql=null;
      uedge.intervals--;
      if (uedge.intervals != 0) {
        interval_cleanup.remove(interval);
      }
 else {
      }
    }
  }
}
next_interval=(interval_pq.isEmpty() ? after : interval_pq.peek().time);
next_episode=(next_edge > next_interval ? next_edge : next_interval);
while (prohibits.size() != 0 && prohibits.get(prohibits.size() - 1) > current_episode) {
  prohibits.remove(prohibits.size() - 1);
}
while (prohibits.size() != 0 && current_episode > next_episode && current_episode == prohibits.get(prohibits.size() - 1)) {
  current_episode--;
  prohibits.remove(prohibits.size() - 1);
}
if (logger.isTraceEnabled()) {
  logger.trace(epmem_print_retrieval_state(literal_cache,pedge_caches,uedge_caches));
}
stats.considered.set(stats.considered.get() + 1);
stats.last_considered.set(stats.last_considered.get() + 1);
trace.startNewLine().print(Category.EPMEM,"String_Node_Str" + current_episode + "String_Node_Str"+ current_cardinality+ "String_Node_Str"+ current_score+ "String_Node_Str");
if (current_episode > next_episode && changed_score && (best_episode == EPMEM_MEMID_NONE || current_score > best_score || (do_graph_match && current_score == best_score && !best_graph_matched))) {
  boolean new_king=false;
  if (best_episode == EPMEM_MEMID_NONE || current_score > best_score) {
    best_episode=current_episode;
    best_score=current_score;
    best_cardinality=current_cardinality;
    new_king=true;
  }
  if (current_cardinality == perfect_cardinality) {
    boolean graph_matched=false;
    if (do_graph_match) {
      if (gm_order == DefaultEpisodicMemoryParams.GmOrderingChoices.undefined) {
        Collections.shuffle(gm_ordering,random);
      }
 else       if (gm_order == DefaultEpisodicMemoryParams.GmOrderingChoices.mcv) {
        Collections.sort(gm_ordering,new Comparator<EpmemLiteral>(){
          @Override public int compare(          EpmemLiteral a,          EpmemLiteral b){
            return (a.matches.size() < b.matches.size() ? -1 : 1);
          }
        }
);
      }
      best_bindings.clear();
      @SuppressWarnings("String_Node_Str") Map<Long,SymbolImpl>[] bound_nodes=(Map<Long,SymbolImpl>[])new Map[2];
      for (int i=0; i < bound_nodes.length; i++) {
        bound_nodes[i]=new LinkedHashMap<Long,SymbolImpl>();
      }
      if (logger.isDebugEnabled()) {
        logger.debug("String_Node_Str");
        logger.debug(epmem_print_retrieval_state(literal_cache,pedge_caches,uedge_caches));
      }
      stats.graph_matches.set(stats.graph_matches.get() + 1);
      stats.last_graph_matches.set(stats.last_graph_matches.get() + 1);
      graph_matched=epmem_graph_match(gm_ordering,gm_ordering.listIterator(),best_bindings,bound_nodes,2);
    }
    if (!do_graph_match || graph_matched) {
      best_episode=current_episode;
      best_graph_matched=true;
      current_episode=EPMEM_MEMID_NONE;
      new_king=true;
    }
  }
  if (new_king) {
    trace.startNewLine().print(Category.EPMEM,"String_Node_Str" + Boolean.toString(current_cardinality == perfect_cardinality) + "String_Node_Str"+ Boolean.toString(best_graph_matched)+ "String_Node_Str");
  }
}
if (current_episode == EPMEM_MEMID_NONE) {
  break;
}
 else {
  current_episode=next_episode;
}
}
}
if (best_episode == EPMEM_MEMID_NONE) {
epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_failure,pos_query);
if (neg_query != null) {
epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_failure,neg_query);
}
}
 else {
stats.qry_ret.set(best_episode);
stats.qry_card.set(best_cardinality);
SymbolImpl temp_sym;
Map<Long,SymbolImpl> node_map_map=new LinkedHashMap<Long,SymbolImpl>();
Map<Long,SymbolImpl> node_mem_map=new LinkedHashMap<Long,SymbolImpl>();
temp_sym=symbols.createInteger(leaf_literals.size());
epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_cue_size,temp_sym);
temp_sym=symbols.createInteger(best_cardinality);
epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_match_cardinality,temp_sym);
temp_sym=symbols.createDouble(best_score);
epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_match_score,temp_sym);
temp_sym=symbols.createDouble(best_score / perfect_score);
epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_normalized_match_score,temp_sym);
epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_success,pos_query);
if (neg_query != null) {
epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_success,neg_query);
}
if (do_graph_match) {
temp_sym=symbols.createInteger((best_graph_matched ? 1 : 0));
epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_graph_match,temp_sym);
if (best_graph_matched) {
  @SuppressWarnings("String_Node_Str") int levelLocal=epmem_info(state).epmem_result_header.level;
  SymbolImpl mapping=symbols.make_new_identifier('M',level);
  epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_graph_match_mapping,mapping);
  for (  Entry<EpmemLiteral,EpmemNodePair> iter : best_bindings.entrySet()) {
    if (iter.getKey().value_is_id != 0) {
      temp_sym=symbols.make_new_identifier('N',level);
      epmem_buffer_add_wme(meta_wmes,mapping,predefinedSyms.epmem_sym_graph_match_mapping_node,temp_sym);
      epmem_buffer_add_wme(meta_wmes,temp_sym,predefinedSyms.epmem_sym_graph_match_mapping_cue,iter.getKey().value_sym);
      node_map_map.put(iter.getValue().second,temp_sym);
      node_mem_map.put(iter.getValue().second,null);
    }
  }
}
}
if (level > 2) {
epmem_install_memory(state,best_episode,meta_wmes,retrieval_wmes,node_mem_map);
}
if (best_graph_matched) {
for (Entry<Long,SymbolImpl> iter : node_mem_map.entrySet()) {
  SymbolImpl map_iter=node_map_map.get(iter.getKey());
  if (map_iter != null && iter.getValue() != null) {
    epmem_buffer_add_wme(meta_wmes,map_iter,predefinedSyms.epmem_sym_retrieved,iter.getValue());
  }
}
}
}
}
for (EpmemInterval interval : interval_cleanup) {
if (interval.sqlResult != null) {
interval.sqlResult.close();
interval.sqlResult=null;
interval.sql.close();
interval.sql=null;
}
}
for (int type=EPMEM_RIT_STATE_NODE; type <= EPMEM_RIT_STATE_EDGE; type++) {
for (EpmemPEdge pedge : pedge_caches[type].values()) {
if (pedge.sqlResults != null) {
pedge.sqlResults.close();
pedge.sqlResults=null;
pedge.sql.close();
pedge.sql=null;
}
}
}
}

Fixed version (ground truth):
/** 
 * <p> episodic_memory.cpp:3869:void  epmem_process_query( agent *my_agent,  Symbol *state,  Symbol *pos_query,  Symbol *neg_query,  epmem_time_list& prohibits, epmem_time_id before,  epmem_time_id after,  epmem_symbol_set& currents, soar_module::wme_set& cue_wmes,  soar_module::symbol_triple_list& meta_wmes,  soar_module::symbol_triple_list& retrieval_wmes,  int level=3 )
 * @param state
 * @param query
 * @param neg_query
 * @param prohibit
 * @param before
 * @param after
 * @param currents
 * @param cue_wmes
 * @param meta_wmes
 * @param retrieval_wmes
 * @throws SQLException 
 * @throws SoarException 
 */
private void epmem_process_query(IdentifierImpl state,SymbolImpl pos_query,SymbolImpl neg_query,List<Long> prohibits,long before,long after,Set<SymbolImpl> currents,Set<WmeImpl> cue_wmes,List<SymbolTriple> meta_wmes,List<SymbolTriple> retrieval_wmes,int level) throws SQLException, SoarException {
  if (pos_query == null) {
    epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_status,predefinedSyms.epmem_sym_bad_cmd);
    return;
  }
  if (before != EPMEM_MEMID_NONE && after != EPMEM_MEMID_NONE && before <= after) {
    epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_status,predefinedSyms.epmem_sym_bad_cmd);
    return;
  }
  if (logger.isDebugEnabled()) {
    logger.debug("String_Node_Str");
  }
  stats.last_considered.set(0L);
  stats.last_graph_matches.set(0L);
  if (!prohibits.isEmpty()) {
    Collections.sort(prohibits);
  }
  boolean do_graph_match=(params.graph_match.get() == GraphMatchChoices.on);
  GmOrderingChoices gm_order=params.gm_ordering.get();
  Map<WmeImpl,EpmemLiteral> literal_cache=new LinkedHashMap<WmeImpl,EpmemLiteral>();
  @SuppressWarnings("String_Node_Str") Map<EpmemTriple,EpmemPEdge>[] pedge_caches=new Map[2];
  pedge_caches[0]=new LinkedHashMap<DefaultEpisodicMemory.EpmemTriple,DefaultEpisodicMemory.EpmemPEdge>();
  pedge_caches[1]=new LinkedHashMap<DefaultEpisodicMemory.EpmemTriple,DefaultEpisodicMemory.EpmemPEdge>();
  @SuppressWarnings("String_Node_Str") SortedMap<EpmemTriple,EpmemUEdge> uedge_caches[]=new SortedMap[2];
  uedge_caches[0]=new TreeMap<EpmemTriple,EpmemUEdge>();
  uedge_caches[1]=new TreeMap<EpmemTriple,EpmemUEdge>();
  Set<EpmemInterval> interval_cleanup=new LinkedHashSet<EpmemInterval>();
  EpmemLiteral root_literal=new EpmemLiteral();
  Set<EpmemLiteral> leaf_literals=new LinkedHashSet<EpmemLiteral>();
  PriorityQueue<EpmemPEdge> pedge_pq=new PriorityQueue<EpmemPEdge>(11,new Comparator<EpmemPEdge>(){
    public int compare(    EpmemPEdge a,    EpmemPEdge b){
      if (a.time != b.time) {
        return (a.time < b.time) ? 1 : -1;
      }
 else {
        return (a.triple.compareTo(b.triple));
      }
    }
  }
);
  PriorityQueue<EpmemInterval> interval_pq=new PriorityQueue<EpmemInterval>(11,new Comparator<EpmemInterval>(){
    public int compare(    EpmemInterval a,    EpmemInterval b){
      if (a.time != b.time) {
        return (a.time < b.time) ? 1 : -1;
      }
 else       if (a.is_end_point == b.is_end_point) {
        return (a.uedge.triple.compareTo(b.uedge.triple));
      }
 else {
        return (a.is_end_point == EPMEM_RANGE_END) ? 1 : -1;
      }
    }
  }
);
  Map<SymbolImpl,Integer> symbol_num_incoming=new LinkedHashMap<SymbolImpl,Integer>();
  Map<EpmemSymbolNodePair,Integer> symbol_node_count=new LinkedHashMap<EpmemSymbolNodePair,Integer>();
  long best_episode=EPMEM_MEMID_NONE;
  double best_score=0;
  boolean best_graph_matched=false;
  long best_cardinality=0;
  Map<EpmemLiteral,EpmemNodePair> best_bindings=new LinkedHashMap<EpmemLiteral,EpmemNodePair>();
  double current_score=0;
  long current_cardinality=0;
  LinkedList<EpmemLiteral> gm_ordering=new LinkedList<EpmemLiteral>();
  if (level > 1) {
{
      stats.qry_pos.set(0l);
      stats.qry_neg.set(0l);
      root_literal.id_sym=null;
      root_literal.value_sym=pos_query;
      root_literal.is_neg_q=EPMEM_NODE_POS;
      root_literal.value_is_id=EPMEM_RIT_STATE_EDGE;
      root_literal.is_leaf=false;
      root_literal.is_current=false;
      root_literal.attribute_s_id=EPMEM_NODEID_BAD;
      root_literal.child_n_id=EPMEM_NODEID_ROOT;
      root_literal.weight=0.0;
      root_literal.parents=new LinkedHashSet<EpmemLiteral>();
      root_literal.children=new LinkedHashSet<EpmemLiteral>();
      root_literal.matches=new TreeSet<EpmemNodePair>();
      root_literal.values=new LinkedHashMap<Long,Integer>();
      symbol_num_incoming.put(pos_query,1);
      literal_cache.put(null,root_literal);
      Set<SymbolImpl> visiting=new LinkedHashSet<SymbolImpl>();
      visiting.add(pos_query);
      visiting.add(neg_query);
      for (int query_type=EPMEM_NODE_POS; query_type <= EPMEM_NODE_NEG; query_type++) {
        SymbolImpl query_root=null;
switch (query_type) {
case EPMEM_NODE_POS:
          query_root=pos_query;
        break;
case EPMEM_NODE_NEG:
      query_root=neg_query;
    break;
}
if (query_root == null) {
  continue;
}
List<WmeImpl> children=epmem_get_augs_of_id(query_root,DefaultMarker.create());
for (WmeImpl wme_iter : children) {
  EpmemLiteral child=epmem_build_dnf(wme_iter,literal_cache,leaf_literals,symbol_num_incoming,gm_ordering,currents,query_type,visiting,cue_wmes);
  if (child != null) {
    child.id_sym=pos_query;
    child.parents.add(root_literal);
    root_literal.children.add(child);
  }
}
}
stats.qry_lits.set(stats.qry_pos.get() + stats.qry_neg.get());
}
double perfect_score=0;
int perfect_cardinality=0;
for (EpmemLiteral iter : leaf_literals) {
if (iter.is_neg_q == 0) {
perfect_score+=iter.weight;
perfect_cardinality++;
}
}
if (before == EPMEM_MEMID_NONE) {
before=stats.time.get() - 1;
}
 else {
before=before - 1;
}
long current_episode=before;
long next_episode;
{
EpmemTriple triple=new EpmemTriple(EPMEM_NODEID_BAD,EPMEM_NODEID_BAD,EPMEM_NODEID_ROOT);
EpmemPEdge root_pedge=new EpmemPEdge();
root_pedge.triple=triple.copyEpmemTriple();
root_pedge.value_is_id=EPMEM_RIT_STATE_EDGE;
root_pedge.has_noncurrent=false;
root_pedge.literals=new ConcurrentSkipListSet<EpmemLiteral>();
root_pedge.literals.add(root_literal);
root_pedge.sql=db.pool_dummy.getCopy();
root_pedge.sql.setLong(1,Long.MAX_VALUE);
root_pedge.sqlResults=root_pedge.sql.executeQuery();
root_pedge.time=Long.MAX_VALUE;
pedge_pq.add(root_pedge);
pedge_caches[EPMEM_RIT_STATE_EDGE].put(triple,root_pedge);
EpmemUEdge root_uedge=new EpmemUEdge();
root_uedge.triple=triple.copyEpmemTriple();
root_uedge.value_is_id=EPMEM_RIT_STATE_EDGE;
root_uedge.has_noncurrent=false;
root_uedge.activation_count=0;
root_uedge.pedges=new LinkedHashSet<EpmemPEdge>();
root_uedge.intervals=1;
root_uedge.activated=false;
uedge_caches[EPMEM_RIT_STATE_EDGE].put(triple,root_uedge);
EpmemInterval root_interval=new EpmemInterval();
root_interval.uedge=root_uedge;
root_interval.is_end_point=1;
root_interval.sql=db.pool_dummy.getCopy();
root_interval.sql.setLong(1,before);
root_interval.sqlResult=root_interval.sql.executeQuery();
root_interval.time=before;
interval_pq.add(root_interval);
interval_cleanup.add(root_interval);
}
if (logger.isDebugEnabled()) {
logger.debug(epmem_print_retrieval_state(literal_cache,pedge_caches,uedge_caches));
}
while (pedge_pq.size() != 0 && current_episode > after) {
long next_edge;
long next_interval;
boolean changed_score=false;
next_edge=pedge_pq.peek().time;
while ((pedge_pq.size() != 0) && (pedge_pq.peek().time == next_edge || pedge_pq.peek().time >= current_episode)) {
final EpmemPEdge pedge=pedge_pq.poll();
final EpmemTriple triple=pedge.triple.copyEpmemTriple();
if (pedge.sqlResults.getMetaData().getColumnCount() > 1) {
  triple.child_n_id=pedge.sqlResults.getLong(1 + 1);
}
 else {
  triple.child_n_id=0L;
}
if (logger.isDebugEnabled()) {
  logger.debug("String_Node_Str" + triple.parent_n_id + "String_Node_Str"+ triple.attribute_s_id+ "String_Node_Str"+ triple.child_n_id);
}
if (pedge.value_is_id != 0) {
  @SuppressWarnings("String_Node_Str") boolean created=false;
  for (  EpmemLiteral literal_iter : pedge.literals) {
    EpmemLiteral literal=literal_iter;
    for (    EpmemLiteral child_iter : literal.children) {
      created|=epmem_register_pedges(triple.child_n_id,child_iter,pedge_pq,after,pedge_caches,uedge_caches);
    }
  }
}
Map<EpmemTriple,EpmemUEdge> uedge_cache=uedge_caches[pedge.value_is_id];
EpmemUEdge uedge_iter=uedge_cache.get(triple);
if (uedge_iter == null) {
  EpmemUEdge uedge=new EpmemUEdge();
  uedge.triple=triple.copyEpmemTriple();
  uedge.value_is_id=pedge.value_is_id;
  uedge.has_noncurrent=pedge.has_noncurrent;
  uedge.activation_count=0;
  uedge.pedges=new LinkedHashSet<EpmemPEdge>();
  uedge.intervals=0;
  uedge.activated=false;
  boolean created=false;
  long edge_id=pedge.sqlResults.getLong(0 + 1);
  long promo_time=EPMEM_MEMID_NONE;
  boolean is_lti=(pedge.value_is_id != 0 && pedge.triple.child_n_id != EPMEM_NODEID_BAD && pedge.triple.child_n_id != EPMEM_NODEID_ROOT);
  if (is_lti) {
    db.find_lti_promotion_time.setLong(1,triple.child_n_id);
    ResultSet results=db.find_lti_promotion_time.executeQuery();
    if (results.next()) {
      promo_time=results.getLong(0 + 1);
    }
    results.close();
  }
  for (int interval_type=EPMEM_RANGE_EP; interval_type <= EPMEM_RANGE_POINT; interval_type++) {
    for (int point_type=EPMEM_RANGE_START; point_type <= EPMEM_RANGE_END; point_type++) {
      PreparedStatement interval_sql=null;
      if (is_lti) {
        interval_sql=db.pool_find_lti_queries[point_type][interval_type].getCopy();
      }
 else {
        interval_sql=db.pool_find_interval_queries[pedge.value_is_id][point_type][interval_type].getCopy();
      }
      int bind_pos=1;
      if (point_type == EPMEM_RANGE_END && interval_type == EPMEM_RANGE_NOW) {
        interval_sql.setLong(bind_pos++,current_episode);
      }
      interval_sql.setLong(bind_pos++,edge_id);
      if (is_lti && interval_type == EPMEM_RANGE_EP) {
        interval_sql.setLong(bind_pos++,promo_time);
      }
      interval_sql.setLong(bind_pos++,current_episode);
      ResultSet results=interval_sql.executeQuery();
      if (results.next()) {
        EpmemInterval interval=new EpmemInterval();
        interval.is_end_point=point_type;
        interval.uedge=uedge;
        interval.time=results.getLong(0 + 1);
        if (is_lti && point_type == EPMEM_RANGE_START && interval_type != EPMEM_RANGE_POINT && interval.time < promo_time) {
          interval.time=promo_time - 1;
        }
        interval.sql=interval_sql;
        interval.sqlResult=results;
        interval_pq.add(interval);
        interval_cleanup.add(interval);
        uedge.intervals++;
        created=true;
      }
 else {
        results.close();
        interval_sql.close();
      }
    }
  }
  if (created) {
    if (is_lti) {
      EpmemInterval start_interval=new EpmemInterval();
      start_interval.uedge=uedge;
      start_interval.is_end_point=EPMEM_RANGE_START;
      start_interval.time=promo_time - 1;
      start_interval.sql=null;
      interval_pq.add(start_interval);
      interval_cleanup.add(start_interval);
    }
    uedge.pedges.add(pedge);
    uedge_cache.put(triple.copyEpmemTriple(),uedge);
  }
 else {
    uedge.pedges.clear();
    uedge.pedges=null;
  }
}
 else {
  EpmemUEdge uedge=uedge_iter;
  uedge.pedges.add(pedge);
  if (uedge.activated && uedge.activation_count == 1) {
    for (    EpmemLiteral lit_iter : pedge.literals) {
      EpmemLiteral literal=lit_iter;
      ByRef<Double> curScoreRef=new ByRef<Double>(current_score);
      ByRef<Long> curCardinalityRef=new ByRef<Long>(current_cardinality);
      changed_score|=epmem_satisfy_literal(literal,triple.parent_n_id,triple.child_n_id,curScoreRef,curCardinalityRef,symbol_node_count,uedge_caches,symbol_num_incoming);
      current_score=curScoreRef.value;
      current_cardinality=curCardinalityRef.value;
    }
  }
}
if (pedge.sql != null) {
  if (pedge.sqlResults == null) {
    ResultSet results=pedge.sql.executeQuery();
    pedge.sqlResults=results;
  }
  if (pedge.sqlResults.next()) {
    if (pedge.sqlResults.getMetaData().getColumnCount() > 2) {
      pedge.time=pedge.sqlResults.getLong(2 + 1);
    }
 else {
      pedge.time=0L;
    }
    pedge_pq.add(pedge);
  }
 else {
    pedge.sqlResults.close();
    pedge.sqlResults=null;
    pedge.sql.close();
    pedge.sql=null;
  }
}
}
next_edge=(pedge_pq.isEmpty() ? after : pedge_pq.peek().time);
while (interval_pq.size() != 0 && interval_pq.peek().time > next_edge && current_episode > after) {
if (logger.isDebugEnabled()) {
  logger.debug("String_Node_Str" + current_episode);
}
while (interval_pq.size() != 0 && interval_pq.peek().time >= current_episode) {
  EpmemInterval interval=interval_pq.poll();
  EpmemUEdge uedge=interval.uedge;
  EpmemTriple triple=uedge.triple.copyEpmemTriple();
  if (logger.isDebugEnabled()) {
    logger.debug("String_Node_Str" + (interval.is_end_point != 0 ? "String_Node_Str" : "String_Node_Str") + "String_Node_Str"+ triple.parent_n_id+ "String_Node_Str"+ triple.attribute_s_id+ "String_Node_Str"+ triple.child_n_id);
  }
  if (interval.is_end_point != 0) {
    uedge.activated=true;
    uedge.activation_count++;
    if (uedge.activation_count == 1) {
      for (      EpmemPEdge pedge_iter : uedge.pedges) {
        EpmemPEdge pedge=pedge_iter;
        for (        EpmemLiteral lit_iter : pedge.literals) {
          EpmemLiteral literal=lit_iter;
          ByRef<Double> curScoreRef=new ByRef<Double>(current_score);
          ByRef<Long> curCardinalityRef=new ByRef<Long>(current_cardinality);
          changed_score|=epmem_satisfy_literal(literal,triple.parent_n_id,triple.child_n_id,curScoreRef,curCardinalityRef,symbol_node_count,uedge_caches,symbol_num_incoming);
          current_score=curScoreRef.value;
          current_cardinality=curCardinalityRef.value;
        }
      }
    }
  }
 else {
    uedge.activated=false;
    uedge.activation_count--;
    for (    EpmemPEdge pedge_iter : uedge.pedges) {
      EpmemPEdge pedge=pedge_iter;
      for (      EpmemLiteral lit_iter : pedge.literals) {
        ByRef<Double> curScore=new ByRef<Double>(current_score);
        ByRef<Long> curCardinality=new ByRef<Long>(current_cardinality);
        changed_score|=epmem_unsatisfy_literal(lit_iter,triple.parent_n_id,triple.child_n_id,curScore,curCardinality,symbol_node_count);
        current_score=curScore.value;
        current_cardinality=curCardinality.value;
      }
    }
  }
  if (interval.sql != null) {
    if (interval.sqlResult == null) {
      interval.sqlResult=interval.sql.executeQuery();
    }
    if (interval.uedge.has_noncurrent && interval.sqlResult.next()) {
      interval.time=interval.sqlResult.getInt(0 + 1);
      interval_pq.add(interval);
    }
 else {
      interval.sqlResult.close();
      interval.sqlResult=null;
      interval.sql.close();
      interval.sql=null;
      uedge.intervals--;
      if (uedge.intervals != 0) {
        interval_cleanup.remove(interval);
      }
 else {
      }
    }
  }
}
next_interval=(interval_pq.isEmpty() ? after : interval_pq.peek().time);
next_episode=(next_edge > next_interval ? next_edge : next_interval);
while (prohibits.size() != 0 && prohibits.get(prohibits.size() - 1) > current_episode) {
  prohibits.remove(prohibits.size() - 1);
}
while (prohibits.size() != 0 && current_episode > next_episode && current_episode == prohibits.get(prohibits.size() - 1)) {
  current_episode--;
  prohibits.remove(prohibits.size() - 1);
}
if (logger.isTraceEnabled()) {
  logger.trace(epmem_print_retrieval_state(literal_cache,pedge_caches,uedge_caches));
}
stats.considered.set(stats.considered.get() + 1);
stats.last_considered.set(stats.last_considered.get() + 1);
trace.startNewLine().print(Category.EPMEM,"String_Node_Str" + current_episode + "String_Node_Str"+ current_cardinality+ "String_Node_Str"+ current_score+ "String_Node_Str");
if (current_episode > next_episode && changed_score && (best_episode == EPMEM_MEMID_NONE || current_score > best_score || (do_graph_match && current_score == best_score && !best_graph_matched))) {
  boolean new_king=false;
  if (best_episode == EPMEM_MEMID_NONE || current_score > best_score) {
    best_episode=current_episode;
    best_score=current_score;
    best_cardinality=current_cardinality;
    new_king=true;
  }
  if (current_cardinality == perfect_cardinality) {
    boolean graph_matched=false;
    if (do_graph_match) {
      if (gm_order == DefaultEpisodicMemoryParams.GmOrderingChoices.undefined) {
        Collections.shuffle(gm_ordering,random);
      }
 else       if (gm_order == DefaultEpisodicMemoryParams.GmOrderingChoices.mcv) {
        Collections.sort(gm_ordering,new Comparator<EpmemLiteral>(){
          @Override public int compare(          EpmemLiteral a,          EpmemLiteral b){
            return (a.matches.size() < b.matches.size() ? -1 : 1);
          }
        }
);
      }
      best_bindings.clear();
      @SuppressWarnings("String_Node_Str") Map<Long,SymbolImpl>[] bound_nodes=(Map<Long,SymbolImpl>[])new Map[2];
      for (int i=0; i < bound_nodes.length; i++) {
        bound_nodes[i]=new LinkedHashMap<Long,SymbolImpl>();
      }
      if (logger.isDebugEnabled()) {
        logger.debug("String_Node_Str");
        logger.debug(epmem_print_retrieval_state(literal_cache,pedge_caches,uedge_caches));
      }
      stats.graph_matches.set(stats.graph_matches.get() + 1);
      stats.last_graph_matches.set(stats.last_graph_matches.get() + 1);
      graph_matched=epmem_graph_match(gm_ordering,gm_ordering.listIterator(),best_bindings,bound_nodes,2);
    }
    if (!do_graph_match || graph_matched) {
      best_episode=current_episode;
      best_graph_matched=true;
      current_episode=EPMEM_MEMID_NONE;
      new_king=true;
    }
  }
  if (new_king) {
    trace.startNewLine().print(Category.EPMEM,"String_Node_Str" + Boolean.toString(current_cardinality == perfect_cardinality) + "String_Node_Str"+ Boolean.toString(best_graph_matched)+ "String_Node_Str");
  }
}
if (current_episode == EPMEM_MEMID_NONE) {
  break;
}
 else {
  current_episode=next_episode;
}
}
}
if (best_episode == EPMEM_MEMID_NONE) {
epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_failure,pos_query);
if (neg_query != null) {
epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_failure,neg_query);
}
}
 else {
stats.qry_ret.set(best_episode);
stats.qry_card.set(best_cardinality);
SymbolImpl temp_sym;
Map<Long,SymbolImpl> node_map_map=new LinkedHashMap<Long,SymbolImpl>();
Map<Long,SymbolImpl> node_mem_map=new LinkedHashMap<Long,SymbolImpl>();
temp_sym=symbols.createInteger(leaf_literals.size());
epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_cue_size,temp_sym);
temp_sym=symbols.createInteger(best_cardinality);
epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_match_cardinality,temp_sym);
temp_sym=symbols.createDouble(best_score);
epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_match_score,temp_sym);
temp_sym=symbols.createDouble(best_score / perfect_score);
epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_normalized_match_score,temp_sym);
epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_success,pos_query);
if (neg_query != null) {
epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_success,neg_query);
}
if (do_graph_match) {
temp_sym=symbols.createInteger((best_graph_matched ? 1 : 0));
epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_graph_match,temp_sym);
if (best_graph_matched) {
  @SuppressWarnings("String_Node_Str") int levelLocal=epmem_info(state).epmem_result_header.level;
  SymbolImpl mapping=symbols.make_new_identifier('M',level);
  epmem_buffer_add_wme(meta_wmes,epmem_info(state).epmem_result_header,predefinedSyms.epmem_sym_graph_match_mapping,mapping);
  for (  Entry<EpmemLiteral,EpmemNodePair> iter : best_bindings.entrySet()) {
    if (iter.getKey().value_is_id != 0) {
      temp_sym=symbols.make_new_identifier('N',level);
      epmem_buffer_add_wme(meta_wmes,mapping,predefinedSyms.epmem_sym_graph_match_mapping_node,temp_sym);
      epmem_buffer_add_wme(meta_wmes,temp_sym,predefinedSyms.epmem_sym_graph_match_mapping_cue,iter.getKey().value_sym);
      node_map_map.put(iter.getValue().second,temp_sym);
      node_mem_map.put(iter.getValue().second,null);
    }
  }
}
}
if (level > 2) {
epmem_install_memory(state,best_episode,meta_wmes,retrieval_wmes,node_mem_map);
}
if (best_graph_matched) {
for (Entry<Long,SymbolImpl> iter : node_mem_map.entrySet()) {
  SymbolImpl map_iter=node_map_map.get(iter.getKey());
  if (map_iter != null && iter.getValue() != null) {
    epmem_buffer_add_wme(meta_wmes,map_iter,predefinedSyms.epmem_sym_retrieved,iter.getValue());
  }
}
}
}
}
for (EpmemInterval interval : interval_cleanup) {
if (interval.sqlResult != null) {
interval.sqlResult.close();
interval.sqlResult=null;
interval.sql.close();
interval.sql=null;
}
}
for (int type=EPMEM_RIT_STATE_NODE; type <= EPMEM_RIT_STATE_EDGE; type++) {
for (EpmemPEdge pedge : pedge_caches[type].values()) {
if (pedge.sqlResults != null) {
pedge.sqlResults.close();
pedge.sqlResults=null;
pedge.sql.close();
pedge.sql=null;
}
}
}
}