Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New handling of startup/shutdown/reset reactions in modes #1169

Merged
merged 23 commits into from
Jun 10, 2022
Merged

Conversation

a-sr
Copy link
Collaborator

@a-sr a-sr commented May 13, 2022

Requires reactor-c PR #79
Requires benchmarks PR #15
Requires benchmarks PR #18

Implies website PR #67
Implies website PR #68

Closes #999
Closes #1048


Implements the following semantics:

  • Startup reactions:
    • Only triggered upon first entry into a mode
    • Implication: Some legacy reactors (without reset reactions) might stop working when entered via reset because an "initial token" is not recreated upon reset entry
  • Shutdown reactions:
    • Triggered at shutdown time of reactor
    • Only for modes that were active anytime before
    • Executed even is mode is inactive
  • New reset reactions:
    • Triggered at activation time of a mode if entered via reset
  • New reset transition:
    • No longer resets state variables
      • Can be handled by reset trigger
      • Implication: Always requires manual reset. Legacy reactors (without reset reaction) keep state variables values upon reset, leading to previous "history" leaking into the current (otherwise reset) behavior (e.g. a counter)

TODO:

  • Fix Python support
  • Fix newly blocked keywords in tested models (New Keywords for Modal Reactors #947)
  • Add a warning for reactor instantiations with state variables but without reset reactions

@a-sr a-sr added c Related to C target python Related to the Python target language Related to the syntax and semantics of LF modal models labels May 19, 2022
@a-sr a-sr requested a review from edwardalee May 19, 2022 13:23
@a-sr a-sr marked this pull request as ready for review May 19, 2022 13:23
@a-sr
Copy link
Collaborator Author

a-sr commented May 19, 2022

The implementation is complete and this PR and the related ones are ready for review.

The benchmark tests still fail because the fix in the benchmark repository is not yet merged.
The error in cli-tests on Mac looks weird but seems unrelated to this PR, since other tests succeed. @lhstrh Could you help me out with that one?

Copy link
Collaborator

@edwardalee edwardalee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! The website documentation will also need to be updated.

@@ -134,7 +134,7 @@ TargetDecl:
((parens+='(' (init+=Expression (',' init+=Expression)*)? parens+=')')
| (braces+='{' (init+=Expression (',' init+=Expression)*)? braces+='}')
)?
) ';'?
) (reset?='reset')? ';'?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused about this syntax. What does it mean if a state variable of type foo* is marked as reset?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also find it a bit strange to see this modifier at the end of the declaration. I think it would make more sense to have it at the beginning.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I'm thinking that a keyword like transient might be better, because it is a clear adjective and a much less frequently used term in variable names.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't reset already a keyword (for reset transitions)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always like to be able to effortlessly use programming language syntax in natural language in ways that makes sense. To me, a state ... reset is awkward, whereas transient state (or something along those lines) makes sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see why that's desirable. I still think it's better to have reset state ... than state ... reset.

Do you know what the reset behavior is for state variables with a pointer types?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the reset as a shorthand notation for resetting the state variable in a reset reaction, so I agree with @edwardalee that it should have a connection to it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my mind, I do not consider the reset as an integral component of the state variable but rather a handler added to it, like a deadline or an after delay. I also did not want to draw to much attention to it by putting it first but if you think a reset state makes more sense, I can change that.

Do you know what the reset behavior is for state variables with a pointer types?

Well, the variable is reset to the initial value provided by the user. I assumed that there always has to be one but if that was a misconception, I will add an error message in case reset is used without an initial value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handlers go at the end, sure, but this isn't really a handler but a modifier that labels the state as being automatically reset. I suggest we put the reset in front of the state variable declaration.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

Copy link
Member

@lhstrh lhstrh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, except for the syntax (as pointed out in conversation). If also really like your suggestion to validate that any state variable that is marked as reset indeed has an initial state assigned to it.

@a-sr
Copy link
Collaborator Author

a-sr commented May 28, 2022

Ok

  • added the validation error for reset state variables without initial values,
  • moved the reset modifier in front of the state keyword, and
  • adjusted the syntax examples on the website (PR68)

@a-sr
Copy link
Collaborator Author

a-sr commented May 29, 2022

Rust benchmark tests currently fail because there are newly added models using reset as variable name. Benchmarks PR #18 fixes these models.

@edwardalee
Copy link
Collaborator

Is this ready to merge?

Copy link
Member

@lhstrh lhstrh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are looking great! My only comment is that the new validator checks should have unit tests. Other than that, this looks ready to merge.

test/C/src/modal_models/ModalStateResetAuto.lf Outdated Show resolved Hide resolved
=}

initial mode One {
state counter1:int(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the state variable isn't rendered quite correctly in the diagram:
image

}
names.add(instantiation.getName());
}
}
}
}

@Check(CheckType.FAST)
public void checkMissingStateResetInMode(Reactor reactor) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that a reset transition is the default, meaning that is no modifier is given in the transition effect, this is to mean that it is a reset transition. This could be confusing, because it may suggest that a "normal" transition is of a different kind than reset or continue. We had a similar problem with actions, and decided that it would be better to require a disambiguation from the programmer and thus only allow logical action or physical action, and not just action. I think we should take the same approach here, and enforce this in the validator so we can present a useful error message.

STARTUP='startup' | SHUTDOWN='shutdown' | RESET='reset';

enum ModeTransition:
RESET='reset' | HISTORY='continue';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still a bit on the fence about the continue keyword. If refer to this a "history transition" and label it with an "H" in the diagram, then why don't we just use the history keyword and avoid all confusion?

@@ -29,6 +29,10 @@ reactor Modal {
state counter1(0)
timer T1(0msec, 250msec)

reaction(reset) {=
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rendering of the reset trigger seems a bit off:

image

I would actually like to propose an alternative visual syntax. How about something along these lines:
image

@@ -2221,7 +2222,153 @@ public void testUnrecognizedTarget() throws Exception {
"Unrecognized target: Pjthon");
}


@Test
public void testInitialMode() throws Exception {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Thanks for putting these in!

Copy link
Member

@lhstrh lhstrh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really good. I raised a couple points about syntax, validation, and visual representation. If you have the bandwidth, I would prefer we address these issues as part of this PR... I'm also fine with merging and adding fixes later.

@lhstrh
Copy link
Member

lhstrh commented Jun 10, 2022

I'll go ahead and merge this in to avoid stalling things any further. I've created issues to track my comments.

@lhstrh lhstrh merged commit 85fe66c into master Jun 10, 2022
@lhstrh lhstrh deleted the modal-models branch June 10, 2022 21:11
@lhstrh lhstrh added the enhancement Enhancement of existing feature label Jul 7, 2022
@lhstrh lhstrh changed the title Implement new handling of startup/shutdown/reset reactions in modes New handling of startup/shutdown/reset reactions in modes Jul 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c Related to C target enhancement Enhancement of existing feature language Related to the syntax and semantics of LF modal models python Related to the Python target
Projects
None yet
3 participants