How to Interact with the Statistical Manager by client

From Gcube Wiki
Revision as of 22:25, 15 April 2014 by Gianpaolo.coro (Talk | contribs) (Maven coordinates)

Jump to: navigation, search

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>
<dependency>
   <groupId>org.gcube.core</groupId>
   <artifactId>common-fw-clients</artifactId>
   <version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
   <groupId>org.gcube.core</groupId>
   <artifactId>common-gcore-clients</artifactId>
   <version>[1.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
  <groupId>org.gcube.contentmanagement</groupId>
  <artifactId>storage-manager-wrapper</artifactId>
  <version>2.0.2-3.0.0</version>
</dependency>
<dependency>
  <groupId>org.gcube.contentmanagement</groupId>
  <artifactId>storage-manager-core</artifactId>
  <version>2.0.3-3.0.0</version>
</dependency>

Features

Get the list of algorithms

Given a VRE Scope in the e-Infrastructure (selected among these for Production and set to /gcube/devsec/devVRE for Development environment), 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 contained in the SMInputEntry Object. Furthermore, you can specify a user name and other optional metadata. The SM uses maps of Strings to specify all the input parameters.

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 the status of a computation and eventually 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 it for calculations. This operation is necessary to ensure high efficiency.

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