Esempio n. 1
0
  public static StateStreamRuntime parseInputStream(
      StateInputStream stateInputStream,
      ExecutionPlanContext executionPlanContext,
      MetaStateEvent metaStateEvent,
      Map<String, AbstractDefinition> streamDefinitionMap,
      Map<String, AbstractDefinition> tableDefinitionMap,
      Map<String, EventTable> eventTableMap,
      List<VariableExpressionExecutor> variableExpressionExecutors,
      LatencyTracker latencyTracker) {

    Map<String, ProcessStreamReceiver> processStreamReceiverMap =
        new HashMap<String, ProcessStreamReceiver>();

    StateStreamRuntime stateStreamRuntime =
        new StateStreamRuntime(executionPlanContext, metaStateEvent);

    String defaultLockKey = "";

    for (String streamId : stateInputStream.getAllStreamIds()) {
      int streamCount = stateInputStream.getStreamCount(streamId);
      if (streamCount == 1) {
        if (stateInputStream.getStateType() == StateInputStream.Type.SEQUENCE) {
          processStreamReceiverMap.put(
              streamId,
              new SequenceSingleProcessStreamReceiver(
                  streamId, stateStreamRuntime, defaultLockKey, latencyTracker));
        } else {
          processStreamReceiverMap.put(
              streamId,
              new PatternSingleProcessStreamReceiver(streamId, defaultLockKey, latencyTracker));
        }
      } else {
        if (stateInputStream.getStateType() == StateInputStream.Type.SEQUENCE) {
          processStreamReceiverMap.put(
              streamId,
              new SequenceMultiProcessStreamReceiver(
                  streamId, streamCount, stateStreamRuntime, defaultLockKey, latencyTracker));
        } else {
          processStreamReceiverMap.put(
              streamId,
              new PatternMultiProcessStreamReceiver(
                  streamId, streamCount, defaultLockKey, latencyTracker));
        }
      }
    }

    StateElement stateElement = stateInputStream.getStateElement();

    InnerStateRuntime innerStateRuntime =
        parse(
            stateElement,
            streamDefinitionMap,
            tableDefinitionMap,
            eventTableMap,
            metaStateEvent,
            executionPlanContext,
            variableExpressionExecutors,
            processStreamReceiverMap,
            null,
            null,
            stateInputStream.getStateType(),
            new ArrayList<Map.Entry<Long, Set<Integer>>>(),
            latencyTracker);

    stateStreamRuntime.setInnerStateRuntime(innerStateRuntime);

    return stateStreamRuntime;
  }
