Last updated on 16th July 2024
This part will get you started with populating models using data.
Let's take a simplified version of the agent class City
and let's say you have the following kind of data where each City
has:
name
, that is a String
population
, that is an int
surface
, that is a float
More precisely the data is organised like so:
id | name | population | surface |
---|---|---|---|
23323443 | Kodiak | 783 904 | 20 229 |
48217039 | Firebrick | 13 003 | 4320 |
37410058 | Onalaska | 2 377 843 | 61 839 |
93749281 | Ak-Chin Village | 453 150 | 17 323 |
55283047 | Blacktail | 1 921 613 | 49 315 |
... | ... | ... | ... |
Here, there is an additional field called id
(which is a unique number) that identifies each record.
We can come up with a simplified version of the Agent class City
described above to match this data:
To import data, you have to annotate your fields like so so that records in your data can be mapped to Cities.
Class definition (Java)
class City extends Agent {
@Constant
String name;
@Constant
int population;
@Constant
float surface;
//...
}
Finally, to import your agents in your model, you have to use the system loadGroup() method.
You have to specify a Source to use ; this object is dedicated to import data from your file or data base. Let's use a CSVSource to import the data from a CSV file !
Creating data group (Java)
// Creating a Source to import data -- you can use another type of source too
CSVSource citySource = new CSVSource("/path/to/myCities.csv");
Group<City> cities = loadGroup(City.class, citySource);
And now, all the Cities contains as records in your data are imported into your model !
Let's now imagine that you want to introduce Roads between your Cities.
And let's say your have the same dataset of Cities ...
id | name | population | surface |
---|---|---|---|
23323443 | Kodiak | 783904 | 20229 |
48217039 | Firebrick | 13003 | 4320 |
37410058 | Onalaska | 2377843 | 61839 |
93749281 | Ak-Chin Village | 453150 | 17323 |
55283047 | Blacktail | 1921613 | 49315 |
... | ... | ... | ... |
... and that you have another a data sets of Roads linking Cities together:
from | to | length |
---|---|---|
23323443 | 48217039 | 23.32 |
37410058 | 23323443 | 343.3 |
23323443 | 55283047 | 43.13 |
55283047 | 93749281 | 33.38 |
... | ... | ... |
Here, from
and to
identify respectively the source agents and the target agents.
The remaining fields will be injected in the link at their creation. In our context, there is just one remaining field, length
, that represent the length of a Road
from the City
identified by the ID from to the City
identified by the ID to
.
You can import those links in your model and between your two groups using the connect()
with a 'Source'.
Connecting a Group Using Data (Java)
CSVSource citySource = new CSVSource("/path/to/myCities.csv");
CSVSource roadSource = new CSVSource("/path/to/myRoads.csv");
Group<City> cities = loadGroup(City.class, citySource);
cities.loadConnections(cities, Road.class, roadSource);
You can also connect two different Groups
as well:
Connect two Groups using Data
CSVSource citySource = new CSVSource("/path/to/myCities.csv");
CSVSource countrySource = new CSVSource("/path/to/myCountry.csv");
CSVSource linkSource = new CSVSource("/path/to/myLinks.csv");
Group<City> cities = loadGroup(City.class, citySource);
Group<Country> countries = loadGroup(Country.class, countrySource);
cities.loadConnections(countries, MyLink.class, linkSource);
If you have data you want to import from a database, you can use a JDBCSource
that supports MySQL, SQLite as well as PostGreSQL. Here is an example for SQLite:
Using JDBC Sources (java)
String connectionString = s"jdbc:sqlite:path/to/myDataBase.db";
JDBCSource citySource = new JDBCSource(connectionString, "Country", "username", "password");