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.planning.ldm.policy;
028:
029: import org.cougaar.util.UnaryPredicate;
030:
031: /**
032: *
033: **/
034:
035: /**
036: * An PredicateRuleParameter is a RuleParameter with a UnaryPredicate
037: * as its value. The inRange method is implemented to apply the
038: * predicate to the test object.
039: **/
040: public class PredicateRuleParameter implements RuleParameter,
041: java.io.Serializable {
042: protected String my_name;
043: protected UnaryPredicate thePredicate;
044:
045: /**
046: * Constructor sets the predicate
047: **/
048: public PredicateRuleParameter(String param_name,
049: UnaryPredicate aPredicate) {
050: my_name = param_name;
051: thePredicate = aPredicate;
052: }
053:
054: public PredicateRuleParameter() {
055: }
056:
057: /**
058: * Parameter type is PREDICATE
059: */
060: public int ParameterType() {
061: return RuleParameter.PREDICATE_PARAMETER;
062: }
063:
064: public String getName() {
065: return my_name;
066: }
067:
068: public void setName(String name) {
069: my_name = name;
070: }
071:
072: /**
073: * Get parameter value (UnaryPredicate)
074: * @return Object parameter value (UnaryPredicate). Note : could be null.
075: */
076: public Object getValue() {
077: return thePredicate;
078: }
079:
080: /**
081: * Convenience accessor not requiring casting the result
082: **/
083: public UnaryPredicate getPredicate() {
084: return thePredicate;
085: }
086:
087: /**
088: * Set parameter value
089: * @param newPredicate must be a UnaryPredicate
090: * @throws RuleParameterIllegalValueException
091: */
092: public void setValue(Object newPredicate)
093: throws RuleParameterIllegalValueException {
094: if (!(newPredicate instanceof UnaryPredicate)) {
095: throw new RuleParameterIllegalValueException(
096: RuleParameter.PREDICATE_PARAMETER,
097: "Object must be a UnaryPredicate");
098: }
099: thePredicate = (UnaryPredicate) newPredicate;
100: }
101:
102: /**
103: *
104: * @param test_value : Any object
105: * @return true if test_value is acceptable to the predicate
106: */
107: public boolean inRange(Object test_value) {
108: if (thePredicate == null)
109: return false;
110: return thePredicate.execute(test_value);
111: }
112:
113: public String toString() {
114: return "#<PREDICATE_PARAMETER : " + thePredicate.toString();
115: }
116:
117: public Object clone() {
118: return new PredicateRuleParameter(my_name, thePredicate);
119: }
120:
121: }
|