Skip to content

Commit

Permalink
new way to express reactions
Browse files Browse the repository at this point in the history
  • Loading branch information
tanneberger committed Oct 17, 2024
1 parent c896702 commit 0dd74b4
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 66 deletions.
19 changes: 9 additions & 10 deletions include/reactor-uc/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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); \
}

Expand Down
8 changes: 4 additions & 4 deletions test/unit/action_microstep_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@

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];
Trigger *_triggers[2];
int cnt;
} MyReactor ;

REACTION_BODY(MyReaction, MyReactor, 0, {
REACTION_BODY(MyReactor, 0, {
MyAction *my_action = &self->my_action;

if (self->cnt == 0) {
Expand Down Expand Up @@ -48,7 +48,7 @@ void MyReactor_ctor(MyReactor *self, Environment *env) {

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);
Expand Down
8 changes: 4 additions & 4 deletions test/unit/action_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@

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];
Trigger *_triggers[2];
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);
Expand All @@ -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);
Expand Down
16 changes: 8 additions & 8 deletions test/unit/delayed_conn_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);

Expand All @@ -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));
Expand All @@ -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
Expand Down
24 changes: 12 additions & 12 deletions test/unit/physical_action_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,20 +37,20 @@ 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");
printf("PhysicalAction = %d\n", lf_get(my_action));
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);
Expand All @@ -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);

Expand Down
16 changes: 8 additions & 8 deletions test/unit/port_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
Expand All @@ -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));
Expand All @@ -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
Expand Down
24 changes: 12 additions & 12 deletions test/unit/shutdown_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions test/unit/startup_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@
#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");
})

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);
Expand Down
Loading

0 comments on commit 0dd74b4

Please sign in to comment.