01: /*
02: * <copyright>
03: *
04: * Copyright 2003-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.lib.aggagent.script;
28:
29: import org.cougaar.lib.aggagent.query.AggregationQuery;
30: import org.cougaar.lib.aggagent.query.Alert;
31: import org.cougaar.lib.aggagent.query.QueryResultAdapter;
32: import org.cougaar.lib.aggagent.query.ResultSetDataAtom;
33:
34: import silk.Procedure;
35: import silk.SI;
36:
37: /**
38: * A concrete implementation of the Alert class that uses a SILK script as its
39: * handleUpdate method.
40: */
41: public class SilkAlert extends Alert {
42: private Procedure handler = null;
43:
44: public SilkAlert(String script) {
45: handler = (Procedure) SI.eval(script);
46: }
47:
48: /**
49: * Respond to a change in the result set by calling the SILK script. Note
50: * that even though this method does not take arguments, the procedure
51: * defined in the script should accept one argument, which is the Alert
52: * Object itself.
53: */
54: public void handleUpdate() {
55: handler.apply(new Object[] { this });
56: }
57:
58: // - - - - - - - Test code below this point - - - - - - - - - - - - - - - - - -
59:
60: private static String CODE = "(begin "
61: + "(lambda (self) "
62: + "(define qra (.getQueryAdapter self)) "
63: + "(.setAlerted self (.hasNext (.getAllAtoms (.getResultSet qra))))))";
64:
65: public static void main(String[] argv) {
66: Alert ale = new SilkAlert(CODE);
67: QueryResultAdapter qra = new QueryResultAdapter(
68: (AggregationQuery) null);
69: qra.addAlert(ale);
70: test(ale, "Test 1", false);
71:
72: ResultSetDataAtom atom = new ResultSetDataAtom();
73: atom.addIdentifier("name", "random value");
74: atom.addValue("number", "5");
75: // qra.getResultSet().update(atom);
76: ale.update();
77: test(ale, "Test 2", true);
78: }
79:
80: private static void test(Alert a, String msg, boolean answer) {
81: System.out.println(msg + ": "
82: + (a.isAlerted() == answer ? "Success" : "Failure"));
83: }
84: }
|