Statistical Algorithms Importer: Java Project FAQ

From Gcube Wiki
Jump to: navigation, search

F.A.Q. of Statistical Algorithms Importer (SAI), here are common mistakes we have found in Java Project.

Main Class

The first parameter of a Java process is the main class that will be executed, this integration is for java processes that accept arguments as a list. In other words, a Java program process will be called as:

  • java -jar arg1 arg2 arg3...

Please note the full package name must be entered as default value, for example:

  • org.d4science.projectx.XClass

If your process works like an executable that requires some parameters, you can consider to use the preinstalled software integration way, which allows you to write a shell script though which you can build the invocation to your executable(it is added as a resource of the project):

How to Use File Input

Add input file parameter in Java project:
File Input Parameter, SAI
Java source code in sample:
package org.myfactory.specialgroup;
 
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
 
/**
 * 
 * @author Giancarlo Panichi
 *
 */
public class FileConsumer {
 
	public static void main(String[] args) {
		try {
			Path fileInput=Paths.get(args[0]); //second parameter in the project
			Path fileOutput=Paths.get("output.txt");
			Files.copy(fileInput, fileOutput, StandardCopyOption.REPLACE_EXISTING);
 
		} catch (Throwable e) {
			System.out.println("Error in process: " + e.getLocalizedMessage());
			e.printStackTrace();
		}
	}
 
}
Java code in sample:

File:JavaBlackBox FileInputPrameter.zip

How to Use Enumerated Input

Consider the Las Vegas algorithm:
Las Vegas Info, SAI
Indicates the java version:
Las Vegas Interpreter, SAI
Indicates the I/O parameters:
Las Vegas I/O Parameters, SAI
DataMiner result:
Las Vegas on DataMiner, SAI


Java source code in Las Vegas:
package org.myfactory.specialgroup;
 
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.function.ToIntFunction;
import java.util.stream.Stream; 
 
/**
 * 
 * Las Vegas
 * 
 * @author Giancarlo Panichi
 *
 */
public class Casino {
 
	private static String game;
	private static boolean bluff;
	private static Path betsFile;
 
	private static void init(String g, String f, String b) {
		game = g;
		betsFile = Paths.get(f);
 
		try {
			bluff = Boolean.valueOf(b);
		} catch (Exception e) {
			bluff = false;
		}
	}
 
	private static ToIntFunction<String> play = new ToIntFunction<String>() {
 
		@Override
		public int applyAsInt(String beat) {
			Integer b = 0;
			try {
				b = Integer.valueOf(beat);
			} catch (NumberFormatException e) {
 
			}
 
			Integer winnings = 0;
			if (b > 0) {
				winnings = playSpecificGame(b);
			}
 
			return winnings;
		}
	};
 
	private static Integer playSpecificGame(Integer beat) {
		Integer winnings = 0;
		int factor = 0;
		switch (game) {
		case "slots":
			// 4
			factor = (int) (Math.random() * 4);
			if (factor > 2) {
				winnings = beat * 4;
			} else {
				winnings = 0;
			}
			break;
 
		case "roulette":
			// 38
			factor = (int) (Math.random() * 38);
			if (factor > 19) {
				winnings = beat * 38;
			} else {
				winnings = 0;
			}
			break;
 
		case "poker":
			// 52
			factor = (int) (Math.random() * 52);
			if (factor > 26 || (bluff && factor > 13)) {
				winnings = beat * 52;
			} else {
				winnings = 0;
			}
			break;
		default:
			winnings = 0;
			break;
 
		}
 
		return winnings;
	}
 
	public static void main(String[] args) {
 
		try {
			System.out.println("Las Vegas");
			System.out.println("Game: " + args[0]);
			System.out.println("Bets: " + args[1]);
			System.out.println("Bluff: " + args[2]);
 
			init(args[0], args[1],args[2]);
			Integer winnings = 0;
 
			// read stream
			try (Stream<String> stream = Files.lines(betsFile)) {
				winnings = stream.mapToInt(play::applyAsInt).sum();
			} catch (IOException e) {
				System.out.println("Error reading the file: " + e.getLocalizedMessage());
				e.printStackTrace();
			}
 
			String resultString = "You Won: " + winnings;
 
			Path result = Paths.get("win.txt");
			Files.write(result, resultString.getBytes(), StandardOpenOption.CREATE);
 
		} catch (Throwable e) {
			System.out.println("Error in process: " + e.getLocalizedMessage());
			e.printStackTrace();
		}
	}
 
}
bets.txt
100
50
40
200
10
30
400
result in win.txt:
You Won: 43160
Java code in Las Vegas:

File:JavaBlackBox LasVegas.zip

StorageHub

StorageHub is the service for accessing to the user's workspace. Below we show the StorageHubFacilityJava algorithm, it exhibits the interactions with StorageHub through its Rest API:
StorageHub Facility Java, SAI
Indicates the I/O parameters:
StorageHub Facility Java I/O parameters, SAI
Indicates the java version:
StorageHub Facility Java version, SAI
Indicates the code jar:
StorageHub Facility Java Code, SAI


DataMiner result:
StorageHub Facility Java in DataMiner, SAI
This algorithm shows 5 types of interactions with StorageHub:
  • Get Root Info
  • Get Item Info (requires an itemId as argument1)
  • Get Root Children
  • Get Item Children (requires an itemId as argument1)
  • Item Download (requires an itemId as argument1)
Java source code of StorageHubFacilityJava:
File:StorageHubFacilityJava.zip
View the demo in DataMiner:
StorageHub Facility Java