Program: 28755
Project: 28755
Buggy source:
/** 
 * Parse a query and return corresponding QueryRuntime.
 * @param query                    query to be parsed.
 * @param siddhiAppContext         associated Siddhi app context.
 * @param streamDefinitionMap      keyvalue containing user given stream definitions.
 * @param tableDefinitionMap       keyvalue containing table definitions.
 * @param windowDefinitionMap      keyvalue containing window definition map.
 * @param aggregationDefinitionMap keyvalue containing aggregation definition map.
 * @param tableMap                 keyvalue containing event tables.
 * @param aggregationMap           keyvalue containing aggrigation runtimes.
 * @param windowMap                keyvalue containing event window map.
 * @param lockSynchronizer         Lock synchronizer for sync the lock across queries.
 * @param queryIndex               query index to identify unknown query by number
 * @return queryRuntime
 */
public static QueryRuntime parse(Query query,SiddhiAppContext siddhiAppContext,Map<String,AbstractDefinition> streamDefinitionMap,Map<String,AbstractDefinition> tableDefinitionMap,Map<String,AbstractDefinition> windowDefinitionMap,Map<String,AbstractDefinition> aggregationDefinitionMap,Map<String,Table> tableMap,Map<String,AggregationRuntime> aggregationMap,Map<String,Window> windowMap,LockSynchronizer lockSynchronizer,String queryIndex){
  List<VariableExpressionExecutor> executors=new ArrayList<VariableExpressionExecutor>();
  QueryRuntime queryRuntime;
  Element nameElement=null;
  LatencyTracker latencyTracker=null;
  LockWrapper lockWrapper=null;
  try {
    nameElement=AnnotationHelper.getAnnotationElement("String_Node_Str","String_Node_Str",query.getAnnotations());
    String queryName=null;
    if (nameElement != null) {
      queryName=nameElement.getValue();
    }
 else {
      queryName="String_Node_Str" + queryIndex + "String_Node_Str"+ UUID.randomUUID().toString();
    }
    latencyTracker=QueryParserHelper.createLatencyTracker(siddhiAppContext,queryName,SiddhiConstants.METRIC_INFIX_QUERIES,null);
    OutputStream.OutputEventType outputEventType=query.getOutputStream().getOutputEventType();
    boolean outputExpectsExpiredEvents=false;
    if (outputEventType != OutputStream.OutputEventType.CURRENT_EVENTS) {
      outputExpectsExpiredEvents=true;
    }
    StreamRuntime streamRuntime=InputStreamParser.parse(query.getInputStream(),siddhiAppContext,streamDefinitionMap,tableDefinitionMap,windowDefinitionMap,aggregationDefinitionMap,tableMap,windowMap,aggregationMap,executors,latencyTracker,outputExpectsExpiredEvents,queryName);
    QuerySelector selector=SelectorParser.parse(query.getSelector(),query.getOutputStream(),siddhiAppContext,streamRuntime.getMetaComplexEvent(),tableMap,executors,queryName);
    boolean isWindow=query.getInputStream() instanceof JoinInputStream;
    if (!isWindow && query.getInputStream() instanceof SingleInputStream) {
      for (      StreamHandler streamHandler : ((SingleInputStream)query.getInputStream()).getStreamHandlers()) {
        if (streamHandler instanceof org.wso2.siddhi.query.api.execution.query.input.handler.Window) {
          isWindow=true;
          break;
        }
      }
    }
    Element synchronizedElement=AnnotationHelper.getAnnotationElement("String_Node_Str",null,query.getAnnotations());
    if (synchronizedElement != null) {
      if (!("String_Node_Str".equalsIgnoreCase(synchronizedElement.getValue()))) {
        lockWrapper=new LockWrapper("String_Node_Str");
        lockWrapper.setLock(new ReentrantLock());
      }
    }
 else {
      if (isWindow || !(streamRuntime instanceof SingleStreamRuntime)) {
        if (streamRuntime instanceof JoinStreamRuntime) {
          MetaStateEvent metaStateEvent=(MetaStateEvent)streamRuntime.getMetaComplexEvent();
          MetaStreamEvent[] metaStreamEvents=metaStateEvent.getMetaStreamEvents();
          if (metaStreamEvents[0].getEventType() == EventType.WINDOW && metaStreamEvents[1].getEventType() == EventType.WINDOW) {
            LockWrapper leftLockWrapper=windowMap.get(metaStreamEvents[0].getLastInputDefinition().getId()).getLock();
            LockWrapper rightLockWrapper=windowMap.get(metaStreamEvents[1].getLastInputDefinition().getId()).getLock();
            if (!leftLockWrapper.equals(rightLockWrapper)) {
              lockSynchronizer.sync(leftLockWrapper,rightLockWrapper);
            }
            lockWrapper=leftLockWrapper;
          }
 else           if (metaStreamEvents[0].getEventType() == EventType.WINDOW) {
            lockWrapper=windowMap.get(metaStreamEvents[0].getLastInputDefinition().getId()).getLock();
          }
 else           if (metaStreamEvents[1].getEventType() == EventType.WINDOW) {
            lockWrapper=windowMap.get(metaStreamEvents[1].getLastInputDefinition().getId()).getLock();
          }
 else {
            lockWrapper=new LockWrapper("String_Node_Str");
            lockWrapper.setLock(new ReentrantLock());
          }
        }
 else {
          lockWrapper=new LockWrapper("String_Node_Str");
          lockWrapper.setLock(new ReentrantLock());
        }
      }
    }
    OutputRateLimiter outputRateLimiter=OutputParser.constructOutputRateLimiter(query.getOutputStream().getId(),query.getOutputRate(),query.getSelector().getGroupByList().size() != 0,isWindow,siddhiAppContext.getScheduledExecutorService(),siddhiAppContext,queryName);
    if (outputRateLimiter instanceof WrappedSnapshotOutputRateLimiter) {
      selector.setBatchingEnabled(false);
    }
    siddhiAppContext.addEternalReferencedHolder(outputRateLimiter);
    OutputCallback outputCallback=OutputParser.constructOutputCallback(query.getOutputStream(),streamRuntime.getMetaComplexEvent().getOutputStreamDefinition(),tableMap,windowMap,siddhiAppContext,!(streamRuntime instanceof SingleStreamRuntime),queryName);
    QueryParserHelper.reduceMetaComplexEvent(streamRuntime.getMetaComplexEvent());
    QueryParserHelper.updateVariablePosition(streamRuntime.getMetaComplexEvent(),executors);
    QueryParserHelper.initStreamRuntime(streamRuntime,streamRuntime.getMetaComplexEvent(),lockWrapper,queryName);
    selector.setEventPopulator(StateEventPopulatorFactory.constructEventPopulator(streamRuntime.getMetaComplexEvent()));
    queryRuntime=new QueryRuntime(query,siddhiAppContext,streamRuntime,selector,outputRateLimiter,outputCallback,streamRuntime.getMetaComplexEvent(),lockWrapper != null,queryName);
    if (outputRateLimiter instanceof WrappedSnapshotOutputRateLimiter) {
      selector.setBatchingEnabled(false);
      ((WrappedSnapshotOutputRateLimiter)outputRateLimiter).init(streamRuntime.getMetaComplexEvent().getOutputStreamDefinition().getAttributeList().size(),selector.getAttributeProcessorList(),streamRuntime.getMetaComplexEvent());
    }
    outputRateLimiter.init(siddhiAppContext,lockWrapper,queryName);
  }
 catch (  DuplicateDefinitionException e) {
    if (nameElement != null) {
      throw new DuplicateDefinitionException(e.getMessageWithOutContext() + "String_Node_Str" + nameElement.getValue(),e,e.getQueryContextStartIndex(),e.getQueryContextEndIndex(),siddhiAppContext.getName(),siddhiAppContext.getSiddhiAppString());
    }
 else {
      throw new DuplicateDefinitionException(e.getMessage(),e,e.getQueryContextStartIndex(),e.getQueryContextEndIndex(),siddhiAppContext.getName(),siddhiAppContext.getSiddhiAppString());
    }
  }
catch (  Throwable t) {
    ExceptionUtil.populateQueryContext(t,query,siddhiAppContext);
    throw t;
  }
  return queryRuntime;
}

