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: package org.cougaar.logistics.plugin.manager;
027:
028: import java.util.*;
029:
030: import org.cougaar.core.blackboard.IncrementalSubscription;
031: import org.cougaar.core.component.ServiceBroker;
032: import org.cougaar.core.component.ServiceRevokedListener;
033: import org.cougaar.core.component.ServiceRevokedEvent;
034: import org.cougaar.planning.plugin.legacy.SimplePlugin;
035: import org.cougaar.core.service.LoggingService;
036: import org.cougaar.core.service.UIDService;
037: import org.cougaar.core.service.community.CommunityService;
038:
039: import org.cougaar.multicast.AttributeBasedAddress;
040:
041: import org.cougaar.util.UnaryPredicate;
042:
043: /**
044: * Test implementation to verify that ABAMessages are reaching the
045: * Manager agent. Logs all added/modified/removed LoadIndicators
046: */
047: public class LogisticsManagerPlugin extends SimplePlugin {
048: private IncrementalSubscription myLoadIndicators;
049: private IncrementalSubscription myFallingBehindPolicies;
050:
051: private UIDService myUIDService;
052: private LoggingService myLoggingService;
053:
054: private double myFallingBehindValue = UNINITIALIZED;
055:
056: Collection myCommunitiesToManage;
057:
058: private static double UNINITIALIZED = -1.0;
059:
060: // From FallingBehind values in the playbook
061: private static double NORMAL = .5;
062: private static double MODERATE = 1.0;
063: private static double SEVERE = 2.0;
064: private static double MAX_FALLING_BEHIND_VALUE = SEVERE;
065: private static double DEFAULT_FALLING_BEHIND_VALUE = NORMAL;
066:
067: private static UnaryPredicate myLoadIndicatorsPred = new UnaryPredicate() {
068: public boolean execute(Object o) {
069: if (o instanceof LoadIndicator) {
070: return true;
071: } else {
072: return false;
073: }
074: }
075: };
076:
077: private static UnaryPredicate myFallingBehindPoliciesPred = new UnaryPredicate() {
078: public boolean execute(Object o) {
079: if (o instanceof FallingBehindPolicy) {
080: return true;
081: } else {
082: return false;
083: }
084: }
085: };
086:
087: protected void setupSubscriptions() {
088: myUIDService = (UIDService) getBindingSite().getServiceBroker()
089: .getService(this , UIDService.class, null);
090:
091: myLoggingService = (LoggingService) getBindingSite()
092: .getServiceBroker().getService(this ,
093: LoggingService.class, null);
094:
095: myLoadIndicators = (IncrementalSubscription) subscribe(myLoadIndicatorsPred);
096: myFallingBehindPolicies = (IncrementalSubscription) subscribe(myFallingBehindPoliciesPred);
097:
098: // Find name of community to monitor
099: String filter = "(CommunityManager="
100: + getAgentIdentifier().toString() + ")";
101: myCommunitiesToManage = getCommunityService()
102: .listParentCommunities(null, filter, null);
103:
104: if (myCommunitiesToManage.isEmpty()) {
105: myLoggingService.warn(getAgentIdentifier()
106: + " is not a CommunityManager."
107: + " Plugin will not be receiving LoadIndicators.");
108: }
109:
110: if (didRehydrate()) {
111: myFallingBehindValue = communityFallingBehindValue();
112: }
113:
114: if (myFallingBehindValue == UNINITIALIZED) {
115: myFallingBehindValue = DEFAULT_FALLING_BEHIND_VALUE;
116: }
117:
118: if (myFallingBehindPolicies.size() == 0) {
119: createFallingBehindPolicies();
120: }
121:
122: }
123:
124: public void execute() {
125:
126: if (myLoggingService.isDebugEnabled()) {
127: for (Iterator iterator = myLoadIndicators
128: .getAddedCollection().iterator(); iterator
129: .hasNext();) {
130: myLoggingService.debug("New LoadIndicator: "
131: + ((LoadIndicator) iterator.next()).toString());
132: }
133:
134: for (Iterator iterator = myLoadIndicators
135: .getChangedCollection().iterator(); iterator
136: .hasNext();) {
137: myLoggingService.debug("Modified LoadIndicator: "
138: + ((LoadIndicator) iterator.next()).toString());
139: }
140:
141: for (Iterator iterator = myLoadIndicators
142: .getRemovedCollection().iterator(); iterator
143: .hasNext();) {
144: myLoggingService.debug("Removed LoadIndicator: "
145: + ((LoadIndicator) iterator.next()).toString());
146: }
147: }
148:
149: if (myLoadIndicators.hasChanged()) {
150: double currentFallingBehindValue = communityFallingBehindValue();
151:
152: if (myFallingBehindValue != currentFallingBehindValue) {
153: myFallingBehindValue = currentFallingBehindValue;
154: adjustFallingBehindPolicy();
155:
156: }
157: }
158: }
159:
160: protected double getFallingBehindValue() {
161: return myFallingBehindValue;
162: }
163:
164: private double communityFallingBehindValue() {
165: double FallingBehindValue = -1;
166:
167: for (Iterator iterator = myLoadIndicators.getCollection()
168: .iterator(); iterator.hasNext();) {
169: double nextFallingBehindValue = loadStatusToFallingBehindValue(((LoadIndicator) iterator
170: .next()));
171:
172: FallingBehindValue = Math.max(FallingBehindValue,
173: nextFallingBehindValue);
174:
175: if (FallingBehindValue == MAX_FALLING_BEHIND_VALUE) {
176: // Exit loop because we can't get any worse
177: break;
178: }
179: }
180:
181: return FallingBehindValue;
182: }
183:
184: private double loadStatusToFallingBehindValue(
185: LoadIndicator loadIndicator) {
186: String loadStatus = loadIndicator.getLoadStatus();
187:
188: if (loadStatus.equals(LoadIndicator.NORMAL_LOAD)) {
189: return NORMAL;
190: } else if (loadStatus.equals(LoadIndicator.MODERATE_LOAD)) {
191: return MODERATE;
192: } else if (loadStatus.equals(LoadIndicator.SEVERE_LOAD)) {
193: return SEVERE;
194: } else {
195: myLoggingService.warn("Unrecognized load status: "
196: + loadStatus);
197: return UNINITIALIZED;
198: }
199: }
200:
201: private void adjustFallingBehindPolicy() {
202: if (myLoggingService.isDebugEnabled()) {
203: myLoggingService
204: .debug("Modifying FallingBehindPolicy: new falling behind = "
205: + getFallingBehindValue());
206: }
207:
208: FallingBehindPolicy fallingBehindPolicy;
209:
210: for (Iterator iterator = myFallingBehindPolicies.iterator(); iterator
211: .hasNext();) {
212: fallingBehindPolicy = (FallingBehindPolicy) iterator.next();
213: fallingBehindPolicy
214: .setFallingBehindValue(getFallingBehindValue());
215: publishChange(fallingBehindPolicy);
216: }
217:
218: }
219:
220: /**
221: * Gets reference to CommunityService.
222: */
223: private CommunityService getCommunityService() {
224: ServiceBroker sb = getBindingSite().getServiceBroker();
225: if (sb.hasService(CommunityService.class)) {
226: return (CommunityService) sb.getService(this ,
227: CommunityService.class,
228: new ServiceRevokedListener() {
229: public void serviceRevoked(
230: ServiceRevokedEvent re) {
231: }
232: });
233: } else {
234: myLoggingService.error("CommunityService not available");
235: return null;
236: }
237: }
238:
239: private void createFallingBehindPolicies() {
240: // Publish default falling behind policy
241: for (Iterator iterator = myCommunitiesToManage.iterator(); iterator
242: .hasNext();) {
243: FallingBehindPolicy fallingBehindPolicy = new FallingBehindPolicy(
244: getFallingBehindValue());
245: fallingBehindPolicy.setUID(myUIDService.nextUID());
246: String community = (String) iterator.next();
247: fallingBehindPolicy.setTarget(AttributeBasedAddress
248: .getAttributeBasedAddress(community, "Role",
249: "Member"));
250: publishAdd(fallingBehindPolicy);
251: }
252: }
253:
254: }
|