有没有一种方法可以随机生成从数据库创建的代理?
Is there a method to randomly generate agents created from a database?
Example of packages 我目前正在使用从源节点的数据库中读取的参数生成代理。模型中的这些代理是不同类型的包(因此基本上包具有相同的参数名称列表,如包名称、体积等,但具有不同的参数值)。问题是我需要随机生成这些包,但它目前是按照包在数据库中列出的顺序生成的。有什么方法可以修改代码以实现所需的功能吗?
当前代码片段:
{
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;
}
您可以使用代码从数据库中读取,然后将列表打乱以使其随机化,然后生成具有其特征的代理。
List <Tuple> x=selectFrom(db).list();
Collections.shuffle(x);
for(Tuple t : x)
add_agents(t.get(db.whatever));
这可以使用以下代码实现。此示例适用于具有 3 条记录的 table:
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)
);
所以假设输入 db_table 看起来像这样:
db_name
db_value1
db_value2
a
foo1
1
b
bar10
10
c
foo100
100
然后上面的代码将产生这个输出:
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}
很明显,它从数据库中的 3 中随机选择一条记录并打印其内容。
Example of packages 我目前正在使用从源节点的数据库中读取的参数生成代理。模型中的这些代理是不同类型的包(因此基本上包具有相同的参数名称列表,如包名称、体积等,但具有不同的参数值)。问题是我需要随机生成这些包,但它目前是按照包在数据库中列出的顺序生成的。有什么方法可以修改代码以实现所需的功能吗?
当前代码片段:
{
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;
}
您可以使用代码从数据库中读取,然后将列表打乱以使其随机化,然后生成具有其特征的代理。
List <Tuple> x=selectFrom(db).list();
Collections.shuffle(x);
for(Tuple t : x)
add_agents(t.get(db.whatever));
这可以使用以下代码实现。此示例适用于具有 3 条记录的 table:
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)
);
所以假设输入 db_table 看起来像这样:
db_name | db_value1 | db_value2 |
---|---|---|
a | foo1 | 1 |
b | bar10 | 10 |
c | foo100 | 100 |
然后上面的代码将产生这个输出:
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}
很明显,它从数据库中的 3 中随机选择一条记录并打印其内容。