Search in sources :

Example 11 with KieBaseModel

use of org.kie.api.builder.model.KieBaseModel in project drools by kiegroup.

the class KieModuleModelImpl method newKieBaseModel.

/* (non-Javadoc)
     * @see org.kie.kModule.KieProject#addKBase(org.kie.kModule.KieBaseModelImpl)
     */
public KieBaseModel newKieBaseModel(String name) {
    KieBaseModel kbase = new KieBaseModelImpl(this, name);
    Map<String, KieBaseModel> newMap = new HashMap<String, KieBaseModel>();
    newMap.putAll(this.kBases);
    newMap.put(kbase.getName(), kbase);
    setKBases(newMap);
    return kbase;
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) HashMap(java.util.HashMap)

Example 12 with KieBaseModel

use of org.kie.api.builder.model.KieBaseModel in project drools by kiegroup.

the class KieCDIExtension method addKBaseBean.

public void addKBaseBean(AfterBeanDiscovery abd, KieCDIEntry entry) {
    ReleaseId releaseId = entry.getReleaseId();
    // default to classpath, but allow it to be overridden
    KieContainerImpl kieContainer = classpathKContainer;
    if (releaseId != null) {
        kieContainer = (KieContainerImpl) gavs.get(releaseId);
        if (kieContainer == null) {
            log.error("Unable to create @KBase({}), could not retrieve KieContainer for ReleaseId {}", entry.getValue(), releaseId.toString());
            return;
        }
    }
    KieProject kProject = kieContainer.getKieProject();
    KieBaseModel kBaseModel = null;
    String kBaseQName = entry.getValue();
    if (StringUtils.isEmpty(kBaseQName)) {
        kBaseModel = kProject.getDefaultKieBaseModel();
    } else {
        kBaseModel = kProject.getKieBaseModel(kBaseQName);
    }
    if (kBaseModel == null) {
        log.error("Annotation @KBase({}) found, but no KieBaseModel exists.\nEither the required kmodule.xml does not exist, is corrupted, or is missing the KieBase entry", kBaseQName);
        return;
    }
    if (!kBaseModel.getScope().trim().equals(entry.getScope().getClass().getName())) {
        try {
            if (kBaseModel.getScope().indexOf('.') >= 0) {
                entry.setScope((Class<? extends Annotation>) Class.forName(kBaseModel.getScope()));
            } else {
                entry.setScope((Class<? extends Annotation>) Class.forName("javax.enterprise.context." + kBaseModel.getScope()));
            }
        } catch (ClassNotFoundException e) {
            log.error("KieBaseModule {} overrides default annotation, but it was not able to find it {}\n{}", new String[] { kBaseQName, kBaseModel.getScope(), e.getMessage() });
        }
    }
    KBaseBean bean = new KBaseBean(kBaseQName, kBaseModel, kieContainer, entry.getKReleaseId(), entry.getScope(), entry.getName(), entry.getInjectionPoints());
    if (log.isDebugEnabled()) {
        InternalKieModule kModule = kProject.getKieModuleForKBase(kBaseQName);
        log.debug("Added Bean for @KBase({})", kBaseQName, kModule);
    }
    abd.addBean(bean);
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieProject(org.drools.compiler.kie.builder.impl.KieProject) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) KReleaseId(org.kie.api.cdi.KReleaseId) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 13 with KieBaseModel

use of org.kie.api.builder.model.KieBaseModel in project drools by kiegroup.

the class AbstractKnowledgeTest method createKieModule.

