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

From Gcube Wiki
Jump to: navigation, search
Line 26: Line 26:
 
<source lang="java">
 
<source lang="java">
 
ScopeProvider.instance.set("/gcube/devsec/devVRE");
 
ScopeProvider.instance.set("/gcube/devsec/devVRE");
StatisticalManagerFactory factory = StatisticalManagerDSL
+
StatisticalManagerFactory factory = StatisticalManagerDSL
.createStateful().build();
+
  .createStateful().build();
SMListGroupedAlgorithms groups = factory.getAlgorithms();
+
SMListGroupedAlgorithms groups = factory.getAlgorithms();
String algorithmId = null;
+
String algorithmId = null;
String algDescr = null;
+
String algDescr = null;
for (SMGroupedAlgorithms group : groups.getList()) {
+
for (SMGroupedAlgorithms group : groups.getList()) {
  
for (SMAlgorithm algorithm : group.getList()) {
+
for (SMAlgorithm algorithm : group.getList()) {
System.out.println(algorithm.getName());
+
System.out.println(algorithm.getName());
if (algorithm.getName().equals("NameAlgorithm")) {
+
if (algorithm.getName().equals("NameAlgorithm")) {
algorithmId = algorithm.getName();
+
algorithmId = algorithm.getName();
 
+
algDescr = algorithm.getDescription();
algDescr = algorithm.getDescription();
+
}
}
+
}
}
+
}
}
+
 
</source>
 
</source>
  
Line 49: Line 48:
 
<source lang="java">
 
<source lang="java">
 
SMParameters smParams = factory.getAlgorithmParameters(algorithmId);
 
SMParameters smParams = factory.getAlgorithmParameters(algorithmId);
SMInputEntry[] list = new SMInputEntry[smParams.getList().length];
+
SMInputEntry[] list = new SMInputEntry[smParams.getList().length];
int i = 0;
+
int i = 0;
for (SMParameter smParam : smParams.getList()) {
+
 
list[i++] = new SMInputEntry(smParam.getName(), "value");
+
for (SMParameter smParam : smParams.getList()) {
}
+
list[i++] = new SMInputEntry(smParam.getName(), "value");
 +
}
 
</source>
 
</source>
  
Line 61: Line 61:
 
<source lang="java">
 
<source lang="java">
 
SMComputationRequest request = new SMComputationRequest();
 
SMComputationRequest request = new SMComputationRequest();
SMComputationConfig config = new SMComputationConfig();
+
SMComputationConfig config = new SMComputationConfig();
config.setParameters(new SMEntries(list));
+
config.setParameters(new SMEntries(list));
config.setAlgorithm(algorithmId);
+
config.setAlgorithm(algorithmId);
request.setUser("user");
+
request.setUser("user");
request.setTitle(algorithmId);
+
request.setTitle(algorithmId);
request.setDescription(algDescr);
+
request.setDescription(algDescr);
request.setConfig(config);
+
request.setConfig(config);
String computationId = factory.executeComputation(request);
+
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>
 
</source>
  
Line 90: Line 75:
  
 
<source lang="java">
 
<source lang="java">
SMComputation computation = factory.getComputation(computationId);
+
SMComputation computation = factory.getComputation(computationId);
SMOperationStatus status = SMOperationStatus.values()[computation
+
SMOperationStatus status = SMOperationStatus.values()[computation
.getOperationStatus()];
+
    .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) {
+
  
 +
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:
 
case FILE:
 
SMFile fileRes = (SMFile) smResource;
 
SMFile fileRes = (SMFile) smResource;
Line 126: Line 108:
 
}
 
}
  
smResource.getName();
+
smResource.getName();
smResource.getResourceId();
+
smResource.getResourceId();
smResource.getDescription();
+
smResource.getDescription();
  
}
+
}
 
</source>
 
</source>
  
Line 138: Line 120:
 
<source lang="java">
 
<source lang="java">
 
File tempFile = new File("path");
 
File tempFile = new File("path");
 +
StatisticalManagerDataSpace dataSpace = StatisticalManagerDSL.dataSpace().build();;
 +
String user = "user";
 +
String template = "GENERIC";
  
StatisticalManagerDataSpace dataSpace = StatisticalManagerDSL.dataSpace().build();;
+
TableTemplates tableTemplate=null;
String user = "user";
+
for (TableTemplates t: TableTemplates.values())
String template = "GENERIC";
+
if (template.contentEquals(t.toString())) {
 
+
tableTemplate = t;
TableTemplates tableTemplate=null;
+
break;
for (TableTemplates t: TableTemplates.values())
+
}
if (template.contentEquals(t.toString())) {
+
//first row is header of column
tableTemplate = t;
+
boolean hasHeader=false;
break;
+
String delimiter=",";
}
+
String id = dataSpace.createTableFromCSV(tempFile, hasHeader, delimiter , "", "table name", tableTemplate, "table descritpion", user);
//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>
 
</source>
  

Revision as of 16:46, 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);

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