Stop Signal
Simple example to show how to use SceneBeans Examples


Purpose of this example

Please refer to the SceneBeans homepage for documentation and more general information about SceneBeans.

The XML file

The animation represents a trafic light, and is described in the XML file: stop.xml. You can use the build-in editor to edit it by using the Load SceneBeans animation-tool.

Explanation of the main parts of the file

<animation> : The top-level document element. Contains all information about the animation. A file can only contain one animation.

<draw id="main"> : The things we want to draw. The stop light consist of four figures; a rectangle and three circles.

First comes the red circle:

 <!-- draw red circle -->
 <transform type="translate">
  <param name="translation" value="(50,50)" />
  <style type="RGBAColor" id="red">
   <param name="color" value="ff0000"/>
   <primitive type="circle">
    <param name="radius" value="40"/>
   </primitive>
  </style>
 </transform>

A circle <primitive type="circle"> with a radius of 40 pixels <param name="radius" value="40"/>. It is colored <style type="RGBAColor" id="red"> red <param name="color" value="ff0000"/>.
To move it away from the upper left corner (0,0) to (50,50) we use the<transform type="translate"> tag with the parameter <param name="translation" value="(50,50)" />. The position is the center of the circle

The yellow and green circles are defined in the same way, but obviously with other translations.

There is though a slight addition to the green circle, which has been made sensitive to mouse clicks: <input type="mouseClick" id="click">. It sends a ReleasedEvent with the value "released": <input type="mouseClick" id="click">

We want the animation to announce the released event to BRITNeY, when it receives it from the green circles input part (click), so we declare:

 <event object="click" event="released">
  <announce event="released"/>
 </event>

We also define a command to "turn off" the trafic light:

 <command name="clear">
  <set object="red" param="color" value="500000"/>
  <set object="yellow" param="color" value="505000"/>
  <set object="green" param="color" value="005000"/>
 </command>

If you are editing the animation in BRITNeY, is both the commands and events listed in the workspace.

stopSignalWorkspace.png

The Model

Now we developed our animation, and want to tie it to a CPN model of a stop light. The model is really simpel:

stopModel1

Note on best practice: In the development process is it a good idea to make these declarations as auxiliary text and using Evaluate ML in stead of making them as genuine declarations, which gets evaluated after each edit. This is done on the Queries page in the nets.

We start with creating the SceneBeans object: struct lights = SceneBeans(val name="Stop light");
Then we load the animation: lights.setAnimation("c:/stop.xml"); to get this done automatical we can assign it to a anonymous value: val _ = lights.setAnimation("c:/stop.xml"); And this goes for all the commands we issue.

To initialise the stop light perform: lights.invokeCommand("clear"); and lights.setValue("green", "color", "00ff00");

As these are quite long and we are gonning to use them repeatably in the net, we define the corresponding functions: clear (), red (), yellow (), and green (). We use the functions in the code segments to control which lamp(s) are turned on in the stop light.

As described earlier we get an event each time the green light is clicked. To access these, we use: lights.getNextEvent();. If we write the function fun wait () = (lights.getNextEvent(); ()) and put it first in the code segments, the animation will wait for receiving an event before proceeding. How long this takes of course depends on whether there are any events in the event queue.

Download

You should put stop.xml in c: or change the path in the nets.

Version 11, Thu 13 Jul 2006 10:21:36 [tveon] - created Fri 08 Oct 2004 14:24:33 [mw]