03.08.2013 Views

PTOLEMY II - CiteSeerX

PTOLEMY II - CiteSeerX

PTOLEMY II - CiteSeerX

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Custom Applets<br />

8.3.5 Using Model Parameters<br />

Typically, a model has a set of parameters that you wish for the user to be able to control in the<br />

applet. Suppose for example that in the above applet you wish for the user to be able to control the stop<br />

time of the director and the period of the clock actor. You can modify the Java code in figure 8.3 as<br />

shown in figure 8.8. This code uses the Parameter class to define two top-level parameters. The following<br />

lines create the top-level parameters:<br />

Parameter stopTime = new Parameter(this, "stopTime");<br />

Parameter clockPeriod = new Parameter(this, "clockPeriod");<br />

The default values of these two parameters are set by the following lines:<br />

stopTime.setExpression("10.0");<br />

clockPeriod.setExpression("2.0");<br />

package doc.tutorial;<br />

import ptolemy.actor.TypedCompositeActor;<br />

import ptolemy.actor.gui.PtolemyApplet;<br />

import ptolemy.actor.lib.Clock;<br />

import ptolemy.actor.lib.gui.TimedPlotter;<br />

import ptolemy.data.expr.Parameter;<br />

import ptolemy.domains.de.kernel.DEDirector;<br />

import ptolemy.kernel.util.IllegalActionException;<br />

import ptolemy.kernel.util.NameDuplicationException;<br />

import ptolemy.kernel.util.Workspace;<br />

public class TutorialApplet3 extends TypedCompositeActor {<br />

public TutorialApplet3(Workspace workspace)<br />

throws IllegalActionException, NameDuplicationException {<br />

super(workspace);<br />

}<br />

}<br />

// Create model parameters<br />

Parameter stopTime = new Parameter(this, "stopTime");<br />

Parameter clockPeriod = new Parameter(this, "clockPeriod");<br />

// Give the model parameters default values.<br />

stopTime.setExpression("10.0");<br />

clockPeriod.setExpression("2.0");<br />

// Create the director<br />

DEDirector director = new DEDirector(this, "director");<br />

setDirector(director);<br />

// Create two actors.<br />

Clock clock = new Clock(this,"clock");<br />

TimedPlotter plotter = new TimedPlotter(this,"plotter");<br />

// Set the user controlled parameters.<br />

director.stopTime.setExpression("stopTime");<br />

clock.period.setExpression("clockPeriod");<br />

// Connect the actors.<br />

connect(clock.output, plotter.input);<br />

FIGURE 8.8. Code that adds model parameters control to the applet. This code can be found in $PT<strong>II</strong>/doc/<br />

tutorial/TutorialApplet3.java.<br />

Heterogeneous Concurrent Modeling and Design 8-241

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!