Search code examples
javaanylogic

How to represent a string retrieved from the AnyLogic model database as a variable name?


I am building an AnyLogic model of a large storage facility (like a container terminal) that has hundreds of storage locations over a number of areas. The items being stored are stacked on top of each and need to be retrieved in a LIFO fashion. The model loads the current inventory at start-up as an agent population including a parameter that identifies the location of each agent.

The model also has database table that links the unique name of each location (String value) with the name of a Queue object representing the location in the model. At start-up the model needs to create a collection (LinkedHashMap) that will map the location name (Key) (String) with its Queue name (Value). However as the database stores values as strings I need to convert the queue name value retrieved from the database query (String) to an object variable name.

Here's a sample of the offending function. You'll notice the second line of the 'for' loop tries to populate the q variable (Queue) with the value from the "loc_q" column in the database (String). Of course this results in a type mismatch error. How do I convert the String value into a Queue name?

 Queue q = null;
 String str = null;

 List<Tuple> s = selectFrom(storage_locations)
    .orderBy(storage_locations.loc_id.asc())
    .list(storage_locations.loc_id, storage_locations.loc_q);

 for (Tuple sl : s) {
     str = sl.get(storage_locations.loc_id);
     q = sl.get(storage_locations.loc_q);
     colMapQ.put(str, q);
 }

I confess I'm not a Java guru so I'm likely missing a basic step here. I've spent 3 days searching stack overflow and gitHub without much success. Has anyone come across this problem before? I'd appreciate your help.


Solution

  • You can't. You need to do this yourself by creating a mapping function that returns a Queue object matching a given String name.

    Select all queue objects in your model, right-click on one and create a collection from them all.

    Now, write a function that loops through all entries and returns the Queue element that has the same name as your input, something like

    for (Queue currentQueue : collectionOfAllQueues) {
        if (currentQueue.getName().equals(requiredDbaseName){
            return currentQueue;
        }
    }