Difference between revisions of "How to Interact with the Statistical Manager by client"

From Gcube Wiki
Jump to: navigation, search
(Scope)
Line 19: Line 19:
 
</dependency>
 
</dependency>
 
</source>
 
</source>
 +
 +
=Features=
 +
 +
====Get the list of algorithms ====
 +
Given a VRE Scope in the e-Infrastructure, the list of available algorithms is retrievable in the following way:
 +
<source lang="java">
 +
ScopeProvider.instance.set("/gcube/devsec/devVRE");
 +
StatisticalManagerFactory factory = StatisticalManagerDSL
 +
.createStateful().build();
 +
SMListGroupedAlgorithms groups = factory.getAlgorithms();
 +
String algorithmId = null;
 +
String algDescr = null;
 +
for (SMGroupedAlgorithms group : groups.getList()) {
 +
 +
for (SMAlgorithm algorithm : group.getList()) {
 +
System.out.println(algorithm.getName());
 +
if (algorithm.getName().equals("NameAlgorithm")) {
 +
algorithmId = algorithm.getName();
 +
 +
algDescr = algorithm.getDescription();
 +
}
 +
}
 +
}
 +
</source>
 +
 +
====Get the Inputs of an Algorithm ====
 +
Referring to the previous code, once selected an algorithm, it is possible to retrieve its parameters in the following way:
 +
 +
<source lang="java">
 +
SMParameters smParams = factory.getAlgorithmParameters(algorithmId);
 +
SMInputEntry[] list = new SMInputEntry[smParams.getList().length];
 +
int i = 0;
 +
for (SMParameter smParam : smParams.getList()) {
 +
list[i++] = new SMInputEntry(smParam.getName(), "value");
 +
}
 +
</source>
 +
 +
====Execute an Algorithm ====
 +
Before invoking an algoritm, you must setup it by reusing the Maps of Strings in the SMInputEntry Object, according to the parameters required by the algorithm. Furthermore, you can specify a user name and other optional metadata.
 +
 +
<source lang="java">
 +
SMComputationRequest request = new SMComputationRequest();
 +
SMComputationConfig config = new SMComputationConfig();
 +
config.setParameters(new SMEntries(list));
 +
config.setAlgorithm(algorithmId);
 +
request.setUser("user");
 +
request.setTitle(algorithmId);
 +
request.setDescription(algDescr);
 +
request.setConfig(config);
 +
String computationId = factory.executeComputation(request);
 +
</source>
 +
 +
====Execute an Algorithm ====
 +
Before invoking an algoritm, you must setup it by reusing the Maps of Strings in the SMInputEntry Object, according to the parameters required by the algorithm. All the input parameters are threated as Strings. Furthermore, you can specify a user name and other optional metadata.
 +
 +
<source lang="java">
 +
SMComputationRequest request = new SMComputationRequest();
 +
SMComputationConfig config = new SMComputationConfig();
 +
config.setParameters(new SMEntries(list));
 +
config.setAlgorithm(algorithmId);
 +
request.setUser("user");
 +
request.setTitle(algorithmId);
 +
request.setDescription(algDescr);
 +
request.setConfig(config);
 +
String computationId = factory.executeComputation(request);
 +
</source>
 +
 +
====Retrieve the Status and the Output====
 +
Follow this code to check for the status of the computation and eventually to retrieve the output:
 +
 +
<source lang="java">
 +
SMComputation computation = factory.getComputation(computationId);
 +
SMOperationStatus status = SMOperationStatus.values()[computation
 +
.getOperationStatus()];
 +
 +
float percentage = 0;
 +
if (status == SMOperationStatus.RUNNING) {
 +
SMOperationInfo infos = factory.getComputationInfo(computationId,
 +
"user");
 +
percentage = Float.parseFloat(infos.getPercentage());
 +
} else if (status == SMOperationStatus.COMPLETED) {
 +
SMAbstractResource abstractResource = computation
 +
.getAbstractResource();
 +
SMResource smResource = abstractResource.getResource();
 +
int resourceTypeIndex = smResource.getResourceType();
 +
SMResourceType smResType = SMResourceType.values()[resourceTypeIndex];
 +
 +
switch (smResType) {
 +
 +
case FILE:
 +
SMFile fileRes = (SMFile) smResource;
 +
break;
 +
case OBJECT:
 +
SMObject objRes = (SMObject) smResource;
 +
 +
if (objRes.getName().contentEquals(
 +
PrimitiveTypes.MAP.toString())) {
 +
} else if (objRes.getName().contentEquals(
 +
PrimitiveTypes.IMAGES.toString())) {
 +
} else
 +
 +
break;
 +
case TABULAR:
 +
SMTable tableRes = (SMTable) smResource;
 +
break;
 +
}
 +
 +
smResource.getName();
 +
smResource.getResourceId();
 +
smResource.getDescription();
 +
 +
}
 +
</source>
 +
 +
====Import your own data====
 +
You can import a dataset on the SM in order to (i) store it in the e-Infrastructure and (ii) to use that for calculations. This operation enhances efficiency performances.
 +
 +
<source lang="java">
 +
File tempFile = new File("path");
 +
 +
StatisticalManagerDataSpace dataSpace = StatisticalManagerDSL.dataSpace().build();;
 +
String user = "user";
 +
String template = "GENERIC";
 +
 +
TableTemplates tableTemplate=null;
 +
for (TableTemplates t: TableTemplates.values())
 +
if (template.contentEquals(t.toString())) {
 +
tableTemplate = t;
 +
break;
 +
}
 +
