Skip to content

Commit

Permalink
Publisher Subscriber example
Browse files Browse the repository at this point in the history
  • Loading branch information
EltonCN committed Oct 18, 2024
1 parent 3108287 commit e60d45a
Show file tree
Hide file tree
Showing 8 changed files with 625 additions and 7 deletions.
502 changes: 502 additions & 0 deletions examples/Publisher-Subscriber.ipynb

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions examples/Publisher-Subscriber/.$diagram.drawio.bkp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<mxfile host="Electron" agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/24.7.17 Chrome/128.0.6613.36 Electron/32.0.1 Safari/537.36" version="24.7.17">
<diagram name="Página-1" id="H1gPgiuQNB48lIkSNIZe">
<mxGraphModel dx="880" dy="524" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="j3rSS8qZvCsvwcYJBwNX-1" value="" style="group" vertex="1" connectable="0" parent="1">
<mxGeometry x="340" y="220" width="163" height="60" as="geometry" />
</mxCell>
<mxCell id="j3rSS8qZvCsvwcYJBwNX-2" value="&lt;font style=&quot;font-size: 12px&quot;&gt;Behavioral&lt;br&gt;Codelet&lt;/font&gt;" style="rounded=1;whiteSpace=wrap;html=1;strokeWidth=3;fillColor=#ffcc99;strokeColor=#36393d;" vertex="1" parent="j3rSS8qZvCsvwcYJBwNX-1">
<mxGeometry x="15.49647887323944" width="132.00704225352115" height="60" as="geometry" />
</mxCell>
<mxCell id="j3rSS8qZvCsvwcYJBwNX-3" value="&lt;font style=&quot;font-size: 8px;&quot;&gt;O&lt;/font&gt;" style="whiteSpace=wrap;html=1;strokeWidth=2;fillColor=#ffcccc;strokeColor=#36393d;fontSize=8;" vertex="1" parent="j3rSS8qZvCsvwcYJBwNX-1">
<mxGeometry x="147.50352112676057" y="9.999999999999998" width="15.49647887323944" height="15" as="geometry" />
</mxCell>
<mxCell id="j3rSS8qZvCsvwcYJBwNX-4" value="&lt;font style=&quot;font-size: 8px;&quot;&gt;A&lt;/font&gt;" style="whiteSpace=wrap;html=1;strokeWidth=2;fillColor=#ffcccc;strokeColor=#36393d;fontSize=8;" vertex="1" parent="j3rSS8qZvCsvwcYJBwNX-1">
<mxGeometry x="147.50352112676057" y="34.99999999999999" width="15.49647887323944" height="15" as="geometry" />
</mxCell>
<mxCell id="j3rSS8qZvCsvwcYJBwNX-5" value="&lt;font style=&quot;font-size: 8px; line-height: 120%;&quot;&gt;LI&lt;/font&gt;" style="whiteSpace=wrap;html=1;strokeWidth=2;fillColor=#ffcccc;strokeColor=#36393d;verticalAlign=middle;spacing=0;fontSize=8;" vertex="1" parent="j3rSS8qZvCsvwcYJBwNX-1">
<mxGeometry y="9.999999999999998" width="15.49647887323944" height="15" as="geometry" />
</mxCell>
<mxCell id="j3rSS8qZvCsvwcYJBwNX-6" value="&lt;font style=&quot;font-size: 8px;&quot;&gt;GI&lt;/font&gt;" style="whiteSpace=wrap;html=1;strokeWidth=2;fillColor=#ffcccc;strokeColor=#36393d;fontSize=8;" vertex="1" parent="j3rSS8qZvCsvwcYJBwNX-1">
<mxGeometry y="34.99999999999999" width="15.49647887323944" height="15" as="geometry" />
</mxCell>
<mxCell id="j3rSS8qZvCsvwcYJBwNX-7" value="&lt;font style=&quot;font-size: 11px&quot;&gt;Memory Object&lt;br&gt;&lt;/font&gt;" style="ellipse;whiteSpace=wrap;html=1;strokeColor=#36393d;strokeWidth=2;fillColor=#ffff88;fontSize=8;align=center;verticalAlign=top;labelPosition=center;verticalLabelPosition=bottom;" vertex="1" parent="1">
<mxGeometry x="100" y="220" width="57" height="60" as="geometry" />
</mxCell>
<mxCell id="j3rSS8qZvCsvwcYJBwNX-8" value="&lt;font style=&quot;font-size: 11px&quot;&gt;Memory Object&lt;br&gt;&lt;/font&gt;" style="ellipse;whiteSpace=wrap;html=1;strokeColor=#36393d;strokeWidth=2;fillColor=#ffff88;fontSize=8;align=center;verticalAlign=top;labelPosition=center;verticalLabelPosition=bottom;" vertex="1" parent="1">
<mxGeometry x="50" y="390" width="57" height="60" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
66 changes: 66 additions & 0 deletions examples/Publisher-Subscriber/diagram.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<mxfile host="Electron" agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/24.7.17 Chrome/128.0.6613.36 Electron/32.0.1 Safari/537.36" version="24.7.17">
<diagram name="Página-1" id="H1gPgiuQNB48lIkSNIZe">
<mxGraphModel dx="607" dy="361" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="j3rSS8qZvCsvwcYJBwNX-1" value="" style="group" vertex="1" connectable="0" parent="1">
<mxGeometry x="352" y="220" width="163" height="60" as="geometry" />
</mxCell>
<mxCell id="j3rSS8qZvCsvwcYJBwNX-2" value="Average&lt;div&gt;Codelet&lt;/div&gt;" style="rounded=1;whiteSpace=wrap;html=1;strokeWidth=3;fillColor=#ffcc99;strokeColor=#36393d;" vertex="1" parent="j3rSS8qZvCsvwcYJBwNX-1">
<mxGeometry x="15.49647887323944" width="132.00704225352115" height="60" as="geometry" />
</mxCell>
<mxCell id="j3rSS8qZvCsvwcYJBwNX-3" value="&lt;font style=&quot;font-size: 8px;&quot;&gt;O&lt;/font&gt;" style="whiteSpace=wrap;html=1;strokeWidth=2;fillColor=#ffcccc;strokeColor=#36393d;fontSize=8;" vertex="1" parent="j3rSS8qZvCsvwcYJBwNX-1">
<mxGeometry x="147.50352112676057" y="9.999999999999998" width="15.49647887323944" height="15" as="geometry" />
</mxCell>
<mxCell id="j3rSS8qZvCsvwcYJBwNX-4" value="&lt;font style=&quot;font-size: 8px;&quot;&gt;A&lt;/font&gt;" style="whiteSpace=wrap;html=1;strokeWidth=2;fillColor=#ffcccc;strokeColor=#36393d;fontSize=8;" vertex="1" parent="j3rSS8qZvCsvwcYJBwNX-1">
<mxGeometry x="147.50352112676057" y="34.99999999999999" width="15.49647887323944" height="15" as="geometry" />
</mxCell>
<mxCell id="j3rSS8qZvCsvwcYJBwNX-5" value="&lt;font style=&quot;font-size: 8px; line-height: 120%;&quot;&gt;LI&lt;/font&gt;" style="whiteSpace=wrap;html=1;strokeWidth=2;fillColor=#ffcccc;strokeColor=#36393d;verticalAlign=middle;spacing=0;fontSize=8;" vertex="1" parent="j3rSS8qZvCsvwcYJBwNX-1">
<mxGeometry y="9.999999999999998" width="15.49647887323944" height="15" as="geometry" />
</mxCell>
<mxCell id="j3rSS8qZvCsvwcYJBwNX-6" value="&lt;font style=&quot;font-size: 8px;&quot;&gt;GI&lt;/font&gt;" style="whiteSpace=wrap;html=1;strokeWidth=2;fillColor=#ffcccc;strokeColor=#36393d;fontSize=8;" vertex="1" parent="j3rSS8qZvCsvwcYJBwNX-1">
<mxGeometry y="34.99999999999999" width="15.49647887323944" height="15" as="geometry" />
</mxCell>
<mxCell id="j3rSS8qZvCsvwcYJBwNX-9" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="j3rSS8qZvCsvwcYJBwNX-7" target="j3rSS8qZvCsvwcYJBwNX-5">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="j3rSS8qZvCsvwcYJBwNX-7" value="&lt;font style=&quot;font-size: 11px&quot;&gt;Value&lt;/font&gt;" style="ellipse;whiteSpace=wrap;html=1;strokeColor=#36393d;strokeWidth=2;fillColor=#ffff88;fontSize=8;align=center;verticalAlign=top;labelPosition=center;verticalLabelPosition=bottom;" vertex="1" parent="1">
<mxGeometry x="160" y="207.5" width="57" height="60" as="geometry" />
</mxCell>
<mxCell id="j3rSS8qZvCsvwcYJBwNX-13" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="j3rSS8qZvCsvwcYJBwNX-8" target="j3rSS8qZvCsvwcYJBwNX-5">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="730" y="237" />
<mxPoint x="730" y="400" />
<mxPoint x="320" y="400" />
<mxPoint x="320" y="237" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="j3rSS8qZvCsvwcYJBwNX-8" value="&lt;font style=&quot;font-size: 11px&quot;&gt;Average&lt;br&gt;&lt;/font&gt;" style="ellipse;whiteSpace=wrap;html=1;strokeColor=#36393d;strokeWidth=2;fillColor=#ffff88;fontSize=8;align=center;verticalAlign=top;labelPosition=center;verticalLabelPosition=bottom;" vertex="1" parent="1">
<mxGeometry x="650" y="207.5" width="57" height="60" as="geometry" />
</mxCell>
<mxCell id="j3rSS8qZvCsvwcYJBwNX-14" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="j3rSS8qZvCsvwcYJBwNX-10" target="j3rSS8qZvCsvwcYJBwNX-5">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="730" y="330" />
<mxPoint x="730" y="400" />
<mxPoint x="320" y="400" />
<mxPoint x="320" y="237" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="j3rSS8qZvCsvwcYJBwNX-10" value="&lt;font style=&quot;font-size: 11px&quot;&gt;Counter&lt;br&gt;&lt;/font&gt;" style="ellipse;whiteSpace=wrap;html=1;strokeColor=#36393d;strokeWidth=2;fillColor=#ffff88;fontSize=8;align=center;verticalAlign=top;labelPosition=center;verticalLabelPosition=bottom;" vertex="1" parent="1">
<mxGeometry x="650" y="300" width="57" height="60" as="geometry" />
</mxCell>
<mxCell id="j3rSS8qZvCsvwcYJBwNX-11" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="j3rSS8qZvCsvwcYJBwNX-3" target="j3rSS8qZvCsvwcYJBwNX-8">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="j3rSS8qZvCsvwcYJBwNX-12" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="j3rSS8qZvCsvwcYJBwNX-3" target="j3rSS8qZvCsvwcYJBwNX-10">
<mxGeometry relative="1" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
Binary file added examples/Publisher-Subscriber/diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion src/cst_python/core/entities/codelet.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
from cst_python.python import alias
from .memory import Memory
from .memory_buffer import MemoryBuffer
from .memory_observer import MemoryObserver

