我想找到随后发生的事件模式
内在模式是:
这种模式需要
我写了一些东西,但我知道它确实不完整。
两个问题
我的输出是
3> {first=[Customer[timestamp=50,customerId=111,toAdd=2,sensorData=33]], second=[Customer[timestamp=100,customerId=222,toAdd=2,sensorData=33], Customer[timestamp=600,customerId=333,toAdd=2,sensorData=33]]}
即使我想要的输出应该是所有前六个事件(用户111/222和传感器是33然后44然后55
Pattern<Customer, ?> sameUserDifferentSensor = Pattern.<Customer>begin("first", skipStrategy)
.followedBy("second").where(new IterativeCondition<Customer>() {
@Override
public boolean filter(Customer currCustomerEvent, Context<Customer> ctx) throws Exception {
List<Customer> firstPatternEvents = Lists.newArrayList(ctx.getEventsForPattern("first"));
int i = firstPatternEvents.size();
int currSensorData = currCustomerEvent.getSensorData();
int prevSensorData = firstPatternEvents.get(i-1).getSensorData();
int currCustomerId = currCustomerEvent.getCustomerId();
int prevCustomerId = firstPatternEvents.get(i-1).getCustomerId();
return currSensorData==prevSensorData && currCustomerId!=prevCustomerId;
}
})
.within(Time.seconds(5))
.timesOrMore(3);
PatternStream<Customer> sameUserDifferentSensorPatternStream = CEP.pattern(customerStream, sameUserDifferentSensor);
DataStream<String> alerts1 = sameUserDifferentSensorPatternStream.select((PatternSelectFunction<Customer, String>) Object::toString);
如果您首先通过sensorArea键入流,您将会有更轻松的时间。它们将在流上进行模式匹配,其中所有事件都针对单个sensorArea,这将使模式更容易表达,匹配更有效。
您无法避免使用迭代条件和ctx,但在键入流后它应该更便宜。
此外,您的代码示例与文本说明不匹配。文本说“在5秒内”和“3次或更多次”,而代码有within(Time.seconds(2))
和timesOrMore(2)
。