rumoca 0.7.28

Modelica compiler written in RUST
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! MLS §17: Medium Priority State Machine Tests
//!
//! This module tests medium priority normative requirements:
//! - §17.1: State machine operators (transition, initialState, activeState)
//! - §17.2: Transition properties (priority, immediate, reset, synchronize)
//! - §17.3: State machine execution rules
//!
//! Reference: https://specification.modelica.org/master/state-machines.html

use crate::spec::{expect_failure, expect_parse_success};

// ============================================================================
// §17.1 STATE MACHINE OPERATORS
// ============================================================================

/// MLS §17.1: Basic state machine operators
mod state_machine_operators {
    use super::*;

    /// MLS: "transition() defines state transition"
    #[test]
    fn mls_17_1_transition_basic() {
        expect_parse_success(
            r#"
            model StateMachine
                inner Integer state(start = 0);

                model State1
                    outer Integer state;
                equation
                    state = 1;
                end State1;

                model State2
                    outer Integer state;
                equation
                    state = 2;
                end State2;

                State1 state1;
                State2 state2;
            equation
                transition(state1, state2, time > 1);
            end StateMachine;
            "#,
        );
    }

    /// MLS: "initialState() marks initial state"
    #[test]
    fn mls_17_1_initial_state() {
        expect_parse_success(
            r#"
            model StateMachine
                model State1
                equation
                end State1;

                State1 state1;
            equation
                initialState(state1);
            end StateMachine;
            "#,
        );
    }

    /// MLS: "activeState(state) returns true if state is active"
    #[test]
    fn mls_17_1_active_state() {
        expect_parse_success(
            r#"
            model StateMachine
                model State1
                equation
                end State1;

                State1 state1;
                Boolean isState1Active;
            equation
                isState1Active = activeState(state1);
                initialState(state1);
            end StateMachine;
            "#,
        );
    }

    /// MLS: "ticksInState() returns ticks in current state"
    #[test]
    fn mls_17_1_ticks_in_state() {
        expect_parse_success(
            r#"
            model StateMachine
                model State1
                    Integer ticks;
                equation
                    ticks = ticksInState();
                end State1;

                State1 state1;
            equation
                initialState(state1);
            end StateMachine;
            "#,
        );
    }

    /// MLS: "timeInState() returns time in current state"
    #[test]
    fn mls_17_1_time_in_state() {
        expect_parse_success(
            r#"
            model StateMachine
                model State1
                    Real stateTime;
                equation
                    stateTime = timeInState();
                end State1;

                State1 state1;
            equation
                initialState(state1);
            end StateMachine;
            "#,
        );
    }
}

// ============================================================================
// §17.2 TRANSITION PROPERTIES
// ============================================================================

/// MLS §17.2: Transition property specifications
mod transition_properties {
    use super::*;

    /// MLS: "Transitions must have unique priorities"
    #[test]
    #[ignore = "transition priority uniqueness not yet enforced"]
    fn mls_17_2_transition_priority_unique() {
        expect_failure(
            r#"
            model StateMachine
                model State1 end State1;
                model State2 end State2;
                model State3 end State3;

                State1 s1;
                State2 s2;
                State3 s3;
            equation
                initialState(s1);
                transition(s1, s2, true, priority = 1);
                transition(s1, s3, true, priority = 1);
            end StateMachine;
            "#,
            "StateMachine",
        );
    }

    /// MLS: "immediate=true evaluates condition immediately"
    #[test]
    fn mls_17_2_transition_immediate() {
        expect_parse_success(
            r#"
            model StateMachine
                model State1 end State1;
                model State2 end State2;

                State1 s1;
                State2 s2;
            equation
                initialState(s1);
                transition(s1, s2, true, immediate = true);
            end StateMachine;
            "#,
        );
    }

    /// MLS: "reset=true reinitializes variables on entry"
    #[test]
    fn mls_17_2_transition_reset() {
        expect_parse_success(
            r#"
            model StateMachine
                model State1
                    Real x(start = 0);
                equation
                    der(x) = 1;
                end State1;

                model State2
                    Real y(start = 0);
                equation
                    der(y) = 2;
                end State2;

                State1 s1;
                State2 s2;
            equation
                initialState(s1);
                transition(s1, s2, s1.x > 5, reset = true);
            end StateMachine;
            "#,
        );
    }

    /// MLS: "synchronize=true waits for all parallel states"
    #[test]
    fn mls_17_2_transition_synchronize() {
        expect_parse_success(
            r#"
            model StateMachine
                model State1 end State1;
                model State2 end State2;

                State1 s1;
                State2 s2;
            equation
                initialState(s1);
                transition(s1, s2, true, synchronize = true);
            end StateMachine;
            "#,
        );
    }

