Search in sources :

Example 1 with Dialect

use of org.drools.compiler.compiler.Dialect 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)

Example 2 with Dialect

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

the class MVELConstraintBuilder method buildCompilationUnit.

public MVELCompilationUnit buildCompilationUnit(RuleBuildContext context, Pattern pattern, String expression, Map<String, OperatorDescr> aliases) {
    Dialect dialect = context.getDialect();
    context.setDialect(context.getDialect("mvel"));
    try {
        PredicateDescr predicateDescr = new PredicateDescr(context.getRuleDescr().getResource(), expression);
        AnalysisResult analysis = buildAnalysis(context, pattern, predicateDescr, aliases);
        if (analysis == null) {
            // something bad happened
            return null;
        }
        Declaration[][] usedDeclarations = getUsedDeclarations(context, pattern, analysis);
        return buildCompilationUnit(context, usedDeclarations[0], usedDeclarations[1], predicateDescr, analysis);
    } finally {
        context.setDialect(dialect);
    }
}
Also used : MVELDialect(org.drools.compiler.rule.builder.dialect.mvel.MVELDialect) Dialect(org.drools.compiler.compiler.Dialect) PredicateDescr(org.drools.compiler.lang.descr.PredicateDescr) MVELAnalysisResult(org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult) AnalysisResult(org.drools.compiler.compiler.AnalysisResult)

Example 3 with Dialect

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

the class KnowledgeBuilderTest method testCompilerConfiguration.

@Test
public void testCompilerConfiguration() throws Exception {
    // test default is eclipse jdt core
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    PackageDescr pkgDescr = new PackageDescr("org.drools.compiler.test");
    builder.addPackage(pkgDescr);
    DialectCompiletimeRegistry reg = builder.getPackageRegistry(pkgDescr.getName()).getDialectCompiletimeRegistry();
    final Field dialectField = builder.getClass().getDeclaredField("defaultDialect");
    dialectField.setAccessible(true);
    String dialectName = (String) dialectField.get(builder);
    reg = builder.getPackageRegistry(pkgDescr.getName()).getDialectCompiletimeRegistry();
    Dialect dialect = reg.getDialect(dialectName);
    final Field compilerField = dialect.getClass().getDeclaredField("compiler");
    compilerField.setAccessible(true);
    JavaCompiler compiler = (JavaCompiler) compilerField.get(dialect);
    KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
    JavaDialectConfiguration javaConf = (JavaDialectConfiguration) conf.getDialectConfiguration("java");
    switch(javaConf.getCompiler()) {
        case NATIVE:
            assertSame(NativeJavaCompiler.class, compiler.getClass());
            break;
        case ECLIPSE:
            assertSame(EclipseJavaCompiler.class, compiler.getClass());
            break;
        case JANINO:
            assertSame(JaninoJavaCompiler.class, compiler.getClass());
            break;
        default:
            fail("Unrecognized java compiler");
    }
    // test JANINO with property settings
    conf = new KnowledgeBuilderConfigurationImpl();
    javaConf = (JavaDialectConfiguration) conf.getDialectConfiguration("java");
    javaConf.setCompiler(JavaDialectConfiguration.CompilerType.JANINO);
    builder = new KnowledgeBuilderImpl(conf);
    builder.addPackage(pkgDescr);
    dialectName = (String) dialectField.get(builder);
    reg = builder.getPackageRegistry(pkgDescr.getName()).getDialectCompiletimeRegistry();
    dialect = reg.getDialect(dialectName);
    compiler = (JavaCompiler) compilerField.get(dialect);
    assertSame(JaninoJavaCompiler.class, compiler.getClass());
    // test eclipse jdt core with property settings and default source level
    conf = new KnowledgeBuilderConfigurationImpl();
    javaConf = (JavaDialectConfiguration) conf.getDialectConfiguration("java");
    javaConf.setCompiler(JavaDialectConfiguration.CompilerType.ECLIPSE);
    builder = new KnowledgeBuilderImpl(conf);
    builder.addPackage(pkgDescr);
    dialectName = (String) dialectField.get(builder);
    reg = builder.getPackageRegistry(pkgDescr.getName()).getDialectCompiletimeRegistry();
    dialect = reg.getDialect(dialectName);
    compiler = (JavaCompiler) compilerField.get(dialect);
    assertSame(EclipseJavaCompiler.class, compiler.getClass());
}
Also used : FactField(org.kie.api.definition.type.FactField) Field(java.lang.reflect.Field) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) Dialect(org.drools.compiler.compiler.Dialect) NativeJavaCompiler(org.drools.compiler.commons.jci.compilers.NativeJavaCompiler) EclipseJavaCompiler(org.drools.compiler.commons.jci.compilers.EclipseJavaCompiler) JaninoJavaCompiler(org.drools.compiler.commons.jci.compilers.JaninoJavaCompiler) JavaCompiler(org.drools.compiler.commons.jci.compilers.JavaCompiler) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) JavaDialectConfiguration(org.drools.compiler.rule.builder.dialect.java.JavaDialectConfiguration) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 4 with Dialect

