tutorials:interfaces:tour
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorials:interfaces:tour [2025/06/04 09:28] – [Headless] tim2 | tutorials:interfaces:tour [2025/06/04 15:03] (current) – [Minimal required preparations] tim2 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== A tour through GroIMP interfaces ====== | ====== A tour through GroIMP interfaces ====== | ||
+ | In the following, we will use the same model in different GroIMP user interfaces. | ||
+ | | {{page> | ||
===== The model ===== | ===== The model ===== | ||
- | To highlight the flow of simulations | + | To highlight the flow of simulations |
- | The idea is to see how different indicators change over 10 growth | + | The idea is to observe |
{{ : | {{ : | ||
Line 97: | Line 99: | ||
</ | </ | ||
+ | |||
+ | {{ : | ||
===== Headless ===== | ===== Headless ===== | ||
- | Now that we have a function that only requires us to press one button to get all steps, we are already closer to run it completely automatically. | + | Now that we have a function that only requires us to press one button to get all steps, we are already closer to run it completely automatically. |
+ | |||
+ | |||
+ | |||
+ | | {{page> | ||
+ | |||
- | The simplistic way to do so is using the headless mode. In this mode GroIMP starts | + | In this mode, GroIMP starts |
- | For that to work we have to take care of three things, first that GroIMP knows what to do with the model once its opened, then that the model closes | + | For that to work, we have to take care of three things, first GroIMP knows what to do with the model once it' |
=== startup === | === startup === | ||
Line 124: | Line 134: | ||
=== run(Object info) === | === run(Object info) === | ||
- | In this run function we must describe everything that is suppose | + | In this run function, we must describe everything that is supposed |
<code java> | <code java> | ||
Line 137: | Line 147: | ||
Even though GroIMP ran the simulation to completion we did not define any output for our data. | Even though GroIMP ran the simulation to completion we did not define any output for our data. | ||
- | There are several ways to do this using java, but for this tutorial we are going with one of the simplest. | + | There are several ways to do this using Java, but for this tutorial, we are going with one of the simplest. |
- | We are exporting the dataset we anyway | + | We are exporting the dataset we already created to a CSV file and saving it in the same directory as the project. |
<code java> | <code java> | ||
Line 154: | Line 164: | ||
=== Parameters === | === Parameters === | ||
- | GroIMP gives us the ability to forward custom | + | GroIMP gives us the ability to forward custom |
The usage of this is quite simple, every value we add with -X< | The usage of this is quite simple, every value we add with -X< | ||
Line 168: | Line 178: | ||
</ | </ | ||
+ | And to make the csv a bit clearer: | ||
+ | |||
+ | <code java> | ||
+ | protected void init ()[ | ||
+ | Axiom ==> A(1); | ||
+ | | ||
+ | | ||
+ | | ||
+ | .setText(0," | ||
+ | .setText(2," | ||
+ | .setText(3," | ||
+ | .setText(4," | ||
+ | .setText(5," | ||
+ | .setText(6," | ||
+ | } | ||
+ | ] | ||
+ | </ | ||
+ | |||
+ | {{ : | ||
This setup would now allow us to integrate the model into pipelines, e.g. an R script: | This setup would now allow us to integrate the model into pipelines, e.g. an R script: | ||
Line 176: | Line 205: | ||
data <- read.csv(" | data <- read.csv(" | ||
</ | </ | ||
+ | |||
===== HTTP ===== | ===== HTTP ===== | ||
+ | With a bit of scripting, we could use the headless mode also for orchestration or automation, yet we would start and stop GroIMP every time which of course takes a bit of time. To avoid this, and to also allow us to execute on a remote server or in parallel on a GroIMP instance we can use the HTTP-Server | ||
- | < | + | | {{page> |
+ | |||
+ | |||
+ | ==== Minimal required preparations ==== | ||
+ | |||
+ | |||
+ | If we want to run our model through the HTTP-server we have to make three small changes first: | ||
+ | |||
+ | __1. Change the condition in the startup function, to make sure it runs if the model was opened through the http server.__ | ||
+ | |||
+ | To do so we test if we have an HTTPResponse object, this object is created when the model is opened through the server and holds the request from the client and the ability to respond. | ||
+ | |||
+ | < | ||
+ | protected void startup(){ | ||
+ | | ||
+ | // | ||
+ | if( de.grogra.imp.net.HttpResponse.get(workbench())!=null){ | ||
+ | runLater(null); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | __2. Make sure that we are not looking for the headless parameter anymore (its not there).__ | ||
+ | |||
+ | we have to make sure we removed | ||
+ | |||
+ | __3. We have to stop stopping GroIMP __ | ||
+ | |||
+ | In the headless mode, we finished the run(Object info) function with ' | ||
+ | |||
+ | |||
+ | ==== First execution ==== | ||
+ | |||
+ | To open a model through the HTTPServer we have to provide the path to the gsz file, this path is relative to a given "root directory" | ||
+ | We can set this base directory in the GroIMP preferences under "HTTP > open Project", | ||
+ | |||
+ | After we define our root directory we can start the HTTP server through the main menu through "Net > Open HTTP Server" | ||
+ | |||
+ | If the server is running we can visit [[http:// | ||
+ | |||
+ | The command we are interested in is open (and yes is the only one ;-) ). To open we have to provide the path to the model in the given style: | ||
+ | |||
+ | <code url> | ||
+ | http:// | ||
+ | </ | ||
+ | So if you use the project directory as a root directory of the http server, the call would be: | ||
+ | |||
+ | [[http:// | ||
+ | |||
+ | |||
+ | If we run this, its open and runs the model and then stops. The model stays open and the request in the browser keeps loading because the browser never gets any feedback. | ||
+ | |||
+ | |||
+ | To give the browser feedback we need to use the send function from the HttpResponse object at the end of our run function: | ||
+ | |||
+ | <code java> | ||
+ | de.grogra.imp.net.HttpResponse.get(workbench()).send(false); | ||
+ | </ | ||
+ | |||
+ | The false value in the function defines that we do not intend to send information back. Therefore this will only give us "The content has not been set." as a message in the browser. | ||
+ | |||
+ | ==== LazyHTTP ==== | ||
+ | |||
+ | |||
+ | To send content back and to also receive parameters we could now work with the HttpResponse object, but to make this a bit easier we will use the [[https:// | ||
+ | |||
+ | <code java> | ||
+ | import de.grogra.imp.net.lazyhttp.*; | ||
+ | |||
+ | ... | ||
+ | |||
+ | protected void startup(){ | ||
+ | | ||
+ | | ||
+ | | ||
+ | runLater(resp); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public void run(Object info){ | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | ... | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | Additionally, | ||
+ | |||
+ | |||
+ | === Parameters === | ||
+ | |||
+ | Using the LazyHttp library we can also parse given values from the HTTP request: | ||
+ | |||
+ | <code java> | ||
+ | public void run(Object info){ | ||
+ | | ||
+ | | ||
+ | ... | ||
+ | </ | ||
+ | |||
+ | To give a parameter like this to the HTTP server we add '& | ||
+ | |||
+ | [[http:// | ||
+ | |||
+ | |||
+ | === Integration in R === | ||
+ | |||
+ | |||
+ | |||
+ | <code r> | ||
groIMP.HTTP <- function(path, | groIMP.HTTP <- function(path, | ||
parameters="" | parameters="" | ||
Line 197: | Line 344: | ||
} | } | ||
- | df <- groIMP.CSV(" | + | df <- groIMP.CSV(" |
</ | </ | ||
- | {{ : | + | |
+ | {{ : | ||
===== API ===== | ===== API ===== | ||
+ | If you require more interaction with the model during the simulation or want to have direct access to what happens we can use the GroIMP API. | ||
+ | |||
+ | | {{page> | ||
+ | |||
+ | |||
+ | To start the API is a bit more complicated than the HTTP-server, | ||
+ | [[tutorials: | ||
+ | |||
+ | |||
+ | The API works without any changes in the GroIMP model, by using similar functionalities than the GUI, we can run RGG functions, execute XL queries, trigger import or export or view datasets. | ||
+ | |||
+ | The full list of commands can be found here: | ||
+ | https:// | ||
+ | |||
+ | And a browser based tutorial [[tutorials: | ||
+ | |||
+ | Additionally GroIMP provides two libraries to connect to the API: | ||
+ | |||
+ | - [[https:// | ||
+ | |||
+ | - [[https:// | ||
+ | |||
+ | |||
+ | ===== Application on our model ===== | ||
+ | |||
+ | < | ||
+ | library(dplyr) | ||
+ | library(httr2) | ||
+ | library(GroR) | ||
+ | |||
+ | |||
+ | wb< | ||
+ | |||
+ | WBRef.runRGGFunction(wb," | ||
+ | data< | ||
+ | df <- read.table(text = data, sep =",", | ||
+ | |||
+ | WBRef.runXLQuery(wb," | ||
+ | WBRef.runRGGFunction(wb," | ||
+ | data< | ||
+ | df2 <- read.table(text = data, sep =",", | ||
+ | |||
+ | plot(df$sumLeafgetArea) | ||
+ | plot(df2$sumLeafgetArea) | ||
+ | </ |
tutorials/interfaces/tour.1749022110.txt.gz · Last modified: 2025/06/04 09:28 by tim2