<jupyter_start><jupyter_text>Traffic LightThis is an exercise for the lecture and not part of the mandatory preparation material.<jupyter_code>import ipywidgets as widgets
from IPython.display import display
from stmpy import Machine, Driver

class TrafficLight:
    
    def load_images(self):
        self.red_on = open("images/red_on.png", "rb").read()
        self.red_off = open("images/red_off.png", "rb").read()
        self.yellow_on = open("images/yellow_on.png", "rb").read()
        self.yellow_off = open("images/yellow_off.png", "rb").read()
        self.green_on = open("images/green_on.png", "rb").read()
        self.green_off = open("images/green_off.png", "rb").read()
        
    def on_button_switch(self, b):
        self.stm.send('switch')
        
    def on_button_terminate(self, b):
        self.stm.driver.stop()
    
    def display(self):
        self.button_switch = widgets.Button(description="Switch")
        self.button_switch.on_click(self.on_button_switch)
        self.button_terminate = widgets.Button(description="Terminate")
        self.button_terminate.on_click(self.on_button_terminate)
        self.red = widgets.Image(value=self.red_off, format='png', width=50, height=50)
        self.yellow = widgets.Image(value=self.yellow_off, format='png', width=50, height=50)
        self.green = widgets.Image(value=self.green_off, format='png', width=50, height=50)
        display(self.red, self.yellow, self.green, self.button_switch, self.button_terminate)
        
    def __init__(self):
        self.load_images()
        self.display()
        
    def turn_red_on(self):
        self.red.set_trait(name='value', value=self.red_on)
        
    def turn_red_off(self):
        self.red.set_trait(name='value', value=self.red_off)
        
    def turn_yellow_on(self):
        self.yellow.set_trait(name='value', value=self.yellow_on)
        
    def turn_yellow_off(self):
        self.yellow.set_trait(name='value', value=self.yellow_off)
        
    def turn_green_on(self):
        self.green.set_trait(name='value', value=self.green_on)
        
    def turn_green_off(self):
        self.green.set_trait(name='value', value=self.green_off)
import logging

traffic = TrafficLight()

t0 = {'source': 'initial',
      'target': 'off'}

t1 = {'trigger':'switch',
      'source':'off',
      'target':'red',
      'effect':'turn_red_on'
}
t2 = {'trigger':'switch', 
      'source':'red', 
      'target':'red_yellow', 
      'effect':'turn_yellow_on'
}
t3 = {'trigger':'switch', 
      'source':'red_yellow', 
      'target':'green', 
      'effect':'turn_red_off; turn_yellow_off; turn_green_on'
}
t4 = {'trigger':'switch', 
      'source':'green', 
      'target':'yellow', 
      'effect':'turn_green_off; turn_yellow_on'
}
t5 = {'trigger':'switch', 
      'source':'yellow', 
      'target':'red', 
      'effect':'turn_yellow_off; turn_red_on'
}
stm_traffic = Machine(name='stm_traffic', transitions=[t0, t1, t2, t3, t4, t5], obj=traffic)
traffic.stm = stm_traffic

logger = logging.getLogger('stmpy.Driver')
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)

logger = logging.getLogger('stmpy.Machine')
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)

%config Application.log_level="INFO"
    

driver = Driver()
driver.add_machine(stm_traffic)
driver.start()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1540965816603364, 'fraction_numerical': 0.0065111231687466084, 'mean_word_length': 3.6908396946564888, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '418366', 'license': 'MIT', 'n_tokens_mistral': 1313, 'n_tokens_neox': 1250, 'n_words': 195, 'path': 'notebooks/Traffic Light.ipynb', 'repo_name': 'Hallvardd/ttm4115-project', 'size': '3686'}