use of org.drools.core.rule.Declaration 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.core.rule.Declaration 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.core.rule.Declaration in project drools by kiegroup.
the class StatefulKnowledgeSessionImpl method getActivationParameters.
private Map getActivationParameters(Tuple tuple) {
Map result = new HashMap();
Declaration[] declarations = ((RuleTerminalNode) tuple.getTupleSink()).getAllDeclarations();
for (int i = 0; i < declarations.length; i++) {
FactHandle handle = tuple.get(declarations[i]);
if (handle instanceof InternalFactHandle) {
result.put(declarations[i].getIdentifier(), declarations[i].getValue(this, ((InternalFactHandle) handle).getObject()));
}
}
return result;
}
use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class StatefulKnowledgeSessionImpl method internalGetQueryResult.
protected QueryResultsImpl internalGetQueryResult(boolean calledFromRHS, String queryName, Object... arguments) {
try {
startOperation();
this.lock.lock();
this.kBase.executeQueuedActions();
// eventually enqueued by events that have been inserted when already expired
if (calledFromRHS) {
flushPropagations();
flushPropagations();
} else {
agenda.executeFlush();
agenda.executeFlush();
}
DroolsQuery queryObject = new DroolsQuery(queryName, arguments, getQueryListenerInstance(), false, null, null, null, null, null);
InternalFactHandle handle = this.handleFactory.newFactHandle(queryObject, null, this, this);
final PropagationContext pCtx = pctxFactory.createPropagationContext(getNextPropagationIdCounter(), PropagationContext.Type.INSERTION, null, null, handle, getEntryPoint());
BaseNode[] tnodes = evalQuery(queryName, queryObject, handle, pCtx, calledFromRHS);
List<Map<String, Declaration>> decls = new ArrayList<Map<String, Declaration>>();
if (tnodes != null) {
for (BaseNode node : tnodes) {
decls.add(((QueryTerminalNode) node).getSubRule().getOuterDeclarations());
}
}
this.handleFactory.destroyFactHandle(handle);
return new QueryResultsImpl((List<QueryRowWithSubruleIndex>) queryObject.getQueryResultCollector().getResults(), decls.toArray(new Map[decls.size()]), this, (queryObject.getQuery() != null) ? queryObject.getQuery().getParameters() : new Declaration[0]);
} finally {
this.lock.unlock();
endOperation();
}
}
use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class KiePackagesBuilder method addConstraintsToPattern.
private void addConstraintsToPattern(RuleContext ctx, Pattern pattern, org.drools.model.Pattern modelPattern, Constraint constraint) {
if (constraint.getType() == Constraint.Type.SINGLE) {
SingleConstraint singleConstraint = (SingleConstraint) constraint;
boolean isEqual = singleConstraint.getIndex() != null && singleConstraint.getIndex().getConstraintType() == Index.ConstraintType.EQUAL;
if (singleConstraint.getVariables().length > 0) {
Variable[] vars = singleConstraint.getVariables();
Declaration[] declarations = new Declaration[vars.length];
Declaration unificationDeclaration = null;
for (int i = 0; i < vars.length; i++) {
declarations[i] = ctx.getDeclaration(vars[i]);
if (isEqual && declarations[i].getPattern().getObjectType().equals(ClassObjectType.DroolsQuery_ObjectType)) {
unificationDeclaration = declarations[i];
} else if (pattern.getSource() instanceof MultiAccumulate) {
Declaration accDeclaration = pattern.getDeclarations().get(declarations[i].getBindingName());
if (accDeclaration != null) {
declarations[i].setReadAccessor(accDeclaration.getExtractor());
}
}
}
ConstraintEvaluator constraintEvaluator = singleConstraint.isTemporal() ? new TemporalConstraintEvaluator(declarations, pattern, singleConstraint) : new ConstraintEvaluator(declarations, pattern, singleConstraint);
org.drools.core.spi.Constraint droolsConstraint = unificationDeclaration != null ? new UnificationConstraint(unificationDeclaration, constraintEvaluator) : new LambdaConstraint(constraintEvaluator);
pattern.addConstraint(droolsConstraint);
}
} else if (modelPattern.getConstraint().getType() == Constraint.Type.AND) {
for (Constraint child : constraint.getChildren()) {
addConstraintsToPattern(ctx, pattern, modelPattern, child);
}
}
}
Aggregations