package org.ngbw.examples; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.concurrent.Future; import org.ngbw.sdk.UserAuthenticationException; import org.ngbw.sdk.Workbench; import org.ngbw.sdk.WorkbenchSession; import org.ngbw.sdk.common.util.Resource; import org.ngbw.sdk.common.util.ResourceNotFoundException; import org.ngbw.sdk.core.shared.TaskRunStage; import org.ngbw.sdk.database.Folder; import org.ngbw.sdk.database.Task; import org.ngbw.sdk.database.TaskInputSourceDocument; import org.ngbw.sdk.database.TaskLogMessage; import org.ngbw.sdk.database.TaskOutputSourceDocument; import org.ngbw.sdk.database.User; public class TestTools { private static final String USERNAME = "t"; private static final String PASSWORD = "t"; //Workbench object instances private static Workbench workbench = Workbench.getInstance(); private static WorkbenchSession session; private static Folder folder; //global map for all parameters of all tools private static Map> toolParameterMap; //global map of all input parameters to their fileNames private static Map> inputFileNameMap; //filenames versus file data private static Map inputDataMap; //read parameter properties file into map static { String parameterPropertiesFile = "examples/tooltest/test.properties"; //example parameter.properties //DSSP.classic_ = false //DSSP.surface_ = false //getting properties from properties file Properties parameterProperties; try { parameterProperties = Resource.getResource(parameterPropertiesFile).getProperties(); } catch (ResourceNotFoundException e) { //that file cannot be found throw new RuntimeException(e.toString(), e); } //there might still be an exception if it is not a valid properties file readParameterProperties(parameterProperties); String inputDataPropertiesFile = "examples/tooltest/testInputFiles.properties"; //example parameter.properties //DSSP.pdbfile_ = path/to/pdbfile.pdb //getting properties from properties file Properties inputDataProperties; try { inputDataProperties = Resource.getResource(inputDataPropertiesFile).getProperties(); } catch (ResourceNotFoundException e) { //that file cannot be found throw new RuntimeException(e.toString(), e); } //there might still be an exception if it is not a valid properties file readInputDataProperties(inputDataProperties); } public static void main(String[] args) { try { /*================================================================ * Login *================================================================*/ try { session = workbench.getSession(USERNAME, PASSWORD); } catch (UserAuthenticationException e) { System.err.println(e.toString()); System.exit(-1); } User user = session.getUser(); /*================================================================ * Folder setup *================================================================*/ String folderLabel = "Test Folder"; //Folder folder = findFolderByLabel(user.findFolders(), folderLabel); folder = findFolderByLabel(user.findFolders(), folderLabel); if (folder == null) { folder = new Folder(user); folder.setLabel(folderLabel); folder.save(); } Collection arglist = null; if (args.length > 0) { arglist = Arrays.asList(args); } //loop over all tools. if we have command line args, skip tools not given on command line, otherwise // process all. try { int i=0; for (String toolId : toolParameterMap.keySet()) { if ((arglist != null) && !arglist.contains(toolId)) { continue; } System.out.println(); System.out.println("TESTING: "+toolId); System.out.println(); i++; System.out.println("TOOL # "+i); System.out.println(); //test a tool Task task = runCommand(toolId); Map> output = task.output(); if (output !=null) { for (String parameter : output.keySet()) { System.out.println("Parameter: " + parameter); List outputData = output.get(parameter); for (TaskOutputSourceDocument file : outputData) { System.out.println("Filename: " + file.getName()); //enter you display routine here } } } task.delete(); } } catch (RuntimeException e) { System.out.println(); System.out.println("TESTING FAILED"); System.out.println(); e.printStackTrace(); } } catch (Exception err) { err.printStackTrace(System.err); System.exit(-1); } } //wrapper for pulling everything together private static Task runCommand(String toolId) throws SQLException, IOException { Task task = new Task(folder); task.setToolId(toolId); task.setLabel(toolId + "Task-" + System.currentTimeMillis()); task.toolParameters().putAll(toolParameterMap.get(toolId)); task.input().putAll(getInputData(toolId)); task.setStage(TaskRunStage.READY); Future taskResult = workbench.saveAndSubmitTask(task, folder); try { task = taskResult.get(); } catch (Exception err) { throw new RuntimeException(err); } for (TaskLogMessage msg : task.logMessages()) System.out.println(msg); return task; } //return map that keys input data to input parameter private static Map> getInputData(String toolId) { if (toolId == null) throw new NullPointerException("Tool is null!"); if (inputFileNameMap.containsKey(toolId) == false) throw new NullPointerException("Tool + " + toolId + " is not registered"); Map> inputData = new HashMap>(); for (String parameter : inputFileNameMap.get(toolId).keySet()) { String fileName = inputFileNameMap.get(toolId).get(parameter); byte[] docData = inputDataMap.get(fileName); if (docData == null) throw new NullPointerException("No data registered for " + fileName); List paramInput = new ArrayList(); paramInput.add(new TaskInputSourceDocument(fileName, docData)); inputData.put(parameter, paramInput); } return inputData; } //resolve properties into parameter map private static void readParameterProperties(Properties properties) { toolParameterMap = new HashMap>(); Enumeration propNames = properties.propertyNames(); while (propNames.hasMoreElements()) { String propertyKey = (String) propNames.nextElement(); String value = properties.getProperty(propertyKey); String[] combo = propertyKey.split("\\."); if (combo.length != 2) throw new RuntimeException("Invalid property key: " + propertyKey); String toolName = combo[0]; String parameter = combo[1]; if (toolParameterMap.containsKey(toolName) == false) toolParameterMap.put(toolName, new HashMap()); Map parameters = toolParameterMap.get(toolName); parameters.put(parameter, value); } } //resolve input file names properties into inputData map private static void readInputDataProperties(Properties properties) { inputFileNameMap = new HashMap>(); inputDataMap = new HashMap(); Enumeration propNames = properties.propertyNames(); while (propNames.hasMoreElements()) { String propertyKey = (String) propNames.nextElement(); String inputFileName = properties.getProperty(propertyKey); String[] combo = propertyKey.split("\\."); if (combo.length != 2) throw new RuntimeException("Invalid property key: " + propertyKey); String toolName = combo[0]; String parameter = combo[1]; if (inputDataMap.containsKey(inputFileName) == false) { byte[] data; //get a local data file into a byte[] try { data = Resource.getResource(inputFileName).getBytes(); } catch (ResourceNotFoundException e) { //that file cannot be found throw new RuntimeException(e.toString(), e); } inputDataMap.put(inputFileName, data); } if (inputFileNameMap.containsKey(toolName) == false) inputFileNameMap.put(toolName, new HashMap()); Map parameters = inputFileNameMap.get(toolName); parameters.put(parameter, inputFileName); } } private static Folder findFolderByLabel(List folders, String label) { for (Iterator elements = folders.iterator() ; elements.hasNext() ; ) { Folder folder = elements.next(); if (folder.getLabel().equals(label)) return folder; } return null; } }