soc = (SocietyDBComponent)organizer.getSociety(societyName); if (soc == null) {
SocietyComponent sc = organizer.getSociety(societyName); if (sc != null && (sc instanceof SocietyDBComponent)) soc = (SocietyDBComponent)sc; else {
  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);        soc = (SocietyDBComponent)organizer.getSociety(societyName);        if (soc == null) {          soc = new SocietyDBComponent(societyName, id);          soc.initProperties();        }       } else {        soc = null;      }    } 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,                            ComponentData.SOCIETY                            + "|"                            + 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);        List defaultRecipes = checkForRecipes(origTrialId, origExperimentId);      List recipes = checkForRecipes(trialId, experimentId);    // When creating an experiment from one of the base experiments    // copy over the recipes that came with the base    if (! origExperimentId.startsWith("EXPT-"))      recipes.addAll(defaultRecipes);    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);          ((RecipeBase)mc).resetModified();          ((RecipeBase)mc).installListeners();        }        AgentComponent[] recagents = mc.getAgents();         if (recagents != null && recagents.length > 0) {          agents.addAll(Arrays.asList(recagents));        }              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, ac.getShortName(), assemblyMatch);      }      agents.addAll(Arrays.asList(socagents));    }    AgentComponent[] allagents = (AgentComponent[])agents.toArray(new AgentComponent[agents.size()]);     experiment.addSocietyComponent((SocietyComponent)soc);    // 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, hostName, assemblyMatch);    }    mapNodesToHosts(experiment, assemblyMatch);    try {      if (!ExperimentDB.isExperimentNameInDatabase(experimentName))	experiment.saveToDb(saveToDbConflictHandler);      else {	// Add the recipes to the expt_trial_mod_recipe table	PopulateDb pdb = null;	try {	  pdb = new PopulateDb(experiment.getExperimentID(), experiment.getTrialID());	  List rcs = new ArrayList();	  for (int i = 0; i < experiment.getRecipeComponentCount(); i++) {	    rcs.add(experiment.getRecipeComponent(i));	  }	  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;  }