use of org.drools.compiler.compiler.Dialect in project jbpm by kiegroup.

the class ProcessBuilderImpl method buildProcess.

public void buildProcess(final Process process, Resource resource) {
    if (resource != null) {
        ((org.jbpm.process.core.Process) process).setResource(resource);
    }
    boolean hasErrors = false;
    ProcessValidator validator = ProcessValidatorRegistry.getInstance().getValidator(process, resource);
    if (validator == null) {
        logger.warn("Could not find validator for process {}.", ((Process) process).getType());
        logger.warn("Continuing without validation of the process {} [{}]", process.getName(), process.getId());
    } else {
        ProcessValidationError[] errors = validator.validateProcess((WorkflowProcess) process);
        if (errors.length != 0) {
            hasErrors = true;
            for (int i = 0; i < errors.length; i++) {
                this.errors.add(new ParserError(resource, errors[i].toString(), -1, -1));
            }
        }
    }
    if (!hasErrors) {
        // generate and add rule for process
        String rules = "package " + process.getPackageName() + "\n";
        // NPE for validator
        if (validator != null && validator.compilationSupported()) {
            rules = generateRules(process);
        }
        try {
            knowledgeBuilder.addPackageFromDrl(new StringReader(rules), resource);
        } catch (IOException e) {
            // should never occur
            logger.error("IOException during addPackageFromDRL", e);
        } catch (DroolsParserException e) {
            // should never occur
            logger.error("DroolsParserException during addPackageFromDRL", e);
        }
        PackageRegistry pkgRegistry = this.knowledgeBuilder.getPackageRegistry(process.getPackageName());
        if (pkgRegistry != null) {
            InternalKnowledgePackage p = pkgRegistry.getPackage();
            if (p != null) {
                if (validator != null) {
                    // NPE for validator
                    if (validator.compilationSupported()) {
                        ProcessDescr processDescr = new ProcessDescr();
                        processDescr.setName(process.getPackageName() + "." + process.getName());
                        processDescr.setResource(resource);
                        processDescr.setProcessId(process.getId());
                        DialectCompiletimeRegistry dialectRegistry = pkgRegistry.getDialectCompiletimeRegistry();
                        Dialect dialect = dialectRegistry.getDialect("java");
                        dialect.init(processDescr);
                        ProcessBuildContext buildContext = new ProcessBuildContext(this.knowledgeBuilder, p, process, processDescr, dialectRegistry, dialect);
                        buildContexts((ContextContainer) process, buildContext);
                        if (process instanceof WorkflowProcess) {
                            buildNodes((WorkflowProcess) process, buildContext);
                        }
                    }
                    Process duplicateProcess = p.getRuleFlows().get(process.getId());
                    if (duplicateProcess != null) {
                        Resource duplicatedResource = duplicateProcess.getResource();
                        if (resource == null || duplicatedResource == null || duplicatedResource.getSourcePath() == null || duplicatedResource.getSourcePath().equals(resource.getSourcePath())) {
                            this.errors.add(new DuplicateProcess(process, this.knowledgeBuilder.getBuilderConfiguration()));
                        } else {
                            this.errors.add(new ParserError(resource, "Process with same id already exists: " + process.getId(), -1, -1));
                        }
                    }
                    p.addProcess(process);
                    // NPE for validator
                    if (validator.compilationSupported()) {
                        pkgRegistry.compileAll();
                        pkgRegistry.getDialectRuntimeRegistry().onBeforeExecute();
                    }
                }
            }
        } else {
            // name of the process
            throw new RuntimeException("invalid package name");
        }
    }
}
Also used : ParserError(org.drools.compiler.compiler.ParserError) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) Resource(org.kie.api.io.Resource) DuplicateProcess(org.drools.compiler.compiler.DuplicateProcess) Process(org.kie.api.definition.process.Process) WorkflowProcess(org.kie.api.definition.process.WorkflowProcess) ProcessDescr(org.drools.compiler.lang.descr.ProcessDescr) IOException(java.io.IOException) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) Constraint(org.jbpm.workflow.core.Constraint) ProcessValidationError(org.jbpm.process.core.validation.ProcessValidationError) DuplicateProcess(org.drools.compiler.compiler.DuplicateProcess) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) StringReader(java.io.StringReader) ProcessDialect(org.jbpm.process.builder.dialect.ProcessDialect) JavaDialect(org.drools.compiler.rule.builder.dialect.java.JavaDialect) Dialect(org.drools.compiler.compiler.Dialect) ProcessBuildContext(org.jbpm.process.builder.ProcessBuildContext) WorkflowProcess(org.kie.api.definition.process.WorkflowProcess) ProcessValidator(org.jbpm.process.core.validation.ProcessValidator) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 5 with Dialect

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

