0

Example of packages I am currently generating agents with parameters read from DB at a source node. These agents in the model are packages of different types (so basically the packages have the same list of parameter names like package name, volume, etc. but with differing parameter values). The issue is that I need to generate these packages randomly, but it is currently generated in sequence of how the packages are listed in DB. Is there any way to amend the code in order to achieve what is needed?

Snippet of current code:

{
DbPackages _result_xjal = new DbPackages(); 
_result_xjal.setParametersToDefaultValues();
_result_xjal.packageDb = self.databaseTable.getValue( "package_db", String.class );
_result_xjal.tester = self.databaseTable.getValue( "tester", String.class );
_result_xjal.handler = self.databaseTable.getValue( "handler", String.class );
...
return _result_xjal;
}
user16171092
  • 135
  • 8
  • Could you share a screenshot with a few records from the table that contains the packages? Also, when you say *random*, do you mean that on some event (like once every minute) the simulation just picks a record at random and creates a package for it? – Artem P. Jun 09 '21 at 09:16
  • @ArtemP. Hi I've added in the image as a link. Hope you are able to view it. Yes, for the random I would like a random package to be generated at every interval. Is this possible? – user16171092 Jun 09 '21 at 09:56

2 Answers2

0

You can read from the database with code, then shuffle the list to randomize it and then generate the agents with their characteristics.

List <Tuple> x=selectFrom(db).list();
Collections.shuffle(x);
for(Tuple t : x)
    add_agents(t.get(db.whatever));
Felipe
  • 8,311
  • 2
  • 15
  • 31
  • I've attempted to use the code you have provided, but was faced with the following errors: 1) x cannot be resolved to a variable 2) List cannot be resolved to a variable 3) Tuple cannot be resolved to a variable 4) Syntax error on token "=", <= expected 5) packageDb (relating to whatever in db.whatever) cannot be resolved or is not a field Would also like to clarify that this code would create a random list of the agents? Subsequently how should I ask for the agent to be generated from the list? Also, does that mean that throughout the simulation, the same sequence will be followed? – user16171092 Jun 09 '21 at 06:33
0

This can be achieved using the following code. This example is for a table with 3 records:

List<Tuple> vals = 
    selectFrom(db_table)
        .offset(uniform_discr(0, 2)) // <- this randomly offsets the output
        .orderBy(db_table.db_name.asc())
        .list();

Tuple t = vals.get(0); // <- this line picks the first record in result
traceln("Got value: {%s, %s, %d}", 
    t.get(db_table.db_name),
    t.get(db_table.db_value1),
    t.get(db_table.db_value2)
    );

So suppose the input db_table looks like this:

db_name db_value1 db_value2
a foo1 1
b bar10 10
c foo100 100

Then the above code will produce this output:

Got value: {c, foo100, 100}
Got value: {b, bar10, 10}
Got value: {a, foo1, 1}
Got value: {a, foo1, 1}
Got value: {c, foo100, 100}
Got value: {a, foo1, 1}
Got value: {c, foo100, 100}
Got value: {c, foo100, 100}
Got value: {c, foo100, 100}
Got value: {c, foo100, 100}
Got value: {b, bar10, 10}
Got value: {b, bar10, 10}
Got value: {a, foo1, 1}

as evident, it picks a random record from 3 in the database and prints its contents.

Artem P.
  • 816
  • 1
  • 6
  • 8