#TODO: Profile, Broadcast, impending access, correct exception types

#@alias.aliased
class Codelet(abc.ABC):
class Codelet(MemoryObserver): #(abc.ABC) is not necessary
_last_id = 0

def __init__(self) -> None:
Expand Down
7 changes: 2 additions & 5 deletions src/cst_python/core/entities/memory_observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

from cst_python.python import alias

class MemoryObserver(abc.ABC):
def __init__(self) -> None:
raise NotImplementedError()

class MemoryObserver(abc.ABC):
#@alias.alias("notifyCodelet")
@abc.abstractmethod
def notify_codelet(self):
raise NotImplementedError()
...
1 change: 0 additions & 1 deletion src/cst_python/core/entities/raw_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def create_memory_object(self, name:str, info:Optional[Any]=None) -> MemoryObjec
mo = MemoryObject()

mo.set_info(info)
mo.timestamp = time.time()
mo.set_evaluation(0.0)
mo.set_name(name)

Expand Down
19 changes: 19 additions & 0 deletions tests/examples/test_publisher_subscriber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os
import math

from testbook import testbook
from testbook.client import TestbookNotebookClient

from ..utils import get_examples_path

examples_path = get_examples_path()

@testbook(os.path.join(examples_path, "Publisher-Subscriber.ipynb"), execute=True)
def test_publisher_subscriber(tb :TestbookNotebookClient):
expected_results : list[list] = [10, 15, 10, 15, 10, 15]

for i, excepted_result in enumerate(expected_results):
result = tb.cell_output_text(f"check_average{i}")
result = eval(result)

assert math.isclose(result, excepted_result)

0 comments on commit e60d45a

Please sign in to comment.