Castle: The best Real-Time/Embedded/HighTech language EVER. Attempt 2
修訂 | ceeed66715518e5ef6476c773ae96d73dc5f3a87 (tree) |
---|---|
時間 | 2024-05-14 01:15:56 |
作者 | Albert Mietus < albert AT mietus DOT nl > |
Commiter | Albert Mietus < albert AT mietus DOT nl > |
BUSY: sieveCastle (TESTDOUBLE): started with an EventHandler.
@@ -8,7 +8,8 @@ | ||
8 | 8 | pytst/sieve/basic1/test_2_sieve_NS.py \ |
9 | 9 | pytst/sieve/basic1/test_3_components.py \ |
10 | 10 | # |
11 | -CURRENT = pytst/sieve/basic1/test_sieveCastle.py \ | |
11 | +CURRENT = \ | |
12 | + pytst/sieve/basic1/test_sieveCastle.py \ | |
12 | 13 | # |
13 | 14 | |
14 | 15 | TODO = \ |
@@ -5,6 +5,8 @@ | ||
5 | 5 | .. see also:: :file:`./__init__.py` for a general intro |
6 | 6 | """ |
7 | 7 | |
8 | +__all__= ['GeneratorMoat', 'SieveMoat', 'FinderMoat'] | |
9 | + | |
8 | 10 | from castle.aigr.aid import TypedParameter |
9 | 11 | from castle.aigr import ComponentInterface, ID |
10 | 12 | from castle.aigr import Port, PortDirection |
@@ -27,11 +29,9 @@ | ||
27 | 29 | # port SimpleSieve<in>:try; |
28 | 30 | # port SimpleSieve<out>:coprime; |
29 | 31 | # } |
30 | -SieveMoat = ComponentInterface(name=ID("Sieve"), | |
31 | - ports=( | |
32 | - Port(name='try', direction=PortDirection.In, type=protocols.SimpleSieve), | |
33 | - Port(name='coprime', direction=PortDirection.Out, type=protocols.SimpleSieve), | |
34 | - )) | |
32 | +port_try = Port(name='try', direction=PortDirection.In, type=protocols.SimpleSieve) | |
33 | +port_coprime = Port(name='coprime', direction=PortDirection.Out, type=protocols.SimpleSieve) | |
34 | +SieveMoat = ComponentInterface(name=ID("Sieve"), ports=(port_try, port_coprime)) | |
35 | 35 | |
36 | 36 | |
37 | 37 | # component Finder : Component { |
@@ -17,8 +17,8 @@ | ||
17 | 17 | """protocol SimpleSieve : EventProtocol { |
18 | 18 | input(int:try); |
19 | 19 | }""" |
20 | +input_event = Event(name=ID('input'), return_type=None, typedParameters=(TypedParameter(name='try', type=int),)) | |
20 | 21 | SimpleSieve = EventProtocol(ID('SimpleSieve'), |
21 | - events=( | |
22 | - Event(name=ID('input'), return_type=None, typedParameters=(TypedParameter(name='try', type=int),)),)) | |
22 | + events=(input_event,)) | |
23 | 23 | |
24 | 24 |
@@ -4,21 +4,23 @@ | ||
4 | 4 | |
5 | 5 | .. see also:: :file:`./__init__.py` for a general intro |
6 | 6 | """ |
7 | +import logging; logger = logging.getLogger(__name__) | |
7 | 8 | |
8 | 9 | __all__ = ['Sieve'] |
9 | 10 | |
10 | 11 | |
11 | 12 | from castle import aigr |
12 | -from castle.aigr import ComponentImplementation, ID, Method | |
13 | +from castle.aigr import ComponentImplementation, ID | |
14 | +from castle.aigr import Method, EventHandler | |
15 | + | |
16 | +from castle.aigr_extra.blend import mangle_event_handler | |
13 | 17 | |
14 | 18 | |
19 | +from . import components, protocols | |
15 | 20 | |
16 | 21 | # implement Sieve { |
17 | 22 | # int myPrime; |
18 | 23 | # ... |
19 | - | |
20 | -from . import components | |
21 | - | |
22 | 24 | Sieve = ComponentImplementation('Sieve', |
23 | 25 | interface=components.SieveMoat, |
24 | 26 | parameters=(), |
@@ -27,6 +29,7 @@ | ||
27 | 29 | assert isinstance(Sieve.body, aigr.Body) # This make mypy happy to fill-in the Body |
28 | 30 | |
29 | 31 | |
32 | + | |
30 | 33 | # init(int:onPrime) // `init` is (typically) part of the construction of a element. |
31 | 34 | # { |
32 | 35 | # super.init(); // `super` acts as port to the base-class |
@@ -45,8 +48,8 @@ | ||
45 | 48 | targets=(aigr.Part(base=ID('self'), attribute=ID('myPrime', context=aigr.Set())),), |
46 | 49 | values=(ID('onPrime', context=aigr.Ref()),))])) |
47 | 50 | |
51 | +Sieve.body.expand(init_method) | |
48 | 52 | |
49 | -Sieve.body.expand(init_method) | |
50 | 53 | |
51 | 54 | |
52 | 55 | # SimpleSieve.input(try) on .try |
@@ -57,3 +60,14 @@ | ||
57 | 60 | # } |
58 | 61 | # |
59 | 62 | # } //@end Sieve |
63 | +event_handler_1 = EventHandler(ID(mangle_event_handler(protocol="SimpleSieve", event="input", port="try"), context=aigr.Def()), | |
64 | + protocol=ID('SimpleSieve', context=aigr.Ref()), | |
65 | + event=ID('input', context=aigr.Ref()), | |
66 | + port=ID('try', context=aigr.Ref()), | |
67 | + body=aigr.Body(statements=[ | |
68 | + # XXXX | |
69 | + ])) | |
70 | + | |
71 | + | |
72 | + | |
73 | +Sieve.body.expand(event_handler_1) |
@@ -6,8 +6,13 @@ | ||
6 | 6 | |
7 | 7 | import pytest |
8 | 8 | |
9 | + | |
10 | +from castle import aigr | |
11 | +from castle.aigr_extra.blend import mangle_event_handler | |
12 | + | |
9 | 13 | from castle.TESTDOUBLES.aigr.sieve.basic1 import sieveCastle |
10 | -from castle import aigr | |
14 | +from castle.TESTDOUBLES.aigr.sieve.basic1 import protocols, components | |
15 | + | |
11 | 16 | |
12 | 17 | from . import find_name_in_body |
13 | 18 |
@@ -30,11 +35,32 @@ | ||
30 | 35 | def test_0c_noParms(comp): |
31 | 36 | assert comp.parameters == () |
32 | 37 | |
33 | -def test_has_init(comp): | |
38 | + | |
39 | +def test_1_init_has_2lines(comp): | |
34 | 40 | init = find_name_in_body('init', comp.body) |
35 | 41 | assert isinstance(init, aigr.Method), f"Expected an init method, got {init}" |
36 | - | |
37 | -def test_init_has_body(comp): | |
38 | - init = find_name_in_body('init', comp.body) | |
39 | 42 | assert len(init.body)==2, f"Expected that 'init' has 2 statements, but found: {len(init.body.statements)}" |
40 | 43 | |
44 | + | |
45 | +def verify_IDref(id, expected_name): | |
46 | + assert id == expected_name, f"ID does not match, expected {expected_name}, got {id}" | |
47 | + assert isinstance(id, aigr.ID), f"ID ({id}) is not an ID, but type:{type(id)}" | |
48 | + assert isinstance(id.context, aigr.Ref), f"found ID '{id}' has not ref" | |
49 | + # XXX ToDo: check the ref -- for now empty is fine | |
50 | + | |
51 | + | |
52 | +def test_2_handler_on_try(comp): | |
53 | + """The event-handler name and the name of protocol/event/port are all text, but ... | |
54 | + should match the names of the earlier/elsewere defined protocol/event/port structure. | |
55 | + """ | |
56 | + # Notes | |
57 | + #@ - event(`input`) -- this protocol has only one event, so its simple | |
58 | + ## - port('try') -- thats the 1ste one. But keep it in sync (CastleCode is leading) | |
59 | + (protocol, event, port) = protocols.SimpleSieve, protocols.SimpleSieve.events[0], components.SieveMoat.ports[0] | |
60 | + | |
61 | + handler = find_name_in_body(mangle_event_handler(protocol=protocol.name, event=event.name, port=port.name), comp.body) | |
62 | + assert handler, "No handler found" | |
63 | + | |
64 | + verify_IDref(handler.protocol, protocol.name) | |
65 | + verify_IDref(handler.event, event.name) | |
66 | + verify_IDref(handler.port, port.name) |