Program: 6624
Project: 6624
Buggy source:
public List<MappingInstance> getMappingInstances(List<Compartment> compartments,List<Channel> channels){
  if (compartments == null || channels == null) {
    throw new NullPointerException();
  }
  List<Agent> remainingTemplateAgents=new ArrayList<Agent>(agents);
  List<Agent> fixedTemplateAgents=getFixedAgents(compartments);
  remainingTemplateAgents.removeAll(fixedTemplateAgents);
  List<AgentLink> remainingTemplateLinks=new ArrayList<AgentLink>(agentLinks);
  List<MappingInstance> mappings=new ArrayList<MappingInstance>();
  if (fixedTemplateAgents.size() > 0) {
    List<AgentLink> processedTemplateLinks=getInternalLinks(remainingTemplateLinks,fixedTemplateAgents);
    remainingTemplateLinks.removeAll(processedTemplateLinks);
    if (!isInternalLinksValid(processedTemplateLinks,compartments,channels)) {
      return new ArrayList<MappingInstance>();
    }
    MappingInstance mappingInstance=new MappingInstance();
    for (    Agent agent : fixedTemplateAgents) {
      mappingInstance.mapping.put(agent,agent.clone());
    }
    mappings.add(mappingInstance);
  }
  if (fixedTemplateAgents.size() == 0) {
    Agent templateTargetAgent=remainingTemplateAgents.get(0);
    fixedTemplateAgents.add(templateTargetAgent);
    remainingTemplateAgents.remove(templateTargetAgent);
    List<AgentLink> processedTemplateLinks=getInternalLinks(remainingTemplateLinks,fixedTemplateAgents);
    remainingTemplateLinks.removeAll(processedTemplateLinks);
    if (!isInternalLinksValid(processedTemplateLinks,compartments,channels)) {
      return new ArrayList<MappingInstance>();
    }
    List<Agent> locatedTargetAgents=templateTargetAgent.getLocatedAgents(compartments);
    for (    Agent locatedTargetAgent : locatedTargetAgents) {
      MappingInstance mapping=new MappingInstance();
      mapping.mapping.put(templateTargetAgent,locatedTargetAgent);
      mappings.add(mapping);
    }
  }
  while (remainingTemplateAgents.size() > 0) {
    Agent templateTargetAgent=chooseNextAgent(fixedTemplateAgents,remainingTemplateAgents,remainingTemplateLinks);
    fixedTemplateAgents.add(templateTargetAgent);
    remainingTemplateAgents.remove(templateTargetAgent);
    List<AgentLink> addedTemplateLinks=getInternalLinks(remainingTemplateLinks,fixedTemplateAgents);
    remainingTemplateLinks.removeAll(addedTemplateLinks);
    List<MappingInstance> newMappings=new ArrayList<MappingInstance>();
    for (    MappingInstance oldMapping : mappings) {
      List<Location> targetLocations=null;
      for (      AgentLink link : addedTemplateLinks) {
        Agent templateSourceAgent=link.getLinkedAgent(templateTargetAgent);
        if (templateSourceAgent != null) {
          Agent locatedSourceAgent=oldMapping.mapping.get(templateSourceAgent);
          String channelName=(link.sourceSite.getChannel() != null) ? link.sourceSite.getChannel() : link.targetSite.getChannel();
          Channel channel=null;
          if (channelName != null) {
            channel=getChannel(channels,channelName);
          }
          List<Location> currentTargetLocations=getPossibleLocations(locatedSourceAgent.location,templateTargetAgent.location,channel,compartments);
          if (targetLocations == null) {
            targetLocations=currentTargetLocations;
          }
 else {
            targetLocations.retainAll(currentTargetLocations);
          }
        }
      }
      if (targetLocations == null || targetLocations.size() == 0) {
        continue;
      }
      for (      Location targetLocation : targetLocations) {
        MappingInstance newMapping=new MappingInstance();
        newMapping.mapping.putAll(oldMapping.mapping);
        Agent locatedTargetAgent=new Agent(templateTargetAgent.name,targetLocation,templateTargetAgent.getSites());
        newMapping.mapping.put(templateTargetAgent,locatedTargetAgent);
        newMappings.add(newMapping);
      }
    }
    mappings=newMappings;
  }
  reorderLocatedMappings(mappings,agents);
  return mappings;
}

