01: /*
02: * <copyright>
03: *
04: * Copyright 1997-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.mlm.examples;
28:
29: import java.util.Collection;
30: import java.util.Iterator;
31: import java.util.Set;
32:
33: import org.cougaar.core.blackboard.AnonymousChangeReport;
34: import org.cougaar.core.blackboard.ChangeReport;
35: import org.cougaar.core.blackboard.IncrementalSubscription;
36: import org.cougaar.planning.plugin.legacy.SimplePlugin;
37: import org.cougaar.util.UnaryPredicate;
38:
39: /**
40: * ChangeReporter subscribes for everything, and prints
41: * any detailed ChangeReports associated with any changed
42: * objects.
43: **/
44:
45: public class ChangeReporter extends SimplePlugin {
46: private IncrementalSubscription stuff = null;
47:
48: protected void setupSubscriptions() {
49: UnaryPredicate trueP = new UnaryPredicate() {
50: public boolean execute(Object o) {
51: return true;
52: }
53: };
54: stuff = (IncrementalSubscription) subscribe(trueP);
55: }
56:
57: protected void execute() {
58: Collection c = stuff.getChangedCollection();
59: if (c != null && c.size() > 0) {
60: for (Iterator it = c.iterator(); it.hasNext();) {
61: Object o = it.next();
62:
63: Set crs = stuff.getChangeReports(o);
64:
65: if (crs != AnonymousChangeReport.SET) {
66: if (crs == null) {
67: // shouldn't happen:
68: synchronized (System.out) {
69: System.out.println(o.toString()
70: + " null change reports?");
71: }
72: } else if (crs.isEmpty()) {
73: // shouldn't happen:
74: synchronized (System.out) {
75: System.out.println(o.toString()
76: + " empty change reports?");
77: }
78: } else {
79: synchronized (System.out) {
80: System.out.println(o.toString()
81: + " change reports:");
82: for (Iterator crit = crs.iterator(); crit
83: .hasNext();) {
84: ChangeReport cr = (ChangeReport) crit
85: .next();
86: System.out.println("\t" + cr);
87: }
88: }
89: }
90: } else {
91: System.out.println(o.toString()
92: + " changed without details");
93: }
94: }
95: }
96: }
97: }
|