Search in sources :

Example 1 with KieBaseModelImpl

use of org.drools.compiler.kproject.models.KieBaseModelImpl in project drools by kiegroup.

the class KieContainerImpl method update.

private Results update(final InternalKieModule currentKM, final ReleaseId newReleaseId) {
    final InternalKieModule newKM = (InternalKieModule) kr.getKieModule(newReleaseId);
    if (newKM == null) {
        return null;
    }
    final KieJarChangeSet cs = currentKM.getChanges(newKM);
    List<String> modifiedClassNames = getModifiedClasses(cs);
    final boolean modifyingUsedClass = isModifyingUsedClass(modifiedClassNames, getClassLoader());
    final List<Class<?>> modifiedClasses = reinitModifiedClasses(newKM, modifiedClassNames, getClassLoader());
    final List<String> unchangedResources = getUnchangedResources(newKM, cs);
    Map<String, KieBaseModel> currentKieBaseModels = ((KieModuleKieProject) kProject).updateToModule(newKM);
    final ResultsImpl results = new ResultsImpl();
    List<String> kbasesToRemove = new ArrayList<String>();
    for (Entry<String, KieBase> kBaseEntry : kBases.entrySet()) {
        String kbaseName = kBaseEntry.getKey();
        KieBaseModelImpl newKieBaseModel = (KieBaseModelImpl) kProject.getKieBaseModel(kbaseName);
        KieBaseModelImpl currentKieBaseModel = (KieBaseModelImpl) currentKieBaseModels.get(kbaseName);
        // if a kbase no longer exists, just remove it from the cache
        if (newKieBaseModel == null) {
            // have to save for later removal to avoid iteration errors
            kbasesToRemove.add(kbaseName);
        } else {
            final InternalKnowledgeBase kBase = (InternalKnowledgeBase) kBaseEntry.getValue();
            KieBaseUpdateContext context = new KieBaseUpdateContext(kProject, kBase, currentKM, newReleaseId, newKM, cs, modifiedClasses, modifyingUsedClass, unchangedResources, results, newKieBaseModel, currentKieBaseModel);
            kBase.enqueueModification(currentKM.createKieBaseUpdater(context));
        }
    }
    for (String kbaseToRemove : kbasesToRemove) {
        kBases.remove(kbaseToRemove);
    }
    // remove sessions that no longer exist
    this.kSessions.entrySet().removeIf(ksession -> kProject.getKieSessionModel(ksession.getKey()) == null);
    this.statelessKSessions.entrySet().removeIf(ksession -> kProject.getKieSessionModel(ksession.getKey()) == null);
    return results;
}
Also used : KieJarChangeSet(org.drools.compiler.kie.util.KieJarChangeSet) ArrayList(java.util.ArrayList) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieBase(org.kie.api.KieBase) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 2 with KieBaseModelImpl

use of org.drools.compiler.kproject.models.KieBaseModelImpl in project drools by kiegroup.

the class KBaseConverter method marshal.

