Ejemplo n.º 1
0
  /**
   * Parse the given property name returning a Property instance for the property.
   *
   * @param propertyName is the property name to parse
   * @param isRootedDynamic is true to indicate that the property is already rooted in a dynamic
   *     property and therefore all child properties should be dynamic properties as well
   * @return Property instance for property
   */
  public static Property parse(String propertyName, boolean isRootedDynamic) {
    Tree tree = parse(propertyName);

    if ((ExecutionPathDebugLog.isDebugEnabled) && (log.isDebugEnabled())) {
      ASTUtil.dumpAST(tree);
    }

    if (tree.getChildCount() == 1) {
      return makeProperty(tree.getChild(0), isRootedDynamic);
    }

    List<Property> properties = new LinkedList<Property>();
    boolean isRootedInDynamic = isRootedDynamic;
    for (int i = 0; i < tree.getChildCount(); i++) {
      Tree child = tree.getChild(i);

      Property property = makeProperty(child, isRootedInDynamic);
      if (property instanceof DynamicSimpleProperty) {
        isRootedInDynamic = true;
      }
      properties.add(property);
    }

    return new NestedProperty(properties);
  }
 public void testGetPropertyNameEscaped() throws Exception {
   final String PROPERTY = "a\\.b\\.c";
   Pair<Tree, CommonTokenStream> parsed = SupportParserHelper.parseEventProperty(PROPERTY);
   Tree propertyNameExprNode = parsed.getFirst();
   ASTUtil.dumpAST(propertyNameExprNode);
   String propertyName = ASTFilterSpecHelper.getPropertyName(propertyNameExprNode, 0);
   assertEquals(PROPERTY, propertyName);
 }
  public void testGetPropertyName() throws Exception {
    final String PROPERTY = "a('aa').b[1].c";

    // Should parse and result in the exact same property name
    Pair<Tree, CommonTokenStream> parsed = SupportParserHelper.parseEventProperty(PROPERTY);
    Tree propertyNameExprNode = parsed.getFirst();
    ASTUtil.dumpAST(propertyNameExprNode);
    String propertyName = ASTFilterSpecHelper.getPropertyName(propertyNameExprNode, 0);
    assertEquals(PROPERTY, propertyName);

    // Try AST with tokens separated, same property name
    parsed = SupportParserHelper.parseEventProperty("a(    'aa'   ). b [ 1 ] . c");
    propertyNameExprNode = parsed.getFirst();
    propertyName = ASTFilterSpecHelper.getPropertyName(propertyNameExprNode, 0);
    assertEquals(PROPERTY, propertyName);
  }
