Program: 80485
Project: 80485
Buggy source:
private UpdateContext updateLocalGraph(List<Object> domainObjects){
  UpdateContext context=new UpdateContext();
  new ClosureCalculator().calculateClosure(domainObjects,context);
  Graph graph=null;
  Object domainObject;
  Map<Integer,QueryNode2ResultNode> nodeIndexMap=null;
  List<IClause> clauses=null;
  List<IClause> removeStartClauses=null;
  List<IClause> removeClauses=null;
  for (int i=0; i < context.domainObjects.size(); i++) {
    domainObject=context.domainObjects.get(i);
    Long id=this.domainState.getIdFrom_Object2IdMap(domainObject);
    if (id != null) {
      JcNode n=new JcNode(NodePrefix.concat(String.valueOf(i)));
      QueryNode2ResultNode n2n=new QueryNode2ResultNode();
      n2n.queryNode=n;
      if (nodeIndexMap == null)       nodeIndexMap=new HashMap<Integer,QueryNode2ResultNode>();
      nodeIndexMap.put(new Integer(i),n2n);
      if (clauses == null)       clauses=new ArrayList<IClause>();
      clauses.add(START.node(n).byId(id.longValue()));
    }
  }
  Map<Integer,QueryRelation2ResultRelation> relationIndexMap=null;
  for (int i=0; i < context.relations.size(); i++) {
    IRelation relat=context.relations.get(i);
    Long id=this.domainState.getFrom_Relation2IdMap(relat);
    if (id != null) {
      JcRelation r=new JcRelation(RelationPrefix.concat(String.valueOf(i)));
      QueryRelation2ResultRelation r2r=new QueryRelation2ResultRelation();
      r2r.queryRelation=r;
      if (relationIndexMap == null)       relationIndexMap=new HashMap<Integer,QueryRelation2ResultRelation>();
      relationIndexMap.put(new Integer(i),r2r);
      clauses.add(START.relation(r).byId(id.longValue()));
    }
  }
  if (context.relationsToRemove.size() > 0) {
    removeStartClauses=new ArrayList<IClause>();
    removeClauses=new ArrayList<IClause>();
    for (int i=0; i < context.relationsToRemove.size(); i++) {
      IRelation relat=context.relationsToRemove.get(i);
      Long id=this.domainState.getFrom_Relation2IdMap(relat);
      JcRelation r=new JcRelation(RelationPrefix.concat(String.valueOf(i)));
      removeStartClauses.add(START.relation(r).byId(id.longValue()));
      removeClauses.add(DO.DELETE(r));
    }
  }
  if (context.domainObjectsToRemove.size() > 0) {
    if (removeStartClauses == null) {
      removeStartClauses=new ArrayList<IClause>();
      removeClauses=new ArrayList<IClause>();
    }
    for (int i=0; i < context.domainObjectsToRemove.size(); i++) {
      Object dobj=context.domainObjectsToRemove.get(i);
      Long id=this.domainState.getIdFrom_Object2IdMap(dobj);
      JcNode n=new JcNode(NodePrefix.concat(String.valueOf(i)));
      removeStartClauses.add(START.node(n).byId(id.longValue()));
      removeClauses.add(DO.DELETE(n));
    }
  }
  if (clauses != null || removeStartClauses != null) {
    JcQuery query;
    List<JcQuery> queries=new ArrayList<JcQuery>();
    if (clauses != null) {
      clauses.add(RETURN.ALL());
      IClause[] clausesArray=clauses.toArray(new IClause[clauses.size()]);
      query=new JcQuery();
      query.setClauses(clausesArray);
      queries.add(query);
    }
    if (removeStartClauses != null) {
      removeStartClauses.addAll(removeClauses);
      query=new JcQuery();
      query.setClauses(removeStartClauses.toArray(new IClause[removeStartClauses.size()]));
      queries.add(query);
    }
    List<JcQueryResult> results=this.dbAccess.execute(queries);
    List<JcError> errors=Util.collectErrors(results);
    if (errors.size() > 0) {
      throw new JcResultException(errors);
    }
    if (clauses != null) {
      JcQueryResult result=results.get(0);
      graph=result.getGraph();
      if (nodeIndexMap != null) {
        Iterator<Entry<Integer,QueryNode2ResultNode>> nit=nodeIndexMap.entrySet().iterator();
        while (nit.hasNext()) {
          Entry<Integer,QueryNode2ResultNode> entry=nit.next();
          entry.getValue().resultNode=result.resultOf(entry.getValue().queryNode).get(0);
        }
      }
      if (relationIndexMap != null) {
        Iterator<Entry<Integer,QueryRelation2ResultRelation>> rit=relationIndexMap.entrySet().iterator();
        while (rit.hasNext()) {
          Entry<Integer,QueryRelation2ResultRelation> entry=rit.next();
          entry.getValue().resultRelation=result.resultOf(entry.getValue().queryRelation).get(0);
        }
      }
    }
  }
  if (graph == null)   graph=Graph.create(this.dbAccess);
  context.domObj2Node=new HashMap<Object,GrNode>(context.domainObjects.size());
  context.domRelation2Relations=new ArrayList<DomRelation2ResultRelation>();
  for (int i=0; i < context.domainObjects.size(); i++) {
    GrNode rNode=null;
    if (nodeIndexMap != null && nodeIndexMap.get(i) != null) {
      rNode=nodeIndexMap.get(i).resultNode;
    }
    if (rNode == null)     rNode=graph.createNode();
    context.domObj2Node.put(context.domainObjects.get(i),rNode);
    updateGraphFromObject(context.domainObjects.get(i),rNode);
  }
  for (int i=0; i < context.relations.size(); i++) {
    GrRelation rRelation=null;
    if (relationIndexMap != null && relationIndexMap.get(i) != null) {
      rRelation=relationIndexMap.get(i).resultRelation;
    }
    if (rRelation == null) {
      IRelation relat=context.relations.get(i);
      rRelation=graph.createRelation(relat.getType(),context.domObj2Node.get(relat.getStart()),context.domObj2Node.get(relat.getEnd()));
      DomRelation2ResultRelation d2r=new DomRelation2ResultRelation();
      d2r.domRelation=relat;
      d2r.resultRelation=rRelation;
      context.domRelation2Relations.add(d2r);
    }
    updateGraphFromRelation(context.relations.get(i),rRelation);
  }
  context.graph=graph;
  return context;
}

