Home of the AlmostImplementedException

How to define your own types

In order to work with TTCN3 you will have to define your own types. They are like objects with properties in Java or C#.
Lets begin, as always with a very simple example

type charstring customString;

With this simple statement we declare a new type customString which is simply a charstring. Thats not helpful at all, but now we can change the behavior of charstring. Lets say we limit its length to only 15 chars.

type charstring customString length(15);

Limit an existing type is nice but more important is the combination of multiple types. For this we use type record:

type record Value {
charstring path,
charstring file optional

Now we have created a new type called Value with 2 charstring-properties. The second one is optional which means if you use it, only path is mandatory. Before we get to the more complex examples let me show you how to use this type:

var Value v_variable := {“path”};

log (v_variable);

Here we create a new variable of type Value and assign a new record with a charstring. For this we use curly brackets to indicate that we want a record. For TTCN-3 it doesn’t matter what type of record you use as long as one fits in the other. Since file is optional a record with only one charstring is needed.

The output of log is: { path := “path”, file := omit }. As you can see path is set and file is omit. Omit is the default value of an uninitialized type. You can check if a type or property is set with isvalue(v_variable.path). It returns true if a variable is fully initialized (a.k.a has a value).

Now we try a more difficult example:

type record Message {
charstring file optional,
charstring portTypeName,
charstring operationName,
charstring bindingName optional,
integer count,
set of Value values

This time we define a new type record called Message which uses multiple charstrings, one integer and a set of the type Value (see above). If you need a list of any type you can use record of or set of. Both do the same except that set of is a sorted list.
Its more complicated to fill this type. Try it for your self. Without more knowledge we have to fill every property, even the optional ones.

var Message v_variable := {“a”, “b”, “c”, “d”, 5, {}};

Since we didn’t declare set of Value as optional we have to fill it with at least an empty record. But is there no way to fill only mandatory fields?
Yes there is. In fact there are two. First you can use property-names to tell TTCN-3 which ones you want to fill or use a template (which is recommended). In the next post i’ll show how to use templates, for today we’ll use the straightforward way.

var Message v_variable := {
{“path”, “file”},
{“another path”}

As you can see, i now use property-names and set all mandatory fields and also fill our values-set. Since values is a set of Value which is a record with one mandatory and one optional charstring, we have to assign a record of record of one or two charstrings.

Thats it for this time. In my next post i’ll show you how to use matching-templates and templates to fill types.

Oh I almost forgot, here is the log-result of our variable above:

Share :

Leave a Reply