public KieModuleModel createKieModule(String namespace, boolean createJar, String version) throws IOException, ClassNotFoundException, InterruptedException {
    KieModuleModel kproj = new KieModuleModelImpl();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel(namespace + ".KBase1").setEqualsBehavior(EqualityBehaviorOption.EQUALITY).setEventProcessingMode(EventProcessingOption.STREAM).addPackage(namespace + ".KBase1").setDefault(true);
    kieBaseModel1.newKieSessionModel(namespace + ".KSession1").setType(KieSessionType.STATELESS).setClockType(ClockTypeOption.get("realtime")).setDefault(true);
    kieBaseModel1.newKieSessionModel(namespace + ".KSession2").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo"));
    kieBaseModel1.newKieSessionModel(namespace + ".KSessionDefault").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo")).setDefault(true);
    KieBaseModel kieBaseModel2 = kproj.newKieBaseModel(namespace + ".KBase2").setEqualsBehavior(EqualityBehaviorOption.IDENTITY).addPackage(namespace + ".KBase2").setEventProcessingMode(EventProcessingOption.CLOUD);
    kieBaseModel2.newKieSessionModel(namespace + ".KSession3").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo"));
    KieBaseModel kieBaseModel3 = kproj.newKieBaseModel(namespace + ".KBase3").addInclude(kieBaseModel1.getName()).addInclude(kieBaseModel2.getName()).setEqualsBehavior(EqualityBehaviorOption.IDENTITY).setEventProcessingMode(EventProcessingOption.CLOUD);
    kieBaseModel3.newKieSessionModel(namespace + ".KSession4").setType(KieSessionType.STATELESS).setClockType(ClockTypeOption.get("pseudo"));
    KieServices ks = KieServices.Factory.get();
    KieFileSystemImpl kfs = (KieFileSystemImpl) ks.newKieFileSystem();
    kfs.write("src/main/resources/META-INF/beans.xml", generateBeansXML());
    kfs.writeKModuleXML(((KieModuleModelImpl) kproj).toXML());
    ReleaseId releaseId = ks.newReleaseId(namespace, "art1", version);
    kfs.generateAndWritePomXML(releaseId);
    String kBase1R1 = getRule(namespace + ".test1", "rule1", version);
    String kBase1R2 = getRule(namespace + ".test1", "rule2", version);
    String kbase2R1 = getRule(namespace + ".test2", "rule1", version);
    String kbase2R2 = getRule(namespace + ".test2", "rule2", version);
    String fldKB1 = "src/main/resources/" + kieBaseModel1.getName().replace('.', '/');
    String fldKB2 = "src/main/resources/" + kieBaseModel2.getName().replace('.', '/');
    kfs.write(fldKB1 + "/rule1.drl", kBase1R1.getBytes());
    kfs.write(fldKB1 + "/rule2.drl", kBase1R2.getBytes());
    kfs.write(fldKB2 + "/rule1.drl", kbase2R1.getBytes());
    kfs.write(fldKB2 + "/rule2.drl", kbase2R2.getBytes());
    kfs.write("src/main/java/org/drools/compiler/cdi/test/KProjectTestClass" + namespace + ".java", generateKProjectTestClass(kproj, namespace));
    KieBuilder kBuilder = ks.newKieBuilder(kfs);
    kBuilder.buildAll();
    if (kBuilder.getResults().hasMessages(Level.ERROR)) {
        fail("should not have errors" + kBuilder.getResults());
    }
    MemoryKieModule kieModule = (MemoryKieModule) kBuilder.getKieModule();
    MemoryFileSystem trgMfs = kieModule.getMemoryFileSystem();
    if (createJar) {
        trgMfs.writeAsJar(fileManager.getRootDirectory(), namespace + "-" + version);
    } else {
        java.io.File file = fileManager.newFile(namespace + "-" + version);
        trgMfs.writeAsFs(file);
    }
    return kproj;
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystemImpl(org.drools.compiler.kie.builder.impl.KieFileSystemImpl) KieModuleModel(org.kie.api.builder.model.KieModuleModel) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) KieModuleModelImpl(org.drools.compiler.kproject.models.KieModuleModelImpl)

Example 14 with KieBaseModel

use of org.kie.api.builder.model.KieBaseModel in project drools by kiegroup.

the class SimulateTestBase method createKJarWithMultipleResources.

