use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class KnowledgeBuilderTest method createRule.
private RuleImpl createRule(final ConditionalElementDescr ceDescr, final KnowledgeBuilderImpl builder, final String consequence) throws Exception {
final PackageDescr packageDescr = new PackageDescr("p1");
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
lhs.addDescr((BaseDescr) ceDescr);
final PatternDescr patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
patternDescr.addConstraint(new ExprConstraintDescr("type == \"stilton\" "));
ceDescr.addDescr(patternDescr);
ruleDescr.setConsequence(consequence);
builder.addPackage(packageDescr);
InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
final RuleImpl rule = pkg.getRule("rule-1");
assertEquals("rule-1", rule.getName());
return rule;
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class CompilerPerfProfileTest method build.
private void build(String msg, String resource, boolean showResults) throws DroolsParserException, IOException {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
long start = System.currentTimeMillis();
builder.addPackageFromDrl(new InputStreamReader(getClass().getResourceAsStream(resource)));
InternalKnowledgePackage pkg = builder.getPackage("org.drools.compiler.test");
assertFalse(builder.hasErrors());
assertNotNull(pkg);
if (showResults) {
System.out.print("Time taken for " + msg + " : " + (System.currentTimeMillis() - start));
}
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class PackageBuilderConfigurationTest method testMockDialect.
@Test
public void testMockDialect() {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.pkg1");
KnowledgeBuilderConfigurationImpl cfg1 = new KnowledgeBuilderConfigurationImpl();
MockDialectConfiguration mockConf = new MockDialectConfiguration();
// cfg1.buildDialectRegistry().addDialect( "mock",
// mockConf.getDialect() );
cfg1.addDialect("mock", mockConf);
cfg1.setDefaultDialect("mock");
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl(pkg, cfg1);
PackageRegistry pkgRegistry = builder.getPackageRegistry(pkg.getName());
DialectCompiletimeRegistry dialectRegistry = pkgRegistry.getDialectCompiletimeRegistry();
MockDialect mockDialect2 = (MockDialect) dialectRegistry.getDialect(cfg1.getDefaultDialect());
assertSame(pkg, mockDialect2.getPkg());
assertNull(mockDialect2.getRuleDescr());
RuleDescr ruleDescr = new RuleDescr("test rule");
ruleDescr.addAttribute(new AttributeDescr("dialect", "mock"));
ruleDescr.setLhs(new AndDescr());
EvalDescr evalDescr = new EvalDescr();
ruleDescr.getLhs().addDescr(evalDescr);
PackageDescr pkgDescr = new PackageDescr("org.pkg1");
pkgDescr.addImport(new ImportDescr("java.util.HashMap"));
FunctionImportDescr functionImportDescr = new FunctionImportDescr();
functionImportDescr.setTarget("java.lang.System.currentTimeMillis");
pkgDescr.addFunctionImport(functionImportDescr);
pkgDescr.addRule(ruleDescr);
builder.addPackage(pkgDescr);
assertSame(ruleDescr, mockDialect2.getRuleDescr());
assertTrue(mockDialect2.getImport().contains("java.util.HashMap"));
assertTrue(mockDialect2.getStaticImport().contains("java.lang.System.currentTimeMillis"));
assertEquals("eval was built", evalDescr.getContent());
assertEquals("consequence was built", ruleDescr.getConsequence());
assertTrue(mockDialect2.isCompileAll());
assertNotNull(pkg.getRule("test rule"));
// make sure there were no other general errors.
assertFalse(builder.hasErrors());
}
use of org.drools.core.definitions.InternalKnowledgePackage 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.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class KnowledgeBaseImpl method populateTypeDeclarationMaps.
/**
* type classes must be re-wired after serialization
*
* @throws ClassNotFoundException
*/
private void populateTypeDeclarationMaps() throws ClassNotFoundException {
// FIXME: readLock
this.classTypeDeclaration = new HashMap<String, TypeDeclaration>();
for (InternalKnowledgePackage pkg : this.pkgs.values()) {
for (TypeDeclaration type : pkg.getTypeDeclarations().values()) {
type.setTypeClass(this.rootClassLoader.loadClass(type.getTypeClassName()));
this.classTypeDeclaration.put(type.getTypeClassName(), type);
}
}
}
Aggregations