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: /**
030: * Base class for Plays and OperatingModePolicies. Most of the
031: * functionality of the subclasses resides here. A Play or
032: * OperatingModePolicy has a ConstrainingClause that can be evaluated
033: * to determine if it applies to the current Conditions and a list
034: * (array) of operating mode constraints that specify the values that
035: * the modes should be given.
036: **/
037: public class PlayBase implements java.io.Serializable {
038:
039: private ConstrainingClause ifClause;
040: private ConstraintPhrase[] operatingModeConstraints;
041:
042: /**
043: * Constructor
044: * @param ifClause the 'if' clause
045: * @param omConstraints the constraints on operating modes
046: **/
047: public PlayBase(ConstrainingClause ifClause,
048: ConstraintPhrase[] omConstraints) {
049: this .ifClause = ifClause;
050: this .operatingModeConstraints = omConstraints;
051: }
052:
053: /**
054: * Gets the if clause
055: * @return the 'if' ConstrainingClause
056: */
057: public ConstrainingClause getIfClause() {
058: return ifClause;
059: }
060:
061: /**
062: * Gets the array of ConstraintPhrases to be applied to the
063: * operating modes.
064: * @return the array of ConstraintPhrases.
065: **/
066: public ConstraintPhrase[] getOperatingModeConstraints() {
067: return operatingModeConstraints;
068: }
069:
070: /**
071: * Gets the Play or OperatingModePolicy as a String. The form of the
072: * String is approximately the same as the input to the Parser.
073: * @return The Play or OperatingModePolicy as a String.
074: **/
075: public String toString() {
076: StringBuffer buf = new StringBuffer();
077: buf.append(ifClause);
078: for (int i = 0; i < operatingModeConstraints.length; i++) {
079: buf.append(":").append(operatingModeConstraints[i]);
080: }
081: return buf.toString();
082: }
083:
084: public int hashCode() {
085: int hc = ifClause.hashCode();
086: for (int i = 0; i < this .operatingModeConstraints.length; i++) {
087: hc = 31 * hc + operatingModeConstraints[i].hashCode();
088: }
089: return hc;
090: }
091:
092: public boolean equals(Object o) {
093: if (this == o)
094: return true;
095: if (o == null)
096: return false;
097: if (this .getClass() != o.getClass())
098: return false;
099: PlayBase that = (PlayBase) o;
100: if (!this .ifClause.equals(that.ifClause))
101: return false;
102: if (this .operatingModeConstraints.length != that.operatingModeConstraints.length)
103: return false;
104: for (int i = 0; i < this .operatingModeConstraints.length; i++) {
105: if (!this .operatingModeConstraints[i]
106: .equals(that.operatingModeConstraints[i]))
107: return false;
108: }
109: return true;
110: }
111: }
|