Search in sources :

Example 21 with DSLTokenizedMappingFile

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

the class DSLMappingFileTest 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 ");
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile) Test(org.junit.Test)

Example 22 with DSLTokenizedMappingFile

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

the class DSLMappingFileTest method testParseFileWithEscapes.

@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 ");
    }
}
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 23 with DSLTokenizedMappingFile

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

the class GuidedRuleDslrFileIndexer 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 Path nioPackagePath = Paths.convert(packagePath);
    final Collection<Path> dslPaths = fileDiscoveryService.discoverFiles(nioPackagePath, FILTER_DSLS);
    for (final 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) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) IOException(java.io.IOException) DSLMappingFile(org.drools.compiler.lang.dsl.DSLMappingFile) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile)

Example 24 with DSLTokenizedMappingFile

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

the class DslrFileIndexer 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 25 with DSLTokenizedMappingFile

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

the class DslFileIndexer method fillIndexBuilder.

@Override
public DefaultIndexBuilder fillIndexBuilder(final Path path) throws Exception {
    final List<String> lhs = new ArrayList<String>();
    final List<String> rhs = new ArrayList<String>();
    final String dsl = ioService.readAllString(path);
    // Construct a dummy DRL file to parse index elements
    final DSLTokenizedMappingFile dslLoader = new DSLTokenizedMappingFile();
    if (dslLoader.parseAndLoad(new StringReader(dsl))) {
        DSLMapping dslMapping = dslLoader.getMapping();
        for (DSLMappingEntry e : dslMapping.getEntries()) {
            switch(e.getSection()) {
                case CONDITION:
                    lhs.add(e.getValuePattern());
                    break;
                case CONSEQUENCE:
                    rhs.add(e.getValuePattern());
                    break;
                default:
            }
        }
        final String drl = makeDrl(path, lhs, rhs);
        return fillDrlIndexBuilder(path, drl);
    }
    return null;
}
Also used : DSLMapping(org.drools.compiler.lang.dsl.DSLMapping) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) DSLMappingEntry(org.drools.compiler.lang.dsl.DSLMappingEntry) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile)

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