use of org.drools.core.rule.TypeDeclaration in project drools by kiegroup.
the class KieMetaInfoBuilder method generateKieModuleMetaInfo.
public KieModuleMetaInfo generateKieModuleMetaInfo(ResourceStore trgMfs) {
// TODO: I think this method is wrong because it is only inspecting packages that are included
// in at least one kbase, but I believe it should inspect all packages, even if not included in
// any kbase, as they could be included in the future
Map<String, TypeMetaInfo> typeInfos = new HashMap<String, TypeMetaInfo>();
Map<String, Set<String>> rulesPerPackage = new HashMap<String, Set<String>>();
KieModuleModel kieModuleModel = kModule.getKieModuleModel();
for (String kieBaseName : kieModuleModel.getKieBaseModels().keySet()) {
KnowledgeBuilderImpl kBuilder = (KnowledgeBuilderImpl) kModule.getKnowledgeBuilderForKieBase(kieBaseName);
Map<String, PackageRegistry> pkgRegistryMap = kBuilder.getPackageRegistry();
KieModuleCache.KModuleCache.Builder _kmoduleCacheBuilder = createCacheBuilder();
KieModuleCache.CompilationData.Builder _compData = createCompilationData();
for (KiePackage kPkg : kBuilder.getKnowledgePackages()) {
PackageRegistry pkgRegistry = pkgRegistryMap.get(kPkg.getName());
JavaDialectRuntimeData runtimeData = (JavaDialectRuntimeData) pkgRegistry.getDialectRuntimeRegistry().getDialectData("java");
List<String> types = new ArrayList<String>();
for (FactType factType : kPkg.getFactTypes()) {
Class<?> typeClass = ((ClassDefinition) factType).getDefinedClass();
TypeDeclaration typeDeclaration = pkgRegistry.getPackage().getTypeDeclaration(typeClass);
if (typeDeclaration != null) {
typeInfos.put(typeClass.getName(), new TypeMetaInfo(typeDeclaration));
}
String className = factType.getName();
String internalName = className.replace('.', '/') + ".class";
if (trgMfs != null) {
byte[] bytes = runtimeData.getBytecode(internalName);
if (bytes != null) {
trgMfs.write(internalName, bytes, true);
}
}
types.add(internalName);
}
Set<String> rules = rulesPerPackage.get(kPkg.getName());
if (rules == null) {
rules = new HashSet<String>();
}
for (Rule rule : kPkg.getRules()) {
if (!rules.contains(rule.getName())) {
rules.add(rule.getName());
}
}
if (!rules.isEmpty()) {
rulesPerPackage.put(kPkg.getName(), rules);
}
addToCompilationData(_compData, runtimeData, types);
}
_kmoduleCacheBuilder.addCompilationData(_compData.build());
if (trgMfs != null) {
writeCompilationDataToTrg(_kmoduleCacheBuilder.build(), kieBaseName, trgMfs);
}
}
return new KieModuleMetaInfo(typeInfos, rulesPerPackage);
}
use of org.drools.core.rule.TypeDeclaration in project drools by kiegroup.
the class TypeDeclarationCache method getAndRegisterTypeDeclaration.
public TypeDeclaration getAndRegisterTypeDeclaration(Class<?> cls, String packageName) {
if (cls.isPrimitive() || cls.isArray()) {
return null;
}
TypeDeclaration typeDeclaration = getCachedTypeDeclaration(cls);
if (typeDeclaration != null) {
registerTypeDeclaration(packageName, typeDeclaration);
return typeDeclaration;
}
typeDeclaration = getExistingTypeDeclaration(cls);
if (typeDeclaration != null) {
initTypeDeclaration(cls, typeDeclaration);
return typeDeclaration;
}
typeDeclaration = createTypeDeclarationForBean(cls);
initTypeDeclaration(cls, typeDeclaration);
registerTypeDeclaration(packageName, typeDeclaration);
return typeDeclaration;
}
use of org.drools.core.rule.TypeDeclaration in project drools by kiegroup.
the class TypeDeclarationCache method initTypeDeclaration.
private void initTypeDeclaration(Class<?> cls, TypeDeclaration typeDeclaration) {
ClassDefinition clsDef = typeDeclaration.getTypeClassDef();
if (clsDef == null) {
clsDef = setClassDefinitionOnTypeDeclaration(cls, typeDeclaration);
} else {
processFieldsPosition(cls, clsDef, typeDeclaration);
}
if (typeDeclaration.isPropertyReactive()) {
processModifiedProps(cls, clsDef);
}
// build up a set of all the super classes and interfaces
Set<TypeDeclaration> tdecls = new LinkedHashSet<TypeDeclaration>();
tdecls.add(typeDeclaration);
buildTypeDeclarations(cls, tdecls);
// Iterate and for each typedeclr assign it's value if it's not already set
// We start from the rear as those are the furthest away classes and interfaces
TypeDeclaration[] tarray = tdecls.toArray(new TypeDeclaration[tdecls.size()]);
for (int i = tarray.length - 1; i >= 0; i--) {
TypeDeclaration currentTDecl = tarray[i];
if (!isSet(typeDeclaration.getSetMask(), TypeDeclaration.ROLE_BIT) && isSet(currentTDecl.getSetMask(), TypeDeclaration.ROLE_BIT)) {
typeDeclaration.setRole(currentTDecl.getRole());
}
if (!isSet(typeDeclaration.getSetMask(), TypeDeclaration.FORMAT_BIT) && isSet(currentTDecl.getSetMask(), TypeDeclaration.FORMAT_BIT)) {
typeDeclaration.setFormat(currentTDecl.getFormat());
}
if (!isSet(typeDeclaration.getSetMask(), TypeDeclaration.TYPESAFE_BIT) && isSet(currentTDecl.getSetMask(), TypeDeclaration.TYPESAFE_BIT)) {
typeDeclaration.setTypesafe(currentTDecl.isTypesafe());
}
}
this.cacheTypes.put(cls.getName(), typeDeclaration);
}
use of org.drools.core.rule.TypeDeclaration in project drools by kiegroup.
the class TypeDeclarationCache method initBuiltinTypeDeclaration.
private TypeDeclaration initBuiltinTypeDeclaration(Class<?> cls) {
TypeDeclaration type = new TypeDeclaration(cls.getSimpleName());
type.setTypesafe(false);
type.setTypeClass(cls);
setClassDefinitionOnTypeDeclaration(cls, type);
cacheTypes.put(cls.getCanonicalName(), type);
return type;
}
use of org.drools.core.rule.TypeDeclaration in project drools by kiegroup.
the class TypeDeclarationCache method buildTypeDeclarationInterfaces.
private boolean buildTypeDeclarationInterfaces(Class cls, Set<TypeDeclaration> tdecls) {
PackageRegistry pkgReg;
TypeDeclaration tdecl = this.cacheTypes.get((cls.getName()));
if (tdecl == null) {
pkgReg = kbuilder.getPackageRegistry(ClassUtils.getPackage(cls));
if (pkgReg != null) {
tdecl = pkgReg.getPackage().getTypeDeclaration(cls.getSimpleName());
}
}
if (tdecl != null) {
if (!tdecls.add(tdecl)) {
// the interface already exists, return to stop recursion
return false;
}
}
Class<?>[] intfs = cls.getInterfaces();
for (Class<?> intf : intfs) {
pkgReg = kbuilder.getPackageRegistry(ClassUtils.getPackage(intf));
if (pkgReg != null) {
tdecl = pkgReg.getPackage().getTypeDeclaration(intf.getSimpleName());
}
if (tdecl != null) {
tdecls.add(tdecl);
}
}
for (Class<?> intf : intfs) {
if (!buildTypeDeclarationInterfaces(intf, tdecls)) {
return false;
}
}
return true;
}
Aggregations