Java application calling Anylogic (without export) - anylogic

I am using AnyLogic University edition (cannot export AnyLogic model). I need my Java application to call an AnyLogic model (passing a few parameters), execute the model and receive the experiment results (from AnyLogic to my application). How can this be achieved?
Another question is, can the same be done from other programming languages (say, VB or Python)? What I mean is, calling an AnyLogic model from Python.

One approach would be to use a ProcessBuilder in Java to execute a shell command, that runs your anylogic model like so (assuming the model is in /c directory on windows):
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("cmd.exe", "/c","anylogic 'absolute path to the model .alp file'");
There are several ways to then pass back results. The easiest may be to import an Excel file into Anylogic which stores your results, and then read it with a CSVReadWrite object in your Java application.

Not sure what is your objective behind calling the simulation dynamically using a java application. But one way to do it might be inside Anylogic itself. You can use the 'Custom Experiment' to write any reasonably small java source code and run the model (in iteration if needed) to collect results and possibly even saving the results to a csv file after each run. See the link below for examples and details
https://help.anylogic.com/nav/0_11

Related

Modelica Variable size connectors

I want to do Monte Carlo simulation with a model. This model has some external functions linked to a library that reads its inputs from a file. I have been recently experimenting with ".mos" files to run the same model over and over. With external files it is easy to change them and re-simulate the model using the mos file (even in parallel). Yet I was unable to change the internal parameters of the model in the mos script. I have done many experiments and been looking for the answer for some time now. I wonder what is the correct syntax for changing the parameters of the model inside a simple .mos file like the one below.
cd(<working directory>);
loadFile(<address of the package that also loads the OpenModelica>);
simulate(<model>, stopTime=<...>, numberOfIntervals=<...>);
quit();
For instance, lets say the model has a parameter param and a submodel sub with the parameter sub_param. How can I change param and sub_param inside the mos file.

Link AnyLogic and Matlab