the class KnowledgeBuilderTest method testCompilerConfiguration.

@Test
public void testCompilerConfiguration() throws Exception {
    // test default is eclipse jdt core
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    PackageDescr pkgDescr = new PackageDescr("org.drools.mvel.compiler.test");
    builder.addPackage(pkgDescr);
    DialectCompiletimeRegistry reg = builder.getPackageRegistry(pkgDescr.getName()).getDialectCompiletimeRegistry();
    final Field dialectField = builder.getClass().getDeclaredField("defaultDialect");
    dialectField.setAccessible(true);
    String dialectName = (String) dialectField.get(builder);
    reg = builder.getPackageRegistry(pkgDescr.getName()).getDialectCompiletimeRegistry();
    Dialect dialect = reg.getDialect(dialectName);
    final Field compilerField = dialect.getClass().getDeclaredField("compiler");
    compilerField.setAccessible(true);
    JavaCompiler compiler = (JavaCompiler) compilerField.get(dialect);
    KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
    JavaForMvelDialectConfiguration javaConf = (JavaForMvelDialectConfiguration) conf.getDialectConfiguration("java");
    switch(javaConf.getCompiler()) {
        case NATIVE:
            assertSame(NativeJavaCompiler.class, compiler.getClass());
            break;
        case ECLIPSE:
            assertSame(EclipseJavaCompiler.class, compiler.getClass());
            break;
        default:
            fail("Unrecognized java compiler");
    }
    // test eclipse jdt core with property settings and default source level
    conf = new KnowledgeBuilderConfigurationImpl();
    javaConf = (JavaForMvelDialectConfiguration) conf.getDialectConfiguration("java");
    javaConf.setCompiler(JavaForMvelDialectConfiguration.CompilerType.ECLIPSE);
    builder = new KnowledgeBuilderImpl(conf);
    builder.addPackage(pkgDescr);
    dialectName = (String) dialectField.get(builder);
    reg = builder.getPackageRegistry(pkgDescr.getName()).getDialectCompiletimeRegistry();
    dialect = reg.getDialect(dialectName);
    compiler = (JavaCompiler) compilerField.get(dialect);
    assertSame(EclipseJavaCompiler.class, compiler.getClass());
}
Also used : FactField(org.kie.api.definition.type.FactField) Field(java.lang.reflect.Field) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) Dialect(org.drools.compiler.compiler.Dialect) NativeJavaCompiler(org.kie.memorycompiler.jdknative.NativeJavaCompiler) JavaCompiler(org.kie.memorycompiler.JavaCompiler) EclipseJavaCompiler(org.drools.ecj.EclipseJavaCompiler) JavaForMvelDialectConfiguration(org.drools.mvel.java.JavaForMvelDialectConfiguration) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Aggregations

Dialect (org.drools.compiler.compiler.Dialect)17 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)8 Test (org.junit.Test)6 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)5 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)5 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)4 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)4 PackageDescr (org.drools.drl.ast.descr.PackageDescr)4 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)3 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)3 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)3 MVELDialect (org.drools.mvel.builder.MVELDialect)3 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 Field (java.lang.reflect.Field)2 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)2 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)2 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)2 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)2 Accumulate (org.drools.core.rule.Accumulate)2