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:
031: import org.cougaar.core.blackboard.Directive;
032: import org.cougaar.core.domain.LogicProvider;
033: import org.cougaar.core.domain.MessageLogicProvider;
034: import org.cougaar.core.domain.RootPlan;
035: import org.cougaar.core.util.UID;
036: import org.cougaar.planning.ldm.PlanningFactory;
037: import org.cougaar.planning.ldm.plan.NewTransferableRescind;
038: import org.cougaar.planning.ldm.plan.Transferable;
039: import org.cougaar.planning.ldm.plan.TransferableAssignment;
040: import org.cougaar.planning.ldm.plan.TransferableRescind;
041: import org.cougaar.planning.ldm.plan.TransferableVerification;
042: import org.cougaar.util.UnaryPredicate;
043:
044: /**
045: * ReceiveTransferableLP Adds or modifies transferables to agents.
046: *
047: **/
048:
049: public class ReceiveTransferableLP implements LogicProvider,
050: MessageLogicProvider {
051:
052: private final RootPlan rootplan;
053: private final PlanningFactory ldmf;
054:
055: public ReceiveTransferableLP(RootPlan rootplan, PlanningFactory ldmf) {
056: this .rootplan = rootplan;
057: this .ldmf = ldmf;
058: }
059:
060: public void init() {
061: }
062:
063: private static UnaryPredicate makeTp(final Transferable t) {
064: return new UnaryPredicate() {
065: public boolean execute(Object o) {
066: return (o instanceof Transferable)
067: && ((Transferable) o).same(t);
068: }
069: };
070: }
071:
072: /**
073: * Adds/removes Transferables to/from LogPlan... Side-effect = other subscribers
074: * also updated.
075: **/
076: public void execute(Directive dir, Collection changes) {
077: if (dir instanceof TransferableAssignment) {
078: processTransferableAssignment((TransferableAssignment) dir,
079: changes);
080: } else if (dir instanceof TransferableRescind) {
081: processTransferableRescind((TransferableRescind) dir,
082: changes);
083: } else if (dir instanceof TransferableVerification) {
084: processTransferableVerification(
085: (TransferableVerification) dir, changes);
086: }
087: }
088:
089: private void processTransferableRescind(TransferableRescind tr,
090: Collection changes) {
091: final UID uid = tr.getTransferableUID();
092: Transferable t = (Transferable) rootplan.findUniqueObject(uid);
093: if (t != null)
094: rootplan.remove(t);
095: }
096:
097: private void processTransferableVerification(
098: TransferableVerification tv, Collection changes) {
099: final UID uid = tv.getTransferableUID();
100: if (rootplan.findUniqueObject(uid) == null) {
101: // create and send a TransferableRescind task
102: NewTransferableRescind ntr = ldmf.newTransferableRescind();
103: ntr.setTransferableUID(uid);
104: ntr.setDestination(tv.getSource());
105: rootplan.sendDirective(ntr);
106: }
107: }
108:
109: private void processTransferableAssignment(
110: TransferableAssignment ta, Collection changes) {
111: Transferable tat = (Transferable) ta.getTransferable();
112: Transferable t = (Transferable) rootplan.findUniqueObject(tat
113: .getUID());
114: try {
115: if (t != null) {
116: // shouldn't really need the clone...
117: Transferable tatc = (Transferable) tat.clone();
118: t.setAll(tatc); // local slots
119: reconcile(t, tatc); // may do fancy things with various objects
120: rootplan.change(t, changes);
121: } else {
122: Transferable tatc = (Transferable) tat.clone();
123: rootplan.add(tatc);
124: reconcile(null, tatc);
125: }
126: } catch (Exception excep) {
127: excep.printStackTrace();
128: }
129: }
130:
131: // we should really break this out into an abstract api, but it
132: // scares me to put logprovider code into object like Oplan.
133: /** Reconcile any component state of the blackboard with regard to
134: * @param t Old version of transferrable from blackboard. May be null.
135: * @param pt Putative new version of the transferrable.
136: **/
137: private void reconcile(Transferable t, Transferable pt) {
138: }
139: }
|