use of org.drools.model.EntryPoint 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.model.EntryPoint 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.model.EntryPoint in project drools by kiegroup.
the class KiePackagesBuilder method build.
public CanonicalKiePackages build() {
for (Model model : models) {
for (EntryPoint entryPoint : model.getEntryPoints()) {
InternalKnowledgePackage pkg = packages.computeIfAbsent(model.getName(), this::createKiePackage);
pkg.addEntryPointId(entryPoint.getName());
}
for (TypeMetaData metaType : model.getTypeMetaDatas()) {
InternalKnowledgePackage pkg = packages.computeIfAbsent(metaType.getPackage(), this::createKiePackage);
pkg.addTypeDeclaration(createTypeDeclaration(metaType, getPropertySpecificOption(), pkg.getTypeResolver()));
}
for (Global global : model.getGlobals()) {
InternalKnowledgePackage pkg = packages.computeIfAbsent(global.getPackage(), this::createKiePackage);
pkg.addGlobal(global.getName(), global.getType());
}
for (Query query : model.getQueries()) {
InternalKnowledgePackage pkg = packages.computeIfAbsent(query.getPackage(), this::createKiePackage);
pkg.addRule(compileQuery(pkg, query));
}
int ruleCounter = 0;
for (Rule rule : model.getRules()) {
InternalKnowledgePackage pkg = packages.computeIfAbsent(rule.getPackage(), this::createKiePackage);
for (RuleImpl ruleImpl : compileRule(pkg, rule)) {
ruleImpl.setLoadOrder(ruleCounter++);
pkg.addRule(ruleImpl);
}
}
}
return new CanonicalKiePackages(packages);
}
Aggregations