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

Groovy tutorial from start



This tutorial is the same as the Groovy tutorial, but without having to do the first tutorial first.

This tutorial present the coding and configuration of a very simple system with two modules. One of them will be scripted in Groovy.

Overview

Suppose the we want to specify two modules:
  • One module increments or decrements a value cyclically. This module will be scripted in Groovy
  • Another module allows to click on a toggle to set if the first module should increment or decrement the value. It also shows the value

tuto1

Architecture

We will define two applications:
  • The first PublishModule, scripted in Groovy, 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, coded in Java which:
    • Subscribe to the published value and show this value
    • Show a toggle and sends an event when the user clicks on this toggle

tutogroovy

XML configuration

Services definition

We define our two services in a services.xml XML file:
  • The event service carries the state of the toggle button. It is an event Service because the service should only be invoked when the state of the toggle button changes
  • The published service carries the value. It is a publish Service because the service should be invoked cyclically
      <services>
         <event name="event" id="1" >
            <data name="event" type="bool" />
         </event>
         <publish name="published" id="2" >
            <data name="value" type="int" />
         </publish>
      </services>

Types definition

These two services use a very simple types definition. There are only two types:
  • The bool type is a boolean
  • The int type is an int
We define these types in a types.xml XML file:
      <types>
         <simpleType name="bool" baseType="boolean" />
         <simpleType name="int" baseType="int" />
      </types>

Applications definition

We define our two applications in an applications.xml XML file:
  • The eventAppli only contains the EventModule. This Module:
    • sends the event service
    • subscribes to the published service
  • The publishAppli only contains the PublishModule. This Module:
    • sends cyclically the published service
    • subscribes to the event service
         <applications>
            <application name="eventAppli" id="1">
               <modules>
                  <module name="EventModule" id="1" >
                     <interfaces>
                        <eventSend service="event" attach="attach"/>
                        <subscribe service="published" />
                     </interfaces>
                  </module>
               </modules>
            </application>
           <application name="publishAppli" id="2">
             <modules>
               <groovyModule name="PublishModule" id="1" >
                 <interfaces>
                   <eventReceived service="event"/>
                   <cyclic service="published" frequency="200ms" attach="attach"/>
                 </interfaces>
               </groovyModule>
             </modules>
           </application>
         </applications>
Note that the PublishModule is defined as a groovyModule rather than a module element, because it is scripted in groovy.



For now we did not bridge our services to any implementation for our two applications. This will be done in the next step.

Code the applications

Script the PublishModule in groovy

The PublishModule:
  • sends cyclically the published service
  • subscribes to the event service
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"/>
We will create a publish.groovy script to 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();
      }

Specify the bridge between the PublishModule and its implementation

Now that we have coded the PublishModule, we must specify in the applications.xml XML file the bridge between this module and its implementation:
  • The groovy script which implements the PublishModule
      <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>

Code the EventModule

The EventModule:
  • sends the event service
  • subscribes to the published service
We will create an EventModule class to implement the behavior of this Module. The graphical representation will be in a separate subclass of JFrame to show the toggle button and the label showing the current value:
  • a subscribe method will be notified of the published Service. The value will set the text of the label
  • a publish method will be invoked when the user clicks on the toggle button, to send the for the event Service
      public class EventModule {
        private ServiceInstance eventService = null;
        private ServiceInstance publishService = null;
        private EventGUI gui = null;

        public void init(Module module) {
          eventService = module.getService("event");
          publishService = module.getService("published");
          gui = new EventGUI(this);
          gui.setup();
          gui.pack();
          gui.setVisible(true);
        }

        public void subscribe(ServiceInstance service) {
          int count = publishService.getData("value").getValueAsInt();
          gui.updateCount(count);
        }

        public void publish(boolean b) {
          eventService.setDataBooleanValue("event", b);
          eventService.invoke();
        }
      }
The associated GUI class has nothing specific to the framework:
      public class EventGUI extends JFrame {
        private JToggleButton button = null;
        private JLabel label = null;
        private EventModule module = null;

        public EventGUI(EventModule module) {
          super("Event GUI");
          this.module = module;
        }

        public void updateCount(int count) {
          label.setText(Integer.toString(count));
          label.invalidate();
          label.repaint();
        }

        public void setup() {
          Container pane = this.getContentPane();
          pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS));
          button = new JToggleButton("Positive Step");
          label = new JLabel("0");
          label.setHorizontalAlignment(JLabel.CENTER);
          pane.add(button);
          pane.add(Box.createHorizontalStrut(50));
          pane.add(label);
          pane.add(Box.createHorizontalStrut(50));
          button.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            if (button.isSelected()) {
              button.setText("Negative Step");
            } else {
              button.setText("Positive Step");
            }
            module.publish(button.isSelected());
          }
          });
        }
      }

Specify the bridge between the EventModule and its implementation

Now that we have coded the EventModule, we must specify in the applications.xml XML file the bridge between this module and its implementation:
  • The jar file which contains the eventAppli
  • The Java class which implements the EventModule
      <application name="eventAppli" id="1">
         <deployment>
            <lib url="samples1Event.jar" />
         </deployment>
         <modules>
            <module name="EventModule" id="1" >
               <implementation path="org.da.samples.protoframework.event.EventModule" >
      ...
               </implementation>
            </module>
         </modules>
      </application>
We must also specify:
  • That the init method will be called at the initialization of the Module
  • That the subscribe method will be called each time the Module is notified from the published Service
      <implementation path="org.da.samples.protoframework.publish.PublishModule" >
         <initEntryPoint method="init" />
         <defaultReceiveEntryPoint method="subscribe" />
      </implementation>

Finalize the XML configuration

We now have a final applications.xml XML file:
      <applications>
         <application name="eventAppli" id="1">
            <deployment>
               <lib url="samples1Event.jar" />
            </deployment>
            <modules>
               <module name="EventModule" id="1" >
                  <implementation path="org.da.samples.protoframework.event.EventModule" >
                     <initEntryPoint method="init" />
                     <defaultReceiveEntryPoint method="subscribe" />
                  </implementation>
                  <interfaces>
                     <eventSend service="event" attach="attach"/>
                     <subscribe service="published" />
                  </interfaces>
               </module>
            </modules>
         </application>
         <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>
      </applications>
In the last step, we will specify the filelist.xml for the configuration of the system, including:
  • The types.xml definition
  • The services.xml definition
  • The applications.xml definition
      <files>
         <file url="applications.xml" />
         <file url="services.xml" />
         <file url="types.xml" />
      </files>

Starting the framework

To start the framework, we must start the framework with our filelist.xml file for our configuration:
      java -jar protoframework.jar config=filelist.xml


tuto1

See also


Categories: tutorials

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