Difference between revisions of "Statistical Algorithms Importer: Java Project FAQ"
(→StorageHub) |
|||
(42 intermediate revisions by 2 users not shown) | |||
Line 3: | Line 3: | ||
|} | |} | ||
− | F.A.Q. of Statistical Algorithms Importer (SAI), here are common mistakes we have found in Java Project. | + | F.A.Q. of [[Statistical_Algorithms_Importer|Statistical Algorithms Importer (SAI)]], here are common mistakes we have found in Java Project. |
− | + | ||
== Main Class == | == Main Class == | ||
− | The first parameter of a Java process is the main class that will be executed. | + | 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): |
+ | *[[Statistical Algorithms Importer: Pre-Installed Project|Pre-Installed Project]] | ||
+ | *[[Statistical Algorithms Importer: Pre-Installed Project FAQ|Pre-Installed Project FAQ]] | ||
− | + | == How to Use File Input == | |
+ | :Add input file parameter in Java project: | ||
+ | [[Image:StatisticalAlgorithmsImporter_JavaBlackBox_FileInputParameter.png|thumb|center|750px|File Input Parameter, SAI]] | ||
+ | |||
+ | :Java source code in sample: | ||
+ | |||
+ | <source lang='java'> | ||
package org.myfactory.specialgroup; | package org.myfactory.specialgroup; | ||
Line 23: | Line 31: | ||
import java.nio.file.Paths; | import java.nio.file.Paths; | ||
import java.nio.file.StandardCopyOption; | import java.nio.file.StandardCopyOption; | ||
− | |||
− | |||
/** | /** | ||
Line 35: | Line 41: | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
try { | try { | ||
− | Path fileInput=Paths.get(args[0]); | + | Path fileInput=Paths.get(args[0]); //second parameter in the project |
Path fileOutput=Paths.get("output.txt"); | Path fileOutput=Paths.get("output.txt"); | ||
Files.copy(fileInput, fileOutput, StandardCopyOption.REPLACE_EXISTING); | Files.copy(fileInput, fileOutput, StandardCopyOption.REPLACE_EXISTING); | ||
Line 46: | Line 52: | ||
} | } | ||
+ | </source> | ||
+ | |||
+ | :Java code in sample: | ||
+ | [[File:JavaBlackBox_FileInputPrameter.zip|JavaBlackBox_FileInputPrameter.zip]] | ||
+ | |||
+ | == How to Use Enumerated Input == | ||
+ | :Consider the Las Vegas algorithm: | ||
+ | [[Image:StatisticalAlgorithmsImporter_LasVegas-0.png|thumb|center|750px|Las Vegas Info, SAI]] | ||
+ | |||
+ | :Indicates the java version: | ||
+ | [[Image:StatisticalAlgorithmsImporter_LasVegas-1.png|thumb|center|750px|Las Vegas Interpreter, SAI]] | ||
+ | |||
+ | :Indicates the I/O parameters: | ||
+ | [[Image:StatisticalAlgorithmsImporter_LasVegas-2.png|thumb|center|750px|Las Vegas I/O Parameters, SAI]] | ||
+ | |||
+ | :DataMiner result: | ||
+ | [[Image:StatisticalAlgorithmsImporter_LasVegas-3.png|thumb|center|750px|Las Vegas on DataMiner, SAI]] | ||
+ | |||
+ | |||
+ | :Java source code in Las Vegas: | ||
+ | |||
+ | <source lang='java'> | ||
+ | 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(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </source> | ||
+ | |||
+ | :bets.txt | ||
+ | <pre style="display:block;font-family:monospace;white-space:pre;margin:1em 0;"> | ||
+ | 100 | ||
+ | 50 | ||
+ | 40 | ||
+ | 200 | ||
+ | 10 | ||
+ | 30 | ||
+ | 400 | ||
</pre> | </pre> | ||
+ | :result in win.txt: | ||
+ | <pre style="display:block;font-family:monospace;white-space:pre;margin:1em 0;"> | ||
+ | You Won: 43160 | ||
+ | </pre> | ||
+ | |||
+ | :Java code in Las Vegas: | ||
+ | [[File:JavaBlackBox_LasVegas.zip|JavaBlackBox_LasVegas.zip]] | ||
+ | |||
+ | == StorageHub == | ||
+ | :[[StorageHub REST API|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: | ||
+ | [[Image:StorageHubFacilityJava0.png|thumb|center|750px|StorageHub Facility Java, SAI]] | ||
+ | |||
+ | :Indicates the I/O parameters: | ||
+ | [[Image:StorageHubFacilityJava1.png|thumb|center|750px|StorageHub Facility Java I/O parameters, SAI]] | ||
+ | |||
+ | :Indicates the java version: | ||
+ | [[Image:StorageHubFacilityJava2.png|thumb|center|750px|StorageHub Facility Java version, SAI]] | ||
+ | |||
+ | :Indicates the code jar: | ||
+ | [[Image:StorageHubFacilityJava3.png|thumb|center|750px|StorageHub Facility Java Code, SAI]] | ||
+ | |||
+ | |||
+ | :DataMiner result: | ||
+ | [[Image:StorageHubFacilityJava4.png|thumb|center|750px|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|StorageHubFacilityJava.zip]] | ||
+ | |||
+ | :View the demo in DataMiner: | ||
+ | :[https://services.d4science.org/group/rprototypinglab/data-miner?OperatorId=org.gcube.dataanalysis.wps.statisticalmanager.synchserver.mappedclasses.transducerers.STORAGEHUBFACILITYJAVA StorageHub Facility Java] | ||
[[Category:Statistical Algorithms Importer]] | [[Category:Statistical Algorithms Importer]] |
Latest revision as of 15:00, 22 November 2018
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:
- 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:
- Indicates the java version:
- Indicates the I/O parameters:
- DataMiner result:
- 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:
- Indicates the I/O parameters:
- Indicates the java version:
- Indicates the code jar:
- DataMiner result:
- 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