001: /*
002: * <copyright>
003: *
004: * Copyright 2002-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.core.adaptivity;
028:
029: import java.util.Collections;
030: import java.util.Set;
031:
032: import org.cougaar.core.mts.MessageAddress;
033: import org.cougaar.core.relay.Relay;
034: import org.cougaar.core.util.UID;
035: import org.cougaar.util.log.Logger;
036: import org.cougaar.util.log.Logging;
037:
038: /**
039: * A remotely-controlled OperatingModePolicy. Allows a policy manager in one
040: * agent to control a Policy of the in another. It is instantiated in
041: * the controlling agent and transferred
042: * to the controlled agent using the Relay logic providers. A copy
043: * of the instance is published in the controlled agent's blackboard
044: * and used like any other OperatingModePolicy.
045: **/
046: public class InterAgentOperatingModePolicy extends OperatingModePolicy
047: implements Relay.Source, Relay.Target, java.io.Serializable {
048: // this can't be transient like other relays, cause not storing
049: // target separately
050: private Set targets = Collections.EMPTY_SET;
051: protected MessageAddress source;
052: protected Relay.Token owner;
053:
054: // Constructors
055: public InterAgentOperatingModePolicy(PolicyKernel pk) {
056: super (pk);
057: }
058:
059: public InterAgentOperatingModePolicy(ConstrainingClause ifClause,
060: ConstraintPhrase[] omConstraints) {
061: super (ifClause, omConstraints);
062: }
063:
064: public InterAgentOperatingModePolicy(String name,
065: ConstrainingClause ifClause,
066: ConstraintPhrase[] omConstraints, String authority) {
067: super (name, ifClause, omConstraints, authority);
068: }
069:
070: protected InterAgentOperatingModePolicy(
071: InterAgentOperatingModePolicy other, MessageAddress src,
072: Relay.Token owner) {
073: this (other.getName(), other.getIfClause(), other
074: .getOperatingModeConstraints(), other.getAuthority());
075: setUID(other.getUID());
076: this .owner = owner;
077: this .source = src;
078: }
079:
080: // Initialization methods
081: /**
082: * Set the message address of the target. This implementation
083: * presumes that there is but one target.
084: * @param targetAddress the address of the target agent.
085: **/
086: public void setTarget(MessageAddress targetAddress) {
087: targets = Collections.singleton(targetAddress);
088: }
089:
090: /**
091: * appliesToThisAgent - return true if InterAgentOperatingModePolicy applies to
092: * this Agent.
093: * Default behaviour is to assume that InterAgentOperatingModePolicy does not
094: * apply to the originating Agent.
095: */
096: public boolean appliesToThisAgent() {
097: // Policy only valid is it did not originate here
098: return (getSource() != null);
099: }
100:
101: // Relay.Source interface
102: /**
103: * Get all the addresses of the target agents to which this Relay
104: * should be sent. For this implementation this is always a
105: * singleton set contain just one target.
106: **/
107: public Set getTargets() {
108: return targets;
109: }
110:
111: /**
112: * Get an object representing the value of this Relay suitable
113: * for transmission. This implementation uses itself to represent
114: * its Content.
115: **/
116: public Object getContent() {
117: return this ;
118: }
119:
120: /**
121: * @return a factory to convert the content to a Relay Target.
122: **/
123: public Relay.TargetFactory getTargetFactory() {
124: return InterAgentPolicyFactory.INSTANCE;
125: }
126:
127: /**
128: * Set the response that was sent from a target. For LP use only.
129: * This implemenation does nothing because responses are not needed
130: * or used.
131: **/
132: public int updateResponse(MessageAddress target, Object response) {
133: // No response expected
134: return Relay.NO_CHANGE;
135: }
136:
137: // Relay.Target implementation
138: /**
139: * Get the address of the Agent holding the Source copy of
140: * this Relay.
141: **/
142: public MessageAddress getSource() {
143: return source;
144: }
145:
146: /**
147: * Get the current response for this target. Null indicates that
148: * this target has no response. This implementation never has a
149: * response so it always returns null.
150: **/
151: public Object getResponse() {
152: return null;
153: }
154:
155: /**
156: * Update with new content. The only part of the source content used
157: * is the value of the operating mode.
158: * @return true if the update changed the Relay. The LP should
159: * publishChange the Relay. This implementation returns true only
160: * if the new value differs from the current value.
161: **/
162: public int updateContent(Object content, Relay.Token token) {
163: if (token != owner) {
164: Logger logger = Logging.getLogger(getClass());
165: if (logger.isInfoEnabled()) {
166: logger
167: .info("Ignoring \"Not owner\" bug in \"updateContent()\","
168: + " possibly a rehydration bug (token="
169: + token + ", owner=" + owner + ")");
170: }
171: }
172: InterAgentOperatingModePolicy newOMP = (InterAgentOperatingModePolicy) content;
173: // brute force, no brains
174: setPolicyKernel(newOMP.getPolicyKernel());
175: return Relay.CONTENT_CHANGE;
176: }
177:
178: /**
179: * This factory creates a new InterAgentOperatingModePolicy.
180: **/
181: private static class InterAgentPolicyFactory implements
182: Relay.TargetFactory, java.io.Serializable {
183: public static final InterAgentPolicyFactory INSTANCE = new InterAgentPolicyFactory();
184:
185: private InterAgentPolicyFactory() {
186: }
187:
188: public Relay.Target create(UID uid, MessageAddress source,
189: Object content, Relay.Token owner) {
190: InterAgentOperatingModePolicy iaomp = (InterAgentOperatingModePolicy) content;
191: return new InterAgentOperatingModePolicy(iaomp, source,
192: owner);
193: }
194:
195: private Object readResolve() {
196: return INSTANCE;
197: }
198: }
199: }
|