Search in sources :

Example 26 with DrlParser

use of org.drools.drl.parser.DrlParser in project drools by kiegroup.

the class DumperTestHelper method dump.

public static String dump(String filename) throws Exception {
    DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
    final PackageDescr pkgOriginal = parser.parse(new InputStreamReader(DumperTestHelper.class.getResourceAsStream(filename)));
    final DrlDumper dumper = new DrlDumper();
    return dumper.dump(pkgOriginal);
}
Also used : InputStreamReader(java.io.InputStreamReader) DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper)

Example 27 with DrlParser

use of org.drools.drl.parser.DrlParser in project drools by kiegroup.

the class DumperTestHelper method DrlFile.

public static void DrlFile(String filename) throws Exception {
    DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    final PackageDescr pkgOriginal = parser.parse(new InputStreamReader(DumperTestHelper.class.getResourceAsStream(filename)));
    final DrlDumper dumper = new DrlDumper();
    String result1 = dumper.dump(pkgOriginal);
    final PackageDescr pkgDerivated = parser.parse(new StringReader(result1));
    String result2 = dumper.dump(pkgDerivated);
    System.out.println(result1);
    Assertions.assertThat(result1).isEqualToIgnoringWhitespace(result2);
}
Also used : InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper)

Example 28 with DrlParser

use of org.drools.drl.parser.DrlParser in project drools by kiegroup.

the class DumperTest method checkRoundtrip.

private void checkRoundtrip(String drl) throws DroolsParserException {
    DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
    final PackageDescr pkgOriginal = parser.parse(false, drl);
    final DrlDumper dumper = new DrlDumper();
    String out = dumper.dump(pkgOriginal);
    Assertions.assertThat(drl).isEqualToIgnoringWhitespace(out);
}
Also used : DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper)

Example 29 with DrlParser

use of org.drools.drl.parser.DrlParser 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.drl.parser.lang.dsl.DefaultExpanderResolver) DefaultExpander(org.drools.drl.parser.lang.dsl.DefaultExpander) Expander(org.drools.drl.parser.lang.Expander) StringReader(java.io.StringReader) DrlParser(org.drools.drl.parser.DrlParser) DSLMappingFile(org.drools.drl.parser.lang.dsl.DSLMappingFile) DSLTokenizedMappingFile(org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile) DefaultExpander(org.drools.drl.parser.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 30 with DrlParser

use of org.drools.drl.parser.DrlParser in project drools by kiegroup.

the class DrlParserTest method testExpandDRL.

@Test
public void testExpandDRL() 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";
    DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    String result = parser.getExpandedDRL(drl, new StringReader(dsl));
    Assertions.assertThat("rule 'foo' " + NL + " when " + NL + " Something() " + NL + " then " + NL + " another(); " + NL + "end").isEqualToIgnoringWhitespace(result);
}
Also used : StringReader(java.io.StringReader) DrlParser(org.drools.drl.parser.DrlParser) Test(org.junit.Test)

Aggregations

DrlParser (org.drools.drl.parser.DrlParser)41 PackageDescr (org.drools.drl.ast.descr.PackageDescr)38 Test (org.junit.Test)32 RuleDescr (org.drools.drl.ast.descr.RuleDescr)9 InputStreamReader (java.io.InputStreamReader)8 StringReader (java.io.StringReader)8 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 ParserError (org.drools.drl.parser.ParserError)4 DrlDumper (org.drools.mvel.DrlDumper)4 Reader (java.io.Reader)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)3 PatternDescr (org.drools.drl.ast.descr.PatternDescr)3 Ignore (org.junit.Ignore)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)2 InputStreamResource (org.drools.core.io.impl.InputStreamResource)2 AttributeDescr (org.drools.drl.ast.descr.AttributeDescr)2 MVELConsequence (org.drools.mvel.expr.MVELConsequence)2