mapss.dif.ptolemy
Class DIFGraphExporter

java.lang.Object
  extended by mapss.dif.ptolemy.DIFGraphExporter

public class DIFGraphExporter
extends java.lang.Object

This class provides methods for exporting Ptolemy models to DIF.

Ptolemy doesn't allow two actors with the same name, so we don't use hash code in naming.

DIFGraphExporter converts every AtomicActor (ptolemy.actor.AtomicActor) as a node in DIF and converts every CompositeActor (ptolemy.actor.CompositeActor) as a hierarchy in DIF.

For each relation with multiple destinations, DIFGraphExporter will create a special "fork" node for it. The fork node has computation attribute "dif.fork". If a relation with multiple sources, it is illegal for SDF and will cause an exception.

The DIFNodeWeight of node representing AtomicActor or CompositeActor is set to that AtomicActor or CompositeActor. The DIFNodeWeight of forkNode is set to the relation represented by that forkNode.

DIFGraphExporter records actor class information as "computation" attribute. All parameters of an actor are recorded as node attributes with type "parameter". For every IOPort of a ptolemy atomic actor with relation connected to it, DIFGraphExporter will record this IOPort as node attribute with type "input" or "output" and the attribute reference is set to the corresponding Edge or Port in DIF. If the IOPort is multiport, then multiple edges or ports connected to it are recorded as a LinkedList.

DIFGraphExporter do not allowed IOPort (ptolemy.actor.IOPort) of CompositeActor to be multiport. If encounter this situation, DIFGraphExporter throws an exception. For each IOPort (is not multiport) of a CompositeActor, DIFGraphExporter will create the corresponding Port Port.

If setFullName(boolean) to true, the name of each DIFHierarchy is set to the full name of the corresponding CompositeActor. For example: if the full name of a ptolemy CompositeActor h3 is "h1.h2.h3", then the name of the DIFHierarchy representing that CompositeActor is "h1_h2_h3". If all CompositeAcotrs in all levels have different names, then its ok to setFullName(boolean) to false. In this case, the name will be "h3". Note that when writing to dif specification, DIFHierarchy.getName() is used as graph name instead of DIFGraph.getName(). The default settings is false.

Usage:

DIFGraphExporter exporter = new DIFGraphExporter(CompositeActor object);
DIFHierarcy hierarchy = exporter.convert();
String difSpecificaiton = exporter.toDIF();
exporter.write(fileName);

Version:
$Id: DIFGraphExporter.java 409 2007-05-13 19:47:16Z plishker $
Author:
Chia-Jui Hsu
See Also:
DIFGraphImporter, DIFGraph, Hierarchy

Constructor Summary
DIFGraphExporter(ptolemy.actor.CompositeActor compositeActor)
          Constructor.
DIFGraphExporter(java.lang.String fileName)
          Constructor.
 
Method Summary
protected  java.lang.Object _computeEdgeWeight(ptolemy.actor.IOPort sourcePort, ptolemy.actor.IOPort sinkPort)
          Determine the weight to be assigned to the weighted graph edge that represents a given connection in a Ptolemy II model.
protected  java.lang.Object _computeNodeWeight(java.lang.Object actor)
          Determine the weight to be assigned to the weighted graph node that represents a given actor.
protected  void _connectEdgeToPort(ptolemy.actor.IOPort ioPort, mocgraph.Edge edge)
          Connect the Port in dif subhierarchy corresponding to Ptolemy IOPort ioPortto an associated outside Edge edge.
protected  void _constructSingleInput(ptolemy.actor.IOPort inputPort, DIFHierarchy hierarchy, DIFGraph graph)
          Construct an input Port in a DIF hierarchy.
protected  void _constructSingleOutput(ptolemy.actor.IOPort outputPort, DIFHierarchy hierarchy, DIFGraph graph)
          Construct an output Port in DIF hierarchy.
