All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.drools.project.model.ProjectRuntime Maven / Gradle / Ivy

There is a newer version: 1.7.0.Final
Show newest version
/*
 * Copyright 2021 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.drools.project.model;

import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.kie.kogito.rules.KieRuntimeBuilder;
import org.drools.modelcompiler.builder.KieBaseBuilder;

@org.springframework.stereotype.Component()
public class ProjectRuntime implements KieRuntimeBuilder {

    private static final ProjectModel model = new ProjectModel();

    private static final java.util.Map kbases = initKieBases();

    public static final ProjectRuntime INSTANCE = new ProjectRuntime();

    private static java.util.Map initKieBases() {
        java.util.Map kbaseMap = new java.util.HashMap<>();
        if (org.kie.kogito.internal.RuntimeEnvironment.isNative()) {
            kbaseMap.put("defaultKieBase", KieBaseBuilder.createKieBaseFromModel(model.getModelsForKieBase("defaultKieBase"), model.getKieModuleModel().getKieBaseModels().get("defaultKieBase")));
        }
        return kbaseMap;
    }

    @Override
    public KieBase getKieBase() {
        return getKieBase("defaultKieBase");
    }

    @Override
    public KieBase getKieBase(String name) {
        return kbases.computeIfAbsent(name, n -> KieBaseBuilder.createKieBaseFromModel(model.getModelsForKieBase(n), model.getKieModuleModel().getKieBaseModels().get(n)));
    }

    @Override
    public KieSession newKieSession() {
        return newKieSession("defaultKieSession");
    }

    @Override
    public KieSession newKieSession(String sessionName) {
        return newKieSession(sessionName, new org.drools.core.config.StaticRuleConfig(new org.drools.core.config.DefaultRuleEventListenerConfig()));
    }

    @Override
    public KieSession newKieSession(String sessionName, org.kie.kogito.rules.RuleConfig ruleConfig) {
        KieBase kbase = getKieBaseForSession(sessionName);
        if (kbase == null) {
            throw new RuntimeException("Unknown KieSession with name '" + sessionName + "'");
        }
        KieSession ksession = kbase.newKieSession(getConfForSession(sessionName), null);
        ruleConfig.ruleEventListeners().agendaListeners().forEach(ksession::addEventListener);
        ruleConfig.ruleEventListeners().ruleRuntimeListeners().forEach(ksession::addEventListener);
        return ksession;
    }

    private KieBase getKieBaseForSession(String sessionName) {
        switch(sessionName) {
            case "defaultStatelessKieSession":
                return getKieBase("defaultKieBase");
            case "defaultKieSession":
                return getKieBase("defaultKieBase");
        }
        return null;
    }

    private org.kie.api.runtime.KieSessionConfiguration getConfForSession(String sessionName) {
        org.drools.core.SessionConfigurationImpl conf = new org.drools.core.SessionConfigurationImpl();
        switch(sessionName) {
            case "defaultStatelessKieSession":
                {
                    conf.setOption(org.kie.api.runtime.conf.ClockTypeOption.get("realtime"));
                }
                break;
            case "defaultKieSession":
                {
                    conf.setOption(org.kie.api.runtime.conf.ClockTypeOption.get("realtime"));
                }
                break;
        }
        return conf;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy