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: package org.cougaar.mlm.plugin.perturbation;
027:
028: import java.util.Enumeration;
029: import java.util.Properties;
030: import java.util.Vector;
031:
032: import org.cougaar.core.blackboard.IncrementalSubscription;
033: import org.cougaar.core.blackboard.Subscriber;
034: import org.cougaar.core.blackboard.Subscription;
035: import org.cougaar.glm.ldm.oplan.Oplan;
036: import org.cougaar.planning.ldm.PlanningFactory;
037: import org.cougaar.planning.ldm.asset.Asset;
038: import org.cougaar.planning.plugin.legacy.SimplePlugin;
039: import org.cougaar.util.UnaryPredicate;
040:
041: /**
042: * The PerturbationPlugin is intended to be a test tool which
043: * allows for the modification and/or deletion of Log Plan
044: * objects in Scenario Time.
045: *
046: *
047: */
048: public class PerturbationPlugin extends SimplePlugin
049: /**
050: * The PerturbationPlugin class subscribes for all cluster
051: * assets, initiates the reading of the Perturbation data
052: * and the creation of the scheduler which starts the
053: * Perturbations as necessary.
054: *
055: * @see PerturbationReader PerturbationScheduler
056: */
057: {
058: private Subscriber subscriber_;
059: private Subscription myObjects_;
060: private Enumeration assetList_;
061: private Vector perturbations_;
062: private PlanningFactory myPlanningFactory_;
063: private Properties globalParameters = new Properties();
064:
065: private static UnaryPredicate assetOrOplanPredicate() {
066: return new UnaryPredicate() {
067: public boolean execute(Object o) {
068: return ((o instanceof Asset) || (o instanceof Oplan));
069: //return (o instanceof Asset);
070: }
071: };
072: }
073:
074: /* LDM Access
075: */
076: protected PlanningFactory getMyPlanningFactory() {
077: return myPlanningFactory_;
078: }
079:
080: /*
081: * Creates a subscription by subscribing for all known
082: * assets. ( Since there is no way to know what assets
083: * have been designated for perturbation).
084: */
085: protected void setupSubscriptions() {
086: myObjects_ = subscribe(assetOrOplanPredicate());
087:
088: // Set up the perturbations.
089: setupPerturbations();
090: }
091:
092: /**
093: * Sets up the perturbations by instantiating the
094: * @see PerturbationReader and the @see PerturbationScheduler.
095: */
096: protected void setupPerturbations() {
097: subscriber_ = myObjects_.getSubscriber();
098: assetList_ = ((IncrementalSubscription) myObjects_).elements();
099:
100: // Get the Plugin Parameters
101: Vector params = getParameters();
102:
103: // Instantiate the PerturbationReader and get the
104: // generated Perturbations.
105: if (params == null) {
106: throw new RuntimeException("PerturbationPlugin requires "
107: + "a parameter");
108: }
109: String xmlfilename = (String) params.elementAt(0);
110: globalParameters.put("XMLFile", xmlfilename);
111: PerturbationReader pr = new PerturbationReader(xmlfilename,
112: subscriber_, this );
113: perturbations_ = pr.getPerturbations();
114: System.out.println("\n<<<PerturbationPlugin>>> "
115: + perturbations_.size() + " detected.");
116:
117: // Create an vector to hold the assets
118: Vector theList = new Vector();
119: while (assetList_.hasMoreElements())
120: theList.addElement(assetList_.nextElement());
121:
122: // For each perturbation, set the assets and the
123: // subscriber.
124: for (int i = 0; i < perturbations_.size(); i++) {
125: ((Perturbation) ((PerturbationNode) perturbations_
126: .elementAt(i)).job).setAssets(theList);
127:
128: ((Perturbation) ((PerturbationNode) perturbations_
129: .elementAt(i)).job).setSubscriber(subscriber_);
130: }
131:
132: // Instantiate the PerturbationScheduler
133: PerturbationScheduler ps = new PerturbationScheduler(
134: perturbations_);
135: }
136:
137: /*for testing*/
138: public void printAssets() {
139: openTransaction();
140: myObjects_ = subscribe(assetOrOplanPredicate());
141: closeTransaction();
142: assetList_ = ((IncrementalSubscription) myObjects_).elements();
143: while (assetList_.hasMoreElements()) {
144: System.out.println("\n\n");
145: System.out.println(assetList_.nextElement());
146: }
147: }
148:
149: /**
150: * Normally this method would execute the Plugins functionality
151: * every time it is awaken. Currently, in the PerturbationPlugin
152: * this type of scheduling is handled by the PerturbationScheduler
153: * so that this method has no functionality. This may change in
154: * later releases of this Plugin.
155: */
156: protected void execute() {
157: myPlanningFactory_ = getFactory();
158: }
159:
160: }
|