Ejemplo n.º 4
0
  /**
   * Build an output limit spec from the AST node supplied.
   *
   * @param node - parse node
   * @param astExprNodeMap is the map of current AST tree nodes to their respective expression root
   *     node
   * @param engineURI the engine uri
   * @param timeProvider provides time
   * @param variableService provides variable resolution
   * @param exprEvaluatorContext context for expression evaluatiom
   * @return output limit spec
   */
  public static OutputLimitSpec buildOutputLimitSpec(
      Tree node,
      Map<Tree, ExprNode> astExprNodeMap,
      VariableService variableService,
      String engineURI,
      TimeProvider timeProvider,
      ExprEvaluatorContext exprEvaluatorContext) {
    int count = 0;
    Tree child = node.getChild(count);

    // parse type
    OutputLimitLimitType displayLimit = OutputLimitLimitType.DEFAULT;
    if (child.getType() == EsperEPL2GrammarParser.FIRST) {
      displayLimit = OutputLimitLimitType.FIRST;
      child = node.getChild(++count);
    } else if (child.getType() == EsperEPL2GrammarParser.LAST) {
      displayLimit = OutputLimitLimitType.LAST;
      child = node.getChild(++count);
    } else if (child.getType() == EsperEPL2GrammarParser.SNAPSHOT) {
      displayLimit = OutputLimitLimitType.SNAPSHOT;
      child = node.getChild(++count);
    } else if (child.getType() == EsperEPL2GrammarParser.ALL) {
      displayLimit = OutputLimitLimitType.ALL;
      child = node.getChild(++count);
    }

    // next is a variable, or time period, or number
    String variableName = null;
    double rate = -1;
    ExprNode whenExpression = null;
    List<ExprNode> crontabScheduleSpec = null;
    List<OnTriggerSetAssignment> thenExpressions = null;
    ExprTimePeriod timePeriodExpr = null;

    if (node.getType() == EsperEPL2GrammarParser.WHEN_LIMIT_EXPR) {
      Tree expressionNode = node.getChild(count);
      whenExpression = astExprNodeMap.remove(expressionNode);
      if (node.getChildCount() > count + 1) {
        Tree setExpr = ASTUtil.findFirstNode(node, EsperEPL2GrammarParser.ON_SET_EXPR);
        thenExpressions = EPLTreeWalker.getOnTriggerSetAssignments(setExpr, astExprNodeMap);
      }
    } else if (node.getType() == EsperEPL2GrammarParser.CRONTAB_LIMIT_EXPR) {
      Tree parent = node.getChild(0);
      if (parent.getType() != EsperEPL2GrammarParser.CRONTAB_LIMIT_EXPR_PARAM) {
        parent = node.getChild(1);
      }

      crontabScheduleSpec = new ArrayList<ExprNode>(parent.getChildCount());
      for (int i = 0; i < parent.getChildCount(); i++) {
        crontabScheduleSpec.add(astExprNodeMap.remove(parent.getChild(i)));
      }
    } else if (node.getType() == EsperEPL2GrammarParser.AFTER_LIMIT_EXPR) {
      // no action here, since AFTER time may occur in all
    } else {
      if (child.getType() == EsperEPL2GrammarParser.IDENT) {
        variableName = child.getText();
      } else if (child.getType() == EsperEPL2GrammarParser.TIME_PERIOD) {
        ExprNode expression = astExprNodeMap.remove(child);

        try {
          ExprValidationContext validationContext =
              new ExprValidationContext(
                  new StreamTypeServiceImpl(engineURI, false),
                  null,
                  null,
                  timeProvider,
                  variableService,
                  exprEvaluatorContext,
                  null,
                  null,
                  null,
                  null);
          timePeriodExpr =
              (ExprTimePeriod) ExprNodeUtility.getValidatedSubtree(expression, validationContext);
        } catch (ExprValidationException ex) {
          throw new ASTWalkException("Invalid time period expresion: " + ex.getMessage(), ex);
        }
      } else {
        rate = Double.parseDouble(child.getText());
      }
    }

    // get the AFTER time period
    ExprTimePeriod afterTimePeriodExpr = null;
    Integer afterNumberOfEvents = null;
    for (int i = 0; i < node.getChildCount(); i++) {
      if (node.getChild(i).getType() == EsperEPL2GrammarParser.AFTER) {
        ExprNode expression = astExprNodeMap.remove(node.getChild(i).getChild(0));
        if (expression != null) {
          try {
            ExprValidationContext validationContext =
                new ExprValidationContext(
                    new StreamTypeServiceImpl(engineURI, false),
                    null,
                    null,
                    timeProvider,
                    variableService,
                    exprEvaluatorContext,
                    null,
                    null,
                    null,
                    null);
            afterTimePeriodExpr =
                (ExprTimePeriod) ExprNodeUtility.getValidatedSubtree(expression, validationContext);
          } catch (ExprValidationException ex) {
            throw new ASTWalkException("Invalid time period expresion: " + ex.getMessage(), ex);
          }
        } else {
          Object constant = ASTConstantHelper.parse(node.getChild(i).getChild(0));
          afterNumberOfEvents = ((Number) constant).intValue();
        }
      }
    }

    switch (node.getType()) {
      case EsperEPL2GrammarParser.EVENT_LIMIT_EXPR:
        return new OutputLimitSpec(
            rate,
            variableName,
            OutputLimitRateType.EVENTS,
            displayLimit,
            null,
            null,
            null,
            null,
            afterTimePeriodExpr,
            afterNumberOfEvents);
      case EsperEPL2GrammarParser.TIMEPERIOD_LIMIT_EXPR:
        return new OutputLimitSpec(
            null,
            null,
            OutputLimitRateType.TIME_PERIOD,
            displayLimit,
            null,
            null,
            null,
            timePeriodExpr,
            afterTimePeriodExpr,
            afterNumberOfEvents);
      case EsperEPL2GrammarParser.CRONTAB_LIMIT_EXPR:
        return new OutputLimitSpec(
            null,
            null,
            OutputLimitRateType.CRONTAB,
            displayLimit,
            null,
            null,
            crontabScheduleSpec,
            null,
            afterTimePeriodExpr,
            afterNumberOfEvents);
      case EsperEPL2GrammarParser.WHEN_LIMIT_EXPR:
        return new OutputLimitSpec(
            null,
            null,
            OutputLimitRateType.WHEN_EXPRESSION,
            displayLimit,
            whenExpression,
            thenExpressions,
            null,
            null,
            afterTimePeriodExpr,
            afterNumberOfEvents);
      case EsperEPL2GrammarParser.AFTER_LIMIT_EXPR:
        return new OutputLimitSpec(
            null,
            null,
            OutputLimitRateType.AFTER,
            displayLimit,
            null,
            null,
            null,
            null,
            afterTimePeriodExpr,
            afterNumberOfEvents);
      default:
        throw new IllegalArgumentException(
            "Node type " + node.getType() + " not a recognized output limit type");
    }
  }