//Copyright (c)2005 Holobloc Inc.
//Contributed to the OOONEIDA FBench project under the Common Public License.
package fbench.graph.model;
import java.util.Vector;
import fbench.graph.GraphElement;
import fbench.graph.Plug;
import fbench.graph.Socket;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* A GraphModel for an IEC 61499 FBNetwork Element.
*
* @author JHC
* @version 20050825/JHC
*/
public class FBNetwork extends GraphModel {
public FBNetwork() {
super();
}
public FBNetwork(Element el) {
super(el);
}
/**
* Adds the connections from the sub-element with the given name to the
* graph Vector.
*/
protected void addConnections(Vector graph, String conntype) {
NodeList nl = getElement().getElementsByTagName(conntype);
int n = nl.getLength();
if (n > 0) {
nl = ((Element) nl.item(0)).getChildNodes();
n = nl.getLength();
for (int i = 0; i < n; i++) {
Element el = (Element) nl.item(i);
GraphElement ge = GraphElement.forElement(el);
if (ge != null)
graph.add(ge);
}
}
}
/**
* Returns a Vector of GraphElements (if any) to be used for drawing the
* graph of the contents of this node.
*/
public Vector getGraph() {
NodeList nl = getElement().getElementsByTagName("FB");
int n = nl.getLength();
Vector ans = new Vector(n * 3);
for (int i = 0; i < n; i++) {
GraphElement ge = GraphElement.forElement((Element) nl.item(i));
if (ge != null)
ans.add(ge);
}
addAdapters(ans, "Sockets");
addAdapters(ans, "Plugs");
addConnections(ans, "EventConnections");
addConnections(ans, "DataConnections");
addConnections(ans, "AdapterConnections");
return ans;
}
public boolean canAccept(String filename) {
return filename.endsWith(".fbt");
}
/**
* Adds adapters (if any) of the given type ("Plugs" or "Sockets") to the
* graph Vector.
*/
protected void addAdapters(Vector graph, String adptype) {
NodeList intfcs = ((Element) getElement().getParentNode())
.getElementsByTagName("InterfaceList");
if (intfcs.getLength() < 1)
return;
NodeList adplist = ((Element) intfcs.item(0))
.getElementsByTagName(adptype);
if (adplist.getLength() < 1)
return;
NodeList adps = ((Element) adplist.item(0)).getChildNodes();
for (int i = 0; i < adps.getLength(); i++) {
GraphElement ge = adptype.equals("Plugs") ? new Plug()
: new Socket();
ge.setElement((Element) adps.item(i));
if (ge != null)
graph.add(ge);
}
}
}