protected  void _convertAtomicActor(ptolemy.actor.AtomicActor actor, DIFHierarchy hierarchy, DIFGraph graph)
          Convert an actor (ptolemy.actor.AtomicActor) in Ptolemy II to a node (mocgraph.Node) in DIFGraph.
protected  void _convertCompositeActor(ptolemy.actor.CompositeActor actor, DIFHierarchy hierarchy, DIFGraph graph)
          Convery an composite actor (ptolemy.actor.CompositeActor) in Ptolemy II as a super node (mocgraph.Node) in DIFGraph.
protected  void _createComposite(ptolemy.actor.CompositeActor compositeActor, DIFHierarchy hierarchy, DIFGraph graph)
          This method will create a DIFHierarchy and DIFGraph to represent a CompositeActor.
protected  java.lang.String _formatName(java.lang.String name)
          If the name conflicts with identifier convention Conventions.labelConvention(java.lang.String), enclose the name with '$'.
protected  java.lang.String _getName(java.lang.Object object, boolean fullName)
          Get object's name and add perfix if .
protected  void _initializeExporter()
          Initialize DIFGraphReader
protected  mocgraph.Graph _initializeGraph(ptolemy.actor.CompositeActor compositeActor)
          Initialize the exporter.
protected  void _putEdges(ptolemy.actor.CompositeActor compositeActor, DIFGraph graph)
          This method will put edges between nodes inside the graph and also connect an edge to a port if this edge is connected to a super node.
protected  void _putInterfaces(ptolemy.actor.CompositeActor compositeActor, DIFHierarchy hierarchy, DIFGraph graph)
          This method will put input and output interfaces of compositeActor.
protected  ptolemy.actor.CompositeActor _readFile(java.lang.String fileName)
          Convert a MoML (.xml) file that contains a Ptolemy II specification into a composite actor representation).
protected  void _relatePortToPort(ptolemy.actor.IOPort insidePort, Port port)
          Realte the outside DIF Port port to an inside DIF Port corresponding to a Ptolemy IOPort insidePort
protected  void _setGraphAttributes(ptolemy.actor.CompositeActor compositeActor, DIFGraph graph)
          Convert every Parameter in compositeActor to DIFAttribute in DIFGraph graph.
protected  void _setNodeAttributes(DIFGraph graph, mocgraph.Node node, ptolemy.kernel.util.NamedObj actor)
          get Parameters of an actor, save parameters as Node Attributes in DIFGraph.
protected  void _setPortAttribute(mocgraph.Edge edge, ptolemy.actor.IOPort ioPort, DIFGraph graph)
          Set node attributre for IOPort of a ptolemy atomic actor in DIF.
protected  void _setPortAttribute(Port port, ptolemy.actor.IOPort ioPort, DIFGraph graph)
          Set node attributre for IOPort of a ptolemy atomic actor.
 DIFHierarchy convert()
          Convert the CompositeActor object associated with this DIFGraphExporter into DIFHierarchy.
 void setFullName(boolean status)
          If set to true, the hierarchy name is the full name of the ptolemy composite actor.
 java.lang.String toDIF()
          Output the DIF specification of the converted DIFHierarchy.
 void write(java.lang.String fileName)
          Creates a DIF file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DIFGraphExporter

public DIFGraphExporter(ptolemy.actor.CompositeActor compositeActor)
Constructor.

Parameters:
compositeActor - The ptolemy.actor.CompositeActor object associated with this DIFGraphExporter.

DIFGraphExporter

public DIFGraphExporter(java.lang.String fileName)
Constructor. This constructor reads the ptolemy .xml file and generates the CompositeActor object by using ptolemy xml parser. The CompositeActor object will be associated with this DIFGraphExporter.

Parameters:
fileName - The ptoelmy .xml file.
Method Detail

convert

public DIFHierarchy convert()
Convert the CompositeActor object associated with this DIFGraphExporter into DIFHierarchy.

Returns:
The converted DIFHierarchy object.

toDIF

public java.lang.String toDIF()
                       throws DIFLanguageException
Output the DIF specification of the converted DIFHierarchy.

