Java Code Examples for org.kie.api.builder.KieRepository#getKieModule()

The following examples show how to use org.kie.api.builder.KieRepository#getKieModule() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KieRepositoryTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testLoadKjarFromClasspath() {
    // DROOLS-1335
    ClassLoader cl = Thread.currentThread().getContextClassLoader();

    URLClassLoader urlClassLoader = new URLClassLoader( new URL[]{this.getClass().getResource( "/kie-project-simple-1.0.0.jar" )} );
    Thread.currentThread().setContextClassLoader( urlClassLoader );

    try {
        KieServices ks = KieServices.Factory.get();
        KieRepository kieRepository = ks.getRepository();
        ReleaseId releaseId = ks.newReleaseId( "org.test", "kie-project-simple", "1.0.0" );
        KieModule kieModule = kieRepository.getKieModule( releaseId );
        assertNotNull( kieModule );
        assertEquals( releaseId, kieModule.getReleaseId() );
    } finally {
        Thread.currentThread().setContextClassLoader( cl );
    }
}
 
Example 2
Source File: KieRepositoryTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testTryLoadNotExistingKjarFromClasspath() {
    // DROOLS-1335
    ClassLoader cl = Thread.currentThread().getContextClassLoader();

    URLClassLoader urlClassLoader = new URLClassLoader( new URL[]{this.getClass().getResource( "/kie-project-simple-1.0.0.jar" )} );
    Thread.currentThread().setContextClassLoader( urlClassLoader );

    try {
        KieServices ks = KieServices.Factory.get();
        KieRepository kieRepository = ks.getRepository();
        ReleaseId releaseId = ks.newReleaseId( "org.test", "kie-project-simple", "1.0.1" );
        KieModule kieModule = kieRepository.getKieModule( releaseId );
        assertNull( kieModule );
    } finally {
        Thread.currentThread().setContextClassLoader( cl );
    }
}
 
Example 3
Source File: KieRepositoryTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testLoadingNotAKJar() {
    // DROOLS-1351
    ClassLoader cl = Thread.currentThread().getContextClassLoader();

    URLClassLoader urlClassLoader = new URLClassLoader( new URL[]{this.getClass().getResource( "/only-jar-pojo-not-kjar-no-kmodule-1.0.0.jar" )} );
    Thread.currentThread().setContextClassLoader( urlClassLoader );

    try {
        KieServices ks = KieServices.Factory.get();
        KieRepository kieRepository = ks.getRepository();
        ReleaseId releaseId = ks.newReleaseId( "org.test", "only-jar-pojo-not-kjar-no-kmodule", "1.0.0" );
        KieModule kieModule = kieRepository.getKieModule( releaseId );
        assertNull( kieModule );
    } finally {
        Thread.currentThread().setContextClassLoader( cl );
    }
}
 
Example 4
Source File: KieBuilderTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public void createAndTestKieContainer(ReleaseId releaseId, KieBuilder kb, String kBaseName) throws IOException,
        ClassNotFoundException,
        InterruptedException {
    KieServices ks = KieServices.Factory.get();
    
    kb.buildAll();
    
    if ( kb.getResults().hasMessages(Level.ERROR) ) {
        fail("Unable to build KieModule\n" + kb.getResults( ).toString() );
    }
    KieRepository kr = ks.getRepository();
    KieModule kJar = kr.getKieModule(releaseId);
    assertNotNull( kJar );
    
    KieContainer kContainer = ks.newKieContainer(releaseId);
    KieBase kBase = kBaseName != null ? kContainer.getKieBase( kBaseName ) : kContainer.getKieBase();

    KieSession kSession = kBase.newKieSession();
    List list = new ArrayList();
    kSession.setGlobal( "list", list );
    kSession.fireAllRules();

    assertEquals( 1, list.size() );
    assertEquals( "org.kie.test.Message", list.get(0).getClass().getName() );       
}
 
Example 5
Source File: MBeansMonitoringTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
/**
 * Copied from KieRepositoryTest to test JMX monitoring
 */
@Test
public void testLoadKjarFromClasspath() {
    // DROOLS-1335
    ClassLoader cl = Thread.currentThread().getContextClassLoader();

    URLClassLoader urlClassLoader = new URLClassLoader( new URL[]{this.getClass().getResource( "/kie-project-simple-1.0.0.jar" )} );
    Thread.currentThread().setContextClassLoader( urlClassLoader );
    
    MBeanServer mbserver = ManagementFactory.getPlatformMBeanServer();

    try {
        KieServices ks = KieServices.Factory.get();
        KieRepository kieRepository = ks.getRepository();
        ReleaseId releaseId = ks.newReleaseId( "org.test", "kie-project-simple", "1.0.0" );
        KieModule kieModule = kieRepository.getKieModule( releaseId );
        assertNotNull( kieModule );
        assertEquals( releaseId, kieModule.getReleaseId() );
        
        ks.newKieContainer("myID", releaseId);
        
        KieContainerMonitorMXBean c1Monitor = JMX.newMXBeanProxy(
                mbserver,
                DroolsManagementAgent.createObjectNameBy("myID"),
                KieContainerMonitorMXBean.class);
        
        assertTrue(c1Monitor.getConfiguredReleaseId().sameGAVof(releaseId));
        assertTrue(c1Monitor.getResolvedReleaseId().sameGAVof(releaseId));
    } finally {
        Thread.currentThread().setContextClassLoader( cl );
    }
}
 
Example 6
Source File: KieBuilderTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@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() );
    }
}