Though most application compositions can be specified at compile (or link) time, for some applications (such as symbolic computations, branch-and-bound) it is necessary to dynamically specify connections, or to dynamically create components. Another situation where dynamic component creation is suitable commonly occurs in particle-based scientific applications. Particle sets are partitioned at runtime based on their spatial coordinates using algorithms such as recursive bisection. If each partition is represented by a sequential component that models a 3-D box containing particles, the component representing the entire particle set needs to be constructed from the 3-D boxes at runtime. This dynamic construction for a particle set is illustrated in figure 3.20 (shown in 2-D for simplicity).
|
For such applications, one has to use the component-connection API explicitly. This API is available as an interface between the creator of the component and a ``system'' component. Thus, creator's output port connects to the system's input port and emits the class type to be created, and also specifies connection information. An example in figure 3.21 constructs a tree of objects dynamically.
{CodeOne}
class node {
in Start(void);
out Child1(void);
out Child2(void);
out Create(CreateDynamic);
};
{CodeTwo}
node root;
connect root.Create to
system.CreateObject;
connect system.Start to
root.Start;
{CodeThree}
node::Start(void)
{
CreateDynamic c1;
c1.classid = this.classid;
c1.connect(Port(0, "Create"),
Port(system, "CreateObject"));
c1.connect(Port(this, "Child1"),
Port(0, "Start"));
Create.emit(c1);
CreateDynamic c2;
c2.classid = this.classid;
c2.connect(Port(0, "Create"),
Port(system, "CreateObject"));
c2.connect(Port(this, "Child2"),
Port(0, "Start"));
Create.emit(c2);
// ...
}
In this example, the root node creates two children by emitting two CreateDynamic requests on its Create port, which is connected to the system's CreateObject input port. Before emitting CreateDynamic, it fills it with information about the object to be created. This includes the object's type, and its connections. The Port call takes two parameters, object ID and port name, and returns a class-local port ID. Note that the ID for the object that is to be created is unknown to the creator. For this purpose, a special value, 0, is recognized by Charisma API as the ID of the object to be created.