コード例 #1
0
  private void setupTest(String consequence, Map<String, Object> namedConsequences) {
    builder = new MVELConsequenceBuilder();

    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools.compiler.test");
    pkg.addImport(new ImportDeclaration(Cheese.class.getCanonicalName()));

    KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
    KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg, conf);

    ruleDescr = new RuleDescr("test consequence builder");
    ruleDescr.setConsequence(consequence);
    ruleDescr.addAttribute(new AttributeDescr("dialect", "mvel"));

    for (Entry<String, Object> entry : namedConsequences.entrySet()) {
      ruleDescr.addNamedConsequences(entry.getKey(), entry.getValue());
    }

    RuleImpl rule = new RuleImpl(ruleDescr.getName());
    rule.addPattern(new Pattern(0, new ClassObjectType(Cheese.class), "$cheese"));

    rule.addPattern(new Pattern(0, new ClassObjectType(Map.class), "$map"));

    PackageRegistry pkgRegistry = pkgBuilder.getPackageRegistry(pkg.getName());
    DialectCompiletimeRegistry reg =
        pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    context =
        new RuleBuildContext(
            pkgBuilder, ruleDescr, reg, pkg, reg.getDialect(pkgRegistry.getDialect()));
    context.getBuildStack().push(rule.getLhs());

    context.getDialect().getConsequenceBuilder().build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
    for (String name : namedConsequences.keySet()) {
      context.getDialect().getConsequenceBuilder().build(context, name);
    }

    context.getDialect().addRule(context);
    pkgRegistry.getPackage().addRule(context.getRule());
    pkgBuilder.compileAll();
    pkgBuilder.reloadAll();
    if (pkgBuilder.hasErrors()) {
      fail(pkgBuilder.getErrors().toString());
    }
  }
コード例 #2
0
  public void build(final RuleBuildContext context, String consequenceName) {

    // pushing consequence LHS into the stack for variable resolution
    context.getBuildStack().push(context.getRule().getLhs());

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

      final RuleDescr ruleDescr = context.getRuleDescr();

      String text =
          (Rule.DEFAULT_CONSEQUENCE_NAME.equals(consequenceName))
              ? (String) ruleDescr.getConsequence()
              : (String) ruleDescr.getNamedConsequences().get(consequenceName);

      text = processMacros(text);

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

      AnalysisResult analysis =
          dialect.analyzeBlock(
              context,
              context.getRuleDescr(),
              dialect.getInterceptors(),
              text,
              new BoundIdentifiers(
                  DeclarationScopeResolver.getDeclarationClasses(decls),
                  context.getPackageBuilder().getGlobals(),
                  null,
                  KnowledgeHelper.class),
              null,
              "drools",
              KnowledgeHelper.class);

      if (analysis == null) {
        // something bad happened, issue already logged in errors
        return;
      }

      final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();

      final Declaration[] declarations = new Declaration[usedIdentifiers.getDeclrClasses().size()];
      String[] declrStr = new String[declarations.length];
      int j = 0;
      for (String str : usedIdentifiers.getDeclrClasses().keySet()) {
        declrStr[j] = str;
        declarations[j++] = decls.get(str);
      }
      Arrays.sort(declarations, SortDeclarations.instance);
      for (int i = 0; i < declrStr.length; i++) {
        declrStr[i] = declarations[i].getIdentifier();
      }
      context.getRule().setRequiredDeclarationsForConsequence(consequenceName, declrStr);
      MVELCompilationUnit unit =
          dialect.getMVELCompilationUnit(
              text,
              analysis,
              declarations,
              null,
              null,
              context,
              "drools",
              KnowledgeHelper.class,
              false);

      MVELConsequence expr = new MVELConsequence(unit, dialect.getId());

      if (Rule.DEFAULT_CONSEQUENCE_NAME.equals(consequenceName)) {
        context.getRule().setConsequence(expr);
      } else {
        context.getRule().addNamedConsequence(consequenceName, expr);
      }

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

      expr.compile(data);
    } catch (final Exception e) {
      copyErrorLocation(e, context.getRuleDescr());
      context.addError(
          new DescrBuildError(
              context.getParentDescr(),
              context.getRuleDescr(),
              null,
              "Unable to build expression for 'consequence': "
                  + e.getMessage()
                  + " '"
                  + context.getRuleDescr().getConsequence()
                  + "'"));
    }
  }