use of org.drools.drl.ast.descr.RuleDescr in project drools by kiegroup.
the class KiePMMLDescrFactoryTest method checkRules.
private void checkRules(List<RuleDescr> toCheck) {
assertEquals(1, toCheck.size());
RuleDescr retrieved = toCheck.get(0);
assertEquals(RULE_NAME, retrieved.getName());
}
use of org.drools.drl.ast.descr.RuleDescr in project drools by kiegroup.
the class KnowledgeBuilderImpl method reportHierarchyErrors.
private void reportHierarchyErrors(Map<String, List<RuleDescr>> parents, Map<String, RuleDescr> sorted) {
boolean circularDep = false;
for (List<RuleDescr> rds : parents.values()) {
for (RuleDescr ruleDescr : rds) {
if (parents.get(ruleDescr.getParentName()) != null && (sorted.containsKey(ruleDescr.getName()) || parents.containsKey(ruleDescr.getName()))) {
circularDep = true;
results.add(new RuleBuildError(descrToRule(ruleDescr), ruleDescr, null, "Circular dependency in rules hierarchy"));
break;
}
manageUnresolvedExtension(ruleDescr, sorted.values());
}
if (circularDep) {
break;
}
}
}
use of org.drools.drl.ast.descr.RuleDescr in project drools by kiegroup.
the class KnowledgeBuilderImpl method sortRulesByDependency.
private SortedRules sortRulesByDependency(PackageDescr packageDescr, PackageRegistry pkgRegistry) {
// Using a topological sorting algorithm
// see http://en.wikipedia.org/wiki/Topological_sorting
InternalKnowledgePackage pkg = pkgRegistry.getPackage();
List<RuleDescr> roots = new ArrayList<>();
Map<String, List<RuleDescr>> children = new HashMap<>();
LinkedHashMap<String, RuleDescr> sorted = new LinkedHashMap<>();
List<RuleDescr> queries = new ArrayList<>();
Set<String> compiledRules = new HashSet<>();
for (RuleDescr ruleDescr : packageDescr.getRules()) {
if (ruleDescr.isQuery()) {
queries.add(ruleDescr);
} else if (!ruleDescr.hasParent()) {
roots.add(ruleDescr);
} else {
if (pkg.getRule(ruleDescr.getParentName()) != null) {
// The parent of this rule has been already compiled
compiledRules.add(ruleDescr.getParentName());
}
children.computeIfAbsent(ruleDescr.getParentName(), k -> new ArrayList<>()).add(ruleDescr);
}
}
SortedRules sortedRules = new SortedRules();
sortedRules.queries = queries;
if (children.isEmpty()) {
// Sorting not necessary
if (!queries.isEmpty()) {
// Build all queries first
packageDescr.getRules().removeAll(queries);
packageDescr.getRules().addAll(0, queries);
sortedRules.rules.add(packageDescr.getRules().subList(queries.size(), packageDescr.getRules().size()));
} else {
sortedRules.rules.add(packageDescr.getRules());
}
return sortedRules;
}
for (String compiledRule : compiledRules) {
List<RuleDescr> childz = children.remove(compiledRule);
roots.addAll(childz);
}
List<RuleDescr> rulesLevel = roots;
while (!rulesLevel.isEmpty()) {
rulesLevel = sortRulesLevel(rulesLevel, sorted, sortedRules, children);
sortedRules.newLevel();
}
reportHierarchyErrors(children, sorted);
packageDescr.getRules().clear();
packageDescr.getRules().addAll(queries);
for (RuleDescr descr : sorted.values()) {
packageDescr.getRules().add(descr);
}
return sortedRules;
}
use of org.drools.drl.ast.descr.RuleDescr in project drools by kiegroup.
the class KnowledgeBuilderImpl method normalizeRuleAnnotations.
public void normalizeRuleAnnotations(PackageDescr packageDescr, TypeResolver typeResolver) {
boolean isStrict = configuration.getLanguageLevel().useJavaAnnotations();
for (RuleDescr ruleDescr : packageDescr.getRules()) {
normalizeAnnotations(ruleDescr, typeResolver, isStrict);
traverseAnnotations(ruleDescr.getLhs(), typeResolver, isStrict);
}
}
use of org.drools.drl.ast.descr.RuleDescr 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());
}
}
}
}
Aggregations