Home » excel » excel – How to use an Anylogic Object (INode) as a Type when using Databases

excel – How to use an Anylogic Object (INode) as a Type when using Databases

Posted by: admin May 14, 2020 Leave a comment

Questions:

The Problem: I have database tables (imported excel) with orders and locations. The orders have a column called “destination” that is cross referenced (using “Foreign Key” to reference locations.location) to the location column locations table which is supposed to represent the actual INode. How do i specify this address say in a moveTo block?

  1. I dont see an option where i can specify INode as the type in the Database table. I get a permission error if i choose Other as type and try to write INode.
  2. How do i effect this cross reference in code or in the Actions line? Its already set up with a foreign key in the agent table/parameters.
How to&Answers:

you cannot represent AnyLogic objects like INode as database entries. Instead, you can store characteristics about the node and create nodes programmatically at the model start.

So store things like X/Y coordinates, etc.

Below is an example for GIS points to show you the approach. You need to do something similar for INodes (check the AnyLogic help for INode API to learn how to create them).

I store the location name as a string in the dbase:
enter image description here

On the model startup, I programmatically create GISPoint elements from the data (assumes I have a GIS map called map):

List<Tuple> rows = selectFrom(routes)
.list();

for (Tuple row : rows) {
        GISPoint locationFrom = map.searchFirst(row.get( routes.location_from ));
        GISPoint locationTo = map.searchFirst(row.get( routes.location_to ));
        map.add(locationFrom);
        map.add(locationTo);
        GISRoute route = map.getRoute(locationFrom.getLatitude(), locationFrom.getLongitude(), 
             locationTo.getLatitude(), locationTo.getLongitude());
        route.setLineColor( spectrumColor(uniform_discr(1,10), 10) );
}

Answer:

The idea: Parsing

I agree with Benjamin: You cannot directly save a reference to the Java object of an INode. If you still want to use predefined INodes, you can save the textual name of your node in the Excel/database and then parse (“convert”) it to the reference of the INode with this name.

How to do it

To prepare for this, it is easiest to manually put all INodes that you want to potentially retrieve into one Collection, for this example I name it allMyINodes:

collection of all nodes

Then one can use a parsing function, here is one proposal:
INode getINodeByName(String nodeName):

INode myNode;

myNode = allMyINodes.stream()
    .filter(c -> c.getName().equals(nodeName))
    .findAny()
    .orElse(null);

return myNode;

parsing function

Here is an example project in the Anylogic Cloud, you can run and download the source files there as well.

Applying it to your scenario

In your application, this means that for example in a moveTo-block, you would write
getINodeByName(....here the code to get one node name from the db....) for your destination of type INode.

As to your second question on how to retrieve the location based on a found order and a link by key, I think this is a separate question and only database/SQL related, maybe you open a separate post on this with an SQL tag.