Home
Categories
Dictionary
Glossary
Download
Project Details
Changes Log
What Links Here
FAQ
License

Groovy tutorial



The first tutorial presented the coding and configuration of a very simple system with two modules. In this Groovy tutorial, one of the modules will be reimplemented in the Groovy scripting language.

If you want to look at this same tutorial form start (without having to perform the first tutorial), go to Groovy tutorial from start.

Overview

In the first tutorial, we had two modules, both of which implemented in Java:
  • The PublishModule module increments or decrements a value cyclically
  • The EventModule module allows to click on a toggle to set if the first module should increment or decrement the value, and shows the value
We will reimplement the PublishModule in the Groovy scripting language.

Architecture

The first tutorial had two applications coded in Java:
  • The first PublishModule which:
    • Increment or decrement the value
    • Publish cyclically the value
    • Listen to the toggle event to set if the value should increment or decrement
  • The second EventModule which:
    • Subscribe to the published value and show this value
    • Show a toggle and sends an event when the user clicks on this toggle

tuto1java
We will reimplement the PublishModule in Groovy:
tutogroovy

XML configuration

Only the applications.xml XML file has to be modified to switch from a Java module implementation to a Groovy module implementation.

Applications definition

We will change the applications.xml XML file, by modifying the PublishModule configuration:
      <application name="publishAppli" id="2">
         <modules>
            <groovyModule name="PublishModule" id="1" >
               <groovyImplementation path="publish.groovy" >
                  <defaultSendEntryPoint method="publish" />
               </groovyImplementation>
               <interfaces>
                  <eventReceived service="event"/>
                  <cyclic service="published" frequency="200ms" attach="attach"/>
               </interfaces>
            </groovyModule>
         </modules>
      </application>
As you can see, we specified that the implementation of the PublishModule is in the publish.groovy file, in the same directory as the application.xml file. We also specified that a publish method in the Groovy script will be called each time the published Service is cyclically invoked.

Code the PublishModule in groovy

The Groovy code is very similar to the Java code used for the first tutorial.

As for the first tutorial, we don't have to bother about how to send cyclically the value, because we already specified that we wanted the published service to be invoked cyclically every 200 milliseconds in the module XML specification:
     <cyclic service="published" frequency="200ms" attach="attach"/>
The publish.groovy script will implement the behavior of this Module:
  • a subscribe method will be notified of the event Service. Depending on the value of the event data, a step variable will have a 1 or -1 value
  • a publish method will be invoked cyclically for the published Service. The current value will be incremented or decremented, and the Service will be notified with this current value
      int step = 1;
      int count = 1;

      public void subscribe(ServiceInstance service) {
        boolean evt = service.getData("event").getValueAsBoolean();
        if (evt) {
          step = -1;
        } else {
          step = 1;
        }
      }

      public void publish(ServiceInstance service) {
        service.setDataIntValue("value", count);
        count += step;
        service.invoke();
      }

See also


Categories: tutorials

Copyright 2017-2020 Dassault Aviation. All Rights Reserved. Documentation and source under the LGPL v3 licence