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.lib.callback;
028:
029: import java.util.Enumeration;
030:
031: import org.cougaar.planning.ldm.plan.Expansion;
032: import org.cougaar.planning.ldm.plan.Task;
033: import org.cougaar.planning.ldm.plan.Workflow;
034: import org.cougaar.util.UnaryPredicate;
035: import org.cougaar.util.log.Logger;
036:
037: /**
038: * Filters for expansions with workflows, where the tasks
039: * meet the test of isInteresting.
040: *
041: * The reaction to a new workflow is simpler than
042: * UTILSingleTaskWorkflowCallback. This is better
043: * for threaded allocators, where we can't make assumptions
044: * about how the tasks will be handled, like we can with
045: * a one at a time model.
046: */
047:
048: public class UTILInclusiveWorkflowCallback extends
049: UTILFilterCallbackAdapter {
050: public UTILInclusiveWorkflowCallback(UTILGenericListener listener,
051: Logger logger) {
052: super (listener, logger);
053: }
054:
055: /**
056: * Filters for expansions and then examines the tasks
057: * within the workflow for any tasks that don't have
058: * associated plan elements. These are then tested
059: * against the plugin-specific interestingTask test.
060: *
061: * set logger.isDebugEnabled() to true if you want to see logger.info on every
062: * handled expansion. I.e. which tasks have been
063: * allocated and which failed to allocate.
064: * (Previously handled expansions are ignored by the
065: * predicate.)
066: *
067: * @return annonymous UnaryPredicate inner class
068: */
069: protected UnaryPredicate getPredicate() {
070: return new UnaryPredicate() {
071: public boolean execute(Object o) {
072: if (o instanceof Expansion) {
073: UTILGenericListener genericListener = (UTILGenericListener) myListener;
074: Workflow wf = ((Expansion) o).getWorkflow();
075: if (wf == null)
076: return false;
077: Enumeration e = wf.getTasks();
078:
079: while (e.hasMoreElements()) {
080: Task subtask = (Task) e.nextElement();
081: if (genericListener.interestingTask(subtask)) {
082: return true;
083: }
084: }
085: }
086: return false;
087: }
088: };
089: }
090:
091: /**
092: * Tells listener of new expansions
093: *
094: * set logger.isDebugEnabled() to true if you want to see what gets
095: * ignored by the callback. (Changed and removed
096: * expansions are ignored.)
097: *
098: * Again, this can make you feel better,
099: * since the interestingTask test
100: * is made on these ignored expansions too.
101: *
102: * (Since sometimes calls to interestingTask are
103: * followed by the listener doing something, but in
104: * these cases, nothing happens despite the
105: * listener/plugin being "interested" in the task.)
106: */
107: public void reactToChangedFilter() {
108: Enumeration newExps = mySub.getAddedList();
109: UTILGenericListener genericListener = (UTILGenericListener) myListener;
110:
111: while (newExps.hasMoreElements()) {
112: Expansion exp = (Expansion) newExps.nextElement();
113: Workflow wf = exp.getWorkflow();
114: if (wf != null) {
115: Enumeration e = wf.getTasks();
116: while (e.hasMoreElements()) {
117: Task t = (Task) e.nextElement();
118:
119: if (genericListener.interestingTask(t)) {
120: genericListener.handleTask(t);
121: }
122: }
123: }
124: }
125:
126: if (logger.isDebugEnabled()) {
127: if (mySub.getChangedList().hasMoreElements())
128: logger
129: .debug("UTILWorkflowCallback : "
130: + "Expansions were changed. (Ignored by callback)");
131: if (mySub.getRemovedList().hasMoreElements())
132: logger
133: .debug("UTILWorkflowCallback : "
134: + "Expansions were removed. (Ignored by callback)");
135: }
136: logger.info("Prapare to clean up");
137: if (genericListener instanceof UTILTemporaryListener) {
138: logger.info("CLEAN UP");
139: ((UTILTemporaryListener) genericListener).cleanup();
140: }
141: }
142: }
|