001: /*
002: * <copyright>
003: *
004: * Copyright 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: package org.cougaar.planning.plugin.deletion;
027:
028: import org.cougaar.core.plugin.deletion.*;
029: import org.cougaar.planning.ldm.policy.IntegerRuleParameter;
030: import org.cougaar.planning.ldm.policy.LongRuleParameter;
031: import org.cougaar.planning.ldm.policy.Policy;
032: import org.cougaar.planning.ldm.policy.PredicateRuleParameter;
033: import org.cougaar.planning.ldm.policy.RuleParameter;
034: import org.cougaar.planning.ldm.policy.RuleParameterIllegalValueException;
035: import org.cougaar.util.UnaryPredicate;
036:
037: public class DeletionPolicyBase extends Policy implements
038: DeletionPolicy {
039: // Cached parameter values
040: private UnaryPredicate thePredicate;
041: private long theDeletionDelay;
042: private int thePriority;
043: public static final String PREDICATE_KEY = "predicate";
044: public static final String DELETION_DELAY_KEY = "deletionDelay";
045: public static final String PRIORITY_KEY = "priority";
046:
047: public DeletionPolicyBase() {
048: }
049:
050: public void init(UnaryPredicate aPredicate, long deletionDelay) {
051: init(null, aPredicate, deletionDelay, 0);
052: }
053:
054: public void init(UnaryPredicate aPredicate, long deletionDelay,
055: int priority) {
056: init(null, aPredicate, deletionDelay, priority);
057: }
058:
059: public void init(String aName, UnaryPredicate aPredicate,
060: long deletionDelay) {
061: init(aName, aPredicate, deletionDelay, 0);
062: }
063:
064: /**
065: * Initialize this policy.
066: * @param aName a name for this policy used in printouts and for debugging
067: * @param aPredicate A predicate for selecting Tasks for which this
068: * policy applies
069: * @param deletionDelay The age the tasks must reach before
070: * being deleted
071: * @param priority When multiple policies apply to a task, the
072: * highest priority policy wins and the rest are ignored.
073: **/
074: public void init(String aName, UnaryPredicate aPredicate,
075: long deletionDelay, int priority) {
076: setName(aName);
077: try {
078: Add(new PredicateRuleParameter(PREDICATE_KEY, aPredicate));
079: Add(new IntegerRuleParameter(PRIORITY_KEY, MIN_PRIORITY,
080: MAX_PRIORITY, priority));
081: Add(new LongRuleParameter(DELETION_DELAY_KEY, 0L,
082: Long.MAX_VALUE, deletionDelay));
083: } catch (RuleParameterIllegalValueException e) {
084: // No way this should happen because x <= x <= x is never false;
085: e.printStackTrace();
086: }
087: }
088:
089: public void Add(RuleParameter param) {
090: clearCache();
091: super .Add(param);
092: }
093:
094: public void clearCache() {
095: thePredicate = null;
096: theDeletionDelay = NO_DELETION_DELAY;
097: thePriority = NO_PRIORITY;
098: }
099:
100: public UnaryPredicate getPredicate() {
101: if (thePredicate == null) {
102: PredicateRuleParameter prp = (PredicateRuleParameter) Lookup(PREDICATE_KEY);
103: thePredicate = prp.getPredicate();
104: }
105: return thePredicate;
106: }
107:
108: public void setDeletionDelay(long deletionDelay) {
109: try {
110: theDeletionDelay = deletionDelay;
111: Replace(new LongRuleParameter(DELETION_DELAY_KEY,
112: deletionDelay, deletionDelay, deletionDelay));
113: } catch (RuleParameterIllegalValueException e) {
114: // No way this should happen because x <= x <= x is never false;
115: e.printStackTrace();
116: }
117: }
118:
119: public long getDeletionDelay() {
120: if (theDeletionDelay == NO_DELETION_DELAY) {
121: theDeletionDelay = ((LongRuleParameter) Lookup(DELETION_DELAY_KEY))
122: .longValue();
123: }
124: return theDeletionDelay;
125: }
126:
127: public int getPriority() {
128: if (thePriority == NO_PRIORITY) {
129: thePriority = ((IntegerRuleParameter) Lookup(PRIORITY_KEY))
130: .intValue();
131: }
132: return thePriority;
133: }
134: }
|