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.lib.web.axis.distance;
028:
029: import org.cougaar.util.Arguments;
030: import org.cougaar.util.UnaryPredicate;
031: import org.cougaar.core.blackboard.IncrementalSubscription;
032: import org.cougaar.core.plugin.ComponentPlugin;
033: import org.cougaar.core.service.LoggingService;
034: import org.cougaar.core.service.UIDService;
035: import org.cougaar.core.util.UID;
036: import java.util.Enumeration;
037:
038: /**
039: * This component publishes a test {@link DistanceQuery} object that
040: * the {@link DistanceQueryPlugin} will process.
041: * <p>
042: * Requires the {@link DistanceQueryPlugin} to be loaded.
043: * <p>
044: * Load with:<pre>
045: * <component
046: * class="org.cougaar.lib.web.axis.distance.DistanceQueryTest">
047: * <argument>fromZip=02138</argument>
048: * <argument>toZip=90210</argument>
049: * </component>
050: * </pre>
051: * <p>
052: * The expected output is:<br><code>
053: * .. Published query:
054: * (distance-query uid=NodeA/<i>int</i>
055: * fromZip=02138 toZip=90210 distance=0.0)<br>
056: * .. Observed query answer:
057: * (distance-query uid=NodeA/<i>int</i>
058: * fromZip=02138 toZip=90210 distance=2594.585085768498)<br>
059: * </code>
060: */
061: public class DistanceQueryTest extends ComponentPlugin {
062:
063: private static final String DEFAULT_FROM_ZIP = "02138";
064: private static final String DEFAULT_TO_ZIP = "90210";
065:
066: private static final UnaryPredicate DISTANCE_QUERY_PREDICATE = new DistanceQueryPredicate();
067:
068: private LoggingService log;
069: private UIDService uidService;
070:
071: private IncrementalSubscription distSub;
072:
073: public void setLoggingService(LoggingService log) {
074: this .log = log;
075: }
076:
077: public void setUIDService(UIDService uidService) {
078: this .uidService = uidService;
079: }
080:
081: protected void setupSubscriptions() {
082:
083: // subscribe to our query response, which we'll create shortly
084: distSub = (IncrementalSubscription) blackboard
085: .subscribe(DISTANCE_QUERY_PREDICATE);
086:
087: // check plugin parameters for "fromZip=" and "toZip="
088: Arguments args = new Arguments(getParameters());
089: String fromZip = args.getString("fromZip", DEFAULT_FROM_ZIP);
090: String toZip = args.getString("toZip", DEFAULT_TO_ZIP);
091:
092: // publish our query
093: UID uid = uidService.nextUID();
094: DistanceQuery dq = new DistanceQuery(uid, fromZip, toZip);
095: blackboard.publishAdd(dq);
096:
097: if (log.isShoutEnabled()) {
098: log.shout("Published query: " + dq);
099: }
100:
101: // in "execute()" we'll listen for a response
102: }
103:
104: protected void execute() {
105: // ignore distSub.getAddedList(), we create them
106:
107: // check for answers from the DistanceQueryPlugin
108: for (Enumeration en = distSub.getChangedList(); en
109: .hasMoreElements();) {
110: DistanceQuery dq = (DistanceQuery) en.nextElement();
111: if (log.isShoutEnabled()) {
112: log.shout("Observed query answer: " + dq);
113: }
114: }
115: }
116:
117: private static final class DistanceQueryPredicate implements
118: UnaryPredicate {
119: public boolean execute(Object o) {
120: return (o instanceof DistanceQuery);
121: }
122: }
123: }
|