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 org.cougaar.core.qos.metrics.Metric;
030: import org.cougaar.core.qos.metrics.MetricsService;
031: import org.cougaar.core.qos.metrics.VariableEvaluator;
032:
033: public abstract class MetricsCondition implements Condition {
034: public static final java.lang.String METRICS_PREFIX = "metrics:";
035: public static final java.lang.String METRICS_DOUBLE_PREFIX = METRICS_PREFIX
036: + "double:";
037: public static final java.lang.String METRICS_STRING_PREFIX = METRICS_PREFIX
038: + "string:";
039: public static final java.lang.String METRICS_INTEGER_PREFIX = METRICS_PREFIX
040: + "int:";
041: public static final java.lang.String METRICS_LONG_PREFIX = METRICS_PREFIX
042: + "long:";
043: public static final java.lang.String METRICS_BOOLEAN_PREFIX = METRICS_PREFIX
044: + "boolean:";
045:
046: private static final OMCRangeList ALL_BOOLEAN_RANGE_LIST = new OMCRangeList(
047: new int[] { 0, 1 });
048:
049: private static final java.lang.Integer ONE = new java.lang.Integer(
050: 1);
051: private static final java.lang.Integer ZERO = new java.lang.Integer(
052: 0);
053:
054: public static MetricsCondition create(java.lang.String name,
055: MetricsService metricsService,
056: VariableEvaluator variableEvaluator) {
057: if (name.startsWith(METRICS_DOUBLE_PREFIX)) {
058: return new Double(name, metricsService, variableEvaluator);
059: }
060: if (name.startsWith(METRICS_STRING_PREFIX)) {
061: return new String(name, metricsService, variableEvaluator);
062: }
063: if (name.startsWith(METRICS_INTEGER_PREFIX)) {
064: return new Integer(name, metricsService, variableEvaluator);
065: }
066: if (name.startsWith(METRICS_BOOLEAN_PREFIX)) {
067: return new Boolean(name, metricsService, variableEvaluator);
068: }
069: throw new IllegalArgumentException(
070: "Unknown MetricsCondition type: " + name);
071: }
072:
073: private java.lang.String name;
074: private OMCRangeList allowedValues;
075: private java.lang.String metricsPath;
076: private MetricsService metricsService;
077: private VariableEvaluator variableEvaluator;
078:
079: protected MetricsCondition(java.lang.String name,
080: MetricsService metricsService,
081: VariableEvaluator variableEvaluator,
082: OMCRangeList allowedValues, java.lang.String prefix) {
083: this .metricsService = metricsService;
084: this .variableEvaluator = variableEvaluator;
085: this .name = name;
086: this .allowedValues = allowedValues;
087: metricsPath = name.substring(prefix.length());
088: Metric testMetric = getMetric();
089: if (testMetric == null) {
090: throw new IllegalArgumentException("Metric has no value:"
091: + metricsPath);
092: }
093: }
094:
095: public java.lang.String toString() {
096: return "MetricsCondition(" + getName() + ")";
097: }
098:
099: protected Metric getMetric() {
100: return metricsService.getValue(metricsPath, variableEvaluator);
101: }
102:
103: public java.lang.String getName() {
104: return name;
105: }
106:
107: public OMCRangeList getAllowedValues() {
108: return allowedValues;
109: }
110:
111: public abstract Comparable getValue();
112:
113: public static class Double extends MetricsCondition {
114: public Double(java.lang.String name,
115: MetricsService metricsService,
116: VariableEvaluator variableEvaluator) {
117: super (name, metricsService, variableEvaluator,
118: OMCRangeList.ALL_DOUBLE_RANGE_LIST,
119: METRICS_DOUBLE_PREFIX);
120: }
121:
122: protected Double(java.lang.String name,
123: MetricsService metricsService,
124: VariableEvaluator variableEvaluator,
125: java.lang.String prefix) {
126: super (name, metricsService, variableEvaluator,
127: OMCRangeList.ALL_DOUBLE_RANGE_LIST, prefix);
128: }
129:
130: public Comparable getValue() {
131: Metric metric = getMetric();
132: Object rawValue = metric.getRawValue();
133: if (rawValue instanceof java.lang.Double)
134: return (java.lang.Double) rawValue;
135: return new java.lang.Double(metric.doubleValue());
136: }
137: }
138:
139: public static class Default extends Double {
140: public Default(java.lang.String name,
141: MetricsService metricsService,
142: VariableEvaluator variableEvaluator) {
143: super (name, metricsService, variableEvaluator,
144: METRICS_DOUBLE_PREFIX);
145: }
146: }
147:
148: public static class Integer extends MetricsCondition {
149: public Integer(java.lang.String name,
150: MetricsService metricsService,
151: VariableEvaluator variableEvaluator) {
152: super (name, metricsService, variableEvaluator,
153: OMCRangeList.ALL_INTEGER_RANGE_LIST,
154: METRICS_INTEGER_PREFIX);
155: }
156:
157: public Comparable getValue() {
158: Metric metric = getMetric();
159: Object rawValue = metric.getRawValue();
160: if (rawValue instanceof java.lang.Integer)
161: return (java.lang.Integer) rawValue;
162: return new java.lang.Integer(metric.intValue());
163: }
164: }
165:
166: public static class Long extends MetricsCondition {
167: public Long(java.lang.String name,
168: MetricsService metricsService,
169: VariableEvaluator variableEvaluator) {
170: super (name, metricsService, variableEvaluator,
171: OMCRangeList.ALL_LONG_RANGE_LIST,
172: METRICS_LONG_PREFIX);
173: }
174:
175: public Comparable getValue() {
176: Metric metric = getMetric();
177: Object rawValue = metric.getRawValue();
178: if (rawValue instanceof java.lang.Long)
179: return (java.lang.Long) rawValue;
180: return new java.lang.Long(metric.longValue());
181: }
182: }
183:
184: public static class String extends MetricsCondition {
185: public String(java.lang.String name,
186: MetricsService metricsService,
187: VariableEvaluator variableEvaluator) {
188: super (name, metricsService, variableEvaluator,
189: OMCRangeList.ALL_STRING_RANGE_LIST,
190: METRICS_STRING_PREFIX);
191: }
192:
193: public Comparable getValue() {
194: Metric metric = getMetric();
195: Object rawValue = metric.getRawValue();
196: if (rawValue instanceof java.lang.String)
197: return (java.lang.String) rawValue;
198: return metric.stringValue();
199: }
200: }
201:
202: public static class Boolean extends MetricsCondition {
203: public Boolean(java.lang.String name,
204: MetricsService metricsService,
205: VariableEvaluator variableEvaluator) {
206: super (name, metricsService, variableEvaluator,
207: ALL_BOOLEAN_RANGE_LIST, METRICS_BOOLEAN_PREFIX);
208: }
209:
210: public Comparable getValue() {
211: Metric metric = getMetric();
212: if (metric.booleanValue()) {
213: return ONE;
214: } else {
215: return ZERO;
216: }
217: }
218: }
219: }
|