Cilia/Cilia Advanced/Adapter)Creation

From Wiki Adele Team
Jump to: navigation, search

Creating Adapters

As we have presented in the Cilia Overview adapters are in charge of connect a Cilia chain with the external world. In-adapters are in charge of collect data and then bring them to the Cilia chain, on the other hand out-adapters send the data manipulated by the chain to the external world.

In this section we will present how to define new adapters in the Cilia framework.

A Sensor In-Adapter

This example presents an in-adapter that collects data in a periodical way from a presence detector sensor. The sensor software is encapsulated into an OSGi service, the interface of service is as follow.

package fr.liglab.adele.cilia.sample.sensor;

public interface PresenceDetector {
      
   /**
    * Returns the Presence Detector ID
    * @return
    */
   public String getId();
   
   /**
    * Returns the Presence Detector Location
    * @return
    */
   public String getLocalisation();
}

Collector Implementation

In order to create a new in-adapter a Collector component must be implemented, the collector is in charge of get data from the Sensor service. To implement a Collector in Cilia developers must to extend the abstract class fr.liglab.adele.cilia.framework.AbstractCollector. In this example we will extend the class fr.liglab.adele.cilia.components.collectors.AbstractPullCollector that extends the base class and includes a periodic behavior.


The collector class looks like:

package fr.liglab.adele.cilia.sample.collector;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import fr.liglab.adele.cilia.Data;
import fr.liglab.adele.cilia.components.collectors.AbstractPullCollector;
import fr.liglab.adele.cilia.sample.sensor.PresenceDetector;


public class SensorCollector extends AbstractPullCollector {

   PresenceDetector sensor;
   
   /* (non-Javadoc)
    * @see fr.liglab.adele.cilia.components.collectors.AbstractPullCollector#pullData()
    */
   protected List pullData() throws IOException {
      String sensorData = sensor.getId() + sensor.getLocalisation();
      Data data = new Data(sensorData);      
      return Collections.singletonList(data);
   }

}

The collector will use a service of type sensor, in the DSL definition this requirement must be specified. The definition of Collector in Cilia DSL will be:

  <collector classname="fr.liglab.adele.cilia.sample.collector.SensorCollector" name="SensorCollector" >
    <requires field="sensor"/>
  </collector>

Adapter Definition

Once the collector has been defined we will define the in-adapter into the Cilia DSL language.

  <adapter name="PresenceDetectorAdapter" pattern="in-only">
    <collector type="SensorCollector" />
  </adapter>

Maven Dependencies

In order to build this example, the Maven POM file must defined all dependencies. In our case three dependencies must be specified, to the Cilia core, Cilia Compendium and finally to the artifact including the interface of presence sensor. Dependencies section of POM file should look like this:

  <dependencies>
    <dependency>
      <groupId>fr.liglab.adele.cilia</groupId>
      <artifactId>cilia-core</artifactId>
      <version>1.1.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>fr.liglab.adele.cilia</groupId>
      <artifactId>cilia-ipojo-compendium</artifactId>
      <version>1.1.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.medical</groupId>
      <artifactId>presence-detector</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>