use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.
the class DialectUtil method findClassByName.
public static Class<?> findClassByName(RuleBuildContext context, String className) {
if (className == null) {
return null;
}
String namespace = context.getRuleDescr().getNamespace();
KnowledgeBuilderImpl packageBuilder = context.getKnowledgeBuilder();
Class<?> clazz = null;
try {
clazz = Class.forName(className.indexOf('.') < 0 ? namespace + "." + className : className, false, packageBuilder.getRootClassLoader());
} catch (ClassNotFoundException e) {
}
if (clazz != null) {
return clazz;
}
Set<String> imports = new HashSet<String>();
List<PackageDescr> pkgDescrs = packageBuilder.getPackageDescrs(namespace);
if (pkgDescrs == null) {
return null;
}
for (PackageDescr pkgDescr : pkgDescrs) {
for (ImportDescr importDescr : pkgDescr.getImports()) {
imports.add(importDescr.getTarget());
}
}
return findClass(className, imports, packageBuilder.getRootClassLoader());
}
use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.
the class PatternBuilder method getTypeDeclaration.
private TypeDeclaration getTypeDeclaration(RuleBuildContext context, Class<?> userProvidedClass) {
String packageName = ClassUtils.getPackage(userProvidedClass);
KnowledgeBuilderImpl kbuilder = context.getKnowledgeBuilder();
PackageRegistry pkgr = kbuilder.getPackageRegistry(packageName);
TypeDeclaration typeDeclaration = pkgr != null ? pkgr.getPackage().getTypeDeclaration(userProvidedClass) : null;
if (typeDeclaration == null && kbuilder.getKnowledgeBase() != null) {
// check if the type declaration is contained only in the already existing kbase (possible during incremental compilation)
InternalKnowledgePackage pkg = kbuilder.getKnowledgeBase().getPackage(packageName);
typeDeclaration = pkg != null ? pkg.getTypeDeclaration(userProvidedClass) : null;
}
if (typeDeclaration == null) {
typeDeclaration = context.getPkg().getTypeDeclaration(userProvidedClass);
}
return typeDeclaration;
}
use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.
the class Misc2Test method testFactTemplates.
@Test
public void testFactTemplates() {
// DROOLS-600
String drl = "package com.testfacttemplate;" + " rule \"test rule\" " + " dialect \"mvel\" " + " when " + " $test : TestFactTemplate( status == 1 ) " + " then " + " System.out.println( \"Hello World\" ); " + " end ";
KnowledgePackageImpl kPackage = new KnowledgePackageImpl("com.testfacttemplate");
FieldTemplate fieldTemplate = new FieldTemplateImpl("status", 0, Integer.class);
FactTemplate factTemplate = new FactTemplateImpl(kPackage, "TestFactTemplate", new FieldTemplate[] { fieldTemplate });
KnowledgeBuilder kBuilder = new KnowledgeBuilderImpl(kPackage);
StringReader rule = new StringReader(drl);
try {
((KnowledgeBuilderImpl) kBuilder).addPackageFromDrl(rule);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.
the class KieBuilderSetImpl method buildChanges.
private IncrementalResults buildChanges(Collection<String> filesToBuild) {
Map<String, Collection<KnowledgeBuilderError>> currentErrors = new HashMap<String, Collection<KnowledgeBuilderError>>();
InternalKieModule kieModule = (InternalKieModule) kieBuilder.getKieModuleIgnoringErrors();
for (KieBaseModel kBaseModel : kieModule.getKieModuleModel().getKieBaseModels().values()) {
KnowledgeBuilder kBuilder = kieModule.getKnowledgeBuilderForKieBase(kBaseModel.getName());
if (kBuilder == null) {
continue;
}
CompositeKnowledgeBuilder ckbuilder = kBuilder.batch();
KnowledgeBuilderImpl.ResourceRemovalResult removalResult = new KnowledgeBuilderImpl.ResourceRemovalResult();
KnowledgeBuilderImpl pkgBuilder = ((KnowledgeBuilderImpl) kBuilder);
Set<String> wrongResources = resourcesWithErrors.get(kBaseModel.getName());
for (String resourceName : wrongResources) {
removalResult.add(pkgBuilder.removeObjectsGeneratedFromResource(new DummyResource(resourceName)));
removalResult.mergeModified(addResource(ckbuilder, kBaseModel, kieModule, resourceName));
}
for (String file : filesToBuild) {
if (wrongResources.contains(file)) {
removalResult.mergeModified(true);
} else {
// remove the objects generated by the old Resource
removalResult.add(pkgBuilder.removeObjectsGeneratedFromResource(new DummyResource(file)));
// add the modified Resource
removalResult.mergeModified(addResource(ckbuilder, kBaseModel, kieModule, file));
}
}
if (removalResult.isModified()) {
if (!removalResult.getRemovedTypes().isEmpty()) {
ProjectClassLoader projectClassLoader = (ProjectClassLoader) ((KnowledgeBuilderImpl) kBuilder).getRootClassLoader();
projectClassLoader.reinitTypes();
for (String removedType : removalResult.getRemovedTypes()) {
projectClassLoader.undefineClass(removedType);
}
}
ckbuilder.build();
resourcesWithErrors.put(kBaseModel.getName(), findResourcesWithErrors(kBuilder));
if (kBuilder.hasErrors()) {
currentErrors.put(kBaseModel.getName(), kBuilder.getErrors());
kBuilder.undo();
} else {
KieServices.Factory.get().getRepository().addKieModule(kieModule);
kieBuilder.updateKieModuleMetaInfo();
}
}
}
IncrementalResultsImpl results = getIncrementalResults(currentErrors);
previousErrors = currentErrors;
return results;
}
use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl 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));
}
}
Aggregations