From 6f05c8f3e756befcb2ad30581f7a4d74bf8f5aa9 Mon Sep 17 00:00:00 2001 From: tanneberger Date: Fri, 18 Oct 2024 00:21:57 +0200 Subject: [PATCH] new way to express reactions --- include/reactor-uc/macros.h | 19 +++++++++---------- test/unit/action_microstep_test.c | 6 +++--- test/unit/action_test.c | 8 ++++---- test/unit/delayed_conn_test.c | 16 ++++++++-------- test/unit/physical_action_test.c | 24 ++++++++++++------------ test/unit/port_test.c | 16 ++++++++-------- test/unit/shutdown_test.c | 24 ++++++++++++------------ test/unit/startup_test.c | 8 ++++---- test/unit/timer_test.c | 8 ++++---- 9 files changed, 64 insertions(+), 65 deletions(-) diff --git a/include/reactor-uc/macros.h b/include/reactor-uc/macros.h index ea1bf6ae..93c2e077 100644 --- a/include/reactor-uc/macros.h +++ b/include/reactor-uc/macros.h @@ -104,7 +104,7 @@ typedef struct Output Output; } PortName; \ \ void PortName##_ctor(PortName *self, Reactor *parent) { \ - Output_ctor(&self->super, parent, self->sources, sizeof(self->sources) / sizeof(self->sources[0])); \ + Output_ctor(&self->super, parent, self->sources, SourceSize); \ } typedef struct Input Input; @@ -117,8 +117,7 @@ typedef struct Input Input; } PortName; \ \ void PortName##_ctor(PortName *self, Reactor *parent) { \ - Input_ctor(&self->super, parent, self->effects, sizeof(self->effects) / sizeof(self->effects[0]), self->buffer, \ - sizeof(self->buffer[0])); \ + Input_ctor(&self->super, parent, self->effects, (EffectSize), self->buffer, sizeof(self->buffer[0])); \ } typedef struct Timer Timer; @@ -130,25 +129,25 @@ typedef struct Timer Timer; } TimerName; \ \ void TimerName##_ctor(TimerName *self, Reactor *parent) { \ - Timer_ctor(&self->super, parent, Offset, Period, self->effects, sizeof(self->effects) / sizeof(self->effects[0])); \ + Timer_ctor(&self->super, parent, Offset, Period, self->effects, EffectSize); \ } typedef struct Reaction Reaction; -#define DEFINE_REACTION(ReactionName, EffectSize) \ +#define DEFINE_REACTION(ReactorName, ReactionIndex, EffectSize) \ typedef struct { \ Reaction super; \ Trigger *effects[(EffectSize)]; \ - } ReactionName; + } ReactorName##_##ReactionIndex; -#define REACTION_BODY(ReactionName, ReactorName, ReactionIndex, ReactionBody) \ - void ReactionName##_##ReactionIndex(Reaction *_self) { \ +#define REACTION_BODY(ReactorName, ReactionIndex, ReactionBody) \ + void ReactorName##_body_##ReactionIndex(Reaction *_self) { \ ReactorName *self = (ReactorName *)_self->parent; \ Environment *env = self->super.env; \ ReactionBody \ } \ - void ReactionName##_ctor(ReactionName *self, Reactor *parent) { \ - Reaction_ctor(&self->super, parent, ReactionName##_##ReactionIndex, self->effects, \ + void ReactorName##_##ReactionIndex##_ctor(ReactorName##_##ReactionIndex *self, Reactor *parent) { \ + Reaction_ctor(&self->super, parent, ReactorName##_body_##ReactionIndex, self->effects, \ sizeof(self->effects) / sizeof(self->effects[0]), ReactionIndex); \ } diff --git a/test/unit/action_microstep_test.c b/test/unit/action_microstep_test.c index 5faadd79..77ba0445 100644 --- a/test/unit/action_microstep_test.c +++ b/test/unit/action_microstep_test.c @@ -3,11 +3,11 @@ DEFINE_LOGICAL_ACTION(MyAction, 1, 1, int, 1, MSEC(0), MSEC(0)); DEFINE_STARTUP(MyStartup, 1); -DEFINE_REACTION(MyReaction, 1); +DEFINE_REACTION(MyReactor, 0, 1); typedef struct { Reactor super; - MyReaction my_reaction; + MyReactor_0 my_reaction; MyAction my_action; MyStartup startup; Reaction *_reactions[1]; @@ -15,7 +15,7 @@ typedef struct { int cnt; } MyReactor ; -REACTION_BODY(MyReaction, MyReactor, 0, { +REACTION_BODY(MyReactor, 0, { MyAction *my_action = &self->my_action; if (self->cnt == 0) { diff --git a/test/unit/action_test.c b/test/unit/action_test.c index 62439327..4e4248cd 100644 --- a/test/unit/action_test.c +++ b/test/unit/action_test.c @@ -3,11 +3,11 @@ DEFINE_LOGICAL_ACTION(MyAction, 1, 1, int, 2, MSEC(0), MSEC(0)); DEFINE_STARTUP(MyStartup, 1); -DEFINE_REACTION(MyReaction, 1); +DEFINE_REACTION(MyReactor, 0, 1); typedef struct { Reactor super; - MyReaction my_reaction; + MyReactor_0 my_reaction; MyAction my_action; MyStartup startup; Reaction *_reactions[1]; @@ -15,7 +15,7 @@ typedef struct { int cnt; } MyReactor ; -REACTION_BODY(MyReaction, MyReactor, 0, { +REACTION_BODY(MyReactor, 0, { MyAction *my_action = &self->my_action; if (self->cnt == 0) { TEST_ASSERT_EQUAL(lf_is_present(my_action), false); @@ -38,7 +38,7 @@ void MyReactor_ctor(MyReactor *self, Environment *env) { self->_triggers[1] = (Trigger *)&self->my_action; Reactor_ctor(&self->super, "MyReactor", env, NULL, NULL, 0, self->_reactions, 1, self->_triggers, 2); MyAction_ctor(&self->my_action, &self->super); - MyReaction_ctor(&self->my_reaction, &self->super); + MyReactor_0_ctor(&self->my_reaction, &self->super); MyStartup_ctor(&self->startup, &self->super); ACTION_REGISTER_EFFECT(self->my_action, self->my_reaction); REACTION_REGISTER_EFFECT(self->my_reaction, self->my_action); diff --git a/test/unit/delayed_conn_test.c b/test/unit/delayed_conn_test.c index d0c0f70c..7ddb2cc9 100644 --- a/test/unit/delayed_conn_test.c +++ b/test/unit/delayed_conn_test.c @@ -3,19 +3,19 @@ // Components of Reactor Sender DEFINE_TIMER(Timer1, 1, 0, MSEC(100)) -DEFINE_REACTION(Reaction1, 0); +DEFINE_REACTION(Sender, 0, 0); DEFINE_OUTPUT_PORT(Out, 1) typedef struct { Reactor super; - Reaction1 reaction; + Sender_0 reaction; Timer1 timer; Out out; Reaction *_reactions[1]; Trigger *_triggers[1]; } Sender; -REACTION_BODY(Reaction1, Sender, 0, { +REACTION_BODY(Sender, 0, { Out *out = &self->out; printf("Timer triggered @ %ld\n", env->get_elapsed_logical_time(env)); @@ -26,7 +26,7 @@ void Sender_ctor(Sender *self, Reactor *parent, Environment *env) { self->_reactions[0] = (Reaction *)&self->reaction; self->_triggers[0] = (Trigger *)&self->timer; Reactor_ctor(&self->super, "Sender", env, parent, NULL, 0, self->_reactions, 1, self->_triggers, 1); - Reaction1_ctor(&self->reaction, &self->super); + Sender_0_ctor(&self->reaction, &self->super); Timer1_ctor(&self->timer, &self->super); Out_ctor(&self->out, &self->super); @@ -37,19 +37,19 @@ void Sender_ctor(Sender *self, Reactor *parent, Environment *env) { } // Reactor Receiver -DEFINE_REACTION(Reaction2, 0) +DEFINE_REACTION(Receiver, 0, 0) DEFINE_INPUT_PORT(In, 1, interval_t, 1) typedef struct { Reactor super; - Reaction2 reaction; + Receiver_0 reaction; In inp; int cnt; Reaction *_reactions[1]; Trigger *_triggers[1]; } Receiver ; -REACTION_BODY(Reaction2, Receiver, 0, { +REACTION_BODY(Receiver, 0, { In *inp = &self->inp; printf("Input triggered @ %ld with %ld\n", env->get_elapsed_logical_time(env), lf_get(inp)); @@ -60,7 +60,7 @@ void Receiver_ctor(Receiver *self, Reactor *parent, Environment *env) { self->_reactions[0] = (Reaction *)&self->reaction; self->_triggers[0] = (Trigger *)&self->inp; Reactor_ctor(&self->super, "Receiver", env, parent, NULL, 0, self->_reactions, 1, self->_triggers, 1); - Reaction2_ctor(&self->reaction, &self->super); + Receiver_0_ctor(&self->reaction, &self->super); In_ctor(&self->inp, &self->super); // Register reaction as an effect of in diff --git a/test/unit/physical_action_test.c b/test/unit/physical_action_test.c index 6d4216e4..274bcb3b 100644 --- a/test/unit/physical_action_test.c +++ b/test/unit/physical_action_test.c @@ -7,15 +7,15 @@ Environment env; DEFINE_PHYSICAL_ACTION(MyAction, 1, 1, int, 1, MSEC(0), MSEC(0)) DEFINE_STARTUP(MyStartup, 1) DEFINE_SHUTDOWN(MyShutdown, 1) -DEFINE_REACTION(ShutdownReaction, 0) -DEFINE_REACTION(MyReaction, 1) -DEFINE_REACTION(StartupReaction, 1) +DEFINE_REACTION(MyReactor, 0, 1) +DEFINE_REACTION(MyReactor, 1, 1) +DEFINE_REACTION(MyReactor, 2, 0) typedef struct { Reactor super; - StartupReaction startup_reaction; - ShutdownReaction shutdown_reaction; - MyReaction my_reaction; + MyReactor_0 startup_reaction; + MyReactor_1 my_reaction; + MyReactor_2 shutdown_reaction; MyAction my_action; MyStartup startup; MyShutdown shutdown; @@ -37,12 +37,12 @@ void *async_action_scheduler(void *_action) { pthread_t thread; -REACTION_BODY(StartupReaction, MyReactor, 0, { +REACTION_BODY(MyReactor, 0, { MyAction *action = &self->my_action; pthread_create(&thread, NULL, async_action_scheduler, (void *)action); }); -REACTION_BODY(MyReaction, MyReactor, 1, { +REACTION_BODY(MyReactor, 1, { MyAction *my_action = &self->my_action; printf("Hello World\n"); @@ -50,7 +50,7 @@ REACTION_BODY(MyReaction, MyReactor, 1, { TEST_ASSERT_EQUAL(lf_get(my_action), self->cnt++); }) -REACTION_BODY(ShutdownReaction, MyReactor, 2, { +REACTION_BODY(MyReactor, 2, { run_thread = false; void *retval; int ret = pthread_join(thread, &retval); @@ -66,9 +66,9 @@ void MyReactor_ctor(MyReactor *self, Environment *_env) { Reactor_ctor(&self->super, "MyReactor", _env, NULL, NULL, 0, self->_reactions, 3, self->_triggers, 3); MyAction_ctor(&self->my_action, &self->super); - MyReaction_ctor(&self->my_reaction, &self->super); - StartupReaction_ctor(&self->startup_reaction, &self->super); - ShutdownReaction_ctor(&self->shutdown_reaction, &self->super); + MyReactor_0_ctor(&self->startup_reaction, &self->super); + MyReactor_1_ctor(&self->my_reaction, &self->super); + MyReactor_2_ctor(&self->shutdown_reaction, &self->super); MyStartup_ctor(&self->startup, &self->super); MyShutdown_ctor(&self->shutdown, &self->super); diff --git a/test/unit/port_test.c b/test/unit/port_test.c index 89cdda0a..1eb3a06f 100644 --- a/test/unit/port_test.c +++ b/test/unit/port_test.c @@ -3,19 +3,19 @@ // Components of Reactor Sender DEFINE_TIMER(Timer1, 1, 0, SEC(1)) -DEFINE_REACTION(Reaction1, 0) +DEFINE_REACTION(Sender, 0, 0) DEFINE_OUTPUT_PORT(Out, 1) typedef struct { Reactor super; - Reaction1 reaction; + Sender_0 reaction; Timer1 timer; Out out; Reaction *_reactions[1]; Trigger *_triggers[1]; } Sender; -REACTION_BODY(Reaction1, Sender, 0, { +REACTION_BODY(Sender, 0, { Out *out = &self->out; printf("Timer triggered @ %ld\n", env->get_elapsed_logical_time(env)); @@ -26,7 +26,7 @@ void Sender_ctor(Sender *self, Reactor *parent, Environment *env) { self->_reactions[0] = (Reaction *)&self->reaction; self->_triggers[0] = (Trigger *)&self->timer; Reactor_ctor(&self->super, "Sender", env, parent, NULL, 0, self->_reactions, 1, self->_triggers, 1); - Reaction1_ctor(&self->reaction, &self->super); + Sender_0_ctor(&self->reaction, &self->super); Timer1_ctor(&self->timer, &self->super); Out_ctor(&self->out, &self->super); TIMER_REGISTER_EFFECT(self->timer, self->reaction); @@ -35,18 +35,18 @@ void Sender_ctor(Sender *self, Reactor *parent, Environment *env) { // Reactor Receiver -DEFINE_REACTION(Reaction2, 0) +DEFINE_REACTION(Receiver, 0, 0) DEFINE_INPUT_PORT(In, 1, instant_t, 1) typedef struct { Reactor super; - Reaction2 reaction; + Receiver_0 reaction; In inp; Reaction *_reactions[1]; Trigger *_triggers[1]; } Receiver ; -REACTION_BODY(Reaction2, Receiver, 0, { +REACTION_BODY(Receiver, 0, { In *inp = &self->inp; printf("Input triggered @ %ld with %ld\n", env->get_elapsed_logical_time(env), lf_get(inp)); @@ -57,7 +57,7 @@ void Receiver_ctor(Receiver *self, Reactor *parent, Environment *env) { self->_reactions[0] = (Reaction *)&self->reaction; self->_triggers[0] = (Trigger *)&self->inp; Reactor_ctor(&self->super, "Receiver", env, parent, NULL, 0, self->_reactions, 1, self->_triggers, 1); - Reaction2_ctor(&self->reaction, &self->super); + Receiver_0_ctor(&self->reaction, &self->super); In_ctor(&self->inp, &self->super); // Register reaction as an effect of in diff --git a/test/unit/shutdown_test.c b/test/unit/shutdown_test.c index 63c39678..6192174d 100644 --- a/test/unit/shutdown_test.c +++ b/test/unit/shutdown_test.c @@ -4,41 +4,41 @@ DEFINE_STARTUP(MyStartup, 1) DEFINE_SHUTDOWN(MyShutdown, 1) -DEFINE_REACTION(Reaction1, 0) -DEFINE_REACTION(Reaction2, 0) +DEFINE_REACTION(MyReactor, 0, 0) +DEFINE_REACTION(MyReactor, 1, 0) typedef struct { Reactor super; - Reaction1 reaction1; - Reaction2 reaction2; + MyReactor_0 reaction0; + MyReactor_1 reaction1; MyStartup startup; MyShutdown shutdown; Reaction *_reactions[2]; Trigger *_triggers[2]; } MyReactor; -REACTION_BODY(Reaction1, MyReactor, 0, { +REACTION_BODY(MyReactor, 0, { printf("Startup reaction executing\n"); }) -REACTION_BODY(Reaction2, MyReactor, 1, { +REACTION_BODY(MyReactor, 1, { printf("Shutdown reaction executing\n"); }) void MyReactor_ctor(MyReactor *self, Environment *env) { - self->_reactions[0] = (Reaction *)&self->reaction1; - self->_reactions[1] = (Reaction *)&self->reaction2; + self->_reactions[0] = (Reaction *)&self->reaction0; + self->_reactions[1] = (Reaction *)&self->reaction1; self->_triggers[0] = (Trigger *)&self->startup; self->_triggers[1] = (Trigger *)&self->shutdown; Reactor_ctor(&self->super, "MyReactor", env, NULL, NULL, 0, self->_reactions, 2, self->_triggers, 2); - Reaction1_ctor(&self->reaction1, &self->super); - Reaction2_ctor(&self->reaction2, &self->super); + MyReactor_0_ctor(&self->reaction0, &self->super); + MyReactor_1_ctor(&self->reaction1, &self->super); MyStartup_ctor(&self->startup, &self->super); MyShutdown_ctor(&self->shutdown, &self->super); - STARTUP_REGISTER_EFFECT(self->startup, self->reaction1); - SHUTDOWN_REGISTER_EFFECT(self->shutdown, self->reaction2); + STARTUP_REGISTER_EFFECT(self->startup, self->reaction0); + SHUTDOWN_REGISTER_EFFECT(self->shutdown, self->reaction1); } ENTRY_POINT(MyReactor) diff --git a/test/unit/startup_test.c b/test/unit/startup_test.c index 6410f3b0..3a1636c4 100644 --- a/test/unit/startup_test.c +++ b/test/unit/startup_test.c @@ -3,18 +3,18 @@ #include "unity.h" DEFINE_STARTUP(MyStartup, 1) -DEFINE_REACTION(MyReaction, 0) +DEFINE_REACTION(MyReactor, 0, 0) typedef struct { Reactor super; - MyReaction my_reaction; + MyReactor_0 my_reaction; MyStartup startup; Reaction *_reactions[1]; Trigger *_triggers[1]; int cnt; } MyReactor; -REACTION_BODY(MyReaction, MyReactor, 0, { +REACTION_BODY(MyReactor, 0, { printf("Hello World\n"); }) @@ -22,7 +22,7 @@ void MyReactor_ctor(MyReactor *self, Environment *env) { self->_reactions[0] = (Reaction *)&self->my_reaction; self->_triggers[0] = (Trigger *)&self->startup; Reactor_ctor(&self->super, "MyReactor", env, NULL, NULL, 0, self->_reactions, 1, self->_triggers, 1); - MyReaction_ctor(&self->my_reaction, &self->super); + MyReactor_0_ctor(&self->my_reaction, &self->super); MyStartup_ctor(&self->startup, &self->super); STARTUP_REGISTER_EFFECT(self->startup, self->my_reaction); diff --git a/test/unit/timer_test.c b/test/unit/timer_test.c index a3e3ca46..07996115 100644 --- a/test/unit/timer_test.c +++ b/test/unit/timer_test.c @@ -2,17 +2,17 @@ #include "unity.h" DEFINE_TIMER(MyTimer, 1, 0, MSEC(100)) -DEFINE_REACTION(MyReaction, 0) +DEFINE_REACTION(MyReactor, 0, 0) typedef struct { Reactor super; - MyReaction my_reaction; + MyReactor_0 my_reaction; MyTimer timer; Reaction *_reactions[1]; Trigger *_triggers[1]; } MyReactor; -REACTION_BODY(MyReaction, MyReactor, 0, { +REACTION_BODY(MyReactor, 0, { printf("Hello World @ %ld\n", env->get_elapsed_logical_time(env)); }) @@ -20,7 +20,7 @@ void MyReactor_ctor(MyReactor *self, Environment *env) { self->_reactions[0] = (Reaction *)&self->my_reaction; self->_triggers[0] = (Trigger *)&self->timer; Reactor_ctor(&self->super, "MyReactor", env, NULL, NULL, 0, self->_reactions, 1, self->_triggers, 1); - MyReaction_ctor(&self->my_reaction, &self->super); + MyReactor_0_ctor(&self->my_reaction, &self->super); MyTimer_ctor(&self->timer, &self->super); TIMER_REGISTER_EFFECT(self->timer, self->my_reaction); }