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.planning.ldm.plan;
028:
029: import java.util.Collection;
030: import java.util.Date;
031: import java.util.Enumeration;
032: import java.util.Iterator;
033: import java.util.List;
034:
035: import org.cougaar.planning.ldm.asset.Asset;
036: import org.cougaar.util.UnaryPredicate;
037:
038: /**
039: * A RoleSchedule is a representation of an asset's scheduled
040: * commitments. These commitments(plan elements) are stored
041: * in a collection. RoleSchedules do not travel with an
042: * asset accross agent boundaries, therefore, the roleschedule
043: * is only valid while that asset is assigned to the current agent.
044: **/
045: public class RoleScheduleImpl extends ScheduleImpl implements
046: RoleSchedule, NewRoleSchedule {
047: private transient Schedule availableschedule;
048: private Asset asset;
049:
050: /** Constructor
051: * @param theasset this roleschedule is attached to
052: **/
053: public RoleScheduleImpl(Asset theasset) {
054: super ();
055: setScheduleType(ScheduleType.ROLE);
056: setScheduleElementType(ScheduleElementType.PLAN_ELEMENT);
057: asset = theasset;
058: }
059:
060: /** @return the Asset of this roleschedule.
061: **/
062: public Asset getAsset() {
063: return asset;
064: }
065:
066: /** SHOULD *ONLY* BE CALLED BY THE ASSET CREATOR or THE ASSETTRANSFER LP!
067: * set the availableschedule
068: * @param avschedule - the schedule that the asset is assigned
069: * or available to this agent
070: **/
071: public void setAvailableSchedule(Schedule avschedule) {
072: availableschedule = avschedule;
073: }
074:
075: //return the available schedule for this asset
076: public Schedule getAvailableSchedule() {
077: return availableschedule;
078: }
079:
080: /**
081: * Cougaar INTERNAL METHOD - SHOULD NEVER BE CALLED BY A PLUGIN
082: * add a single planelement to the roleschedule container
083: * @param aPlanElement PlanElement to add
084: * @deprecated Use add(Object aPlanElement) instead.
085: **/
086: public synchronized void addToRoleSchedule(PlanElement aPlanElement) {
087: add(aPlanElement);
088: }
089:
090: /**
091: * Cougaar INTERNAL METHOD - SHOULD NEVER BE CALLED BY A PLUGIN
092: * remove a single planelement from the roleschedule container
093: * @param aPlanElement PlanElement to remove
094: * @deprecated Use remove(Object aPlanElement) instead.
095: **/
096: public synchronized void removeFromRoleSchedule(
097: PlanElement aPlanElement) {
098: remove(aPlanElement);
099: }
100:
101: public Collection getEncapsulatedRoleSchedule(Date start, Date end) {
102: return getEncapsulatedRoleSchedule(start.getTime(), end
103: .getTime());
104: }
105:
106: public synchronized Collection getEncapsulatedRoleSchedule(
107: long start, long end) {
108: return getEncapsulatedScheduleElements(start, end);
109: }
110:
111: public synchronized Collection getEqualAspectValues(
112: final int aspect, final double value) {
113: return filter(new UnaryPredicate() {
114: public boolean execute(Object obj) {
115: AllocationResult ar = ((PlanElement) obj)
116: .getEstimatedResult();
117: if (ar != null) {
118: return (value == ar.getValue(aspect));
119: }
120: return false;
121: }
122: });
123: }
124:
125: public synchronized Collection getMatchingRoleElements(
126: final Role aRole) {
127: return filter(new UnaryPredicate() {
128: public boolean execute(Object obj) {
129: if (obj instanceof Allocation) {
130: Role disrole = ((Allocation) obj).getRole();
131: if (disrole.equals(aRole)) {
132: return true;
133: }
134: } else if (obj instanceof AssetTransfer) {
135: Role disrole = ((AssetTransfer) obj).getRole();
136: if (disrole.equals(aRole)) {
137: return true;
138: }
139: }
140: return false;
141: }
142: });
143: }
144:
145: public Collection getOverlappingRoleSchedule(Date start, Date end) {
146: return getOverlappingRoleSchedule(start.getTime(), end
147: .getTime());
148: }
149:
150: public synchronized Collection getOverlappingRoleSchedule(
151: long start, long end) {
152: return getOverlappingScheduleElements(start, end);
153: }
154:
155: /** get an enumeration over a copy of all of the schedule elements of this
156: * schedule.
157: * Note that this is a copy, changes to the underlying schedule will not be
158: * reflected in the Enumeration.
159: * @return Enumeration{ScheduleElement}
160: */
161: public Enumeration getRoleScheduleElements() {
162: return getAllScheduleElements();
163: }
164:
165: /** Convenience utility that adds the requested aspectvalues of the estimated
166: * allocationresult of each PlanElement (RoleSchedule Element) in the given
167: * orderedset.
168: * If the requested aspecttype is not defined for any of the elements, nothing
169: * will be added to the sum for that particular element.
170: * This utility should be used to add aspecttypes like quantity, cost, etc.
171: * @return double The sum of the aspectvalues
172: * @param elementsToAdd A set of roleschedule elements (planelements) to add
173: * @see org.cougaar.planning.ldm.plan.AspectType
174: **/
175: public double addAspectValues(Collection elementsToAdd,
176: int aspecttype) {
177: double acc = 0.0;
178:
179: synchronized (elementsToAdd) {
180: if (elementsToAdd instanceof List) {
181: int listSize = elementsToAdd.size();
182:
183: for (int index = 0; index < listSize; index++) {
184: PlanElement anElement = (PlanElement) ((List) elementsToAdd)
185: .get(index);
186: AllocationResult aResult = anElement
187: .getEstimatedResult();
188: if (aResult != null
189: && aResult.isDefined(aspecttype)) {
190: acc += aResult.getValue(aspecttype);
191: }
192: }
193: } else {
194: for (Iterator i = elementsToAdd.iterator(); i.hasNext();) {
195: PlanElement anElement = (PlanElement) i.next();
196: AllocationResult aResult = anElement
197: .getEstimatedResult();
198: if (aResult != null
199: && aResult.isDefined(aspecttype)) {
200: acc += aResult.getValue(aspecttype);
201: }
202: }
203: }
204: } // end synchronization on elementsToAdd
205:
206: return acc;
207: }
208:
209: // for BeanInfo
210: public synchronized String[] getRoleScheduleIDs() {
211: int l = size();
212: String[] IDs = new String[l];
213: for (int i = 0; i < l; i++) {
214: IDs[i] = ((PlanElement) get(i)).getUID().toString();
215: }
216: return IDs;
217: }
218:
219: public String getRoleScheduleID(int i) {
220: return ((PlanElement) get(i)).getUID().toString();
221: }
222:
223: }
|