001: /*--------------------------------------------------------------------------
002: * <copyright>
003: *
004: * Copyright 1999-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 org.cougaar.glm.ldm.asset.Organization;
029: import org.cougaar.glm.ldm.asset.SupplyClassPG;
030: import org.cougaar.glm.ldm.plan.GeolocLocation;
031: import org.cougaar.planning.ldm.asset.Asset;
032: import org.cougaar.planning.ldm.asset.LocationSchedulePG;
033: import org.cougaar.planning.ldm.asset.TypeIdentificationPG;
034: import org.cougaar.planning.ldm.plan.LocationScheduleElement;
035: import org.cougaar.planning.ldm.plan.Relationship;
036: import org.cougaar.planning.ldm.plan.RelationshipSchedule;
037: import org.cougaar.planning.ldm.plan.Role;
038: import org.cougaar.planning.ldm.plan.Schedule;
039: import org.cougaar.util.MutableTimeSpan;
040: import org.cougaar.util.log.Logger;
041: import org.cougaar.util.log.Logging;
042:
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import java.util.Enumeration;
046: import java.util.HashMap;
047: import java.util.Iterator;
048: import java.util.Vector;
049:
050: /**
051: * Provides convenience methods.
052: */
053: public class AssetUtils {
054:
055: /**
056: * Cache for isAssetOfType. Keys are Strings, values are either
057: * null (unknown), an Asset class (the type), or Object (== assetFail).
058: */
059: private static final HashMap assetTypes = new HashMap(89);
060: private static final Object assetFail = new Object();
061: private static Logger logger = Logging.getLogger(AssetUtils.class);
062:
063: /**
064: * @param type java Class name (assumed to be in package org.cougaar.planning.ldm.asset)
065: * @return true if asset is and instance of type.
066: */
067: public static boolean isAssetOfType(Asset a, String type) {
068: Class cl;
069: synchronized (assetTypes) {
070: Object at = assetTypes.get(type);
071: if (at == null) {
072: try {
073: cl = Class.forName("org.cougaar.glm.ldm.asset."
074: + type);
075: assetTypes.put(type, cl);
076: } catch (ClassNotFoundException cnfe) {
077: assetTypes.put(type, assetFail);
078: if (logger.isErrorEnabled()) {
079: logger.error("isAssetOfType: " + cnfe);
080: }
081: return false;
082: }
083: } else if (at == assetFail) {
084: // failed before
085: return false;
086: } else {
087: cl = (Class) at;
088: }
089: }
090: return cl.isInstance(a);
091: }
092:
093: /**
094: * @param type String describing class of resource
095: * @return true if Asset has SupplyClassPG and SupplyType equals type
096: */
097: public static boolean isSupplyClassOfType(Asset asset, String type) {
098: boolean result = false;
099: if (asset == null) {
100: return false;
101: }
102: SupplyClassPG pg = (SupplyClassPG) asset
103: .searchForPropertyGroup(SupplyClassPG.class);
104: if (pg != null) {
105: result = type.equals(pg.getSupplyType());
106: } else {
107: if (logger.isDebugEnabled()) {
108: logger
109: .debug("isSupplyClassOfType(): Asset without SupplyClassPG "
110: + assetDesc(asset));
111: }
112: }
113: return result;
114: }
115:
116: public static String assetDesc(Asset asset) {
117: String nsn = getAssetIdentifier(asset);
118: return nsn + "(" + getPartNomenclature(asset) + ")";
119: }
120:
121: public static String getPartNomenclature(Asset part) {
122: String nomen = "Unknown part name";
123: TypeIdentificationPG tip = part.getTypeIdentificationPG();
124: if (tip != null) {
125: nomen = tip.getNomenclature();
126: }
127: return nomen;
128: }
129:
130: public static String getAssetIdentifier(Asset asset) {
131:
132: if (asset == null) {
133: return null;
134: } else {
135: TypeIdentificationPG tip = asset.getTypeIdentificationPG();
136: if (tip != null) {
137: return tip.getTypeIdentification();
138: } else {
139: if (logger.isErrorEnabled()) {
140: logger.error("asset: " + asset
141: + " has null getTypeIdentificationPG()");
142: }
143: return null;
144: }
145: }
146: }
147:
148: // Determines if an orginazation provides a supporting role for another organization.
149: public static boolean isOrgSupporting(Organization org,
150: Organization support_org, Role role) {
151: RelationshipSchedule rel_sched = org.getRelationshipSchedule();
152: // Ask for matching relationships where support_org is the provider.
153: // MutableTimeSpan() will create a TimeSpan from the beginning of time to the end of time and
154: // therefore look at all relationships.
155: Collection c = rel_sched.getMatchingRelationships(role,
156: support_org, new MutableTimeSpan());
157: return !c.isEmpty();
158: }
159:
160: public static Enumeration getSupportingOrgs(Organization myOrg,
161: Role role, long time) {
162: return getSupportingOrgs(myOrg, role, time, time);
163: }
164:
165: public static Enumeration getSupportingOrgs(Organization myOrg,
166: Role role, long start, long end) {
167: RelationshipSchedule rel_sched = myOrg
168: .getRelationshipSchedule();
169: Collection c = rel_sched.getMatchingRelationships(role, start,
170: end);
171: Vector support_orgs = new Vector();
172: Iterator i = c.iterator();
173: Relationship r;
174: while (i.hasNext()) {
175: r = (Relationship) i.next();
176: support_orgs.add(rel_sched.getOther(r));
177: }
178: return support_orgs.elements();
179: }
180:
181: public static Enumeration getGeolocLocationAtTime(Organization org,
182: long time) {
183: LocationSchedulePG lspg = org.getLocationSchedulePG();
184: Vector geolocs = new Vector();
185: try {
186: Schedule ls = lspg.getSchedule();
187: Iterator i = ls.getScheduleElementsWithTime(time)
188: .iterator();
189: while (i.hasNext()) {
190: LocationScheduleElement lse = (LocationScheduleElement) i
191: .next();
192: geolocs.add((GeolocLocation) lse.getLocation());
193: }
194: } catch (NullPointerException npe) {
195: // Not all organizations have LocationSchedulePG's
196: // GLMDebug.DEBUG("AssetUtils",
197: // "getGeolocLocationAtTime(), LocationSchedulePG NOT found on "+org);
198: }
199: return geolocs.elements();
200: }
201:
202: /**
203: * getPreviousGeolocLocation - given a time, return the organization's prior location
204: * Uses LocationSchedulePG and HomeLocation.
205: */
206: public static GeolocLocation getPreviousGeolocLocation(
207: Organization org, long time) {
208: System.out.println(99);
209: LocationSchedulePG lspg = org.getLocationSchedulePG();
210: GeolocLocation previous = null;
211: try {
212: ArrayList ls = new ArrayList(lspg.getSchedule());
213:
214: for (Iterator i = ls.iterator(); i.hasNext();) {
215: LocationScheduleElement lse = (LocationScheduleElement) i
216: .next();
217: if (lse.included(time)) {
218: break;
219: }
220: previous = (GeolocLocation) lse.getLocation();
221: }
222: } catch (NullPointerException npe) {
223: // Not all organizations have LocationSchedulePG's
224: // GLMDebug.DEBUG("AssetUtils",
225: // "getGeolocLocationAtTime(), LocationSchedulePG NOT found on "+org);
226: }
227:
228: // Assume org at home if previous location not found in the LocationSchedulePG
229: if ((previous == null) && (org.hasMilitaryOrgPG())) {
230: previous = (GeolocLocation) org.getMilitaryOrgPG()
231: .getHomeLocation();
232: }
233: return previous;
234: }
235:
236: public static void printRelationshipSchedule(Organization myOrg) {
237: RelationshipSchedule sched = myOrg.getRelationshipSchedule();
238: if (logger.isDebugEnabled()) {
239: logger
240: .debug("____________________________________________________________");
241: }
242: for (Iterator iterator = new ArrayList(sched).iterator(); iterator
243: .hasNext();) {
244: Relationship r = (Relationship) iterator.next();
245: if (logger.isDebugEnabled()) {
246: logger.debug(r.getRoleA() + ", " + r.getRoleB()
247: + ", start: " + r.getStartTime() + ", end: "
248: + r.getEndTime());
249: }
250: }
251: }
252: }
|