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.adaptivity.InterAgentOperatingModePolicy;
031: import org.cougaar.core.mts.MessageAddress;
032: import org.cougaar.core.blackboard.IncrementalSubscription;
033: import org.cougaar.planning.plugin.legacy.SimplePlugin;
034: import org.cougaar.core.service.BlackboardService;
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.planning.ldm.plan.Task;
042: import org.cougaar.planning.ldm.plan.Verb;
043:
044: import org.cougaar.core.util.UID;
045:
046: import org.cougaar.util.UnaryPredicate;
047:
048: import org.cougaar.glm.ldm.oplan.Oplan;
049:
050: /**
051: * Test implementation - generates a LoadIndicator when the Oplan is received,
052: * modifies LoadIndicator when GLS received.
053: *
054: */
055: public class TransportLoadIndicatorTestPlugin extends SimplePlugin {
056: private IncrementalSubscription myLoadIndicatorSubscription;
057: private IncrementalSubscription myTaskSubscription;
058: private IncrementalSubscription myInterAgentOperatingModePolicySubscription;
059:
060: private BlackboardService myBlackboardService;
061: private LoggingService myLoggingService;
062: private UIDService myUIDService;
063:
064: private UnaryPredicate myLoadIndicatorPred = new UnaryPredicate() {
065: public boolean execute(Object o) {
066: if (o instanceof LoadIndicator) {
067: return true;
068: } else {
069: return false;
070: }
071: }
072: };
073:
074: private UnaryPredicate myTaskPred = new UnaryPredicate() {
075: public boolean execute(Object o) {
076: if (o instanceof Task) {
077: Task task = (Task) o;
078: Verb verb = task.getVerb();
079: if (verb
080: .equals(org.cougaar.logistics.ldm.Constants.Verb.Transport)) {
081: return true;
082: }
083: }
084:
085: return false;
086: }
087: };
088:
089: private UnaryPredicate myInterAgentOperatingModePolicyPred = new UnaryPredicate() {
090: public boolean execute(Object o) {
091: if (o instanceof InterAgentOperatingModePolicy) {
092: return true;
093: } else {
094: return false;
095: }
096: }
097: };
098:
099: protected void setupSubscriptions() {
100: myLoadIndicatorSubscription = (IncrementalSubscription) subscribe(myLoadIndicatorPred);
101: myTaskSubscription = (IncrementalSubscription) subscribe(myTaskPred);
102: myInterAgentOperatingModePolicySubscription = (IncrementalSubscription) subscribe(myInterAgentOperatingModePolicyPred);
103:
104: myBlackboardService = (BlackboardService) getBindingSite()
105: .getServiceBroker().getService(this ,
106: BlackboardService.class, null);
107:
108: myUIDService = (UIDService) getBindingSite().getServiceBroker()
109: .getService(this , UIDService.class, null);
110:
111: myLoggingService = (LoggingService) getBindingSite()
112: .getServiceBroker().getService(this ,
113: LoggingService.class, null);
114:
115: }
116:
117: public void execute() {
118: if ((myTaskSubscription.getAddedCollection().size() > 0)
119: && (myLoadIndicatorSubscription.size() == 0)) {
120: CommunityService communityService = (CommunityService) getBindingSite()
121: .getServiceBroker().getService(this ,
122: CommunityService.class, null);
123:
124: if (communityService == null) {
125: myLoggingService
126: .error("CommunityService not available.");
127: return;
128: }
129:
130: Collection alCommunities = communityService
131: .listParentCommunities(getAgentIdentifier()
132: .toString(),
133: "(CommunityType=AdaptiveLogistics)", null);
134:
135: if (alCommunities.size() == 0) {
136: myLoggingService
137: .warn(getAgentIdentifier().toString()
138: + " does not belong to an AdaptiveLogistics community.");
139: }
140:
141: for (Iterator iterator = alCommunities.iterator(); iterator
142: .hasNext();) {
143: String community = (String) iterator.next();
144: LoadIndicator loadIndicator = new LoadIndicator(this
145: .getClass(), getAgentIdentifier().toString(),
146: myUIDService.nextUID(),
147: LoadIndicator.MODERATE_LOAD);
148: loadIndicator.addTarget(AttributeBasedAddress
149: .getAttributeBasedAddress(community, "Role",
150: "AdaptiveLogisticsManager"));
151: if (myLoggingService.isDebugEnabled()) {
152: myLoggingService.debug(getAgentIdentifier()
153: .toString()
154: + ": adding LoadIndicator to be sent to "
155: + loadIndicator.getTargets());
156: }
157: publishAdd(loadIndicator);
158: }
159: }
160:
161: if (myInterAgentOperatingModePolicySubscription
162: .getChangedCollection().size() > 0) {
163: // Increase load to SEVERE
164: for (Iterator iterator = myLoadIndicatorSubscription
165: .getCollection().iterator(); iterator.hasNext();) {
166: LoadIndicator loadIndicator = (LoadIndicator) iterator
167: .next();
168: if (!loadIndicator.getLoadStatus().equals(
169: LoadIndicator.SEVERE_LOAD)) {
170: loadIndicator
171: .setLoadStatus(LoadIndicator.SEVERE_LOAD);
172: if (myLoggingService.isDebugEnabled()) {
173: myLoggingService
174: .debug(getAgentIdentifier().toString()
175: + ": changing load status to SEVERE_LOAD for LoadIndicator to "
176: + loadIndicator.getTargets());
177: }
178: publishChange(loadIndicator);
179: }
180: }
181: }
182:
183: if (myLoggingService.isDebugEnabled()) {
184: for (Iterator iterator = myInterAgentOperatingModePolicySubscription
185: .getAddedCollection().iterator(); iterator
186: .hasNext();) {
187: myLoggingService.debug(getAgentIdentifier().toString()
188: + ": new InterAgentOperatingModePolicy: "
189: + ((InterAgentOperatingModePolicy) iterator
190: .next()).toString());
191:
192: }
193:
194: for (Iterator iterator = myInterAgentOperatingModePolicySubscription
195: .getChangedCollection().iterator(); iterator
196: .hasNext();) {
197: myLoggingService.debug(getAgentIdentifier().toString()
198: + ": modified InterAgentOperatingModePolicy: "
199: + ((InterAgentOperatingModePolicy) iterator
200: .next()).toString());
201: }
202:
203: for (Iterator iterator = myInterAgentOperatingModePolicySubscription
204: .getRemovedCollection().iterator(); iterator
205: .hasNext();) {
206: myLoggingService.debug(getAgentIdentifier().toString()
207: + ": removed InterAgentOperatingModePolicy: "
208: + ((InterAgentOperatingModePolicy) iterator
209: .next()).toString());
210:
211: }
212: }
213: }
214: }
|