Esempio n. 2
0
  private static InnerStateRuntime parse(
      StateElement stateElement,
      Map<String, AbstractDefinition> streamDefinitionMap,
      Map<String, AbstractDefinition> tableDefinitionMap,
      Map<String, EventTable> eventTableMap,
      MetaStateEvent metaStateEvent,
      ExecutionPlanContext executionPlanContext,
      List<VariableExpressionExecutor> variableExpressionExecutors,
      Map<String, ProcessStreamReceiver> processStreamReceiverMap,
      StreamPreStateProcessor streamPreStateProcessor,
      StreamPostStateProcessor streamPostStateProcessor,
      StateInputStream.Type stateType,
      ArrayList<Map.Entry<Long, Set<Integer>>> withinStates,
      LatencyTracker latencyTracker) {

    if (stateElement instanceof StreamStateElement) {

      BasicSingleInputStream basicSingleInputStream =
          ((StreamStateElement) stateElement).getBasicSingleInputStream();
      SingleStreamRuntime singleStreamRuntime =
          SingleInputStreamParser.parseInputStream(
              basicSingleInputStream,
              executionPlanContext,
              variableExpressionExecutors,
              streamDefinitionMap,
              tableDefinitionMap,
              eventTableMap,
              metaStateEvent,
              processStreamReceiverMap.get(basicSingleInputStream.getUniqueStreamIds().get(0)),
              false,
              latencyTracker);

      int stateIndex = metaStateEvent.getStreamEventCount() - 1;
      if (streamPreStateProcessor == null) {

        if (stateElement.getWithin() != null) {
          Set<Integer> withinStateset = new HashSet<Integer>();
          withinStateset.add(SiddhiConstants.ANY);
          withinStates.add(
              0,
              new AbstractMap.SimpleEntry<Long, Set<Integer>>(
                  stateElement.getWithin().getValue(), withinStateset));
        }

        streamPreStateProcessor =
            new StreamPreStateProcessor(stateType, clonewithinStates(withinStates));
        streamPreStateProcessor.init(executionPlanContext);

        if (stateElement.getWithin() != null) {
          withinStates.remove(0);
        }
      }
      streamPreStateProcessor.setStateId(stateIndex);
      streamPreStateProcessor.setNextProcessor(singleStreamRuntime.getProcessorChain());
      singleStreamRuntime.setProcessorChain(streamPreStateProcessor);
      if (streamPostStateProcessor == null) {
        streamPostStateProcessor = new StreamPostStateProcessor();
      }
      streamPostStateProcessor.setStateId(stateIndex);
      singleStreamRuntime.getProcessorChain().setToLast(streamPostStateProcessor);
      streamPostStateProcessor.setThisStatePreProcessor(streamPreStateProcessor);
      streamPreStateProcessor.setThisStatePostProcessor(streamPostStateProcessor);

      StreamInnerStateRuntime innerStateRuntime = new StreamInnerStateRuntime(stateType);

      innerStateRuntime.setFirstProcessor(streamPreStateProcessor);
      innerStateRuntime.setLastProcessor(streamPostStateProcessor);
      innerStateRuntime.addStreamRuntime(singleStreamRuntime);

      return innerStateRuntime;

    } else if (stateElement instanceof NextStateElement) {

      StateElement currentElement = ((NextStateElement) stateElement).getStateElement();
      InnerStateRuntime currentInnerStateRuntime =
          parse(
              currentElement,
              streamDefinitionMap,
              tableDefinitionMap,
              eventTableMap,
              metaStateEvent,
              executionPlanContext,
              variableExpressionExecutors,
              processStreamReceiverMap,
              streamPreStateProcessor,
              streamPostStateProcessor,
              stateType,
              withinStates,
              latencyTracker);

      if (stateElement.getWithin() != null) {
        Set<Integer> withinStateSet = new HashSet<Integer>();
        withinStateSet.add(currentInnerStateRuntime.getFirstProcessor().getStateId());
        withinStateSet.add(currentInnerStateRuntime.getLastProcessor().getStateId());
        withinStates.add(
            0,
            new AbstractMap.SimpleEntry<Long, Set<Integer>>(
                stateElement.getWithin().getValue(), withinStateSet));
      }

      StateElement nextElement = ((NextStateElement) stateElement).getNextStateElement();
      InnerStateRuntime nextInnerStateRuntime =
          parse(
              nextElement,
              streamDefinitionMap,
              tableDefinitionMap,
              eventTableMap,
              metaStateEvent,
              executionPlanContext,
              variableExpressionExecutors,
              processStreamReceiverMap,
              streamPreStateProcessor,
              streamPostStateProcessor,
              stateType,
              withinStates,
              latencyTracker);

      if (stateElement.getWithin() != null) {
        withinStates.remove(0);
      }
      //            currentInnerStateRuntime.getFirstProcessor().getStateId()
      currentInnerStateRuntime
          .getLastProcessor()
          .setNextStatePreProcessor(nextInnerStateRuntime.getFirstProcessor());

      NextInnerStateRuntime nextStateRuntime =
          new NextInnerStateRuntime(currentInnerStateRuntime, nextInnerStateRuntime, stateType);
      nextStateRuntime.setFirstProcessor(currentInnerStateRuntime.getFirstProcessor());
      nextStateRuntime.setLastProcessor(nextInnerStateRuntime.getLastProcessor());

      for (SingleStreamRuntime singleStreamRuntime :
          currentInnerStateRuntime.getSingleStreamRuntimeList()) {
        nextStateRuntime.addStreamRuntime(singleStreamRuntime);
      }
      for (SingleStreamRuntime singleStreamRuntime :
          nextInnerStateRuntime.getSingleStreamRuntimeList()) {
        nextStateRuntime.addStreamRuntime(singleStreamRuntime);
      }

      return nextStateRuntime;

    } else if (stateElement instanceof EveryStateElement) {

      StateElement currentElement = ((EveryStateElement) stateElement).getStateElement();
      InnerStateRuntime innerStateRuntime =
          parse(
              currentElement,
              streamDefinitionMap,
              tableDefinitionMap,
              eventTableMap,
              metaStateEvent,
              executionPlanContext,
              variableExpressionExecutors,
              processStreamReceiverMap,
              streamPreStateProcessor,
              streamPostStateProcessor,
              stateType,
              withinStates,
              latencyTracker);

      EveryInnerStateRuntime everyInnerStateRuntime =
          new EveryInnerStateRuntime(innerStateRuntime, stateType);

      everyInnerStateRuntime.setFirstProcessor(innerStateRuntime.getFirstProcessor());
      everyInnerStateRuntime.setLastProcessor(innerStateRuntime.getLastProcessor());

      for (SingleStreamRuntime singleStreamRuntime :
          innerStateRuntime.getSingleStreamRuntimeList()) {
        everyInnerStateRuntime.addStreamRuntime(singleStreamRuntime);
      }
      if (stateType == StateInputStream.Type.PATTERN) {
        everyInnerStateRuntime
            .getLastProcessor()
            .setNextEveryStatePerProcessor(everyInnerStateRuntime.getFirstProcessor());
      }
      return everyInnerStateRuntime;

    } else if (stateElement instanceof LogicalStateElement) {

      LogicalStateElement.Type type = ((LogicalStateElement) stateElement).getType();

      if (stateElement.getWithin() != null) {
        Set<Integer> withinStateset = new HashSet<Integer>();
        withinStateset.add(SiddhiConstants.ANY);
        withinStates.add(
            0,
            new AbstractMap.SimpleEntry<Long, Set<Integer>>(
                stateElement.getWithin().getValue(), withinStateset));
      }

      LogicalPreStateProcessor logicalPreStateProcessor1 =
          new LogicalPreStateProcessor(type, stateType, withinStates);
      logicalPreStateProcessor1.init(executionPlanContext);
      LogicalPostStateProcessor logicalPostStateProcessor1 = new LogicalPostStateProcessor(type);

      LogicalPreStateProcessor logicalPreStateProcessor2 =
          new LogicalPreStateProcessor(type, stateType, withinStates);
      logicalPreStateProcessor2.init(executionPlanContext);
      LogicalPostStateProcessor logicalPostStateProcessor2 = new LogicalPostStateProcessor(type);

      if (stateElement.getWithin() != null) {
        withinStates.remove(0);
      }

      logicalPostStateProcessor1.setPartnerPreStateProcessor(logicalPreStateProcessor2);
      logicalPostStateProcessor2.setPartnerPreStateProcessor(logicalPreStateProcessor1);

      logicalPostStateProcessor1.setPartnerPostStateProcessor(logicalPostStateProcessor2);
      logicalPostStateProcessor2.setPartnerPostStateProcessor(logicalPostStateProcessor1);

      logicalPreStateProcessor1.setPartnerStatePreProcessor(logicalPreStateProcessor2);
      logicalPreStateProcessor2.setPartnerStatePreProcessor(logicalPreStateProcessor1);

      StateElement stateElement2 = ((LogicalStateElement) stateElement).getStreamStateElement2();
      InnerStateRuntime innerStateRuntime2 =
          parse(
              stateElement2,
              streamDefinitionMap,
              tableDefinitionMap,
              eventTableMap,
              metaStateEvent,
              executionPlanContext,
              variableExpressionExecutors,
              processStreamReceiverMap,
              logicalPreStateProcessor2,
              logicalPostStateProcessor2,
              stateType,
              withinStates,
              latencyTracker);

      StateElement stateElement1 = ((LogicalStateElement) stateElement).getStreamStateElement1();
      InnerStateRuntime innerStateRuntime1 =
          parse(
              stateElement1,
              streamDefinitionMap,
              tableDefinitionMap,
              eventTableMap,
              metaStateEvent,
              executionPlanContext,
              variableExpressionExecutors,
              processStreamReceiverMap,
              logicalPreStateProcessor1,
              logicalPostStateProcessor1,
              stateType,
              withinStates,
              latencyTracker);

      LogicalInnerStateRuntime logicalInnerStateRuntime =
          new LogicalInnerStateRuntime(innerStateRuntime1, innerStateRuntime2, stateType);

      logicalInnerStateRuntime.setFirstProcessor(innerStateRuntime1.getFirstProcessor());
      logicalInnerStateRuntime.setLastProcessor(innerStateRuntime2.getLastProcessor());

      for (SingleStreamRuntime singleStreamRuntime :
          innerStateRuntime2.getSingleStreamRuntimeList()) {
        logicalInnerStateRuntime.addStreamRuntime(singleStreamRuntime);
      }

      for (SingleStreamRuntime singleStreamRuntime :
          innerStateRuntime1.getSingleStreamRuntimeList()) {
        logicalInnerStateRuntime.addStreamRuntime(singleStreamRuntime);
      }

      return logicalInnerStateRuntime;

    } else if (stateElement instanceof CountStateElement) {

      int minCount = ((CountStateElement) stateElement).getMinCount();
      int maxCount = ((CountStateElement) stateElement).getMaxCount();
      if (minCount == SiddhiConstants.ANY) {
        minCount = 0;
      }
      if (maxCount == SiddhiConstants.ANY) {
        maxCount = Integer.MAX_VALUE;
      }

      if (stateElement.getWithin() != null) {
        Set<Integer> withinStateset = new HashSet<Integer>();
        withinStateset.add(SiddhiConstants.ANY);
        withinStates.add(
            0,
            new AbstractMap.SimpleEntry<Long, Set<Integer>>(
                stateElement.getWithin().getValue(), withinStateset));
      }

      CountPreStateProcessor countPreStateProcessor =
          new CountPreStateProcessor(minCount, maxCount, stateType, withinStates);
      countPreStateProcessor.init(executionPlanContext);
      CountPostStateProcessor countPostStateProcessor =
          new CountPostStateProcessor(minCount, maxCount);

      if (stateElement.getWithin() != null) {
        withinStates.remove(0);
      }

      countPreStateProcessor.setCountPostStateProcessor(countPostStateProcessor);
      StateElement currentElement = ((CountStateElement) stateElement).getStreamStateElement();
      InnerStateRuntime innerStateRuntime =
          parse(
              currentElement,
              streamDefinitionMap,
              tableDefinitionMap,
              eventTableMap,
              metaStateEvent,
              executionPlanContext,
              variableExpressionExecutors,
              processStreamReceiverMap,
              countPreStateProcessor,
              countPostStateProcessor,
              stateType,
              withinStates,
              latencyTracker);

      return new CountInnerStateRuntime((StreamInnerStateRuntime) innerStateRuntime);

    } else {
      throw new OperationNotSupportedException();
      // todo support not
    }
  }