Source Code Cross Referenced for SimpleSinkAllocatorPlugin.java in  » Science » Cougaar12_4 » org » cougaar » planning » examples » 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.planning.examples 
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.planning.examples;
028:
029:        import org.cougaar.core.blackboard.IncrementalSubscription;
030:        import org.cougaar.planning.ldm.plan.*;
031:        import org.cougaar.planning.ldm.asset.Asset;
032:        import org.cougaar.planning.plugin.legacy.SimplePlugin;
033:        import org.cougaar.util.UnaryPredicate;
034:        import java.util.Enumeration;
035:        import java.util.Vector;
036:        import java.util.Date;
037:
038:        // A simple plugin to respond with any task with an allocation result
039:        // satisfying the preferences.
040:        public class SimpleSinkAllocatorPlugin extends SimplePlugin {
041:            // Subscription for all tasks
042:            private IncrementalSubscription allTasks;
043:            private UnaryPredicate allTasksPredicate = new UnaryPredicate() {
044:                public boolean execute(Object o) {
045:                    return o instanceof  Task;
046:                }
047:            };
048:
049:            // Plan Element set for all tasks predicate
050:            private PlanElementSet allTaskPEs;
051:
052:            // Single asset to which to allocate all tasks
053:            private Asset sink_asset;
054:
055:            // Subscribe to all tasks, and create a dummy asset to which to allocate
056:            // everything
057:            public void setupSubscriptions() {
058:
059:                // Subscribe for all tasks
060:                allTasks = (IncrementalSubscription) subscribe(
061:                        allTasksPredicate, allTaskPEs);
062:
063:                // Create an abstract asset to which to allocate everything
064:                sink_asset = theLDMF.createPrototype("AbstractAsset",
065:                        "SinkAsset");
066:                publishAdd(sink_asset);
067:            }
068:
069:            // Allocate every new task, and reallocate every changed task
070:            public void execute() {
071:
072:                // Allocate every new task
073:                for (Enumeration e_added = allTasks.getAddedList(); e_added
074:                        .hasMoreElements();) {
075:                    Task task = (Task) e_added.nextElement();
076:
077:                    // Compute allocation result indicating success within preferences
078:                    AllocationResult result = computeAllocationResult(task);
079:
080:                    // Generate allocation
081:                    Allocation allocation = theLDMF.createAllocation(task
082:                            .getPlan(), task, sink_asset, result,
083:                            Role.AVAILABLE);
084:
085:                    // publish new allocation
086:                    publishAdd(allocation);
087:                }
088:
089:                // Change allocation result on every new task
090:                for (Enumeration e_changed = allTasks.getChangedList(); e_changed
091:                        .hasMoreElements();) {
092:                    Task task = (Task) e_changed.nextElement();
093:                    // Compute new allocation result for changed task
094:                    AllocationResult result = computeAllocationResult(task);
095:
096:                    // Find PE (allocation) for task (don't use getPlanElement)
097:                    Allocation allocation = (Allocation) allTaskPEs
098:                            .findPlanElement(task);
099:
100:                    if (allocation != null) {
101:
102:                        // Set the new estimated result for the allocation based on changed
103:                        // preferences
104:                        allocation.setEstimatedResult(result);
105:                        publishChange(allocation);
106:
107:                    } else {
108:                        System.out
109:                                .println("Error! Should have a plan element for a changed task allocation!");
110:                    }
111:                }
112:            }
113:
114:            private AllocationResult computeAllocationResult(Task task) {
115:                System.out.print("[" + getMessageAddress()
116:                        + "] allocating task " + task.getUID() + "["
117:                        + task.getVerb() + "] :");
118:
119:                // Grab the preferences and grab the aspect types and results
120:                // Don't know how big the list is, so store in an array and then convert
121:                Enumeration all_preferences = task.getPreferences();
122:                Vector all_aspect_types = new Vector();
123:                Vector all_aspect_results = new Vector();
124:                while (all_preferences.hasMoreElements()) {
125:                    // Grab each preference and save aspect type and result
126:                    Preference preference = (Preference) all_preferences
127:                            .nextElement();
128:                    int aspect_type = preference.getAspectType();
129:                    double preference_result = computePreferenceResult(preference);
130:                    String preference_result_image = Double
131:                            .toString(preference_result);
132:                    if ((aspect_type == AspectType.START_TIME)
133:                            || (aspect_type == AspectType.END_TIME)) {
134:                        preference_result_image = new Date(
135:                                (long) preference_result).toString();
136:                    }
137:                    System.out.print(" [" + aspect_type + "] : "
138:                            + preference_result_image);
139:                    all_aspect_types.addElement(new Integer(aspect_type));
140:                    all_aspect_results
141:                            .addElement(new Double(preference_result));
142:                }
143:                System.out.println("");
144:
145:                // Copy the Integer/Double values 
146:                // into aspect_type(int)/aspect_result(double) arrays
147:                int[] aspect_types = new int[all_aspect_types.size()];
148:                for (int i = 0; i < aspect_types.length; i++) {
149:                    aspect_types[i] = ((Integer) (all_aspect_types.elementAt(i)))
150:                            .intValue();
151:                }
152:
153:                double[] aspect_results = new double[all_aspect_results.size()];
154:                for (int i = 0; i < aspect_results.length; i++) {
155:                    aspect_results[i] = ((Double) (all_aspect_results
156:                            .elementAt(i))).doubleValue();
157:                }
158:
159:                // Compute new allocation result
160:                AllocationResult result = theLDMF.newAllocationResult(1.0, // rating,
161:                        true, // success,
162:                        aspect_types, aspect_results);
163:
164:                // Add in any auxiliary queries
165:                int[] query_types = task.getAuxiliaryQueryTypes();
166:                for (int i = 0; i < query_types.length; i++) {
167:                    int query_type = query_types[i];
168:                    if (query_type >= 0)
169:                        result.addAuxiliaryQueryInfo(query_type,
170:                                "QueryResponse-" + query_type);
171:                }
172:
173:                return result;
174:            }
175:
176:            // Compute preference result for given preference based on optimal point
177:            // in scoring function
178:            private double computePreferenceResult(Preference preference) {
179:                ScoringFunction func = preference.getScoringFunction();
180:                AspectScorePoint point = func.getBest();
181:                return point.getValue();
182:            }
183:
184:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.