Closedown

This commit is contained in:
boccioli_m
2015-06-29 16:55:06 +02:00
parent a1eabd0ca7
commit 9a8f34a5da
6 changed files with 216 additions and 120 deletions

View File

@@ -6,9 +6,7 @@ import ch.psi.pshell.core.Controller;
import ch.psi.utils.swing.MonitoredPanel;
import ch.psi.pshell.ui.Panel;
import ch.psi.utils.swing.SwingUtils;
import ch.psi.wsaf.ApplicationStateException;
import ch.psi.wsaf.Task;
import ch.psi.wsaf.TaskRunningException;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
@@ -28,18 +26,19 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;
import java.util.logging.FileHandler;
import java.util.logging.SimpleFormatter;
import javax.script.ScriptException;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableCellRenderer;
import org.apache.commons.io.FilenameUtils;
import org.python.core.PyList;
import static org.python.bouncycastle.util.Arrays.append;
/**
@@ -87,20 +86,13 @@ public class TestingList extends Panel {
boolean status = (boolean) eval("status");
Object deviceName = eval("DEVICE");
String sStatus = (status == true) ? TestStatus.SUCCESS.toString() : TestStatus.FAILURE.toString();
// if (exception!=null){
// SwingUtils.showMessage(getComponent(), "Error", String.valueOf(exception));
// } else {
// SwingUtils.showMessage(getComponent(), "onExecutedFile()", String.valueOf(result));
// }
if (ret != "") {
//SwingUtils.showMessage(getComponent(), "", ret.toString() + " - " + ret.getClass().getName());
iCurrentTestPos = testingList.showResult(deviceName.toString(), fileName, ret.toString(), sStatus);
System.out.println("received end of test for test "+fileName);
// if (ret != "") {
//start next test
if (testingList.isTestRunAllowed()) {
testingList.executeTest(iCurrentTestPos + 1);
if (testingList.isTestRunAllowed() && testingList.runningTestsCount()==0) {
testingList.executeTest(iCurrentTestPos);
}
} else { // ret empty means that either the test script does not have variable ret, or that the script could not be started at all
/* } else { // ret empty means that either the test script does not have variable ret, or that the script could not be started at all
ret = "Could not start test script or script doea not contain default variables";
sStatus = TestStatus.FAILURE.toString();
String[] dsDeviceName = testingList.getTestInProgress();
@@ -108,12 +100,12 @@ public class TestingList extends Panel {
if (dsDeviceName[0] != "") {
iCurrentTestPos = testingList.showResult(dsDeviceName[0], fileName, ret.toString(), sStatus);
//start next test
if (testingList.isTestRunAllowed()) {
testingList.executeTest(iCurrentTestPos + 1);
if (testingList.isTestRunAllowed() && testingList.runningTestsCount()==0) {
testingList.executeTest(iCurrentTestPos);
}
}
}
} catch (Exception ex) {
*/ } catch (Exception ex) {
String ret = "Could not start test script";
String sStatus = TestStatus.FAILURE.toString();
String[] dsDeviceName = testingList.getTestInProgress();
@@ -490,20 +482,22 @@ public class TestingList extends Panel {
}//GEN-LAST:event_jCheckBox1ActionPerformed
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
try {
switch (evt.getClickCount()) {
case 1:
int colIndex = jTable1.getSelectedColumn();
if (colIndex == COL.CHECK.ordinal()) {
updateStatus();
}
break;
case 2:
openDetails();
break;
if(this.jTable1.isEnabled()){
try {
switch (evt.getClickCount()) {
case 1:
int colIndex = jTable1.getSelectedColumn();
if (colIndex == COL.CHECK.ordinal()) {
updateStatus();
}
break;
case 2:
openDetails();
break;
}
} catch (Exception ex) {
SwingUtils.showException(this, ex);
}
} catch (Exception ex) {
SwingUtils.showException(this, ex);
}
}//GEN-LAST:event_jTable1MouseClicked
@@ -582,7 +576,7 @@ public class TestingList extends Panel {
* running
*/
public boolean isTestRunAllowed() {
return this.jButtonRun.getToolTipText().equals("Stop tests");
return (this.jButtonRun.getToolTipText().equals("Stop tests") && pendingTestsCount()>0);
}
//move selection up in table
@@ -660,6 +654,14 @@ public class TestingList extends Panel {
int rowD = -1;
String sTestName = testName;
logger.log(Level.FINE, "Looking for: deviceName: " + deviceName + "; testPath: " + testName + " in table.");
String sStatus;
if(status == "true"){
sStatus = TestStatus.SUCCESS.toString();
} else if (status == "false"){
sStatus = TestStatus.FAILURE.toString();
} else{
sStatus = status;
}
//search for device name in table
for (int row = 0; row <= jTable1.getRowCount() - 1; row++) {
if (deviceName.equals(jTable1.getValueAt(row, COL.DEVICENAME.ordinal()))
@@ -670,14 +672,14 @@ public class TestingList extends Panel {
}
}
ImageIcon icon = new ImageIcon();
switch (status) {
switch (sStatus) {
case "Success":
icon = TestStatus.SUCCESS.Icon();
logger.log(Level.INFO, status + " - Device: " + deviceName + "; Test: " + sTestName + "; Result: " + res);
logger.log(Level.INFO, sStatus + " - Device: " + deviceName + "; Test: " + sTestName + "; Result: " + res);
break;
case "Failure":
icon = TestStatus.FAILURE.Icon();
logger.log(Level.SEVERE, status + " - Device: " + deviceName + "; Test: " + sTestName + "; Result: " + res);
logger.log(Level.SEVERE, sStatus + " - Device: " + deviceName + "; Test: " + sTestName + "; Result: " + res);
break;
case "Running":
icon = TestStatus.RUNNING.Icon();
@@ -688,12 +690,46 @@ public class TestingList extends Panel {
jTable1.setValueAt(icon, rowD, COL.ICON.ordinal());
jTable1.setValueAt(getNow(), rowD, COL.TIME.ordinal());
jTable1.setValueAt(res, rowD, COL.RESULT.ordinal());
jTable1.setValueAt(status, rowD, COL.STATUS.ordinal());
jTable1.setValueAt(sStatus, rowD, COL.STATUS.ordinal());
} else {
logger.log(Level.SEVERE, "Cant find Test: " + testName + " in table.");
}
//check if there are still pending tests. If not, set the status of the tool to Stopped.
if (rowD >= jTable1.getRowCount()-1 || pendingTestsCount()==0) {
setToStopped();
}
return rowD;
}
//returns the amount of tests currently in Pending state
public int pendingTestsCount(){
return testsStatusCount(TestStatus.PENDING);
}
//returns the amount of tests currently in Pending state
public int runningTestsCount(){
return testsStatusCount(TestStatus.RUNNING);
}
//returns the amount of tests currently in Pending state
public int successTestsCount(){
return testsStatusCount(TestStatus.SUCCESS);
}
//returns the amount of tests currently in "status" state
private int testsStatusCount(TestStatus status){
String sStatus;
boolean bSelected;
int iPendingTestsCount = 0;
for (int row = 0; row < jTable1.getRowCount(); row++) {
bSelected = (boolean) jTable1.getValueAt(row, COL.CHECK.ordinal());
sStatus = jTable1.getValueAt(row, COL.STATUS.ordinal()).toString();
if(bSelected && sStatus == status.toString()){
iPendingTestsCount++;
}
}
return iPendingTestsCount;
}
/**
* find the test currently in progress
@@ -712,7 +748,7 @@ public class TestingList extends Panel {
}
}
} catch (Exception ex) {
this.setButtonToStop();
this.setToStopped();
SwingUtils.showMessage(this, "getTestInProgress()", ex.toString());
} finally {
return dsTestProperties;
@@ -825,8 +861,7 @@ public class TestingList extends Panel {
}
}
if (!bSelected) { //No test to play. Stop
setButtonToStop();
logger.log(Level.INFO, "End of tests.");
setToStopped();
}
}
@@ -836,7 +871,7 @@ public class TestingList extends Panel {
String sStartSequence, sStatus;
int[] selectedTestsRows = {};
//scan through the table starting from 'position' and execute the first selected test found
int row = position;
int row = 0;// position;
if (row >= 0 && row < jTable1.getRowCount()) {
for (row = position; row < jTable1.getRowCount(); row++) {
bSelected = (boolean) jTable1.getValueAt(row, COL.CHECK.ordinal());
@@ -849,14 +884,14 @@ public class TestingList extends Panel {
//System.out.println(String.valueOf(row) + "\t" + String.valueOf(bSelected) + "\t" + String.valueOf(selectedTestsRows.length) + "\t" + sStartSequence + "\t" + sStatus);
if (bSelected
&& sStatus.equals(TestStatus.PENDING.toString())
&& (selectedTestsRows.length == 0 || //the test must be: selected, pending, first of the list.
sStartSequence.equals(StartSequence.START_SEQ_TOGETHER.toString()))) { //or the test must be: selected, pending, set as start with previous
&& sStatus.equals(TestStatus.PENDING.toString())
&& (selectedTestsRows.length == 0 || //the test must be: selected, pending, first of the list.
sStartSequence.equals(StartSequence.START_SEQ_TOGETHER.toString()))) { //or the test must be: selected, pending, set as start with previous
selectedTestsRows = append(selectedTestsRows, row);
} else if (bSelected
&& sStatus.equals(TestStatus.PENDING.toString()) &&//if this test must be executed...
selectedTestsRows.length > 0 && //but there are already tests to be executed in parallel....
(sStartSequence.equals(StartSequence.START_SEQ_AFTER.toString()))) { //...and this test must be executed in series, then stop searching
&& sStatus.equals(TestStatus.PENDING.toString()) &&//if this test must be executed...
selectedTestsRows.length > 0 && //but there are already tests to be executed in parallel....
(sStartSequence.equals(StartSequence.START_SEQ_AFTER.toString()))) { //...and this test must be executed in series, then stop searching
break;
}
}
@@ -866,16 +901,21 @@ public class TestingList extends Panel {
}
}
}
if (!bSelected) { //No test to play. Stop
setButtonToStop();
logger.log(Level.INFO, "End of tests.");
}
}
private void setToStopped(){
//No more tests to play. Stop
setButtonToStop();
logger.log(Level.INFO, "End of tests.");
}
//start all the tests in the rowsToExecute
private int executeParallelTestsGroup(int[] rowsToExecute) {
int iRet = -1;
HashMap args;
HashMap args = new HashMap(); //this is the global map that will contain one map per test.
HashMap testArgs; //this is the map for a test.
RunTest runTest;
for (int row : rowsToExecute) {
String sDeviceName = jTable1.getValueAt(row, COL.DEVICENAME.ordinal()).toString();
@@ -901,26 +941,14 @@ public class TestingList extends Panel {
logger.log(Level.INFO, "Running Test '" + sTestName + "'. No parameters found.");
System.out.println("Running test '" + sTestName + "'. No parameters found.");
}
args = new HashMap();
testArgs = new HashMap();
//args.put("ret", "");
args.put("parameters", mParameters);
args.put("device", sDeviceName);
args.put("status", false);
testArgs.put("parameters", mParameters);
testArgs.put("test", sTestName);
testArgs.put("device", sDeviceName);
testArgs.put("status", false);
args.put(sTestPath,testArgs);
System.out.println("A");
runTest = new RunTest(sTestPath, args);
System.out.println("b");
Thread t = new Thread(runTest);
System.out.println("c");
t.start();
System.out.println("d");
// //Object ret = eval("parallelize((run,(" + sTestPath + "," + ")))");
// Object ret = eval("parallelize((run,('Motor Test 3 100ms',)), (run,('Motor Test 3 200ms',)))");
// Object ret1 = ((ArrayList) ret).get(0);
// //Object ret2 = ((ArrayList) ret).get(1);
// System.out.println(String.valueOf(ret1));
//runAsync(sTestPath, args);
iRet = 0;
} catch (Exception ex) {
@@ -930,15 +958,30 @@ public class TestingList extends Panel {
setButtonToStop();
}
}
try{
runTest = new RunTest(args);
System.out.println("C");
Thread t = new Thread(runTest);
System.out.println("D");
t.start();
System.out.println("E");
} catch (Exception ex) {
SwingUtils.showMessage(this, "executeTest(), run thread", ex.toString());
logger.log(Level.SEVERE, ex.toString());
setButtonToStop();
}
return iRet;
}
//delete this
public class RunTest implements Runnable {
private String sDeviceName, sTestPath;
private HashMap mParameters;
private HashMap<String,Object> args;
private HashMap<String,HashMap> hTests;
public RunTest(String sTestPath, HashMap<String,Object> args) {
System.out.println("A1");
@@ -954,28 +997,63 @@ public class TestingList extends Panel {
this.sTestPath = sTestPath;
this.mParameters.put("a", 1);
}
public RunTest(HashMap<String,HashMap> args) {
System.out.println("A0");
this.hTests = (HashMap) args;
}
public void run() {
// code in the other thread, can reference "var" variable
int iLastExecutedTestIndex = -1;
final String sParallelizeBegin = "(run,(str('";
final String sParallelizeEnd = "'),))";
try {
System.out.println("Parameters passed: " + String.valueOf(args.get("parameters")));
System.out.println("A4");
for (String key:args.keySet()){
System.out.println("key: "+ key + " val: "+ String.valueOf(args.get(key)));
// getController().getScriptManager().setVar(key,args.get(key));
//setGlobalVar(key, (Object) args.get(key));
System.out.println("A0.1");
//runAsync(sTestPath, args);parallelize((run,('Motor Test 3 100ms'), (run,('Motor Test 3 200ms')))
// Object ret = eval("parallelize((run,('Motor Test 3 100ms',)), (run,('Motor Test 3 200ms',)))");
String sParallelizeCommand = "parallelize(";
int i = 0;
int iTotalEntries = hTests.entrySet().size();
for(Map.Entry<String, HashMap> hTest : hTests.entrySet()){
setGlobalsVars(hTest.getValue());
sTestPath = hTest.getKey().toString();
sTestPath = sTestPath.replace("\\","\\\\");
sParallelizeCommand = sParallelizeCommand + "(run,(str('" + sTestPath;
i++;
System.out.println("Parameters passed: " + String.valueOf(hTest.getValue().get("parameters")));
System.out.println("A"+i);
if (i<iTotalEntries){
sParallelizeCommand = sParallelizeCommand + "'),,locals='pio')),"; //between each "run" command
}else{
sParallelizeCommand = sParallelizeCommand + "'),)))"; //very last part of command "parallelize"
}
}
// setGlobalsVars(args);
System.out.println("A5");
//runAsync(sTestPath, args);
Object ret = eval("parallelize((run,('Motor Test 3 100ms'), (run,('Motor Test 3 200ms')))");
System.out.println(sParallelizeCommand);
Object ret = eval(sParallelizeCommand);
// Object ret = eval("parallelize((run,(str('"+sTestPath+"'),)), (run,('Motor Test 3 200ms',)))");
System.out.println("Ret = " + String.valueOf(ret));
Object ret1 = ((ArrayList) ret).get(0);
//Object ret1 = ((ArrayList) ret).get(0);
//Object ret11 = ((List<PyList>) ret1).get(0);
//Object ret2 = ((ArrayList) ret).get(1);
System.out.println(String.valueOf(ret1));
//System.out.println(String.valueOf(ret1));
//System.out.println(String.valueOf(ret11));
String sTestResult, sTestStatus, sTestName;
//read the return mapping and put the result in the right table row
for(Object oTestRet : (ArrayList) ret){
sTestPath = String.valueOf(((List<PyList>) oTestRet).get(0));
sTestStatus = String.valueOf(((List<PyList>) oTestRet).get(1));
sTestResult = String.valueOf(((List<PyList>) oTestRet).get(2));
HashMap<String, HashMap> hTest = hTests.get(sTestPath);
sDeviceName = String.valueOf(hTest.get("device"));
sTestName = String.valueOf(hTest.get("test"));
System.out.println(sDeviceName + "|" + sTestPath + "|" + sTestResult + "|" + sTestStatus);
iLastExecutedTestIndex = showResult(sDeviceName, sTestPath, sTestResult, sTestStatus) ;
}
} catch (Exception ex) {
Logger.getLogger(TestingList.class.getName()).log(Level.SEVERE, null, ex);
SwingUtils.showMessage(TestingList.this.getComponent(), "runTest()", ex.toString());
System.out.println(String.valueOf(ex));
}
}
}