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.mlm.plugin.generic;
028:
029: import java.io.BufferedReader;
030: import java.io.FileInputStream;
031: import java.io.FileNotFoundException;
032: import java.io.InputStreamReader;
033: import java.util.Enumeration;
034: import java.util.Vector;
035:
036: import org.cougaar.core.blackboard.IncrementalSubscription;
037: import org.cougaar.core.service.BlackboardService;
038: import org.cougaar.glm.GLMPlugin;
039: import org.cougaar.planning.ldm.PlanningFactory;
040: import org.cougaar.util.UnaryPredicate;
041:
042: import FESI.jslib.JSException;
043: import FESI.jslib.JSGlobalObject;
044: import FESI.jslib.JSUtil;
045:
046: public class GenericScriptablePlugin extends GLMPlugin {
047:
048: IncrementalSubscription[] mySubscriptions = new IncrementalSubscription[10];
049: JSGlobalObject global = null;
050:
051: protected void setupSubscriptions() {
052: PlanningFactory ldmf = theLDMF;
053:
054: try {
055: Vector all_parameters = getParameters();
056: String scriptFile = (String) all_parameters.elementAt(0);
057: System.out.println(" The scriptName is: " + scriptFile);
058:
059: String alpInstallPath = System
060: .getProperty("org.cougaar.install.path");
061: String sep = System.getProperty("file.separator");
062: String helperScriptFile = alpInstallPath + sep + "external"
063: + sep + "alpine" + sep + "data" + sep + "generic"
064: + sep + "scripts" + sep + "HelperFunctions.es";
065: // String helperScriptFile = "/home/pcheruku/alpine/alp/external/alpine/data/generic/scripts/HelperFunctions.es";
066:
067: //Load the two basic FESI extensions.
068: String extensions[] = new String[3];
069: extensions[0] = "FESI.Extensions.FileIO";
070: extensions[1] = "FESI.Extensions.BasicIO";
071: extensions[2] = "FESI.Extensions.JavaAccess";
072:
073: global = JSUtil.makeEvaluator(extensions);
074:
075: //Read the helper methods.
076: FileInputStream helperFile = new FileInputStream(
077: helperScriptFile);
078: BufferedReader helperReader = new BufferedReader(
079: new InputStreamReader(helperFile));
080: global.eval(helperReader, "Helper File Evaluator");
081:
082: //Get all the subscriptions from the script.
083: FileInputStream fi = new FileInputStream(scriptFile);
084: BufferedReader bf = new BufferedReader(
085: new InputStreamReader(fi));
086: global.eval(bf, "Script File Evaluator");
087:
088: //setup properties in ECMAScript for the helper methods before doing anything else.
089: global.setMember("ldmf", ldmf);
090: global.setMember("_plugin", this ); //for now use the _plugin variable, but later use of this should be avoided.
091:
092: global.eval("arrayPredicates = scriptSubscription()");
093: //global.eval( "writeln( \"array predicates are : \"); " );
094: //global.eval( "writeln( arrayPredicates )" );
095: global
096: .eval("scriptVector = arrayToVector( arrayPredicates )");
097: Vector myPredicates = new Vector();
098: myPredicates = (Vector) global.getMember("scriptVector");
099:
100: System.out
101: .println(" The no. of predicates returned by ECMAScript are: "
102: + myPredicates.size());
103:
104: int i = 0;
105: Enumeration enumpreds = myPredicates.elements();
106: while (enumpreds.hasMoreElements()) {
107: UnaryPredicate myPred = (UnaryPredicate) (enumpreds
108: .nextElement());
109: mySubscriptions[i] = (IncrementalSubscription) subscribe(myPred);
110: i++;
111: }
112:
113: } catch (JSException e) {
114: System.err.println(e);
115: System.exit(1);
116: } catch (FileNotFoundException fe) {
117: System.err
118: .println("FileNotFoundException. Please give the right parameters to the plugin");
119: fe.printStackTrace();
120: }
121:
122: }
123:
124: public void execute() {
125:
126: try {
127: global.setMember("mySubscriptions", mySubscriptions);
128: global.eval(" scriptExecute( mySubscriptions ) ");
129: } catch (JSException je) {
130: System.out
131: .println(" Exception when trying to run the scriptExecute method from the script file");
132: je.printStackTrace();
133: }
134:
135: }
136:
137: //Work around for a possible ( have to investigate more ) bug in ECMAScript. Just calls the getSubscriber();
138: public BlackboardService getPluginSubscriber() {
139: return getBlackboardService();
140: }
141: }
|