*/
protected void createConnection(Xinterface interfaceFrom,
Xinterface interfaceTo) throws IllegalActionException,
NameDuplicationException {
// Find ports connected to interfaces.
Port portFrom = (Port) _interfaceIOPortTable.get(interfaceFrom);
Port portTo = (Port) _interfaceIOPortTable.get(interfaceTo);
if (portFrom == null || portTo == null) {
throw new InternalErrorException(
"Could not find port for From interface: " + interfaceFrom);
}
// Determine the From Relation.
Relation relationFrom;
if (interfaceFrom.parameters == null) {
if (_interfaceRelationTable.containsKey(interfaceFrom)) {
// This is a single (non-parameterized) port, and
// the relation already exists, so we reuse it.
Object o = _interfaceRelationTable.get(interfaceFrom);
if (!(o instanceof Relation)) {
throw new InternalErrorException(
"Single port should only be connected to a "
+ "Relation.");
}
relationFrom = (Relation) o;
} else {
// This is a single (non-parameterized) port, and
// the relation does not already exist, so we
// create a new relation, create a link and a
// _Link from the port to the new relation, and
// add the _Link to the list of links.
String relationName = _relations.getNewRelationName();
relationFrom = new IORelation(_compositeActor, relationName);
_interfaceRelationTable.put(interfaceFrom, relationFrom);
portFrom.link(relationFrom);
_Link link = new _Link(portFrom, relationFrom);
_linkList.add(link);
}
} else {
// This is a multi (parameterized) port, so we create
// a new relation, add it to the relation list of the
// interface, create a link and a _Link from the port
// to the new relation, and add the _Link to the list
// of links.
String relationName = _relations.getNewRelationName();
relationFrom = new IORelation(_compositeActor, relationName);
if (_interfaceRelationTable.containsKey(interfaceFrom)) {
// If the interface already has a relation list,
// add the new relation to the list.
Object o = _interfaceRelationTable.get(interfaceFrom);
if (!(o instanceof ArrayList)) {
throw new InternalErrorException(
"Multiport should only be connected to an "
+ "ArrayList of Relation.");
}
ArrayList arrayList = (ArrayList) o;
arrayList.add(relationFrom);
} else {
// If the interface does not already have a
// relation list, create it and add the new
// relation to the list.
ArrayList arrayList = new ArrayList();
arrayList.add(relationFrom);
_interfaceRelationTable.put(interfaceFrom, arrayList);
}
portFrom.link(relationFrom);
_Link link = new _Link(portFrom, relationFrom);
_linkList.add(link);
}
// Determine the To Relation.
Relation relationTo;
if (interfaceTo.parameters == null) {
if (_interfaceRelationTable.containsKey(interfaceTo)) {
// This is a single (non-parameterized) port, and
// the relation already exists, so we reuse it.
Object o = _interfaceRelationTable.get(interfaceTo);
if (!(o instanceof Relation)) {
throw new InternalErrorException(
"Single port should only be connected to a "
+ "Relation.");
}
relationTo = (Relation) o;
} else {
// This is a single (non-parameterized) port, and
// the relation does not already exist, so we
// create a new relation, create a link and a
// _Link from the new relation to the port, and
// add the _Link to the list of links.
String relationName = _relations.getNewRelationName();
relationTo = new IORelation(_compositeActor, relationName);
_interfaceRelationTable.put(interfaceTo, relationTo);
portTo.link(relationTo);
_Link link = new _Link(relationTo, portTo);
_linkList.add(link);
}
} else {
// This is a multi (parameterized) port, so we create
// a new relation, add it to the relation list of the
// interface, create a link and a _Link from the new
// relation to the port, and add the _Link to the list
// of links.
String relationName = _relations.getNewRelationName();
relationTo = new IORelation(_compositeActor, relationName);
if (_interfaceRelationTable.containsKey(interfaceTo)) {
// If the interface already has a relation list,
// add the new relation to the list.
Object o = _interfaceRelationTable.get(interfaceTo);
if (!(o instanceof ArrayList)) {
throw new InternalErrorException(
"Multiport should only be connected to an "
+ "ArrayList of Relation.");
}
ArrayList arrayList = (ArrayList) o;
arrayList.add(relationTo);
} else {
// If the interface does not already have a
// relation list, create it and add the new
// relation to the list.
ArrayList arrayList = new ArrayList();
arrayList.add(relationTo);
_interfaceRelationTable.put(interfaceTo, arrayList);
}
portTo.link(relationTo);
_Link link = new _Link(relationTo, portTo);
_linkList.add(link);
}
// Check for extra connections that might appear/disappear
// when translating from a nesC graph to a Ptolemy graph.
{
// If relationTo is connected to an input port to
// which relationFrom is not already connected, warn
// the user the extra connections will be formed.
List portListTo = ((IORelation) relationTo)
.linkedDestinationPortList((IOPort) portTo);
List portListFrom = ((IORelation) relationFrom)
.linkedDestinationPortList();
for (int i = 0; i < portListTo.size(); i++) {
Port tempPortTo = (Port) portListTo.get(i);
if (!portListFrom.contains(tempPortTo)) {
System.err.println("Warning: An extra link from "
+ portFrom.getContainer().getName() + "."
+ portFrom.getName() + " to "
+ tempPortTo.getContainer().getName() + "."
+ tempPortTo.getName() + " will be formed.");
}
}
}
{
// If relationTo is already connected to relationFrom,
// warn the user that this second connection will
// disappear in the current Ptolemy relation group
// implementation.
List portListTo = ((IORelation) relationTo)
.linkedDestinationPortList();
List portListFrom = ((IORelation) relationFrom)
.linkedDestinationPortList();
for (int i = 0; i < portListFrom.size(); i++) {
Port tempPortFrom = (Port) portListFrom.get(i);
if (portListTo.contains(tempPortFrom)) {
System.err.println("Warning: "
+ portFrom.getContainer().getName() + "."
+ portFrom.getName() + " is already connected to "
+ portTo.getContainer().getName() + "."