001: /*
002: * <copyright>
003: *
004: * Copyright 2003-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.lib.aggagent.test;
028:
029: import java.util.Iterator;
030:
031: import org.cougaar.lib.aggagent.query.AggregationQuery;
032: import org.cougaar.lib.aggagent.query.Alert;
033: import org.cougaar.lib.aggagent.query.ResultSetDataAtom;
034: import org.cougaar.lib.aggagent.query.ScriptSpec;
035: import org.cougaar.lib.aggagent.util.Enum.Language;
036: import org.cougaar.lib.aggagent.util.Enum.QueryType;
037: import org.cougaar.lib.aggagent.util.Enum.ScriptType;
038: import org.cougaar.lib.aggagent.util.Enum.XmlFormat;
039: import org.cougaar.util.UnaryPredicate;
040:
041: /**
042: * For testing Alert functionality with the old NumberCycle test scaffolding.
043: * A CycleSizeAlert is a concrete Alert that scans the result set for values
044: * below a specified threshold and fires whenever one is discovered. Also
045: * included for convenience are static functions to create default instances,
046: * compatible queries, and a predicate for locating Alerts on the blackboard.
047: */
048: public class CycleSizeAlert extends Alert {
049: private static int count = 0;
050:
051: private int threshold = 0;
052:
053: public CycleSizeAlert(int t) {
054: threshold = t;
055: setName("Default_" + (count++));
056: }
057:
058: public void handleUpdate() {
059: System.out.println("CycleSizeAlert::handleUpdate: called");
060: Iterator i = getQueryAdapter().getResultSet().getAllAtoms();
061: while (i.hasNext()) {
062: ResultSetDataAtom d = (ResultSetDataAtom) i.next();
063: try {
064: int val = Integer.parseInt(d.getValue("value")
065: .toString());
066: if (val < threshold) {
067: setAlerted(true);
068: return;
069: }
070: } catch (Exception eek) {
071: }
072: }
073: setAlerted(false);
074: }
075:
076: /**
077: * Because I am supremely lazy, this method automatically feeds suitable
078: * SILK scripts into an AggregationQuery instance.
079: */
080: public static AggregationQuery createDefaultQuery() {
081: AggregationQuery q = new AggregationQuery(QueryType.PERSISTENT);
082: q.setName("Test Query");
083: q.addSourceCluster("TestSource");
084:
085: q
086: .setPredicateSpec(new ScriptSpec(
087: ScriptType.UNARY_PREDICATE,
088: Language.SILK,
089: "(begin\n"
090: + " (import \"org.cougaar.lib.aggagent.test.NumberCycle\")\n"
091: + " (lambda (obj)\n"
092: + " (.isInstance NumberCycle.class obj)))"));
093:
094: q
095: .setFormatSpec(new ScriptSpec(
096: Language.SILK,
097: XmlFormat.XMLENCODER,
098: "(begin\n"
099: + " (import \"org.cougaar.lib.aggagent.query.ResultSetDataAtom\")\n"
100: + " (lambda (nc ps)\n"
101: + " (let ((data_atom (ResultSetDataAtom.)))\n"
102: + " (.addIdentifier data_atom \"length\" (.getLength nc))\n"
103: + " (.addValue data_atom \"value\" (.getValue nc))\n"
104: + " (.println ps (.toXML data_atom))\n"
105: + " (.flush ps))))\n"));
106:
107: return q;
108: }
109:
110: /**
111: * Create a "default" instance of CycleSizeAlert. The default value
112: * threshold is 2.
113: */
114: public static CycleSizeAlert getDefaultAlert() {
115: return new CycleSizeAlert(2);
116: }
117:
118: // look for alerts on the logplan with this UnaryPredicate class.
119: private static class ActiveAlertSeeker implements UnaryPredicate {
120: public boolean execute(Object o) {
121: if (o instanceof Alert) {
122: // Alert a = (Alert) o;
123: // return a.isAlerted();
124: return true;
125: }
126: return false;
127: }
128: }
129:
130: private static ActiveAlertSeeker activeAlerts = new ActiveAlertSeeker();
131:
132: public static UnaryPredicate getActiveAlertSeeker() {
133: return activeAlerts;
134: }
135: }
|