Search in sources :

Example 11 with Dialect

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

the class KnowledgeBuilderImpl method addFunction.

private void addFunction(final FunctionDescr functionDescr, PackageRegistry pkgRegistry) {
    Dialect dialect = pkgRegistry.getDialectCompiletimeRegistry().getDialect(functionDescr.getDialect());
    dialect.addFunction(functionDescr, pkgRegistry.getTypeResolver(), this.resource);
}
Also used : Dialect(org.drools.compiler.compiler.Dialect)

Example 12 with Dialect

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

the class DroolsJaxbHelperProviderImpl method addXsdModel.

public static String[] addXsdModel(Resource resource, KnowledgeBuilderImpl kBuilder, Options xjcOpts, String systemId) throws IOException {
    InputSource source = new InputSource(new CachingRewindableReader(resource.getReader()));
    source.setSystemId(systemId.trim().startsWith(".") ? systemId : "." + systemId);
    xjcOpts.addGrammar(source);
    try {
        xjcOpts.parseArguments(new String[] { "-npa" });
    } catch (BadCommandLineException e) {
        throw new IllegalArgumentException("Unable to parse arguments", e);
    }
    ErrorReceiver errorReceiver = new JaxbErrorReceiver4Drools();
    Model model = ModelLoader.load(xjcOpts, new JCodeModel(), errorReceiver);
    model.generateCode(xjcOpts, errorReceiver);
    MapVfsCodeWriter codeWriter = new MapVfsCodeWriter();
    model.codeModel.build(xjcOpts.createCodeWriter(codeWriter));
    MemoryResourceReader src = new MemoryResourceReader();
    boolean useProjectClassLoader = kBuilder.getRootClassLoader() instanceof ProjectClassLoader;
    List<String> classNames = new ArrayList<String>();
    List<String> srcNames = new ArrayList<String>();
    for (Entry<String, byte[]> entry : codeWriter.getMap().entrySet()) {
        String name = entry.getKey();
        int dotPos = name.lastIndexOf('.');
        String pkgName = name.substring(0, dotPos);
        if (!name.endsWith("package-info.java")) {
            classNames.add(pkgName);
        }
        dotPos = pkgName.lastIndexOf('.');
        if (dotPos != -1) {
            pkgName = pkgName.substring(0, dotPos);
        }
        PackageRegistry pkgReg = kBuilder.getPackageRegistry(pkgName);
        if (pkgReg == null) {
            kBuilder.addPackage(new PackageDescr(pkgName));
            pkgReg = kBuilder.getPackageRegistry(pkgName);
        }
        if (useProjectClassLoader) {
            String srcName = convertToResource(entry.getKey());
            src.add(srcName, entry.getValue());
            srcNames.add(srcName);
        } else {
            Dialect dialect = pkgReg.getDialectCompiletimeRegistry().getDialect("java");
            dialect.addSrc(convertToResource(entry.getKey()), entry.getValue());
        }
    }
    if (useProjectClassLoader) {
        ProjectJavaCompiler compiler = new ProjectJavaCompiler(kBuilder.getBuilderConfiguration());
        List<KnowledgeBuilderResult> results = compiler.compileAll((ProjectClassLoader) kBuilder.getRootClassLoader(), srcNames, src);
        for (String className : classNames) {
            Class<?> clazz = null;
            try {
                clazz = Class.forName(className, true, kBuilder.getRootClassLoader());
            } catch (ClassNotFoundException e) {
                continue;
            }
            String pkgName = className.substring(0, className.lastIndexOf('.'));
            PackageRegistry pkgReg = kBuilder.getPackageRegistry(pkgName);
            pkgReg.getPackage().addTypeDeclaration(TypeDeclaration.createTypeDeclarationForBean(clazz));
        }
        kBuilder.updateResults(results);
    } else {
        kBuilder.compileAll();
        kBuilder.updateResults();
    }
    return classNames.toArray(new String[classNames.size()]);
}
Also used : InputSource(org.xml.sax.InputSource) ArrayList(java.util.ArrayList) ErrorReceiver(com.sun.tools.xjc.ErrorReceiver) MemoryResourceReader(org.kie.memorycompiler.resources.MemoryResourceReader) Dialect(org.drools.compiler.compiler.Dialect) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) BadCommandLineException(com.sun.tools.xjc.BadCommandLineException) ProjectJavaCompiler(org.drools.compiler.compiler.ProjectJavaCompiler) JCodeModel(com.sun.codemodel.JCodeModel) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) Model(com.sun.tools.xjc.model.Model) JCodeModel(com.sun.codemodel.JCodeModel) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult)

