Search in sources :

Example 21 with DrlParser

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

the class KnowledgeBuilderImpl method generatedDrlToPackageDescr.

private PackageDescr generatedDrlToPackageDescr(Resource resource, String generatedDrl) throws DroolsParserException {
    // dump the generated DRL if the dump dir was configured
    if (this.configuration.getDumpDir() != null) {
        dumpDrlGeneratedFromDTable(this.configuration.getDumpDir(), generatedDrl, resource.getSourcePath());
    }
    DrlParser parser = new DrlParser(configuration.getLanguageLevel());
    PackageDescr pkg = parser.parse(resource, new StringReader(generatedDrl));
    this.results.addAll(parser.getErrors());
    if (pkg == null) {
        addBuilderResult(new ParserError(resource, "Parser returned a null Package", 0, 0));
    } else {
        pkg.setResource(resource);
    }
    return parser.hasErrors() ? null : pkg;
}
Also used : ParserError(org.drools.drl.parser.ParserError) StringReader(java.io.StringReader) DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 22 with DrlParser

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

the class KnowledgeBuilderImpl method drlToPackageDescr.

PackageDescr drlToPackageDescr(Resource resource) throws DroolsParserException, IOException {
    PackageDescr pkg;
    boolean hasErrors = false;
    if (resource instanceof DescrResource) {
        pkg = (PackageDescr) ((DescrResource) resource).getDescr();
    } else {
        final DrlParser parser = new DrlParser(configuration.getLanguageLevel());
        pkg = parser.parse(resource);
        this.results.addAll(parser.getErrors());
        if (pkg == null) {
            addBuilderResult(new ParserError(resource, "Parser returned a null Package", 0, 0));
        }
        hasErrors = parser.hasErrors();
    }
    if (pkg != null) {
        pkg.setResource(resource);
    }
    return hasErrors ? null : pkg;
}
Also used : ParserError(org.drools.drl.parser.ParserError) DescrResource(org.drools.core.io.impl.DescrResource) DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 23 with DrlParser

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

the class MVELDebugTest method testDebug.

@Test
public void testDebug() throws Exception {
    String rule = "package com.sample; dialect \"mvel\" rule myRule when then\n System.out.println( \"test\" ); end";
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    PackageDescr packageDescr = parser.parse(null, rule);
    RuleDescr ruleDescr = packageDescr.getRules().get(0);
    builder = new KnowledgeBuilderImpl();
    builder.addPackage(packageDescr);
    InternalKnowledgePackage pkg = builder.getPackage("com.sample");
    MVELConsequence consequence = (MVELConsequence) pkg.getRule("myRule").getConsequence();
    String ruleName = ruleDescr.getNamespace() + "." + ruleDescr.getClassName();
    System.out.println(ruleName);
    assertEquals("com.sample.Rule_myRule", ruleName);
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) DrlParser(org.drools.drl.parser.DrlParser) MVELConsequence(org.drools.mvel.expr.MVELConsequence) PackageDescr(org.drools.drl.ast.descr.PackageDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 24 with DrlParser

use of org.drools.drl.parser.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 = CoreComponentFactory.get().createKnowledgePackage("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.mvel.expr.MVELConsequence) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) MVELDialect(org.drools.mvel.builder.MVELDialect) Dialect(org.drools.compiler.compiler.Dialect) DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 25 with DrlParser

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

the class Misc2Test method testEvalConstraintWithMvelOperator.

@Test
public void testEvalConstraintWithMvelOperator() {
    String drl = "rule \"yeah\" " + "\tdialect \"mvel\"\n when " + "Foo( eval( field soundslike \"water\" ) )" + " then " + "end";
    DrlParser drlParser = new DrlParser();
    PackageDescr packageDescr;
    try {
        packageDescr = drlParser.parse(true, drl);
    } catch (DroolsParserException e) {
        throw new RuntimeException(e);
    }
    RuleDescr r = packageDescr.getRules().get(0);
    PatternDescr pd = (PatternDescr) r.getLhs().getDescrs().get(0);
    assertEquals(1, pd.getConstraint().getDescrs().size());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) DrlParser(org.drools.drl.parser.DrlParser) FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DroolsParserException(org.drools.drl.parser.DroolsParserException) 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