use of org.kie.api.internal.io.ResourceTypePackage 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.kie.api.internal.io.ResourceTypePackage in project drools by kiegroup.
the class KnowledgePackageImpl 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.ruleFlows = (Map) in.readObject();
this.globals = (Map<String, String>) 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.traitRegistry = (TraitRegistry) in.readObject();
this.resourceTypePackages = (Map<ResourceType, ResourceTypePackage>) in.readObject();
in.setStore(null);
if (!isDroolsStream) {
in.close();
}
}
use of org.kie.api.internal.io.ResourceTypePackage in project drools by kiegroup.
the class BayesAssemblerService method addResource.
@Override
public void addResource(Object kbuilder, Resource resource, ResourceType type, ResourceConfiguration configuration) throws Exception {
BayesNetwork network;
JunctionTreeBuilder builder;
KnowledgeBuilder kb = (KnowledgeBuilder) kbuilder;
Bif bif = XmlBifParser.loadBif(resource, kb.getErrors());
if (bif == null) {
return;
}
try {
network = XmlBifParser.buildBayesNetwork(bif);
} catch (Exception e) {
kb.getErrors().add(new BayesNetworkAssemblerError(resource, "Unable to parse opening Stream:\n" + e.toString()));
return;
}
try {
builder = new JunctionTreeBuilder(network);
} catch (Exception e) {
kb.getErrors().add(new BayesNetworkAssemblerError(resource, "Unable to build Junction Tree:\n" + e.toString()));
return;
}
KnowledgeBuilderImpl kbuilderImpl = (KnowledgeBuilderImpl) kbuilder;
PackageRegistry pkgReg = kbuilderImpl.getOrCreatePackageRegistry(new PackageDescr(network.getPackageName()));
InternalKnowledgePackage kpkgs = pkgReg.getPackage();
Map<ResourceType, ResourceTypePackage> rpkg = kpkgs.getResourceTypePackages();
BayesPackage bpkg = (BayesPackage) rpkg.get(ResourceType.BAYES);
if (bpkg == null) {
bpkg = new BayesPackage();
rpkg.put(ResourceType.BAYES, bpkg);
}
bpkg.addJunctionTree(network.getName(), builder.build());
}
use of org.kie.api.internal.io.ResourceTypePackage in project drools by kiegroup.
the class WeaverTest method testBayesPackageWeaving.
@Test
public void testBayesPackageWeaving() throws Exception {
KnowledgeBuilderImpl kbuilder = new KnowledgeBuilderImpl();
kbuilder.add(ResourceFactory.newClassPathResource("Garden.xmlbif", AssemblerTest.class), ResourceType.BAYES);
InternalKnowledgeBase kbase = getKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
InternalKnowledgePackage kpkg = (InternalKnowledgePackage) kbase.getKiePackage("org.drools.beliefs.bayes.integration");
Map<ResourceType, ResourceTypePackage> map = kpkg.getResourceTypePackages();
BayesPackage existing = (BayesPackage) map.get(ResourceType.BAYES);
JunctionTree jtree = existing.getJunctionTree("Garden");
assertNotNull(jtree);
}
use of org.kie.api.internal.io.ResourceTypePackage in project drools by kiegroup.
the class DMNAssemblerService method addResource.
@Override
public void addResource(Object kbuilder, Resource resource, ResourceType type, ResourceConfiguration configuration) throws Exception {
logger.warn("invoked legacy addResource (no control on the order of the assembler compilation): " + resource.getSourcePath());
KnowledgeBuilderImpl kbuilderImpl = (KnowledgeBuilderImpl) kbuilder;
DMNCompiler dmnCompiler = kbuilderImpl.getCachedOrCreate(DMN_COMPILER_CACHE_KEY, () -> getCompiler(kbuilderImpl));
Collection<DMNModel> dmnModels = new ArrayList<>();
for (PackageRegistry pr : kbuilderImpl.getPackageRegistry().values()) {
ResourceTypePackage resourceTypePackage = pr.getPackage().getResourceTypePackages().get(ResourceType.DMN);
if (resourceTypePackage != null) {
DMNPackageImpl dmnpkg = (DMNPackageImpl) resourceTypePackage;
dmnModels.addAll(dmnpkg.getAllModels().values());
}
}
compileResourceToModel(kbuilderImpl, dmnCompiler, resource, dmnModels);
}
Aggregations