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