Returns:
The DIF specification as string.
Throws:
DIFLanguageException

setFullName

public void setFullName(boolean status)
If set to true, the hierarchy name is the full name of the ptolemy composite actor. For example: if the full name of a ptolemy composite actor h3 is "h1.h2.h3", then the name of the DIFHierarchy representing that composite actor is "h1_h2_h3". If set to false, the name is "h3" only. But if there are hierarchies in different levels with the same name, it will cause an error when calling convert() or write(String). The default is false.

Parameters:
status -

write

public void write(java.lang.String fileName)
           throws java.io.IOException,
                  DIFLanguageException
Creates a DIF file.

Parameters:
fileName - A file name string. A ".dif" extension will automatically be appended.
Throws:
java.io.IOException - If the file cannot be written.
DIFLanguageException

_computeEdgeWeight

protected java.lang.Object _computeEdgeWeight(ptolemy.actor.IOPort sourcePort,
                                              ptolemy.actor.IOPort sinkPort)
Determine the weight to be assigned to the weighted graph edge that represents a given connection in a Ptolemy II model.

Parameters:
sourcePort - the output port of the connection associated with the edge.
sinkPort - the input port of the connection associated with the edge
Returns:
DIFEdgeWeight.

_computeNodeWeight

protected java.lang.Object _computeNodeWeight(java.lang.Object actor)
Determine the weight to be assigned to the weighted graph node that represents a given actor.

Parameters:
actor - the actor whose node weight is to be determined.
Returns:
DIFNodeWeight.

_connectEdgeToPort

protected void _connectEdgeToPort(ptolemy.actor.IOPort ioPort,
                                  mocgraph.Edge edge)
Connect the Port in dif subhierarchy corresponding to Ptolemy IOPort ioPortto an associated outside Edge edge.

Parameters:
ioPort -
edge -

_constructSingleInput

protected void _constructSingleInput(ptolemy.actor.IOPort inputPort,
                                     DIFHierarchy hierarchy,
                                     DIFGraph graph)
Construct an input Port in a DIF hierarchy.

Parameters:
inputPort - An IOPort in ptolemy which is converted to Port in DIF by this method.
hierarchy - A DIFHierarchy which contains the converted Port.
graph - The graph corresponding to the hierarchy.

_constructSingleOutput

protected void _constructSingleOutput(ptolemy.actor.IOPort outputPort,
                                      DIFHierarchy hierarchy,
                                      DIFGraph graph)
Construct an output Port in DIF hierarchy.

Parameters:
outputPort - An IOPort in ptolemy which is converted to Port in DIF by this method.
hierarchy - A DIFHierarchy which contains the converted Port.
graph - The graph corresponding to the hierarchy.

_convertAtomicActor

protected void _convertAtomicActor(ptolemy.actor.AtomicActor actor,
                                   DIFHierarchy hierarchy,
                                   DIFGraph graph)
Convert an actor (ptolemy.actor.AtomicActor) in Ptolemy II to a node (mocgraph.Node) in DIFGraph.

Parameters:
actor - the AtomicActor which will be put as a node in DIFGraph.
hierarchy - The DIFHierarchy contains parameter graph. This hierarchy is on the upper level of this actor.
graph - The DIFGraph in which actor should put.

_convertCompositeActor

protected void _convertCompositeActor(ptolemy.actor.CompositeActor actor,
                                      DIFHierarchy hierarchy,
                                      DIFGraph graph)
Convery an composite actor (ptolemy.actor.CompositeActor) in Ptolemy II as a super node (mocgraph.Node) in DIFGraph. This super node will map to a sub hierarchy in current level hierarchy.

Parameters:
actor - The CompositeActor which will be put as a super node in DIFGraph.
hierarchy - The DIFHierarchy contains graph. This hierarchy is on the upper level of actor.
graph - The DIFGraph in which actor should put.

_createComposite

protected void _createComposite(ptolemy.actor.CompositeActor compositeActor,
                                DIFHierarchy hierarchy,
                                DIFGraph graph)
