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 org.cougaar.planning.plugin.legacy.PluginDelegate;
030:
031: /**
032: * A Trigger is an object containing information indicating an action
033: * to be taken if a particular state exists among a specified set of objects.
034: * The trigger contains three pieces:
035: * MONITOR - Establishes the set of objects on which to test for the state
036: * TESTER - Determines if the state exists
037: * ACTION - Performs an action on the set of objects (or other class
038: * state info)
039: *
040: * The Trigger contains an Execute method which captures the logic
041: * of executing trigger actions when the monitored state exists.
042: *
043: */
044:
045: public class Trigger implements java.io.Serializable {
046:
047: private TriggerMonitor my_monitor;
048: private TriggerTester my_tester;
049: private TriggerAction my_action;
050:
051: /** Basic Constructor.
052: * @param monitor
053: * @param tester
054: * @param action
055: */
056: public Trigger(TriggerMonitor monitor, TriggerTester tester,
057: TriggerAction action) {
058: my_monitor = monitor;
059: my_tester = tester;
060: my_action = action;
061: }
062:
063: /**
064: * Is this trigger fully filled in, and if so, is the monitor ready to run?
065: */
066: public boolean ReadyToRun(PluginDelegate pid) {
067: // note don't worry if the tester is null, we could have a monitor and an action.
068: if ((my_monitor != null) && (my_action != null)
069: && (my_monitor.ReadyToRun(pid))) {
070: return true;
071: } else {
072: return false;
073: }
074: }
075:
076: /** @return The monitor associated with this Trigger. */
077: public TriggerMonitor getMonitor() {
078: return my_monitor;
079: }
080:
081: /**
082: * Run the trigger : if the condition exists on the objects, fire the action
083: */
084: public void Execute(PluginDelegate pid) {
085: Object[] objects = my_monitor.getAssociatedObjects();
086: if (my_tester != null) {
087: if (my_tester.Test(objects)) {
088: my_action.Perform(objects, pid);
089: }
090: } else {
091: // if we don't have a tester go straight to the action
092: // but make sure that objects is not empty since the monitor objects
093: // returned end up being our tester
094: if (objects.length > 0) {
095: my_action.Perform(objects, pid);
096: }
097: }
098: my_monitor.IndicateRan(pid);
099: }
100:
101: }
|