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.trans;
028:
029: import org.cougaar.core.agent.service.alarm.Alarm;
030: import org.cougaar.core.blackboard.IncrementalSubscription;
031: import org.cougaar.core.component.ServiceRevokedEvent;
032: import org.cougaar.core.component.ServiceRevokedListener;
033: import org.cougaar.core.plugin.ComponentPlugin;
034: import org.cougaar.core.service.BlackboardService;
035: import org.cougaar.core.service.DomainService;
036: import org.cougaar.core.service.LoggingService;
037: import org.cougaar.logistics.plugin.inventory.*;
038: import org.cougaar.logistics.plugin.utils.ScheduleUtils;
039: import org.cougaar.logistics.plugin.utils.OrgActivityPred;
040: import org.cougaar.planning.ldm.PlanningFactory;
041: import org.cougaar.planning.ldm.asset.Asset;
042: import org.cougaar.planning.ldm.asset.AggregateAsset;
043: import org.cougaar.planning.ldm.plan.Task;
044: import org.cougaar.logistics.ldm.Constants;
045: import org.cougaar.glm.ldm.oplan.OrgActivity;
046: import org.cougaar.glm.ldm.asset.Organization;
047: import org.cougaar.util.UnaryPredicate;
048: import org.cougaar.logistics.plugin.packer.GenericPlugin;
049:
050: import java.lang.reflect.Constructor;
051: import java.util.*;
052:
053: /** The Level2TranslatorPlugin generates demand during execution.
054: **/
055:
056: public class Level2TranslatorPlugin extends ComponentPlugin implements
057: UtilsProvider {
058: private DomainService domainService;
059: private LoggingService logger;
060: private TaskUtils taskUtils;
061: private TimeUtils timeUtils;
062: private AssetUtils AssetUtils;
063: private ScheduleUtils scheduleUtils;
064: private HashMap pluginParams;
065:
066: private Level2Disposer disposer;
067: private Level2Expander expander;
068:
069: private String supplyType;
070: private boolean doTranslate = true;
071:
072: public final String SUPPLY_TYPE = "SUPPLY_TYPE";
073: public final String TRANSLATOR_ON = "TRANSLATOR_ON";
074:
075: private Organization myOrganization;
076: private String myOrgName;
077:
078: public void load() {
079: super .load();
080: logger = getLoggingService(this );
081: timeUtils = new TimeUtils(this );
082: AssetUtils = new AssetUtils(this );
083: taskUtils = new TaskUtils(this );
084: scheduleUtils = new ScheduleUtils(this );
085: pluginParams = readParameters();
086:
087: domainService = (DomainService) getServiceBroker().getService(
088: this , DomainService.class,
089: new ServiceRevokedListener() {
090: public void serviceRevoked(ServiceRevokedEvent re) {
091: if (DomainService.class.equals(re.getService()))
092: domainService = null;
093: }
094: });
095:
096: logger = getLoggingService(this );
097:
098: disposer = getNewLevel2DisposerModule();
099: expander = getNewLevel2ExpanderModule();
100:
101: }
102:
103: public void unload() {
104: super .unload();
105: if (domainService != null) {
106: getServiceBroker().releaseService(this ,
107: DomainService.class, domainService);
108: }
109: }
110:
111: /** Subscription for the Organization(s) in which this plugin resides **/
112: private IncrementalSubscription selfOrganizations;
113:
114: private IncrementalSubscription supplyTaskSubscription;
115: private IncrementalSubscription projectionTaskSubscription;
116: private IncrementalSubscription level2TaskSubscription;
117:
118: public void setupSubscriptions() {
119:
120: selfOrganizations = (IncrementalSubscription) blackboard
121: .subscribe(orgsPredicate);
122:
123: }
124:
125: private static class SupplyTaskPredicate implements UnaryPredicate {
126: String supplyType;
127: String orgName;
128: TaskUtils taskUtils;
129:
130: public SupplyTaskPredicate(String type, String orgname,
131: TaskUtils aTaskUtils) {
132: supplyType = type;
133: orgName = orgname;
134: taskUtils = aTaskUtils;
135: }
136:
137: public boolean execute(Object o) {
138: if (o instanceof Task) {
139: Task task = (Task) o;
140: if (task.getVerb().equals(Constants.Verb.SUPPLY)) {
141: if (taskUtils
142: .isDirectObjectOfType(task, supplyType)) {
143: if (!taskUtils.isInternal(task)) {
144: if (!taskUtils
145: .isMyRefillTask(task, orgName)) {
146: return true;
147: }
148: }
149: }
150: }
151: }
152: return false;
153: }
154: }
155:
156: private static class ProjectionTaskPredicate implements
157: UnaryPredicate {
158: String supplyType;
159: String orgName;
160: TaskUtils taskUtils;
161:
162: public ProjectionTaskPredicate(String type, String orgname,
163: TaskUtils aTaskUtils) {
164: supplyType = type;
165: taskUtils = aTaskUtils;
166: orgName = orgname;
167: }
168:
169: public boolean execute(Object o) {
170: if (o instanceof Task) {
171: Task task = (Task) o;
172: if (task.getVerb().equals(Constants.Verb.PROJECTSUPPLY)) {
173: if (taskUtils
174: .isDirectObjectOfType(task, supplyType)) {
175: if (!taskUtils.isMyRefillTask(task, orgName)) {
176: return !(taskUtils.isLevel2(task));
177: }
178: }
179: }
180: }
181: return false;
182: }
183: }
184:
185: private static class Level2TaskPredicate implements UnaryPredicate {
186: String supplyType;
187: TaskUtils taskUtils;
188:
189: public Level2TaskPredicate(String type, TaskUtils aTaskUtils) {
190: supplyType = type;
191: taskUtils = aTaskUtils;
192: }
193:
194: public boolean execute(Object o) {
195: if (o instanceof Task) {
196: Task task = (Task) o;
197: if (task.getVerb().equals(Constants.Verb.PROJECTSUPPLY)) {
198: if (taskUtils
199: .isDirectObjectOfType(task, supplyType)) {
200: if (taskUtils.isLevel2(task)) {
201: return (!(taskUtils
202: .isReadyForTransport(task)));
203: }
204: }
205: }
206: }
207: return false;
208: }
209: }
210:
211: private static UnaryPredicate orgsPredicate = new UnaryPredicate() {
212: public boolean execute(Object o) {
213: if (o instanceof Organization) {
214: return ((Organization) o).isSelf();
215: }
216: return false;
217: }
218: };
219:
220: public TaskUtils getTaskUtils() {
221: return taskUtils;
222: }
223:
224: public TimeUtils getTimeUtils() {
225: return timeUtils;
226: }
227:
228: public AssetUtils getAssetUtils() {
229: return AssetUtils;
230: }
231:
232: public ScheduleUtils getScheduleUtils() {
233: return scheduleUtils;
234: }
235:
236: public String getSupplyType() {
237: return supplyType;
238: }
239:
240: private Organization getMyOrganization(Enumeration orgs) {
241: Organization myOrg = null;
242: // look for this organization
243: if (orgs.hasMoreElements()) {
244: myOrg = (Organization) orgs.nextElement();
245: }
246: return myOrg;
247: }
248:
249: public Organization getMyOrganization() {
250: return myOrganization;
251: }
252:
253: public String getOrgName() {
254: if ((myOrgName == null) && (getMyOrganization() != null)) {
255: myOrgName = getMyOrganization().getItemIdentificationPG()
256: .getItemIdentification();
257: }
258: return myOrgName;
259: }
260:
261: public long getCurrentTimeMillis() {
262: return currentTimeMillis();
263: }
264:
265: public boolean publishAdd(Object o) {
266: getBlackboardService().publishAdd(o);
267: return true;
268: }
269:
270: public boolean publishChange(Object o) {
271: getBlackboardService().publishChange(o);
272: return true;
273: }
274:
275: public boolean publishRemove(Object o) {
276: getBlackboardService().publishRemove(o);
277: return true;
278: }
279:
280: public PlanningFactory getPlanningFactory() {
281: PlanningFactory rootFactory = null;
282: if (domainService != null) {
283: rootFactory = (PlanningFactory) domainService
284: .getFactory("planning");
285: }
286: return rootFactory;
287: }
288:
289: public LoggingService getLoggingService(Object requestor) {
290: return (LoggingService) getServiceBroker().getService(
291: requestor, LoggingService.class, null);
292: }
293:
294: protected void execute() {
295:
296: if (myOrganization == null) {
297: myOrganization = getMyOrganization(selfOrganizations
298: .elements());
299: if (myOrganization != null) {
300: level2TaskSubscription = (IncrementalSubscription) blackboard
301: .subscribe(new Level2TaskPredicate(supplyType,
302: taskUtils));
303: supplyTaskSubscription = (IncrementalSubscription) blackboard
304: .subscribe(new SupplyTaskPredicate(supplyType,
305: getOrgName(), taskUtils));
306: projectionTaskSubscription = (IncrementalSubscription) blackboard
307: .subscribe(new ProjectionTaskPredicate(
308: supplyType, getOrgName(), taskUtils));
309: if (logger.isDebugEnabled()) {
310: logger
311: .debug("Level2TranslatorPlugin just loaded level 2 subscription");
312: }
313: } else {
314: if (logger.isInfoEnabled()) {
315: logger.info("\n Level2TranslatorPlugin "
316: + supplyType
317: + " not ready to process tasks yet."
318: + " my org is: " + myOrganization
319: + " supply type is " + supplyType);
320: }
321: return;
322: }
323: }
324:
325: if ((level2TaskSubscription != null)
326: && (!level2TaskSubscription.getAddedCollection()
327: .isEmpty())) {
328:
329: if (doTranslate) {
330: translateTasks(level2TaskSubscription
331: .getAddedCollection(),
332: projectionTaskSubscription.getCollection(),
333: supplyTaskSubscription.getCollection());
334: } else {
335: //logger.shout("Level2TranslatorPlugin got Level 2 task - now disposing");
336: disposer
337: .disposeAndRemoveExpansion(level2TaskSubscription
338: .getAddedCollection());
339: }
340: } else if ((level2TaskSubscription != null)
341: && ((level2TaskSubscription.hasChanged())
342: || (projectionTaskSubscription.hasChanged()) || (supplyTaskSubscription
343: .hasChanged()))) {
344: if (doTranslate) {
345: translateTasks(level2TaskSubscription.getCollection(),
346: projectionTaskSubscription.getCollection(),
347: supplyTaskSubscription.getCollection());
348: }
349: }
350:
351: }
352:
353: private void translateTasks(Collection level2Tasks,
354: Collection level6Tasks, Collection supplyTasks) {
355: Collection doneL2Tasks = expander.translateLevel2Tasks(
356: level2Tasks, level6Tasks, supplyTasks);
357: if (!doneL2Tasks.isEmpty()) {
358: if (logger.isDebugEnabled()) {
359: logger
360: .debug("Level2TranslatorPlugin:There are done tasks! disposing.");
361: }
362: disposer.disposeAndRemoveExpansion(doneL2Tasks);
363: }
364:
365: }
366:
367: private Level2Disposer getNewLevel2DisposerModule() {
368: return new Level2Disposer(this );
369: }
370:
371: private Level2Expander getNewLevel2ExpanderModule() {
372: return new Level2Expander(this );
373: }
374:
375: /**
376: Read the Plugin parameters(Accepts key/value pairs)
377: Initializes supplyType and inventoryFile
378: **/
379: private HashMap readParameters() {
380: final String errorString = "Level2TranslatorPlugin requires 1 parameter, SUPPLY_TYPE and there is an optional parameter TRANSLATOR_ON with a boolean value (default is true). As in Level2TranslatorPlugin(SUPPLY_TYPE=Ammunition,TRANSLATOR_ON=false)";
381: Collection p = getParameters();
382:
383: if (p.isEmpty()) {
384: if (logger.isErrorEnabled()) {
385: logger.error(errorString);
386: }
387: return null;
388: }
389: HashMap map = new HashMap();
390: int idx;
391:
392: for (Iterator i = p.iterator(); i.hasNext();) {
393: String s = (String) i.next();
394: if ((idx = s.indexOf('=')) != -1) {
395: String key = new String(s.substring(0, idx));
396: String value = new String(s.substring(idx + 1, s
397: .length()));
398: map.put(key.trim(), value.trim());
399: }
400: }
401: supplyType = (String) map.get(SUPPLY_TYPE);
402: String translateOn = (String) map.get(TRANSLATOR_ON);
403:
404: if (translateOn != null) {
405: translateOn = translateOn.trim().toLowerCase();
406: doTranslate = (translateOn.equals("true"));
407: }
408:
409: if ((supplyType == null) && logger.isErrorEnabled()) {
410: logger.error(errorString);
411: }
412: return map;
413: }
414: }
|