在事件处理中遍历列表
Iterating over list in event processing
我目前正在尝试通过脚本在事件处理中迭代 ManagedObjects 列表。
它应该检查列表中每个 ManagedObject
的条件,一旦条件为真 return 即 Object.
这是我的脚本:
create expression ManagedObject getCurrentDepot(position, listObjects) [
for (i = 0; i <= listObjects.length; i++) {
var obj = listObjects[i];
var distance = distance(position.lat, position.lng, obj.Geofence.lat, obj.Geofence.lng);
if (distance <= obj.radius ) {
depot;
}
}
null;
];
我收到以下错误:
Error in statement mytest:statement_2! : Incorrect syntax near ';' at line 3 column 26
它抱怨这一行:
var obj = listObjects[i];
如何正确地遍历列表?
尝试
var distance;
listObjects.forEach(function(obj) {
distance = distance(position.lat, position.lng, obj.Geofence.lat, obj.Geofence.lng);
if (distance <= obj.radius ) {
depot;
}
});
最好的解决方案是不在表达式中进行循环,而在事件处理本身中进行。
create expression Boolean isInRange(position, element) [
var distance = distance(position.lat, position.lng, element.Geofence.lat, obj.Geofence.lng);
if (distance <= element.radius ) {
true;
}
false;
];
insert into MyStreamWithDepot
select
event.listObjects.firstOf(element => isInRange(event.position, element) is true) as currentDepot
from InputEvent event;
您可以在此处查看 "firstOf" 方法的文档 esper documentation
我目前正在尝试通过脚本在事件处理中迭代 ManagedObjects 列表。
它应该检查列表中每个 ManagedObject
的条件,一旦条件为真 return 即 Object.
这是我的脚本:
create expression ManagedObject getCurrentDepot(position, listObjects) [
for (i = 0; i <= listObjects.length; i++) {
var obj = listObjects[i];
var distance = distance(position.lat, position.lng, obj.Geofence.lat, obj.Geofence.lng);
if (distance <= obj.radius ) {
depot;
}
}
null;
];
我收到以下错误:
Error in statement mytest:statement_2! : Incorrect syntax near ';' at line 3 column 26
它抱怨这一行:
var obj = listObjects[i];
如何正确地遍历列表?
尝试
var distance;
listObjects.forEach(function(obj) {
distance = distance(position.lat, position.lng, obj.Geofence.lat, obj.Geofence.lng);
if (distance <= obj.radius ) {
depot;
}
});
最好的解决方案是不在表达式中进行循环,而在事件处理本身中进行。
create expression Boolean isInRange(position, element) [
var distance = distance(position.lat, position.lng, element.Geofence.lat, obj.Geofence.lng);
if (distance <= element.radius ) {
true;
}
false;
];
insert into MyStreamWithDepot
select
event.listObjects.firstOf(element => isInRange(event.position, element) is true) as currentDepot
from InputEvent event;
您可以在此处查看 "firstOf" 方法的文档 esper documentation