When you're about to program an algorithm you want to demonstrate using FDDA, the main thing is to implement the node types that participate. Many algorithms don't need more than one type of node, but you can have as many as you want, of course. A node type distinguishes itself mostly by what it does when the algorithm is started and what it does when it receives a message. Each node type is implemented in a class which must be a subtype of fdda.algorithm.Node (though you may use NodeAdapter as a superclass for your convenience). You should implement one or more of the following methods, which are defined in Node and will be called during the demonstration:
prepareForStart()
is actually called before the
demonstration starts and can be used to reset the algorithmstart()
is called at the start of the demonstration
if the node is marked active by the user in FDDA. It should
be used to start the algorithm.run()
is called if the node is active during the
demonstration. See below.messageReceived(Node, Message)
is called
whenever the node receives a message from a neighbour. It is probably
the most important method.
A node may send messages to its neighbours using the methods provided
in Node,
e.g. sendMessage(Node, Object)
. You may also create the
Message first (using
createMessage(Node)
, e.g.) and send it later. A message
can carry any payload, and you can let it have a special border color
or a short label to show what kind of message it is. Each message
sending operation returns whether it was successful: if a connection
doesn't work, you can't send messages over it.
You may activate a node either in its start
or
its messageReceived
method. The method run()
of an active node is called by the framework once per cycle (which may
be very often). It's probably better, though, to call the
sleepTime(long)
method, where you can specify a time in
milliseconds after which the node will become active automatically. If
you do this at the end of the run()
method, it will become
active periodically. While there are active nodes or nodes waiting to
become active, the algorithm doesn't count as terminated. If you want
to deactivate a node completely (only reacting to received messages),
call the sleep()
method.
There are still a few other more or less important methods of Node that should be mentioned. You
can change the color of the node and its label. And there's the method
initialize
. It is called after the node has been created
and initialized by the framework. Only here - not in the constructor -
is it allowed to use framework methods for the node initialization,
e.g. to add or change properties.
If you like, you can add special properties to your node that the user
may edit in FDDA. These Properties can be of type
Boolean, Integer, Double or String (for your own types, read the advanced topics). You can configure them by
setting so-called facets, e.g. to specify minimum or maximum
values (see the API documentation for more information). You add or
change properties by calling getModelNode()
which returns
the model Node that contains the algorithmic node. This model node is
a PropertyHolder. You may
also implement the PropertyListener
interface if you like to react to changes immediately. A good example
for the use of properties is the class for the Token Ring election algorithm.
Finally, you have to tell FDDA that there's a new algorithm. You do
this by creating a small XML file that lists the node types and names
the algorithm (the DTD is in the installation directory of FDDA, in
the subdirectory DTDs; you can look at an example file here). Then you either create a
subdirectory of the directory fdda/algorithms in your home directory
and put the algorithm class files and the description file there, or
you may bundle them into a jar file and place that in fdda/algorithms
or a subdirectory. You can also change the top search directory for
algorithms using the options dialog of FDDA.
There are two example algorithms to demonstrate how the framework should be used. The first is the echo flooding algorithm in a simple variant, the second one is the election algorithm for token rings with message extinction. The second one also uses an abstract superclass for nodes in a token ring.
URL dieser Seite: http://www.joerg-ruedenauer.de/Software/fdda/programming.html
Autor dieser Seite: Jörg Rüdenauer
Letzte Änderung am: 17.02.2004