ModifiableComponent sc = getComponent(i); if (sc instanceof SocietyComponent) {  ModifiableComponent copiedSC = sc.copy("Society for " + uniqueName); experimentCopy.addComponent(copiedSC); } else System.out.println("Experiment: error in copying: unexpected component of class: " + sc.getClass().getName());
ModifiableComponent mc = getComponent(i); ModifiableComponent copiedComponent = null; if (mc instanceof SocietyComponent) { copiedComponent = mc.copy("Society for " + uniqueName); } else if (mc instanceof RecipeComponent) { copiedComponent = mc.copy("Recipe for " + uniqueName); } if (copiedComponent != null) experimentCopy.addComponent(copiedComponent);
  public ModifiableComponent copy(String uniqueName) {    Experiment experimentCopy = null;    if (inDatabase)       experimentCopy = new Experiment(uniqueName, expID, trialID);    else      experimentCopy = new Experiment(uniqueName);    Properties newProps = experimentCopy.getDefaultNodeArguments();    newProps.clear();    newProps.putAll(getDefaultNodeArguments());    // this copies the society and the agents    // TODO: do agents have to be copied explicitly?    for (int i = 0; i < getComponentCount(); i++) {      ModifiableComponent sc = getComponent(i);      if (sc instanceof SocietyComponent) {        // this gets around asking the organizer for a new unique name        ModifiableComponent copiedSC = sc.copy("Society for " + uniqueName);        experimentCopy.addComponent(copiedSC);      } else        System.out.println("Experiment: error in copying: unexpected component of class: " + sc.getClass().getName());    }    // copy hosts    HostComponent[] hosts = getHosts();    HostComponent[] nhosts = new HostComponent[hosts.length];    for (int i = 0; i < hosts.length; i++) {      nhosts[i] = experimentCopy.addHost(hosts[i].getShortName().toString());      nhosts[i].setServerPort(hosts[i].getServerPort());      nhosts[i].setMonitoringPort(hosts[i].getMonitoringPort());    }    // copy nodes    NodeComponent[] nodes = getNodes();    NodeComponent[] nnodes = new NodeComponent[nodes.length];    for (int i = 0; i < nodes.length; i++) {      nnodes[i] = ((ExperimentNode)nodes[i]).copy(experimentCopy);      experimentCopy.addNodeComponent((ExperimentNode)nnodes[i]);    }    // reconcile hosts-nodes-agents    AgentComponent[] nagents = experimentCopy.getAgents();    NodeComponent nnode = null;    for (int i = 0; i < hosts.length; i++) {      NodeComponent[] onodes = hosts[i].getNodes();      // for each of the old nodes, find the corresponding new node      for (int j = 0; j < onodes.length; j++) {        for (int k = 0; k < nnodes.length; k++) {          if (nnodes[k].getShortName().equals(onodes[j].getShortName())) {              nnode = nnodes[k];              // add new node to new host              nhosts[i].addNode(nnode);              break;          }        }        // add new agents to new node        AgentComponent[] oagents = onodes[j].getAgents();        for (int x = 0; x < oagents.length; x++)           for (int y = 0; y < nagents.length; y++)             if (nagents[y].getShortName().equals(oagents[x].getShortName()))              nnode.addAgent(nagents[y]);      }    }    // copy the results directory; results from the copied experiment    // will be stored in this directory under the copied experiment name    experimentCopy.setResultDirectory(getResultDirectory());        return experimentCopy;  }