Difference between revisions of "Technical topic: OpenGEODE - SDL Operators: How to work with data"

Jump to: navigation, search
(TIMER types =)
Line 191: Line 191:
= TIMER types ==
= TIMER types =
   timer mytimer;
   timer mytimer;

Revision as of 12:28, 5 May 2021


The SDL language primarily targets the description of a system's behaviour. It is however also possible to manipulate data using a collection of built-in operators. This can be convenient to avoid depending on external, manually-written code to process the parameters of messages during the execution of a state machine transition.

Variables are defined in the ASN.1 language and can express simple types (boolean, numbers and enumerated values) as well as complex data structures (records, arrays, and choices). Each type comes with some dedicated operators which are described in this page.


  MyInt ::= INTEGER (-10 .. 255)
  dcl foo MyInt := 42;

The following can be done (in addition to the +, -, / and * operators):

  foo := -5               -- assign a value
  foo := abs (foo)        -- return 5
  foo := fix (5.7)        -- convert float to integer
  foo := power (10, 2)    -- 100
  foo := (foo + 1) mod 10 -- modulo
  write (foo)
  writeln (foo)           -- display the value (procedure call)

REAL types

  MyReal ::= REAL (0.0 .. 10.0)
  dcl foo MyReal := 42.0;
  dcl bar MyInt  := 5;

The following can be done:

  foo := 0.0             -- assign a value
  foo := ceil (1.618)    -- ceiling
  foo := floor (1.618)   -- ceiling
  foo := float (bar)     -- convert integer to real
  foo := round (3.14159) -- round
  foo := sin (3.14)
  foo := cos (3.14)
  foo := sqrt (2)
  foo := trunc (7.77)    -- truncation
  write (foo)
  writeln (foo)


  MyEnum ::= ENUMERATED { foo, bar }
  dcl var MyEnum := foo;
  dcl pos INTEGER (0..1);

Keep in mind that an enumerated is a state and not a number. You can get the position (as a number) of an enumerant like this:

  pos := num (var);

And in the reverse you could set the value from a position:

 var := val (1, MyEnum)  -- sets to bar

You can also print the enumerant name on the screen with the write or writeln operators.

CHOICE types

  MyChoice ::= CHOICE {
     foo BOOLEAN,
     bar INTEGER (0 .. 255)

  dcl a MyChoice := foo : FALSE;   -- ASN.1 Value Notation to set the value

You can evaluate the current choice using the present operator in a decision:

   DECISION present (a)  -- test against foo and bar
   writeln (if present (a) = foo then a.foo else a.bar fi)

You can also use an implicit type to store the choice determinant:

   dcl current_choice MyChoice_selection := present (a);

And if you happen to have an ENUMERATED type with the extact same enumerants as the CHOICE options, you can convert from an to:

     MyDeterminants ::= ENUMERATED { foo, bar }
     dcl det MyDeterminants := to_enum (a, MyDeterminants);
     current_choice := to_selector (det, MyChoice);

Last, there is an internal operator named choice_to_int that can be used if the CHOICE options are (mostly) numerical. It allows to return the value correponding to the current choice item without specifying the field name. You must provide a default value that will be returned in case the current choice's type is not numerical:

   dcl someInt MyInt;
   dcl someChoice MyChoice := { bar : 42 }
   someInt := choice_to_int (someChoice, 10)    -- will return 42
   someChoice := { foo : false }
   someInt := choice_to_int (someChoice, 10)    -- will return 10, the default value since the current choice is not "bar"

When combined with other operators, this can be used to make checks on the value of the choice without having to check manually all possible values.

For example you may want to write a generic function that can check a value against a threshold. The value itself could be in a CHOICE:

  ValueToMonitor ::= CHOICE {
     current Amp,
     voltage INTEGER (0..230)
     -- and a hundred more
  Parameters ::= ENUMERATED { current, voltage, ..... }

but you don't want to write a function that does something like:

   if present (val) = current then
     if val.current < currentThreshold then
       return true
     end if
   else if present (val) = voltage then
      if val.voltage < voltageThreshold then
        return true
      end if
   else .....

Instead you can do in a single line:

   if choice_to_int(val, 0) < thresholds(num(to_enum(val), Parameters)) then
       return true
   end if

assuming that threshold is a table indexed in the order of the determinants.

SEQUENCE OF types (arrays)

  MySeqOf ::= SEQUENCE (SIZE (0..10)) OF BOOLEAN
  dcl foo MySeqOf := { true, false, false };  -- ASN.1 Value Notation
  dcl bar MyInt;
  bar := fix (length (foo)) -- 3
  for each in foo:
     call writeln (each);

SEQUENCE types (records)

  MySeq ::= SEQUENCE {
     b INTEGER (0 .. 255)
  dcl seq MySeq := { a FALSE, b 10 };   -- ASN.1 Value Notation
  DECISION exist (seq.a)  -- test presence of optional field

STRING types


This type is an ASCII string

 SomeString ::= IA5String (1..255)

It can be assigned a value:

 dcl myStr SomeString := 'hello';    -- at declaration
 TASK myStr := 'world';              -- or in a task

It can be used in write/writeln calls.


 OctStr ::= OCTET STRING (SIZE (0.255))

Octet strings can be assigned a text string, a hex string, or a bit string:

 dcl myStr1 OctStr := '68656c6c6f'H;
 dcl myStr2 OctStr := '01010110'B;
 dcl myStr3 OctStr := 'hello world';

It is not possible to iterate (with a for loop) on OCTET STRINGs. Only SEQUENCE OF types are iterable. If you need to iterate on a sequence of octets, you can therefore define an custom octet string like this:

 Octet ::= OCTET STRING (SIZE (1))
 OctetString ::= SEQUENCE (SIZE (0..255)) OF Octet
 dcl helloStr OctetString := {'h', 'e', 'l', 'l', 'o'};
 for chr in helloStr:
    call write(chr);

TIMER types

  timer mytimer;

The SET and RESET operators from SDL are indirectly supported via procedure calls:

  set_timer (1000, mytimer);
  reset_timer (mytimer);