use of org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile in project drools by kiegroup.
the class DSLTokenizedMappingFileTest method testParseFile.
@Test
public void testParseFile() {
try {
final Reader reader = new InputStreamReader(this.getClass().getResourceAsStream(this.filename));
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(31, this.file.getMapping().getEntries().size());
} catch (final IOException e) {
e.printStackTrace();
fail("Should not raise exception ");
}
}
use of org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile in project drools by kiegroup.
the class DSLTokenizedMappingFileTest method testParseFileWithBrackets.
@Test
public void testParseFileWithBrackets() {
String file = "[when]ATTRIBUTE \"{attr}\" IS IN [{list}]=Attribute( {attr} in ({list}) )";
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.CONDITION, entry.getSection());
assertEquals(DSLMappingEntry.EMPTY_METADATA, entry.getMetaData());
assertEquals(lookbehind + "ATTRIBUTE\\s+\"(.*?)\"\\s+IS\\s+IN\\s+[(.*?)](?=\\W|$)", entry.getKeyPattern().toString());
// Attribute( {attr} in ({list}) )
assertEquals("Attribute( {attr} in ({list}) )", entry.getValuePattern());
} catch (final IOException e) {
e.printStackTrace();
fail("Should not raise exception ");
}
}
use of org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile in project drools by kiegroup.
the class DSLTokenizedMappingFileTest method testParseFileWithEscaptedBrackets.
@Test
public void testParseFileWithEscaptedBrackets() {
String file = "[when]ATTRIBUTE \"{attr}\" IS IN \\[{list}\\]=Attribute( {attr} in ({list}) )";
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.CONDITION, entry.getSection());
assertEquals(DSLMappingEntry.EMPTY_METADATA, entry.getMetaData());
assertEquals(lookbehind + "ATTRIBUTE\\s+\"(.*?)\"\\s+IS\\s+IN\\s+\\[(.*?)\\](?=\\W|$)", entry.getKeyPattern().toString());
// Attribute( {attr} in ({list}) )
assertEquals("Attribute( {attr} in ({list}) )", entry.getValuePattern());
} catch (final IOException e) {
e.printStackTrace();
fail("Should not raise exception ");
}
}
use of org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile in project drools by kiegroup.
the class KnowledgeBuilderImpl method addDsl.
public void addDsl(Resource resource) throws IOException {
this.resource = resource;
DSLTokenizedMappingFile file = new DSLTokenizedMappingFile();
try (Reader reader = resource.getReader()) {
if (!file.parseAndLoad(reader)) {
this.results.addAll(file.getErrors());
}
if (this.dslFiles == null) {
this.dslFiles = new ArrayList<>();
}
this.dslFiles.add(file);
} finally {
this.resource = null;
}
}
use of org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile in project drools by kiegroup.
the class DrlParserTest method testExpandDRLUsingInjectedExpander.
@Test
public void testExpandDRLUsingInjectedExpander() throws Exception {
String dsl = "[condition]Something=Something()" + NL + "[then]another=another();";
String drl = "rule 'foo' " + NL + " when " + NL + " Something " + NL + " then " + NL + " another " + NL + "end";
DefaultExpanderResolver resolver = new DefaultExpanderResolver(new StringReader(dsl));
final DSLMappingFile file = new DSLTokenizedMappingFile();
if (file.parseAndLoad(new StringReader(dsl))) {
final Expander expander = new DefaultExpander();
expander.addDSLMapping(file.getMapping());
resolver.addExpander("*", expander);
} else {
throw new RuntimeException("Error parsing and loading DSL file." + file.getErrors());
}
DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
String result = parser.getExpandedDRL(drl, resolver);
Assertions.assertThat("rule 'foo' " + NL + " when " + NL + " Something() " + NL + " then " + NL + " another(); " + NL + "end").isEqualToIgnoringWhitespace(result);
}
Aggregations