Search in sources :

Example 6 with DSLTokenizedMappingFile

use of org.drools.compiler.lang.dsl.DSLTokenizedMappingFile in project drools by kiegroup.

the class DSLMappingFileTest method testNoRHS.

/**
 * Right now this test fails because there is no RHS for the rule. It connects the "then" and "end" to "thenend".
 */
@Test
public void testNoRHS() {
    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 + "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 + "end";
        assertEquals(expected, ruleAfterExpansion);
    } catch (final IOException e) {
        e.printStackTrace();
        fail("Should not raise exception ");
    }
}
Also used : StringReader(java.io.StringReader) StringReader(java.io.StringReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 7 with DSLTokenizedMappingFile

use of org.drools.compiler.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);
}
Also used : DefaultExpanderResolver(org.drools.compiler.lang.dsl.DefaultExpanderResolver) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) Expander(org.drools.compiler.lang.Expander) StringReader(java.io.StringReader) DSLMappingFile(org.drools.compiler.lang.dsl.DSLMappingFile) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 8 with DSLTokenizedMappingFile

use of org.drools.compiler.lang.dsl.DSLTokenizedMappingFile in project drools by kiegroup.

the class RuleModelDRLPersistenceUnmarshallingTest method testDSLExpansionLHS_WithKeyword_then.

@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1173842
void testDSLExpansionLHS_WithKeyword_then() {
    String expected_dslr = "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "There is an Applicant\n" + "- age more then 55\n" + "then\n" + "end\n";
    String expected_drl = "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "Applicant( age > 55 )\n" + "then\n" + "end\n";
    final String dslDefinition1 = "There is an Applicant";
    final String dslFile1 = "[when]" + dslDefinition1 + "=Applicant( )";
    final String dslDefinition2 = "- age more then {age}";
    final String dslFile2 = "[when]" + dslDefinition2 + "=age > {age}";
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(expected_dslr, Collections.emptyList(), dmo, dslFile1, dslFile2);
    assertNotNull(m);
    assertEquals(2, m.lhs.length);
    assertTrue(m.lhs[0] instanceof DSLSentence);
    assertTrue(m.lhs[1] instanceof DSLSentence);
    DSLSentence dslSentence1 = (DSLSentence) m.lhs[0];
    assertEquals(dslDefinition1, dslSentence1.getDefinition());
    assertEquals(0, dslSentence1.getValues().size());
    DSLSentence dslSentence2 = (DSLSentence) m.lhs[1];
    assertEquals(dslDefinition2, dslSentence2.getDefinition());
    assertEquals(1, dslSentence2.getValues().size());
    DSLVariableValue dslVariableValue = dslSentence2.getValues().get(0);
    assertEquals("55", dslVariableValue.getValue());
    // Check round-trip
    assertEqualsIgnoreWhitespace(expected_dslr, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
    // Check DSL expansion (as BZ stated runtime was flawed as well)
    final Expander expander = new DefaultExpander();
    final List<DSLMappingFile> dsls = new ArrayList<>();
    try {
        final DSLTokenizedMappingFile dslTokenizer1 = new DSLTokenizedMappingFile();
        if (dslTokenizer1.parseAndLoad(new StringReader(dslFile1))) {
            dsls.add(dslTokenizer1);
        } else {
            fail();
        }
        final DSLTokenizedMappingFile dslTokenizer2 = new DSLTokenizedMappingFile();
        if (dslTokenizer2.parseAndLoad(new StringReader(dslFile2))) {
            dsls.add(dslTokenizer2);
        } else {
            fail();
        }
    } catch (IOException e) {
        fail();
    }
    for (DSLMappingFile dsl : dsls) {
        expander.addDSLMapping(dsl.getMapping());
    }
    final String actual_drl = expander.expand(expected_dslr);
    assertEqualsIgnoreWhitespace(expected_drl, actual_drl);
}
Also used : Expander(org.drools.compiler.lang.Expander) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) DSLVariableValue(org.drools.workbench.models.datamodel.rule.DSLVariableValue) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) IOException(java.io.IOException) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) DSLMappingFile(org.drools.compiler.lang.dsl.DSLMappingFile) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile) Test(org.junit.Test)

Example 9 with DSLTokenizedMappingFile

use of org.drools.compiler.lang.dsl.DSLTokenizedMappingFile in project drools-wb by kiegroup.

the class GuidedDecisionTableSourceService method getDSLMappingFiles.

private List<DSLMappingFile> getDSLMappingFiles(final Path path) {
    final List<DSLMappingFile> dsls = new ArrayList<DSLMappingFile>();
    final org.uberfire.backend.vfs.Path vfsPath = Paths.convert(path);
    final org.uberfire.backend.vfs.Path packagePath = moduleService.resolvePackage(vfsPath).getPackageMainResourcesPath();
    final org.uberfire.java.nio.file.Path nioPackagePath = Paths.convert(packagePath);
    final Collection<Path> dslPaths = fileDiscoveryService.discoverFiles(nioPackagePath, FILTER_DSLS);
    for (final org.uberfire.java.nio.file.Path dslPath : dslPaths) {
        final String dslDefinition = ioService.readAllString(dslPath);
        final DSLTokenizedMappingFile dslFile = new DSLTokenizedMappingFile();
        try {
            if (dslFile.parseAndLoad(new StringReader(dslDefinition))) {
                dsls.add(dslFile);
            } else {
                logger.error("Unable to parse DSL definition: " + dslDefinition);
            }
        } catch (IOException ioe) {
            logger.error(ioe.getMessage());
        }
    }
    return dsls;
}
Also used : Path(org.uberfire.java.nio.file.Path) Path(org.uberfire.java.nio.file.Path) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DSLMappingFile(org.drools.compiler.lang.dsl.DSLMappingFile) StringReader(java.io.StringReader) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile)

Example 10 with DSLTokenizedMappingFile

use of org.drools.compiler.lang.dsl.DSLTokenizedMappingFile 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));
}
Also used : ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) DSLMappingEntry(org.drools.compiler.lang.dsl.DSLMappingEntry) IOException(java.io.IOException) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence)

Aggregations

DSLTokenizedMappingFile (org.drools.compiler.lang.dsl.DSLTokenizedMappingFile)25 StringReader (java.io.StringReader)24 IOException (java.io.IOException)20 Test (org.junit.Test)15 Reader (java.io.Reader)14 InputStreamReader (java.io.InputStreamReader)13 DSLMappingEntry (org.drools.compiler.lang.dsl.DSLMappingEntry)11 ArrayList (java.util.ArrayList)9 DSLMappingFile (org.drools.compiler.lang.dsl.DSLMappingFile)7 DefaultExpander (org.drools.compiler.lang.dsl.DefaultExpander)7 Path (org.uberfire.java.nio.file.Path)5 Expander (org.drools.compiler.lang.Expander)2 DSLSentence (org.drools.workbench.models.datamodel.rule.DSLSentence)2 Iterator (java.util.Iterator)1 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)1 DSLMapping (org.drools.compiler.lang.dsl.DSLMapping)1 DefaultExpanderResolver (org.drools.compiler.lang.dsl.DefaultExpanderResolver)1 XmlChangeSetReader (org.drools.core.xml.XmlChangeSetReader)1 DSLVariableValue (org.drools.workbench.models.datamodel.rule.DSLVariableValue)1 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)1