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.planning.plugin.adaptivity;
028:
029: import org.cougaar.core.adaptivity.OMCRangeList;
030: import org.cougaar.core.adaptivity.SensorCondition;
031: import org.cougaar.core.blackboard.IncrementalSubscription;
032: import org.cougaar.core.component.ServiceBroker;
033: import org.cougaar.core.persist.NotPersistable;
034: import org.cougaar.core.plugin.ServiceUserPlugin;
035: import org.cougaar.core.service.ConditionService;
036: import org.cougaar.planning.ldm.plan.Task;
037: import org.cougaar.util.UnaryPredicate;
038:
039: /**
040: * Plugin to sense incoming task rate and publish a Condition
041: **/
042: public class TaskRateSensorPlugin extends ServiceUserPlugin {
043: private static final String CONDITION_NAME = "TaskRateSensorPlugin.TASKRATE";
044:
045: private static final OMCRangeList TASKRATE_VALUES = new OMCRangeList(
046: new Double(0.0), new Double(Double.MAX_VALUE));
047:
048: private static final double TIME_CONSTANT = 5000.0; // Five second time constant
049:
050: private ConditionService conditionService;
051:
052: private double filteredTaskRate = 0.0;
053: private long then = System.currentTimeMillis();
054: private IncrementalSubscription tasksSubscription;
055: private UnaryPredicate tasksPredicate = new UnaryPredicate() {
056: public boolean execute(Object o) {
057: if (o instanceof Task) {
058: return true;
059: }
060: return false;
061: }
062: };
063:
064: /**
065: * Private inner class precludes use by others to set our
066: * measurement. Others can only reference the base Condition
067: * class which has no setter method.
068: **/
069: private static class TaskRateTestCondition extends SensorCondition
070: implements NotPersistable {
071: public TaskRateTestCondition(String name,
072: OMCRangeList allowedValues, Comparable value) {
073: super (name, allowedValues, value);
074: }
075:
076: public void setValue(Comparable newValue) {
077: super .setValue(newValue);
078: }
079: }
080:
081: private static final Class[] requiredServices = { ConditionService.class };
082:
083: public TaskRateSensorPlugin() {
084: super (requiredServices);
085: }
086:
087: public void setupSubscriptions() {
088: TaskRateTestCondition taskRate = new TaskRateTestCondition(
089: CONDITION_NAME, TASKRATE_VALUES, new Double(0.0));
090: blackboard.publishAdd(taskRate);
091: tasksSubscription = (IncrementalSubscription) blackboard
092: .subscribe(tasksPredicate);
093: if (haveServices())
094: updateTaskRateSensor(true);
095: }
096:
097: /**
098: * Test if all needed services have been acquired. Test the
099: * conditionService variable for null. If still null ask
100: * acquireServices to continue trying to acquire services. If true
101: * is returned, fill in the service variables and return true.
102: * Subsequent calls will return true immediately.
103: **/
104: private boolean haveServices() {
105: if (conditionService != null)
106: return true;
107: if (acquireServices()) {
108: ServiceBroker sb = getServiceBroker();
109: conditionService = (ConditionService) sb.getService(this ,
110: ConditionService.class, null);
111: return true;
112: }
113: return false;
114: }
115:
116: public void execute() {
117: if (haveServices()) {
118: updateTaskRateSensor(timerExpired());
119: }
120: }
121:
122: private void updateTaskRateSensor(boolean publish) {
123: long now = System.currentTimeMillis();
124: long elapsed = now - then;
125: int newCount = tasksSubscription.getAddedCollection().size()
126: + tasksSubscription.getChangedCollection().size()
127: + tasksSubscription.getRemovedCollection().size();
128: then = now;
129: filteredTaskRate /= Math.exp(elapsed / TIME_CONSTANT);
130: filteredTaskRate += newCount;
131: if (publish) {
132: cancelTimer();
133: if (logger.isDebugEnabled())
134: logger.debug("newCount=" + newCount);
135: TaskRateTestCondition taskRate = (TaskRateTestCondition) conditionService
136: .getConditionByName(CONDITION_NAME);
137: if (taskRate != null) {
138: if (logger.isInfoEnabled())
139: logger.info("Setting " + CONDITION_NAME + " = "
140: + filteredTaskRate);
141: taskRate.setValue(new Double(filteredTaskRate));
142: blackboard.publishChange(taskRate);
143: }
144: resetTimer(2000);
145: }
146: }
147: }
|