1
votes

In my anylogic-project i query datasets from the internal database. The table i want to query has following structure:

Name: product_innovation_level
--------------------------------
ID|level_customer|level_employee|
--|--------------|--------------|
 1|            10|            14|

The only example i could find uses the Tuple-class as shown below. It works, but it feels quite clunky that i have to extract the needed values manually from the item.

Tuple innovationLevel = selectFrom(product_innovation_level).
                        where(product_innovation_level.id.eq(1)).
                        firstResult(product_innovation_level.level_customer, product_innovation_level.level_employee);

double ngEmployee = innovationLevel.get(product_innovation_level.level_employee);
double ngCustomer = innovationLevel.get(product_innovation_level.level_customer);

Is there a better way to select the values directly into an model class? I'm looking for something like this:

xxx innovationLevel = selectFrom(product_innovation_level).
                                  where(product_innovation_level.id.eq(1)).
                                  firstResult(xxx);

What class i schould use for xxx? I found an automatically created class Qproduct_innovation_level, but i didn't found a way to access the values of this dataset.

Qproduct_innovation_level innovationLevel = selectFrom(product_innovation_level).
                                  where(product_innovation_level.id.eq(1)).
                                  firstResult(product_innovation_level);
int i  = innovationLevel.level_customer.intValue();
Error: Type mismatch: cannot convert from NumberExpression<Integer> to int

I tried to write a model-class on my own, but here i get type-mismatch error (cannot convert from Qproduct_innovation_level to InnovationLevelModel).

2
Your second code snippet that is exactly how you do it directly (if you know your query only returns 1 value). What is the problem?Benjamin
Hi Benjamin. I extended my question, i don't know whawt class to use respectively how i can access the needed values.JoKo

2 Answers

0
votes

Ok, my answer may be a bit ridiculous, but maybe it's what you want...

Create a class with the following constructor and variables:

public class MyClass implements Serializable {

    public double ngEmployee;
    public double ngCustomer;
    /**
     * Default constructor
     */
    public MyClass(Tuple innovationLevel) {
        this.ngEmployee = innovationLevel.get(product_innovation_level.level_employee);
        this.ngCustomer = innovationLevel.get(product_innovation_level.level_customer);
    }

}

And then you can create an instance of that class like this:

MyClass mc=new MyClass(selectFrom(product_innovation_level).
                        where(product_innovation_level.id.eq(1)).
                        firstResult(product_innovation_level.level_customer, product_innovation_level.level_employee));

Magic... now you can access the variables using mc.ngEmployee and mc.ngCustomer

0
votes

This is where you use AnyLogic's visually-defined auto-creation of agent populations (including the agent type definition) from a database table.

Use the Agent wizard (from the top of the Agent palette): by creating a population of a new type, using a database table, AnyLogic will

  • Create a new agent type with parameters which match the columns in the table
  • Create a population of this agent type with an instance per row (other variants are possible), mapping the columns to the appropriate agent parameters

Thus, in your case, you could have a population innovationLevels of agent type InnovationLevel, where this agent had parameters

  • id (type int)
  • levelCustomer (type int)
  • levelEmployee (type int)

(and the wizard would auto-define this agent type and its use in a database-table-driven population).

See AnyLogic Help > Agent Based Modeling > Creating a new agent population based on DB data in the help.

If your IDs are sequential (e.g., 1,2,3,...) you can also then access a given innovation level by its position in the list (e.g., innovationLevels(1) for the second in the list which might be id 2). You can also do things like create a map (using a Collection) of id to InnovationLevel if you want to be able to 'randomly access' an innovation level by ID (if the IDs are not sequential).

Other agents could include references to InnovationLevel instances as needed.