001: /*
002: * <copyright>
003: *
004: * Copyright 2001-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.tools.csmart.recipe;
027:
028: import org.cougaar.core.agent.SimpleAgent;
029: import org.cougaar.tools.csmart.core.cdata.AgentComponentData;
030: import org.cougaar.tools.csmart.core.cdata.ComponentData;
031: import org.cougaar.tools.csmart.core.cdata.GenericComponentData;
032: import org.cougaar.tools.csmart.core.db.PopulateDb;
033: import org.cougaar.tools.csmart.core.property.ConfigurableComponent;
034: import org.cougaar.tools.csmart.society.AgentComponent;
035: import org.cougaar.util.log.Logger;
036:
037: import java.io.Serializable;
038: import java.net.URL;
039: import java.util.Collection;
040: import java.util.Iterator;
041:
042: /**
043: * Recipe to add a complete Agent to the society. This agent
044: * consists of all required plugins relationships and asset data.
045: */
046: public class CompleteAgentRecipe extends ComplexRecipeBase implements
047: Serializable {
048: protected static String DESCRIPTION_RESOURCE_NAME = "complete-agent-recipe-description.html";
049:
050: /**
051: * Creates a new <code>CompleteAgentRecipe</code> instance.
052: *
053: */
054: public CompleteAgentRecipe() {
055: this ("Agent Component Recipe");
056: }
057:
058: /**
059: * Creates a new <code>CompleteAgentRecipe</code> instance.
060: *
061: * @param name Name of the Component
062: */
063: public CompleteAgentRecipe(String name) {
064: super (name);
065: }
066:
067: public CompleteAgentRecipe(String name, String assemblyId) {
068: super (name, assemblyId);
069: }
070:
071: public CompleteAgentRecipe(String name, String assemblyId,
072: String recipeId) {
073: super (name, assemblyId, recipeId);
074:
075: // recipeId is not needed for this recipe, however the method sig is needed
076: // so OrganizerHelper can find the class correctly using reflection.
077: }
078:
079: public CompleteAgentRecipe(String name, String assemblyId,
080: String recipeId, String initName) {
081: super (name, assemblyId, recipeId, initName);
082:
083: // recipeId is not needed for this recipe, however the method sig is needed
084: // so OrganizerHelper can find the class correctly using reflection.
085: }
086:
087: public CompleteAgentRecipe(ComponentData cdata, String assemblyId) {
088: super (cdata, assemblyId);
089: }
090:
091: /**
092: * Gets the name of the html help file for this component.
093: *
094: * @return an <code>URL</code> value
095: */
096: public URL getDescription() {
097: return getClass().getResource(DESCRIPTION_RESOURCE_NAME);
098: }
099:
100: /**
101: * Adds any new data to the global <code>ComponentData</code> tree.
102: * No existing data is modified in this method.
103: * Warning: Assumes it is handed a ComponentData which contains
104: * all Agents below it.
105: *
106: * @param data Pointer to the Global <code>ComponentData</code> tree
107: * @return an updated <code>ComponentData</code> value
108: */
109: public ComponentData addComponentData(ComponentData data) {
110: ComponentData[] children = data.getChildren();
111: for (int i = 0; i < children.length; i++) {
112: ComponentData child = children[i];
113: // for each child component data, if it's an agent's component data
114: if (child.getType() == ComponentData.AGENT) {
115: // get all my agent components
116: Iterator iter = ((Collection) getDescendentsOfClass(AgentComponent.class))
117: .iterator();
118: while (iter.hasNext()) {
119: AgentComponent agent = (AgentComponent) iter.next();
120: // if the component data name matches the agent name
121: if (child.getName().equals(
122: agent.getShortName().toString())) {
123: if (log.isDebugEnabled())
124: log
125: .debug("addComponentData found my agent "
126: + child.getName()
127: + " which currently says isModified="
128: + child.isModified());
129:
130: // then set me as the owner of the component data
131: child.setOwner(this );
132: // and add the component data
133: agent.addComponentData(child);
134: }
135: }
136: } else {
137: // FIXME!! Will we support other top-level components?
138: // Process children of component data
139: addComponentData(child);
140: }
141: }
142: return data;
143: }
144:
145: /**
146: * Modifies the global <code>ComponentData</code> tree.
147: * This method is free to make any modifications it needs
148: * to the global tree, not just to it's own Component.
149: * <br>
150: * Currently, this component makes no modifications.
151: *
152: * @param data Pointer to the global <code>ComponentData</code>
153: * @param pdb Access to the database via <code>PopulateDb</code> object
154: * @return a modified <code>ComponentData</code> value
155: */
156: public ComponentData modifyComponentData(ComponentData data,
157: PopulateDb pdb) {
158: return data;
159: }
160:
161: protected ComponentData getComponentData() {
162: ComponentData cd = new GenericComponentData();
163: cd.setType(ComponentData.RECIPE);
164: cd.setClassName(RECIPE_CLASS);
165: cd.setName(getRecipeName());
166: cd.setOwner(null);
167: cd.setParent(null);
168:
169: AgentComponent[] agents = getAgents();
170: for (int i = 0; i < agents.length; i++) {
171: generateAgentComponentData(agents[i], cd, null);
172: }
173:
174: // Now let all components add their data.
175: addComponentData(cd);
176:
177: return modifyComponentData(cd);
178:
179: // FIXME: Why call this twice?
180: // return addComponentData(cd);
181: }
182:
183: private static final void generateAgentComponentData(
184: AgentComponent agent, ComponentData parent,
185: ConfigurableComponent owner) {
186:
187: AgentComponentData ac = new AgentComponentData();
188: ac.setName(agent.getShortName());
189: ac.setClassName(SimpleAgent.class.getName());
190: ac.addParameter(agent.getShortName()); // Agents have one parameter, the agent name
191: ac.setOwner(owner);
192: ac.setParent(parent);
193: parent.addChild((ComponentData) ac);
194: }
195:
196: }
|