//first row is header of column
 +
boolean hasHeader=false;
 +
String delimiter=",";
 +
String id = dataSpace.createTableFromCSV(tempFile, hasHeader, delimiter , "", "table name", tableTemplate, "table descritpion", user);
 +
</source>
 +
  
 
= Related Links =
 
= Related Links =

Revision as of 15:43, 6 November 2013

Prerequisites

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

Introduction

Here we show how to invoke an algorithm residing on the Statistical Manager (SM), from outside, by means of a thin Java client. Let's start creating a project using the eclipse IDE that is mavenized according to our indications. After having mavenized the project in eclipse you have to add the following dependency for the SM client library.

Maven coordinates

The maven artifact coordinates are:

<dependency>
   <groupId>org.gcube.data.analysis</groupId>
   <artifactId>statistical-manager-cl</artifactId>
   <version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
</dependency>

Features

Get the list of algorithms

Given a VRE Scope in the e-Infrastructure, the list of available algorithms is retrievable in the following way:

ScopeProvider.instance.set("/gcube/devsec/devVRE");
		StatisticalManagerFactory factory = StatisticalManagerDSL
				.createStateful().build();
		SMListGroupedAlgorithms groups = factory.getAlgorithms();
		String algorithmId = null;
		String algDescr = null;
		for (SMGroupedAlgorithms group : groups.getList()) {
 
			for (SMAlgorithm algorithm : group.getList()) {
				System.out.println(algorithm.getName());
				if (algorithm.getName().equals("NameAlgorithm")) {
					algorithmId = algorithm.getName();
 
					algDescr = algorithm.getDescription();
				}
			}
		}

Get the Inputs of an Algorithm

Referring to the previous code, once selected an algorithm, it is possible to retrieve its parameters in the following way:

SMParameters smParams = factory.getAlgorithmParameters(algorithmId);
		SMInputEntry[] list = new SMInputEntry[smParams.getList().length];
		int i = 0;
		for (SMParameter smParam : smParams.getList()) {
			list[i++] = new SMInputEntry(smParam.getName(), "value");
		}

Execute an Algorithm

Before invoking an algoritm, you must setup it by reusing the Maps of Strings in the SMInputEntry Object, according to the parameters required by the algorithm. Furthermore, you can specify a user name and other optional metadata.

SMComputationRequest request = new SMComputationRequest();
		SMComputationConfig config = new SMComputationConfig();
		config.setParameters(new SMEntries(list));
		config.setAlgorithm(algorithmId);
		request.setUser("user");
		request.setTitle(algorithmId);
		request.setDescription(algDescr);
		request.setConfig(config);
		String computationId = factory.executeComputation(request);

Execute an Algorithm

Before invoking an algoritm, you must setup it by reusing the Maps of Strings in the SMInputEntry Object, according to the parameters required by the algorithm. All the input parameters are threated as Strings. Furthermore, you can specify a user name and other optional metadata.

SMComputationRequest request = new SMComputationRequest();
		SMComputationConfig config = new SMComputationConfig();
		config.setParameters(new SMEntries(list));
		config.setAlgorithm(algorithmId);
		request.setUser("user");
		request.setTitle(algorithmId);
		request.setDescription(algDescr);
		request.setConfig(config);
		String computationId = factory.executeComputation(request);

Retrieve the Status and the Output

Follow this code to check for the status of the computation and eventually to retrieve the output:

		SMComputation computation = factory.getComputation(computationId);
		SMOperationStatus status = SMOperationStatus.values()[computation
				.getOperationStatus()];
 
		float percentage = 0;
		if (status == SMOperationStatus.RUNNING) {
			SMOperationInfo infos = factory.getComputationInfo(computationId,
					"user");
			percentage = Float.parseFloat(infos.getPercentage());
		} else if (status == SMOperationStatus.COMPLETED) {
			SMAbstractResource abstractResource = computation
					.getAbstractResource();
			SMResource smResource = abstractResource.getResource();
			int resourceTypeIndex = smResource.getResourceType();
			SMResourceType smResType = SMResourceType.values()[resourceTypeIndex];
 
			switch (smResType) {
 
			case FILE:
				SMFile fileRes = (SMFile) smResource;
				break;
			case OBJECT:
				SMObject objRes = (SMObject) smResource;
 
				if (objRes.getName().contentEquals(
						PrimitiveTypes.MAP.toString())) {
				} else if (objRes.getName().contentEquals(
						PrimitiveTypes.IMAGES.toString())) {
				} else
 
					break;
			case TABULAR:
				SMTable tableRes = (SMTable) smResource;
				break;
			}
 
			smResource.getName();
			smResource.getResourceId();
			smResource.getDescription();
 
		}

Import your own data

You can import a dataset on the SM in order to (i) store it in the e-Infrastructure and (ii) to use that for calculations. This operation enhances efficiency performances.

File tempFile = new File("path");
 
		StatisticalManagerDataSpace dataSpace = StatisticalManagerDSL.dataSpace().build();;
		String user = "user";
		String template = "GENERIC";
 
		TableTemplates tableTemplate=null;
		for (TableTemplates t: TableTemplates.values())
			if (template.contentEquals(t.toString())) {
				tableTemplate = t;
				break;
			}
		//first row is header of column
		boolean hasHeader=false;
		String delimiter=",";
		String id = dataSpace.createTableFromCSV(tempFile, hasHeader, delimiter , "", "table name", tableTemplate, "table descritpion", user);


Related Links

Statistical Manager Tutorial

How-to Implement Algorithms for the Statistical_Manager