} else if (origExperimentId != null && origExperimentId.equals(experiment)) {
} else if (origExperimentId != null && origExperimentId.equals(experimentId)) {
  public Experiment createExperiment(String originalExperimentName,                                     String experimentName,                                     String experimentId,                                     String trialId) {    createLogger();    // get assembly ids for trial    ArrayList assemblyIds = getTrialAssemblyIds(experimentId, trialId);    String assemblyMatch = DBUtils.getAssemblyMatch(assemblyIds);    // get nodes for trial    ArrayList nodes = getNodes(trialId, assemblyMatch);    ArrayList hosts = getHosts(trialId, assemblyMatch);    SocietyDBComponent soc = null;    if (assemblyIds.size() != 0) {      // Get the Society Assembly Id (CMT or CSA).      String id = null;      Iterator iter = assemblyIds.iterator();      while(iter.hasNext()) {        id = (String)iter.next();        if(id.startsWith(CMT_START_STRING) || id.startsWith(CSA_START_STRING)) {          break;        }      }            if(id != null) {        String societyName = DBUtils.getSocietyName(id);        SocietyComponent sc = organizer.getSociety(societyName);        if (sc != null && (sc instanceof SocietyDBComponent)) {	  if (log.isDebugEnabled()) {	    log.debug("Found already loaded society " + societyName);	  }          soc = (SocietyDBComponent)sc;        } else {	  if (log.isDebugEnabled()) {	    log.debug("Loading society " + societyName + " from id " + id);	  }          soc = new SocietyDBComponent(societyName, id);          soc.initProperties();        }       }    } else { // We need to create a new trial.      // Need to have the experiment id, trial id, and multiplicity      // for the call that will generate the assembly here.      if(log.isWarnEnabled()) {        log.warn("No assemblies for: " + experimentId + " " + trialId);      }      return null; // creating an experiment from scratch not implemented yet    }    Experiment experiment = new Experiment((String)experimentName,                                            experimentId, trialId);    // The following is ugly      setDefaultNodeArguments(experiment, assemblyMatch,			    PopulateDb.getSocietyAlibId(originalExperimentName));    //    experiment.setCloned(isCloned);    //to hold all potential agents    List agents = new ArrayList();     Map experimentNamesMap = ExperimentDB.getExperimentNames();    String origExperimentId = (String)experimentNamesMap.get(originalExperimentName);    String origTrialId = ExperimentDB.getTrialId(origExperimentId);        // This gets all the recipes for the experiment from which this was created    List defaultRecipes = checkForRecipes(origTrialId, origExperimentId);  //     if (log.isDebugEnabled()) {//       log.debug("default recipes list from OLD exptID has " + defaultRecipes.size() + " items");//       Iterator rI = defaultRecipes.iterator();//       while (rI.hasNext()) {// 	DbRecipe rn = (DbRecipe) rI.next();// 	log.debug("default recipes list: " + rn.name);//       }//     }    List recipes = checkForRecipes(trialId, experimentId);//     if (log.isDebugEnabled()) {//       log.debug("recipes list from this exptID has " + recipes.size() + " items");//       Iterator rI = recipes.iterator();//       while (rI.hasNext()) {// 	DbRecipe rn = (DbRecipe) rI.next();// 	log.debug("recipes list: " + rn.name);//       }//     }    // FIXME: Maybe deal with CMT societies & DEFAULT comms    // here??    // And in general, must grab comm stuff from orig ID and resave it    // under a new ID and attach it to the new Expt.    // FIXME: Do something with community stuff here?    // That is, find any comm info, stash it away in new ASB    // ref'ed in Expt, so that saveToDb can then save it    // in the correct assembly in the Expt.    // maybe change this if to be:    //    if (! recipes.contains(defaultRecipes)    // except of course, looping over the items....    // When creating an experiment from one of the base experiments    // copy over the recipes that came with the base    if (origExperimentId != null && ! origExperimentId.equals(experimentId)) {      if (log.isDebugEnabled()) {	log.debug("Combining old expts recipes with those from this");      }      recipes.addAll(defaultRecipes);      // Now community stuff      PopulateDb pdbc = null;      String commAsb = null;      try {	pdbc = new PopulateDb(experimentId, trialId);	commAsb = pdbc.getNewCommAsbFromExpt(origExperimentId, origTrialId);	if (commAsb != null)	  pdbc.setCommAssemblyId(commAsb);      } catch (SQLException sqle) {	log.error("createExperiment couldnt get new commAsb based on orig expt " + origExperimentId + " for new experiment " + experimentId, sqle);      } catch (IOException ioe) {	log.error("createExperiment couldnt get new commAsb based on orig expt " + origExperimentId + " for new experiment " + experimentId, ioe);      } finally {	try {	  if (pdbc != null)	    pdbc.close();	} catch (SQLException se) {}      }      experiment.setCommAsbID(commAsb);      if (log.isDebugEnabled()) {	log.debug("createExpt got new comm asb based on old expt " + origExperimentId + " of " + commAsb);      }    } else if (origExperimentId != null && origExperimentId.equals(experiment)) {      if (log.isDebugEnabled()) {	log.debug("createExpt had no orig or orig same as this. Try reading previous comm ASB from db");      }      // just loading a previous experiment from the DB.      // get its comm ASB      PopulateDb pdbc = null;      String commAsb = null;      try {	pdbc = new PopulateDb(experimentId, trialId);	commAsb = pdbc.getCommAsbForExpt(experimentId, trialId);      } catch (SQLException sqle) {	log.error("createExperiment couldnt get commAsb for expt " + experimentId, sqle);      } catch (IOException ioe) {	log.error("createExperiment couldnt get commAsb for expt " + experimentId, ioe);      } finally {	try {	  if (pdbc != null)	    pdbc.close();	} catch (SQLException se) {}      }      experiment.setCommAsbID(commAsb);      if (log.isDebugEnabled()) {	log.debug("createExpt got comm asb for expt " + experimentId + " of " + commAsb);      }    } else if (origExperimentId == null) {      if (log.isDebugEnabled()) {	log.debug("createExpt got null orig ExptId when loading expt " + experimentId);      }      // just re-loading an experiment      // get its comm ASB      PopulateDb pdbc = null;      String commAsb = null;      try {	pdbc = new PopulateDb(experimentId, trialId);	commAsb = pdbc.getCommAsbForExpt(experimentId, trialId);      } catch (SQLException sqle) {	log.error("createExperiment couldnt get commAsb for expt " + experimentId, sqle);      } catch (IOException ioe) {	log.error("createExperiment couldnt get commAsb for expt " + experimentId, ioe);      } finally {	try {	  if (pdbc != null)	    pdbc.close();	} catch (SQLException se) {}      }      experiment.setCommAsbID(commAsb);      if (log.isDebugEnabled()) {	log.debug("createExpt got comm asb for expt " + experimentId + " of " + commAsb);      }    }    // If somehow still have no comm ASB, create a new one    if (experiment.getCommAsbID() == null) {      if (log.isDebugEnabled()) {	log.debug("createExpt somehow still no Comm ASB. Create a new one");      }      PopulateDb pdbc = null;      String commAsb = null;      try {	pdbc = new PopulateDb(experimentId, trialId);	commAsb = pdbc.getNewCommAssembly();	if (commAsb != null)	  pdbc.setCommAssemblyId(commAsb);      } catch (SQLException sqle) {	log.error("createExperiment couldnt get new commAsb for expt " + experimentId, sqle);      } catch (IOException ioe) {	log.error("createExperiment couldnt get new commAsb for expt " + experimentId, ioe);      } finally {	try {	  if (pdbc != null)	    pdbc.close();	} catch (SQLException se) {}      }      experiment.setCommAsbID(commAsb);      if (log.isDebugEnabled()) {	log.debug("createExpt got comm asb for expt " + experimentId + " of " + commAsb);      }    }    if (recipes.size() != 0) {      Iterator metIter = recipes.iterator();      while (metIter.hasNext()) {        DbRecipe dbRecipe = (DbRecipe) metIter.next();        RecipeComponent mc = organizer.getRecipe(dbRecipe.name);        if (mc == null) {          mc = createRecipe(dbRecipe.name, dbRecipe.cls);          setRecipeComponentProperties(dbRecipe, mc);          mc.saveToDatabase();        }        AgentComponent[] recagents = mc.getAgents();         if (recagents != null && recagents.length > 0) {          agents.addAll(Arrays.asList(recagents));        }	if (log.isDebugEnabled()) {	  log.debug("Adding recipe to experiment " + experiment.getExperimentName() + ": " + mc.getRecipeName());	}        experiment.addRecipeComponent(mc);      }    }    AgentComponent[] socagents = soc.getAgents();    if (socagents!= null && socagents.length > 0) {      for (int i = 0; i < socagents.length; i++) {        AgentComponent ac = socagents[i];        setComponentProperties(ac, PopulateDb.getAgentAlibId(ac.getShortName()), assemblyMatch);      }      agents.addAll(Arrays.asList(socagents));    }    AgentComponent[] allagents = (AgentComponent[])agents.toArray(new AgentComponent[agents.size()]);     experiment.addSocietyComponent((SocietyComponent)soc);    if (log.isDebugEnabled()) {      log.debug("createExperiment about to do Agent/Node mapping");    }    // Retrieve the Agent/Node mapping. Add the Nodes to the Experiment    // And set the properties for the Nodes.    // Add all Nodes.    addAgents(experiment, nodes, allagents, trialId, assemblyMatch);    // Add all Hosts.    Iterator hostIter = hosts.iterator();    while (hostIter.hasNext()) {      String hostName = (String) hostIter.next();      HostComponent hc = experiment.addHost(hostName);      setComponentProperties(hc, PopulateDb.getHostAlibId(hostName), assemblyMatch);    }    mapNodesToHosts(experiment, assemblyMatch);    try {          if (!ExperimentDB.isExperimentNameInDatabase(experimentName)) {	if (log.isDebugEnabled()) {	  log.debug("Saving expt " + experimentName + " not in DB to DB");	}	experiment.saveToDb(saveToDbConflictHandler);      }      else {	// Add the recipes to the expt_trial_mod_recipe table	List rcs = new ArrayList();	for (int i = 0; i < experiment.getRecipeComponentCount(); i++) {	  rcs.add(experiment.getRecipeComponent(i));	}	if (log.isDebugEnabled()) {	  log.debug("Setting recipes on experiment " + experiment.getExperimentID() + " in DB");	}	PopulateDb pdb = null;	try {	  pdb = new PopulateDb(experiment.getExperimentID(), experiment.getTrialID());	  pdb.setAndCleanModRecipes(rcs);	} catch (Exception e) {	  if (log.isErrorEnabled()) {	    log.error("Problem saving recipes on experiment " + experiment.getExperimentID(), e);	  }	} finally {	  try {	    pdb.close();	  } catch (Exception e) {}	}      }    } catch (RuntimeException e) {      if(log.isErrorEnabled()) {        log.error("RuntimeException saving experiment " + experimentName, e);      }    }    experiment.resetModified(); // the experiment is NOT modified at this point    return experiment;  }