@Test
  public void testMVELDebugSymbols() throws DroolsParserException {

    MVELDebugHandler.setDebugMode(true);

    try {
      final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
      final PackageDescr pkgDescr =
          parser.parse(new InputStreamReader(getClass().getResourceAsStream("mvel_rule.drl")));

      // just checking there is no parsing errors
      assertFalse(parser.getErrors().toString(), parser.hasErrors());

      InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools");

      final RuleDescr ruleDescr = pkgDescr.getRules().get(0);

      final RuleBuilder builder = new RuleBuilder();

      final KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
      final KnowledgeBuilderConfigurationImpl conf = pkgBuilder.getBuilderConfiguration();
      DialectCompiletimeRegistry dialectRegistry =
          pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
      Dialect dialect = dialectRegistry.getDialect("mvel");

      RuleBuildContext context =
          new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, dialect);

      builder.build(context);

      assertTrue(context.getErrors().toString(), context.getErrors().isEmpty());

      final RuleImpl rule = context.getRule();

      MVELConsequence mvelCons = (MVELConsequence) rule.getConsequence();
      mvelCons.compile(
          (MVELDialectRuntimeData)
              pkgBuilder
                  .getPackageRegistry(pkg.getName())
                  .getDialectRuntimeRegistry()
                  .getDialectData("mvel"));
      String s = DebugTools.decompile(mvelCons.getCompExpr());

      int fromIndex = 0;
      int count = 0;
      while ((fromIndex = s.indexOf("DEBUG_SYMBOL", fromIndex + 1)) > -1) {
        count++;
      }
      assertEquals(4, count);
    } finally {
      MVELDebugHandler.setDebugMode(false);
    }
  }
Example #2
0
 @Test
 public void testDebug() throws Exception {
   String rule =
       "package com.sample; dialect \"mvel\" rule myRule when then\n System.out.println( \"test\" ); end";
   PackageBuilder builder = new PackageBuilder();
   DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
   PackageDescr packageDescr = parser.parse(rule);
   RuleDescr ruleDescr = packageDescr.getRules().get(0);
   builder = new PackageBuilder();
   builder.addPackage(packageDescr);
   Package pkg = builder.getPackage();
   MVELConsequence consequence = (MVELConsequence) pkg.getRule("myRule").getConsequence();
   String sourceName = ((CompiledExpression) consequence.getCompExpr()).getSourceName();
   System.out.println(sourceName);
   String ruleName = ruleDescr.getNamespace() + "." + ruleDescr.getClassName();
   System.out.println(ruleName);
   assertEquals(sourceName, ruleName);
 }
  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()
                  + "'"));
    }
  }