I was wondering if it would be possible that link AnyLogic to Matlab?
I need a way that call a function from Matlab to Anylogic.
At this level AnyLogic should be considered as Java application, and the question should be formulated as Link Java and Matlab. You may search for different Java libraries that establish connection with MatLab and Java app, allowing to pass commands from app to Matlab, and get the result. Example of such library — matlabcontrol. There is also example model.
UPD: Matlab provides Java API for AnyLogic, so you may directly use it within AnyLogic. In this case third-party libraries are not required. For this purpose you need to add engine.jar to the model dependencies, the .jar is provided with Matlab, it is located in:
matlab\extern\engines\java\jar
Here is the model that invokes Matlab using the respective examples from:
matlab\extern\examples\engines\java
I'm not sure which direction you are talking about.
calling a matlab function in anylogic:
Since Anylogic is based on Java, you could use the Java Runtime class, and do something like:
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("C:\<a long path here>\matlab.exe" -nodisplay -nosplash -nodesktop -r "run('C:\<a long path here>\mfile.m');exit;"
after that you need to get the output from the process object pr and do your magic.
calling anylogic simulation in matlab:
have a look at : http://help.anylogic.com/topic/com.xj.anylogic.help/html/integration/Integration.html

Is there something like a "pre-build" callback function?

I have a Simulink model, the purpose of which is automated code generation.
My model uses S-functions (developed by another party), which has hard-coded assumptions about the path. For instance, several external data files are needed, which are referenced in the S-function via a relative path like ..\Bin\data\datafile.bin. This makes it necessary to set MATLAB's current working directory to a specific path before the model can be run.
I can automatically check and set the correct path via model callback functions. However, all model callback functions only seem to be related to the simulation process, not the build process. That means that I can run the model irrespective of what directory I'm in, but when I try to build the model, it always fails unless I manually navigate MATLAB back to the correct directory.
Needless to say, that's quite annoying. So I was wondering if there is something like a "preBuildFcn" callback fnuction, a function that is run before starting the build process? Any other solution (that does not involve modifying the S-function) is also very welcome.
There are plenty of hooks into the build process of Simulink / Embedded Coder ('entry', 'before_tlc', 'after_tlc', 'before_make', 'after_make', 'exit', and 'error'). I assume you want an 'entry' hook.
All you need to do is write an M-function with the name your_system_target_file name_make_rtw_hook, as explained in the documentation Customize Build Process with STF_make_rtw_hook File.
In case you can't open the online documentation (login required), here is the path to the HTML in your MATLAB installation: MATLAB root\help\rtw\ug\customizing-the-target-build-process-with-the-stf-make-rtw-hook-file.html
I am not sure whether building simulink models is sufficiently similar to building regular MATLAB programs, but here is what I used in the past:
Set up the project manually
Build the project programmatically
The program that is used to build the project should be able to set the path or do other custom things.

how to run the example of uima-text-segmenter?

I want to call the API of uima-text-segmenter https://code.google.com/p/uima-text-segmenter/source/browse/trunk/INSTALL?r=22 to run an example.
But I don`t know how to call the API...
the readme said,
With the DocumentAnalyzer, run the following descriptor
`desc/textSegmenter/wst-snowball-C99-JTextTilingAAE.xml` by taking the
uima-examples data as input.
Could anyone give me some code which could be run directly in main func for example?
Thanks a lot!
Long answer:
The link describes how you would set up the application from within the Eclipse UIMA environment. This sort of set-up is typically targeted at subject matter specialists with little or no coding experience. It allows them to work (relatively fast) with UIMA in a declarative way: all data structures and analysis engines (computing blocks within UIMA) are declared in xml (with a GUI on top of it), after which the framework takes care of the rest. In this scenario, you would typically run a UIMA pipeline using a run configuration from within Eclipse (or the included UIMA pipeline runner application). Luckily, UIMA allows you to do exactly the same from code, but I would recommend using UIMAFit (http://uima.apache.org/d/uimafit-current/tools.uimafit.book.html#d5e137) for this purpose instead of of UIMA, as it bundles lots of useful things and coding shortcuts.
Short answer:
Using UIMAFit, you can call Factory methods that create CollectionReader (read input), AnalysisEngine (process input) and Consumer objects (write/do other stuff) from (third-party provided) XML files. Use these methods to construct your pipeline and the SimplePipeline class to run it. To extract the data you need, you would manipulate the CAS object (containing your data) in a Consumer object, possibly with a callback. You could also do this in a Analysis Engine object. I recommend using DKPro's FeaturePathFactory (https://code.google.com/p/dkpro-core-asl/source/browse/de.tudarmstadt.ukp.dkpro.core-asl/trunk/de.tudarmstadt.ukp.dkpro.core.api.featurepath-asl/src/main/java/de/tudarmstadt/ukp/dkpro/core/api/featurepath/FeaturePathFactory.java?spec=svn1811&r=1811) to quickly access the feature you are after.
Code examples:
http://uima.apache.org/d/uimafit-current/tools.uimafit.book.html#d5e137 contains examples, but they all go in the opposite direction (class objects are used in the factory methods, instead of XML files - XML is generated from these classes). Take a look at the UIMAFit API to find the method you need, AnalysisEngineDescription from XML for example: http://uima.apache.org/d/uimafit-current/api/org/apache/uima/fit/factory/AnalysisEngineFactory.html#createEngineDescriptionFromPath-java.lang.String-java.lang.Object...-

Deployment of Simulink Models

I have been trying to find out how to deploy a Simulink model. There are possibilities and problems as well.
If I use Simulink Coder how can I find the generated code on my computer? Where is it saved as a file or package of files?
Can we deploy Simulink as .NET Assembly? If we can, where can I find a detailed documentation about it.
Is there any other way to use my Simulink model standalone?
Thank you for any effort.
By default all the code gets placed into a folder, in the current directory, called ModelName_CodeTarget, where ModelName is the name of your model and CodeTarget is the particular type of code you are generating. (This folder is created during the build process if it doesn't already exist.)
There is no mechanism for automatically generating a .Net assembly. Simulink Coder generates C code; what you do with it is then up to you (just as it would be with hand written code). Note that the process is fully customizable, so you could create your own build process to autmatically wrap the C code into an assembly (but it would be a very advanced maneuvre to do this).
There are a couple of Simulink Coder "targets" that generate standalone executable files. For instance, the GRT (Generic Real-Time) target generates a simple .exe file; the RSIM target (the Rapid Simulation Target) generates an exe where you can specify different .mat files that can be used to specify different model parameters for different simulation runs, and have the results of the different runs stored to different output .mat files.
Most of these questions are answered by looking at the early chapters of the Simulink Coder documentation.
It would be worth you taking some time to look at it to get a feel for how the code generation process works.