Fixed version (ground truth):
private UpdateContext updateLocalGraph(List<Object> domainObjects){
  UpdateContext context=new UpdateContext();
  new ClosureCalculator().calculateClosure(domainObjects,context);
  Graph graph=null;
  Object domainObject;
  Map<Integer,QueryNode2ResultNode> nodeIndexMap=null;
  List<IClause> clauses=null;
  List<IClause> removeStartClauses=null;
  List<IClause> removeClauses=null;
  for (int i=0; i < context.domainObjects.size(); i++) {
    domainObject=context.domainObjects.get(i);
    Long id=this.domainState.getIdFrom_Object2IdMap(domainObject);
    if (id != null) {
      JcNode n=new JcNode(NodePrefix.concat(String.valueOf(i)));
      QueryNode2ResultNode n2n=new QueryNode2ResultNode();
      n2n.queryNode=n;
      if (nodeIndexMap == null)       nodeIndexMap=new HashMap<Integer,QueryNode2ResultNode>();
      nodeIndexMap.put(new Integer(i),n2n);
      if (clauses == null)       clauses=new ArrayList<IClause>();
      clauses.add(START.node(n).byId(id.longValue()));
    }
  }
  Map<Integer,QueryRelation2ResultRelation> relationIndexMap=null;
  for (int i=0; i < context.relations.size(); i++) {
    IRelation relat=context.relations.get(i);
    Long id=this.domainState.getFrom_Relation2IdMap(relat);
    if (id != null) {
      JcRelation r=new JcRelation(RelationPrefix.concat(String.valueOf(i)));
      QueryRelation2ResultRelation r2r=new QueryRelation2ResultRelation();
      r2r.queryRelation=r;
      if (relationIndexMap == null)       relationIndexMap=new HashMap<Integer,QueryRelation2ResultRelation>();
      relationIndexMap.put(new Integer(i),r2r);
      clauses.add(START.relation(r).byId(id.longValue()));
    }
  }
  if (context.relationsToRemove.size() > 0) {
    removeStartClauses=new ArrayList<IClause>();
    removeClauses=new ArrayList<IClause>();
    for (int i=0; i < context.relationsToRemove.size(); i++) {
      IRelation relat=context.relationsToRemove.get(i);
      Long id=this.domainState.getFrom_Relation2IdMap(relat);
      JcRelation r=new JcRelation(RelationPrefix.concat(String.valueOf(i)));
      removeStartClauses.add(START.relation(r).byId(id.longValue()));
      removeClauses.add(DO.DELETE(r));
    }
  }
  if (context.domainObjectsToRemove.size() > 0) {
    if (removeStartClauses == null) {
      removeStartClauses=new ArrayList<IClause>();
      removeClauses=new ArrayList<IClause>();
    }
    for (int i=0; i < context.domainObjectsToRemove.size(); i++) {
      Object dobj=context.domainObjectsToRemove.get(i);
      Long id=this.domainState.getIdFrom_Object2IdMap(dobj);
      JcNode n=new JcNode(NodePrefix.concat(String.valueOf(i)));
      removeStartClauses.add(START.node(n).byId(id.longValue()));
      removeClauses.add(DO.DELETE(n));
    }
  }
  if (clauses != null || removeStartClauses != null) {
    JcQuery query;
    List<JcQuery> queries=new ArrayList<JcQuery>();
    if (clauses != null) {
      clauses.add(RETURN.ALL());
      IClause[] clausesArray=clauses.toArray(new IClause[clauses.size()]);
      query=new JcQuery();
      query.setClauses(clausesArray);
      queries.add(query);
    }
    if (removeStartClauses != null) {
      removeStartClauses.addAll(removeClauses);
      query=new JcQuery();
      query.setClauses(removeStartClauses.toArray(new IClause[removeStartClauses.size()]));
      queries.add(query);
    }
    List<JcQueryResult> results=this.dbAccess.execute(queries);
    List<JcError> errors=Util.collectErrors(results);
    if (errors.size() > 0) {
      throw new JcResultException(errors);
    }
    if (clauses != null) {
      JcQueryResult result=results.get(0);
      graph=result.getGraph();
      GrAccess.setDBAccess(this.dbAccess,graph);
      if (nodeIndexMap != null) {
        Iterator<Entry<Integer,QueryNode2ResultNode>> nit=nodeIndexMap.entrySet().iterator();
        while (nit.hasNext()) {
          Entry<Integer,QueryNode2ResultNode> entry=nit.next();
          entry.getValue().resultNode=result.resultOf(entry.getValue().queryNode).get(0);
        }
      }
      if (relationIndexMap != null) {
        Iterator<Entry<Integer,QueryRelation2ResultRelation>> rit=relationIndexMap.entrySet().iterator();
        while (rit.hasNext()) {
          Entry<Integer,QueryRelation2ResultRelation> entry=rit.next();
          entry.getValue().resultRelation=result.resultOf(entry.getValue().queryRelation).get(0);
        }
      }
    }
  }
  if (graph == null)   graph=Graph.create(this.dbAccess);
  context.domObj2Node=new HashMap<Object,GrNode>(context.domainObjects.size());
  context.domRelation2Relations=new ArrayList<DomRelation2ResultRelation>();
  for (int i=0; i < context.domainObjects.size(); i++) {
    GrNode rNode=null;
    if (nodeIndexMap != null && nodeIndexMap.get(i) != null) {
      rNode=nodeIndexMap.get(i).resultNode;
    }
    if (rNode == null)     rNode=graph.createNode();
    context.domObj2Node.put(context.domainObjects.get(i),rNode);
    updateGraphFromObject(context.domainObjects.get(i),rNode);
  }
  for (int i=0; i < context.relations.size(); i++) {
    GrRelation rRelation=null;
    if (relationIndexMap != null && relationIndexMap.get(i) != null) {
      rRelation=relationIndexMap.get(i).resultRelation;
    }
    if (rRelation == null) {
      IRelation relat=context.relations.get(i);
      rRelation=graph.createRelation(relat.getType(),context.domObj2Node.get(relat.getStart()),context.domObj2Node.get(relat.getEnd()));
      DomRelation2ResultRelation d2r=new DomRelation2ResultRelation();
      d2r.domRelation=relat;
      d2r.resultRelation=rRelation;
      context.domRelation2Relations.add(d2r);
    }
    updateGraphFromRelation(context.relations.get(i),rRelation);
  }
  context.graph=graph;
  return context;
}