use of org.drools.compiler.lang.dsl.DSLMappingEntry in project drools by kiegroup.
the class DSLMappingFileTest 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("ATTRIBUTE \"{attr}\" IS IN \\[{list}\\]", entry.getMappingKey());
assertEquals("Attribute( {attr} in ({list}) )", entry.getMappingValue());
} catch (final IOException e) {
e.printStackTrace();
fail("Should not raise exception ");
}
}
use of org.drools.compiler.lang.dsl.DSLMappingEntry in project drools by kiegroup.
the class DSLMappingFileTest method testEnum.
@Test
public void testEnum() {
String file = "[when][]ATTRIBUTE {attr:ENUM:Attribute.value} 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());
System.out.println(entry.getValuePattern());
System.out.println(entry.getVariables());
assertEquals("ATTRIBUTE {attr:ENUM:Attribute.value} in {list}", entry.getMappingKey());
assertEquals("Attribute( {attr} in ({list}) )", entry.getMappingValue());
assertEquals("(?:(?<=^)|(?<=\\W))ATTRIBUTE\\s+(.*?)\\s+in\\s+(.*?)$", entry.getKeyPattern().toString());
} catch (final IOException e) {
e.printStackTrace();
fail("Should not raise exception ");
}
}
use of org.drools.compiler.lang.dsl.DSLMappingEntry in project drools-wb by kiegroup.
the class DSLExtension method getExtensions.
@Override
public List<ExtensionMapping<?>> getExtensions(Path path, String content) {
final DSLTokenizedMappingFile dslLoader = new DSLTokenizedMappingFile();
List<DSLSentence> actionSentences = new ArrayList<>();
List<DSLSentence> conditionSentences = new ArrayList<>();
try {
if (dslLoader.parseAndLoad(new StringReader(content))) {
for (DSLMappingEntry entry : dslLoader.getMapping().getEntries()) {
if (entry.getSection() == DSLMappingEntry.CONDITION) {
final DSLMappingEntry definition = entry;
final DSLSentence sentence = new DSLSentence();
sentence.setDrl(definition.getMappingValue());
sentence.setDefinition(definition.getMappingKey());
conditionSentences.add(sentence);
} else if (entry.getSection() == DSLMappingEntry.CONSEQUENCE) {
final DSLMappingEntry definition = entry;
final DSLSentence sentence = new DSLSentence();
sentence.setDrl(definition.getMappingValue());
sentence.setDefinition(definition.getMappingKey());
actionSentences.add(sentence);
}
}
}
} catch (IOException e) {
log.error(e.getMessage());
}
return Arrays.asList(new DSLMapping(DSLActionSentence.INSTANCE, actionSentences), new DSLMapping(DSLConditionSentence.INSTANCE, conditionSentences));
}
use of org.drools.compiler.lang.dsl.DSLMappingEntry in project drools by kiegroup.
the class DSLMappingEntryTest method makeExpander.
private DefaultExpander makeExpander(DSLMappingEntry... entries) {
DefaultExpander expander = new DefaultExpander();
DefaultDSLMapping mapping = new DefaultDSLMapping();
for (DSLMappingEntry entry : entries) {
mapping.addEntry(entry);
}
List<String> options = new ArrayList<String>();
options.add("result");
options.add("when");
options.add("steps");
mapping.setOptions(options);
expander.addDSLMapping(mapping);
return expander;
}
use of org.drools.compiler.lang.dsl.DSLMappingEntry in project drools by kiegroup.
the class DSLMappingEntryTest method testExpandSpaces.
@Test
public void testExpandSpaces() throws IOException {
DSLMappingEntry entry = this.setupEntry();
DefaultExpander ex = makeExpander(entry);
String[] strs = new String[] { "0_sp", " 1_sp", " 3_sp", "0_sp_1 ", "0_sp_3 ", "0_sp 1_sp 2_sp", " 3_sp 3_sp 1_sp 1_sp_2 " };
StringBuilder sb = new StringBuilder("rule x\n" + "when\n");
for (String str : strs) {
sb.append("String is \"" + str + "\"" + NL);
}
sb.append("then\n" + "end\n");
String dslr = sb.toString();
String drl = ex.expand(dslr);
for (String str : strs) {
assertTrue(drl.contains('"' + str + '"'));
}
}
Aggregations