This is an old revision of the document!
package demo; import java.util.Date; import java.util.logging.Logger; import org.json.JSONObject; import com.webmbt.agent.RemoteCmd; import com.webmbt.agent.TestOptimalAgent; /** * This is a demo class for a remote TestOptimal agent which fetches the model sequence commands from * TestOptimal server running on a remote host and executes them locally. * */ public class DemoEmbeddedSysAgent extends TestOptimalAgent { public static native double add(double x, double y); public static native double substract(double x, double y); public static native double multiply(double x, double y); public static native double divide(double x, double y); /** * Loads the embedded c/c++ library that you have implemented to interact with your embedded system. The functions in the DLL * are exposed to JNI interface to be called from this java code. * * The dll file must be placed in /lib folder within TO_Agent project/folder. * */ static { try{ // replace demo_64.dll with demo_32.dll if you are running x86 (32 bit) Windows OS String dllPath = System.getProperty("user.dir") + "/lib/demo_64.dll"; System.load(dllPath); System.out.println(dllPath); } catch(Exception e) { e.printStackTrace(); } } /** * start up TestOptimal java agent as a normal java process, remotely execcute Demo_EmbeddedSys_RemoteAgent model on TestOptimal server. * * @throws Exception */ public static void main (String[] args) throws Exception { args = new String[] {"localhost", "8888", "Demo_EmbeddedSys"}; TestOptimalAgent toagent = new DemoEmbeddedSysAgent(null, "http://" + args[0] + ":" + args[1], args[2], 5, 200); toagent.execModel("myUserID", "myPwd", "autoClose=false", "plugin=RemoteCommand"); Thread.sleep(500); toagent.run(); toagent.saveStat("EmbeddedSys_RemoteAgent at " + (new Date()).toString()); System.out.println(toagent.getExecSummary()); /* leave model open so that you may use TO IDE browser to view the execution results. Be sure to close the model when you are done. * In real testing, you may uncomment out following like to close the model. toagent.closeModel(); */ } /** * Constructor. * * @param logger_p * @param TestOptimalSvrAddr_p * @param modelName_p * @param maxRetry_p * @param retryMillis_p * @throws Exception */ public DemoEmbeddedSysAgent (Logger logger_p, String TestOptimalSvrAddr_p, String modelName_p, int maxRetry_p, int retryMillis_p) throws Exception { super(logger_p, TestOptimalSvrAddr_p, modelName_p, maxRetry_p, retryMillis_p); } /** * This is the example of how to parse the remote commands received from your model MScript and call * to your embedded system. * * This function is called for each remote command received from the model. The function is expected * to parse the remote command to figure out what it is expected to do to invoke embedded system * and execute that action. The results from the action is then returned as a String to be sent back * to your model to be checked/validated against the expected results by your model. * * Throw Exception if error/failure is encountered in performing action on the embedded system. */ @Override public String execute(RemoteCmd remoteCmd_p) throws Exception { String cmdSyntax = remoteCmd_p.getCmdActionSyntax(); this.info("Remote cmd: " + cmdSyntax); JSONObject cmdJSON = new JSONObject(cmdSyntax); String cmdAction = cmdJSON.getString("ACTION"); this.info("Action: " + cmdAction); if (cmdAction.equalsIgnoreCase("MBT_start")) { this.info("Starting VendingMachine"); return "OK"; } else if (cmdAction.equalsIgnoreCase("launchAUT")) { this.info("Running the test"); return "OK"; } else if (cmdAction.equalsIgnoreCase("resetAUT")) { this.info("Resetting the test"); return "OK"; } else if (cmdAction.equalsIgnoreCase("add")) { String p1 = cmdJSON.getString("P1"); String p2 = cmdJSON.getString("P2"); this.info("add " + p1 + ", " + p2); try { double p1Value = Double.parseDouble(p1); double p2Value = Double.parseDouble(p2); double d = add(p1Value, p2Value); return String.valueOf(d); } catch (Throwable e) { this.error("Failed to call add function: " + e.toString()); throw new Exception (e.toString()); } } else if (cmdAction.equalsIgnoreCase("substract")) { String p1 = cmdJSON.getString("P1"); String p2 = cmdJSON.getString("P2"); this.info("substract " + p1 + ", " + p2); try { double p1Value = Double.parseDouble(p1); double p2Value = Double.parseDouble(p2); double d = substract(p1Value, p2Value); return String.valueOf(d); } catch (Throwable e) { this.error("Failed to call substract function: " + e.toString()); throw new Exception (e.toString()); } } else if (cmdAction.equalsIgnoreCase("multiply")) { String p1 = cmdJSON.getString("P1"); String p2 = cmdJSON.getString("P2"); this.info("multiply " + p1 + ", " + p2); try { double p1Value = Double.parseDouble(p1); double p2Value = Double.parseDouble(p2); double d = multiply(p1Value, p2Value); return String.valueOf(d); } catch (Throwable e) { this.error("Failed to call multiply function: " + e.toString()); throw new Exception (e.toString()); } } else if (cmdAction.equalsIgnoreCase("divide")) { String p1 = cmdJSON.getString("P1"); String p2 = cmdJSON.getString("P2"); this.info("divide " + p1 + ", " + p2); try { double p1Value = Double.parseDouble(p1); double p2Value = Double.parseDouble(p2); double d = divide(p1Value, p2Value); return String.valueOf(d); } catch (Throwable e) { this.error("Failed to call divide function: " + e.toString()); throw new Exception (e.toString()); } } else { this.error("Invalid command: " + cmdAction); throw new Exception ("Error: invalid command " + cmdAction); } } }