NewWorkflow wf = dp.expandedWorkflow;
NewWorkflow wf = null; Expansion exp = (Expansion) drTask.getPlanElement(); if (exp != null) { wf = (NewWorkflow) exp.getWorkflow(); }
  protected void expandDetermineRequirements(      DeployPlan dp, Task drTask,       Enumeration assetsEnum) {    if (logger.isDebugEnabled()) {      printDebug("Expand task: "+drTask+" with DP: "+ dp);    }    // this is protection for problem where database may indicate zero items    // of a certain type.    assetsEnum = removeZeroQuantityAggregates (assetsEnum).elements();    // get first and second assets from enumeration    Asset firstAssetForTransport = null;    Asset secondAssetForTransport = null;    boolean isPersonAsset = false;    while (true) {      if (!(assetsEnum.hasMoreElements())) {        if (firstAssetForTransport != null) {          // okay, need to transport a single asset          break;        } else {          // bad, nothing to transport?          printError("No assets to Transport?!!!");          return;        }      }      Asset a = (Asset)assetsEnum.nextElement();      if (a == null) {        printError("Null asset for Transport?!!!");        continue;      }      if (firstAssetForTransport == null) {        // the first of the assets        firstAssetForTransport = a;	// Is this a Person or Equipment? Use first element to determine batch	Object o = a;	while (o instanceof AggregateAsset) {          o = ((AggregateAsset)o).getAsset();	}        isPersonAsset = o instanceof Person;      } else {        // okay, need to transport multiple assets        secondAssetForTransport = a;        break;      }    }    // have one or more assets to transport    // check if we're grouping assets    if (createAssetGroups &&        (secondAssetForTransport != null)) {      // make vector of assets      Vector v = new Vector();      v.addElement(firstAssetForTransport);      v.addElement(secondAssetForTransport);      while (assetsEnum.hasMoreElements()) {        Asset a = (Asset)assetsEnum.nextElement();        if (a == null) {          printError("Null asset for Transport?!!!");        } else {          v.addElement(a);        }      }      // create group      AssetGroup ag = (AssetGroup) theLDMF.createAsset(AssetGroup.class);      try {        NewTypeIdentificationPG typeIdPG =           (NewTypeIdentificationPG)theLDMF.createPropertyGroup(             TypeIdentificationPGImpl.class);        typeIdPG.setTypeIdentification("trans_group");        typeIdPG.setNomenclature("strat-proj-plugin_asset_group");        ag.setTypeIdentificationPG(typeIdPG);      } catch (Exception eFailedTypePG) {        // don't care?      }      ag.setAssets(v);      // pretend that we have only the asset group      firstAssetForTransport = ag;      secondAssetForTransport = null;    }    // do expansion    // PREPOSITIONAL PHRASES    Vector prepphrases = new Vector();    //   TO  (my activity deployment location)    NewPrepositionalPhrase to = theLDMF.newPrepositionalPhrase();    to.setPreposition(Constants.Preposition.TO);    to.setIndirectObject(dp.toLoc);    prepphrases.addElement(to);            //   FROM  (my home location)    NewPrepositionalPhrase from = theLDMF.newPrepositionalPhrase();    from.setPreposition(Constants.Preposition.FROM);    // HACK - assume that all the assets in the Enumeration are of the same type    if (isPersonAsset || (dp.fromPrepoLoc == null)) {      from.setIndirectObject(dp.fromLoc);    } else {      from.setIndirectObject(dp.fromPrepoLoc);    }    prepphrases.addElement(from);                    //   FOR  (me)    NewPrepositionalPhrase forpp = theLDMF.newPrepositionalPhrase();    forpp.setPreposition(Constants.Preposition.FOR);    Object forOrgID;    try {      forOrgID = 	dp.forOrg.getItemIdentificationPG().getItemIdentification();    } catch (Exception eForOrg) {      printError("SELF "+dp.forOrg+" Lacks ItemIdentification!  Using Org!");      forOrgID = dp.forOrg;    }    forpp.setIndirectObject(forOrgID);    prepphrases.addElement(forpp);     //   OFTYPE  (StrategicTransport)    // create prepositionalphrase oftype strategictransportation    NewPrepositionalPhrase pp = theLDMF.newPrepositionalPhrase();    pp.setPreposition(Constants.Preposition.OFTYPE);    if (strans == null) {      try {        PlanningFactory ldmfactory = getFactory();        Asset strans_proto = ldmfactory.createPrototype(           Class.forName( "org.cougaar.planning.ldm.asset.AbstractAsset" ),           "StrategicTransportation" );        strans = (AbstractAsset)ldmfactory.createInstance( strans_proto );      } catch (Exception exc) {        printError("Unable to create abstract strategictransport\n"+exc);      }    }    pp.setIndirectObject(strans);    prepphrases.addElement(pp);    // Kludge - add "PREPO" prepositional phrase if we aren't using the Org's from loc    if ((dp.fromPrepoLoc != null) &&  !isPersonAsset) {	pp = theLDMF.newPrepositionalPhrase();	pp.setPreposition("PREPO");	prepphrases.add(pp);    }    // PREFERENCES    Vector prefs = new Vector();    //   START DATE  (startTime)    AspectValue startAV;    if (isPersonAsset) {      startAV = AspectValue.newAspectValue(AspectType.START_TIME, dp.startTime.getTime());    } else {      startAV = AspectValue.newAspectValue(AspectType.START_TIME, dp.prepoStartTime.getTime());    }    ScoringFunction startSF =      ScoringFunction.createNearOrAbove(startAV, 0);    Preference startPref =      theLDMF.newPreference(AspectType.START_TIME, startSF);    prefs.addElement(startPref);    //   END DATE    (RDD - 5*range) <= RDD - range <= RDD    Date lateEndDate = dp.thruTime;    Date bestEndDate = ShortDateFormat.adjustDate(lateEndDate, 0, -dp.thruRange);    Date earlyEndDate = ShortDateFormat.adjustDate(lateEndDate, 0, -5*dp.thruRange);    if (earlyEndDate.before(dp.startTime))      earlyEndDate = dp.startTime;    AspectValue earlyEndAV =       AspectValue.newAspectValue(AspectType.END_TIME, earlyEndDate.getTime());    AspectValue bestEndAV =       AspectValue.newAspectValue(AspectType.END_TIME, bestEndDate.getTime());    AspectValue lateEndAV =       AspectValue.newAspectValue(AspectType.END_TIME, lateEndDate.getTime());    ScoringFunction endSF =       ScoringFunction.createStrictlyBetweenWithBestValues(          earlyEndAV, bestEndAV, lateEndAV);    Preference endPref = theLDMF.newPreference(AspectType.END_TIME, endSF);    prefs.addElement(endPref);    // Get Workflow    NewWorkflow wf = dp.expandedWorkflow;    boolean newWf = (wf == null);    if (newWf) {      wf = theLDMF.newWorkflow();      dp.expandedWorkflow = wf;      wf.setIsPropagatingToSubtasks(true);      wf.setParentTask(drTask);      if (logger.isDebugEnabled()) {        printDebug("made new workflow - " + wf);      }    } else {      if (logger.isDebugEnabled()) {        printDebug("Using existing workflow - " + wf + " that has these tasks : ");      }      Enumeration eT = wf.getTasks();      while (eT.hasMoreElements()) {        Task ewft = (Task)eT.nextElement();        if (logger.isDebugEnabled()) {          printDebug(" task: "+ewft.getVerb()+ " (" + ewft.getUID() + ") " +                     " asset: "+ewft.getDirectObject());        }      }    }    // Make sure we can modify the PlanElement!    // create the subtasks.  first add our "first" and "second"    // assets, then use the enumeration.    boolean useAssetsEnum = false;    Asset assetForTransport = firstAssetForTransport;    while (true) {      // Create transport task       NewTask subtask = makeTransportTask (drTask, assetForTransport, prepphrases, prefs, wf);      // publish task      publishAdd(subtask);      // next asset      if (useAssetsEnum) {        // third and later assets        if (!(assetsEnum.hasMoreElements())) {          // done!          break;        }        assetForTransport = (Asset)assetsEnum.nextElement();        if (assetForTransport == null) {          printError("Null asset for Transport?!!!");          continue;        }      } else {        // second asset        if (secondAssetForTransport == null) {          // done!  only one asset to transport.          break;        }        assetForTransport = secondAssetForTransport;        // next time use the enumeration        useAssetsEnum = true;      }    }    if (newWf) {      // make the expansion      if (logger.isDebugEnabled()) {        printDebug("Create expansion of determine requirement task " + drTask.getUID());      }      AllocationResult estimatedResult =         PluginHelper.createEstimatedAllocationResult(drTask, theLDMF, 1.0, true);      PlanElement pe =         theLDMF.createExpansion(          drTask.getPlan(),          drTask,          wf,          estimatedResult);      publishAdd(pe);      if (logger.isDebugEnabled()) {	printDebug("Published new DetermineReqs Task Expansion for d.r. task " + drTask.getUID());      }    }    else {      if (logger.isDebugEnabled()) {	printDebug("Publish changed expansion of d.r. task " + drTask.getUID());      }	      publishChange (wf.getParentTask().getPlanElement());    }  }