This method will create a DIFHierarchy and DIFGraph to represent a CompositeActor.

Parameters:
compositeActor - The CompositeActor need to be further refine by DIFHierarchy and DIFGraph.
hierarchy - The DIFHierarchy to represent this CompositeActor.
graph - The DIFGraph to represent this CompositeActor.

_formatName

protected java.lang.String _formatName(java.lang.String name)
If the name conflicts with identifier convention Conventions.labelConvention(java.lang.String), enclose the name with '$'. If the name conflicts with DIF keywords, enclose the name with '$'.

Parameters:
name - The name need to be formatted.
Returns:
formated name.

_getName

protected java.lang.String _getName(java.lang.Object object,
                                    boolean fullName)
Get object's name and add perfix if .

Parameters:
object -
fullName - Flag to indicate using full name or not.
Returns:
the formatted name of the object.

_initializeGraph

protected mocgraph.Graph _initializeGraph(ptolemy.actor.CompositeActor compositeActor)
Initialize the exporter.

Parameters:
compositeActor - the Ptolemy II model that will be converted.
Returns:
the empty DIFGraph that is to hold the converted model.

_initializeExporter

protected void _initializeExporter()
Initialize DIFGraphReader


_putEdges

protected void _putEdges(ptolemy.actor.CompositeActor compositeActor,
                         DIFGraph graph)
This method will put edges between nodes inside the graph and also connect an edge to a port if this edge is connected to a super node.

Parameters:
compositeActor - The CompositeActor represented by parameter graph.
graph - The DIFGraph in which edges are put.

_putInterfaces

protected void _putInterfaces(ptolemy.actor.CompositeActor compositeActor,
                              DIFHierarchy hierarchy,
                              DIFGraph graph)
This method will put input and output interfaces of compositeActor.

Parameters:
compositeActor - The CompositeActor.
hierarchy - The DIFHierarchy represents the compositeActor.
graph - The DIFGraph in of the DIFHierarchy.

_readFile

protected ptolemy.actor.CompositeActor _readFile(java.lang.String fileName)
Convert a MoML (.xml) file that contains a Ptolemy II specification into a composite actor representation).

Parameters:
fileName - The name of the MoML file.
Returns:
The composite actor representation.

_relatePortToPort

protected void _relatePortToPort(ptolemy.actor.IOPort insidePort,
                                 Port port)
Realte the outside DIF Port port to an inside DIF Port corresponding to a Ptolemy IOPort insidePort

Parameters:
insidePort -
port -

_setGraphAttributes

protected void _setGraphAttributes(ptolemy.actor.CompositeActor compositeActor,
                                   DIFGraph graph)
Convert every Parameter in compositeActor to DIFAttribute in DIFGraph graph.

Parameters:
compositeActor -
graph - The DIFGraph represents compositeActor.

_setNodeAttributes

protected void _setNodeAttributes(DIFGraph graph,
                                  mocgraph.Node node,
                                  ptolemy.kernel.util.NamedObj actor)
get Parameters of an actor, save parameters as Node Attributes in DIFGraph.

Parameters:
graph -
node -
actor - that is represented by node.

_setPortAttribute

protected void _setPortAttribute(Port port,
                                 ptolemy.actor.IOPort ioPort,
                                 DIFGraph graph)
Set node attributre for IOPort of a ptolemy atomic actor. The DIFAttribute for that IOPort has been create in _setNodeAttributes() method. This method is used to refer that DIFAttribute to the Port.

Parameters:
port - The outside connected port of a Port that represents ioport.
ioPort -
graph -

_setPortAttribute

protected void _setPortAttribute(mocgraph.Edge edge,
                                 ptolemy.actor.IOPort ioPort,
                                 DIFGraph graph)
Set node attributre for IOPort of a ptolemy atomic actor in DIF. The attribute for that IOPort has been create in _setNodeAttributes() method. This method is used to set the attribute value(reference).

Parameters:
edge - The outside connected edge of a Port that represents ioport.
ioPort -
graph -