Exemplo n.º 1
0
  /**
   * Expand constructions like rules and queries
   *
   * @param drl
   * @return
   */
  private StringBuffer expandConstructions(final String drl) {
    // display keys if requested
    if (showKeyword) {
      for (DSLMappingEntry entry : this.keywords) {
        System.out.println("keyword: " + entry.getMappingKey());
        System.out.println("         " + entry.getKeyPattern());
      }
    }
    if (showWhen) {
      for (DSLMappingEntry entry : this.condition) {
        System.out.println("when: " + entry.getMappingKey());
        System.out.println("      " + entry.getKeyPattern());
        //                System.out.println( "      " + entry.getValuePattern() );
      }
    }
    if (showThen) {
      for (DSLMappingEntry entry : this.consequence) {
        System.out.println("then: " + entry.getMappingKey());
        System.out.println("      " + entry.getKeyPattern());
      }
    }

    // parse and expand specific areas
    final Matcher m = finder.matcher(drl);
    final StringBuffer buf = new StringBuffer();
    int drlPos = 0;
    int linecount = 0;
    while (m.find()) {
      final StringBuilder expanded = new StringBuilder();

      int newPos = m.start();
      linecount += countNewlines(drl, drlPos, newPos);
      drlPos = newPos;

      String constr = m.group().trim();
      if (constr.startsWith("rule")) {
        String headerFragment = m.group(1);
        expanded.append(headerFragment); // adding rule header and attributes
        String lhsFragment = m.group(2);
        expanded.append(
            this.expandLHS(lhsFragment, linecount + countNewlines(drl, drlPos, m.start(2)) + 1));
        String thenFragment = m.group(3);
        expanded.append(thenFragment); // adding "then" header
        String rhsFragment =
            this.expandRHS(m.group(4), linecount + countNewlines(drl, drlPos, m.start(4)) + 1);
        expanded.append(rhsFragment);
        expanded.append(m.group(5)); // adding rule trailer

      } else if (constr.startsWith("query")) {
        String fragment = m.group(6);
        expanded.append(fragment); // adding query header and attributes
        String lhsFragment =
            this.expandLHS(m.group(7), linecount + countNewlines(drl, drlPos, m.start(7)) + 1);
        expanded.append(lhsFragment);
        expanded.append(m.group(8)); // adding query trailer

      } else {
        // strange behavior
        this.addError(new ExpanderException("Unable to expand statement: " + constr, 0));
      }
      m.appendReplacement(buf, Matcher.quoteReplacement(expanded.toString()));
    }
    m.appendTail(buf);
    return buf;
  }