{"id":2168,"date":"2023-12-13T08:56:10","date_gmt":"2023-12-13T07:56:10","guid":{"rendered":"https:\/\/oopm.org\/?page_id=2168"},"modified":"2024-11-26T14:34:30","modified_gmt":"2024-11-26T13:34:30","slug":"51-1-traffic-light-example51-1","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=2168","title":{"rendered":"15.1 Traffic light example"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages2168&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages2168&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">Below we show an example of a traffic light that alternates between red and green in direction north and south respectively.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>trafficSystem: <strong>obj<\/strong>\n   <strong>class<\/strong> TrafficLight(direction: <strong>ref<\/strong> String):\n      state: <strong>ref<\/strong> String\n      cycle\n         state:= \"red\"\n         this(TrafficLight).suspend\n         state:= \"green\"\n         this(TrafficLight).suspend\n   north: <strong>obj<\/strong> TrafficLight(\"North:\")\n   south: <strong>obj<\/strong> TrafficLight(\"South:\")\n   north.call -- set north to green\n   cycle\n      north.call\n      south.call\n      sleep(1000)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The object&nbsp;<code>trafficSystem<\/code>&nbsp;has a class&nbsp;<code>TrafficLight<\/code>, which represents the concept of a traffic light.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Two objects\u00a0<code>north<\/code>\u00a0and south of type\u00a0<code>TrafficLight<\/code>\u00a0are declared (and thereby generated). The generation of a\u00a0<code>TrafficLight<\/code>-object implies that the statements in the class are executed. This means execution of\u00a0<code>cycle ...<\/code> This has the effect that the variable\u00a0<code>state<\/code>\u00a0is assigned the string\u00a0<code>\"red\"<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then the statement&nbsp;<code>this(TrafficLight).suspend&nbsp;<\/code>is executed. This has the effect that execution of the&nbsp;<code>TrafficLight<\/code>-object is temporarily suspended. The effect is that after the declarations of north and south, they both have&nbsp;<code>state = \"red\"<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Immediately after the generation of&nbsp;<code>north<\/code>&nbsp;and&nbsp;<code>south<\/code>&nbsp;the&nbsp;<code>trafficSystem<\/code>executes&nbsp;<code>north.call.<\/code>&nbsp;This has the effect that execution of&nbsp;<code>north<\/code>&nbsp;is resumed, which implies execution of&nbsp;<code>state := \"green\"&nbsp;<\/code>followed by a new suspension of the execution of&nbsp;<code>north<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The situation is then that&nbsp;<code>north.state = \"green\"<\/code>&nbsp;and&nbsp;<code>south.state = \"red\"<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>TrafficSystem<\/code>&nbsp;then executes a&nbsp;<code>cycle<\/code>&nbsp;where&nbsp;<code>north<\/code>&nbsp;and&nbsp;<code>south<\/code>&nbsp;both are resumed implying that their&nbsp;<code>state<\/code>&nbsp;variables swithces from&nbsp;<code>\"green\"<\/code>&nbsp;to&nbsp;<code>\"red\"<\/code>of&nbsp;<code>\"red\"<\/code>&nbsp;to&nbsp;<code>\"green\"<\/code>&nbsp;and so on. The&nbsp;<code>TrafficSystem<\/code>&nbsp;then&nbsp;<code>sleep<\/code>s for some time before another iteration in the cycle.<\/p>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=2168\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>Below we show an example of a traffic light that alternates between red and green in direction north and south respectively. The object&nbsp;trafficSystem&nbsp;has a class&nbsp;TrafficLight, which represents the concept of a traffic light. Two objects\u00a0north\u00a0and south of type\u00a0TrafficLight\u00a0are declared (and thereby generated). The generation of a\u00a0TrafficLight-object implies that the statements in the class are executed. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1961,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2168","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2168"}],"version-history":[{"count":12,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2168\/revisions"}],"predecessor-version":[{"id":9873,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2168\/revisions\/9873"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1961"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}