Fixed version (ground truth):
public List<MappingInstance> getMappingInstances(List<Compartment> compartments,List<Channel> channels){
  if (compartments == null || channels == null) {
    throw new NullPointerException();
  }
  List<Agent> remainingTemplateAgents=new ArrayList<Agent>(agents);
  List<Agent> fixedTemplateAgents=getFixedAgents(compartments);
  remainingTemplateAgents.removeAll(fixedTemplateAgents);
  List<AgentLink> remainingTemplateLinks=new ArrayList<AgentLink>(agentLinks);
  List<MappingInstance> mappings=new ArrayList<MappingInstance>();
  if (fixedTemplateAgents.size() > 0) {
    List<AgentLink> processedTemplateLinks=getInternalLinks(remainingTemplateLinks,fixedTemplateAgents);
    remainingTemplateLinks.removeAll(processedTemplateLinks);
    if (!isInternalLinksValid(processedTemplateLinks,compartments,channels)) {
      return new ArrayList<MappingInstance>();
    }
    MappingInstance mappingInstance=new MappingInstance();
    for (    Agent agent : fixedTemplateAgents) {
      mappingInstance.mapping.put(agent,agent.clone());
    }
    mappings.add(mappingInstance);
  }
  if (fixedTemplateAgents.size() == 0) {
    Agent templateTargetAgent=remainingTemplateAgents.get(0);
    fixedTemplateAgents.add(templateTargetAgent);
    remainingTemplateAgents.remove(templateTargetAgent);
    List<AgentLink> processedTemplateLinks=getInternalLinks(remainingTemplateLinks,fixedTemplateAgents);
    remainingTemplateLinks.removeAll(processedTemplateLinks);
    if (!isInternalLinksValid(processedTemplateLinks,compartments,channels)) {
      return new ArrayList<MappingInstance>();
    }
    List<Agent> locatedTargetAgents=templateTargetAgent.getLocatedAgents(compartments);
    for (    Agent locatedTargetAgent : locatedTargetAgents) {
      MappingInstance mapping=new MappingInstance();
      mapping.mapping.put(templateTargetAgent,locatedTargetAgent);
      mappings.add(mapping);
    }
  }
  while (remainingTemplateAgents.size() > 0) {
    Agent templateTargetAgent=chooseNextAgent(fixedTemplateAgents,remainingTemplateAgents,remainingTemplateLinks);
    fixedTemplateAgents.add(templateTargetAgent);
    remainingTemplateAgents.remove(templateTargetAgent);
    List<AgentLink> addedTemplateLinks=getInternalLinks(remainingTemplateLinks,fixedTemplateAgents);
    remainingTemplateLinks.removeAll(addedTemplateLinks);
    List<MappingInstance> newMappings=new ArrayList<MappingInstance>();
    for (    MappingInstance oldMapping : mappings) {
      List<Location> targetLocations=null;
      for (      AgentLink link : addedTemplateLinks) {
        Agent templateSourceAgent=link.getLinkedAgent(templateTargetAgent);
        if (templateSourceAgent != null) {
          Agent locatedSourceAgent=oldMapping.mapping.get(templateSourceAgent);
          String channelName=(link.sourceSite.getChannel() != null) ? link.sourceSite.getChannel() : link.targetSite.getChannel();
          Channel channel=null;
          if (channelName != null) {
            channel=getChannel(channels,channelName);
          }
          List<Location> currentTargetLocations=getPossibleLocations(locatedSourceAgent.location,templateTargetAgent.location,channel,compartments);
          if (targetLocations == null) {
            targetLocations=currentTargetLocations;
          }
 else {
            targetLocations.retainAll(currentTargetLocations);
          }
        }
      }
      if (targetLocations == null || targetLocations.size() == 0) {
        continue;
      }
      for (      Location targetLocation : targetLocations) {
        MappingInstance newMapping=new MappingInstance();
        for (        Map.Entry<Agent,Agent> entry : oldMapping.mapping.entrySet()) {
          newMapping.mapping.put(entry.getKey(),entry.getValue().clone());
        }
        Agent locatedTargetAgent=new Agent(templateTargetAgent.name,targetLocation,templateTargetAgent.getSites());
        newMapping.mapping.put(templateTargetAgent,locatedTargetAgent);
        newMappings.add(newMapping);
      }
    }
    mappings=newMappings;
  }
  reorderLocatedMappings(mappings,agents);
  return mappings;
}