Difference between revisions of "How-to Implement Algorithms for the Statistical Manager"

From Gcube Wiki
Jump to: navigation, search
Line 5: Line 5:
 
Let's start creating a project using the eclipse IDE that is mavenized.
 
Let's start creating a project using the eclipse IDE that is mavenized.
 
After having mavenized the project in eclipse you have to put dependencies.
 
After having mavenized the project in eclipse you have to put dependencies.
====Step by Step====
+
====Maven coordinates ====
 
The maven artifact coordinates are:  
 
The maven artifact coordinates are:  
 
<source lang="java">
 
<source lang="java">
Line 49: Line 49:
 
}
 
}
 
</source>
 
</source>
 +
The  <code>init()</code> is the initialization method. In this simple example we need  to initialize log and we use logger from ecological engine library. In case algorithm uses database, we have to open its connection in this method.
 +
The  <code>shutdown()</code> closes database connection.
 +
In the <code>getDescription()</code> method we add a simple description for the algorithm.
 +
 +
=Customize input visualization =
 +
==== String input parameters ====
 +
User input is obtained  calling from <code>setInputParameters()</code> the method addStringInput with following parameters:
 +
* name of the variable ;
 +
* description for the variable;
 +
* default value;
 +
 +
User input is retrieved using <code>getInputParameter()</code> passing name used as parameter into <code>setInputParameters()</code>.
 +
<source lang="java">
 +
protected void setInputParameters() {
 +
addStringInput(NameOfVariable, "Description", "DefaultInput");
 +
 +
}
 +
</source>
 +
The input parameter will be automatically passed by Statistical Manager to the procedure.
 +
In particular, to process method we can retrieve such parameter by name that we set in addStringInput method.
 +
<source lang="java">
 +
@Override
 +
protected void process() throws Exception {
 +
....
 +
String userInputValue = getInputParameter(NameOfVariable);
 +
}
 +
</source>
 +
 +
==== Combo box input parameter ====
 +
In order to obtain a combo box  we  have to define enumerator that contains the possible
 +
choice that could be selected in the combo box and you have to pass it to the method <code>addEnumerateInput</code> as follow:
 +
 +
<source lang="java">
 +
public enum Enum {
 +
FIRST_ENUM,
 +
SECOND_ENUM
 +
}
 +
 +
protected void setInputParameters() {
 +
addEnumerateInput(Enum.values(), variableName, "Description",
 +
Enum.FIRST_ENUM.name());
 +
}
 +
</source>
 +
addEnumerateInput parameters are rispectivly:

Revision as of 12:07, 21 June 2013

Prerequisites

IDE: Eclipse Java EE IDE for Web Developers. Version: 3.7+

Step by Step

Let's start creating a project using the eclipse IDE that is mavenized. After having mavenized the project in eclipse you have to put dependencies.

Maven coordinates

The maven artifact coordinates are:

<dependency>
	<groupId>org.gcube.dataanalysis</groupId>
	<artifactId>ecological-engine</artifactId>
	<version>1.6.1-SNAPSHOT</version>
</dependency>

Let's start creating a new call which implements basic algorithm; it will be executed by Statistical Manager. Next step is to extend basic interface StandardLocalExternalAlgorithm. The following snippet shows unimplemented interface methods that we are going to fulfill.

public class SimpleAlgorithm extends StandardLocalExternalAlgorithm{
 
	@Override
	public void init() throws Exception {
		// TODO Auto-generated method stub		
	}
	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	protected void process() throws Exception {
		// TODO Auto-generated method stub
 
	}
	@Override
	protected void setInputParameters() {
		// TODO Auto-generated method stub
 
	}
	@Override
	public void shutdown() {
		// TODO Auto-generated method stub		
	}
	@Override
	public StatisticalType getOutput() {
		return null;
	}
}

The init() is the initialization method. In this simple example we need to initialize log and we use logger from ecological engine library. In case algorithm uses database, we have to open its connection in this method. The shutdown() closes database connection. In the getDescription() method we add a simple description for the algorithm.

Customize input visualization

String input parameters

User input is obtained calling from setInputParameters() the method addStringInput with following parameters:

  • name of the variable ;
  • description for the variable;
  • default value;

User input is retrieved using getInputParameter() passing name used as parameter into setInputParameters().

protected void setInputParameters() {
		addStringInput(NameOfVariable, "Description", "DefaultInput");
 
}

The input parameter will be automatically passed by Statistical Manager to the procedure. In particular, to process method we can retrieve such parameter by name that we set in addStringInput method.

@Override
protected void process() throws Exception {
....
String userInputValue = getInputParameter(NameOfVariable);
}

Combo box input parameter

In order to obtain a combo box we have to define enumerator that contains the possible choice that could be selected in the combo box and you have to pass it to the method addEnumerateInput as follow:

public enum Enum {
FIRST_ENUM,
SECOND_ENUM
}
 
protected void setInputParameters() {
addEnumerateInput(Enum.values(), variableName, "Description",
					Enum.FIRST_ENUM.name());
}

addEnumerateInput parameters are rispectivly: