001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.glm.util;
028:
029: import java.util.Enumeration;
030: import java.util.Vector;
031:
032: import org.cougaar.core.blackboard.CollectionSubscription;
033: import org.cougaar.core.blackboard.IncrementalSubscription;
034: import org.cougaar.planning.ldm.PlanningFactory;
035: import org.cougaar.planning.ldm.asset.AbstractAsset;
036: import org.cougaar.planning.ldm.plan.AllocationResult;
037: import org.cougaar.planning.ldm.plan.AspectScorePoint;
038: import org.cougaar.planning.ldm.plan.PlanElement;
039: import org.cougaar.planning.ldm.plan.Preference;
040: import org.cougaar.planning.ldm.plan.PrepositionalPhrase;
041: import org.cougaar.planning.ldm.plan.ScoringFunction;
042: import org.cougaar.planning.ldm.plan.Task;
043:
044: /**
045: * Helper class for building Allocator Plugins.
046: */
047: public class AllocatorHelper extends
048: org.cougaar.planning.plugin.util.AllocatorHelper {
049:
050: /**
051: * UpdatePV looks for differences between the reported and
052: * estimated allocation results. If they are
053: * different then the estimated value is set to the reported
054: * value in both the cases.
055: */
056: public static void updatePV(PlanElement pe,
057: CollectionSubscription sub) {
058:
059: if (pe.getReportedResult() != null) {
060: //compare the result objects.
061: // If they are NOT ==, re-set the estimated result.
062: // For now ignore whether their compositions are equal.
063: AllocationResult repar = pe.getReportedResult();
064: AllocationResult estar = pe.getEstimatedResult();
065: if ((estar == null) || (!(repar == estar))) {
066: pe.setEstimatedResult(repar);
067: sub.getSubscriber().publishChange(pe);
068: }
069: }
070: }
071:
072: /** Takes a subscription, gets the changed list and updates the changedList.*/
073: public static void updateAllocationResult(
074: IncrementalSubscription sub) {
075:
076: Enumeration changedPEs = sub.getChangedList();
077: while (changedPEs.hasMoreElements()) {
078: PlanElement pe = (PlanElement) changedPEs.nextElement();
079: if (pe.getReportedResult() != null) {
080: //compare entire pv arrays
081: AllocationResult repar = pe.getReportedResult();
082: AllocationResult estar = pe.getEstimatedResult();
083: if ((estar == null) || (!repar.isEqual(estar))) {
084: pe.setEstimatedResult(repar);
085: sub.getSubscriber().publishChange(pe);
086: }
087: }
088: }
089: }
090:
091: /**
092: * Checks if the Task is of specified OFTYPE.
093: */
094: public static boolean isOfType(Task t, String p, String typeid) {
095: Enumeration prepPhrases = ((Task) t).getPrepositionalPhrases();
096: PrepositionalPhrase pPhrase;
097: String prep;
098: AbstractAsset aa = null;
099: String mytypeid = null;
100:
101: while (prepPhrases.hasMoreElements()) {
102: pPhrase = (PrepositionalPhrase) prepPhrases.nextElement();
103: prep = pPhrase.getPreposition();
104: if (prep.equals(p)) {
105: Object indirectobj = pPhrase.getIndirectObject();
106: if (indirectobj instanceof AbstractAsset) {
107: aa = (AbstractAsset) indirectobj;
108: mytypeid = aa.getTypeIdentificationPG()
109: .getTypeIdentification();
110: if (mytypeid.equals(typeid))
111: return true;
112: }
113: }
114: }
115: return false;
116: }
117:
118: public static AllocationResult createEstimatedAllocationResult(
119: Task t, PlanningFactory ldmf) {
120: Enumeration preferences = t.getPreferences();
121: if (preferences != null && preferences.hasMoreElements()) {
122: // do something really simple for now.
123: Vector aspects = new Vector();
124: Vector results = new Vector();
125: while (preferences.hasMoreElements()) {
126: Preference pref = (Preference) preferences
127: .nextElement();
128: int at = pref.getAspectType();
129: aspects.addElement(new Integer(at));
130: ScoringFunction sf = pref.getScoringFunction();
131: // allocate as if you can do it at the "Best" point
132: double myresult = ((AspectScorePoint) sf.getBest())
133: .getValue();
134: results.addElement(new Double(myresult));
135: }
136: int[] aspectarray = new int[aspects.size()];
137: double[] resultsarray = new double[results.size()];
138: for (int i = 0; i < aspectarray.length; i++)
139: aspectarray[i] = (int) ((Integer) aspects.elementAt(i))
140: .intValue();
141: for (int j = 0; j < resultsarray.length; j++)
142: resultsarray[j] = (double) ((Double) results
143: .elementAt(j)).doubleValue();
144:
145: AllocationResult myestimate = ldmf.newAllocationResult(0.0,
146: true, aspectarray, resultsarray);
147: return myestimate;
148: }
149: // if there were no preferences...return a null estimate for the allocation result (for now)
150: return null;
151: }
152:
153: /*
154: public static void allocateToIndirectObjects ( Workflow wf, PlanningFactory ldmf, Subscriber mySubscriber ) {
155: Asset a = null;
156: Enumeration tasks = wf.getTasks();
157: while ( tasks.hasMoreElements()) {
158: Task t = (Task) tasks.nextElement();
159: AllocationResult estimatedresult = createEstimatedAllocationResult(t, ldmf);
160: Enumeration pp = t.getPrepositionalPhrases();
161: while (pp.hasMoreElements()) {
162: PrepositionalPhrase p = (PrepositionalPhrase) pp.nextElement();
163: if (p.getPreposition().equals(Preposition.FOR)) {
164: Object indir = p.getIndirectObject();
165: if ( indir instanceof Organization) {
166: a = (Asset) indir;
167: Allocation alloc = ldmf.createAllocation(ldmf.getRealityPlan(), t, a, estimatedresult, Role.BOGUS);
168: mySubscriber.publishAdd(alloc);
169: }
170: }
171: }
172: }
173: }
174: */
175: }
|