<?xml version="1.0" ?>
<diagram name="Traffic_Light" author="Raphael Mannadiar">
	<description>
		PIM Traffic lights.
	</description>
	<inport name="ui"/>
	<top>
		from python_runtime.libs.ui import *
		from python_runtime.libs.utils import *
	</top>
	<class name="TrafficLight">
		<method name="TrafficLight">
			<parameter name="canvas"/>
			<body>
			<![CDATA[
				size = 100
				offset = size + 5
				self.RED = 0
				self.YELLOW = 1
				self.GREEN = 2
				self.colors = ['#f00', '#ff0', '#0f0']
				self.lights = [canvas.add_rectangle(size / 2, size / 2, size, size, {'fill':'#000'}), canvas.add_rectangle(size / 2, size / 2 + offset, size, size, {'fill':'#000'}), canvas.add_rectangle(size / 2, size / 2 + 2 * offset, size, size, {'fill':'#000'})]
			]]>
			</body>
		</method>
		<method name="setYellow">
			<body>
			<![CDATA[
				self.clear()
				self.lights[self.YELLOW].set_color(self.colors[self.YELLOW])
			]]>
			</body>
		</method>
		<method name="setGreen">
			<body>
			<![CDATA[
				self.clear()
				self.lights[self.GREEN].set_color(self.colors[self.GREEN])
			]]>
			</body>
		</method>
		<method name="clear">
			<body>
			<![CDATA[
				self.lights[self.RED].set_color('#000')
				self.lights[self.YELLOW].set_color('#000')
				self.lights[self.GREEN].set_color('#000')
			]]>
			</body>
		</method>
		<method name="setRed">
			<body>
			<![CDATA[
				self.clear()
				self.lights[self.RED].set_color(self.colors[self.RED])
			]]>
			</body>
		</method>
		<scxml  initial="on">
			<state id="on" initial="normal">
				<transition port="ui" target="../off" event="stop_clicked">
				</transition>
				<state id="interrupted" initial="yellow">
					<transition port="ui" target="../normal/history" event="police_interrupt_clicked">
					</transition>
					<state id="yellow">
						<onentry>
							<script>
							<![CDATA[
								self.setYellow()
							]]>
							</script>
						</onentry>
						<transition after="0.5" target="../black">
						</transition>
					</state>
					<state id="black">
						<onentry>
							<script>
							<![CDATA[
								self.clear()
							]]>
							</script>
						</onentry>
						<transition after="0.5" target="../yellow">
						</transition>
					</state>
				</state>
				<state id="normal" initial="red">
					<transition port="ui" target="../interrupted" event="police_interrupt_clicked">
					</transition>
					<history id="history"/>
					<state id="yellow">
						<onentry>
							<script>
							<![CDATA[
								self.setYellow()
							]]>
							</script>
						</onentry>
						<transition after="1.0" target="../red">
						</transition>
					</state>
					<state id="red">
						<onentry>
							<script>
							<![CDATA[
								self.setRed()
							]]>
							</script>
						</onentry>
						<transition after="3.0" target="../green">
						</transition>
					</state>
					<state id="green">
						<onentry>
							<script>
							<![CDATA[
								self.setGreen()
							]]>
							</script>
						</onentry>
						<transition after="2.0" target="../yellow">
						</transition>
					</state>
				</state>
			</state>
			<state id="off">
				<onentry>
					<script>
					<![CDATA[
						self.clear()
					]]>
					</script>
				</onentry>
			</state>
		</scxml>
	</class>
	<class name="MainApp" default="True">
		<relationships>
			<association name="trafficlight" class="TrafficLight"/>
		</relationships>
		<method name="MainApp">
			<body>
			<![CDATA[
				self.canvas = ui.append_canvas(ui.window, 100, 310, {'background':'#eee'})
				police_button = ui.append_button(ui.window, 'Police interrupt')
				stop_button = ui.append_button(ui.window, 'Stop')
				ui.bind_event(police_button.element, ui.EVENTS.MOUSE_CLICK, self.controller, 'police_interrupt_clicked')
				ui.bind_event(stop_button.element, ui.EVENTS.MOUSE_CLICK, self.controller, 'stop_clicked')
			]]>
			</body>
		</method>
		<scxml  initial="initializing">
			<state id="initialized">
			</state>
			<state id="initializing">
				<transition target="../creating">
					<raise event="create_instance" scope="cd">
						<parameter expr="'trafficlight'"/>
						<parameter expr="'TrafficLight'"/>
						<parameter expr="self.canvas"/>
					</raise>
				</transition>
			</state>
			<state id="creating">
				<transition target="../initialized" event="instance_created">
					<parameter name="association_name" type="string"/>
					<raise event="start_instance" scope="cd">
						<parameter expr="association_name"/>
					</raise>
					<raise event="set_association_name" scope="narrow" target="association_name">
						<parameter expr="association_name"/>
					</raise>
				</transition>
			</state>
		</scxml>
	</class>
</diagram>