SystemJ in Action

The purpose of the following example is to illustrate the SystemJ statements in action on the example of a simple program which follows our graphical example (see SystemJ in Nutshell). The example shows the use of major SystemJ statements, but is slightly abstracted in order to allow us to focus on key elements; it should be considered as a backbone of a more complex program and part of a scenario that can be envisaged. Also, note that not all features shown in graphically presented example are used in this example code (signals A, C and D), but easily be incorporated in slightly different scenario, as well as some other features could be introduced as we will explain in the text.

The program is presented in listing below. So, let’s start with the program explanation. It begins with system declaration (line 1), which is followed by the interface declaration (lines 2 to 8). Notice that all input and output signals into the system must be declared including their type, as well as all channels which will be used in the system. As channel as an object that has two ends (input and output), we have to declare both ends at this stage (line 6 and 7). What follows is the program/system body which is delimited with the curly brackets (lines 9 and 66). Within the program body we have three clock domains, CD1 (between lines 10 and 32), CD2 (between lines 34 and 41) and CD3 (between the lines 43 and 65). Clock domains are asynchronous each to the other and we indicate it by the asynchronous parallel operator “><” (lines 33 and 42).

In the implemented scenario, reaction R11 (lines 12 to 19) in clock domain CD1 waits on presence of signal B that is of integer type, extracts its value and after some calculation emits the result in local signal E and then waits on feedback from Reaction R12 (lines 21 to 31) that will be received on signal F. Reaction12 does some computation on the value of signal E, and eventually (not shown in the code) returns feedback in signal F. Notice that these two reactions operate in synchrony and are connected by synchronous parallel operator “||” (line 20). If we go back to Reaction11, once it receives signal F it wraps it into a message which will be then sent to another clock domain, CD2. From figure we see that CD2 can receive messages from both CD1 and CD3, so it receives the messages from these clock domains (or more precisely from the reactions within these clock domains) concurrently. It is the reason why we created two child reactions (line 36) in CD2 to monitor concurrently for incoming messages from two different sources (on channels C12 and C32).  Once Reacton21 receives message on any of the channels, it calculates the result which is then sent to clock domain CD3 via channel C23 (line 40). Clock domain CD3 has two top level reactions and their code is only partly populated. Reaction31 waits on the result of computation of Reaction32 (actually its child reaction R322) which is emitted in the local signal called tosend (shown in Figure 2) and once the signal is present reaction R31 completes the message to send to CD3 over channel C32.

While the emphasis in this example is on SystemJ features, Java programmers may have noticed a number of Java statements used in the program. First, it should be noticed that SystemJ statements only work on signals and channels, and Java does not understand what those objects are. Therefore, some translation in the exchange of information between two types of statements must take place. It is done through the # operator which allows us to extract the value of a signal (e.g. line 14) or channel (e.g. line 37) and assign it to the normal Java objects for processing using Java statements. The same operator is used to assign the value of a Java object to a signal or channel value.

The connection and non-existence of strict boundaries between Java and SystemJ statements also illustrates very intimate linkages between the two languages, which are at the end the foundation of SystemJ power: ability to deal with complex control-type situations and data abstractions at the same time, which has been the ideal for many programmers and even languages.

Further details on the language and its use are provided in the SystemJ manual and demo examples, but to find out the real power of the language you will have to download SystemJ Developer, full Eclipse-based IDE and try it on the provided and your own examples.

Listing Example of SystemJ program

Line

Program

1 system{
2                  interface{
3                                 input int signal B;
4                                 input signal C;
5                                 output signal A,D;
6                                 input int channel C12,C23,C32;
7                                 output int channel C12,C23,C32;
8                 }
9                 {
10                                 {//CD1
11                                                 signal E,F; //local signals
12                                                 {//R11
13                                                        await(B); //waiting for input
14                                                        int value = #B; //getting the value
15                                                           //Computation on value
16                                                        emit E(Math.abs(value)); //emitting the absolute value
17                                                        await (F);
18                                                        send C12(#F); //send the value on F to CD2
19                                                 }
20                                                 ||
21                                                  {//R12
22                                                         int t=0;
23                                                         present(E){
24                                                                 //computation
25                                                                  t = #E;
26                                                                  }
27                                                          else{
28                                                                   t = 1;
29                                                                  }
30                                                                  //some computation on t
31                                                  }
32                                  }
33                                  ><
34                                  {//CD2
35                                                  //R21
36                                                  {receive C12;} || {receive C32;}
37                                                  int e = Math.abs(#C12);
38                                                  //Java computation on e
39                                                  Calculate.sine(e);
40                                                  send C23(e);
41                                  }
42                                  ><
43                                  {//CD3
44                                                  int tosend;
45                                                  int signal tosend;
46                                                  {//R31
47                                                                  await (tosend);
48                                                                  send C32(#tosend);
49                                                  }
50                                                  ||
51                                                  {//R32
52                                                           int random;
53                                                           {//R321
54                                                             while(true){
55                                                                     random = Math.ceil(Random.rand());
56                                                                     pause;
57                                                                     }
58                                                              }
59                                                              ||
60                                                               {//R322
61                                                                       receive C23;
62                                                                       emit tosend(Calculate(#C23,random));
63                                                                 }
64                                                  }
65                                  }
66                   }               
67  }