Source Code Cross Referenced for DetermineRequirementsExpander.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 java.util.Collection;
030:        import java.util.ArrayList;
031:        import java.util.Vector;
032:        import java.util.Iterator;
033:        import java.util.Enumeration;
034:        import java.util.HashSet;
035:
036:        import org.cougaar.planning.ldm.asset.Asset;
037:        import org.cougaar.planning.ldm.plan.*;
038:        import org.cougaar.planning.ldm.asset.AggregateAsset;
039:        import org.cougaar.planning.ldm.measure.CountRate;
040:        import org.cougaar.planning.plugin.util.PluginHelper;
041:        import org.cougaar.logistics.ldm.Constants;
042:        import org.cougaar.glm.ldm.plan.AlpineAspectType;
043:
044:        /**
045:         * <pre>
046:         * The default RequirementsExpander for the DemandForecastPlugin.
047:         *
048:         * This class expands a determine requirements task into generateProjections tasks for each
049:         * MEI that has the passed in supply class PG on it.
050:         *
051:         *
052:         **/
053:
054:        public class DetermineRequirementsExpander extends DemandForecastModule
055:                implements  DetReqExpanderIfc {
056:
057:            public DetermineRequirementsExpander(DemandForecastPlugin dfPlugin) {
058:                super (dfPlugin);
059:            }
060:
061:            /**
062:             * Expand DetermineRequirements tasks into GenerateProjections tasks.
063:             **/
064:            public void expandDetermineRequirements(Task detReqTask,
065:                    Collection assets) {
066:                if ((assets == null) || (assets.isEmpty())) {
067:                    disposeOfTask(detReqTask);
068:                    return;
069:                }
070:                ArrayList gpTasks = new ArrayList();
071:                Iterator assetIT = assets.iterator();
072:                while (assetIT.hasNext()) {
073:                    Asset consumer = (Asset) assetIT.next();
074:                    if (getAssetUtils().getQuantity(consumer) > 0) {
075:                        NewTask gpTask = createGPTask(detReqTask, consumer);
076:                        gpTasks.add(gpTask);
077:                    } else {
078:                        if (logger.isWarnEnabled()) {
079:                            Asset asset;
080:                            if (consumer instanceof  AggregateAsset) {
081:                                asset = ((AggregateAsset) consumer).getAsset();
082:                            } else {
083:                                // Not expecting this case, only AggregateAssets can have a zero quantity
084:                                asset = consumer.getPrototype();
085:                            }
086:                            logger
087:                                    .warn("Ignoring Asset: "
088:                                            + getAssetUtils()
089:                                                    .getAssetIdentifier(asset)
090:                                            + " at "
091:                                            + dfPlugin.getMyOrganization()
092:                                            + " - Asset has a quantity of zero. Bug #3467");
093:                        }
094:                    }
095:                }
096:                if (gpTasks.isEmpty()) {
097:                    if (logger.isWarnEnabled()) {
098:                        logger
099:                                .warn("Cannot expand - no subtasks for determine requirements task "
100:                                        + getTaskUtils().taskDesc(detReqTask));
101:                    }
102:                } else {
103:                    PlanElement pe = detReqTask.getPlanElement();
104:                    if ((pe != null) && (pe instanceof  Disposition)) {
105:                        dfPlugin.publishRemove(pe);
106:                        pe = null;
107:                    }
108:                    // First time through build a fresh expansion
109:                    if (pe == null) {
110:                        createAndPublishExpansion(detReqTask, gpTasks);
111:                    }
112:                    // There are new assets to add to the expansion
113:                    else if (pe instanceof  Expansion) {
114:                        addToAndPublishExpansion(detReqTask, gpTasks);
115:                    } else {
116:                        if (logger.isErrorEnabled()) {
117:                            logger
118:                                    .error("Unhandled plan element type on DetermineRequirementsTask :"
119:                                            + pe.getClass().getName());
120:                        }
121:                    }
122:                }
123:            }
124:
125:            public void removeSubtasksFromDetermineRequirements(
126:                    Task detReqTask, Collection removedAssets) {
127:                Expansion expansion = (Expansion) detReqTask.getPlanElement();
128:                NewWorkflow wf = (NewWorkflow) expansion.getWorkflow();
129:                HashSet remTaskHash = new HashSet(removedAssets);
130:                Enumeration subtasks = wf.getTasks();
131:                while (subtasks.hasMoreElements()) {
132:                    Task task = (Task) subtasks.nextElement();
133:                    Asset consumer = task.getDirectObject();
134:                    if (remTaskHash.contains(consumer)) {
135:                        wf.removeTask(task);
136:                        dfPlugin.publishRemove(task);
137:                    }
138:                }
139:                dfPlugin.publishChange(expansion);
140:            }
141:
142:            protected void createAndPublishExpansion(Task parent,
143:                    Collection subtasks) {
144:                Iterator subtasksIT = subtasks.iterator();
145:                while (subtasksIT.hasNext()) {
146:                    dfPlugin.publishAdd(subtasksIT.next());
147:                }
148:                Workflow wf = buildWorkflow(parent, subtasks);
149:                Expansion expansion = getPlanningFactory().createExpansion(
150:                        parent.getPlan(), parent, wf, null);
151:                dfPlugin.publishAdd(expansion);
152:                if (logger.isDebugEnabled()) {
153:                    logger.debug("Agent: " + dfPlugin.getClusterId().toString()
154:                            + "DetReqExp type[" + dfPlugin.getSupplyType()
155:                            + "] " + " Expanding DetReq: " + parent.getUID()
156:                            + " Expansion is: " + expansion.getUID());
157:                }
158:            }
159:
160:            protected void addToAndPublishExpansion(Task parent,
161:                    Collection subtasks) {
162:                Expansion expansion = (Expansion) parent.getPlanElement();
163:                NewWorkflow wf = (NewWorkflow) expansion.getWorkflow();
164:                Iterator subtasksIT = subtasks.iterator();
165:                while (subtasksIT.hasNext()) {
166:                    Task task = (Task) subtasksIT.next();
167:                    wf.addTask(task);
168:                    ((NewTask) task).setWorkflow(wf);
169:                    dfPlugin.publishAdd(task);
170:                }
171:                dfPlugin.publishChange(expansion);
172:            }
173:
174:            protected NewTask createGPTask(Task parentTask, Asset consumer) {
175:                Vector prefs = new Vector();
176:
177:                PrepositionalPhrase prepPhrase = newPrepositionalPhrase(
178:                        Constants.Preposition.OFTYPE, dfPlugin.getSupplyType());
179:
180:                prefs.addElement(createStartTimePref(parentTask));
181:
182:                NewTask newTask = getPlanningFactory().newTask();
183:
184:                newTask.setParentTask(parentTask);
185:                newTask.setPlan(parentTask.getPlan());
186:
187:                //MWD Remove
188:                //newTask.setPrepositionalPhrases(parentTask.getPrepositionalPhrases());
189:                //newTask = addPrepositionalPhrase(newTask, prepPhrase);
190:
191:                newTask.setPrepositionalPhrases(prepPhrase);
192:
193:                newTask.setDirectObject(consumer);
194:                newTask.setVerb(Verb.get(Constants.Verb.GENERATEPROJECTIONS));
195:
196:                newTask.setPreferences(prefs.elements());
197:
198:                return newTask;
199:            }
200:
201:            /**
202:             *  Build a workflow from a vector of tasks.
203:             * @param parent parent task of workflow
204:             * @param subtasks workflow tasks
205:             * @return Workflow
206:             **/
207:            public Workflow buildWorkflow(Task parent, Collection subtasks) {
208:                NewWorkflow wf = getPlanningFactory().newWorkflow();
209:                wf.setParentTask(parent);
210:                wf.setIsPropagatingToSubtasks(true);
211:                NewTask t;
212:                Iterator subtasksIT = subtasks.iterator();
213:                while (subtasksIT.hasNext()) {
214:                    t = (NewTask) subtasksIT.next();
215:                    t.setWorkflow(wf);
216:                    wf.addTask(t);
217:                }
218:                return wf;
219:            }
220:
221:            protected void disposeOfTask(Task task) {
222:                AspectValue avs[] = new AspectValue[1];
223:                avs[0] = AspectValue.newAspectValue(
224:                        AlpineAspectType.DEMANDRATE, CountRate
225:                                .newEachesPerDay(0.0));
226:                AllocationResult dispAR = getPlanningFactory()
227:                        .newAllocationResult(Constants.Confidence.OBSERVED,
228:                                true, avs);
229:                Disposition disposition = getPlanningFactory()
230:                        .createDisposition(task.getPlan(), task, dispAR);
231:                dfPlugin.publishAdd(disposition);
232:            }
233:
234:            protected long getStartTimePref(Task task) {
235:                synchronized (task) {
236:                    Enumeration taskPrefs = task.getPreferences();
237:                    while (taskPrefs.hasMoreElements()) {
238:                        Preference aPref = (Preference) taskPrefs.nextElement();
239:                        if (aPref.getAspectType() == AspectType.START_TIME) {
240:                            ScoringFunction sf = aPref.getScoringFunction();
241:                            return (long) sf.getBest().getValue();
242:                        }
243:                    }
244:                }
245:                return 0L;
246:            }
247:
248:            protected Preference createStartTimePref(Task parentTask) {
249:                long startTime = getStartTimePref(parentTask);
250:                AspectValue av = AspectValue.newAspectValue(
251:                        AspectType.START_TIME, startTime);
252:                ScoringFunction score = ScoringFunction
253:                        .createNearOrAbove(av, 0);
254:                return getPlanningFactory().newPreference(
255:                        AspectType.START_TIME, score);
256:            }
257:
258:            public PrepositionalPhrase newPrepositionalPhrase(
259:                    String preposition, Object io) {
260:                NewPrepositionalPhrase pp = getPlanningFactory()
261:                        .newPrepositionalPhrase();
262:                pp.setPreposition(preposition);
263:                pp.setIndirectObject(io);
264:                return pp;
265:            }
266:
267:            public void updateAllocationResults(Collection planElements) {
268:                Iterator peIt = planElements.iterator();
269:                while (peIt.hasNext()) {
270:                    PlanElement pe = (PlanElement) peIt.next();
271:                    if (PluginHelper.updatePlanElement(pe)) {
272:                        dfPlugin.publishChange(pe);
273:                    }
274:                }
275:            }
276:
277:            /**
278:             *
279:             * MWD remove
280:             *
281:             public static NewTask addPrepositionalPhrase(NewTask task, PrepositionalPhrase pp) {
282:             Enumeration enum = task.getPrepositionalPhrases();
283:             Vector phrases = new Vector();
284:             while (enum.hasMoreElements()) {
285:             phrases.addElement(enum.nextElement());
286:             }
287:             phrases.addElement(pp);
288:             task.setPrepositionalPhrases(phrases.elements());
289:             return task;
290:             }
291:
292:             **/
293:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.