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.glm.plugins;
028:
029: import java.util.Enumeration;
030: import java.util.Vector;
031:
032: import org.cougaar.core.blackboard.IncrementalSubscription;
033: import org.cougaar.glm.ldm.oplan.ForcePackage;
034: import org.cougaar.glm.ldm.oplan.Oplan;
035: import org.cougaar.glm.ldm.oplan.OrgActivity;
036: import org.cougaar.planning.plugin.legacy.SimplePlugin;
037: import org.cougaar.util.UnaryPredicate;
038:
039: public class OplanObserverPlugin extends SimplePlugin {
040:
041: private String myId;
042: private boolean trackOplan;
043: private boolean trackForcePackage;
044: private boolean trackOrgActivity;
045:
046: private IncrementalSubscription oplans;
047:
048: private static UnaryPredicate oplansPred() {
049: return new UnaryPredicate() {
050: public boolean execute(Object o) {
051: return (o instanceof Oplan);
052: }
053: };
054: }
055:
056: private IncrementalSubscription forcepackages;
057:
058: private static UnaryPredicate forcepackagesPred() {
059: return new UnaryPredicate() {
060: public boolean execute(Object o) {
061: return (o instanceof ForcePackage);
062: }
063: };
064: }
065:
066: private IncrementalSubscription orgactivities;
067:
068: private static UnaryPredicate orgactivitiesPred() {
069: return new UnaryPredicate() {
070: public boolean execute(Object o) {
071: return (o instanceof OrgActivity);
072: }
073: };
074: }
075:
076: public void setupSubscriptions() {
077: Vector params = getParameters();
078: if (params != null)
079: for (int i = 0; i < params.size(); i++) {
080: String param = (String) params.elementAt(i);
081: trackOplan = (trackOplan || param.toLowerCase().equals(
082: "oplan"));
083: trackForcePackage = (trackForcePackage || param
084: .toLowerCase().equals("forcepackage"));
085: trackOrgActivity = (trackOrgActivity || param
086: .toLowerCase().equals("orgactivity"));
087: }
088:
089: myId = getMessageAddress().toAddress();
090:
091: oplans = (IncrementalSubscription) subscribe(oplansPred());
092: forcepackages = (IncrementalSubscription) subscribe(forcepackagesPred());
093: orgactivities = (IncrementalSubscription) subscribe(orgactivitiesPred());
094: }
095:
096: public void execute() {
097: Enumeration e = null;
098:
099: if (oplans.hasChanged() && trackOplan) {
100: e = oplans.getAddedList();
101: while (e.hasMoreElements()) {
102: Oplan o = (Oplan) e.nextElement();
103: display("new Oplan = " + o.getOplanId());
104: }
105:
106: e = oplans.getChangedList();
107: while (e.hasMoreElements()) {
108: Oplan o = (Oplan) e.nextElement();
109: display("changed Oplan = " + o.getOplanId());
110: }
111:
112: e = oplans.getRemovedList();
113: while (e.hasMoreElements()) {
114: Oplan o = (Oplan) e.nextElement();
115: display("removed Oplan = " + o.getOplanId());
116: }
117: }
118:
119: if (forcepackages.hasChanged() && trackForcePackage) {
120: e = forcepackages.getAddedList();
121: while (e.hasMoreElements()) {
122: ForcePackage o = (ForcePackage) e.nextElement();
123: display("new ForcePackage = " + o.getForcePackageId());
124: }
125:
126: e = forcepackages.getChangedList();
127: while (e.hasMoreElements()) {
128: ForcePackage o = (ForcePackage) e.nextElement();
129: display("changed ForcePackage = "
130: + o.getForcePackageId());
131: }
132:
133: e = forcepackages.getRemovedList();
134: while (e.hasMoreElements()) {
135: ForcePackage o = (ForcePackage) e.nextElement();
136: display("removed ForcePackage = "
137: + o.getForcePackageId());
138: }
139: }
140:
141: if (orgactivities.hasChanged() && trackOrgActivity) {
142: e = orgactivities.getAddedList();
143: while (e.hasMoreElements()) {
144: OrgActivity o = (OrgActivity) e.nextElement();
145: display("new OrgActivity = " + o.getOrgID());
146: }
147:
148: e = orgactivities.getChangedList();
149: while (e.hasMoreElements()) {
150: OrgActivity o = (OrgActivity) e.nextElement();
151: display("changed OrgActivity = " + o.getOrgID());
152: }
153:
154: e = orgactivities.getRemovedList();
155: while (e.hasMoreElements()) {
156: OrgActivity o = (OrgActivity) e.nextElement();
157: display("removed OrgActivity = " + o.getOrgID());
158: }
159: }
160:
161: }
162:
163: private void display(String output) {
164: System.out.println("\n <" + myId + "> found a " + output);
165: }
166:
167: }
|