Search in sources :

Example 11 with KieBaseModelImpl

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

the class CanonicalKieModule method createKiePackages.

private CanonicalKiePackages createKiePackages(KieProject kieProject, KieBaseModelImpl kBaseModel, BuildContext buildContext, KieBaseConfiguration conf) {
    Set<String> includes = kieProject == null ? Collections.emptySet() : kieProject.getTransitiveIncludes(kBaseModel);
    List<Process> processes = findProcesses(internalKieModule, kBaseModel);
    Collection<Model> modelsForKBase;
    if (includes.isEmpty()) {
        modelsForKBase = getModelForKBase(kBaseModel);
    } else {
        modelsForKBase = new ArrayList<>(getModelForKBase(kBaseModel));
        for (String include : includes) {
            if (StringUtils.isEmpty(include)) {
                continue;
            }
            InternalKieModule includeModule = kieProject.getKieModuleForKBase(include);
            if (includeModule == null) {
                String text = "Unable to build KieBase, could not find include: " + include;
                buildContext.getMessages().addMessage(Message.Level.ERROR, KieModuleModelImpl.KMODULE_SRC_PATH.asString(), text).setKieBaseName(kBaseModel.getName());
                continue;
            }
            if (!(includeModule instanceof CanonicalKieModule)) {
                String text = "It is not possible to mix drl based and executable model based projects. Found a drl project: " + include;
                buildContext.getMessages().addMessage(Message.Level.ERROR, KieModuleModelImpl.KMODULE_SRC_PATH.asString(), text).setKieBaseName(kBaseModel.getName());
                continue;
            }
            KieBaseModelImpl includeKBaseModel = (KieBaseModelImpl) kieProject.getKieBaseModel(include);
            CanonicalKieModule canonicalInclude = (CanonicalKieModule) includeModule;
            canonicalInclude.setModuleClassLoader((ProjectClassLoader) kieProject.getClassLoader());
            modelsForKBase.addAll(canonicalInclude.getModelForKBase(includeKBaseModel));
            processes.addAll(findProcesses(includeModule, includeKBaseModel));
        }
    }
    CanonicalKiePackages canonicalKiePkgs = new KiePackagesBuilder(conf, getBuilderConfiguration(kBaseModel), modelsForKBase).build();
    CanonicalKiePackages canonicalKiePackages = mergeProcesses(processes, canonicalKiePkgs);
    modelsForKBase.clear();
    this.models.clear();
    return canonicalKiePackages;
}
Also used : ModelComponent.areEqualInModel(org.drools.model.impl.ModelComponent.areEqualInModel) KieBaseModel(org.kie.api.builder.model.KieBaseModel) PomModel(org.kie.util.maven.support.PomModel) Model(org.drools.model.Model) KieModuleModel(org.kie.api.builder.model.KieModuleModel) Process(org.kie.api.definition.process.Process) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 12 with KieBaseModelImpl

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

the class KieBuilderTest method testKieModuleDependencies.

@Test
public void testKieModuleDependencies() throws ClassNotFoundException, InterruptedException, IOException {
    KieServices ks = KieServices.Factory.get();
    String namespace1 = "org.kie.test1";
    ReleaseId releaseId1 = KieServices.Factory.get().newReleaseId(namespace1, "memory", "1.0");
    KieModuleModel kProj1 = createKieProject(namespace1);
    KieFileSystem kfs1 = KieServices.Factory.get().newKieFileSystem();
    generateAll(kfs1, namespace1, releaseId1, kProj1);
    KieBuilder kb1 = createKieBuilder(kfs1);
    kb1.buildAll();
    if (kb1.getResults().hasMessages(Level.ERROR)) {
        fail("Unable to build KieJar\n" + kb1.getResults().toString());
    }
    KieRepository kr = ks.getRepository();
    KieModule kModule1 = kr.getKieModule(releaseId1);
    assertNotNull(kModule1);
    String namespace2 = "org.kie.test2";
    ReleaseId releaseId2 = KieServices.Factory.get().newReleaseId(namespace2, "memory", "1.0");
    KieModuleModel kProj2 = createKieProject(namespace2);
    KieBaseModelImpl kieBase2 = (KieBaseModelImpl) kProj2.getKieBaseModels().get(namespace2);
    kieBase2.addInclude(namespace1);
    KieFileSystem kfs2 = KieServices.Factory.get().newKieFileSystem();
    generateAll(kfs2, namespace2, releaseId2, kProj2);
    KieBuilder kb2 = createKieBuilder(kfs2);
    kb2.setDependencies(kModule1);
    kb2.buildAll();
    if (kb2.getResults().hasMessages(Level.ERROR)) {
        fail("Unable to build KieJar\n" + kb2.getResults().toString());
    }
    KieModule kModule2 = kr.getKieModule(releaseId2);
    assertNotNull(kModule2);
    KieContainer kContainer = ks.newKieContainer(releaseId2);
    KieBase kBase = kContainer.getKieBase(namespace2);
    KieSession kSession = kBase.newKieSession();
    List list = new ArrayList();
    kSession.setGlobal("list", list);
    kSession.fireAllRules();
    assertEquals(2, list.size());
    if ("org.kie.test1.Message".equals(list.get(0).getClass().getName())) {
        assertEquals("org.kie.test2.Message", list.get(1).getClass().getName());
    } else {
        assertEquals("org.kie.test2.Message", list.get(0).getClass().getName());
        assertEquals("org.kie.test1.Message", list.get(1).getClass().getName());
    }
}
Also used : KieRepository(org.kie.api.builder.KieRepository) KieFileSystem(org.kie.api.builder.KieFileSystem) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 13 with KieBaseModelImpl

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

