Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
example_remoteagent [2020/05/10 16:22] admin removed |
— (current) | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | 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. | ||
- | * 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/ | ||
- | * | ||
- | */ | ||
- | static { | ||
- | try{ | ||
- | // replace demo_64.dll with demo_32.dll if you are running x86 (32 bit) Windows OS | ||
- | String dllPath = System.getProperty(" | ||
- | 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[] {" | ||
- | TestOptimalAgent toagent = new DemoEmbeddedSysAgent(null, | ||
- | toagent.execModel(" | ||
- | Thread.sleep(500); | ||
- | toagent.run(); | ||
- | toagent.saveStat(" | ||
- | 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, | ||
- | throws Exception { | ||
- | super(logger_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. | ||
- | * to your model to be checked/ | ||
- | * | ||
- | * Throw Exception if error/ | ||
- | */ | ||
- | @Override | ||
- | public String execute(RemoteCmd remoteCmd_p) throws Exception { | ||
- | String cmdSyntax = remoteCmd_p.getCmdActionSyntax(); | ||
- | this.info(" | ||
- | JSONObject cmdJSON = new JSONObject(cmdSyntax); | ||
- | String cmdAction = cmdJSON.getString(" | ||
- | this.info(" | ||
- | |||
- | if (cmdAction.equalsIgnoreCase(" | ||
- | this.info(" | ||
- | return " | ||
- | } | ||
- | else if (cmdAction.equalsIgnoreCase(" | ||
- | this.info(" | ||
- | return " | ||
- | } | ||
- | else if (cmdAction.equalsIgnoreCase(" | ||
- | this.info(" | ||
- | return " | ||
- | } | ||
- | else if (cmdAction.equalsIgnoreCase(" | ||
- | String p1 = cmdJSON.getString(" | ||
- | String p2 = cmdJSON.getString(" | ||
- | this.info(" | ||
- | try { | ||
- | double p1Value = Double.parseDouble(p1); | ||
- | double p2Value = Double.parseDouble(p2); | ||
- | double d = add(p1Value, | ||
- | return String.valueOf(d); | ||
- | } | ||
- | catch (Throwable e) { | ||
- | this.error(" | ||
- | throw new Exception (e.toString()); | ||
- | } | ||
- | } | ||
- | |||
- | else if (cmdAction.equalsIgnoreCase(" | ||
- | String p1 = cmdJSON.getString(" | ||
- | String p2 = cmdJSON.getString(" | ||
- | this.info(" | ||
- | try { | ||
- | double p1Value = Double.parseDouble(p1); | ||
- | double p2Value = Double.parseDouble(p2); | ||
- | double d = substract(p1Value, | ||
- | return String.valueOf(d); | ||
- | } | ||
- | catch (Throwable e) { | ||
- | this.error(" | ||
- | throw new Exception (e.toString()); | ||
- | } | ||
- | } | ||
- | else if (cmdAction.equalsIgnoreCase(" | ||
- | String p1 = cmdJSON.getString(" | ||
- | String p2 = cmdJSON.getString(" | ||
- | this.info(" | ||
- | try { | ||
- | double p1Value = Double.parseDouble(p1); | ||
- | double p2Value = Double.parseDouble(p2); | ||
- | double d = multiply(p1Value, | ||
- | return String.valueOf(d); | ||
- | } | ||
- | catch (Throwable e) { | ||
- | this.error(" | ||
- | throw new Exception (e.toString()); | ||
- | } | ||
- | } | ||
- | else if (cmdAction.equalsIgnoreCase(" | ||
- | String p1 = cmdJSON.getString(" | ||
- | String p2 = cmdJSON.getString(" | ||
- | this.info(" | ||
- | try { | ||
- | double p1Value = Double.parseDouble(p1); | ||
- | double p2Value = Double.parseDouble(p2); | ||
- | double d = divide(p1Value, | ||
- | return String.valueOf(d); | ||
- | } | ||
- | catch (Throwable e) { | ||
- | this.error(" | ||
- | throw new Exception (e.toString()); | ||
- | } | ||
- | } | ||
- | else { | ||
- | this.error(" | ||
- | throw new Exception (" | ||
- | } | ||
- | } | ||
- | |||
- | } | ||