001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2005 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.core.qos.frame.visualizer;
028:
029: import java.net.URL;
030: import java.util.ArrayList;
031: import java.util.Collection;
032: import java.util.Enumeration;
033: import java.util.HashMap;
034:
035: import org.cougaar.core.blackboard.IncrementalSubscription;
036: import org.cougaar.core.component.ServiceBroker;
037: import org.cougaar.core.qos.frame.DataFrame;
038: import org.cougaar.core.qos.frame.Frame;
039: import org.cougaar.core.qos.frame.FrameSet;
040: import org.cougaar.core.plugin.ParameterizedPlugin;
041: import org.cougaar.core.service.BlackboardService;
042: import org.cougaar.core.service.LoggingService;
043: import org.cougaar.core.service.ThreadService;
044: import org.cougaar.util.UnaryPredicate;
045:
046: public class FrameVisualizerPlugin extends ParameterizedPlugin {
047: private UnaryPredicate framePred = new UnaryPredicate() {
048: public boolean execute(Object o) {
049: return ((o instanceof DataFrame) && ((DataFrame) o)
050: .getFrameSet().getName().equals(frameSetName));
051: }
052: };
053: private IncrementalSubscription sub;
054: private LoggingService log;
055: private String frameSetName;
056: private ArrayList frameCache;
057:
058: FrameModel frameModel;
059: boolean newFramesPresent;
060:
061: //static String TICK_EVENT_LABEL = "TICK";
062: //protected int tickNumber;
063:
064: public void load() {
065: super .load();
066: ServiceBroker sb = getServiceBroker();
067: log = (LoggingService) sb.getService(this ,
068: LoggingService.class, null);
069: }
070:
071: protected void setupSubscriptions() {
072: BlackboardService bbs = getBlackboardService();
073: if (log.isDebugEnabled())
074: log.debug("FrameSet name is " + frameSetName);
075:
076: sub = (IncrementalSubscription) bbs.subscribe(framePred);
077:
078: if (!sub.getAddedCollection().isEmpty() && log.isDebugEnabled())
079: log.debug("Subscription has initial contents");
080: do_execute(bbs);
081: }
082:
083: public void start() {
084: frameSetName = getParameter("frame-set");
085: frameCache = new ArrayList();
086: newFramesPresent = false;
087:
088: String specFileName = getParameter("spec-file");
089: // File xml_file = ConfigFinder.getInstance().locateFile(specFileName);
090: ClassLoader loader = FrameVisualizerPlugin.class
091: .getClassLoader();
092: URL xml_url = loader.getResource(specFileName);
093: if (xml_url == null) {
094: log.error("View Spec file not found " + specFileName);
095: return;
096: }
097: frameModel = new FrameModel();
098: ServiceBroker sb = getServiceBroker();
099: ThreadService tsvc = (ThreadService) sb.getService(this ,
100: ThreadService.class, null);
101: new DisplayWindow(frameModel, xml_url, tsvc);
102:
103: //SwingUtilities.invokeLater(new CreateWindowHelper(xml_file));
104: super .start();
105: }
106:
107: protected void execute() {
108: BlackboardService bbs = getBlackboardService();
109: do_execute(bbs);
110: }
111:
112: private void do_execute(BlackboardService bbs) {
113: if (!sub.hasChanged()) {
114: if (log.isDebugEnabled())
115: log.debug("No Frame changes");
116: return;
117: }
118: if (log.isDebugEnabled())
119: log.debug("There are changes.");
120: Enumeration en;
121:
122: // New Frames
123: en = sub.getAddedList();
124: if (en.hasMoreElements()) {
125: if (log.isDebugEnabled())
126: log.debug("There are new frames.");
127: processNewFrames(en);
128: }
129:
130: // Changed Frames
131: en = sub.getChangedList();
132: if (en.hasMoreElements()) {
133: if (log.isDebugEnabled())
134: log.debug("There are changed frames.");
135: processChangedFrames(en, sub);
136: }
137: // Remove Frames. Won't happen.
138: en = sub.getRemovedList();
139: if (en.hasMoreElements()) {
140: if (log.isDebugEnabled())
141: log.debug("There are removed frames (ignored).");
142: processRemovedFrames(en);
143: }
144: }
145:
146: private void processNewFrames(Enumeration en) {
147: FrameSet frameSet = null;
148: ArrayList added = new ArrayList();
149: while (en.hasMoreElements()) {
150: Frame frame = (Frame) en.nextElement();
151: if (frameSet == null)
152: frameSet = frame.getFrameSet();
153: if (log.isDebugEnabled()) {
154: log.debug("Observed added '" + frame.getValue("name")
155: + "' " + frame);
156: }
157: // Handle new Frame
158: added.add(frame);
159: frameCache.add(frame);
160: newFramesPresent = true;
161: }
162:
163: if (newFramesPresent) {
164: if (!frameModel.hasFrameSet())
165: frameModel.setFrameSet(frameSet);
166:
167: frameModel.framesAdded(added);
168: //pluginDisplay.setFrameHelper(frameModel);
169: newFramesPresent = false;
170: }
171: }
172:
173: private void processChangedFrames(Enumeration en,
174: IncrementalSubscription sub) {
175: HashMap changes = new HashMap();
176: Collection ch;
177: while (en.hasMoreElements()) {
178: Frame frame = (Frame) en.nextElement();
179: if (log.isDebugEnabled()) {
180: log.debug("Observed changed " + frame);
181: }
182:
183: ch = sub.getChangeReports(frame);
184: if (changes.get(frame) != null)
185: throw new IllegalArgumentException(
186: "already created a change for this frame: "
187: + frame);
188: changes.put(frame, ch);
189: }
190: if (log.isDebugEnabled())
191: log.debug("Calling frameModel.framesChanged");
192: frameModel.framesChanged(changes);
193: }
194:
195: private void processRemovedFrames(Enumeration en) {
196: while (en.hasMoreElements()) {
197: Frame frame = (Frame) en.nextElement();
198: if (log.isDebugEnabled()) {
199: log.debug("Observed removed " + frame);
200: }
201: }
202: }
203:
204: }
|