001: /*
002:
003: * <copyright>
004: *
005: * Copyright 2002-2007 BBNT Solutions, LLC
006: * under sponsorship of the Defense Advanced Research Projects
007: * Agency (DARPA).
008: *
009: * You can redistribute this software and/or modify it under the
010: * terms of the Cougaar Open Source License as published on the
011: * Cougaar Open Source Website (www.cougaar.org).
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
014: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
015: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
016: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
017: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
018: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
019: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
020: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
021: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
022: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
023: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
024: *
025: * </copyright>
026:
027: */
028:
029: package org.cougaar.qos.qrs;
030:
031: /**
032: * A sample ResourceContext which looks for ip-flow capacity data on any feed,
033: * by using a Remos-style key with an IntegraterDS. The available formulas are
034: * 'CapacityMax' and 'CapacityUnused'.
035: */
036: public class SiteFlowDS extends ResourceContext {
037: static void register() {
038: ContextInstantiater cinst = new AbstractContextInstantiater() {
039: public ResourceContext instantiateContext(
040: String[] parameters, ResourceContext parent)
041: throws ResourceContext.ParameterError {
042: return new SiteFlowDS(parameters, parent);
043: }
044:
045: };
046: registerContextInstantiater("SiteFlow", cinst);
047: }
048:
049: private static final String SOURCE = "source";
050: private static final String DESTINATION = "destination";
051:
052: // Can be the first element in a path. They have no parent or
053: // context other than the root.
054: protected ResourceContext preferredParent(RSS root) {
055: return root;
056: }
057:
058: protected DataFormula instantiateFormula(String kind) {
059: if (kind.equals("CapacityMax")) {
060: return new CapacityMax();
061: } else if (kind.equals("CapacityUnused")) {
062: return new CapacityUnused();
063: } else {
064: return null;
065: }
066: }
067:
068: /**
069: * The parameters should contain two strings, the source and destination
070: * Sites of the flow.
071: */
072: protected void verifyParameters(String[] parameters)
073: throws ParameterError {
074: // should be two strings (ip addresses)
075: if (parameters == null || parameters.length != 2) {
076: throw new ParameterError("SiteFlowDS ...");
077: } else {
078: bindSymbolValue(SOURCE, parameters[0]);
079: bindSymbolValue(DESTINATION, parameters[1]);
080: }
081: }
082:
083: private SiteFlowDS(String[] parameters, ResourceContext parent)
084: throws ParameterError {
085: super (parameters, parent);
086: }
087:
088: abstract static class Formula extends DataFormula implements
089: Constants {
090: abstract String getKey();
091:
092: protected void initialize(ResourceContext context) {
093: super .initialize(context);
094:
095: String destination = (String) context.getValue(DESTINATION);
096: String source = (String) context.getValue(SOURCE);
097: String formulaKey = getKey();
098:
099: String key = "Site" + KEY_SEPR + "Flow" + KEY_SEPR + source
100: + KEY_SEPR + destination + KEY_SEPR + formulaKey;
101: String[] parameters = { key };
102: ResourceContext dependency = RSS.instance().resolveSpec(
103: "Integrater", parameters);
104: registerDependency(dependency, "Formula", "Forward");
105:
106: String rkey = "Site" + KEY_SEPR + "Flow" + KEY_SEPR
107: + destination + KEY_SEPR + source + KEY_SEPR
108: + formulaKey;
109: String[] rparameters = { rkey };
110: ResourceContext rdependency = RSS.instance().resolveSpec(
111: "Integrater", rparameters);
112: registerDependency(rdependency, "Formula", "Reverse");
113:
114: }
115:
116: }
117:
118: public static class CapacityMax extends Formula {
119:
120: private DataValue[] values;
121:
122: protected void initialize(ResourceContext context) {
123: super .initialize(context);
124: values = new DataValue[3];
125: values[0] = new DataValue(10000, DEFAULT_CREDIBILITY);
126: }
127:
128: String getKey() {
129: return "Capacity" + KEY_SEPR + "Max";
130: }
131:
132: protected DataValue doCalculation(DataFormula.Values values) {
133: this .values[1] = values.get("Reverse");
134: this .values[2] = values.get("Forward");
135: return DataValue.maxCredibility(this .values);
136: }
137:
138: }
139:
140: public static class CapacityUnused extends Formula {
141:
142: private DataValue[] values;
143:
144: protected void initialize(ResourceContext context) {
145: super .initialize(context);
146: values = new DataValue[3];
147: // default is the CapacityMax from the same context
148: registerDependency(context, "CapacityMax");
149: }
150:
151: String getKey() {
152: return "Capacity" + KEY_SEPR + "Unused";
153: }
154:
155: protected DataValue doCalculation(DataFormula.Values values) {
156: this .values[0] = values.get("CapacityMax");
157: this .values[1] = values.get("Reverse");
158: this .values[2] = values.get("Forward");
159: return DataValue.maxCredibility(this.values);
160: }
161:
162: }
163:
164: }
|