@Override
public QueryState transition(Query query, QueryEvent event) {
// increase the count for completed subqueries
query.completedSubQueryCount++;
SubQueryCompletedEvent castEvent = (SubQueryCompletedEvent) event;
ExecutionBlockCursor cursor = query.getExecutionBlockCursor();
MasterPlan masterPlan = query.getPlan();
// if the subquery is succeeded
if (castEvent.getFinalState() == SubQueryState.SUCCEEDED) {
ExecutionBlock nextBlock = cursor.nextBlock();
if (!query.getPlan().isTerminal(nextBlock) || !query.getPlan().isRoot(nextBlock)) {
SubQuery nextSubQuery = new SubQuery(query.context, query.getPlan(), nextBlock, query.sm);
nextSubQuery.setPriority(query.priority--);
query.addSubQuery(nextSubQuery);
nextSubQuery.handle(new SubQueryEvent(nextSubQuery.getId(),