public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
    KieBaseModelImpl kBase = (KieBaseModelImpl) value;
    writer.addAttribute("name", kBase.getName());
    writer.addAttribute("default", Boolean.toString(kBase.isDefault()));
    if (kBase.getEventProcessingMode() != null) {
        writer.addAttribute("eventProcessingMode", kBase.getEventProcessingMode().getMode());
    }
    if (kBase.getEqualsBehavior() != null) {
        writer.addAttribute("equalsBehavior", kBase.getEqualsBehavior().toString().toLowerCase());
    }
    if (kBase.getMutability() != null) {
        writer.addAttribute("mutability", kBase.getMutability().toString().toLowerCase());
    }
    if (kBase.getDeclarativeAgenda() != null) {
        writer.addAttribute("declarativeAgenda", kBase.getDeclarativeAgenda().toString().toLowerCase());
    }
    if (kBase.getSequential() != null) {
        writer.addAttribute("sequential", kBase.getSequential() == YES ? "true" : "false");
    }
    if (kBase.getSessionsPool() != null) {
        writer.addAttribute("sessionsPool", "" + kBase.getSessionsPool().getSize());
    }
    if (kBase.getBetaRangeIndexOption() != null) {
        writer.addAttribute("betaRangeIndex", kBase.getBetaRangeIndexOption().toString().toLowerCase());
    }
    if (kBase.getScope() != null) {
        writer.addAttribute("scope", kBase.getScope());
    }
    if (!kBase.getPackages().isEmpty()) {
        StringBuilder buf = new StringBuilder();
        boolean first = true;
        for (String pkg : kBase.getPackages()) {
            if (first) {
                first = false;
            } else {
                buf.append(", ");
            }
            buf.append(pkg);
        }
        writer.addAttribute("packages", buf.toString());
    }
    if (!kBase.getIncludes().isEmpty()) {
        StringBuilder sb = new StringBuilder();
        boolean insertComma = false;
        for (String include : kBase.getIncludes()) {
            if (insertComma) {
                sb.append(", ");
            }
            sb.append(include);
            if (!insertComma) {
                insertComma = true;
            }
        }
        writer.addAttribute("includes", sb.toString());
    }
    for (RuleTemplateModel ruleTemplateModel : kBase.getRuleTemplates()) {
        writeObject(writer, context, "ruleTemplate", ruleTemplateModel);
    }
    for (KieSessionModel kSessionModel : kBase.getKieSessionModels().values()) {
        writeObject(writer, context, "ksession", kSessionModel);
    }
}
Also used : RuleTemplateModel(org.kie.api.builder.model.RuleTemplateModel) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) KieSessionModel(org.kie.api.builder.model.KieSessionModel)

Example 3 with KieBaseModelImpl

use of org.drools.compiler.kproject.models.KieBaseModelImpl in project drools by kiegroup.

the class KieModuleConverter method unmarshal.

public Object unmarshal(HierarchicalStreamReader reader, final UnmarshallingContext context) {
    final KieModuleModelImpl kModule = new KieModuleModelImpl();
    readNodes(reader, new AbstractXStreamConverter.NodeReader() {

        public void onNode(HierarchicalStreamReader reader, String name, String value) {
            if ("kbase".equals(name)) {
                KieBaseModelImpl kBaseModule = readObject(reader, context, KieBaseModelImpl.class);
                kModule.getRawKieBaseModels().put(kBaseModule.getName(), kBaseModule);
                kBaseModule.setKModule(kModule);
            } else if ("configuration".equals(name)) {
                kModule.setConfProps(readPropertyMap(reader, context));
            }
        }
    });
    return kModule;
}
Also used : HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) KieModuleModelImpl(org.drools.compiler.kproject.models.KieModuleModelImpl)

Example 4 with KieBaseModelImpl

use of org.drools.compiler.kproject.models.KieBaseModelImpl in project drools by kiegroup.

the class DMNRuntimeKBWrappingIKB method getListeners.

@Override
public List<DMNRuntimeEventListener> getListeners() {
    if (knowledgeBase != null && knowledgeBase.getKieContainer() instanceof KieContainerImpl) {
        KieBaseModelImpl kieBaseModel = (KieBaseModelImpl) ((KieContainerImpl) knowledgeBase.getKieContainer()).getKieProject().getKieBaseModel(knowledgeBase.getId());
        ChainedProperties cp = ChainedProperties.getChainedProperties(getRootClassLoader());
        Properties kmoduleProperties = new Properties();
        kmoduleProperties.putAll(kieBaseModel.getKModule().getConfigurationProperties());
        cp.addProperties(kmoduleProperties);
        Map<String, String> listenersMap = new HashMap<>();
        cp.mapStartsWith(listenersMap, DMNAssemblerService.DMN_RUNTIME_LISTENER_PREFIX, true);
        logger.debug("{}", listenersMap);
        return listenersMap.values().stream().map(this::loadEventListener).flatMap(o -> o.map(Stream::of).orElseGet(Stream::empty)).collect(Collectors.toList());
    } else {
        logger.warn("No DMNRuntime Listener can be provided, as created without a reference to KnowledgeBase");
    }
    return Collections.emptyList();
}
Also used : MsgUtil(org.kie.dmn.core.util.MsgUtil) DMNPackage(org.kie.dmn.api.core.DMNPackage) LoggerFactory(org.slf4j.LoggerFactory) ClassLoaderUtil(org.kie.dmn.feel.util.ClassLoaderUtil) HashMap(java.util.HashMap) ResourceType(org.kie.api.io.ResourceType) ArrayList(java.util.ArrayList) KieRuntimeFactory(org.kie.api.runtime.KieRuntimeFactory) DMNModel(org.kie.dmn.api.core.DMNModel) Map(java.util.Map) ResourceTypePackageRegistry(org.drools.core.definitions.ResourceTypePackageRegistry) KieBase(org.kie.api.KieBase) DMNAssemblerService(org.kie.dmn.core.assembler.DMNAssemblerService) DMNProfile(org.kie.dmn.core.compiler.DMNProfile) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) DMNRuntimeEventListener(org.kie.dmn.api.core.event.DMNRuntimeEventListener) Properties(java.util.Properties) Logger(org.slf4j.Logger) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) KieContainer(org.kie.api.runtime.KieContainer) ChainedProperties(org.kie.internal.utils.ChainedProperties) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) Msg(org.kie.dmn.core.util.Msg) Collections(java.util.Collections) HashMap(java.util.HashMap) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) ChainedProperties(org.kie.internal.utils.ChainedProperties) Stream(java.util.stream.Stream) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) Properties(java.util.Properties) ChainedProperties(org.kie.internal.utils.ChainedProperties)

