Repast Simphony Java 中的等效项是使用查询。查询将谓词应用于上下文中的每个代理,并返回在迭代器中计算结果为 true 的谓词。 PropertyEquals 查询将代理的财产评估为某个值(例如“wealth_type”和“rich”)。注意,这里的“属性”指的是Java属性,即getter类型的方法:
String getWealthType() {
return wealthType;
}
其中“wealthType”是财产的名称。
例如,在 JZombies 示例模型中,我们可以查询能量等于 5 的人类。
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());
}
在这里,我们检查 energy * 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"
}