« Back

Calling Execution Files in ADOxx

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Calling Execution Files in ADOxx
community documentation add-on implementation
Answer
3/27/14 11:04 AM
How to call execution files (.exe, dll, .Jar) in ADOxx?
How to:
1) Select an algorithm in the modeling tool
2) Configure the algorithm in the modeling tool
3) Trigger the algorithm
a. Export current model to the format needed by the algorithm
b. Perform algorithm/call functionality (e.g. optimal path, example given relates to clingo implementation as a solver)
c. Read and parse results of algorithm
4) Represent the result in the model and provide means to store information back to the model for comparision

RE: Calling Execution Files in ADOxx
Answer
1/20/14 12:10 PM as a reply to Fadi.
External functionality can be called using as SYSTEM or DLL calls in AdoScript. For the logic described in the steps of the question, the following implementation is proposed.

a) Add a new menu item to trigger the functionality. This is performed by adding the following statement in the "External coupling" library attribute:

1# add the item as a new menu item under a new top-level menu called "Model algorithms" for each component
2ITEM "Run algorithm"
3     acquisition:"Model algorithms" modeling:"Model algorithms" analysis:"Model algorithms"
4     simulation:"Model algorithms" evaluation:"XML Reader" importexport:"Model algorithms"
5#-----------------------------------------------
6# execute an external ASC file when clicking on the menu, could be in the file space (as below) or also in library using db:\\
7EXECUTE file: ("d:\\runAlgorithm.asc")

b) Implement the logic as described in the steps (stored in the ASC file referenced)
 1# Step 1 [ Optinal ] - open a configuration screen.
 2# a configuration screen can be realized using the CoreUI MessagePort for model select boxes and the like or custom screens using a DLL for the matrix dialog
 3# Step 2 Validate model
 4CC "Modeling" GET_ACT_MODEL
 5SETL nCurrentModelID: (modelid)
 6IF (modelid != -1) {
 7CC "Core" GET_MODEL_INFO modelid: (nCurrentModelID)
 8SETL sCurrentModeltype: (modeltype)
 9# only run script if a model of a specific type is open
10IF (sCurrentModeltype = "Algorithm model") {
11  # Export model as XML
12  CC "Documentation" XML_MODELS modelids: (STR nCurrentModelID) mgroupids: ("") attrprofs: ("") apgroups: ("")
13  SETL sXMLContent: (xml)
14  # Write export to tmp file
15  SETL sXMLDocumentPath: ("d:\\temp\\adoxxmodel.xml")
16  SETL sTranformationResult: ("d:\\temp\\adoxxmodel_transform.xml")
17  SETL sXSLFile: ("d:\\temp\\model_transform.xsl")
18  CC "AdoScript" FWRITE file: (sXMLDocumentPath) text: (sXMLContent) binary:0
19  # perform a transformation using external functionality developed in JAVA
20  SYSTEM ("java -jar \""+sADOxxWorkingDir+"\\tools\\saxonb8j\\saxon8.jar\" -t -o \""+sTranformationResult+"\" \"" +sXMLDocumentPath+ "\" \"" +sXSLFile + "\"") #with-console-window
21  # read the returned file from external operation and parse/set value accordingly
22  CC "AdoScript" FREAD file: (sTranformationResult)
23  SETL transformResult: (text)
24  # set return value back to model
25  # CC "Core" SET_ATTR_VAL ...
26  }
27}
28ELSE {
29  CC "AdoScript" INFOBOX ("Currently no model window exists.")
30}

RE: Calling Execution Files in ADOxx
Answer
2/4/14 10:18 AM as a reply to Fadi.
As an additional case, to show how DLLs and EXE files can be called from within ADOxx, please find below the script part that shows in a simplifed environment how

a) DLL calls can be used to construct a complex UI dialog
b) EXE calls to the R (http://www.r-project.org/) as an example are available to performed based on model information
c) the model is updated with the results of the calculation and also visually highlights elements (based on a comparison)

