Skip to content

Commit

Permalink
Update component variable docu
Browse files Browse the repository at this point in the history
  • Loading branch information
schmalzing committed Nov 28, 2024
1 parent 8729630 commit e2734c4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 36 deletions.
80 changes: 44 additions & 36 deletions docs/behavior/Variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,51 @@

# Component variables

```montiarc
component MovingAverageSmoother {
port in double roughSignal,
out double smoothedSignal;
// Declaring fields to save the most recent values
double oldestVal = 0.0;
double olderVal = 0.0;
double youngerVal = 0.0;
double youngestVal = 0.0;
compute {
double average = (oldestVal + olderVal + youngerVal + youngestVal + roughSignal) / 5.0;
// Updating the history values
oldestVal = olderVal;
olderVal = youngerVal;
youngerVal = youngestVal;
youngestVal = roughSignal;
// Sending the smoothed value through the out port
smoothedSignal = average;
}
}
Component variables extend a component's state space. They can store messages
and intermediate computation results to record state that is maintained across
individual execution steps.

Component variables are declared directly within the body of a component.
The declaration of a component variable specifies the variable's type, states
its name, and assigns its initial value. A component-variable declaration looks
like

```
TYPE NAME = INITIAL;
```

where

* `TYPE` is the variable's data type (reference)

Components may contain internal variables to record state that is maintained
across individual execution steps.
Variables are declared directly within the component body with the syntax
`<var-type> <var-name> = <initial-value> ;`.
It is mandatory to assign initial values to all variables.
One can also declare multiple variables of the same type within the same
declaration by separating them with commas:
```montiarc
component MovingAverageSmoother {
// ...
double oldestVal = 0.0, olderVal = 0.0, youngerVal = 0.0, youngestVal = 0.0;
// ...
* `NAME` is the variable's name (defining)

* `INITIAL` is an expression defining the variable's initial value

For example,

```
component SumUp {
port in int i;
port in Signal r;
port out int o;
int sum = 0;
automaton {
initial state S;
S -> S i / {
sum = sum + i;
};
S -> S r / {
o = sum;
};
}
}
```

calculates the sum over past inputs using component variable `sum` of type
`int`. The variable's initial value is `0`. Every time the component receives
a message on port `i` it adds it to the value of `sum` and stores the new value
in variable `sum`. When the component receives a message on port `r` it
outputs the total sum calculated so far on port `o`.
11 changes: 11 additions & 0 deletions generators/ma2jsim-it/main/montiarc/montiarc/Delay
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package montiarc;

component Delay<T> {
port in T i;
port <<delayed>> out T o;

automaton {
initial state S;
S -> S / { o = i; };
}
}

0 comments on commit e2734c4

Please sign in to comment.