the class KBaseConverter method unmarshal.

public Object unmarshal(HierarchicalStreamReader reader, final UnmarshallingContext context) {
    final KieBaseModelImpl kBase = new KieBaseModelImpl();
    String kbaseName = reader.getAttribute("name");
    kBase.setNameForUnmarshalling(kbaseName != null ? kbaseName : StringUtils.uuid());
    kBase.setDefault("true".equals(reader.getAttribute("default")));
    String eventMode = reader.getAttribute("eventProcessingMode");
    if (eventMode != null) {
        kBase.setEventProcessingMode(EventProcessingOption.determineEventProcessingMode(eventMode));
    }
    String equalsBehavior = reader.getAttribute("equalsBehavior");
    if (equalsBehavior != null) {
        kBase.setEqualsBehavior(EqualityBehaviorOption.determineEqualityBehavior(equalsBehavior));
    }
    String mutability = reader.getAttribute("mutability");
    if (mutability != null) {
        kBase.setMutability(KieBaseMutabilityOption.determineMutability(mutability));
    }
    String declarativeAgenda = reader.getAttribute("declarativeAgenda");
    if (declarativeAgenda != null) {
        kBase.setDeclarativeAgenda(DeclarativeAgendaOption.determineDeclarativeAgenda(declarativeAgenda));
    }
    String sequential = reader.getAttribute("sequential");
    if (sequential != null) {
        kBase.setSequential(SequentialOption.determineSequential(sequential));
    }
    String sessionsPool = reader.getAttribute("sessionsPool");
    if (sessionsPool != null) {
        kBase.setSessionsPool(SessionsPoolOption.get(Integer.parseInt(sessionsPool)));
    }
    String betaRangeIndex = reader.getAttribute("betaRangeIndex");
    if (betaRangeIndex != null) {
        kBase.setBetaRangeIndexOption(BetaRangeIndexOption.determineBetaRangeIndex(betaRangeIndex));
    }
    String scope = reader.getAttribute("scope");
    if (scope != null) {
        kBase.setScope(scope.trim());
    }
    String pkgs = reader.getAttribute("packages");
    if (pkgs != null) {
        for (String pkg : pkgs.split(",")) {
            kBase.addPackage(pkg.trim());
        }
    }
    String includes = reader.getAttribute("includes");
    if (includes != null) {
        for (String include : includes.split(",")) {
            kBase.addInclude(include.trim());
        }
    }
    readNodes(reader, new AbstractXStreamConverter.NodeReader() {

        public void onNode(HierarchicalStreamReader reader, String name, String value) {
            if ("ksession".equals(name)) {
                KieSessionModelImpl kSession = readObject(reader, context, KieSessionModelImpl.class);
                kBase.getRawKieSessionModels().put(kSession.getName(), kSession);
                kSession.setKBase(kBase);
            } else if ("ruleTemplate".equals(name)) {
                RuleTemplateModelImpl ruleTemplate = readObject(reader, context, RuleTemplateModelImpl.class);
                kBase.getRawRuleTemplates().add(ruleTemplate);
                ruleTemplate.setKBase(kBase);
            }
        // @TODO we don't use support nested includes
        // if ( "includes".equals( name ) ) {
        // for ( String include : readList( reader ) ) {
        // kBase.addInclude( include );
        // }
        // }
        }
    });
    return kBase;
}
Also used : KieSessionModelImpl(org.drools.compiler.kproject.models.KieSessionModelImpl) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) RuleTemplateModelImpl(org.drools.compiler.kproject.models.RuleTemplateModelImpl) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl)

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