use of org.drools.core.definitions.InternalKnowledgePackage 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()) {
kBase.removeRules(rulesToBeRemoved);
}
});
}
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class KnowledgeBuilderImpl method addPackage.
public synchronized void addPackage(InternalKnowledgePackage newPkg) {
PackageRegistry pkgRegistry = this.pkgRegistryMap.get(newPkg.getName());
InternalKnowledgePackage pkg = null;
if (pkgRegistry != null) {
pkg = pkgRegistry.getPackage();
}
if (pkg == null) {
PackageDescr packageDescr = new PackageDescr(newPkg.getName());
pkgRegistry = getOrCreatePackageRegistry(packageDescr);
mergePackage(this.pkgRegistryMap.get(packageDescr.getNamespace()), packageDescr);
pkg = pkgRegistry.getPackage();
}
// first merge anything related to classloader re-wiring
pkg.getDialectRuntimeRegistry().merge(newPkg.getDialectRuntimeRegistry(), this.rootClassLoader);
if (newPkg.getFunctions() != null) {
for (Map.Entry<String, Function> entry : newPkg.getFunctions().entrySet()) {
if (pkg.getFunctions().containsKey(entry.getKey())) {
addBuilderResult(new DuplicateFunction(entry.getValue(), this.configuration));
}
pkg.addFunction(entry.getValue());
}
}
pkg.getClassFieldAccessorStore().merge(newPkg.getClassFieldAccessorStore());
pkg.getDialectRuntimeRegistry().onBeforeExecute();
// we have to do this before the merging, as it does some classloader resolving
TypeDeclaration lastType = null;
try {
// Resolve the class for the type declaation
if (newPkg.getTypeDeclarations() != null) {
// add type declarations
for (TypeDeclaration type : newPkg.getTypeDeclarations().values()) {
lastType = type;
type.setTypeClass(this.rootClassLoader.loadClass(type.getTypeClassName()));
}
}
} catch (ClassNotFoundException e) {
throw new RuntimeException("unable to resolve Type Declaration class '" + lastType.getTypeName() + "'");
}
// now merge the new package into the existing one
mergePackage(pkg, newPkg);
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class KnowledgeBuilderImpl method processGlobals.
private void processGlobals(PackageRegistry pkgRegistry, PackageDescr packageDescr) {
InternalKnowledgePackage pkg = pkgRegistry.getPackage();
Set<String> existingGlobals = new HashSet<String>(pkg.getGlobals().keySet());
for (final GlobalDescr global : packageDescr.getGlobals()) {
final String identifier = global.getIdentifier();
existingGlobals.remove(identifier);
String className = global.getType();
// JBRULES-3039: can't handle type name with generic params
while (className.indexOf('<') >= 0) {
className = className.replaceAll("<[^<>]+?>", "");
}
try {
Class<?> clazz = pkgRegistry.getTypeResolver().resolveType(className);
if (clazz.isPrimitive()) {
addBuilderResult(new GlobalError(global, " Primitive types are not allowed in globals : " + className));
return;
}
pkg.addGlobal(identifier, clazz);
addGlobal(identifier, clazz);
if (kBase != null) {
kBase.addGlobal(identifier, clazz);
}
} catch (final ClassNotFoundException e) {
addBuilderResult(new GlobalError(global, e.getMessage()));
e.printStackTrace();
}
}
for (String toBeRemoved : existingGlobals) {
if (filterAcceptsRemoval(ResourceChange.Type.GLOBAL, pkg.getName(), toBeRemoved)) {
pkg.removeGlobal(toBeRemoved);
if (kBase != null) {
kBase.removeGlobal(toBeRemoved);
}
}
}
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class KnowledgeBuilderImpl method getPackages.
public InternalKnowledgePackage[] getPackages() {
InternalKnowledgePackage[] pkgs = new InternalKnowledgePackage[this.pkgRegistryMap.size()];
String errors = null;
if (!getErrors().isEmpty()) {
errors = getErrors().toString();
}
int i = 0;
for (PackageRegistry pkgRegistry : this.pkgRegistryMap.values()) {
InternalKnowledgePackage pkg = pkgRegistry.getPackage();
pkg.getDialectRuntimeRegistry().onBeforeExecute();
if (errors != null) {
pkg.setError(errors);
}
pkgs[i++] = pkg;
}
return pkgs;
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class TypeDeclarationBuilder method processUnresolvedTypes.
private void processUnresolvedTypes(PackageDescr packageDescr, PackageRegistry pkgRegistry, Collection<AbstractClassTypeDeclarationDescr> unsortedDescrs, List<TypeDefinition> unresolvedTypes, Map<String, AbstractClassTypeDeclarationDescr> unprocesseableDescrs) {
// merge "duplicate" definitions and declarations
unsortedDescrs = compactDefinitionsAndDeclarations(unsortedDescrs, unprocesseableDescrs);
// now sort declarations by mutual dependencies
ClassHierarchyManager classHierarchyManager = new ClassHierarchyManager(unsortedDescrs, kbuilder);
for (AbstractClassTypeDeclarationDescr typeDescr : classHierarchyManager.getSortedDescriptors()) {
PackageRegistry pkgReg = getPackageRegistry(pkgRegistry, packageDescr, typeDescr);
createBean(typeDescr, pkgReg, classHierarchyManager, unresolvedTypes, unprocesseableDescrs);
}
for (AbstractClassTypeDeclarationDescr typeDescr : classHierarchyManager.getSortedDescriptors()) {
if (!unprocesseableDescrs.containsKey(typeDescr.getType().getFullName())) {
PackageRegistry pkgReg = getPackageRegistry(pkgRegistry, packageDescr, typeDescr);
InternalKnowledgePackage pkg = pkgReg.getPackage();
TypeDeclaration type = pkg.getTypeDeclaration(typeDescr.getType().getName());
typeDeclarationConfigurator.wireFieldAccessors(pkgReg, typeDescr, type);
if (kbuilder.getKnowledgeBase() != null) {
// in case of incremental compilatoin (re)register the new type declaration on the existing kbase
kbuilder.getKnowledgeBase().registerTypeDeclaration(type, pkg);
}
}
}
}
Aggregations