use of org.ballerinalang.siddhi.core.event.state.StateEvent in project ballerina by ballerina-lang.
the class AbsentLogicalPreStateProcessor method process.
@Override
public void process(ComplexEventChunk complexEventChunk) {
if (!this.active) {
return;
}
this.lock.lock();
boolean notProcessed = true;
try {
long currentTime = complexEventChunk.getFirst().getTimestamp();
if (currentTime >= this.lastArrivalTime + waitingTime) {
ComplexEventChunk<StateEvent> retEventChunk = new ComplexEventChunk<>(false);
Iterator<StateEvent> iterator;
if (isStartState && stateType == StateInputStream.Type.SEQUENCE && newAndEveryStateEventList.isEmpty() && pendingStateEventList.isEmpty()) {
StateEvent stateEvent = stateEventPool.borrowEvent();
addState(stateEvent);
} else if (stateType == StateInputStream.Type.SEQUENCE && !newAndEveryStateEventList.isEmpty()) {
this.resetState();
}
this.updateState();
iterator = pendingStateEventList.iterator();
while (iterator.hasNext()) {
StateEvent stateEvent = iterator.next();
// Remove expired events based on within
if (withinStates.size() > 0) {
if (isExpired(stateEvent, currentTime)) {
iterator.remove();
continue;
}
}
// Collect the events that came before the waiting time
if (waitingTimePassed(currentTime, stateEvent)) {
iterator.remove();
if (logicalType == LogicalStateElement.Type.OR && stateEvent.getStreamEvent(partnerStatePreProcessor.getStateId()) == null) {
// OR Partner not received
stateEvent.addEvent(stateId, streamEventPool.borrowEvent());
retEventChunk.add(stateEvent);
} else if (logicalType == LogicalStateElement.Type.AND && stateEvent.getStreamEvent(partnerStatePreProcessor.getStateId()) != null) {
// AND partner received but didn't send out
retEventChunk.add(stateEvent);
} else if (logicalType == LogicalStateElement.Type.AND && stateEvent.getStreamEvent(partnerStatePreProcessor.getStateId()) == null) {
// AND partner didn't receive
// Let the partner to process or not
stateEvent.addEvent(stateId, streamEventPool.borrowEvent());
}
}
}
retEventChunk.reset();
notProcessed = retEventChunk.getFirst() == null;
while (retEventChunk.hasNext()) {
StateEvent stateEvent = retEventChunk.next();
retEventChunk.remove();
sendEvent(stateEvent);
}
this.lastArrivalTime = 0;
}
} finally {
this.lock.unlock();
}
if (thisStatePostProcessor.nextEveryStatePerProcessor != null || (notProcessed && isStartState)) {
// If every or (notProcessed and startState), schedule again
long nextBreak;
if (lastArrivalTime == 0) {
nextBreak = siddhiAppContext.getTimestampGenerator().currentTime() + waitingTime;
} else {
nextBreak = lastArrivalTime + waitingTime;
}
this.scheduler.notifyAt(nextBreak);
}
}
use of org.ballerinalang.siddhi.core.event.state.StateEvent in project ballerina by ballerina-lang.
the class CountPreStateProcessor method processAndReturn.
@Override
public ComplexEventChunk<StateEvent> processAndReturn(ComplexEventChunk complexEventChunk) {
ComplexEventChunk<StateEvent> returnEventChunk = new ComplexEventChunk<StateEvent>(false);
complexEventChunk.reset();
// Sure only one will be sent
StreamEvent streamEvent = (StreamEvent) complexEventChunk.next();
for (Iterator<StateEvent> iterator = pendingStateEventList.iterator(); iterator.hasNext(); ) {
StateEvent stateEvent = iterator.next();
if (removeIfNextStateProcessed(stateEvent, iterator, stateId + 1)) {
continue;
}
if (removeIfNextStateProcessed(stateEvent, iterator, stateId + 2)) {
continue;
}
stateEvent.addEvent(stateId, streamEventCloner.copyStreamEvent(streamEvent));
successCondition = false;
process(stateEvent);
if (this.thisLastProcessor.isEventReturned()) {
this.thisLastProcessor.clearProcessedEvent();
returnEventChunk.add(stateEvent);
}
if (stateChanged) {
iterator.remove();
}
if (!successCondition) {
switch(stateType) {
case PATTERN:
stateEvent.removeLastEvent(stateId);
break;
case SEQUENCE:
stateEvent.removeLastEvent(stateId);
iterator.remove();
break;
}
}
}
return returnEventChunk;
}
use of org.ballerinalang.siddhi.core.event.state.StateEvent in project ballerina by ballerina-lang.
the class LogicalPreStateProcessor method processAndReturn.
@Override
public ComplexEventChunk<StateEvent> processAndReturn(ComplexEventChunk complexEventChunk) {
ComplexEventChunk<StateEvent> returnEventChunk = new ComplexEventChunk<StateEvent>(false);
complexEventChunk.reset();
// Sure only one will be sent
StreamEvent streamEvent = (StreamEvent) complexEventChunk.next();
for (Iterator<StateEvent> iterator = pendingStateEventList.iterator(); iterator.hasNext(); ) {
StateEvent stateEvent = iterator.next();
if (withinStates.size() > 0) {
if (isExpired(stateEvent, streamEvent.getTimestamp())) {
iterator.remove();
continue;
}
}
if (logicalType == LogicalStateElement.Type.OR && stateEvent.getStreamEvent(partnerStatePreProcessor.getStateId()) != null) {
iterator.remove();
continue;
}
stateEvent.setEvent(stateId, streamEventCloner.copyStreamEvent(streamEvent));
process(stateEvent);
if (this.thisLastProcessor.isEventReturned()) {
this.thisLastProcessor.clearProcessedEvent();
returnEventChunk.add(stateEvent);
}
if (stateChanged) {
iterator.remove();
} else {
switch(stateType) {
case PATTERN:
stateEvent.setEvent(stateId, null);
break;
case SEQUENCE:
stateEvent.setEvent(stateId, null);
iterator.remove();
break;
}
}
}
return returnEventChunk;
}
use of org.ballerinalang.siddhi.core.event.state.StateEvent in project ballerina by ballerina-lang.
the class LogicalPreStateProcessor method addEveryState.
@Override
public void addEveryState(StateEvent stateEvent) {
StateEvent clonedEvent = stateEventCloner.copyStateEvent(stateEvent);
clonedEvent.setEvent(stateId, null);
newAndEveryStateEventList.add(clonedEvent);
if (partnerStatePreProcessor != null) {
clonedEvent.setEvent(partnerStatePreProcessor.stateId, null);
partnerStatePreProcessor.newAndEveryStateEventList.add(clonedEvent);
}
}
use of org.ballerinalang.siddhi.core.event.state.StateEvent in project ballerina by ballerina-lang.
the class FindStoreQueryRuntime method execute.
@Override
public Event[] execute() {
try {
StateEvent stateEvent = new StateEvent(1, 0);
StreamEvent streamEvents = null;
switch(eventType) {
case TABLE:
streamEvents = table.find(stateEvent, compiledCondition);
break;
case WINDOW:
streamEvents = window.find(stateEvent, compiledCondition);
break;
case AGGREGATE:
stateEvent = new StateEvent(2, 0);
StreamEvent streamEvent = new StreamEvent(0, 2, 0);
stateEvent.addEvent(0, streamEvent);
streamEvents = aggregation.find(stateEvent, compiledCondition);
break;
case DEFAULT:
break;
}
if (streamEvents == null) {
return null;
} else {
if (selector != null) {
return executeSelector(streamEvents, eventType);
} else {
List<Event> events = new ArrayList<Event>();
while (streamEvents != null) {
events.add(new Event(streamEvents.getTimestamp(), streamEvents.getOutputData()));
streamEvents = streamEvents.getNext();
}
return events.toArray(new Event[0]);
}
}
} catch (Throwable t) {
throw new StoreQueryRuntimeException("Error executing '" + queryName + "', " + t.getMessage(), t);
}
}
Aggregations