use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class KnowledgeBuilderImpl method createPackageRegistry.
private PackageRegistry createPackageRegistry(PackageDescr packageDescr) {
initPackage(packageDescr);
InternalKnowledgePackage pkg;
if (this.kBase == null || (pkg = this.kBase.getPackage(packageDescr.getName())) == null) {
// there is no rulebase or it does not define this package so define it
pkg = new KnowledgePackageImpl(packageDescr.getName());
pkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(this.rootClassLoader));
// if there is a rulebase then add the package.
if (this.kBase != null) {
try {
pkg = (InternalKnowledgePackage) this.kBase.addPackage(pkg).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
} else {
// the RuleBase will also initialise the
pkg.getDialectRuntimeRegistry().onAdd(this.rootClassLoader);
}
}
PackageRegistry pkgRegistry = new PackageRegistry(rootClassLoader, configuration, pkg);
// add default import for this namespace
pkgRegistry.addImport(new ImportDescr(packageDescr.getNamespace() + ".*"));
for (ImportDescr importDescr : packageDescr.getImports()) {
pkgRegistry.registerImport(importDescr.getTarget());
}
return pkgRegistry;
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class KnowledgeBuilderImpl method validateUniqueRuleNames.
protected void validateUniqueRuleNames(final PackageDescr packageDescr) {
final Set<String> names = new HashSet<String>();
PackageRegistry packageRegistry = this.pkgRegistryMap.get(packageDescr.getNamespace());
InternalKnowledgePackage pkg = null;
if (packageRegistry != null) {
pkg = packageRegistry.getPackage();
}
for (final RuleDescr rule : packageDescr.getRules()) {
validateRule(packageDescr, rule);
final String name = rule.getName();
if (names.contains(name)) {
addBuilderResult(new ParserError(rule.getResource(), "Duplicate rule name: " + name, rule.getLine(), rule.getColumn(), packageDescr.getNamespace()));
}
if (pkg != null) {
RuleImpl duplicatedRule = pkg.getRule(name);
if (duplicatedRule != null) {
Resource resource = rule.getResource();
Resource duplicatedResource = duplicatedRule.getResource();
if (resource == null || duplicatedResource == null || duplicatedResource.getSourcePath() == null || duplicatedResource.getSourcePath().equals(resource.getSourcePath())) {
addBuilderResult(new DuplicateRule(rule, packageDescr, this.configuration));
} else {
addBuilderResult(new ParserError(rule.getResource(), "Duplicate rule name: " + name, rule.getLine(), rule.getColumn(), packageDescr.getNamespace()));
}
}
}
names.add(name);
}
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class KnowledgeBuilderImpl method rewireAllClassObjectTypes.
public void rewireAllClassObjectTypes() {
if (kBase != null) {
for (InternalKnowledgePackage pkg : kBase.getPackagesMap().values()) {
pkg.getDialectRuntimeRegistry().getDialectData("java").setDirty(true);
pkg.getClassFieldAccessorStore().wire();
}
}
}
use of org.drools.core.definitions.InternalKnowledgePackage 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 LinkedList<RuleDescr>();
Map<String, List<RuleDescr>> children = new HashMap<String, List<RuleDescr>>();
LinkedHashMap<String, RuleDescr> sorted = new LinkedHashMap<String, RuleDescr>();
List<RuleDescr> queries = new ArrayList<RuleDescr>();
Set<String> compiledRules = new HashSet<String>();
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.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class KnowledgeBuilderImpl method processWindowDeclarations.
private 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);
}
}
}
Aggregations