Example 5 with KieBaseModelImpl

use of org.drools.compiler.kproject.models.KieBaseModelImpl in project drools by kiegroup.

the class CanonicalKieModule method findProcesses.

private List<Process> findProcesses(InternalKieModule kieModule, KieBaseModelImpl kBaseModel) {
    List<Process> processes = new ArrayList<>();
    Collection<KiePackage> pkgs = kieModule.getKnowledgePackagesForKieBase(kBaseModel.getName());
    if (pkgs == null) {
        List<Resource> processResources = kieModule.getFileNames().stream().filter(fileName -> {
            ResourceType resourceType = determineResourceType(fileName);
            return resourceType == ResourceType.DRF || resourceType == ResourceType.BPMN2;
        }).map(fileName -> {
            final Resource processResource = kieModule.getResource(fileName);
            processResource.setResourceType(determineResourceType(fileName));
            return processResource;
        }).collect(toList());
        if (!processResources.isEmpty()) {
            KnowledgeBuilderImpl kbuilder = (KnowledgeBuilderImpl) KnowledgeBuilderFactory.newKnowledgeBuilder(createBuilderConfiguration(kBaseModel, moduleClassLoader));
            for (Resource processResource : processResources) {
                kbuilder.add(processResource, processResource.getResourceType());
            }
            for (InternalKnowledgePackage knowledgePackage : kbuilder.getPackages()) {
                processes.addAll(knowledgePackage.getProcesses());
            }
        }
    } else {
        for (KiePackage pkg : pkgs) {
            processes.addAll(pkg.getProcesses());
        }
    }
    return processes;
}
Also used : InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KiePath(org.kie.memorycompiler.resources.KiePath) Arrays(java.util.Arrays) ResourceChange(org.kie.internal.builder.ResourceChange) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) ChangeType(org.kie.internal.builder.ChangeType) KieBaseUpdatersContext(org.drools.compiler.kie.builder.impl.KieBaseUpdatersContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) CoreComponentFactory(org.drools.core.reteoo.CoreComponentFactory) StringUtil.fileNameToClass(org.drools.modelcompiler.util.StringUtil.fileNameToClass) ReleaseId(org.kie.api.builder.ReleaseId) DependencyFilter(org.kie.util.maven.support.DependencyFilter) Collections.singletonList(java.util.Collections.singletonList) ModelComponent.areEqualInModel(org.drools.model.impl.ModelComponent.areEqualInModel) Map(java.util.Map) KieBaseUpdaterImplContext(org.drools.compiler.kie.builder.impl.KieBaseUpdaterImplContext) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) KieModuleModelImpl(org.drools.compiler.kproject.models.KieModuleModelImpl) Predicate(java.util.function.Predicate) KieProject(org.drools.compiler.kie.builder.impl.KieProject) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Process(org.kie.api.definition.process.Process) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) KieBaseUpdaters(org.drools.compiler.kie.builder.impl.KieBaseUpdaters) Set(java.util.Set) Results(org.kie.api.builder.Results) KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieJarChangeSet(org.drools.compiler.kie.util.KieJarChangeSet) Collectors(java.util.stream.Collectors) KieBaseUpdaterOptions(org.drools.compiler.kie.builder.impl.KieBaseUpdaterOptions) Drools(org.drools.core.util.Drools) CanonicalKieBaseUpdater(org.drools.modelcompiler.builder.CanonicalKieBaseUpdater) InvocationTargetException(java.lang.reflect.InvocationTargetException) ResourceConfiguration(org.kie.api.io.ResourceConfiguration) UncheckedIOException(java.io.UncheckedIOException) Resource(org.kie.api.io.Resource) List(java.util.List) Stream(java.util.stream.Stream) BuildContext(org.drools.compiler.kie.builder.impl.BuildContext) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) StringUtils(org.drools.core.util.StringUtils) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilderFactory(org.kie.internal.builder.KnowledgeBuilderFactory) PomModel(org.kie.util.maven.support.PomModel) ModelSourceClass.getProjectModelClassNameNameWithReleaseId(org.drools.modelcompiler.builder.ModelSourceClass.getProjectModelClassNameNameWithReleaseId) HashMap(java.util.HashMap) ResourceType(org.kie.api.io.ResourceType) AlphaNetworkCompilerOption(org.kie.internal.builder.conf.AlphaNetworkCompilerOption) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) IoUtils(org.drools.core.util.IoUtils) ResourceProvider(org.drools.wiring.api.ResourceProvider) FileKieModule(org.drools.compiler.kie.builder.impl.FileKieModule) KieBaseUpdater(org.drools.compiler.kie.builder.impl.KieBaseUpdater) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) NamedModelItem(org.drools.model.NamedModelItem) KieServices(org.kie.api.KieServices) Message(org.kie.api.builder.Message) KiePackage(org.kie.api.definition.KiePackage) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Model(org.drools.model.Model) Iterator(java.util.Iterator) InternalResource(org.drools.core.io.internal.InternalResource) KieService(org.kie.api.internal.utils.KieService) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) AbstractKieModule.checkStreamMode(org.drools.compiler.kie.builder.impl.AbstractKieModule.checkStreamMode) IOException(java.io.IOException) DroolsAssemblerContext(org.drools.compiler.builder.DroolsAssemblerContext) ResultsImpl(org.drools.compiler.kie.builder.impl.ResultsImpl) GeneratedFact(org.drools.core.factmodel.GeneratedFact) File(java.io.File) ZipKieModule(org.drools.compiler.kie.builder.impl.ZipKieModule) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) Collectors.toList(java.util.stream.Collectors.toList) KieModuleModel(org.kie.api.builder.model.KieModuleModel) ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) Comparator(java.util.Comparator) ResourceType.determineResourceType(org.kie.api.io.ResourceType.determineResourceType) Collections(java.util.Collections) InputStream(java.io.InputStream) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ArrayList(java.util.ArrayList) Resource(org.kie.api.io.Resource) InternalResource(org.drools.core.io.internal.InternalResource) Process(org.kie.api.definition.process.Process) ResourceType(org.kie.api.io.ResourceType) ResourceType.determineResourceType(org.kie.api.io.ResourceType.determineResourceType) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

KieBaseModelImpl (org.drools.compiler.kproject.models.KieBaseModelImpl)13 ArrayList (java.util.ArrayList)6 KieBase (org.kie.api.KieBase)6 List (java.util.List)5 KieBaseModel (org.kie.api.builder.model.KieBaseModel)5 KieServices (org.kie.api.KieServices)4 ReleaseId (org.kie.api.builder.ReleaseId)4 KieModuleModel (org.kie.api.builder.model.KieModuleModel)4 Collections (java.util.Collections)3 Map (java.util.Map)3 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)3 KieJarChangeSet (org.drools.compiler.kie.util.KieJarChangeSet)3 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)3 KieSessionModel (org.kie.api.builder.model.KieSessionModel)3 ResourceType (org.kie.api.io.ResourceType)3 HierarchicalStreamReader (com.thoughtworks.xstream.io.HierarchicalStreamReader)2 InputStream (java.io.InputStream)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2