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.Vector;
034:
035: import org.cougaar.core.blackboard.IncrementalSubscription;
036: import org.cougaar.glm.GLMPlugin;
037: import org.cougaar.planning.ldm.PlanningFactory;
038: import org.cougaar.util.UnaryPredicate;
039:
040: import FESI.jslib.JSException;
041: import FESI.jslib.JSGlobalObject;
042: import FESI.jslib.JSUtil;
043:
044: public class GenericAssessorPlugin extends GLMPlugin {
045:
046: IncrementalSubscription[] mySubscriptions;
047: JSGlobalObject global = null;
048:
049: protected void setupSubscriptions() {
050: PlanningFactory ldmf = theLDMF;
051:
052: try {
053: Vector all_parameters = getParameters();
054: String scriptFile = (String) all_parameters.elementAt(0);
055: System.out.println(" The scriptName is: " + scriptFile);
056:
057: String alpInstallPath = System
058: .getProperty("org.cougaar.install.path");
059: String sep = System.getProperty("file.separator");
060: String helperScriptFile = alpInstallPath + sep + "external"
061: + sep + "alpine" + sep + "data" + sep + "generic"
062: + sep + "scripts" + sep + "HelperFunctions.es";
063:
064: //Load the two basic FESI extensions.
065: String extensions[] = new String[3];
066: extensions[0] = "FESI.Extensions.FileIO";
067: extensions[1] = "FESI.Extensions.BasicIO";
068: extensions[2] = "FESI.Extensions.JavaAccess";
069:
070: global = JSUtil.makeEvaluator(extensions);
071:
072: //setup properties in ECMAScript for the helper methods before doing anything else.
073: global.setMember("ldmf", ldmf);
074: global.setMember("_plugin", this );
075: global.setMember("alert", "false");
076:
077: //Read the helper methods.
078: FileInputStream helperFile = new FileInputStream(
079: helperScriptFile);
080: BufferedReader helperReader = new BufferedReader(
081: new InputStreamReader(helperFile));
082: global.eval(helperReader, "Helper File Evaluator");
083:
084: //Get all the subscriptions from the script.
085: FileInputStream fi = new FileInputStream(scriptFile);
086: BufferedReader bf = new BufferedReader(
087: new InputStreamReader(fi));
088: global.eval(bf, "Script File Evaluator");
089:
090: global.eval("arrayPredicates = expector()");
091: global
092: .eval("scriptVector = arraytoVector( arrayPredicates )");
093: Vector myPredicates = (Vector) global
094: .getMember("scriptVector");
095:
096: for (int i = 0; i < myPredicates.size(); i++) {
097: mySubscriptions[i] = (IncrementalSubscription) subscribe((UnaryPredicate) myPredicates
098: .elementAt(i));
099: }
100:
101: } catch (JSException e) {
102: System.err.println(e);
103: System.exit(1);
104: } catch (FileNotFoundException fe) {
105: System.err
106: .println("FileNotFoundException. Please give the right parameters to the plugin");
107: fe.printStackTrace();
108: }
109:
110: }
111:
112: public void execute() {
113:
114: try {
115: global.eval(" monitor( mySubscriptions ) ");
116: String alertString = (String) global.getMember("alert");
117: if (alertString.trim().equals("true")) {
118: global.eval("alert()");
119: }
120: } catch (JSException je) {
121: System.out
122: .println(" Exception when trying to run the scriptExecute method from the script file");
123: System.out
124: .println(" Please check the scriptExecute method for ECMAScript syntax errors");
125: je.printStackTrace();
126: }
127:
128: }
129:
130: }
|