001: /*
002: * <copyright>
003: *
004: * Copyright 2001-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.planning.plugin.completion;
028:
029: import java.text.SimpleDateFormat;
030: import java.util.AbstractCollection;
031: import java.util.Collection;
032: import java.util.Date;
033: import java.util.Iterator;
034:
035: import org.cougaar.core.plugin.ServiceUserPlugin;
036: import org.cougaar.util.EmptyIterator;
037: import org.cougaar.util.UnaryPredicate;
038:
039: /**
040: * This plugin gathers and integrates completion information from
041: * agents in a society to determin the "completion" of the current
042: * tasks. In most agents, it gathers the information and forwards the
043: * completion status of the agent to another agent. This process
044: * continues through a hierarchy of such plugins until the plugin at
045: * the root of the tree is reached. When the root determines that
046: * completion has been acheived (or is never going to be achieved), it
047: * advances the clock with the expectation that the advancement will
048: * engender additional activity and waits for the completion of that
049: * work.
050: **/
051:
052: public abstract class CompletionPlugin extends ServiceUserPlugin {
053: protected CompletionPlugin(Class[] requiredServices) {
054: super (requiredServices);
055: }
056:
057: protected static UnaryPredicate targetRelayPredicate = new UnaryPredicate() {
058: public boolean execute(Object o) {
059: if (o instanceof CompletionRelay) {
060: CompletionRelay relay = (CompletionRelay) o;
061: return relay.getSource() != null;
062: }
063: return false;
064: }
065: };
066:
067: /**
068: * A Collection implementation the retains nothing. Used for a
069: * Subscription to note publish events of interest, but for which
070: * the actual objects are unneeded. Most of the work is done by the
071: * AbstractCollection base class. We implement the minimum required
072: * for a mutable Collection.
073: **/
074: protected class AmnesiaCollection extends AbstractCollection {
075: public Iterator iterator() {
076: return EmptyIterator.iterator();
077: }
078:
079: public int size() {
080: return 0;
081: }
082:
083: public boolean add(Object o) {
084: return false;
085: }
086: }
087:
088: protected void checkPersistenceNeeded(Collection relays) {
089: for (Iterator i = relays.iterator(); i.hasNext();) {
090: CompletionRelay relay = (CompletionRelay) i.next();
091: if (relay.persistenceNeeded()) {
092: setPersistenceNeeded();
093: relay.resetPersistenceNeeded();
094: blackboard.publishChange(relay);
095: }
096: }
097: }
098:
099: protected abstract void setPersistenceNeeded();
100:
101: private static final SimpleDateFormat dateFormat = new SimpleDateFormat(
102: "MM/dd/yyyy HH:mm:ss");
103: private static Date fdate = new Date();
104:
105: public static String formatDate(long time) {
106: synchronized (fdate) {
107: fdate.setTime(time);
108: return dateFormat.format(fdate);
109: }
110: }
111: }
|