Пример #1
0
  @Test
  public void testParseFileWithEscapes() {
    String file =
        "[then]TEST=System.out.println(\"DO_SOMETHING\");"
            + NL
            + ""
            + "[when]code {code1} occurs and sum of all digit not equal \\( {code2} \\+ {code3} \\)=AAAA( cd1 == {code1}, cd2 != ( {code2} + {code3} ))"
            + NL
            + ""
            + "[when]code {code1} occurs=BBBB"
            + NL
            + "";
    try {
      final Reader reader = new StringReader(file);
      this.file = new DSLTokenizedMappingFile();

      final boolean parsingResult = this.file.parseAndLoad(reader);
      reader.close();

      assertTrue(this.file.getErrors().toString(), parsingResult);
      assertTrue(this.file.getErrors().isEmpty());

      final String LHS = "code 1041 occurs and sum of all digit not equal ( 1034 + 1035 )";
      final String rule =
          "rule \"x\"" + NL + "when" + NL + "" + LHS + "" + NL + "then" + NL + "TEST" + NL + "end";

      DefaultExpander de = new DefaultExpander();
      de.addDSLMapping(this.file.getMapping());

      final String ruleAfterExpansion = de.expand(rule);

      final String expected =
          "rule \"x\""
              + NL
              + "when"
              + NL
              + "AAAA( cd1 == 1041, cd2 != ( 1034 + 1035 ))"
              + NL
              + "then"
              + NL
              + "System.out.println(\"DO_SOMETHING\");"
              + NL
              + "end";

      assertEquals(expected, ruleAfterExpansion);

    } catch (final IOException e) {
      e.printStackTrace();
      fail("Should not raise exception ");
    }
  }
Пример #2
0
  @Test
  public void testParseFileWithEscaptedCurlyBrackets() {
    String file =
        "[consequence][$policy]Add surcharge {surcharge} to Policy=modify(policy) \\{price = {surcharge}\\}";
    try {
      final Reader reader = new StringReader(file);
      this.file = new DSLTokenizedMappingFile();

      final boolean parsingResult = this.file.parseAndLoad(reader);
      reader.close();

      assertTrue(this.file.getErrors().toString(), parsingResult);
      assertTrue(this.file.getErrors().isEmpty());

      assertEquals(1, this.file.getMapping().getEntries().size());

      DSLMappingEntry entry = (DSLMappingEntry) this.file.getMapping().getEntries().get(0);

      assertEquals(DSLMappingEntry.CONSEQUENCE, entry.getSection());
      assertEquals("$policy", entry.getMetaData().toString());
      assertEquals("Add surcharge {surcharge} to Policy", entry.getMappingKey());
      assertEquals("modify(policy) \\{price = {surcharge}\\}", entry.getMappingValue());

      String input =
          "rule x"
              + NL
              + "when"
              + NL
              + "then"
              + NL
              + "Add surcharge 300 to Policy"
              + NL
              + "end"
              + NL
              + "";
      String expected =
          "rule x"
              + NL
              + "when"
              + NL
              + "then"
              + NL
              + "modify(policy) {price = 300}"
              + NL
              + "end"
              + NL
              + "";

      DefaultExpander de = new DefaultExpander();
      de.addDSLMapping(this.file.getMapping());

      final String result = de.expand(input);

      //            String result = entry.getKeyPattern().matcher( input ).replaceAll(
      // entry.getValuePattern() );

      assertEquals(expected, result);

    } catch (final IOException e) {
      e.printStackTrace();
      fail("Should not raise exception ");
    }
  }