Source Code Cross Referenced for DemandGeneratorOutputModule.java in  » Science » Cougaar12_4 » org » cougaar » logistics » plugin » demand » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » Cougaar12_4 » org.cougaar.logistics.plugin.demand 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.