Example #1
0
  @Test
  public void testExpanderErrorsAfterExpansion() throws Exception {

    String name = "expander_post_errors.dslr";
    Expander expander = new DefaultExpander();
    String expanded = expander.expand(this.getReader(name));

    DRLParser parser = parse(name, expanded);
    parser.compilationUnit();
    assertTrue(parser.hasErrors());

    assertEquals(1, parser.getErrors().size());
    DroolsParserException err = (DroolsParserException) parser.getErrors().get(0);
    assertEquals(6, err.getLineNumber());
  }
 @SuppressWarnings("unchecked")
 private ConstraintConnectiveDescr parseExpression(
     final RuleBuildContext context, final PatternDescr patternDescr, final String expression) {
   DrlExprParser parser = new DrlExprParser(context.getConfiguration().getLanguageLevel());
   ConstraintConnectiveDescr result = parser.parse(expression);
   if (result == null || parser.hasErrors()) {
     for (DroolsParserException error : parser.getErrors()) {
       context.addError(
           new DescrBuildError(
               context.getParentDescr(),
               patternDescr,
               null,
               "Unable to parser pattern expression:\n" + error.getMessage()));
     }
     return null;
   }
   return result;
 }
  @SuppressWarnings("unchecked")
  private void processBinding(
      RuleBuildContext context,
      BaseDescr descr,
      Declaration[] params,
      List<Integer> declrIndexes,
      List<Integer> varIndexes,
      List<Object> arguments,
      List<Declaration> requiredDeclarations,
      InternalReadAccessor arrayReader,
      Pattern pattern,
      BindingDescr bind,
      ConstraintConnectiveDescr result) {
    Declaration declr =
        context.getDeclarationResolver().getDeclaration(context.getRule(), bind.getVariable());
    if (declr != null) {
      // check right maps to a slot, otherwise we can't reverse this and should error
      int pos = getPos(bind.getExpression(), params);
      if (pos >= 0) {
        // slot exist, reverse and continue
        String slot = bind.getExpression();
        String var = bind.getVariable();
        bind.setVariable(slot);
        bind.setExpression(var);
      } else {
        // else error, we cannot find the slot to unify against
      }
    }

    // left does not already exist, is it a slot?
    int pos = getPos(bind.getVariable(), params);
    if (pos >= 0) {
      // it's an input on a slot, is the input using bindings?
      declr =
          context.getDeclarationResolver().getDeclaration(context.getRule(), bind.getExpression());
      if (declr != null) {
        arguments.set(pos, declr);
        declrIndexes.add(pos);
        requiredDeclarations.add(declr);
      } else {
        // it must be a literal/expression
        // it's an expression and thus an input
        DrlExprParser parser = new DrlExprParser(context.getConfiguration().getLanguageLevel());
        ConstraintConnectiveDescr bresult = parser.parse(bind.getExpression());
        if (parser.hasErrors()) {
          for (DroolsParserException error : parser.getErrors()) {
            context.addError(
                new DescrBuildError(
                    context.getParentDescr(),
                    descr,
                    null,
                    "Unable to parser pattern expression:\n" + error.getMessage()));
          }
          return;
        }

        MVELDumper.MVELDumperContext mvelCtx = new MVELDumper.MVELDumperContext();
        String expr = context.getCompilerFactory().getExpressionProcessor().dump(bresult, mvelCtx);
        try {
          Object o = MVEL.eval(expr);
          arguments.set(pos, o); // for now we just work with literals
        } catch (Exception e) {
          context.addError(
              new DescrBuildError(
                  context.getParentDescr(), descr, null, "Unable to compile expression:\n" + expr));
        }
      }
    } else {
      // this is creating a new output binding
      // we know it doesn't exist, as we already checked for left == var
      declr = pattern.addDeclaration(bind.getVariable());

      pos = getPos(bind.getExpression(), params);
      if (pos < 0) {
        // error this must be a binding on a slot
        context.addError(
            new DescrBuildError(
                context.getParentDescr(),
                descr,
                null,
                "named argument does not exist:\n" + bind.getExpression()));
        return;
      }

      // this bit is different, notice its the ArrayElementReader that we wire up to, not the
      // declaration.
      ArrayElementReader reader =
          new ArrayElementReader(arrayReader, pos, params[pos].getExtractor().getExtractToClass());

      // Should the reader be registered like the others? Probably yes...
      // PatternBuilder.registerReadAccessor(  );

      declr.setReadAccessor(reader);

      varIndexes.add(pos);
      arguments.set(pos, Variable.v);
    }
  }