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: */package org.cougaar.tools.csmart.recipe;
026:
027: import org.cougaar.tools.csmart.core.cdata.ComponentData;
028: import org.cougaar.tools.csmart.core.cdata.GenericComponentData;
029: import org.cougaar.tools.csmart.core.db.PopulateDb;
030: import org.cougaar.tools.csmart.core.property.Property;
031:
032: import java.io.Serializable;
033: import java.net.URL;
034: import java.sql.SQLException;
035: import java.util.ArrayList;
036: import java.util.Iterator;
037: import java.util.Set;
038:
039: /**
040: * AdaptivitySupportRecipe: add standard adaptivity Engine pieces
041: * to selected Agents.
042: *
043: *
044: * Created: Wed May 22 10:22:54 2002
045: *
046: */
047: public class AdaptivitySupportRecipe extends RecipeBase implements
048: Serializable {
049:
050: private static final String DESCRIPTION_RESOURCE_NAME = "adaptivity-support-recipe-description.html";
051: private static final String BACKUP_DESCRIPTION = "AdaptivitySupportRecipe provides a method for adding Adapitivity pieces to all agents in an experiment";
052:
053: private static final String PROP_TARGET_AGENT_QUERY = "Target Agent Selection Query";
054: private static final String PROP_TARGET_AGENT_QUERY_DFLT = "recipeQueryAllAgents";
055: private static final String PROP_TARGET_AGENT_QUERY_DESC = "The query name for selecting agents to which to add adaptivity support.";
056:
057: private static final String PROP_PLAYS_FILE = "Playbook File";
058: private static final String PROP_PLAYS_FILE_DESC = "Name of single file of AE plays, default for per-Agent";
059: private static final String AGENT_VAR = "<agent>";
060: private static final String PROP_PLAYS_FILE_DFLT = AGENT_VAR
061: + "-plays.txt";
062:
063: private static final String PROP_VIEWER_SERVLET = "AEViewerServlet";
064: private static final boolean PROP_VIEWER_SERVLET_DFLT = true;
065: private static final String PROP_VIEWER_SERVLET_DESC = "Include the AEViewerServlet in the target agent(s)";
066:
067: private Property propViewerServlet;
068: private Property propPlaysFile;
069: private Property propTargetAgentQuery;
070:
071: public AdaptivitySupportRecipe() {
072: this ("AdaptivitySupportRecipe");
073: }
074:
075: public AdaptivitySupportRecipe(String name) {
076: super (name);
077: }
078:
079: public void initProperties() {
080:
081: propViewerServlet = addBooleanProperty(PROP_VIEWER_SERVLET,
082: PROP_VIEWER_SERVLET_DFLT);
083: propViewerServlet.setToolTip(PROP_VIEWER_SERVLET_DESC);
084:
085: propPlaysFile = addProperty(PROP_PLAYS_FILE,
086: PROP_PLAYS_FILE_DFLT);
087: propPlaysFile.setToolTip(PROP_PLAYS_FILE_DESC);
088:
089: propTargetAgentQuery = addRecipeQueryProperty(
090: PROP_TARGET_AGENT_QUERY, PROP_TARGET_AGENT_QUERY_DFLT);
091: propTargetAgentQuery.setToolTip(PROP_TARGET_AGENT_QUERY_DESC);
092: }
093:
094: private Property addRecipeQueryProperty(String name, String dflt) {
095: Property prop = addProperty(new RecipeQueryProperty(this , name,
096: dflt));
097: prop.setPropertyClass(String.class);
098: return prop;
099: }
100:
101: /**
102: * Gets the name of the html help file for this component.
103: *
104: * @return an <code>URL</code> value
105: */
106: public URL getDescription() {
107: return getClass().getResource(DESCRIPTION_RESOURCE_NAME);
108: }
109:
110: public String getRecipeName() {
111: return getShortName();
112: }
113:
114: public ComponentData modifyComponentData(ComponentData data,
115: PopulateDb pdb) {
116: try {
117: Set targets = pdb.executeQuery(propTargetAgentQuery
118: .getValue().toString());
119: modifyComponentData(data, pdb, targets);
120: } catch (SQLException sqle) {
121: if (log.isErrorEnabled()) {
122: log.error("Cant run agent target query", sqle);
123: }
124: }
125: return data;
126: }
127:
128: private void modifyComponentData(ComponentData data,
129: PopulateDb pdb, Set targets) throws SQLException {
130: if (targets.contains(pdb.getComponentAlibId(data))) {
131: String agent = pdb.getComponentAlibId(data);
132:
133: if (log.isDebugEnabled()) {
134: log.debug("Adding Adaptivity Support to " + agent);
135: }
136:
137: Iterator iter = getPlugins();
138: while (iter.hasNext()) {
139: String pluginName = (String) iter.next();
140: GenericComponentData plugin = new GenericComponentData();
141: plugin.setType(ComponentData.PLUGIN);
142: plugin.setClassName(pluginName);
143: plugin.setParent(data);
144: plugin.setOwner(this );
145: if (GenericComponentData.alreadyAdded(data, plugin)) {
146: if (log.isDebugEnabled()) {
147: log.debug("Not re-adding" + pluginName);
148: }
149: } else {
150: plugin.setName(GenericComponentData
151: .getSubComponentUniqueName(data, plugin));
152: data.addChildDefaultLoc(plugin);
153: }
154: }
155:
156: GenericComponentData plugin = new GenericComponentData();
157: plugin.setType(ComponentData.PLUGIN);
158: plugin
159: .setClassName("org.cougaar.core.adaptivity.PlaybookManager");
160: plugin.setOwner(this );
161: plugin.setParent(data);
162:
163: // Add the plays file parameter
164: String playsFile = null;
165: if (propPlaysFile == null
166: || propPlaysFile.getValue() == null
167: || propPlaysFile.getValue().equals("")) {
168: playsFile = PROP_PLAYS_FILE_DFLT;
169: } else {
170: playsFile = (String) propPlaysFile.getValue();
171: }
172:
173: int index = playsFile.indexOf(AGENT_VAR);
174: String param = playsFile;
175: if (index != -1)
176: param = playsFile.substring(0, index)
177: + agent
178: + playsFile.substring(index
179: + AGENT_VAR.length());
180:
181: plugin.addParameter(param);
182: // plugin.addParameter(agent + "-plays.txt");
183:
184: if (GenericComponentData.alreadyAdded(data, plugin)) {
185: if (log.isDebugEnabled()) {
186: log.debug("Not re-adding PlaybookManager");
187: }
188: } else {
189: plugin.setName(GenericComponentData
190: .getSubComponentUniqueName(data, plugin));
191: data.addChildDefaultLoc(plugin);
192: }
193:
194: if (((Boolean) (propViewerServlet.getValue()))
195: .booleanValue()) {
196: plugin = new GenericComponentData();
197: plugin.setType(ComponentData.PLUGIN);
198: plugin
199: .setClassName("org.cougaar.core.adaptivity.AEViewerServlet");
200: plugin.setOwner(this );
201: plugin.setParent(data);
202: plugin.addParameter("/aeviewer");
203: if (GenericComponentData.alreadyAdded(data, plugin)) {
204: if (log.isDebugEnabled()) {
205: log.debug("Not re-adding AEViewerServlet");
206: }
207: } else {
208: plugin.setName(GenericComponentData
209: .getSubComponentUniqueName(data, plugin));
210: data.addChildDefaultLoc(plugin);
211: }
212: }
213: }
214:
215: if (data.childCount() > 0) {
216: // for each child, call this same method.
217: ComponentData[] children = data.getChildren();
218: for (int i = 0; i < children.length; i++) {
219: // If the child is a plugins or AgentBinder, no need to look at it
220: if (children[i].getType().equals(ComponentData.PLUGIN)
221: || children[i].getType().equals(
222: ComponentData.AGENTBINDER))
223: continue;
224: modifyComponentData(children[i], pdb, targets);
225: }
226: }
227:
228: }
229:
230: private Iterator getPlugins() {
231: ArrayList list = new ArrayList();
232: list.add("org.cougaar.core.adaptivity.AdaptivityEngine");
233: list
234: .add("org.cougaar.core.adaptivity.ConditionServiceProvider");
235: list
236: .add("org.cougaar.core.adaptivity.OperatingModeServiceProvider");
237: list
238: .add("org.cougaar.core.adaptivity.OperatingModePolicyManager");
239:
240: return list.iterator();
241: }
242:
243: }// AdaptivitySupportRecipe
|