use of org.drools.core.rule.RuleConditionElement in project drools by kiegroup.
the class JavaAccumulateBuilder method build.
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
final AccumulateDescr accumDescr = (AccumulateDescr) descr;
if (!accumDescr.hasValidInput()) {
return null;
}
// build source
BaseDescr input = accumDescr.getInput();
if (input instanceof AndDescr && ((AndDescr) input).getDescrs().size() == 1) {
input = ((AndDescr) input).getDescrs().get(0);
}
final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(input.getClass());
final RuleConditionElement source = builder.build(context, input);
if (source == null) {
return null;
}
final boolean readLocalsFromTuple = PackageBuilderUtil.isReadLocalsFromTuple(context, accumDescr, source);
Map<String, Declaration> declsInScope = context.getDeclarationResolver().getDeclarations(context.getRule());
if (prefixPattern != null && prefixPattern.getDeclaration() != null) {
declsInScope.remove(prefixPattern.getDeclaration().getIdentifier());
}
Map<String, Class<?>> declCls = DeclarationScopeResolver.getDeclarationClasses(declsInScope);
Accumulate accumulate;
if (accumDescr.isExternalFunction()) {
// if it uses 1+ external function, build methods for them
accumulate = buildExternalFunctionCall(context, accumDescr, source, declsInScope, declCls, readLocalsFromTuple);
} else {
// if it uses inline code, build the class for it
accumulate = buildInlineAccumulate(context, accumDescr, source, declsInScope, declCls, readLocalsFromTuple);
}
return accumulate;
}
use of org.drools.core.rule.RuleConditionElement in project drools by kiegroup.
the class MVELAccumulateBuilder method build.
@SuppressWarnings("unchecked")
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
boolean typesafe = context.isTypesafe();
try {
final AccumulateDescr accumDescr = (AccumulateDescr) descr;
if (!accumDescr.hasValidInput()) {
return null;
}
final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(accumDescr.getInput().getClass());
// create source CE
final RuleConditionElement source = builder.build(context, accumDescr.getInput());
if (source == null) {
return null;
}
MVELDialect dialect = (MVELDialect) context.getDialect();
Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
Map<String, Declaration> sourceOuterDeclr = source.getOuterDeclarations();
Map<String, Class<?>> declarationClasses = DeclarationScopeResolver.getDeclarationClasses(decls);
declarationClasses.putAll(DeclarationScopeResolver.getDeclarationClasses(sourceOuterDeclr));
BoundIdentifiers boundIds = new BoundIdentifiers(declarationClasses, context);
final boolean readLocalsFromTuple = PackageBuilderUtil.isReadLocalsFromTuple(context, accumDescr, source);
Accumulator[] accumulators;
if (accumDescr.isExternalFunction()) {
// uses accumulate functions
accumulators = buildExternalFunctions(context, accumDescr, dialect, decls, sourceOuterDeclr, boundIds, readLocalsFromTuple, source, declarationClasses);
} else {
// it is a custom accumulate
accumulators = buildCustomAccumulate(context, accumDescr, dialect, decls, sourceOuterDeclr, boundIds, readLocalsFromTuple);
}
List<Declaration> requiredDeclarations = new ArrayList<Declaration>();
for (Accumulator acc : accumulators) {
MvelAccumulator mvelAcc = (MvelAccumulator) acc;
Collections.addAll(requiredDeclarations, mvelAcc.getRequiredDeclarations());
}
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
Accumulate accumulate;
if (accumDescr.isMultiFunction()) {
accumulate = new MultiAccumulate(source, requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]), accumulators, accumulators.length);
int index = 0;
for (Accumulator accumulator : accumulators) {
data.addCompileable(((MultiAccumulate) accumulate).new Wirer(index++), (MVELCompileable) accumulator);
((MVELCompileable) accumulator).compile(data, context.getRule());
}
} else {
accumulate = new SingleAccumulate(source, requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]), accumulators[0]);
data.addCompileable(((SingleAccumulate) accumulate).new Wirer(), (MVELCompileable) accumulators[0]);
((MVELCompileable) accumulators[0]).compile(data, context.getRule());
}
return accumulate;
} catch (Exception e) {
AsmUtil.copyErrorLocation(e, descr);
context.addError(new DescrBuildError(context.getParentDescr(), descr, e, "Unable to build expression for 'accumulate' : " + e.getMessage()));
return null;
} finally {
context.setTypesafe(typesafe);
}
}
use of org.drools.core.rule.RuleConditionElement in project drools by kiegroup.
the class PatternBuilder method buildQuery.
private RuleConditionElement buildQuery(RuleBuildContext context, PatternDescr descr, PatternDescr patternDescr) {
RuleConditionElement rce = null;
// it might be a recursive query, so check for same names
if (context.getRule().getName().equals(patternDescr.getObjectType())) {
// it's a query so delegate to the QueryElementBuilder
rce = buildQueryElement(context, descr, (QueryImpl) context.getRule());
}
if (rce == null) {
// look up the query in the current package
RuleImpl rule = context.getPkg().getRule(patternDescr.getObjectType());
if (rule instanceof QueryImpl) {
// it's a query so delegate to the QueryElementBuilder
rce = buildQueryElement(context, descr, (QueryImpl) rule);
}
}
if (rce == null) {
// the query may have been imported, so try package imports
for (String importName : context.getDialect().getTypeResolver().getImports()) {
importName = importName.trim();
int pos = importName.indexOf('*');
if (pos >= 0) {
String pkgName = importName.substring(0, pos - 1);
PackageRegistry pkgReg = context.getKnowledgeBuilder().getPackageRegistry(pkgName);
if (pkgReg != null) {
RuleImpl rule = pkgReg.getPackage().getRule(patternDescr.getObjectType());
if (rule instanceof QueryImpl) {
// it's a query so delegate to the QueryElementBuilder
rce = buildQueryElement(context, descr, (QueryImpl) rule);
break;
}
}
}
}
}
if (rce == null) {
// this isn't a query either, so log an error
registerDescrBuildError(context, patternDescr, "Unable to resolve ObjectType '" + patternDescr.getObjectType() + "'");
}
return rce;
}
use of org.drools.core.rule.RuleConditionElement in project drools by kiegroup.
the class KiePackagesBuilder method addPatternForVariable.
private Pattern addPatternForVariable(RuleContext ctx, GroupElement group, Variable patternVariable, Condition.Type type) {
Pattern pattern = null;
// If the variable is already bound to the result of previous accumulate result pattern, then find it.
if (patternVariable instanceof org.drools.model.Declaration) {
org.drools.model.Declaration decl = (org.drools.model.Declaration) patternVariable;
if (decl.getSource() == null) {
Accumulate accSource = ctx.getAccumulateSource(patternVariable);
if (accSource != null) {
for (RuleConditionElement element : group.getChildren()) {
if (element instanceof Pattern && ((Pattern) element).getSource() == accSource) {
pattern = (Pattern) element;
break;
}
}
}
}
}
PatternSource priorSource = null;
if (pattern != null && type == Condition.Type.ACCUMULATE) {
// so it would be potentially tricky if this was a multi var, with multiple bindings.
if (pattern.getSource() instanceof SingleAccumulate) {
group.getChildren().remove(pattern);
priorSource = pattern.getSource();
pattern = null;
}
}
if (pattern == null) {
pattern = new Pattern(ctx.getNextPatternIndex(), // tupleIndex will be set by ReteooBuilder
0, // tupleIndex will be set by ReteooBuilder
0, getObjectType(patternVariable), patternVariable.getName(), true);
pattern.setSource(priorSource);
}
if (patternVariable instanceof org.drools.model.Declaration) {
org.drools.model.Declaration decl = (org.drools.model.Declaration) patternVariable;
if (decl.getSource() != null) {
if (decl.getSource() instanceof EntryPoint) {
pattern.setSource(new EntryPointId(((EntryPoint) decl.getSource()).getName()));
} else if (decl.getSource() instanceof WindowReference) {
WindowReference<?> window = (WindowReference) decl.getSource();
if (!ctx.getPkg().getWindowDeclarations().containsKey(window.getName())) {
createWindowReference(ctx, window);
}
pattern.setSource(new org.drools.core.rule.WindowReference(window.getName()));
} else if (decl.getSource() instanceof From) {
pattern.setSource(buildFrom(ctx, pattern, (From) decl.getSource()));
} else if (decl.getSource() instanceof UnitData) {
UnitData unitData = (UnitData) decl.getSource();
pattern.setSource(new EntryPointId(ctx.getRule().getRuleUnitClassName() + "." + unitData.getName()));
} else {
throw new UnsupportedOperationException("Unknown source: " + decl.getSource());
}
}
if (decl.getWindow() != null) {
pattern.addBehavior(createWindow(decl.getWindow()));
ctx.setNeedStreamMode();
}
} else if (patternVariable instanceof Exchange) {
if (type == Condition.Type.SENDER) {
Function0 supplier = ((Exchange) patternVariable).getMessageSupplier();
DataProvider provider = new LambdaDataProvider(x -> supplier.apply(), false);
pattern.setSource(new AsyncSend(pattern, patternVariable.getName(), provider));
} else if (type == Condition.Type.RECEIVER) {
pattern.setSource(new AsyncReceive(pattern, patternVariable.getName()));
} else {
throw new UnsupportedOperationException();
}
}
ctx.registerPattern(patternVariable, pattern);
return pattern;
}
use of org.drools.core.rule.RuleConditionElement in project drools by kiegroup.
the class KiePackagesBuilder method recursivelyAddConditions.
private void recursivelyAddConditions(RuleContext ctx, GroupElement group, GroupElement allSubConditions, Condition c) {
if (c instanceof CompositePatterns) {
c.getSubConditions().forEach(sc -> recursivelyAddConditions(ctx, group, allSubConditions, sc));
} else if (c instanceof ExistentialPatternImpl) {
if (c.getType() == Condition.Type.FORALL) {
allSubConditions.addChild(buildForAll(ctx, allSubConditions, c));
} else {
GroupElement existGroupElement = new GroupElement(conditionToGroupElementType(c.getType()));
allSubConditions.addChild(existGroupElement);
recursivelyAddConditions(ctx, existGroupElement, existGroupElement, c.getSubConditions().iterator().next());
}
} else if (c instanceof PatternImpl) {
org.drools.model.Pattern pattern = (org.drools.model.Pattern<?>) c;
RuleConditionElement rce = buildPattern(ctx, allSubConditions, pattern);
if (ctx.getAccumulateSource(pattern.getPatternVariable()) == null) {
allSubConditions.addChild(rce);
}
} else if (c instanceof AccumulatePattern) {
RuleConditionElement rce = buildAccumulate(ctx, group, (AccumulatePattern) c);
if (rce != null) {
allSubConditions.addChild(rce);
}
} else if (c instanceof EvalImpl) {
allSubConditions.addChild(buildEval(ctx, (EvalImpl) c));
}
}
Aggregations