use of org.drools.core.rule.Function 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.rule.Function in project drools by kiegroup.
the class KnowledgeBuilderImpl method processFunctions.
private void processFunctions(PackageRegistry pkgRegistry, PackageDescr packageDescr) {
for (FunctionDescr function : packageDescr.getFunctions()) {
Function existingFunc = pkgRegistry.getPackage().getFunctions().get(function.getName());
if (existingFunc != null && function.getNamespace().equals(existingFunc.getNamespace())) {
addBuilderResult(new DuplicateFunction(function, this.configuration));
}
}
for (final FunctionImportDescr functionImport : packageDescr.getFunctionImports()) {
String importEntry = functionImport.getTarget();
pkgRegistry.addStaticImport(functionImport);
pkgRegistry.getPackage().addStaticImport(importEntry);
}
}
use of org.drools.core.rule.Function in project drools by kiegroup.
the class KnowledgeBaseImpl method internalAddPackages.
private void internalAddPackages(List<InternalKnowledgePackage> clonedPkgs) {
for (InternalWorkingMemory wm : getWorkingMemories()) {
wm.flushPropagations();
}
// we need to merge all byte[] first, so that the root classloader can resolve classes
for (InternalKnowledgePackage newPkg : clonedPkgs) {
newPkg.checkValidity();
this.eventSupport.fireBeforePackageAdded(newPkg);
if (newPkg.hasTraitRegistry()) {
getTraitRegistry().merge(newPkg.getTraitRegistry());
}
InternalKnowledgePackage pkg = this.pkgs.get(newPkg.getName());
if (pkg == null) {
pkg = new KnowledgePackageImpl(newPkg.getName());
// @TODO we really should have a single root cache
pkg.setClassFieldAccessorCache(this.classFieldAccessorCache);
pkgs.put(pkg.getName(), pkg);
}
// first merge anything related to classloader re-wiring
pkg.getDialectRuntimeRegistry().merge(newPkg.getDialectRuntimeRegistry(), this.rootClassLoader, true);
}
processAllTypesDeclaration(clonedPkgs);
for (InternalKnowledgePackage newPkg : clonedPkgs) {
// Add functions
JavaDialectRuntimeData runtime = ((JavaDialectRuntimeData) newPkg.getDialectRuntimeRegistry().getDialectData("java"));
for (Function function : newPkg.getFunctions().values()) {
String functionClassName = function.getClassName();
try {
registerFunctionClassAndInnerClasses(functionClassName, runtime, this::registerAndLoadTypeDefinition);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Unable to compile function '" + function.getName() + "'", e);
}
}
}
// now iterate again, this time onBeforeExecute will handle any wiring or cloader re-creating that needs to be done as part of the merge
for (InternalKnowledgePackage newPkg : clonedPkgs) {
InternalKnowledgePackage pkg = this.pkgs.get(newPkg.getName());
// this needs to go here, as functions will set a java dialect to dirty
if (newPkg.getFunctions() != null) {
for (Map.Entry<String, Function> entry : newPkg.getFunctions().entrySet()) {
pkg.addFunction(entry.getValue());
}
}
pkg.getDialectRuntimeRegistry().onBeforeExecute();
// with the classloader recreated for all byte[] classes, we should now merge and wire any new accessors
pkg.getClassFieldAccessorStore().merge(newPkg.getClassFieldAccessorStore());
}
for (InternalKnowledgePackage newPkg : clonedPkgs) {
InternalKnowledgePackage pkg = this.pkgs.get(newPkg.getName());
// now merge the new package into the existing one
mergePackage(pkg, newPkg);
// add the window declarations to the kbase
for (WindowDeclaration window : newPkg.getWindowDeclarations().values()) {
this.reteooBuilder.addNamedWindow(window);
}
// add entry points to the kbase
for (String id : newPkg.getEntryPointIds()) {
this.reteooBuilder.addEntryPoint(id);
}
// add the rules to the RuleBase
for (Rule r : newPkg.getRules()) {
RuleImpl rule = (RuleImpl) r;
checkMultithreadedEvaluation(rule);
internalAddRule(rule);
}
// add the flows to the RuleBase
if (newPkg.getRuleFlows() != null) {
final Map<String, Process> flows = newPkg.getRuleFlows();
for (Process process : flows.values()) {
internalAddProcess(process);
}
}
if (!newPkg.getResourceTypePackages().isEmpty()) {
KieWeavers weavers = ServiceRegistry.getInstance().get(KieWeavers.class);
for (ResourceTypePackage rtkKpg : newPkg.getResourceTypePackages().values()) {
ResourceType rt = rtkKpg.getResourceType();
KieWeaverService factory = weavers.getWeavers().get(rt);
factory.weave(this, newPkg, rtkKpg);
}
}
ruleUnitRegistry.add(newPkg.getRuleUnitRegistry());
this.eventSupport.fireAfterPackageAdded(newPkg);
}
if (config.isMultithreadEvaluation() && !hasMultiplePartitions()) {
disableMultithreadEvaluation("The rete network cannot be partitioned: disabling multithread evaluation");
}
}
use of org.drools.core.rule.Function in project drools by kiegroup.
the class KnowledgeBaseImpl method removeObjectsGeneratedFromResource.
public boolean removeObjectsGeneratedFromResource(Resource resource) {
boolean modified = false;
for (InternalKnowledgePackage pkg : pkgs.values()) {
List<RuleImpl> rulesToBeRemoved = pkg.getRulesGeneratedFromResource(resource);
if (!rulesToBeRemoved.isEmpty()) {
this.reteooBuilder.removeRules(rulesToBeRemoved);
// in order to allow the correct flushing of all outstanding staged tuples
for (RuleImpl rule : rulesToBeRemoved) {
pkg.removeRule(rule);
}
}
List<Function> functionsToBeRemoved = pkg.removeFunctionsGeneratedFromResource(resource);
for (Function function : functionsToBeRemoved) {
internalRemoveFunction(pkg.getName(), function.getName());
}
List<Process> processesToBeRemoved = pkg.removeProcessesGeneratedFromResource(resource);
for (Process process : processesToBeRemoved) {
processes.remove(process.getId());
}
List<TypeDeclaration> removedTypes = pkg.removeTypesGeneratedFromResource(resource);
boolean resourceTypePackageSomethingRemoved = pkg.removeFromResourceTypePackageGeneratedFromResource(resource);
modified |= !rulesToBeRemoved.isEmpty() || !functionsToBeRemoved.isEmpty() || !processesToBeRemoved.isEmpty() || !removedTypes.isEmpty() || resourceTypePackageSomethingRemoved;
}
return modified;
}
use of org.drools.core.rule.Function in project drools by kiegroup.
the class KnowledgeBaseImpl method internalRemoveFunction.
private void internalRemoveFunction(String packageName, String functionName) {
final InternalKnowledgePackage pkg = this.pkgs.get(packageName);
if (pkg == null) {
throw new IllegalArgumentException("Package name '" + packageName + "' does not exist for this Rule Base.");
}
Function function = pkg.getFunctions().get(functionName);
if (function == null) {
throw new IllegalArgumentException("function name '" + packageName + "' does not exist in the Package '" + packageName + "'.");
}
this.eventSupport.fireBeforeFunctionRemoved(pkg, functionName);
pkg.removeFunction(functionName);
this.eventSupport.fireAfterFunctionRemoved(pkg, functionName);
if (rootClassLoader instanceof ProjectClassLoader) {
((ProjectClassLoader) rootClassLoader).undefineClass(function.getClassName());
}
addReloadDialectDatas(pkg.getDialectRuntimeRegistry());
}
Aggregations