Fixed version (ground truth):
/** 
 * Parse a query and return corresponding QueryRuntime.
 * @param query                    query to be parsed.
 * @param siddhiAppContext         associated Siddhi app context.
 * @param streamDefinitionMap      keyvalue containing user given stream definitions.
 * @param tableDefinitionMap       keyvalue containing table definitions.
 * @param windowDefinitionMap      keyvalue containing window definition map.
 * @param aggregationDefinitionMap keyvalue containing aggregation definition map.
 * @param tableMap                 keyvalue containing event tables.
 * @param aggregationMap           keyvalue containing aggrigation runtimes.
 * @param windowMap                keyvalue containing event window map.
 * @param lockSynchronizer         Lock synchronizer for sync the lock across queries.
 * @param queryIndex               query index to identify unknown query by number
 * @return queryRuntime
 */
public static QueryRuntime parse(Query query,SiddhiAppContext siddhiAppContext,Map<String,AbstractDefinition> streamDefinitionMap,Map<String,AbstractDefinition> tableDefinitionMap,Map<String,AbstractDefinition> windowDefinitionMap,Map<String,AbstractDefinition> aggregationDefinitionMap,Map<String,Table> tableMap,Map<String,AggregationRuntime> aggregationMap,Map<String,Window> windowMap,LockSynchronizer lockSynchronizer,String queryIndex){
  List<VariableExpressionExecutor> executors=new ArrayList<VariableExpressionExecutor>();
  QueryRuntime queryRuntime;
  Element nameElement=null;
  LatencyTracker latencyTracker=null;
  LockWrapper lockWrapper=null;
  try {
    nameElement=AnnotationHelper.getAnnotationElement("String_Node_Str","String_Node_Str",query.getAnnotations());
    String queryName=null;
    if (nameElement != null) {
      queryName=nameElement.getValue();
    }
 else {
      queryName="String_Node_Str" + queryIndex + "String_Node_Str"+ UUID.randomUUID().toString();
    }
    latencyTracker=QueryParserHelper.createLatencyTracker(siddhiAppContext,queryName,SiddhiConstants.METRIC_INFIX_QUERIES,null);
    OutputStream.OutputEventType outputEventType=query.getOutputStream().getOutputEventType();
    boolean outputExpectsExpiredEvents=false;
    if (outputEventType != OutputStream.OutputEventType.CURRENT_EVENTS) {
      outputExpectsExpiredEvents=true;
    }
    StreamRuntime streamRuntime=InputStreamParser.parse(query.getInputStream(),siddhiAppContext,streamDefinitionMap,tableDefinitionMap,windowDefinitionMap,aggregationDefinitionMap,tableMap,windowMap,aggregationMap,executors,latencyTracker,outputExpectsExpiredEvents,queryName);
    QuerySelector selector=SelectorParser.parse(query.getSelector(),query.getOutputStream(),siddhiAppContext,streamRuntime.getMetaComplexEvent(),tableMap,executors,queryName,SiddhiConstants.UNKNOWN_STATE);
    boolean isWindow=query.getInputStream() instanceof JoinInputStream;
    if (!isWindow && query.getInputStream() instanceof SingleInputStream) {
      for (      StreamHandler streamHandler : ((SingleInputStream)query.getInputStream()).getStreamHandlers()) {
        if (streamHandler instanceof org.wso2.siddhi.query.api.execution.query.input.handler.Window) {
          isWindow=true;
          break;
        }
      }
    }
    Element synchronizedElement=AnnotationHelper.getAnnotationElement("String_Node_Str",null,query.getAnnotations());
    if (synchronizedElement != null) {
      if (!("String_Node_Str".equalsIgnoreCase(synchronizedElement.getValue()))) {
        lockWrapper=new LockWrapper("String_Node_Str");
        lockWrapper.setLock(new ReentrantLock());
      }
    }
 else {
      if (isWindow || !(streamRuntime instanceof SingleStreamRuntime)) {
        if (streamRuntime instanceof JoinStreamRuntime) {
          MetaStateEvent metaStateEvent=(MetaStateEvent)streamRuntime.getMetaComplexEvent();
          MetaStreamEvent[] metaStreamEvents=metaStateEvent.getMetaStreamEvents();
          if (metaStreamEvents[0].getEventType() == EventType.WINDOW && metaStreamEvents[1].getEventType() == EventType.WINDOW) {
            LockWrapper leftLockWrapper=windowMap.get(metaStreamEvents[0].getLastInputDefinition().getId()).getLock();
            LockWrapper rightLockWrapper=windowMap.get(metaStreamEvents[1].getLastInputDefinition().getId()).getLock();
            if (!leftLockWrapper.equals(rightLockWrapper)) {
              lockSynchronizer.sync(leftLockWrapper,rightLockWrapper);
            }
            lockWrapper=leftLockWrapper;
          }
 else           if (metaStreamEvents[0].getEventType() == EventType.WINDOW) {
            lockWrapper=windowMap.get(metaStreamEvents[0].getLastInputDefinition().getId()).getLock();
          }
 else           if (metaStreamEvents[1].getEventType() == EventType.WINDOW) {
            lockWrapper=windowMap.get(metaStreamEvents[1].getLastInputDefinition().getId()).getLock();
          }
 else {
            lockWrapper=new LockWrapper("String_Node_Str");
            lockWrapper.setLock(new ReentrantLock());
          }
        }
 else {
          lockWrapper=new LockWrapper("String_Node_Str");
          lockWrapper.setLock(new ReentrantLock());
        }
      }
    }
    OutputRateLimiter outputRateLimiter=OutputParser.constructOutputRateLimiter(query.getOutputStream().getId(),query.getOutputRate(),query.getSelector().getGroupByList().size() != 0,isWindow,siddhiAppContext.getScheduledExecutorService(),siddhiAppContext,queryName);
    if (outputRateLimiter instanceof WrappedSnapshotOutputRateLimiter) {
      selector.setBatchingEnabled(false);
    }
    siddhiAppContext.addEternalReferencedHolder(outputRateLimiter);
    OutputCallback outputCallback=OutputParser.constructOutputCallback(query.getOutputStream(),streamRuntime.getMetaComplexEvent().getOutputStreamDefinition(),tableMap,windowMap,siddhiAppContext,!(streamRuntime instanceof SingleStreamRuntime),queryName);
    QueryParserHelper.reduceMetaComplexEvent(streamRuntime.getMetaComplexEvent());
    QueryParserHelper.updateVariablePosition(streamRuntime.getMetaComplexEvent(),executors);
    QueryParserHelper.initStreamRuntime(streamRuntime,streamRuntime.getMetaComplexEvent(),lockWrapper,queryName);
    selector.setEventPopulator(StateEventPopulatorFactory.constructEventPopulator(streamRuntime.getMetaComplexEvent()));
    queryRuntime=new QueryRuntime(query,siddhiAppContext,streamRuntime,selector,outputRateLimiter,outputCallback,streamRuntime.getMetaComplexEvent(),lockWrapper != null,queryName);
    if (outputRateLimiter instanceof WrappedSnapshotOutputRateLimiter) {
      selector.setBatchingEnabled(false);
      ((WrappedSnapshotOutputRateLimiter)outputRateLimiter).init(streamRuntime.getMetaComplexEvent().getOutputStreamDefinition().getAttributeList().size(),selector.getAttributeProcessorList(),streamRuntime.getMetaComplexEvent());
    }
    outputRateLimiter.init(siddhiAppContext,lockWrapper,queryName);
  }
 catch (  DuplicateDefinitionException e) {
    if (nameElement != null) {
      throw new DuplicateDefinitionException(e.getMessageWithOutContext() + "String_Node_Str" + nameElement.getValue(),e,e.getQueryContextStartIndex(),e.getQueryContextEndIndex(),siddhiAppContext.getName(),siddhiAppContext.getSiddhiAppString());
    }
 else {
      throw new DuplicateDefinitionException(e.getMessage(),e,e.getQueryContextStartIndex(),e.getQueryContextEndIndex(),siddhiAppContext.getName(),siddhiAppContext.getSiddhiAppString());
    }
  }
catch (  Throwable t) {
    ExceptionUtil.populateQueryContext(t,query,siddhiAppContext);
    throw t;
  }
  return queryRuntime;
}