Пример #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;

    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()));

      Collection<Declaration> col = decls.values();
      Declaration[] previousDeclarations = (Declaration[]) col.toArray(new Declaration[col.size()]);

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

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

      MVELDialectRuntimeData data =
          (MVELDialectRuntimeData)
              context
                  .getPkg()
                  .getDialectRuntimeRegistry()
                  .getDialectData(context.getDialect().getId());
      data.addCompileable(eval, expr);

      expr.compile(context.getPackageBuilder().getRootClassLoader());
      return eval;
    } catch (final Exception e) {
      context
          .getErrors()
          .add(
              new DescrBuildError(
                  context.getParentDescr(),
                  evalDescr,
                  e,
                  "Unable to build expression for 'eval':"
                      + e.getMessage()
                      + " '"
                      + evalDescr.getContent()
                      + "'"));
      return null;
    }
  }
Пример #2
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) {
   Appendable builder = new StringBuilderAppendable();
   EvalDescr edescr = (EvalDescr) descr;
   FunctionHandlers.dump((LispForm) edescr.getContent(), builder, true);
   edescr.setContent(builder.toString());
   return super.build(context, descr, prefixPattern);
 }
  /**
   * 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;

    try {
      final DroolsMVELFactory factory =
          new DroolsMVELFactory(
              context.getDeclarationResolver().getDeclarations(),
              null,
              context.getPkg().getGlobals());

      Dialect.AnalysisResult analysis =
          context
              .getDialect()
              .analyzeExpression(
                  context,
                  evalDescr,
                  evalDescr.getContent(),
                  new Set[] {
                    context.getDeclarationResolver().getDeclarations().keySet(),
                    context.getPkg().getGlobals().keySet()
                  });
      final List[] usedIdentifiers = analysis.getBoundIdentifiers();

      final Declaration[] declarations = new Declaration[usedIdentifiers[0].size()];
      for (int i = 0, size = usedIdentifiers[0].size(); i < size; i++) {
        declarations[i] =
            context.getDeclarationResolver().getDeclaration((String) usedIdentifiers[0].get(i));
      }

      final EvalCondition eval = new EvalCondition(declarations);

      Serializable expr =
          ((MVELDialect) context.getDialect())
              .compile((String) evalDescr.getContent(), analysis, null, null, null, context);

      eval.setEvalExpression(new MVELEvalExpression(expr, factory, context.getDialect().getId()));

      return eval;
    } catch (final Exception e) {
      context
          .getErrors()
          .add(
              new DescrBuildError(
                  context.getParentDescr(),
                  evalDescr,
                  e,
                  "Unable to build expression for 'eval' node '" + evalDescr.getContent() + "'"));
      return null;
    }
  }
  public void testSimpleExpression() {
    final Package pkg = new Package("pkg1");
    final RuleDescr ruleDescr = new RuleDescr("rule 1");

    PackageBuilder pkgBuilder = new PackageBuilder(pkg);
    final PackageBuilderConfiguration conf = pkgBuilder.getPackageBuilderConfiguration();
    MVELDialect mvelDialect = (MVELDialect) pkgBuilder.getDialectRegistry().getDialect("mvel");

    final InstrumentedBuildContent context =
        new InstrumentedBuildContent(
            conf, pkg, ruleDescr, pkgBuilder.getDialectRegistry(), mvelDialect);

    final InstrumentedDeclarationScopeResolver declarationResolver =
        new InstrumentedDeclarationScopeResolver();

    final FieldExtractor extractor =
        cache.getExtractor(Cheese.class, "price", getClass().getClassLoader());

    final Pattern pattern = new Pattern(0, new ClassObjectType(int.class));
    final Declaration declaration = new Declaration("a", extractor, pattern);
    final Map map = new HashMap();
    map.put("a", declaration);
    declarationResolver.setDeclarations(map);
    context.setDeclarationResolver(declarationResolver);

    final EvalDescr evalDescr = new EvalDescr();
    evalDescr.setContent("a == 10");

    final MVELEvalBuilder builder = new MVELEvalBuilder();
    final EvalCondition eval = (EvalCondition) builder.build(context, evalDescr);

    final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
    final WorkingMemory wm = ruleBase.newStatefulSession();

    final Cheese cheddar = new Cheese("cheddar", 10);
    final InternalFactHandle f0 = (InternalFactHandle) wm.insert(cheddar);
    final ReteTuple tuple = new ReteTuple(f0);

    Object evalContext = eval.createContext();

    assertTrue(eval.isAllowed(tuple, wm, evalContext));

    cheddar.setPrice(9);
    wm.update(f0, cheddar);
    assertFalse(eval.isAllowed(tuple, wm, evalContext));
  }
Пример #5
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;
  }
Пример #6
0
 public void visitEvalDescr(final EvalDescr descr) {
   this.template = new String();
   this.template = "<eval>" + descr.getText() + "</eval>" + XmlDumper.eol;
 }
Пример #7
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);
    }
  }