[kepler-dev] Initializable interface

Edward A. Lee eal at eecs.berkeley.edu
Mon Aug 6 05:03:16 PDT 2007


I've checked in to the Ptolemy tree a change that may be useful
to some Kepler developers.  There is a new interface,
ptolemy.actor.Initializable, that contains preinitialize(),
initialize(), and wrapup() methods.  If you create a class
that implements this interface, you can attach it to any actor,
and these methods will be invoked whenever the corresponding
method of the actor is invoked.

For example, suppose you have a parameter that you want to have
do something (e.g. a database query) when the model begins
executing.  Then subclass Parameter with something that implements
Intializable, and fill in the initialize() method.  Then override
setContainer() to do this:

  public void setContainer(NamedObj container) {
     super.setContainer(container);
     if (container instanceof Initializable) {
         ((Initializable)container).addInitializable(this);
     }
  }

(a more complete implementation would use removeInitializable()
for the previous container, if non-null).

This mechanism relies on actors to call super.initialize() in
their initialize() method (and preinitialize() and wrapup()).
If they don't do that, then they will fail to invoke attached
Initializables.  I believe that all Ptolemy II actors do this
(any way to check?), but I'm not sure about Kepler actors.

Edward


------------ 
Edward A. Lee
Chair of EECS and Robert S. Pepper Distinguished Professor
231 Cory Hall, UC Berkeley, Berkeley, CA 94720-1770
phone: 510-642-0253, fax: 510-642-2845
eal at eecs.Berkeley.EDU, http://ptolemy.eecs.berkeley.edu/~eal  



More information about the Kepler-dev mailing list