Search in sources :

Example 6 with DrlParser

use of org.drools.compiler.compiler.DrlParser in project drools by kiegroup.

the class KnowledgeBuilderImpl method dslrReaderToPackageDescr.

private PackageDescr dslrReaderToPackageDescr(Resource resource, Reader dslrReader) throws DroolsParserException {
    boolean hasErrors;
    PackageDescr pkg;
    DrlParser parser = new DrlParser(configuration.getLanguageLevel());
    DefaultExpander expander = getDslExpander();
    try {
        if (expander == null) {
            expander = new DefaultExpander();
        }
        String str = expander.expand(dslrReader);
        if (expander.hasErrors()) {
            for (ExpanderException error : expander.getErrors()) {
                error.setResource(resource);
                addBuilderResult(error);
            }
        }
        pkg = parser.parse(resource, str);
        this.results.addAll(parser.getErrors());
        hasErrors = parser.hasErrors();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (dslrReader != null) {
            try {
                dslrReader.close();
            } catch (IOException e) {
            }
        }
    }
    return hasErrors ? null : pkg;
}
Also used : DrlParser(org.drools.compiler.compiler.DrlParser) ExpanderException(org.drools.compiler.lang.ExpanderException) IOException(java.io.IOException) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander)

Example 7 with DrlParser

use of org.drools.compiler.compiler.DrlParser in project drools by kiegroup.

the class KnowledgeBuilderImpl method generatedResourcesToPackageDescr.

private List<PackageDescr> generatedResourcesToPackageDescr(Resource resource, List<PMMLResource> resources) throws DroolsParserException {
    List<PackageDescr> pkgDescrs = new ArrayList<>();
    DrlParser parser = new DrlParser(configuration.getLanguageLevel());
    for (PMMLResource res : resources) {
        for (String key : res.getRules().keySet()) {
            String src = res.getRules().get(key);
            PackageDescr descr = null;
            descr = parser.parse(false, src);
            if (descr != null) {
                descr.setResource(resource);
                pkgDescrs.add(descr);
                dumpGeneratedRule(descr, key, src);
            } else {
                addBuilderResult(new ParserError(resource, "Parser returned a null Package", 0, 0));
            }
        }
    }
    return pkgDescrs;
}
Also used : PMMLResource(org.drools.compiler.compiler.PMMLResource) ParserError(org.drools.compiler.compiler.ParserError) ArrayList(java.util.ArrayList) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 8 with DrlParser

use of org.drools.compiler.compiler.DrlParser in project drools by kiegroup.

the class KnowledgeBuilderImpl method addPackageFromDrl.

/**
 * Load a rule package from DRL source using the supplied DSL configuration.
 *
 * @param source The source of the rules.
 * @param dsl    The source of the domain specific language configuration.
 * @throws DroolsParserException
 * @throws IOException
 */
public void addPackageFromDrl(final Reader source, final Reader dsl) throws DroolsParserException, IOException {
    this.resource = new ReaderResource(source, ResourceType.DSLR);
    final DrlParser parser = new DrlParser(configuration.getLanguageLevel());
    final PackageDescr pkg = parser.parse(source, dsl);
    this.results.addAll(parser.getErrors());
    if (!parser.hasErrors()) {
        addPackage(pkg);
    }
    this.resource = null;
}
Also used : DrlParser(org.drools.compiler.compiler.DrlParser) ReaderResource(org.drools.core.io.impl.ReaderResource) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 9 with DrlParser

use of org.drools.compiler.compiler.DrlParser in project drools by kiegroup.

the class RuleBuilderTest method testBuild.

