use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class FromNode method initDeclaredMask.
@Override
protected void initDeclaredMask(BuildContext context, LeftTupleSource leftInput) {
super.initDeclaredMask(context, leftInput);
if (leftDeclaredMask.isAllSet()) {
return;
}
if (context == null || context.getLastBuiltPatterns() == null) {
// only happens during unit tests
leftDeclaredMask = AllSetBitMask.get();
return;
}
Pattern pattern = context.getLastBuiltPatterns()[1];
if (pattern == null) {
return;
}
ObjectType objectType = pattern.getObjectType();
// if pattern is null (e.g. for eval or query nodes) we cannot calculate the mask, so we set it all
if (isPropertyReactive(context, objectType)) {
Collection<String> leftListenedProperties = pattern.getListenedProperties();
List<String> accessibleProperties = getAccessibleProperties(context.getRuleBase(), objectType);
leftDeclaredMask = leftDeclaredMask.setAll(calculatePositiveMask(objectType, leftListenedProperties, accessibleProperties));
leftNegativeMask = leftNegativeMask.setAll(calculateNegativeMask(objectType, leftListenedProperties, accessibleProperties));
}
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class LeftTupleSource method initDeclaredMask.
protected void initDeclaredMask(BuildContext context, LeftTupleSource leftInput) {
if (context == null || context.getLastBuiltPatterns() == null) {
// only happens during unit tests
leftDeclaredMask = AllSetBitMask.get();
return;
}
if (leftInput.getType() != NodeTypeEnums.LeftInputAdapterNode) {
// BetaNode's not after LIANode are not relevant for left mask property specific, so don't block anything.
leftDeclaredMask = AllSetBitMask.get();
return;
}
// left input pattern
Pattern pattern = getLeftInputPattern(context);
ObjectType objectType = getObjectTypeForPropertyReactivity((LeftInputAdapterNode) leftInput, pattern);
if (!(objectType instanceof ClassObjectType)) {
// Only ClassObjectType can use property specific
leftDeclaredMask = AllSetBitMask.get();
return;
}
if (pattern != null && isPropertyReactive(context, objectType)) {
Collection<String> leftListenedProperties = pattern.getListenedProperties();
List<String> accessibleProperties = getAccessibleProperties(context.getRuleBase(), objectType);
leftDeclaredMask = calculatePositiveMask(objectType, leftListenedProperties, accessibleProperties);
leftDeclaredMask = setNodeConstraintsPropertyReactiveMask(leftDeclaredMask, objectType, accessibleProperties);
leftNegativeMask = calculateNegativeMask(objectType, leftListenedProperties, accessibleProperties);
setLeftListenedProperties(leftListenedProperties);
} else {
// if property specific is not on, then accept all modification propagations
leftDeclaredMask = AllSetBitMask.get();
}
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class RuleContext method getDeclaration.
Declaration getDeclaration(Variable variable) {
if (variable == null) {
return null;
}
if (variable.isFact()) {
Declaration declaration = declarations.get(variable);
if (declaration == null) {
declaration = getQueryDeclaration(variable);
}
return declaration;
} else {
Global global = ((Global) variable);
ObjectType objectType = builder.getObjectType(global);
InternalReadAccessor globalExtractor = new GlobalExtractor(global.getName(), objectType);
return new Declaration(global.getName(), globalExtractor, new Pattern(0, objectType));
}
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class KiePackagesBuilder method getRequiredDeclarationsForAccumulate.
private Declaration[] getRequiredDeclarationsForAccumulate(RuleContext ctx, RuleConditionElement source, AccumulateFunction accFunction, Binding binding, BindingEvaluator bindingEvaluator) {
Declaration[] requiredDeclarations = getRequiredDeclarationsForAccumulate(ctx, binding, accFunction);
if (requiredDeclarations.length == 0 && source instanceof Pattern && bindingEvaluator != null && bindingEvaluator.getDeclarations() != null) {
List<Declaration> previousDecl = new ArrayList<>();
Pattern patternSource = (Pattern) source;
patternSource.resetDeclarations();
for (Declaration d : bindingEvaluator.getDeclarations()) {
if (d.getIdentifier().equals(patternSource.getDeclaration().getIdentifier())) {
patternSource.addDeclaration(d);
} else {
previousDecl.add(d);
}
}
requiredDeclarations = previousDecl.toArray(new Declaration[previousDecl.size()]);
}
return requiredDeclarations;
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class KiePackagesBuilder method buildAccumulate.
private RuleConditionElement buildAccumulate(RuleContext ctx, GroupElement group, AccumulatePattern accumulatePattern) {
Pattern pattern = null;
boolean isGroupBy = accumulatePattern instanceof GroupByPattern;
if (accumulatePattern.getAccumulateFunctions() != null) {
if (!isGroupBy && accumulatePattern.getAccumulateFunctions().length == 1) {
// non groupby with single accumulates can be optimized to directly return the result, rather than place in an array of 1
pattern = ctx.getPattern(accumulatePattern.getAccumulateFunctions()[0].getResult());
} else if (accumulatePattern.getAccumulateFunctions().length > 0 && ctx.getPattern(accumulatePattern.getAccumulateFunctions()[0].getResult()) != null) {
// Illegal executable model. Cannot have groupby or multi accumulate mapped to a single result object.
throw new RuntimeException("Only single accumulate functions, with no group by can optimize the result pattern to be the function return value");
}
}
boolean existingPattern = pattern != null;
if (!existingPattern) {
ObjectType type = !isGroupBy && accumulatePattern.getAccumulateFunctions().length == 1 ? new ClassObjectType(accumulatePattern.getAccumulateFunctions()[0].getResult().getType()) : // groupby or multi function accumulate
JAVA_CLASS_ARRAY_TYPE;
pattern = new Pattern(ctx.getNextPatternIndex(), type);
}
PatternImpl<?> sourcePattern = (PatternImpl<?>) accumulatePattern.getPattern();
Set<String> usedVariableName = new LinkedHashSet<>();
if (sourcePattern != null) {
for (Variable v : sourcePattern.getInputVariables()) {
usedVariableName.add(v.getName());
}
}
RuleConditionElement source;
if (accumulatePattern.isQuerySource()) {
source = buildQueryPattern(ctx, ((QueryCallPattern) accumulatePattern.getCondition()));
} else if (accumulatePattern.isCompositePatterns()) {
CompositePatterns compositePatterns = (CompositePatterns) accumulatePattern.getCondition();
GroupElement allSubConditions = new GroupElement(conditionToGroupElementType(compositePatterns.getType()));
for (Condition c : compositePatterns.getSubConditions()) {
recursivelyAddConditions(ctx, group, allSubConditions, c);
}
source = allSubConditions.getChildren().size() == 1 ? allSubConditions.getChildren().get(0) : allSubConditions;
} else {
source = buildPattern(ctx, group, accumulatePattern);
}
Collection<Binding> bindings = new ArrayList<>();
if (sourcePattern != null) {
bindings.addAll(sourcePattern.getBindings());
bindings.add(new SelfPatternBiding<>(sourcePattern.getPatternVariable()));
} else {
// No pattern is associated. It likely uses inner bindings
addInnerBindings(bindings, accumulatePattern.getAccumulateFunctions(), accumulatePattern.getCondition());
}
pattern.setSource(buildAccumulate(ctx, accumulatePattern, source, pattern, usedVariableName, bindings));
return existingPattern ? null : pattern;
}
Aggregations