@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);
    }
  }
예제 #2
0
  private static void bigBlobCompile() throws DroolsParserException, IOException, Exception {
    StringBuilder buf = new StringBuilder();
    buf.append(getHeader());

    for (int i = 0; i < RULE_COUNT; i++) {
      String name = "x" + i;
      int status = i;

      String r = getTemplate1(name, status);
      buf.append(r);
    }

    /* love you */ long time = System.currentTimeMillis();

    DrlParser ps = new DrlParser(LanguageLevelOption.DRL5);
    PackageDescr pkg = ps.parse(new StringReader(buf.toString()));

    System.err.println("Time taken for parsing: " + (System.currentTimeMillis() - time));

    time = System.currentTimeMillis();
    PackageBuilder b = new PackageBuilder();
    b.addPackage(pkg);
    assertFalse(b.getErrors().toString(), b.hasErrors());

    System.err.println("Time taken for compiling: " + (System.currentTimeMillis() - time));
    time = System.currentTimeMillis();

    Package p = b.getPackage();
    RuleBase rb = RuleBaseFactory.newRuleBase();

    rb.addPackage(p);

    System.err.println("Time taken rete building: " + (System.currentTimeMillis() - time));

    File f = new File("foo.rulebase");
    if (f.exists()) f.delete();

    time = System.currentTimeMillis();
    ObjectOutput out = new DroolsObjectOutputStream(new FileOutputStream(f));
    out.writeObject(rb);
    out.flush();
    out.close();
    System.err.println("Time taken serializing rulebase: " + (System.currentTimeMillis() - time));

    time = System.currentTimeMillis();
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
    RuleBase rb_ = (RuleBase) in.readObject();
    System.err.println(
        "Time taken de-serializing rulebase: " + (System.currentTimeMillis() - time));
  }
예제 #3
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);
 }
  @Override
  public KObject toKObject(final Path path) {
    KObject index = null;

    try {
      final String drl = ioService.readAllString(path);
      final DrlParser drlParser = new DrlParser();
      final PackageDescr packageDescr = drlParser.parse(true, drl);

      if (drlParser.hasErrors()) {
        final List<DroolsError> errors = drlParser.getErrors();
        logger.warn(
            ErrorMessageUtilities.makeErrorMessage(
                path, errors.toArray(new DroolsError[errors.size()])));
        return index;
      }
      if (packageDescr == null) {
        logger.warn(ErrorMessageUtilities.makeErrorMessage(path));
        return index;
      }

      final ProjectDataModelOracle dmo = getProjectDataModelOracle(path);
      final Project project = projectService.resolveProject(Paths.convert(path));
      final Package pkg = projectService.resolvePackage(Paths.convert(path));

      final DefaultIndexBuilder builder = new DefaultIndexBuilder(project, pkg);
      final PackageDescrIndexVisitor visitor =
          new PackageDescrIndexVisitor(dmo, builder, packageDescr);
      visitor.visit();

      index = KObjectUtil.toKObject(path, builder.build());

    } catch (Exception e) {
      logger.error("Unable to index '" + path.toUri().toString() + "'.", e);
    }

    return index;
  }