use of org.drools.compiler.compiler.DescrBuildError in project drools by kiegroup.
the class MVELEvalBuilder method build.
/**
* Builds and returns an Eval Conditional Element
*
* @param context The current build context
* @param descr The Eval Descriptor to build the eval conditional element from
*
* @return the Eval Conditional Element
*/
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
boolean typesafe = context.isTypesafe();
// it must be an EvalDescr
final EvalDescr evalDescr = (EvalDescr) descr;
try {
MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
AnalysisResult analysis = context.getDialect().analyzeExpression(context, evalDescr, evalDescr.getContent(), new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
int i = usedIdentifiers.getDeclrClasses().keySet().size();
Declaration[] previousDeclarations = new Declaration[i];
i = 0;
for (String id : usedIdentifiers.getDeclrClasses().keySet()) {
previousDeclarations[i++] = decls.get(id);
}
Arrays.sort(previousDeclarations, SortDeclarations.instance);
MVELCompilationUnit unit = dialect.getMVELCompilationUnit((String) evalDescr.getContent(), analysis, previousDeclarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.EXPRESSION);
final EvalCondition eval = new EvalCondition(previousDeclarations);
MVELEvalExpression expr = new MVELEvalExpression(unit, dialect.getId());
eval.setEvalExpression(KiePolicyHelper.isPolicyEnabled() ? new SafeEvalExpression(expr) : expr);
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
data.addCompileable(eval, expr);
expr.compile(data, context.getRule());
return eval;
} catch (final Exception e) {
copyErrorLocation(e, evalDescr);
context.addError(new DescrBuildError(context.getParentDescr(), evalDescr, e, "Unable to build expression for 'eval':" + e.getMessage() + " '" + evalDescr.getContent() + "'"));
return null;
} finally {
context.setTypesafe(typesafe);
}
}
use of org.drools.compiler.compiler.DescrBuildError in project drools by kiegroup.
the class MVELFromBuilder method build.
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
String text = ((FromDescr) descr).getExpression();
Optional<EntryPointId> entryPointId = context.getEntryPointId(text);
if (entryPointId.isPresent()) {
return entryPointId.get();
}
// This builder is re-usable in other dialects, so specify by name
MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
boolean typeSafe = context.isTypesafe();
if (!dialect.isStrictMode()) {
context.setTypesafe(false);
}
try {
Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
AnalysisResult analysis = dialect.analyzeExpression(context, descr, text, new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
if (analysis == null) {
// something bad happened
return null;
}
Class<?> returnType = ((MVELAnalysisResult) analysis).getReturnType();
if (prefixPattern != null && !prefixPattern.isCompatibleWithFromReturnType(returnType)) {
context.addError(new DescrBuildError(descr, context.getRuleDescr(), null, "Pattern of type: '" + prefixPattern.getObjectType() + "' on rule '" + context.getRuleDescr().getName() + "' is not compatible with type " + returnType.getCanonicalName() + " returned by source"));
return null;
}
final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
final Declaration[] declarations = new Declaration[usedIdentifiers.getDeclrClasses().size()];
int j = 0;
for (String str : usedIdentifiers.getDeclrClasses().keySet()) {
declarations[j++] = decls.get(str);
}
Arrays.sort(declarations, SortDeclarations.instance);
MVELCompilationUnit unit = dialect.getMVELCompilationUnit(text, analysis, declarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.CONSEQUENCE);
MVELDataProvider dataProvider = new MVELDataProvider(unit, context.getDialect().getId());
From from = new From(dataProvider);
from.setResultPattern(prefixPattern);
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
data.addCompileable(from, dataProvider);
dataProvider.compile(data, context.getRule());
return from;
} catch (final Exception e) {
DialectUtil.copyErrorLocation(e, descr);
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Unable to build expression for 'from' : " + e.getMessage() + " '" + text + "'"));
return null;
} finally {
context.setTypesafe(typeSafe);
}
}
use of org.drools.compiler.compiler.DescrBuildError in project drools by kiegroup.
the class MVELReturnValueBuilder method build.
public void build(final RuleBuildContext context, final BoundIdentifiers usedIdentifiers, final Declaration[] previousDeclarations, final Declaration[] localDeclarations, final ReturnValueRestriction returnValueRestriction, final ReturnValueRestrictionDescr returnValueRestrictionDescr, final AnalysisResult analysis) {
boolean typesafe = context.isTypesafe();
try {
MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
context.setTypesafe(((MVELAnalysisResult) analysis).isTypesafe());
MVELCompilationUnit unit = dialect.getMVELCompilationUnit((String) returnValueRestrictionDescr.getContent(), analysis, previousDeclarations, localDeclarations, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.EXPRESSION);
MVELReturnValueExpression expr = new MVELReturnValueExpression(unit, context.getDialect().getId());
returnValueRestriction.setReturnValueExpression(expr);
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
data.addCompileable(returnValueRestriction, expr);
expr.compile(data, context.getRule());
} catch (final Exception e) {
DialectUtil.copyErrorLocation(e, context.getRuleDescr());
context.addError(new DescrBuildError(context.getParentDescr(), context.getRuleDescr(), null, "Unable to build expression for 'returnValue' : " + e.getMessage() + "'" + context.getRuleDescr().getSalience() + "'"));
} finally {
context.setTypesafe(typesafe);
}
}
use of org.drools.compiler.compiler.DescrBuildError in project drools by kiegroup.
the class JavaAccumulateBuilder method bindReaderToDeclaration.
private void bindReaderToDeclaration(RuleBuildContext context, AccumulateDescr accumDescr, Pattern pattern, AccumulateFunctionCallDescr fc, InternalReadAccessor readAccessor, Class<?> resultType, int index) {
if (fc.getBind() != null) {
if (context.getDeclarationResolver().isDuplicated(context.getRule(), fc.getBind(), resultType.getName())) {
if (!fc.isUnification()) {
context.addError(new DescrBuildError(context.getParentDescr(), accumDescr, null, "Duplicate declaration for variable '" + fc.getBind() + "' in the rule '" + context.getRule().getName() + "'"));
} else {
Declaration inner = context.getDeclarationResolver().getDeclaration(fc.getBind());
Constraint c = new MvelConstraint(Collections.singletonList(context.getPkg().getName()), index >= 0 ? "this[ " + index + " ] == " + fc.getBind() : "this == " + fc.getBind(), new Declaration[] { inner }, null, null, IndexUtil.ConstraintType.EQUAL, context.getDeclarationResolver().getDeclaration(fc.getBind()), index >= 0 ? new ArrayElementReader(readAccessor, index, resultType) : readAccessor, true);
((MutableTypeConstraint) c).setType(Constraint.ConstraintType.BETA);
pattern.addConstraint(c);
}
} else {
Declaration declr = pattern.addDeclaration(fc.getBind());
declr.setReadAccessor(readAccessor);
}
}
}
use of org.drools.compiler.compiler.DescrBuildError in project drools by kiegroup.
the class JavaRuleBuilderHelper method createConsequenceContext.
public static Map<String, Object> createConsequenceContext(final RuleBuildContext context, String consequenceName, String className, String consequenceText, Map<String, Declaration> decls, final BoundIdentifiers usedIdentifiers) {
final Declaration[] declarations = new Declaration[usedIdentifiers.getDeclrClasses().size()];
String[] declrStr = new String[declarations.length];
int j = 0;
for (String str : usedIdentifiers.getDeclrClasses().keySet()) {
declrStr[j] = str;
declarations[j++] = decls.get(str);
}
Arrays.sort(declarations, RuleTerminalNode.SortDeclarations.instance);
for (int i = 0; i < declrStr.length; i++) {
declrStr[i] = declarations[i].getIdentifier();
}
context.getRule().setRequiredDeclarationsForConsequence(consequenceName, declrStr);
final Map<String, Object> map = createVariableContext(className, consequenceText, context, declarations, null, usedIdentifiers.getGlobals());
map.put("consequenceName", consequenceName);
// final int[] indexes = new int[declarations.length];
final Integer[] indexes = new Integer[declarations.length];
final Boolean[] notPatterns = new Boolean[declarations.length];
for (int i = 0, length = declarations.length; i < length; i++) {
indexes[i] = i;
notPatterns[i] = (declarations[i].getExtractor() instanceof AcceptsClassObjectType) ? Boolean.FALSE : Boolean.TRUE;
if (indexes[i] == -1) {
context.addError(new DescrBuildError(context.getParentDescr(), context.getRuleDescr(), null, "Internal Error : Unable to find declaration in list while generating the consequence invoker"));
}
}
map.put("indexes", indexes);
map.put("notPatterns", notPatterns);
return map;
}
Aggregations