public static ReleaseId createKJarWithMultipleResources(String id, String[] resources, ResourceType[] types) {
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    KieFileSystem kfs = ks.newKieFileSystem();
    for (int i = 0; i < resources.length; i++) {
        String res = resources[i];
        String type = types[i].getDefaultExtension();
        kfs.write("src/main/resources/" + id.replaceAll("\\.", "/") + "/org/test/res" + i + "." + type, res);
    }
    KieBaseModel kBase1 = kproj.newKieBaseModel(id).setEqualsBehavior(EqualityBehaviorOption.EQUALITY).setEventProcessingMode(EventProcessingOption.STREAM);
    KieSessionModel ksession1 = kBase1.newKieSessionModel(id + ".KSession1").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo"));
    kfs.writeKModuleXML(kproj.toXML());
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    assertTrue(kieBuilder.getResults().getMessages().isEmpty());
    KieModule kieModule = kieBuilder.getKieModule();
    return kieModule.getReleaseId();
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) KieModule(org.kie.api.builder.KieModule) KieSessionModel(org.kie.api.builder.model.KieSessionModel)

Example 15 with KieBaseModel

use of org.kie.api.builder.model.KieBaseModel in project drools by kiegroup.

the class NegativePatternsTest method prepareKieSession.

@Before
public void prepareKieSession() {
    String drl = "package org.drools.compiler.integrationtests\n" + "\n" + "import org.drools.compiler.integrationtests.NegativePatternsTest.TestEvent\n" + "\n" + "declare TestEvent\n" + "    @role( event )\n" + "    @expires( 22ms )\n" + "end\n" + "\n" + "rule \"SingleAbsence\"\n" + "    duration( 18ms )\n" + "    when\n" + "        not ( TestEvent ( ) from entry-point EventStream )\n" + "    then\n" + "        // consequence\n" + "end\n" + "\n" + "rule \"SingleConstrained\"\n" + "    duration( 18ms )\n" + "    when\n" + "        not ( TestEvent ( name == \"EventA\" ) from entry-point EventStream )\n" + "    then\n" + "        // consequence\n" + "end\n" + "\n" + "rule \"MultipleEvents\"\n" + "    duration( 18ms )\n" + "    when\n" + "        TestEvent ( name == \"EventA\" ) over window:time( 22ms ) from entry-point EventStream\n" + "        not ( TestEvent ( name == \"EventB\" ) over window:time( 22ms )  from entry-point EventStream )\n" + "    then\n" + "        // consequence\n" + "end\n" + "\n" + "rule \"MultipleEntryPoints\"\n" + "    duration( 18ms )\n" + "    when\n" + "        not (\n" + "            TestEvent( name == \"EventA\" ) from entry-point EventStream\n" + "        )\n" + "        not (\n" + "            TestEvent( name == \"EventB\" ) from entry-point OtherStream\n" + "        )\n" + "    then\n" + "        // consequence\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kieModule = ks.newKieModuleModel();
    KieBaseModel defaultBase = kieModule.newKieBaseModel("defaultKBase").setDefault(true).addPackage("*").setEventProcessingMode(EventProcessingOption.STREAM);
    defaultBase.newKieSessionModel(DEFAULT_SESSION_NAME).setClockType(ClockTypeOption.get("pseudo")).setDefault(true);
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
    kfs.writeKModuleXML(kieModule.toXML());
    KieModule builtModule = ks.newKieBuilder(kfs).buildAll().getKieModule();
    ks.getRepository().addKieModule(builtModule);
    ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession(DEFAULT_SESSION_NAME);
    firedRulesListener = new TrackingAgendaEventListener();
    ksession.addEventListener(firedRulesListener);
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) KieModule(org.kie.api.builder.KieModule) Before(org.junit.Before)

Aggregations

KieBaseModel (org.kie.api.builder.model.KieBaseModel)74 KieModuleModel (org.kie.api.builder.model.KieModuleModel)55 KieServices (org.kie.api.KieServices)45 KieFileSystem (org.kie.api.builder.KieFileSystem)31 Test (org.junit.Test)26 KieSessionModel (org.kie.api.builder.model.KieSessionModel)24 KieBuilder (org.kie.api.builder.KieBuilder)22 KieContainer (org.kie.api.runtime.KieContainer)22 ReleaseId (org.kie.api.builder.ReleaseId)20 KieSession (org.kie.api.runtime.KieSession)13 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)10 ArrayList (java.util.ArrayList)7 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)7 KieBase (org.kie.api.KieBase)7 KieModule (org.kie.api.builder.KieModule)7 KieRepository (org.kie.api.builder.KieRepository)6 Before (org.junit.Before)5 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)4 IOException (java.io.IOException)4