Beispiel #1
0
  /**
   * Builds and returns an Eval Conditional Element
   *
   * @param context The current build context
   * @param utils The current build utils instance
   * @param patternBuilder not used by EvalBuilder
   * @param descr The Eval Descriptor to build the eval conditional element from
   * @return the Eval Conditional Element
   */
  public RuleConditionElement build(
      final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    // it must be an EvalDescr
    final EvalDescr evalDescr = (EvalDescr) descr;

    final String className = "eval" + context.getNextId();

    evalDescr.setClassMethodName(className);

    Map<String, Declaration> decls =
        context.getDeclarationResolver().getDeclarations(context.getRule());

    AnalysisResult analysis =
        context
            .getDialect()
            .analyzeExpression(
                context,
                evalDescr,
                evalDescr.getContent(),
                new BoundIdentifiers(
                    context.getDeclarationResolver().getDeclarationClasses(decls),
                    context.getPackageBuilder().getGlobals()));
    final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();

    final Declaration[] declarations = decls.values().toArray(new Declaration[decls.size()]);
    Arrays.sort(declarations, SortDeclarations.instance);

    final EvalCondition eval = new EvalCondition(declarations);

    final Map map =
        createVariableContext(
            className,
            (String) evalDescr.getContent(),
            context,
            declarations,
            null,
            usedIdentifiers.getGlobals(),
            null);

    generatTemplates("evalMethod", "evalInvoker", context, className, map, eval, descr);

    return eval;
  }
  private Declaration[] getUsedDeclarations(
      Map<String, Declaration> decls, AnalysisResult analysis) {
    final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
    List<Declaration> usedDeclarations = new ArrayList<Declaration>();
    for (String id : usedIdentifiers.getDeclrClasses().keySet()) {
      if (decls.containsKey(id)) {
        usedDeclarations.add(decls.get(id));
      }
    }

    if (!usedDeclarations.isEmpty()) {
      Collections.sort(usedDeclarations, SortDeclarations.instance);
    }

    return usedDeclarations.toArray(new Declaration[usedDeclarations.size()]);
  }
Beispiel #3
0
  /**
   * Builds and returns an Eval Conditional Element
   *
   * @param context The current build context
   * @param utils The current build utils instance
   * @param patternBuilder not used by EvalBuilder
   * @param descr The Eval Descriptor to build the eval conditional element from
   * @return the Eval Conditional Element
   */
  public RuleConditionElement build(
      final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    boolean typesafe = context.isTypesafe();
    // it must be an EvalDescr
    final EvalDescr evalDescr = (EvalDescr) descr;

    try {
      MVELDialect dialect = (MVELDialect) context.getDialect(context.getDialect().getId());

      Map<String, Declaration> decls =
          context.getDeclarationResolver().getDeclarations(context.getRule());

      AnalysisResult analysis =
          context
              .getDialect()
              .analyzeExpression(
                  context,
                  evalDescr,
                  evalDescr.getContent(),
                  new BoundIdentifiers(
                      context.getDeclarationResolver().getDeclarationClasses(decls),
                      context.getPackageBuilder().getGlobals()));

      final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
      int i = usedIdentifiers.getDeclrClasses().keySet().size();
      Declaration[] previousDeclarations = new Declaration[i];
      i = 0;
      for (String id : usedIdentifiers.getDeclrClasses().keySet()) {
        previousDeclarations[i++] = decls.get(id);
      }
      Arrays.sort(previousDeclarations, SortDeclarations.instance);

      MVELCompilationUnit unit =
          dialect.getMVELCompilationUnit(
              (String) evalDescr.getContent(),
              analysis,
              previousDeclarations,
              null,
              null,
              context,
              "drools",
              KnowledgeHelper.class,
              false);
      final EvalCondition eval = new EvalCondition(previousDeclarations);

      MVELEvalExpression expr = new MVELEvalExpression(unit, dialect.getId());
      eval.setEvalExpression(expr);

      MVELDialectRuntimeData data =
          (MVELDialectRuntimeData)
              context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
      data.addCompileable(eval, expr);

      expr.compile(data);
      return eval;
    } catch (final Exception e) {
      copyErrorLocation(e, evalDescr);
      context.addError(
          new DescrBuildError(
              context.getParentDescr(),
              evalDescr,
              e,
              "Unable to build expression for 'eval':"
                  + e.getMessage()
                  + " '"
                  + evalDescr.getContent()
                  + "'"));
      return null;
    } finally {
      context.setTypesafe(typesafe);
    }
  }