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;
}
答案 0 :(得分:0)
您可以使用代码从数据库中读取,然后将列表打乱以随机化,然后生成具有其特征的代理。
List <Tuple> x=selectFrom(db).list();
Collections.shuffle(x);
for(Tuple t : x)
add_agents(t.get(db.whatever));
答案 1 :(得分:0)
这可以使用以下代码实现。此示例适用于包含 3 条记录的表:
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 |
---|---|---|
一 | 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 中随机选择一条记录并打印其内容。