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.logistics.plugin.demand;
028:
029: import org.cougaar.util.log.Logger;
030:
031: import org.cougaar.planning.ldm.asset.Asset;
032: import org.cougaar.planning.ldm.plan.Task;
033: import org.cougaar.planning.ldm.plan.AspectType;
034: import org.cougaar.logistics.plugin.inventory.MaintainedItem;
035: import org.cougaar.glm.ldm.Constants;
036: import org.cougaar.glm.ldm.asset.SupplyClassPG;
037: import org.cougaar.glm.ldm.plan.GeolocLocation;
038:
039: import java.util.List;
040: import java.util.Iterator;
041: import java.util.TreeMap;
042: import java.util.ArrayList;
043: import java.io.File;
044: import java.io.FileWriter;
045: import java.io.PrintWriter;
046: import java.io.IOException;
047:
048: /**
049: * The DemandGeneratorOutputModule outputs to a file demand data
050: * based on ProjectSupply tasks.
051: *
052: * @see DemandGeneratorPlugin
053: * @see DemandGeneratorModule
054: * @see DemandGeneratorInputModule
055: **/
056: public class DemandGeneratorOutputModule {
057:
058: public final static String OUTPUT_SUBDIR = "DemandGeneratorOutput";
059:
060: protected transient Logger logger;
061: protected transient DemandGeneratorPlugin dgPlugin;
062: private boolean aborted = false;
063:
064: // PrintWriter for file output
065: private PrintWriter writer;
066:
067: /**
068: * DemandGeneratorOutputModule Constructor
069: * @param demandGeneratorPlugin Demand GeneratorPlugin for which this module
070: * is created
071: */
072: public DemandGeneratorOutputModule(
073: DemandGeneratorPlugin demandGeneratorPlugin) {
074: this .dgPlugin = demandGeneratorPlugin;
075: logger = dgPlugin.getLoggingService(this );
076: }
077:
078: /**
079: * Perform the output to file task
080: * @param supplyTasks List of supply tasks from which demand is to be extracted
081: * and written to file
082: */
083: public void writeDemandOutputToFile(List supplyTasks) {
084: if (aborted) {
085: // Do not try and repeatedly open this file
086: return;
087: }
088:
089: String dirPath = System.getProperty("org.cougaar.workspace",
090: ".")
091: + File.separator + OUTPUT_SUBDIR;
092:
093: if (writer == null) {
094:
095: File dirFile = new File(dirPath);
096: // Open file for output of demand data
097: File file = new File(dirPath, "ExecutionDemand."
098: + dgPlugin.getOrgName() + "."
099: + dgPlugin.getSupplyType());
100: try {
101: dirFile.mkdirs();
102: writer = new PrintWriter(new FileWriter(file, dgPlugin
103: .getBlackboardService().didRehydrate()));
104: } catch (IOException ioe) {
105: if (logger.isErrorEnabled()) {
106: logger.error("Unable to open output file: "
107: + file.toString());
108: }
109: aborted = true;
110: return;
111: }
112: }
113:
114: // Track all demand in this ordered map so to ensure that the demand
115: // is written to file time-ordered
116: TreeMap demandMap = new TreeMap();
117:
118: // Loop through each of the supply tasks provided, extract relevant demand
119: // data and output as single entry in output file
120: Iterator tasks = supplyTasks.iterator();
121: while (tasks.hasNext()) {
122: Task task = (Task) tasks.next();
123:
124: // Timestamp on demand data will coorespond to the start time pref
125: long timeStamp = (long) task.getPreference(
126: AspectType.START_TIME).getScoringFunction()
127: .getBest().getValue();
128:
129: // Organization being supported
130: String org = (String) task.getPrepositionalPhrase(
131: Constants.Preposition.FOR).getIndirectObject();
132:
133: // Item (or truck) being maintained
134: MaintainedItem item = (MaintainedItem) task
135: .getPrepositionalPhrase(
136: Constants.Preposition.MAINTAINING)
137: .getIndirectObject();
138: String maintainedItemId = item.getItemIdentification();
139: String maintainedTypeId = item.getTypeIdentification();
140: String maintainedNomen = item.getNomenclature();
141:
142: // Required Item Id
143: Asset consumed = task.getDirectObject();
144: String consumedId = consumed.getTypeIdentificationPG()
145: .getTypeIdentification();
146: SupplyClassPG supplyPG = (SupplyClassPG) consumed
147: .searchForPropertyGroup(org.cougaar.glm.ldm.asset.SupplyClassPG.class);
148: if (supplyPG == null) {
149: if (logger.isErrorEnabled()) {
150: logger
151: .error("Unable to retrieve SupplyClassPG for "
152: + consumedId);
153: }
154: continue;
155: }
156: String type = supplyPG.getSupplyType();
157: if (!type.equals(dgPlugin.getSupplyType())) {
158: continue;
159: }
160:
161: // Required Item Qty
162: double qty = task.getPreference(AspectType.QUANTITY)
163: .getScoringFunction().getBest().getValue();
164:
165: // Geoloc
166: GeolocLocation geoloc = (GeolocLocation) task
167: .getPrepositionalPhrase(Constants.Preposition.TO)
168: .getIndirectObject();
169:
170: // Construct simple csv formatted line of data for this demand & write to file
171: String output = timeStamp + "," + org + ","
172: + maintainedItemId + "," + maintainedTypeId + ","
173: + maintainedNomen + "," + consumedId + "," + qty
174: + "," + geoloc.getLatitude().getDegrees() + ","
175: + geoloc.getLongitude().getDegrees() + ","
176: + geoloc.getName();
177:
178: Long key = new Long(timeStamp);
179: ArrayList list = (ArrayList) demandMap.get(key);
180: if (list == null) {
181: list = new ArrayList();
182: demandMap.put(key, list);
183: }
184: list.add(output);
185: }
186:
187: // Loop through all demand and write to file
188: Iterator lists = demandMap.values().iterator();
189: while (lists.hasNext()) {
190: ArrayList list = (ArrayList) lists.next();
191: Iterator outputs = list.iterator();
192: while (outputs.hasNext()) {
193: String output = (String) outputs.next();
194: writer.println(output);
195: }
196: }
197: writer.flush();
198: }
199: }
|