I would very much appreciate any help I can get for the following. I have an excel spreadsheet that contains projections of precipitation. The sheet has two columns (day and precipitation) and there are over 20,000 rows, i.e. over 20,000 days of precipitation. I loaded this sheet onto AnyLogic. I am using a system dynamics model and have a dynamic variable called Rainfall. For each day of the simulation I want Rainfall to pull the value from the loaded database, but don't know how to link the dynamic variable to the database. I have already tried the following: inserting a tableFunction named ProjectedRainfall and its values are "loaded from database." For "argument column" I chose "day" and for "value column" I chose precipitation. I then inserted a function named getrainfall and under function body I wrote "return ProjectedRainfall(time()). I then added a dynamic variable that is set to equal "getrainfall(time())". This generates an error that states "The method getrainfall() in the Type Main is not applicable for the arguments (double)." Any ideas on what I may be doing wrong or alternatives to how I could go about linking the dynamic variable to the database? Thank you for the help!
2 Answers
No need to go via a table function. you can pull data directly from your table using SQL.
Learn to use the Database query wizard. Put your cursor into a code field and open it as below. It allows you to select any data and even writes the correct code for you:
There are lots of example models and help articles on this, hope this helps to get you going

- 10,603
- 3
- 16
- 28
-
This might take a little more learning, but thank you! Very helpful to learn more about this tool! – Vianey Rueda Jul 19 '21 at 13:31
-
it is actually the easiest way forward :) PS: also study how to ask better questions and exploit StackOverflow fully: https://www.benjamin-schumann.com/blog/2021/4/1/how-to-win-at-anylogic-on-stackoverflow . welcome to the community :) – Benjamin Jul 19 '21 at 15:07
-
I tried this method but get errors. I chose the correct table, for value column I chose precip, I left choice actions blank, for choose action I chose "list of values," and for syntax I chose SQL. The error I get is this: Cannot cast from List
to double. I tried switching the choose action to "select table records" but the error is "Type mismatch: cannot convert from ResultSet to double" – Vianey Rueda Jul 19 '21 at 15:44 -
Please always start a new question if you have a new problem (obviously search for it first, others had similar issues before you :) ). See https://stackoverflow.com/help/how-to-ask – Benjamin Jul 19 '21 at 18:12
Although it is possible to directly lookup your data from the Database when you need it, this is not advised if this will be happening multiple times during your model execution, which appears to be the case in your model.
Your best option when working with the database is to get the data from the database only once and then put the data in either the agents you created or in new Java classes.
See the SO answer here for more details What is the fastest way to look up continuous data on Anylogic (Java, SQL)
Thus putting al the data in a tableFunction is a good way to prevent the costly exercise of accessing the database.
I replicated your situation and it appears to be working
Here is the table with some dummy data
Here is the setup of the table function
Here is the setup of the dynamic variable - See that I have added an argument in the dynamic variable so that it can be used in the default value
Here is the table function when running the model
And here is some text that uses the dynamic variable to display the precipitation for the current day. You could also simply directly access the table function here, no need to go via the dynamic variable. Although I assume you need the dynamic variable for your system dynamics model

- 3,770
- 2
- 16
- 33
-
If you have changed rainfall to a dynamic parameter you need to add `()` at the end. You will see this if you use auto code complete. Remember dynamic parameters are like functions, they have arguments and they return a value, hence the use of the parenthesis. I you don't use the `()` then AnyLogic is looking for a variable with that name, not a function, or dynamic parameter. – Jaco-Ben Vosloo Jul 19 '21 at 14:22
-
I used auto code complete and it filled in rainfall(day), but the new error says that day cannot be resolved to a variable. – Vianey Rueda Jul 19 '21 at 14:32
-
When I type rainfall() I get an error that says "the method rainfall(double) in the type Main is not applicable for the arguments () – Vianey Rueda Jul 19 '21 at 15:00
-
Look at my example. I replaced day with time(), since my model units is days. My dynamic variable is expecting an argument of type double as an input. – Jaco-Ben Vosloo Jul 19 '21 at 17:24
-
I changed the dynamic parameter to read ProjectedRainfall(time()) and now when I type rainfall(time()) it works! Thank you so much for your help! – Vianey Rueda Jul 19 '21 at 17:39