use of org.drools.compiler.compiler.PackageRegistry in project drools by kiegroup.
the class ObjectTypeNodeCompiler method compile.
/**
* Creates a {@link CompiledNetwork} for the specified {@link ObjectTypeNode}. The {@link PackageBuilder} is used
* to compile the generated source and load the class.
*
* @param kBuilder builder used to compile and load class
* @param objectTypeNode OTN we are generating a compiled network for
* @return CompiledNetwork
*/
public static CompiledNetwork compile(KnowledgeBuilderImpl kBuilder, ObjectTypeNode objectTypeNode) {
if (objectTypeNode == null) {
throw new IllegalArgumentException("ObjectTypeNode cannot be null!");
}
if (kBuilder == null) {
throw new IllegalArgumentException("PackageBuilder cannot be null!");
}
ObjectTypeNodeCompiler compiler = new ObjectTypeNodeCompiler(objectTypeNode);
String packageName = compiler.getPackageName();
PackageRegistry pkgReg = kBuilder.getPackageRegistry(packageName);
if (pkgReg == null) {
kBuilder.addPackage(new PackageDescr(packageName));
pkgReg = kBuilder.getPackageRegistry(packageName);
}
String source = compiler.generateSource();
String generatedSourceName = compiler.getName();
JavaDialect dialect = (JavaDialect) pkgReg.getDialectCompiletimeRegistry().getDialect("java");
dialect.addSrc(compiler.getBinaryName(), source.getBytes(IoUtils.UTF8_CHARSET));
kBuilder.compileAll();
kBuilder.updateResults();
CompiledNetwork network;
try {
network = (CompiledNetwork) Class.forName(generatedSourceName, true, kBuilder.getRootClassLoader()).newInstance();
} catch (ClassNotFoundException e) {
throw new RuntimeException("This is a bug. Please contact the development team", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("This is a bug. Please contact the development team", e);
} catch (InstantiationException e) {
throw new RuntimeException("This is a bug. Please contact the development team", e);
}
return network;
}
use of org.drools.compiler.compiler.PackageRegistry in project drools by kiegroup.
the class DMNAssemblerService method compileResourceToModel.
private DMNModel compileResourceToModel(KnowledgeBuilderImpl kbuilderImpl, DMNCompiler dmnCompiler, Resource resource, Collection<DMNModel> dmnModels) {
DMNModel model = dmnCompiler.compile(resource, dmnModels);
if (model != null) {
String namespace = model.getNamespace();
PackageRegistry pkgReg = kbuilderImpl.getOrCreatePackageRegistry(new PackageDescr(namespace));
InternalKnowledgePackage kpkgs = pkgReg.getPackage();
kpkgs.addCloningResource(DMN_COMPILER_CACHE_KEY, dmnCompiler);
Map<ResourceType, ResourceTypePackage> rpkg = kpkgs.getResourceTypePackages();
DMNPackageImpl dmnpkg = (DMNPackageImpl) rpkg.get(ResourceType.DMN);
if (dmnpkg == null) {
dmnpkg = new DMNPackageImpl(namespace);
rpkg.put(ResourceType.DMN, dmnpkg);
} else {
if (dmnpkg.getModel(model.getName()) != null) {
kbuilderImpl.addBuilderResult(new DMNKnowledgeBuilderError(ResultSeverity.ERROR, resource, namespace, "Duplicate model name " + model.getName() + " in namespace " + namespace));
logger.error("Duplicate model name {} in namespace {}", model.getName(), namespace);
}
}
dmnpkg.addModel(model.getName(), model);
dmnpkg.addProfiles(kbuilderImpl.getCachedOrCreate(DMN_PROFILES_CACHE_KEY, () -> getDMNProfiles(kbuilderImpl)));
} else {
kbuilderImpl.addBuilderResult(new DMNKnowledgeBuilderError(ResultSeverity.ERROR, resource, "Unable to compile DMN model for the resource"));
logger.error("Unable to compile DMN model for resource {}", resource.getSourcePath());
}
return model;
}
use of org.drools.compiler.compiler.PackageRegistry in project drools by kiegroup.
the class ClassHierarchyManager method mergeFields.
protected void mergeFields(String simpleSuperTypeName, String superTypePackageName, String fullSuper, TypeDeclarationDescr typeDescr, List<TypeDefinition> unresolvedTypes, Map<String, AbstractClassTypeDeclarationDescr> unprocessableDescrs, TypeResolver resolver) {
Map<String, TypeFieldDescr> fieldMap = new LinkedHashMap<String, TypeFieldDescr>();
boolean isNovel = TypeDeclarationUtils.isNovelClass(typeDescr, kbuilder.getPackageRegistry(typeDescr.getNamespace()));
PackageRegistry registry = kbuilder.getPackageRegistry(superTypePackageName);
InternalKnowledgePackage pack = null;
if (registry != null) {
pack = registry.getPackage();
}
if (unprocessableDescrs.containsKey(fullSuper)) {
unprocessableDescrs.put(typeDescr.getType().getFullName(), typeDescr);
return;
}
// if a class is declared in DRL, its package can't be null? The default package is replaced by "defaultpkg"
boolean isSuperClassTagged = false;
// in the same package, or in a previous one
boolean isSuperClassDeclared = true;
if (pack != null) {
// look for the supertype declaration in available packages
TypeDeclaration superTypeDeclaration = pack.getTypeDeclaration(simpleSuperTypeName);
if (superTypeDeclaration != null && superTypeDeclaration.getTypeClassDef() != null) {
ClassDefinition classDef = superTypeDeclaration.getTypeClassDef();
// inherit fields
for (org.kie.api.definition.type.FactField fld : classDef.getFields()) {
TypeFieldDescr inheritedFlDescr = buildInheritedFieldDescrFromDefinition(fld, typeDescr);
fieldMap.put(inheritedFlDescr.getFieldName(), inheritedFlDescr);
}
// new classes are already distinguished from tagged external classes
isSuperClassTagged = !superTypeDeclaration.isNovel();
}
} else {
isSuperClassDeclared = false;
}
// look for the class externally
if (!isSuperClassDeclared || isSuperClassTagged) {
try {
Class superKlass;
if (registry != null) {
superKlass = registry.getTypeResolver().resolveType(fullSuper);
} else {
// if the supertype has not been declared, and we have got so far, it means that this class is not novel
superKlass = resolver.resolveType(fullSuper);
}
buildDescrsFromFields(superKlass, typeDescr, registry, fieldMap);
} catch (ClassNotFoundException cnfe) {
unprocessableDescrs.put(typeDescr.getType().getFullName(), typeDescr);
return;
}
}
// notice that it is not possible to override a field changing its type
for (String fieldName : typeDescr.getFields().keySet()) {
if (fieldMap.containsKey(fieldName)) {
String type1 = fieldMap.get(fieldName).getPattern().getObjectType();
String type2 = typeDescr.getFields().get(fieldName).getPattern().getObjectType();
if (type2.lastIndexOf(".") < 0) {
try {
TypeResolver typeResolver = kbuilder.getPackageRegistry(typeDescr.getNamespace()).getTypeResolver();
type1 = typeResolver.resolveType(type1).getName();
type2 = typeResolver.resolveType(type2).getName();
// now that we are at it... this will be needed later anyway
fieldMap.get(fieldName).getPattern().setObjectType(type1);
typeDescr.getFields().get(fieldName).getPattern().setObjectType(type2);
} catch (ClassNotFoundException cnfe) {
// will fail later
}
}
boolean clash = !type1.equals(type2);
TypeFieldDescr overriding = null;
if (clash) {
// this may still be an override using a subclass of the original type
try {
Class<?> sup = resolver.resolveType(type1);
Class<?> loc = resolver.resolveType(type2);
if (sup.isAssignableFrom(loc)) {
clash = false;
// mark as non inherited so that a new field is actually built
overriding = fieldMap.get(fieldName);
}
} catch (ClassNotFoundException cnfe) {
// not much to do
}
}
if (clash) {
kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Cannot redeclare field '" + fieldName + " from " + type1 + " to " + type2));
typeDescr.setType(null, null);
return;
} else {
String initVal = fieldMap.get(fieldName).getInitExpr();
TypeFieldDescr fd = typeDescr.getFields().get(fieldName);
if (fd.getInitExpr() == null) {
fd.setInitExpr(initVal);
}
fd.setInherited(fieldMap.get(fieldName).isInherited());
fd.setOverriding(overriding);
for (String key : fieldMap.get(fieldName).getAnnotationNames()) {
if (fd.getAnnotation(key) == null) {
fd.addAnnotation(fieldMap.get(fieldName).getAnnotation(key));
}
}
if (fd.getIndex() < 0) {
fd.setIndex(fieldMap.get(fieldName).getIndex());
}
}
}
fieldMap.put(fieldName, typeDescr.getFields().get(fieldName));
}
typeDescr.setFields(fieldMap);
}
use of org.drools.compiler.compiler.PackageRegistry in project drools by kiegroup.
the class KnowledgeBuilderImpl method processKieBaseTypes.
protected void processKieBaseTypes() {
if (!hasErrors() && this.kBase != null) {
List<InternalKnowledgePackage> pkgs = new ArrayList<>();
for (PackageRegistry pkgReg : pkgRegistryMap.values()) {
pkgs.add(pkgReg.getPackage());
}
this.kBase.processAllTypesDeclaration(pkgs);
}
}
use of org.drools.compiler.compiler.PackageRegistry in project drools by kiegroup.
the class KnowledgeBuilderImpl method compileRulesLevel.
private void compileRulesLevel(PackageDescr packageDescr, PackageRegistry pkgRegistry, List<RuleDescr> rules) {
boolean parallelRulesBuild = this.kBase == null && rules.size() > PARALLEL_RULES_BUILD_THRESHOLD;
if (parallelRulesBuild) {
Map<String, RuleBuildContext> ruleCxts = new ConcurrentHashMap<>();
rules.stream().parallel().filter(ruleDescr -> filterAccepts(ResourceChange.Type.RULE, ruleDescr.getNamespace(), ruleDescr.getName())).forEach(ruleDescr -> {
initRuleDescr(packageDescr, pkgRegistry, ruleDescr);
RuleBuildContext context = buildRuleBuilderContext(pkgRegistry, ruleDescr);
ruleCxts.put(ruleDescr.getName(), context);
List<? extends KnowledgeBuilderResult> results = addRule(context);
if (!results.isEmpty()) {
synchronized (this.results) {
this.results.addAll(results);
}
}
});
for (RuleDescr ruleDescr : rules) {
RuleBuildContext context = ruleCxts.get(ruleDescr.getName());
if (context != null) {
pkgRegistry.getPackage().addRule(context.getRule());
}
}
} else {
for (RuleDescr ruleDescr : rules) {
if (filterAccepts(ResourceChange.Type.RULE, ruleDescr.getNamespace(), ruleDescr.getName())) {
initRuleDescr(packageDescr, pkgRegistry, ruleDescr);
RuleBuildContext context = buildRuleBuilderContext(pkgRegistry, ruleDescr);
this.results.addAll(addRule(context));
pkgRegistry.getPackage().addRule(context.getRule());
}
}
}
}
Aggregations