001: /*
002: * <copyright>
003: *
004: * Copyright 2000-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.society.cdata;
027:
028: import org.cougaar.tools.csmart.core.cdata.ComponentData;
029: import org.cougaar.tools.csmart.society.AgentComponent;
030: import org.cougaar.tools.csmart.society.SocietyBase;
031:
032: import java.util.ArrayList;
033:
034: /**
035: * The class for creating a configurable component that represents
036: * a society from component data.
037: */
038: public class SocietyCDataComponent extends SocietyBase {
039: ComponentData cdata;
040:
041: protected static final String DESCRIPTION_RESOURCE_NAME = "/org/cougaar/tools/csmart/society/society-base-description.html";
042: protected static final String BACKUP_DESCRIPTION = "A Society: Agents, Binders, Plugins, etc.";
043:
044: public SocietyCDataComponent(ComponentData cdata, String assemblyId) {
045: super (cdata.getName());
046: this .cdata = cdata;
047: this .oldAssemblyId = assemblyId;
048: }
049:
050: public void initProperties() {
051: // create society properties from cdata
052: // create agents from cdata
053: ArrayList agentData = new ArrayList();
054: ArrayList alldata = new ArrayList();
055: if (cdata != null)
056: alldata.add(cdata);
057:
058: // FIXME: It'd be nice to deal with binders of Agents in here!!!
059:
060: // Find all the agents
061: for (int i = 0; i < alldata.size(); i++) {
062: ComponentData someData = (ComponentData) alldata.get(i);
063: // if (log.isDebugEnabled())
064: // log.debug("initProps: alldata(" + i + ")= " + someData);
065: String type = someData.getType();
066: // if (log.isDebugEnabled())
067: // log.debug("type = " + type);
068: if (type.equals(ComponentData.AGENT)) {
069: // if (log.isDebugEnabled())
070: // log.debug("adding to list of agents if not already there");
071:
072: // Do not add the same Agent to this society twice.
073: // This should not be necessary if the incoming
074: // CDATA is correctly created - usu from
075: // Experiment.getSocietyComponentData()
076: // If we dont do this, ConfigurableComponent.addChild, when
077: // we create an AgentCDataComponent for this and
078: // add it to this society, will rename the Agent
079: // for us quietly
080: if (!agentData.contains(someData))
081: agentData.add(someData);
082:
083: } else if (type.equals(ComponentData.NODE)
084: || type.equals(ComponentData.HOST)
085: || type.equals(ComponentData.SOCIETY)) {
086: ComponentData[] moreData = someData.getChildren();
087: for (int j = 0; j < moreData.length; j++)
088: alldata.add(moreData[j]);
089: }
090: }
091:
092: if (log.isDebugEnabled())
093: log.debug("initProps found " + agentData.size()
094: + " agents to add");
095:
096: // For each agent, create a component and add it as a child
097: for (int i = 0; i < agentData.size(); i++) {
098: // if (log.isDebugEnabled())
099: // log.debug("initProps had CDATA(" + i + "): " + (ComponentData)agentData.get(i));
100: AgentComponent agentComponent = new AgentCDataComponent(
101: (ComponentData) agentData.get(i));
102: agentComponent.initProperties();
103: // if (log.isDebugEnabled())
104: // log.debug("... which after initProps produced AgentCDataComponent " + agentComponent.getFullName().toString());
105: addChild(agentComponent);
106: // if (log.isDebugEnabled())
107: // log.debug("... and which after addChild is " + agentComponent.getFullName().toString());
108: }
109: }
110:
111: }
|