Example 13 with Dialect

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

the class PatternBuilder method createAndBuildPredicate.

protected Constraint createAndBuildPredicate(RuleBuildContext context, Pattern pattern, BaseDescr base, String expr, Map<String, OperatorDescr> aliases) {
    Dialect dialect = context.getDialect();
    context.setDialect(context.getDialect("mvel"));
    PredicateDescr pdescr = new PredicateDescr(context.getRuleDescr().getResource(), expr);
    pdescr.copyParameters(base);
    pdescr.copyLocation(base);
    Constraint evalConstraint = buildEval(context, pattern, pdescr, aliases, expr, false);
    // fall back to original dialect
    context.setDialect(dialect);
    return evalConstraint;
}
Also used : XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) Dialect(org.drools.compiler.compiler.Dialect) PredicateDescr(org.drools.drl.ast.descr.PredicateDescr)

Example 14 with Dialect

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

the class MVELConstraintBuilder method buildMvelFieldReadAccessor.

@Override
public InternalReadAccessor buildMvelFieldReadAccessor(RuleBuildContext context, BaseDescr descr, Pattern pattern, ObjectType objectType, String fieldName, boolean reportError) {
    InternalReadAccessor reader;
    Dialect dialect = context.getDialect();
    try {
        MVELDialect mvelDialect = (MVELDialect) context.getDialect("mvel");
        context.setDialect(mvelDialect);
        final AnalysisResult analysis = context.getDialect().analyzeExpression(context, descr, fieldName, new BoundIdentifiers(pattern, context, Collections.EMPTY_MAP, objectType));
        if (analysis == null) {
            // something bad happened
            if (reportError) {
                registerDescrBuildError(context, descr, "Unable to analyze expression '" + fieldName + "'");
            }
            return null;
        }
        final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
        if (!usedIdentifiers.getGlobals().isEmpty()) {
            // cannot create a read accessors here when using globals
            return null;
        }
        if (!usedIdentifiers.getDeclrClasses().isEmpty()) {
            if (reportError && descr instanceof BindingDescr) {
                registerDescrBuildError(context, descr, "Variables can not be used inside bindings. Variable " + usedIdentifiers.getDeclrClasses().keySet() + " is being used in binding '" + fieldName + "'");
            }
            return null;
        }
        reader = ((MVELKnowledgePackageImpl) context.getPkg()).getClassFieldAccessorStore().getMVELReader(context.getPkg().getName(), objectType.getClassName(), fieldName, context.isTypesafe(), ((MVELAnalysisResult) analysis).getReturnType());
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
        ((MVELCompileable) reader).compile(data, context.getRule());
        data.addCompileable((MVELCompileable) reader);
    } catch (final Exception e) {
        if (reportError) {
            AsmUtil.copyErrorLocation(e, descr);
            registerDescrBuildError(context, descr, e, "Unable to create reader for '" + fieldName + "':" + e.getMessage());
        }
        // if there was an error, set the reader back to null
        reader = null;
    } finally {
        context.setDialect(dialect);
    }
    return reader;
}
Also used : BindingDescr(org.drools.drl.ast.descr.BindingDescr) MVELCompileable(org.drools.mvel.expr.MVELCompileable) MVELAnalysisResult(org.drools.mvel.builder.MVELAnalysisResult) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) MVELDialect(org.drools.mvel.builder.MVELDialect) Dialect(org.drools.compiler.compiler.Dialect) MVELDialect(org.drools.mvel.builder.MVELDialect) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) MVELAnalysisResult(org.drools.mvel.builder.MVELAnalysisResult) IOException(java.io.IOException) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers)

Example 15 with Dialect

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

the class MVELConstraintBuilder method buildCompilationUnit.

private 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.mvel.builder.MVELDialect) Dialect(org.drools.compiler.compiler.Dialect) PredicateDescr(org.drools.drl.ast.descr.PredicateDescr) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) MVELAnalysisResult(org.drools.mvel.builder.MVELAnalysisResult)

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