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) {
Pattern pattern = new Pattern(ctx.getNextPatternIndex(), // offset will be set by ReteooBuilder
0, getObjectType(patternVariable), patternVariable.getName(), true);
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) {
From<?> from = (From) decl.getSource();
DataProvider provider = new LambdaDataProvider(ctx.getDeclaration(from.getVariable()), from.getProvider(), from.isReactive());
org.drools.core.rule.From fromSource = new org.drools.core.rule.From(provider);
fromSource.setResultPattern(pattern);
pattern.setSource(fromSource);
} 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());
}
} else {
Accumulate accSource = ctx.getAccumulateSource(patternVariable);
if (accSource != null) {
for (RuleConditionElement element : group.getChildren()) {
if (element instanceof Pattern && ((Pattern) element).getSource() == accSource) {
if (accSource instanceof MultiAccumulate) {
((Pattern) element).getConstraints().forEach(pattern::addConstraint);
((Pattern) element).getDeclarations().values().forEach(d -> {
pattern.addDeclaration(d);
d.setPattern(pattern);
});
}
group.getChildren().remove(element);
break;
}
}
pattern.setSource(accSource);
}
}
if (decl.getWindow() != null) {
pattern.addBehavior(createWindow(decl.getWindow()));
}
}
ctx.registerPattern(patternVariable, pattern);
return pattern;
}
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());
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 DescrBuilderTest method testDumperFromPkg.
@Test
public void testDumperFromPkg() {
// DROOLS-109
PackageDescr pkg = DescrFactory.newPackage().name("org.test").newRule().name("org.test").lhs().and().or().pattern().id("$x", false).type("Integer").constraint("this > 10").end().pattern().id("$x", false).type("Integer").constraint("this < 20").end().end().pattern().type("Integer").constraint("this == $x").constraint("this == 42").end().end().end().rhs("").end().end().getDescr();
String drl = new DrlDumper().dump(pkg);
System.out.println(drl);
KnowledgeBuilderImpl knowledgeBuilder = (KnowledgeBuilderImpl) KnowledgeBuilderFactory.newKnowledgeBuilder();
knowledgeBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
System.err.println(knowledgeBuilder.getErrors());
assertFalse(knowledgeBuilder.getErrors().toString(), knowledgeBuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(knowledgeBuilder.getKnowledgePackages());
KieSession knowledgeSession = kbase.newKieSession();
KiePackage rebuiltPkg = knowledgeBuilder.getPackage("org.test");
org.kie.api.definition.rule.Rule rule = rebuiltPkg.getRules().iterator().next();
RuleImpl r = ((RuleImpl) rule);
assertEquals(2, r.getLhs().getChildren().size());
Iterator<RuleConditionElement> iter = r.getLhs().getChildren().iterator();
RuleConditionElement arg1 = iter.next();
assertTrue(arg1 instanceof GroupElement && ((GroupElement) arg1).getType() == GroupElement.Type.OR);
assertEquals(2, ((GroupElement) arg1).getChildren().size());
RuleConditionElement arg2 = iter.next();
assertTrue(arg2 instanceof Pattern);
}
use of org.drools.core.rule.RuleConditionElement 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;
}
use of org.drools.core.rule.RuleConditionElement in project drools by kiegroup.
the class GroupElementBuilder method build.
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
final ConditionalElementDescr cedescr = (ConditionalElementDescr) descr;
final GroupElement ge = this.newGroupElementFor(descr);
context.getDeclarationResolver().pushOnBuildStack(ge);
if (prefixPattern != null) {
ge.addChild(prefixPattern);
}
// iterate over child descriptors
for (final BaseDescr child : cedescr.getDescrs()) {
// gets child to build
// child.setResource(..) does not seem to be necessary (since builderImpls have already set the resource for all children)
// but leaving it in here to be save
child.setResource(context.getRuleDescr().getResource());
child.setNamespace(context.getRuleDescr().getNamespace());
// gets corresponding builder
final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(child.getClass());
if (builder != null) {
final RuleConditionElement element = builder.build(context, child);
// builder will return null. Ex: ClassNotFound for the pattern type
if (element != null) {
ge.addChild(element);
}
} else {
throw new RuntimeException("BUG: no builder found for descriptor class " + child.getClass());
}
}
context.getDeclarationResolver().popBuildStack();
return ge;
}
Aggregations