use of org.drools.compiler.compiler.PackageRegistry 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.compiler.PackageRegistry in project drools by kiegroup.
the class PatternBuilder method buildQuery.
private RuleConditionElement buildQuery(RuleBuildContext context, PatternDescr descr, PatternDescr patternDescr) {
RuleConditionElement rce = null;
// it might be a recursive query, so check for same names
if (context.getRule().getName().equals(patternDescr.getObjectType())) {
// it's a query so delegate to the QueryElementBuilder
rce = buildQueryElement(context, descr, (QueryImpl) context.getRule());
}
if (rce == null) {
// look up the query in the current package
RuleImpl rule = context.getPkg().getRule(patternDescr.getObjectType());
if (rule instanceof QueryImpl) {
// it's a query so delegate to the QueryElementBuilder
rce = buildQueryElement(context, descr, (QueryImpl) rule);
}
}
if (rce == null) {
// the query may have been imported, so try package imports
for (String importName : context.getDialect().getTypeResolver().getImports()) {
importName = importName.trim();
int pos = importName.indexOf('*');
if (pos >= 0) {
String pkgName = importName.substring(0, pos - 1);
PackageRegistry pkgReg = context.getKnowledgeBuilder().getPackageRegistry(pkgName);
if (pkgReg != null) {
RuleImpl rule = pkgReg.getPackage().getRule(patternDescr.getObjectType());
if (rule instanceof QueryImpl) {
// it's a query so delegate to the QueryElementBuilder
rce = buildQueryElement(context, descr, (QueryImpl) rule);
break;
}
}
}
}
}
if (rce == null) {
// this isn't a query either, so log an error
registerDescrBuildError(context, patternDescr, "Unable to resolve ObjectType '" + patternDescr.getObjectType() + "'");
}
return rce;
}
use of org.drools.compiler.compiler.PackageRegistry in project drools by kiegroup.
the class ModelBuilderImpl method buildRules.
protected void buildRules(Collection<CompositePackageDescr> packages) {
for (CompositePackageDescr packageDescr : packages) {
PackageRegistry pkgRegistry = getPackageRegistry(packageDescr.getNamespace());
generatePOJOs(packageDescr, pkgRegistry);
}
List<GeneratedClassWithPackage> allGeneratedPojos = packageModels.values().stream().flatMap(p -> p.getGeneratedPOJOsSource().stream().map(c -> new GeneratedClassWithPackage(c, p.getName(), p.getImports()))).collect(Collectors.toList());
// Every class gets compiled in each classloader, maybe they can be compiled only one time?
final Map<String, Class<?>> allCompiledClasses = new HashMap<>();
for (CompositePackageDescr packageDescr : packages) {
InternalKnowledgePackage pkg = getPackageRegistry(packageDescr.getNamespace()).getPackage();
allCompiledClasses.putAll(compileType(this, pkg.getPackageClassLoader(), allGeneratedPojos));
}
for (CompositePackageDescr packageDescr : packages) {
InternalKnowledgePackage pkg = getPackageRegistry(packageDescr.getNamespace()).getPackage();
allGeneratedPojos.forEach(c -> registerType(pkg.getTypeResolver(), allCompiledClasses));
}
if (hasErrors()) {
// if Error while generating pojo do not try compile rule as they very likely depends hence fail too.
return;
}
for (CompositePackageDescr packageDescr : packages) {
setAssetFilter(packageDescr.getFilter());
PackageRegistry pkgRegistry = getPackageRegistry(packageDescr.getNamespace());
compileKnowledgePackages(packageDescr, pkgRegistry);
setAssetFilter(null);
}
}
use of org.drools.compiler.compiler.PackageRegistry in project drools by kiegroup.
the class DroolsJaxbHelperProviderImpl method addXsdModel.
public static String[] addXsdModel(Resource resource, KnowledgeBuilderImpl kBuilder, Options xjcOpts, String systemId) throws IOException {
InputSource source = new InputSource(new CachingRewindableReader(resource.getReader()));
source.setSystemId(systemId.trim().startsWith(".") ? systemId : "." + systemId);
xjcOpts.addGrammar(source);
try {
xjcOpts.parseArguments(new String[] { "-npa" });
} catch (BadCommandLineException e) {
throw new IllegalArgumentException("Unable to parse arguments", e);
}
ErrorReceiver errorReceiver = new JaxbErrorReceiver4Drools();
Model model = ModelLoader.load(xjcOpts, new JCodeModel(), errorReceiver);
model.generateCode(xjcOpts, errorReceiver);
MapVfsCodeWriter codeWriter = new MapVfsCodeWriter();
model.codeModel.build(xjcOpts.createCodeWriter(codeWriter));
MemoryResourceReader src = new MemoryResourceReader();
boolean useProjectClassLoader = kBuilder.getRootClassLoader() instanceof ProjectClassLoader;
List<String> classNames = new ArrayList<String>();
List<String> srcNames = new ArrayList<String>();
for (Entry<String, byte[]> entry : codeWriter.getMap().entrySet()) {
String name = entry.getKey();
int dotPos = name.lastIndexOf('.');
String pkgName = name.substring(0, dotPos);
if (!name.endsWith("package-info.java")) {
classNames.add(pkgName);
}
dotPos = pkgName.lastIndexOf('.');
if (dotPos != -1) {
pkgName = pkgName.substring(0, dotPos);
}
PackageRegistry pkgReg = kBuilder.getPackageRegistry(pkgName);
if (pkgReg == null) {
kBuilder.addPackage(new PackageDescr(pkgName));
pkgReg = kBuilder.getPackageRegistry(pkgName);
}
if (useProjectClassLoader) {
String srcName = convertToResource(entry.getKey());
src.add(srcName, entry.getValue());
srcNames.add(srcName);
} else {
JavaDialect dialect = (JavaDialect) pkgReg.getDialectCompiletimeRegistry().getDialect("java");
dialect.addSrc(convertToResource(entry.getKey()), entry.getValue());
}
}
if (useProjectClassLoader) {
ProjectJavaCompiler compiler = new ProjectJavaCompiler(kBuilder.getBuilderConfiguration());
List<KnowledgeBuilderResult> results = compiler.compileAll((ProjectClassLoader) kBuilder.getRootClassLoader(), srcNames, src);
for (String className : classNames) {
Class<?> clazz = null;
try {
clazz = Class.forName(className, true, kBuilder.getRootClassLoader());
} catch (ClassNotFoundException e) {
continue;
}
String pkgName = className.substring(0, className.lastIndexOf('.'));
PackageRegistry pkgReg = kBuilder.getPackageRegistry(pkgName);
pkgReg.getPackage().addTypeDeclaration(TypeDeclaration.createTypeDeclarationForBean(clazz));
}
kBuilder.updateResults(results);
} else {
kBuilder.compileAll();
kBuilder.updateResults();
}
return classNames.toArray(new String[classNames.size()]);
}
use of org.drools.compiler.compiler.PackageRegistry 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());
}
Aggregations