use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class DMNWeaverService method merge.
@Override
public void merge(KieBase kieBase, KiePackage kiePkg, DMNPackage dmnpkg) {
Map<ResourceType, ResourceTypePackage> map = ((InternalKnowledgePackage) kiePkg).getResourceTypePackages();
DMNPackageImpl existing = (DMNPackageImpl) map.get(ResourceType.DMN);
if (existing == null) {
existing = new DMNPackageImpl(dmnpkg.getNamespace());
map.put(ResourceType.DMN, existing);
}
for (Map.Entry<String, DMNModel> entry : dmnpkg.getAllModels().entrySet()) {
existing.addModel(entry.getKey(), entry.getValue());
}
existing.addProfiles(((DMNPackageImpl) dmnpkg).getProfiles());
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class CanonicalKieBaseUpdater method run.
@Override
public void run() {
CanonicalKieModule oldKM = (CanonicalKieModule) ctx.currentKM;
CanonicalKieModule newKM = (CanonicalKieModule) ctx.newKM;
List<RuleImpl> rulesToBeRemoved;
List<RuleImpl> rulesToBeAdded;
if (ctx.modifyingUsedClass) {
// remove all ObjectTypeNodes for the modified classes
for (Class<?> cls : ctx.modifiedClasses) {
clearInstancesOfModifiedClass(cls);
}
rulesToBeRemoved = getAllRulesInKieBase(oldKM, ctx.currentKieBaseModel);
rulesToBeAdded = getAllRulesInKieBase(newKM, ctx.newKieBaseModel);
} else {
newKM.setModuleClassLoader(((CanonicalKieModule) ctx.currentKM).getModuleClassLoader());
CanonicalKiePackages newPkgs = newKM.getKiePackages(ctx.newKieBaseModel);
rulesToBeRemoved = new ArrayList<>();
rulesToBeAdded = new ArrayList<>();
for (ResourceChangeSet changeSet : ctx.cs.getChanges().values()) {
if (!isPackageInKieBase(ctx.newKieBaseModel, changeSet.getResourceName())) {
continue;
}
InternalKnowledgePackage oldKpkg = ctx.kBase.getPackage(changeSet.getResourceName());
InternalKnowledgePackage kpkg = (InternalKnowledgePackage) newPkgs.getKiePackage(changeSet.getResourceName());
for (ResourceChange change : changeSet.getChanges()) {
String changedItemName = change.getName();
if (change.getChangeType() == ChangeType.UPDATED || change.getChangeType() == ChangeType.REMOVED) {
if (change.getType() == ResourceChange.Type.GLOBAL) {
ctx.kBase.removeGlobal(changedItemName);
} else {
rulesToBeRemoved.add(oldKpkg.getRule(changedItemName));
}
}
if (change.getChangeType() == ChangeType.UPDATED || change.getChangeType() == ChangeType.ADDED) {
if (change.getType() == ResourceChange.Type.GLOBAL) {
try {
ctx.kBase.addGlobal(changedItemName, kpkg.getTypeResolver().resolveType(kpkg.getGlobals().get(changedItemName)));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
} else {
rulesToBeAdded.add(kpkg.getRule(changedItemName));
}
}
}
}
}
ctx.kBase.removeRules(rulesToBeRemoved);
ctx.kBase.addRules(rulesToBeAdded);
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class ModelBuilderImpl method registerTypeDeclarations.
private void registerTypeDeclarations(Collection<CompositePackageDescr> packages) {
for (CompositePackageDescr packageDescr : packages) {
InternalKnowledgePackage pkg = getOrCreatePackageRegistry(packageDescr).getPackage();
for (TypeDeclarationDescr typeDescr : packageDescr.getTypeDeclarations()) {
normalizeAnnotations(typeDescr, pkg.getTypeResolver(), false);
TypeDeclaration type = new TypeDeclaration(typeDescr.getTypeName());
type.setResource(typeDescr.getResource());
TypeDeclarationFactory.processAnnotations(typeDescr, type);
pkg.addTypeDeclaration(type);
}
}
}
use of org.drools.core.definitions.InternalKnowledgePackage 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.core.definitions.InternalKnowledgePackage 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);
}
}
Aggregations