    /// MLS: "Priority defaults to 1 if not specified"
    #[test]
    fn mls_17_2_transition_default_priority() {
        expect_parse_success(
            r#"
            model StateMachine
                model State1 end State1;
                model State2 end State2;

                State1 s1;
                State2 s2;
            equation
                initialState(s1);
                transition(s1, s2, time > 1);
            end StateMachine;
            "#,
        );
    }
}

// ============================================================================
// §17.3 STATE MACHINE EXECUTION
// ============================================================================

/// MLS §17.3: State machine execution rules
mod execution_rules {
    use super::*;

    /// MLS: "Only one state is active at a time (exclusive states)"
    #[test]
    fn mls_17_3_single_active_state() {
        expect_parse_success(
            r#"
            model StateMachine
                model State1
                    Real x(start = 0);
                equation
                    der(x) = 1;
                end State1;

                model State2
                    Real x(start = 0);
                equation
                    der(x) = -1;
                end State2;

                State1 s1;
                State2 s2;
            equation
                initialState(s1);
                transition(s1, s2, s1.x > 5);
                transition(s2, s1, s2.x < -5);
            end StateMachine;
            "#,
        );
    }

    /// MLS: "Variables persist in inactive states"
    #[test]
    fn mls_17_3_variable_persistence() {
        expect_parse_success(
            r#"
            model StateMachine
                model State1
                    discrete Real counter(start = 0);
                algorithm
                    when sample(0, 0.1) then
                        counter := counter + 1;
                    end when;
                end State1;

                model State2
                    discrete Real counter(start = 0);
                algorithm
                    when sample(0, 0.1) then
                        counter := counter + 2;
                    end when;
                end State2;

                State1 s1;
                State2 s2;
            equation
                initialState(s1);
                transition(s1, s2, s1.counter > 10);
                transition(s2, s1, s2.counter > 20);
            end StateMachine;
            "#,
        );
    }

    /// MLS: "Nested state machines (hierarchical states)"
    #[test]
    #[ignore = "nested state machines not yet implemented"]
    fn mls_17_3_nested_state_machine() {
        expect_parse_success(
            r#"
            model OuterStateMachine
                model InnerStateMachine
                    model SubState1 end SubState1;
                    model SubState2 end SubState2;

                    SubState1 ss1;
                    SubState2 ss2;
                equation
                    initialState(ss1);
                    transition(ss1, ss2, time > 1);
                end InnerStateMachine;

                InnerStateMachine inner;
            equation
                initialState(inner);
            end OuterStateMachine;
            "#,
        );
    }

    /// MLS: "Parallel state machines"
    #[test]
    fn mls_17_3_parallel_states() {
        expect_parse_success(
            r#"
            model ParallelStateMachines
                model StateMachine1
                    model S1 end S1;
                    model S2 end S2;
                    S1 s1;
                    S2 s2;
                equation
                    initialState(s1);
                    transition(s1, s2, time > 1);
                end StateMachine1;

                model StateMachine2
                    model S3 end S3;
                    model S4 end S4;
                    S3 s3;
                    S4 s4;
                equation
                    initialState(s3);
                    transition(s3, s4, time > 2);
                end StateMachine2;

                StateMachine1 sm1;
                StateMachine2 sm2;
            equation
            end ParallelStateMachines;
            "#,
        );
    }
}

// ============================================================================
// §17.1 STATE MACHINE RESTRICTIONS
// ============================================================================

/// MLS §17.1: State machine restrictions
mod state_restrictions {
    use super::*;

    /// MLS: "State must be a block or model"
    #[test]
    #[ignore = "state type restriction not yet enforced"]
    fn mls_17_1_state_must_be_block_or_model() {
        expect_failure(
            r#"
            model StateMachine
                record NotAState
                    Real x;
                end NotAState;

                NotAState s;
            equation
                initialState(s);
            end StateMachine;
            "#,
            "StateMachine",
        );
    }

    /// MLS: "initialState must be called exactly once"
    #[test]
    #[ignore = "single initialState enforcement not yet implemented"]
    fn mls_17_1_single_initial_state() {
        expect_failure(
            r#"
            model StateMachine
                model State1 end State1;
                model State2 end State2;

                State1 s1;
                State2 s2;
            equation
                initialState(s1);
                initialState(s2);
            end StateMachine;
            "#,
            "StateMachine",
        );
    }

    /// MLS: "Transition must reference valid states"
    #[test]
    fn mls_17_1_transition_valid_states() {
        expect_failure(
            r#"
            model StateMachine
                model State1 end State1;
                State1 s1;
            equation
                initialState(s1);
                transition(s1, nonexistent, true);
            end StateMachine;
            "#,
            "StateMachine",
        );
    }
}