@Test
public void testBuild() throws Exception {
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
    kBuilder.addPackage(new PackageDescr("org.drools"));
    InternalKnowledgePackage pkg = kBuilder.getPackage("org.drools");
    final PackageDescr pkgDescr = parser.parse(new InputStreamReader(getClass().getResourceAsStream("nestedConditionalElements.drl")));
    // just checking there is no parsing errors
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    pkg.addGlobal("results", List.class);
    final RuleDescr ruleDescr = pkgDescr.getRules().get(0);
    final String ruleClassName = "RuleClassName.java";
    ruleDescr.setClassName(ruleClassName);
    ruleDescr.addAttribute(new AttributeDescr("dialect", "java"));
    kBuilder.addPackage(pkgDescr);
    assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
    final RuleImpl rule = kBuilder.getPackage("org.drools.compiler").getRule("test nested CEs");
    assertEquals("There should be 2 rule level declarations", 2, rule.getDeclarations().size());
    // second GE should be a not
    final GroupElement not = (GroupElement) rule.getLhs().getChildren().get(1);
    assertTrue(not.isNot());
    // not has no outer declarations
    assertTrue(not.getOuterDeclarations().isEmpty());
    assertEquals(1, not.getInnerDeclarations().size());
    assertTrue(not.getInnerDeclarations().keySet().contains("$state"));
    // second not
    final GroupElement not2 = (GroupElement) ((GroupElement) not.getChildren().get(0)).getChildren().get(1);
    assertTrue(not2.isNot());
    // not has no outer declarations
    assertTrue(not2.getOuterDeclarations().isEmpty());
    assertEquals(1, not2.getInnerDeclarations().size());
    assertTrue(not2.getInnerDeclarations().keySet().contains("$likes"));
}
Also used : InputStreamReader(java.io.InputStreamReader) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) GroupElement(org.drools.core.rule.GroupElement) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) DrlParser(org.drools.compiler.compiler.DrlParser) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 10 with DrlParser

use of org.drools.compiler.compiler.DrlParser in project drools by kiegroup.

the class MVELConsequenceBuilderTest method testMVELDebugSymbols.

@Test
public void testMVELDebugSymbols() throws DroolsParserException {
    MVELDebugHandler.setDebugMode(true);
    try {
        final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
        final PackageDescr pkgDescr = parser.parse(new InputStreamReader(getClass().getResourceAsStream("mvel_rule.drl")));
        // just checking there is no parsing errors
        assertFalse(parser.getErrors().toString(), parser.hasErrors());
        InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools");
        final RuleDescr ruleDescr = pkgDescr.getRules().get(0);
        final KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
        DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
        Dialect dialect = dialectRegistry.getDialect("mvel");
        RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, dialect);
        RuleBuilder.build(context);
        assertTrue(context.getErrors().toString(), context.getErrors().isEmpty());
        final RuleImpl rule = context.getRule();
        MVELConsequence mvelCons = (MVELConsequence) rule.getConsequence();
        mvelCons.compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
        String s = DebugTools.decompile(mvelCons.getCompExpr());
        int fromIndex = 0;
        int count = 0;
        while ((fromIndex = s.indexOf("DEBUG_SYMBOL", fromIndex + 1)) > -1) {
            count++;
        }
        assertEquals(4, count);
    } finally {
        MVELDebugHandler.setDebugMode(false);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) MVELConsequence(org.drools.core.base.mvel.MVELConsequence) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Dialect(org.drools.compiler.compiler.Dialect) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Aggregations

DrlParser (org.drools.compiler.compiler.DrlParser)44 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)43 Test (org.junit.Test)32 InputStreamReader (java.io.InputStreamReader)10 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)9 StringReader (java.io.StringReader)8 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)6 ArrayList (java.util.ArrayList)5 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)5 ParserError (org.drools.compiler.compiler.ParserError)5 DrlDumper (org.drools.compiler.lang.DrlDumper)4 Reader (java.io.Reader)3 List (java.util.List)3 DroolsParserException (org.drools.compiler.compiler.DroolsParserException)3 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)3 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 Ignore (org.junit.Ignore)3 AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)2 MVELConsequence (org.drools.core.base.mvel.MVELConsequence)2