Repast Simphony Java 中的等价物是使用查询。查询将谓词应用于上下文中的每个代理,并在迭代器中返回那些评估为 true 的代理。PropertyEquals 查询将代理的属性 w/r 评估为某个值(例如“wealth_type”和“rich”)。注意这里的“property”指的是Java属性,即getter类型的方法:
String getWealthType() {
return wealthType;
}
其中“wealthType”是财产的名称。
例如,在 JZombies 示例模型中,我们可以查询能量等于 5 的 Humans。
Query<Object> query = new PropertyEquals<Object>(context, "energy", 5);
for (Object o : query.query()) {
Human h = (Human)o;
System.out.println(h.getEnergy());
}
query() 迭代器返回所有能量等于 5 的人。
通过提供自己的谓词,您可以在等价测试中变得更复杂一些。例如,
PropertyEqualsPredicate<Integer, Integer> pep = (a, b) -> {
return a * 2 == b;
};
Query<Object> query2 = new PropertyEquals<Object>(context, "energy", 8, pep);
for (Object o : query2.query()) {
Human h = (Human)o;
System.out.println(h.getEnergy());
}
在这里,我们正在检查能量 * 2 == 8。谓词在第一个参数中传递代理的属性值,在第二个参数中传递要比较的值。鉴于谓词返回布尔值,您还可以测试不等式、大于等。
Simphony 提供多种查询。看,
https://repast.github.io/docs/api/repast_simphony/repast/simphony/query/package-summary.html
https://repast.github.io/docs/RepastReference/RepastReference.html#_repast_model_design_fundamental_concepts
了解更多信息。
您也可以在 Simphony 的 ReLogo 方言中执行此操作:
ask (turtles()){
if (wealth_type == "rich") {
donateMoney()
}
if (wealth_type == "poor") {
getMoneyFromRichPeople()
}
}
如果你只想收集你可以做的richTurtles(其中“it”是访问使用 findAll 迭代的单个海龟的默认方法):
richTurtles = turtles().findAll{
it.wealth_type == "rich"
}
或使用明确的闭包参数:
richTurtles = turtles().findAll{x->
x.wealth_type == "rich"
}