use of org.drools.compiler.rule.builder.RuleBuildContext in project drools by kiegroup.
the class KnowledgeBuilderImpl method compileRulesLevel.
private void compileRulesLevel(PackageDescr packageDescr, PackageRegistry pkgRegistry, List<RuleDescr> rules) {
boolean parallelRulesBuild = this.kBase == null && parallelRulesBuildThreshold != -1 && rules.size() > parallelRulesBuildThreshold;
if (parallelRulesBuild) {
Map<String, RuleBuildContext> ruleCxts = new ConcurrentHashMap<>();
try {
ForkJoinPoolHolder.COMPILER_POOL.submit(() -> rules.stream().parallel().filter(ruleDescr -> filterAccepts(ResourceChange.Type.RULE, ruleDescr.getNamespace(), ruleDescr.getName())).forEach(ruleDescr -> {
initRuleDescr(packageDescr, pkgRegistry, ruleDescr);
RuleBuildContext context = buildRuleBuilderContext(pkgRegistry, ruleDescr);
ruleCxts.put(ruleDescr.getName(), context);
List<? extends KnowledgeBuilderResult> results = addRule(context);
if (!results.isEmpty()) {
synchronized (this.results) {
this.results.addAll(results);
}
}
})).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException("Rules compilation failed or interrupted", e);
}
for (RuleDescr ruleDescr : rules) {
RuleBuildContext context = ruleCxts.get(ruleDescr.getName());
if (context != null) {
pkgRegistry.getPackage().addRule(context.getRule());
}
}
} else {
for (RuleDescr ruleDescr : rules) {
if (filterAccepts(ResourceChange.Type.RULE, ruleDescr.getNamespace(), ruleDescr.getName())) {
initRuleDescr(packageDescr, pkgRegistry, ruleDescr);
RuleBuildContext context = buildRuleBuilderContext(pkgRegistry, ruleDescr);
this.results.addAll(addRule(context));
pkgRegistry.getPackage().addRule(context.getRule());
}
}
}
}
use of org.drools.compiler.rule.builder.RuleBuildContext in project drools by kiegroup.
the class KnowledgeBuilderImpl method processWindowDeclarations.
protected void processWindowDeclarations(PackageRegistry pkgRegistry, PackageDescr packageDescr) {
for (WindowDeclarationDescr wd : packageDescr.getWindowDeclarations()) {
WindowDeclaration window = new WindowDeclaration(wd.getName(), packageDescr.getName());
// TODO: process annotations
// process pattern
InternalKnowledgePackage pkg = pkgRegistry.getPackage();
DialectCompiletimeRegistry ctr = pkgRegistry.getDialectCompiletimeRegistry();
RuleDescr dummy = new RuleDescr(wd.getName() + " Window Declaration");
dummy.setResource(packageDescr.getResource());
dummy.addAttribute(new AttributeDescr("dialect", "java"));
RuleBuildContext context = new RuleBuildContext(this, dummy, ctr, pkg, ctr.getDialect(pkgRegistry.getDialect()));
final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(wd.getPattern().getClass());
if (builder != null) {
final Pattern pattern = (Pattern) builder.build(context, wd.getPattern(), null);
if (pattern.getXpathConstraint() != null) {
context.addError(new DescrBuildError(wd, context.getParentDescr(), null, "OOpath expression " + pattern.getXpathConstraint() + " not allowed in window declaration\n"));
}
window.setPattern(pattern);
} else {
throw new RuntimeException("BUG: assembler not found for descriptor class " + wd.getPattern().getClass());
}
if (!context.getErrors().isEmpty()) {
for (DroolsError error : context.getErrors()) {
addBuilderResult(error);
}
} else {
pkgRegistry.getPackage().addWindowDeclaration(window);
}
}
}
use of org.drools.compiler.rule.builder.RuleBuildContext in project drools by kiegroup.
the class KnowledgeBuilderImpl method buildRuleBuilderContexts.
private Map<String, RuleBuildContext> buildRuleBuilderContexts(List<RuleDescr> rules, PackageRegistry pkgRegistry) {
Map<String, RuleBuildContext> map = new HashMap<>();
for (RuleDescr ruleDescr : rules) {
RuleBuildContext context = buildRuleBuilderContext(pkgRegistry, ruleDescr);
map.put(ruleDescr.getName(), context);
pkgRegistry.getPackage().addRule(context.getRule());
}
return map;
}
use of org.drools.compiler.rule.builder.RuleBuildContext in project drools by kiegroup.
the class JavaConsequenceBuilderPRAlwaysTest method setupTest.
private void setupTest(String consequence, Map<String, Object> namedConsequences) {
InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("org.drools");
pkg.addImport(new ImportDeclaration("org.drools.mvel.compiler.Cheese"));
KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
// Although it should be the default, for completeness we explicit this is the test cases how RHS should be rewritten as:
conf.setOption(PropertySpecificOption.ALWAYS);
KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl(pkg, conf);
ruleDescr = new RuleDescr("test consequence builder");
ruleDescr.setConsequence(consequence);
for (Entry<String, Object> entry : namedConsequences.entrySet()) {
ruleDescr.addNamedConsequences(entry.getKey(), entry.getValue());
}
RuleImpl rule = descrToRule(ruleDescr);
PackageRegistry pkgRegistry = kBuilder.getPackageRegistry(pkg.getName());
DialectCompiletimeRegistry reg = kBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
context = new RuleBuildContext(kBuilder, ruleDescr, reg, pkg, reg.getDialect(pkgRegistry.getDialect()));
rule.addPattern(new Pattern(0, new ClassObjectType(Cheese.class), "$cheese"));
Pattern p = new Pattern(1, new ClassObjectType(Person.class), "$persone");
Declaration declr = p.addDeclaration("age");
final InternalReadAccessor extractor = PatternBuilder.getFieldReadAccessor(context, new BindingDescr("age", "age"), p, "age", declr, true);
rule.addPattern(p);
context.getDeclarationResolver().pushOnBuildStack(rule.getLhs());
context.getDialect().getConsequenceBuilder().build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
for (String name : namedConsequences.keySet()) {
context.getDialect().getConsequenceBuilder().build(context, name);
}
context.getDialect().addRule(context);
pkgRegistry.getPackage().addRule(context.getRule());
kBuilder.compileAll();
kBuilder.reloadAll();
}
use of org.drools.compiler.rule.builder.RuleBuildContext in project drools by kiegroup.
the class MVELConsequenceBuilderTest method testSimpleExpression.
@Test
public void testSimpleExpression() throws Exception {
PackageDescr pkgDescr = new PackageDescr("pkg1");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
pkgBuilder.addPackage(pkgDescr);
InternalKnowledgePackage pkg = pkgBuilder.getPackageRegistry("pkg1").getPackage();
final RuleDescr ruleDescr = new RuleDescr("rule 1");
ruleDescr.setNamespace("pkg1");
ruleDescr.setConsequence("modify (cheese) {price = 5 };\nretract (cheese)");
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
final ObjectType cheeseObjeectType = new ClassObjectType(Cheese.class);
final Pattern pattern = new Pattern(0, cheeseObjeectType, "cheese");
final GroupElement subrule = new GroupElement(GroupElement.AND);
subrule.addChild(pattern);
final Map<String, Declaration> map = new HashMap<String, Declaration>();
map.put("cheese", pattern.getDeclaration());
declarationResolver.setDeclarations(map);
context.setDeclarationResolver(declarationResolver);
final MVELConsequenceBuilder builder = new MVELConsequenceBuilder();
builder.build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
PropagationContextFactory pctxFactory = RuntimeComponentFactory.get().getPropagationContextFactory();
kBase.addPackage(pkg);
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
MockTupleSource source = new MockTupleSource(1, buildContext);
source.setObjectCount(1);
RuleTerminalNode rtn = new RuleTerminalNode(0, source, context.getRule(), subrule, 0, buildContext);
final Cheese cheddar = new Cheese("cheddar", 10);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, rtn, true);
f0.removeLeftTuple(tuple);
final AgendaItem item = new AgendaItemImpl(0, tuple, 10, pctxFactory.createPropagationContext(1, PropagationContext.Type.DELETION, null, null, null), rtn, null);
final DefaultKnowledgeHelper kbHelper = new DefaultKnowledgeHelper(ksession);
kbHelper.setActivation(item);
((MVELConsequence) context.getRule().getConsequence()).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
context.getRule().getConsequence().evaluate(kbHelper, ksession);
assertEquals(5, cheddar.getPrice());
}
Aggregations