3.1 Dynamic Actors 1

This example is contained in org.eclipse.etrice.examples.dynamicactors1.

3.1.1 Purpose

The example demonstrates the usage of an optional actor. It is shown that several actor classes derived from the type of the optional actor reference can be optionally created in place of the optional actor reference. Optional actor instances can also be destroyed and another instance can be created in the free slot.

3.1.2 Details

The structure of this system is simple.

PIC

However, this is only the initial system structure. The leaf instance is just a place holder for an optional actor instance. In this place an instance of a compatible type can be created at run time. Compatible types are the type of the reference itself and all of its sub types as long as they are not abstract. Together with the instance all of its contained instances will be created and all ports are connected.

This example demonstrates how an optional actor is created and destroyed and another one of another type is created to hold the same place.

PIC

When the example is executed the Container actor first dumps the instance tree to the console (line 56 of the listing below). Then it creates an instance of Optional2 (line 57). Now that the p0 port of the container is connected a message sayHello() is sent to the newly created actor instance and the instance tree is dumped a second time. As soon as it receives the answer it prints it to the console. Then the optional actor is destroyed again and another one, now of type Optional1, is created and once more sayHello() is sent.

 
36    StateMachine { 
37      Transition init: initial -> CreateOptional2 { } 
38      Transition tr0: CreateOptional2 -> CreateOptional1 { 
39       triggers { 
40         <hello: p0> 
41       } 
42       action { 
43         "System.out.println(txt+\"\\n\");" 
44         "opt.destroyOptionalActor();" 
45         "dumpTree(\"after deletion of Optional2\");" 
46       } 
47      } 
48      Transition tr2: CreateOptional1 -> ReceivedHelloAgain { 
49       triggers { 
50         <hello: p0> 
51       } 
52       action { 
53         "System.out.println(txt+\"\\n\");" 
54         "opt.destroyOptionalActor();" 
55       } 
56      } 
57      State CreateOptional2 { 
58       entry { 
59         "dumpTree(\"before creation of Optional2\");" 
60         "opt.createOptionalActor(\"Optional2\", getThread());" 
61         "p0.sayHello();" 
62         "dumpTree(\"after creation of Optional2\");" 
63       } 
64      } 
65      State CreateOptional1 { 
66       entry { 
67         "opt.createOptionalActor(\"Optional1\", getThread());" 
68         "p0.sayHello();" 
69         "dumpTree(\"after creation of Optional1\");" 
70       } 
71      } 
72      State ReceivedHelloAgain { 
73       entry {
Listing 3.1: Container actor state machine

The console output of the running application starts with

***   T H E   B E G I N   ***  
*** MainComponent /LS/main::init ***  
type ’quit’ to exit  
before creation of Optional2  
LS  
  main  
    RTSystemPort  
    MessageService_MessageService_PhysicalThread1  
      Dispatcher  
      Queue  
    ActorClass(className=Appl, instancePath=/LS/main/appl)  
      port RTSystemPort  
      ActorClass(className=Container, instancePath=/LS/main/appl/cont)  
        port RTSystemPort  
        port p0  
        ScalarOptionalActorInterface(className=Optional, instancePath=/LS/main/appl/cont/opt)  
          RTSystemPort  
          port p0  
    port RTSystemPort0  
    port RTSystemPort1

The ScalarOptionalActorInterface(className=Optional, instancePath=/LS/main/appl/cont/opt) is an object which is responsible for the life cycle of the dynamic actor (including its contained instances) and for the mediation of the port connections. It contains a replicated RTSystemPort which is used to trigger the initial transition and the port p0 of the interface of the Optional actor class.

After creation of Optional2 the interesting part of the dumped tree is

        ScalarOptionalActorInterface(className=Optional, instancePath=/LS/main/appl/cont/opt)  
          RTSystemPort  
          port p0  
          ActorClass(className=Optional2, instancePath=/LS/main/appl/cont/opt/opt)  
            port RTSystemPort  
            ActorClass(className=AC2, instancePath=/LS/main/appl/cont/opt/opt/sub2)  
              port RTSystemPort  
              ActorClass(className=AC3, instancePath=/LS/main/appl/cont/opt/opt/sub2/deep_sub)  
                port RTSystemPort  
                port p0  
          port RTSystemPort0  
          port RTSystemPort1  
          port RTSystemPort2

It can be seen that the sub tree corresponding to Optional2 was inserted right below the ScalarOptionalActorInterface.

After deletion of the optional actor the dumped instance tree looks exactly as in the beginning.

To illustrate the dynamic behavior of the system we can finally have a look at the generated sequence diagram 3.1. During the sub system initialization three actor instances are created. Then the system is started and the Container actor dynamically creates an instance of Optional2. This is indicated by the note on the life line of /LS/main/appl/cont. Then sayHello() is sent and the answer hello() is received and the optional actor is destroyed again.

The same is repeated with a new optional instance of Optional1.


PIC

Figure 3.1: Sequence diagram of Dynamic Actors Example 1

3.1.3 Noteworthy