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: package org.cougaar.glm.plugins;
027:
028: import java.io.Serializable;
029: import java.util.Enumeration;
030: import java.util.Vector;
031:
032: import org.cougaar.util.TimeSpan;
033: import org.cougaar.util.MutableTimeSpan;
034: import org.cougaar.core.blackboard.IncrementalSubscription;
035: import org.cougaar.core.mts.MessageAddress;
036: import org.cougaar.core.util.UID;
037: import org.cougaar.glm.ldm.oplan.Oplan;
038: import org.cougaar.glm.ldm.oplan.OrgActivity;
039: import org.cougaar.glm.ldm.plan.GeolocLocation;
040:
041: /**
042: * Object for holding meta-data about the OPlan on the blackboard.
043: **/
044: public class ClusterOPlan implements Serializable {
045: MessageAddress clusterId_;
046: long startTime_, endTime_;
047: Vector orgActivities_ = null;
048: Oplan oplan_;
049:
050: // IncrementalSubscription orgActivitySubscription_;
051:
052: public ClusterOPlan(MessageAddress id, Oplan op/*, IncrementalSubscription sub*/) {
053: // System.out.println("--- Creating ClusterOPlan for "+id+", oplan "+op);
054: clusterId_ = id;
055: oplan_ = op;
056: startTime_ = oplan_.getCday().getTime();
057: endTime_ = startTime_ + 1;
058: // endTime_ = oplan_.getEndDay().getTime();
059: // orgActivitySubscription_ = sub;
060: // updateOrgActivities(orgActivitySubscription_.elements());
061: // updateOPlanTimes();
062: }
063:
064: /* If OPlan does not change but OrgActivities for the OPlan change
065: * then get the updated OrgActivities.
066: */
067: public boolean updateOrgActivities(
068: IncrementalSubscription orgActivitySubscription_) {
069: // Only update OrgActivities if subscription has changed
070: if (orgActivitySubscription_.getChangedList().hasMoreElements()
071: || orgActivitySubscription_.getAddedList()
072: .hasMoreElements()
073: || orgActivitySubscription_.getRemovedList()
074: .hasMoreElements()) {
075: // System.out.println("--- New/Changed/Removed OrgActivities for "+clusterId_);
076: updateOrgActivities(orgActivitySubscription_.elements());
077: updateOPlanTimes();
078: return true;
079: }
080: return false;
081: }
082:
083: // Re-calculate the overall start and end times for the OPlan
084: protected void updateOPlanTimes() {
085: long end_time;
086: long newET = Long.MIN_VALUE;
087: long start_time;
088: long newST = Long.MAX_VALUE;
089: OrgActivity activity;
090: Enumeration activities = orgActivities_.elements();
091:
092: // initialize endTime/startTime values
093: if (activities.hasMoreElements()) {
094: activity = (OrgActivity) activities.nextElement();
095: newET = getEndTime(activity);
096: newST = getStartTime(activity);
097: }
098:
099: // search for first/last times
100: while (activities.hasMoreElements()) {
101: activity = (OrgActivity) activities.nextElement();
102: end_time = getEndTime(activity);
103: if ((end_time > newET)) {
104: newET = end_time;
105: }
106: start_time = getStartTime(activity);
107: if (start_time < newST) {
108: newST = start_time;
109: }
110: }
111:
112: if (newET != endTime_ || newST != startTime_) {
113: //System.out.println("ClusterOplan at " + clusterId_ + " updating startTime from " + startTime_ + " to " + newST + ", and endTime from " + endTime_ + " to " + newET);
114: synchronized (this ) {
115: endTime_ = newET;
116: startTime_ = newST;
117: }
118: }
119: }
120:
121: public void updateOrgActivities(Enumeration activities) {
122: orgActivities_ = new Vector();
123: OrgActivity orgact;
124: String cluster_name = clusterId_.toString();
125: while (activities.hasMoreElements()) {
126: // only deal w/ org activities for this cluster
127: orgact = (OrgActivity) activities.nextElement();
128: if (orgact.getOrgID().equals(cluster_name)) {
129: // System.out.println("--- Adding OrgActivity for "+clusterId_+", activity "+orgact.getActivityName()+
130: // ", "+orgact.getOpTempo()+", "+orgact);
131: orgActivities_.add(orgact);
132: }
133: }
134: }
135:
136: /* getOplan() returns the OPlan UID this object is handling
137: */
138: public UID getOplanUID() {
139: return oplan_.getUID();
140: }
141:
142: public long getEndTime(OrgActivity act) {
143: return act.getTimeSpan().getEndDate().getTime();
144: }
145:
146: public long getStartTime(OrgActivity act) {
147: return act.getTimeSpan().getStartDate().getTime();
148: }
149:
150: /* Latest end time of the OrgActivities
151: * If no orgActivities are received, end time is not set
152: * <b>WARN</b>: This is not safe, particularly if you will be comparing to StartTime. Use #getOplanSpan()
153: */
154: public long getEndTime() {
155: return endTime_;
156: }
157:
158: /* Earliest start time of the OrgActivities
159: * If no orgActivities are received, start time is not set
160: * <b>WARN</b>: This is not safe, particularly if you will be comparing to EndTime. Use #getOplanSpan()
161: */
162: public long getStartTime() {
163: return startTime_;
164: }
165:
166: /**
167: * Return the current start and end times (longs) of the OPlan
168: **/
169: public synchronized TimeSpan getOplanSpan() {
170: MutableTimeSpan ts = new MutableTimeSpan();
171: try {
172: ts.setTimeSpan(startTime_, endTime_);
173: } catch (IllegalArgumentException iae) {
174: System.err
175: .println(clusterId_
176: + " ClusterOPlan has invalid oplan start/end times. Start: "
177: + startTime_ + ", End: " + endTime_);
178: }
179: return ts;
180: }
181:
182: public GeolocLocation getGeoLoc(long time) {
183: OrgActivity oa = getOrgActivity(time);
184: if (oa != null) {
185: return oa.getGeoLoc();
186: }
187: return null;
188: }
189:
190: public String getOpTempo(long time) {
191: OrgActivity oa = getOrgActivity(time);
192: if (oa != null) {
193: return oa.getOpTempo();
194: }
195: return null;
196: }
197:
198: public long getOplanCday() {
199: return oplan_.getCday().getTime();
200: }
201:
202: public OrgActivity getOrgActivity(long t) {
203: long end_time, start_time;
204: Enumeration en = orgActivities_.elements();
205: OrgActivity oa;
206: while (en.hasMoreElements()) {
207: oa = (OrgActivity) en.nextElement();
208: start_time = getStartTime(oa);
209: end_time = getEndTime(oa);
210: if ((t >= start_time) && (t < end_time)) {
211: return oa;
212: }
213: }
214: return null;
215: }
216:
217: // /* When disposing of a ClusterOPlan object, need to get the
218: // * OrgActivity subscription to do an 'unsubscribe'
219: // */
220: // public IncrementalSubscription getOrgActivitySubscription() {
221: // return orgActivitySubscription_;
222: // }
223:
224: public String toString() {
225: return oplan_.toString();
226: }
227: }
|