Please find attached the AdoScript that is triggered from a menu element, the R implementation to calculate the averages and the necessary parts to update the model. Attached to this post you find the generic implementation as ABL for import, an example model as well as the integrated scripts for R and ADOxx.

  1# This script demonstrates how external functionality can be integrated       #
  2# in ADOxx, using DLL on on hand to provide a custom, complex dialog box,     #
  3# in a modal format and in a second step to call functionality in an external #
  4# tool for calculation purposes.                                              #
  5# IMPORTANT: this script does not include any
  6
  7
  8# 0. Initialize variables as default values
  9IF (type (c_sActiveFolder) = "undefined") {
 10  SETG c_sActiveFolder: "C:\\temp\\"
 11}
 12IF (type (c_sExtAppPath) = "undefined") {
 13  SETG c_sExtAppPath: "C:\\Programme\\R\\R-3.0.2\\bin\\rscript.exe"
 14}
 15SETG sDataFile: ("ConsumptionInfo.txt")
 16SETG sAvgFile: ("AverageInfo.txt")
 17
 18# 1. Construct a complex dialoag using external DLL calls
 19# the necessary DLL is part of the ADOxx installation, the path is relative expressed below
 20SET str_DLLPath: ("tools\\misc\\")
 21# init the structutur of the dialog, 1 column, 5 rows
 22CALL dll: (str_DLLPath + "mDlg.dll")
 23    function:"long initMDialog(char* cols, char* rows, char** dummy)"
 24    cols: ("1")
 25    rows: ("5")
 26    dummy:0   
 27# define width of column
 28CALL dll: (str_DLLPath + "mDlg.dll")
 29    function:"long setColWidth(char* col, long width, char** dummy)"
 30    col: ("1")
 31    width:150
 32    dummy:0
 33# add editbox with defaultvalue   
 34CALL dll: (str_DLLPath + "mDlg.dll")
 35    function:"long setEditBox (char* sColRow, char* sValue,  char** dummy)"
 36    sColRow: ("1:1")
 37    sValue: (c_sExtAppPath)
 38    dummy:0
 39# add the name of the box   
 40CALL dll: (str_DLLPath + "mDlg.dll")
 41    function:"long setEditBoxCaption (char* sColRow, char* sValue,  char** dummy)"
 42    sColRow: ("1:1")
 43    sValue:"External application path: "
 44    dummy:0 
 45# add a file name identifier
 46CALL dll: (str_DLLPath + "mDlg.dll")
 47    function:"long setEditBox (char* sColRow, char* sValue,  char** dummy)"
 48    sColRow: ("1:2")
 49    sValue: (c_sActiveFolder)
 50    dummy:0
 51# add the name of the box   
 52CALL dll: (str_DLLPath + "mDlg.dll")
 53    function:"long setEditBoxCaption (char* sColRow, char* sValue,  char** dummy)"
 54    sColRow: ("1:2")
 55    sValue:"Existing folder to save data: "
 56    dummy:0    
 57# show constructed dialog box
 58CALL dll: (str_DLLPath + "mDlg.dll")
 59    function:"long showMDlg(char* caption, long hwnd, char** dummy)"
 60    caption: ("Example Dialog")
 61    hwnd:0
 62    dummy:0
 63
 64# get return values and set accordingly for further processing
 65CALL dll: (str_DLLPath + "mDlg.dll")
 66    function:"long getEditBoxResult (char* sColRow, char** sResult)"
 67    sColRow: ("1:1")
 68SETG c_sExtAppPath: (sResult)
 69
 70CALL dll: (str_DLLPath + "mDlg.dll")
 71    function:"long getEditBoxResult (char* sColRow, char** sResult)"
 72    sColRow: ("1:2")   
 73SETG c_sActiveFolder: (sResult)
 74
 75
 76# 2. Export model information for processing, this could also be through a standard interface in ADOxx,
 77# in this example we create an own stream.
 78
 79# get the current model ID
 80CC "Modeling" GET_ACT_MODEL
 81 # --> RESULT modelid: intValue
 82SETL n_ModelID: (modelid)
 83CC "Core" GET_ALL_OBJS_OF_CLASSNAME modelid: (n_ModelID) classname:"Car"
 84SETL l_CarObjIDs: (objids)
 85# export data for each car
 86SET sExportString: ""
 87FOR s_CarObjID in: ( l_CarObjIDs ) {
 88    SET n_CarObjID: ( VAL s_CarObjID)
 89    # read the consumption value
 90    CC "Core" GET_ATTR_VAL objid: (n_CarObjID) attrname: "Consumption"
 91     # --> RESULT ecode: intValue val: anyValue
 92    SET sExportString: ( tokunion ( sExportString , STR val , " " ) )
 93}
 94# write the information to a text file
 95SET sConsumptionDataFile: (c_sActiveFolder + sDataFile)
 96# add a \n at end for R to understand the input as a table
 97CC "AdoScript" FWRITE file: (sConsumptionDataFile) text: (sExportString + "\n") append: 0 binary: 0 base64: 0
 98
 99
100# 3. Run external functionality to calculate the average of consumption
101# copy R script to the active folder
102CC "AdoScript" COPY_FILES "db:\\calcPowerAvg.r" to: (c_sActiveFolder + "calcPowerAvg.r") overwrite-existing
103
104# call external functionality of R using the script and input from the model
105SET c_sScriptName: (c_sActiveFolder + "calcPowerAvg.r")
106SET c_sAvgDataFile: (c_sActiveFolder + sAvgFile)
107SET sExtCall: (c_sExtAppPath + " " + c_sScriptName + " " + sConsumptionDataFile + " " + c_sAvgDataFile)
108SYSTEM ( sExtCall )  with-console-window
109
110#read return results
111CC "AdoScript" FREAD file: (c_sAvgDataFile)
112 # --> RESULT ecode: intValue text: strValue .
113SET nAvgPwrValue: ( VAL text )
114
115# 4. Read return and update objects, the values are set, and the graphical appearance is changed through a DYE approach
116FOR s_CarObjID in: ( l_CarObjIDs ) {
117    SET n_CarObjID: ( VAL s_CarObjID)
118    # read the consumption value
119    CC "Core" SET_ATTR_VAL objid: (n_CarObjID) attrname: "Average consumption" val: (nAvgPwrValue)
120    CC "Core" GET_ATTR_VAL objid: (n_CarObjID) attrname: "Consumption"
121    # highlight elements in model
122    IF (val > nAvgPwrValue) {
123      CC "Modeling" DYE (n_CarObjID) error-mark
124    }
125}
126CC "AdoScript" INFOBOX "External call to R completed and updated model!"
Attachment

Attachments: Example model.adl (4.4k), External Functionality Call Library.abl (17.4k), Screenshot-Result.jpg (12.4k), calcPowerAvg.r (0.2k), callExternal.asc (4.9k)