use of org.drools.drl.ast.descr.RuleDescr in project drools by kiegroup.
the class JavaRuleBuilderHelper method createJavaAnalysisResult.
public static JavaAnalysisResult createJavaAnalysisResult(final RuleBuildContext context, String consequenceName, Map<String, Declaration> decls) {
final RuleDescr ruleDescr = context.getRuleDescr();
BoundIdentifiers bindings = new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context, Collections.EMPTY_MAP, KnowledgeHelper.class);
String consequenceStr = (RuleImpl.DEFAULT_CONSEQUENCE_NAME.equals(consequenceName)) ? (String) ruleDescr.getConsequence() : (String) ruleDescr.getNamedConsequences().get(consequenceName);
consequenceStr = consequenceStr + "\n";
return (JavaAnalysisResult) context.getDialect().analyzeBlock(context, ruleDescr, consequenceStr, bindings);
}
use of org.drools.drl.ast.descr.RuleDescr in project drools by kiegroup.
the class JavaConsequenceBuilderTest 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();
// this test was originally intended with PropertyReactive.ALLOWED:
conf.setOption(PropertySpecificOption.ALLOWED);
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.drl.ast.descr.RuleDescr in project drools by kiegroup.
the class KnowledgeBuilderImpl method preProcessRules.
private void preProcessRules(PackageDescr packageDescr, PackageRegistry pkgRegistry) {
if (this.kBase == null) {
return;
}
InternalKnowledgePackage pkg = pkgRegistry.getPackage();
boolean needsRemoval = false;
// first, check if any rules no longer exist
for (org.kie.api.definition.rule.Rule rule : pkg.getRules()) {
if (filterAcceptsRemoval(ResourceChange.Type.RULE, rule.getPackageName(), rule.getName())) {
needsRemoval = true;
break;
}
}
if (!needsRemoval) {
for (RuleDescr ruleDescr : packageDescr.getRules()) {
if (filterAccepts(ResourceChange.Type.RULE, ruleDescr.getNamespace(), ruleDescr.getName())) {
if (pkg.getRule(ruleDescr.getName()) != null) {
needsRemoval = true;
break;
}
}
}
}
if (needsRemoval) {
kBase.enqueueModification(() -> {
Collection<RuleImpl> rulesToBeRemoved = new HashSet<>();
for (org.kie.api.definition.rule.Rule rule : pkg.getRules()) {
if (filterAcceptsRemoval(ResourceChange.Type.RULE, rule.getPackageName(), rule.getName())) {
rulesToBeRemoved.add(((RuleImpl) rule));
}
}
rulesToBeRemoved.forEach(pkg::removeRule);
for (RuleDescr ruleDescr : packageDescr.getRules()) {
if (filterAccepts(ResourceChange.Type.RULE, ruleDescr.getNamespace(), ruleDescr.getName())) {
RuleImpl rule = pkg.getRule(ruleDescr.getName());
if (rule != null) {
rulesToBeRemoved.add(rule);
}
}
}
if (!rulesToBeRemoved.isEmpty()) {
rulesToBeRemoved.addAll(findChildrenRulesToBeRemoved(packageDescr, rulesToBeRemoved));
kBase.removeRules(rulesToBeRemoved);
}
});
}
}
use of org.drools.drl.ast.descr.RuleDescr in project drools by kiegroup.
the class KnowledgeBuilderImpl method manageUnresolvedExtension.
private void manageUnresolvedExtension(RuleDescr ruleDescr, Collection<RuleDescr> candidates) {
List<String> candidateRules = new ArrayList<>();
for (RuleDescr r : candidates) {
if (StringUtils.stringSimilarity(ruleDescr.getParentName(), r.getName(), StringUtils.SIMILARITY_STRATS.DICE) >= 0.75) {
candidateRules.add(r.getName());
}
}
String msg = "Unresolved parent name " + ruleDescr.getParentName();
if (!candidateRules.isEmpty()) {
msg += " >> did you mean any of :" + candidateRules;
}
results.add(new RuleBuildError(descrToRule(ruleDescr), ruleDescr, msg, "Unable to resolve parent rule, please check that both rules are in the same package"));
}
use of org.drools.drl.ast.descr.RuleDescr in project drools by kiegroup.
the class KnowledgeBuilderImpl method compileAllQueries.
private void compileAllQueries(PackageDescr packageDescr, PackageRegistry pkgRegistry, List<RuleDescr> rules) {
Map<String, RuleBuildContext> ruleCxts = buildRuleBuilderContexts(rules, pkgRegistry);
for (RuleDescr ruleDescr : rules) {
if (filterAccepts(ResourceChange.Type.RULE, ruleDescr.getNamespace(), ruleDescr.getName())) {
initRuleDescr(packageDescr, pkgRegistry, ruleDescr);
this.results.addAll(addRule(ruleCxts.get(ruleDescr.getName())));
}
}
}
Aggregations