001: /*
002: * <copyright>
003: *
004: * Copyright 2001-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.logistics.plugin.trans;
027:
028: import java.util.Date;
029: import java.util.HashSet;
030:
031: import org.cougaar.planning.ldm.asset.AggregateAsset;
032: import org.cougaar.planning.ldm.asset.Asset;
033: import org.cougaar.glm.ldm.asset.GLMAsset;
034: import org.cougaar.glm.ldm.asset.MovabilityPG;
035: import org.cougaar.planning.ldm.plan.Task;
036: import org.cougaar.util.log.Logger;
037:
038: /**
039: * Create either an XML document in the Vishnu Data format or Vishnu objects from ALP objects. <p>
040: *
041: * Adds fields for physical dimensions to tasks and assets.
042: * <p>
043: */
044: public class GenericDataXMLize extends TranscomDataXMLize {
045: boolean warnAboutMissingSpeed = System.getProperty(
046: "GenericDataXMLize.warnAboutMissingSpeed", "false").equals(
047: "true");
048:
049: public GenericDataXMLize(boolean direct, Logger logger) {
050: super (direct, logger, new HashSet());
051: }
052:
053: /**
054: * Create XML for asset, subclass to add fields
055: *
056: * NOTE : field names should match those in .dff file
057: *
058: * @param object node representing asset
059: * @param taskOrAsset asset being translated
060: * @return true if should add object to list of new objects
061: */
062: protected boolean processAsset(Object object, Object taskOrAsset) {
063: GLMAsset asset = (GLMAsset) taskOrAsset;
064: if (!asset.hasContainPG())
065: return false;
066:
067: setType(object, "Asset");
068: String type = setName("Asset", object, asset);
069:
070: dataHelper.createRoleScheduleListField(object, "roleSchedule",
071: asset);
072: dataHelper.createAvailableScheduleListField(object,
073: "availableSchedule", asset);
074:
075: dataHelper.createFloatField(object, "speed",
076: (float) getSpeed(asset));
077: dataHelper.createFloatField(object, "weightCapacity",
078: (float) getWeightCapacity(asset));
079: dataHelper.createFloatField(object, "areaCapacity",
080: (float) getAreaCapacity(asset));
081: addPassengerCapacity(object, asset);
082:
083: if (logger.isDebugEnabled())
084: logger.debug("GenericDataXMLize.processAsset - type "
085: + type);
086: if (direct && logger.isDebugEnabled())
087: logger
088: .debug("GenericDataXMLize.processAsset - created resource : "
089: + object);
090:
091: return true;
092: }
093:
094: protected void addPassengerCapacity(Object object, GLMAsset asset) {
095: dataHelper.createFloatField(object, "passengerCapacity",
096: (float) getPassengerCapacity(asset));
097: }
098:
099: /**
100: * Create XML for task, subclass to add fields
101: *
102: * NOTE : field names should match those in .dff file
103: *
104: * @param object node representing task
105: * @param taskOrAsset task being translated
106: * @return true if should add object to list of new objects
107: */
108: protected boolean processTask(Object object, Object taskOrAsset) {
109: super .processTask(object, taskOrAsset);
110: Task task = (Task) taskOrAsset;
111: Asset directObject = task.getDirectObject();
112: GLMAsset baseAsset;
113:
114: if (directObject instanceof AggregateAsset) {
115: baseAsset = (GLMAsset) ((AggregateAsset) directObject)
116: .getAsset();
117: } else {
118: baseAsset = (GLMAsset) directObject;
119: }
120:
121: dataHelper.createFloatField(object, "weight",
122: (float) getWeight(baseAsset));
123: dataHelper.createFloatField(object, "area",
124: (float) getArea(baseAsset));
125: dataHelper.createFloatField(object, "quantity",
126: (float) getQuantity(directObject));
127: dataHelper.createBooleanField(object, "isVehicle",
128: isVehicle(baseAsset));
129: Date earliestArrival = new Date(glmPrefHelper
130: .getEarlyDate(task).getTime());
131: dataHelper.createDateField(object, "earliestArrival",
132: earliestArrival);
133:
134: /*
135: if (direct && logger.isDebugEnabled()) {
136: logger.debug ("GenericDataXMLize.processTask - created task : " + object);
137: Reusable.RInteger departureTime =
138: (Reusable.RInteger) ((SchObject)object).getField ("departure");
139: Reusable.RInteger arrivalTime =
140: (Reusable.RInteger) ((SchObject)object).getField ("arrival");
141:
142: logger.debug ("\tdeparture " + timeOps.timeToString (departureTime.intValue()) +
143: " arrival " + timeOps.timeToString (arrivalTime.intValue()));
144: }
145: */
146:
147: return true;
148: }
149:
150: protected double getSpeed(GLMAsset asset) {
151: double speed = 55;
152:
153: try {
154: speed = asset.getGroundSelfPropulsionPG().getCruiseSpeed()
155: .getMilesPerHour();
156: } catch (Exception e) {
157: try {
158: speed = asset.getAirSelfPropulsionPG().getCruiseSpeed()
159: .getMilesPerHour();
160: } catch (Exception ee) {
161: try {
162: speed = asset.getWaterSelfPropulsionPG()
163: .getCruiseSpeed().getMilesPerHour();
164: } catch (Exception eee) {
165: if (warnAboutMissingSpeed) {
166: logger
167: .warn("GenericDataXMLize.getSpeed - WARNING - Could not determine"
168: + " resource speed for "
169: + asset.getUID());
170: }
171: }
172: }
173: }
174:
175: return speed;
176: }
177:
178: protected double getWeightCapacity(GLMAsset asset) {
179: return (asset.hasContainPG() && asset.getContainPG()
180: .getMaximumWeight() != null) ? asset.getContainPG()
181: .getMaximumWeight().getShortTons() : 0.0d;
182: }
183:
184: protected double getWeight(GLMAsset asset) {
185: return (asset.hasPhysicalPG() && asset.getPhysicalPG()
186: .getMass() != null) ? asset.getPhysicalPG().getMass()
187: .getShortTons() : 0.0d;
188: }
189:
190: protected double getAreaCapacity(GLMAsset asset) {
191: return (asset.hasContainPG() && asset.getContainPG()
192: .getMaximumFootprintArea() != null) ? asset
193: .getContainPG().getMaximumFootprintArea()
194: .getSquareFeet() : 0.0;
195: }
196:
197: protected double getArea(GLMAsset asset) {
198: return (asset.hasPhysicalPG() && asset.getPhysicalPG()
199: .getFootprintArea() != null) ? asset.getPhysicalPG()
200: .getFootprintArea().getSquareFeet() : 0.0d;
201: }
202:
203: /** returns 1 if asset isn't an aggregate */
204: protected double getQuantity(Asset asset) {
205: return (asset instanceof AggregateAsset) ? ((AggregateAsset) asset)
206: .getQuantity()
207: : 1;
208: }
209:
210: protected double getPassengerCapacity(GLMAsset asset) {
211: return (asset.hasContainPG()) ? asset.getContainPG()
212: .getMaximumPassengers() : 0.0d;
213: }
214:
215: /**
216: * <pre>
217: * Something is a vehicle if
218: * a) it has a ground vehicle PG OR
219: * b) it has a movability PG with a cargo category code whose
220: * first character is either R or A
221: *
222: * </pre>
223: * @return true if asset is a vehicle
224: */
225: protected boolean isVehicle(GLMAsset asset) {
226: if (asset.hasGroundVehiclePG())
227: return true;
228:
229: if (!asset.hasMovabilityPG())
230: return false;
231:
232: try {
233: MovabilityPG move_prop = asset.getMovabilityPG();
234: String cargocatcode = move_prop.getCargoCategoryCode();
235: char first = cargocatcode.charAt(0);
236: if (first == 'R' || first == 'A')
237: return true;
238: } catch (Exception e) {
239: return false;
240: }
241:
242: return false;
243: }
244: }
|