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.mlm.plugin.ldm;
028:
029: import java.util.Collection;
030: import java.util.Iterator;
031: import java.util.Vector;
032:
033: import org.cougaar.core.blackboard.IncrementalSubscription;
034: import org.cougaar.core.util.UID;
035: import org.cougaar.glm.ldm.GLMFactory;
036: import org.cougaar.glm.ldm.oplan.Oplan;
037: import org.cougaar.glm.ldm.oplan.OplanContributor;
038: import org.cougaar.glm.ldm.oplan.OplanCoupon;
039: import org.cougaar.glm.ldm.oplan.OrgActivity;
040: import org.cougaar.glm.ldm.oplan.OrgRelation;
041: import org.cougaar.glm.ldm.plan.QueryRequest;
042: import org.cougaar.planning.plugin.legacy.SimplePlugin;
043: import org.cougaar.util.UnaryPredicate;
044:
045: //import org.cougaar.lib.util.UTILAsset;
046:
047: public class GetOplanPlugin extends SimplePlugin {
048:
049: /** Subscription to hold collection of input tasks **/
050:
051: private Vector roles = new Vector();
052: private String relation = null;
053:
054: private IncrementalSubscription orgActivities;
055: private GLMFactory alpFactory;
056:
057: private IncrementalSubscription oplanCoupons;
058: private static UnaryPredicate oplanCouponPred = new UnaryPredicate() {
059: public boolean execute(Object o) {
060: if (o instanceof OplanCoupon) {
061: return true;
062: } else {
063: return false;
064: }
065: }
066: };
067:
068: protected void setupSubscriptions() {
069: alpFactory = (GLMFactory) getFactory("glm");
070:
071: oplanCoupons = (IncrementalSubscription) subscribe(oplanCouponPred);
072: }
073:
074: public synchronized void execute() {
075:
076: if (oplanCoupons.hasChanged()) {
077: Collection adds = oplanCoupons.getAddedCollection();
078: if (adds != null) {
079: for (Iterator addIterator = adds.iterator(); addIterator
080: .hasNext();) {
081: OplanCoupon ow = (OplanCoupon) addIterator.next();
082: // We shouldn't run the plugin in the same cluster where
083: // the oplan is published, but if we do, just ignore the coupon
084: if (!ow.getOwner().equals(getMessageAddress())) {
085: requestOplan(ow);
086: }
087: }
088: }
089: // Don't be clever about changes for now, just get the whole thing again.
090: Collection changes = oplanCoupons.getChangedCollection();
091: if (changes != null) {
092: for (Iterator changeIterator = changes.iterator(); changeIterator
093: .hasNext();) {
094: OplanCoupon ow = (OplanCoupon) changeIterator
095: .next();
096: if (!ow.getOwner().equals(getMessageAddress())) {
097: requestOplan(ow);
098: }
099: }
100: }
101: Collection deletes = oplanCoupons.getRemovedCollection();
102: if (deletes != null) {
103: for (Iterator delIterator = deletes.iterator(); delIterator
104: .hasNext();) {
105: OplanCoupon ow = (OplanCoupon) delIterator.next();
106: if (!ow.getOwner().equals(getMessageAddress())) {
107: removeOplan(ow);
108: }
109: }
110: }
111: }
112: }
113:
114: private void requestOplan(final OplanCoupon oplanCoupon) {
115:
116: // BOGUS We make the assumption that the _only_ organization in
117: // this cluster has the same name as the cluster. This assumption
118: // is false on both counts.
119: String orgID = getMessageAddress().toString();
120: OplanPredicate oplanPred = new OplanPredicate(oplanCoupon
121: .getOplanUID(), orgID);
122:
123: // Used to find local objects which originate from the original query -
124: // used to reconcile results of the query with local objects.
125: OplanPredicate localPred = new OplanPredicate(oplanCoupon
126: .getOplanUID(), orgID);
127: QueryRequest qr;
128: try {
129: qr = alpFactory
130: .newQueryRequest(oplanPred, localPred, oplanCoupon
131: .getHomeClusterID(), getMessageAddress());
132: } catch (RuntimeException e) {
133: e.printStackTrace();
134: return;
135: }
136: if (qr == null) {
137: } else {
138: publishAdd(qr);
139: //System.out.println("GetOplanPlugin: published QueryRequest");
140: }
141: }
142:
143: /**
144: * Removes all matching Oplan components from the logplan
145: **/
146: private void removeOplan(OplanCoupon ow) {
147:
148: // BOGUS See comment above
149: String orgID = getMessageAddress().toString();
150: OplanPredicate op = new OplanPredicate(ow.getOplanUID(), orgID);
151:
152: Collection oplanObjs = query(op);
153: for (Iterator it = oplanObjs.iterator(); it.hasNext();) {
154: publishRemove(it.next());
155: }
156: }
157:
158: private static class OplanPredicate implements UnaryPredicate {
159: private UID _oplanUID;
160: private String _orgID;
161:
162: public OplanPredicate(UID oplanUID, String orgID) {
163: _oplanUID = oplanUID;
164: _orgID = orgID;
165: }
166:
167: public boolean execute(Object o) {
168: if (o instanceof Oplan) {
169: if (((Oplan) o).getUID().equals(_oplanUID)) {
170: return true;
171: }
172: return false;
173: }
174:
175: if (o instanceof OplanContributor) {
176: OplanContributor oc = (OplanContributor) o;
177: //System.out.println("Predicate: found OplanContributor");
178: if (oc.getOplanUID().equals(_oplanUID)) {
179: //System.out.println("Predicate: has correct OplanUID " + oc.getOplanUID());
180: if (oc instanceof OrgActivity) {
181: if (((OrgActivity) oc).getOrgID()
182: .equals(_orgID)) {
183: //System.out.println("Predicate: correct OrgID in OrgActivity");
184: return true;
185: } else {
186: return false;
187: }
188: } else if (oc instanceof OrgRelation) {
189: if (((OrgRelation) oc).getOrgID()
190: .equals(_orgID)) {
191: return true;
192: } else {
193: return false;
194: }
195: }
196: //System.out.println("Predicate: found a good one! " + o);
197: return true;
198: }
199: }
200: //System.out.println("Predicate: Sorry, play again soon");
201: return false;
202: }
203: }
204: }
|