Search in sources :

Example 16 with KieRepository

use of org.kie.api.builder.KieRepository 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-SNAPSHOT");
    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-SNAPSHOT");
    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 17 with KieRepository

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

the class DroolsAbstractPMMLTest method readKnowledgeBase.

private static KieBase readKnowledgeBase(List<InputStream> theory) {
    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    KieFileSystem kfs = ks.newKieFileSystem();
    for (int j = 0; j < theory.size(); j++) {
        Resource res = ks.getResources().newInputStreamResource(theory.get(j));
        kfs.write(RESOURCE_PATH + "source_" + j + ".drl", res);
    }
    KieModuleModel model = ks.newKieModuleModel();
    KieBaseModel kbModel = model.newKieBaseModel(DEFAULT_KIEBASE).setDefault(true).addPackage(BASE_PACK).setEventProcessingMode(EventProcessingOption.STREAM);
    kfs.writeKModuleXML(model.toXML());
    KieBuilder kb = ks.newKieBuilder(kfs);
    kb.buildAll();
    if (kb.getResults().hasMessages(Message.Level.ERROR)) {
        throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
    }
    KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
    return kContainer.getKieBase();
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieRepository(org.kie.api.builder.KieRepository) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer)

Example 18 with KieRepository

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

the class DroolsAbstractPMMLTest method createExecutor.

protected RuleUnitExecutor createExecutor(String sourceName) {
    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    ReleaseId releaseId = new ReleaseIdImpl("org.kie:pmmlTest:1.0-SNAPSHOT");
    ((KieRepositoryImpl) kr).setDefaultGAV(releaseId);
    Resource res = ResourceFactory.newClassPathResource(sourceName);
    kbase = new KieHelper().addResource(res, ResourceType.PMML).build();
    assertNotNull(kbase);
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    KieContainer kc = ((KnowledgeBaseImpl) ((InternalRuleUnitExecutor) executor).getKieSession().getKieBase()).getKieContainer();
    InternalKieModule ikm = (InternalKieModule) kr.getKieModule(releaseId);
    try (FileOutputStream fos = new FileOutputStream("/tmp/outputModule.jar")) {
        fos.write(ikm.getBytes());
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    kc.getKieBaseNames().forEach(n -> {
        System.out.println(n);
    });
    data = executor.newDataSource("request");
    resultData = executor.newDataSource("results");
    pmmlData = executor.newDataSource("pmmlData");
    return executor;
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) ReleaseIdImpl(org.drools.compiler.kproject.ReleaseIdImpl) KieRepository(org.kie.api.builder.KieRepository) Resource(org.kie.api.io.Resource) FileNotFoundException(java.io.FileNotFoundException) KieHelper(org.kie.internal.utils.KieHelper) KieServices(org.kie.api.KieServices) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) ReleaseId(org.kie.api.builder.ReleaseId) IOException(java.io.IOException) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieRepositoryImpl(org.drools.compiler.kie.builder.impl.KieRepositoryImpl) FileOutputStream(java.io.FileOutputStream) KieContainer(org.kie.api.runtime.KieContainer) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 19 with KieRepository

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

the class DroolsAbstractPMMLTest method getModelSession.

protected KieSession getModelSession(File fileName) {
    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    KieFileSystem kfs = ks.newKieFileSystem();
    KieModuleModel model = ks.newKieModuleModel();
    model.setConfigurationProperty("drools.propertySpecific", "ALLOWED");
    KieBaseModel kbModel = model.newKieBaseModel(DEFAULT_KIEBASE).addPackage(BASE_PACK).setEventProcessingMode(EventProcessingOption.CLOUD);
    KieBuilder kb = ks.newKieBuilder(fileName);
    kb.buildAll();
    if (kb.getResults().hasMessages(Message.Level.ERROR)) {
        throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
    }
    KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
    KieBase kieBase = kContainer.getKieBase();
    setKbase(kieBase);
    return kieBase.newKieSession();
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieRepository(org.kie.api.builder.KieRepository) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer)

Example 20 with KieRepository

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

the class BuildFromKJarTest method test.

@Test
public void test() {
    KieServices ks = KieServices.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "kjar-test", "1.0");
    KieRepository repo = ks.getRepository();
    repo.removeKieModule(releaseId);
    InternalKieModule kmodule = createKieModule(ks, releaseId);
    repo.addKieModule(kmodule);
    executeSession(ks, releaseId);
}
Also used : KieRepository(org.kie.api.builder.KieRepository) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Aggregations

KieRepository (org.kie.api.builder.KieRepository)28 KieServices (org.kie.api.KieServices)23 KieContainer (org.kie.api.runtime.KieContainer)18 KieBuilder (org.kie.api.builder.KieBuilder)11 KieFileSystem (org.kie.api.builder.KieFileSystem)10 KieModule (org.kie.api.builder.KieModule)10 Resource (org.kie.api.io.Resource)8 Test (org.junit.Test)7 KieBase (org.kie.api.KieBase)7 ReleaseId (org.kie.api.builder.ReleaseId)7 KieSession (org.kie.api.runtime.KieSession)7 KieBaseModel (org.kie.api.builder.model.KieBaseModel)6 KieModuleModel (org.kie.api.builder.model.KieModuleModel)6 URLClassLoader (java.net.URLClassLoader)4 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)4 ArrayList (java.util.ArrayList)3 FileOutputStream (java.io.FileOutputStream)2 List (java.util.List)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1