use of org.drools.core.rule.WindowDeclaration in project drools by kiegroup.
the class KnowledgeBaseImpl method mergePackage.
/**
* Merge a new package with an existing package.
* Most of the work is done by the concrete implementations,
* but this class does some work (including combining imports, compilation data, globals,
* and the actual Rule objects into the package).
*/
private void mergePackage(InternalKnowledgePackage pkg, InternalKnowledgePackage newPkg) {
// Merge imports
final Map<String, ImportDeclaration> imports = pkg.getImports();
imports.putAll(newPkg.getImports());
// Merge static imports
for (String staticImport : newPkg.getStaticImports()) {
pkg.addStaticImport(staticImport);
}
String lastIdent = null;
String lastType = null;
try {
// merge globals
if (newPkg.getGlobals() != null && newPkg.getGlobals() != Collections.EMPTY_MAP) {
Map<String, String> globals = pkg.getGlobals();
// Add globals
for (final Map.Entry<String, String> entry : newPkg.getGlobals().entrySet()) {
final String identifier = entry.getKey();
final String type = entry.getValue();
lastIdent = identifier;
lastType = type;
if (globals.containsKey(identifier) && !globals.get(identifier).equals(type)) {
throw new RuntimeException(pkg.getName() + " cannot be integrated");
} else {
pkg.addGlobal(identifier, this.rootClassLoader.loadClass(type));
// this isn't a package merge, it's adding to the rulebase, but I've put it here for convienience
addGlobal(identifier, this.rootClassLoader.loadClass(type));
}
}
}
} catch (ClassNotFoundException e) {
throw new RuntimeException("Unable to resolve class '" + lastType + "' for global '" + lastIdent + "'");
}
// merge entry point declarations
if (newPkg.getEntryPointIds() != null) {
for (String ep : newPkg.getEntryPointIds()) {
pkg.addEntryPointId(ep);
}
}
// merge the type declarations
if (newPkg.getTypeDeclarations() != null) {
// add type declarations
for (TypeDeclaration type : newPkg.getTypeDeclarations().values()) {
// @TODO should we allow overrides? only if the class is not in use.
if (!pkg.getTypeDeclarations().containsKey(type.getTypeName())) {
// add to package list of type declarations
pkg.addTypeDeclaration(type);
}
}
}
// merge window declarations
if (newPkg.getWindowDeclarations() != null) {
// add window declarations
for (WindowDeclaration window : newPkg.getWindowDeclarations().values()) {
if (!pkg.getWindowDeclarations().containsKey(window.getName()) || pkg.getWindowDeclarations().get(window.getName()).equals(window)) {
pkg.addWindowDeclaration(window);
} else {
throw new RuntimeException("Unable to merge two conflicting window declarations for window named: " + window.getName());
}
}
}
// Merge rules into the RuleBase package
// as this is needed for individual rule removal later on
List<RuleImpl> rulesToBeRemoved = new ArrayList<RuleImpl>();
for (Rule newRule : newPkg.getRules()) {
// remove the rule if it already exists
RuleImpl oldRule = pkg.getRule(newRule.getName());
if (oldRule != null) {
rulesToBeRemoved.add(oldRule);
}
}
if (!rulesToBeRemoved.isEmpty()) {
removeRules(rulesToBeRemoved);
}
for (Rule newRule : newPkg.getRules()) {
pkg.addRule((RuleImpl) newRule);
}
// Merge The Rule Flows
if (newPkg.getRuleFlows() != null) {
for (Process flow : newPkg.getRuleFlows().values()) {
pkg.addProcess(flow);
}
}
if (!newPkg.getResourceTypePackages().isEmpty()) {
for (ResourceTypePackage rtkKpg : newPkg.getResourceTypePackages().values()) {
ResourceType rt = rtkKpg.getResourceType();
KieWeavers weavers = ServiceRegistry.getInstance().get(KieWeavers.class);
KieWeaverService weaver = weavers.getWeavers().get(rt);
weaver.merge(this, pkg, rtkKpg);
}
}
}
use of org.drools.core.rule.WindowDeclaration in project drools by kiegroup.
the class KnowledgeBaseImpl method mergePackage.
/**
* Merge a new package with an existing package.
* Most of the work is done by the concrete implementations,
* but this class does some work (including combining imports, compilation data, globals,
* and the actual Rule objects into the package).
*/
private void mergePackage(InternalKnowledgePackage pkg, InternalKnowledgePackage newPkg, Collection<InternalWorkingMemory> workingMemories) {
// Merge imports
final Map<String, ImportDeclaration> imports = pkg.getImports();
imports.putAll(newPkg.getImports());
// Merge static imports
for (String staticImport : newPkg.getStaticImports()) {
pkg.addStaticImport(staticImport);
}
// merge globals
if (newPkg.getGlobals() != null && !newPkg.getGlobals().isEmpty()) {
Map<String, Class<?>> pkgGlobals = pkg.getGlobals();
// Add globals
for (final Map.Entry<String, Class<?>> entry : newPkg.getGlobals().entrySet()) {
final String identifier = entry.getKey();
final Class<?> type = entry.getValue();
if (pkgGlobals.containsKey(identifier) && !pkgGlobals.get(identifier).equals(type)) {
throw new RuntimeException(pkg.getName() + " cannot be integrated");
} else {
pkg.addGlobal(identifier, type);
// this isn't a package merge, it's adding to the rulebase, but I've put it here for convienience
addGlobal(identifier, type);
}
}
}
// merge entry point declarations
if (newPkg.getEntryPointIds() != null) {
for (String ep : newPkg.getEntryPointIds()) {
pkg.addEntryPointId(ep);
}
}
// merge the type declarations
if (newPkg.getTypeDeclarations() != null) {
// add type declarations
for (TypeDeclaration type : newPkg.getTypeDeclarations().values()) {
// @TODO should we allow overrides? only if the class is not in use.
if (!pkg.getTypeDeclarations().containsKey(type.getTypeName())) {
// add to package list of type declarations
pkg.addTypeDeclaration(type);
}
}
}
// merge window declarations
if (newPkg.getWindowDeclarations() != null) {
// add window declarations
for (WindowDeclaration window : newPkg.getWindowDeclarations().values()) {
if (!pkg.getWindowDeclarations().containsKey(window.getName()) || pkg.getWindowDeclarations().get(window.getName()).equals(window)) {
pkg.addWindowDeclaration(window);
} else {
throw new RuntimeException("Unable to merge two conflicting window declarations for window named: " + window.getName());
}
}
}
// Merge rules into the RuleBase package
// as this is needed for individual rule removal later on
List<RuleImpl> rulesToBeRemoved = new ArrayList<>();
for (Rule newRule : newPkg.getRules()) {
// remove the rule if it already exists
RuleImpl oldRule = pkg.getRule(newRule.getName());
if (oldRule != null) {
rulesToBeRemoved.add(oldRule);
}
}
if (!rulesToBeRemoved.isEmpty()) {
kBaseInternal_removeRules(rulesToBeRemoved, workingMemories);
}
for (Rule newRule : newPkg.getRules()) {
pkg.addRule((RuleImpl) newRule);
}
// Merge The Rule Flows
if (newPkg.getRuleFlows() != null) {
for (Process flow : newPkg.getRuleFlows().values()) {
pkg.addProcess(flow);
}
}
if (!newPkg.getResourceTypePackages().isEmpty()) {
KieWeavers weavers = KieService.load(KieWeavers.class);
if (weavers == null) {
throw new IllegalStateException("Unable to find KieWeavers implementation");
}
for (ResourceTypePackage rtkKpg : newPkg.getResourceTypePackages().values()) {
weavers.merge(pkg, rtkKpg);
}
}
}
use of org.drools.core.rule.WindowDeclaration in project drools by kiegroup.
the class MVELKnowledgePackageImpl method readExternal.
/**
* Handles the read serialization of the Package. Patterns in Rules may
* reference generated data which cannot be serialized by default methods.
* The Package uses PackageCompilationData to hold a reference to the
* generated bytecode; which must be restored before any Rules. A custom
* ObjectInputStream, able to resolve classes against the bytecode in the
* PackageCompilationData, is used to restore the Rules.
*
* @param stream, the stream to read data from in order to restore the object;
* should be an instance of DroolsObjectInputStream or
* InputStream
*/
public void readExternal(ObjectInput stream) throws IOException, ClassNotFoundException {
boolean isDroolsStream = stream instanceof DroolsObjectInputStream;
DroolsObjectInputStream in = isDroolsStream ? (DroolsObjectInputStream) stream : new DroolsObjectInputStream(new ByteArrayInputStream((byte[]) stream.readObject()));
this.name = (String) in.readObject();
this.classFieldAccessorStore = (ClassFieldAccessorStore) in.readObject();
in.setStore(this.classFieldAccessorStore);
this.dialectRuntimeRegistry = (DialectRuntimeRegistry) in.readObject();
this.typeDeclarations = (Map) in.readObject();
this.imports = (Map<String, ImportDeclaration>) in.readObject();
this.staticImports = (Set) in.readObject();
this.functions = (Map<String, Function>) in.readObject();
this.accumulateFunctions = (Map<String, AccumulateFunction>) in.readObject();
this.factTemplates = (Map) in.readObject();
this.globals = (Map<String, Class<?>>) in.readObject();
this.valid = in.readBoolean();
this.needStreamMode = in.readBoolean();
this.rules = (Map<String, RuleImpl>) in.readObject();
this.entryPointsIds = (Set<String>) in.readObject();
this.windowDeclarations = (Map<String, WindowDeclaration>) in.readObject();
this.resourceTypePackages = (ResourceTypePackageRegistry) in.readObject();
in.setStore(null);
if (!isDroolsStream) {
in.close();
}
}
use of org.drools.core.rule.WindowDeclaration 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.WindowDeclaration in project drools by kiegroup.
the class KnowledgeBuilderImpl method processWindowDeclarations.
protected 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