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.

Designing Actors<br />

}<br />

}<br />

The prefire() method can also be used to perform an operation that will happen exactly once per<br />

iteration. Consider the prefire method of the Bernoulli actor in figure 5.7:<br />

public boolean prefire() throws IllegalActionException {<br />

if (_random.nextDouble() <<br />

((DoubleToken)(trueProbability.getToken())).doubleValue()) {<br />

_current = true;<br />

} else {<br />

_current = false;<br />

}<br />

return super.prefire();<br />

}<br />

This method selects a new boolean value that will correspond to the token sent during each firing in<br />

that iteration.<br />

5.3.3 Fire<br />

The fire() method is the main point of execution and is generally responsible for reading inputs and<br />

producing outputs. It may also read the current parameter values, and the output may depend on them.<br />

Things to remember when writing fire() methods are:<br />

public class Bernoulli extends RandomSource {<br />

}<br />

public Bernoulli(CompositeEntity container, String name)<br />

throws NameDuplicationException, IllegalActionException {<br />

super(container, name);<br />

}<br />

output.setTypeEquals(BaseType.BOOLEAN);<br />

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

trueProbability.setExpression("0.5");<br />

trueProbability.setTypeEquals(BaseType.DOUBLE);<br />

public Parameter trueProbability;<br />

public void fire() {<br />

super.fire();<br />

output.send(0, new BooleanToken(_current));<br />

}<br />

public boolean prefire() throws IllegalActionException {<br />

if (_random.nextDouble() < ((DoubleToken)(trueProbability.getToken())).doubleValue()) {<br />

_current = true;<br />

} else {<br />

_current = false;<br />

}<br />

return super.prefire();<br />

}<br />

private boolean _current;<br />

FIGURE 5.7. Code for the Bernoulli actor, which is not data polymorphic.<br />

164 Ptolemy <strong>II</strong>

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

Saved successfully!

Ooh no, something went wrong!