Skip to content

Commit

Permalink
Add check for when-condition variability
Browse files Browse the repository at this point in the history
- Check that when-conditions are discrete-time expressions.
  • Loading branch information
perost committed Dec 2, 2020
1 parent 83cc3d5 commit f877fea
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 0 deletions.
6 changes: 6 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFTyping.mo
Expand Up @@ -3091,6 +3091,12 @@ algorithm
(cond, ty, var) := typeCondition(cond, context, source,
Error.WHEN_CONDITION_TYPE_ERROR, allowVector = true, allowClock = true);

if var > Variability.IMPLICITLY_DISCRETE and not Type.isClock(ty) then
Error.addSourceMessage(Error.NON_DISCRETE_WHEN_CONDITION,
{Expression.toString(cond)}, ElementSource.getInfo(source));
fail();
end if;

if not checkWhenInitial(cond) then
Error.addSourceMessage(Error.INITIAL_CALL_WARNING,
{Expression.toString(cond)}, ElementSource.getInfo(source));
Expand Down
2 changes: 2 additions & 0 deletions OMCompiler/Compiler/Util/Error.mo
Expand Up @@ -834,6 +834,8 @@ public constant ErrorTypes.Message MISSING_INNER_NAME_CONFLICT = ErrorTypes.MESS
Gettext.gettext("An inner declaration for outer component ‘%s‘ could not be found, and could not be automatically generated due to a existing declaration of that name."));
public constant ErrorTypes.Message TOP_LEVEL_INPUT_WITH_BINDING = ErrorTypes.MESSAGE(381, ErrorTypes.TRANSLATION(), ErrorTypes.NOTIFICATION(),
Gettext.gettext("Top-level input ‘%s‘ has a binding equation and will not be accessible as an input of the model."));
public constant ErrorTypes.Message NON_DISCRETE_WHEN_CONDITION = ErrorTypes.MESSAGE(382, ErrorTypes.TRANSLATION(), ErrorTypes.ERROR(),
Gettext.gettext("When-condition ‘%s‘ is not a discrete-time expression."));

public constant ErrorTypes.Message INITIALIZATION_NOT_FULLY_SPECIFIED = ErrorTypes.MESSAGE(496, ErrorTypes.TRANSLATION(), ErrorTypes.WARNING(),
Gettext.gettext("The initial conditions are not fully specified. %s."));
Expand Down
4 changes: 4 additions & 0 deletions testsuite/flattening/modelica/scodeinst/Makefile
Expand Up @@ -913,6 +913,10 @@ WhenClockedElse1.mo \
WhenClockedElse2.mo \
WhenClockedStatement1.mo \
WhenClockedTupleCall1.mo \
WhenCondition1.mo \
WhenCondition2.mo \
WhenCondition3.mo \
WhenCondition4.mo \
WhenIllegalContext1.mo \
WhenIllegalContext2.mo \
WhenIllegalContext3.mo \
Expand Down
26 changes: 26 additions & 0 deletions testsuite/flattening/modelica/scodeinst/WhenCondition1.mo
@@ -0,0 +1,26 @@
// name: WhenCondition1
// keywords:
// status: correct
// cflags: -d=newInst
//
//

model WhenCondition1
Boolean b;
Real x;
equation
when b then
x = 1.0;
end when;
end WhenCondition1;

// Result:
// class WhenCondition1
// Boolean b;
// Real x;
// equation
// when b then
// x = 1.0;
// end when;
// end WhenCondition1;
// endResult
26 changes: 26 additions & 0 deletions testsuite/flattening/modelica/scodeinst/WhenCondition2.mo
@@ -0,0 +1,26 @@
// name: WhenCondition2
// keywords:
// status: correct
// cflags: -d=newInst
//
//

model WhenCondition2
Boolean b;
Real x;
equation
when {b, initial()} then
x = 1.0;
end when;
end WhenCondition2;

// Result:
// class WhenCondition2
// Boolean b;
// Real x;
// equation
// when {b, initial()} then
// x = 1.0;
// end when;
// end WhenCondition2;
// endResult
24 changes: 24 additions & 0 deletions testsuite/flattening/modelica/scodeinst/WhenCondition3.mo
@@ -0,0 +1,24 @@
// name: WhenCondition3
// keywords:
// status: correct
// cflags: -d=newInst
//
//

model WhenCondition3
Real x;
equation
when time > 0 then
x = 1.0;
end when;
end WhenCondition3;

// Result:
// class WhenCondition3
// Real x;
// equation
// when time > 0.0 then
// x = 1.0;
// end when;
// end WhenCondition3;
// endResult
24 changes: 24 additions & 0 deletions testsuite/flattening/modelica/scodeinst/WhenCondition4.mo
@@ -0,0 +1,24 @@
// name: WhenCondition4
// keywords:
// status: incorrect
// cflags: -d=newInst
//
//

model WhenCondition4
Real x;
equation
when noEvent(time > 0) then
x = 1.0;
end when;
end WhenCondition4;

// Result:
// Error processing file: WhenCondition4.mo
// [flattening/modelica/scodeinst/WhenCondition4.mo:11:3-13:11:writable] Error: When-condition ‘noEvent(time > 0.0)‘ is not a discrete-time expression.
//
// # Error encountered! Exiting...
// # Please check the error message and the flags.
//
// Execution failed!
// endResult

0 comments on commit f877fea

Please sign in to comment.