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.planning.ldm.trigger;
028:
029: import java.util.ArrayList;
030: import java.util.Enumeration;
031: import java.util.List;
032:
033: import org.cougaar.core.blackboard.IncrementalSubscription;
034: import org.cougaar.planning.plugin.legacy.PluginDelegate;
035: import org.cougaar.util.UnaryPredicate;
036:
037: /**
038: * A TriggerPredicateBasedMonitor is a kind of monitor that generates a
039: * subscription for objects
040: */
041:
042: public class TriggerPredicateBasedMonitor implements TriggerMonitor {
043:
044: transient private IncrementalSubscription my_subscription;
045: private UnaryPredicate my_predicate;
046: transient private List assobjects = null;
047:
048: public TriggerPredicateBasedMonitor(UnaryPredicate predicate) {
049: my_predicate = predicate;
050: my_subscription = null;
051: }
052:
053: public UnaryPredicate getPredicate() {
054: return my_predicate;
055: }
056:
057: public void EstablishSubscription(
058: IncrementalSubscription subscription) {
059: my_subscription = subscription;
060: }
061:
062: public IncrementalSubscription getSubscription() {
063: return my_subscription;
064: }
065:
066: public Object[] getAssociatedObjects() {
067: if (assobjects == null) {
068: assobjects = new ArrayList();
069: }
070: assobjects.clear();
071: // Pull objects out of subscription
072: if (my_subscription != null) {
073: // check for changes
074: Enumeration clist = my_subscription.getChangedList();
075: while (clist.hasMoreElements()) {
076: Object subobj = clist.nextElement();
077: // make sure that this object isn't already in the list, we don't need it
078: // twice if it happened to get added and changed before we got a chance to run.
079: if (!assobjects.contains(subobj)) {
080: assobjects.add(subobj);
081: }
082: }
083: // check for additions
084: Enumeration alist = my_subscription.getAddedList();
085: while (alist.hasMoreElements()) {
086: Object subobj = alist.nextElement();
087: // make sure that this object isn't already in the list, we don't need it
088: // twice if it happened to get added and changed before we got a chance to run.
089: if (!assobjects.contains(subobj)) {
090: assobjects.add(subobj);
091: }
092: }
093:
094: }
095: //System.err.println("Returning "+assobjects.size()+" objects to be tested");
096: return assobjects.toArray();
097: }
098:
099: public boolean ReadyToRun(PluginDelegate pid) {
100: // Check if subscription has changes (don't need pid for right now)
101: if ((my_subscription != null) && (my_subscription.hasChanged())) {
102: return true;
103: }
104: return false;
105: }
106:
107: public void IndicateRan(PluginDelegate pid) {
108: // Probably nothing to do in this case
109: }
110:
111: }
|