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);
}
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()]);
}
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;
}
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;
}
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);
}
}
Aggregations