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.ldm.lps;
028:
029: import java.util.Collection;
030: import java.util.Enumeration;
031:
032: import org.cougaar.core.blackboard.EnvelopeTuple;
033: import org.cougaar.core.domain.EnvelopeLogicProvider;
034: import org.cougaar.core.domain.LogicProvider;
035: import org.cougaar.core.domain.RestartLogicProvider;
036: import org.cougaar.core.domain.RestartLogicProviderHelper;
037: import org.cougaar.core.domain.RootPlan;
038: import org.cougaar.core.mts.MessageAddress;
039: import org.cougaar.glm.ldm.asset.Organization;
040: import org.cougaar.planning.ldm.PlanningFactory;
041: import org.cougaar.planning.ldm.plan.NewTransferableAssignment;
042: import org.cougaar.planning.ldm.plan.NewTransferableRescind;
043: import org.cougaar.planning.ldm.plan.NewTransferableVerification;
044: import org.cougaar.planning.ldm.plan.Transferable;
045: import org.cougaar.planning.ldm.plan.TransferableTransfer;
046: import org.cougaar.util.UnaryPredicate;
047:
048: /** TransferableLP is a "LogPlan Logic Provider":
049: *
050: * it provides the logic to capture
051: * PlanElements that are Transferable and send TransferableAssignment
052: * directives to the proper remote agent.
053: *
054: **/
055:
056: public class TransferableLP implements LogicProvider,
057: EnvelopeLogicProvider, RestartLogicProvider {
058: private final RootPlan rootplan;
059: private final MessageAddress self;
060: private final PlanningFactory ldmf;
061:
062: public TransferableLP(RootPlan rootplan, MessageAddress self,
063: PlanningFactory ldmf) {
064: this .rootplan = rootplan;
065: this .self = self;
066: this .ldmf = ldmf;
067: }
068:
069: public void init() {
070: }
071:
072: /**
073: * @param o Envelopetuple,
074: * where tuple.object
075: * == PlanElement with an Allocation to an Organization ADDED to LogPlan
076: *
077: * If the test returned true i.e. it was an TransferableTransfer...
078: * create an TransferableAssignment task and send itto a remote Cluster (Organization).
079: **/
080: public void execute(EnvelopeTuple o, Collection changes) {
081: Object obj = o.getObject();
082: if (obj instanceof TransferableTransfer) {
083: TransferableTransfer tt = (TransferableTransfer) obj;
084: MessageAddress dest = ((Organization) tt.getAsset())
085: .getMessageAddress();
086: if (o.isAdd()) {
087: processTransferableTransferAdded(tt, dest);
088: } else if (o.isRemove()) {
089: processTransferableTransferRemoved(tt, dest);
090: }
091: }
092: }
093:
094: // RestartLogicProvider implementation
095:
096: /**
097: * Cluster restart handler. Resend all our Transferables to the
098: * restarted agent. Also send TransferableVerification messages
099: * for all the Transferables we have received from the restarted
100: * agent. The restarted agent will rescind them if they are no
101: * longer valid.
102: **/
103: public void restart(final MessageAddress cid) {
104: UnaryPredicate pred = new UnaryPredicate() {
105: public boolean execute(Object o) {
106: if (o instanceof TransferableTransfer) {
107: TransferableTransfer tt = (TransferableTransfer) o;
108: MessageAddress dest = ((Organization) tt.getAsset())
109: .getMessageAddress();
110: return RestartLogicProviderHelper.matchesRestart(
111: self, cid, dest);
112: }
113: return false;
114: }
115: };
116: Enumeration en = rootplan.searchBlackboard(pred);
117: while (en.hasMoreElements()) {
118: TransferableTransfer tt = (TransferableTransfer) en
119: .nextElement();
120: MessageAddress dest = ((Organization) tt.getAsset())
121: .getMessageAddress();
122: processTransferableTransferAdded(tt, dest);
123: }
124: pred = new UnaryPredicate() {
125: public boolean execute(Object o) {
126: if (o instanceof Transferable) {
127: Transferable transferable = (Transferable) o;
128: // we can't use "isFrom(..)", since we'll later need the
129: // specific destination if we want to send a verification
130: MessageAddress dest = transferable.getSource();
131: return RestartLogicProviderHelper.matchesRestart(
132: self, cid, dest);
133: }
134: return false;
135: }
136: };
137: for (en = rootplan.searchBlackboard(pred); en.hasMoreElements();) {
138: Transferable transferable = (Transferable) en.nextElement();
139: NewTransferableVerification nav = ldmf
140: .newTransferableVerification(transferable);
141: nav.setSource(self);
142: nav.setDestination(transferable.getSource());
143: rootplan.sendDirective(nav);
144: }
145: }
146:
147: private void processTransferableTransferAdded(
148: TransferableTransfer tt, MessageAddress dest) {
149: // create an TransferableAssignment task
150: NewTransferableAssignment nta = ldmf
151: .newTransferableAssignment();
152: nta.setTransferable(tt.getTransferable());
153: nta.setDestination(dest);
154:
155: // Give the directive to the blackboard for tranmission
156: rootplan.sendDirective(nta);
157: }
158:
159: private void processTransferableTransferRemoved(
160: TransferableTransfer tt, MessageAddress dest) {
161: // create an TransferableRescind task
162: NewTransferableRescind ntr = ldmf.newTransferableRescind();
163: ntr.setTransferableUID(tt.getTransferable().getUID());
164: ntr.setDestination(dest);
165:
166: // Give the directive to the blackboard for tranmission
167: rootplan.sendDirective(ntr);
168: }
169: }
|