Java Code Examples for org.kie.api.KieServices#newReleaseId()

The following examples show how to use org.kie.api.KieServices#newReleaseId() . 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: ChangeSetBuilderTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
private InternalKieModule createKieJar( String... drls) {
    InternalKieModule kieJar = mock( InternalKieModule.class );
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "hello-world", "1.0");

    List<String> drlFs = new ArrayList<String>();
    
    for( int i=0; i<drls.length; i++ ) {
        if( drls[i] != null ) {
            String fileName = "src/main/resources/org/pkg1/drlFile"+i+".drl";
            drlFs.add( fileName );
            when( kieJar.getBytes( fileName ) ).thenReturn( drls[i].getBytes() );
        }
    }
    when( kieJar.getBytes( KieModuleModelImpl.KMODULE_JAR_PATH ) ).thenReturn( createKieProjectWithPackages(ks, releaseId).toXML().getBytes() );
    when( kieJar.getFileNames() ).thenReturn( drlFs );
    return ( InternalKieModule ) kieJar;
}
 
Example 2
Source File: KieHelloWorldTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testHelloWorldWithWildcardPackages() throws Exception {
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "hello-world", "1.0");

    KieFileSystem kfs = ks.newKieFileSystem()
            .generateAndWritePomXML( releaseId )
            .write( "src/main/resources/org/pkg1/test/r1.drl", createDrlWithGlobal( "org.pkg1.test", "R1" ) )
            .write( "src/main/resources/org/pkg2/test/r2.drl", createDrlWithGlobal( "org.pkg2.test", "R2" ) )
            .writeKModuleXML( createKieProjectWithPackages( ks, "org.pkg1.*" ).toXML() );
    ks.newKieBuilder( kfs ).buildAll();

    KieSession ksession = ks.newKieContainer(releaseId).newKieSession("KSession1");
    ksession.insert(new Message("Hello World"));

    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    assertEquals( 1, ksession.fireAllRules() );
    assertEquals( 1, list.size() );
    assertEquals( "R1", list.get( 0 ) );
}
 
Example 3
Source File: DynamicRuleLoadTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testKJarUpgrade() throws Exception {
    // DROOLS-919
    KieServices ks = KieServices.Factory.get();

    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId1 = ks.newReleaseId( "org.kie", "test-upgrade", "1.0.0" );
    KieModule km = createAndDeployJar( ks, releaseId1, drl1, drl2_1 );

    // Create a session and fire rules
    kieContainer = ks.newKieContainer( km.getReleaseId() );
    ksession = kieContainer.newKieSession();

    ksession.setGlobal( "test", this );
    ksession.insert( new Message( "Hi Universe" ) );
    ksession.fireAllRules();

    assertTrue( done );
}
 
Example 4
Source File: KieHelloWorldTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testHelloWorldWithPackages() throws Exception {
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "hello-world", "1.0");

    KieFileSystem kfs = ks.newKieFileSystem()
            .generateAndWritePomXML( releaseId )
            .write( "src/main/resources/KBase1/org/pkg1/r1.drl", createDrl( "org.pkg1", "R1" ) )
            .write( "src/main/resources/KBase1/org/pkg2/r2.drl", createDrl( "org.pkg2", "R2" ) )
            .writeKModuleXML( createKieProjectWithPackages( ks, "org.pkg1" ).toXML() );
    ks.newKieBuilder( kfs ).buildAll();

    KieSession ksession = ks.newKieContainer(releaseId).newKieSession("KSession1");
    ksession.insert(new Message("Hello World"));

    int count = ksession.fireAllRules();

    assertEquals( 1, count );
}
 
Example 5
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 6
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 7
Source File: MBeansMonitoringTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testEventOffset() throws Exception {
	String drl = "package org.drools.compiler.test\n" +
			"import org.drools.compiler.StockTick\n" +
			"declare StockTick\n" +
			"    @role(event)\n" +
			"    @expires(10s)\n" +
			"end\n" +
			"rule X\n" +
			"when\n" +
			"    StockTick()\n" +
			"then\n" +
			"end";

	KieServices ks = KieServices.Factory.get();

	KieModuleModel kproj = ks.newKieModuleModel();

	KieBaseModel kieBaseModel1 = kproj.newKieBaseModel( KBASE1 ).setDefault( true )
			.setEventProcessingMode( EventProcessingOption.STREAM );
	KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel( KSESSION1 ).setDefault( true )
			.setType( KieSessionModel.KieSessionType.STATEFUL )
			.setClockType( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) );

	ReleaseId releaseId1 = ks.newReleaseId( "org.kie.test", "mbeans", "1.0.0" );
	createKJar( ks, kproj, releaseId1, null, drl );

	KieContainer kc = ks.newKieContainer( releaseId1 );

	KieBase kiebase = kc.getKieBase( KBASE1 );
	MBeanServer mbserver = ManagementFactory.getPlatformMBeanServer();

	ObjectName kbOn = DroolsManagementAgent.createObjectNameFor((InternalKnowledgeBase) kiebase);
	mbserver.invoke( kbOn, "startInternalMBeans", new Object[0], new String[0] );

	Object expOffset = mbserver.getAttribute( new ObjectName( kbOn + ",group=EntryPoints,EntryPoint=DEFAULT,ObjectType=org.drools.compiler.StockTick" ), "ExpirationOffset" );
	assertEquals( 10001, ((Number) expOffset).longValue() );
}
 
Example 8
Source File: KieHelloWorldTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testHelloWorldUsingFolders() throws Exception {
    String drlDef = "package org.drools.compiler.integrationtests\n" +
            "import " + Message.class.getCanonicalName() + "\n" +
            "rule R_def when\n" +
            "   $m : Message( message == \"Hello World\" )\n" +
            "then\n" +
            "end\n";

    KieServices ks = KieServices.Factory.get();

    ReleaseId releaseId = ks.newReleaseId( "org.kie", "hello-world", "1.0" );

    KieFileSystem kfs = ks.newKieFileSystem()
            .generateAndWritePomXML( releaseId )
            .write( "src/main/resources/KBase1/org/pkg1/r1_1.drl", drlDef )
            .write( "src/main/resources/KBase1/org/pkg1/r1_2.drl", createDrl( "R1" ) )
            .write( "src/main/resources/KBase1/org/pkg2/r2.drl", createDrl( "R2" ) )
            .writeKModuleXML( createKieProjectWithPackages( ks, "org.pkg1" )
                    .setConfigurationProperty( "drools.groupDRLsInKieBasesByFolder", "true" )
                    .toXML() );
    ks.newKieBuilder( kfs ).buildAll();

    KieSession ksession = ks.newKieContainer( releaseId ).newKieSession( "KSession1" );
    ksession.insert( new Message( "Hello World" ) );

    assertEquals( 2, ksession.fireAllRules() );
}
 
Example 9
Source File: KieContainerTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetDefaultKieSessionModel() {
    KieServices kieServices = KieServices.Factory.get();
    String drl = "package org.drools.test\n" +
            "rule R1 when\n" +
            "   $m : Object()\n" +
            "then\n" +
            "end\n";
    Resource resource = kieServices.getResources().newReaderResource( new StringReader( drl), "UTF-8" );
    resource.setTargetPath("org/drools/test/rules.drl");
    String kmodule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" +
            "  <kbase name=\"testKbase\" packages=\"org.drools.test\">\n" +
            "    <ksession name=\"testKsession\" default=\"true\"/>\n" +
            "  </kbase>\n" +
            "</kmodule>";

    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId = kieServices.newReleaseId("org.kie", "test-testGetDefaultKieSessionModel", "1.0.0");
    createAndDeployJar(kieServices, kmodule, releaseId, resource);

    KieContainer kieContainer = kieServices.newKieContainer(releaseId);

    KieSessionModel sessionModel = kieContainer.getKieSessionModel(null);
    assertNotNull(sessionModel);
    assertEquals("testKsession", sessionModel.getName());
}
 
Example 10
Source File: KieHelloWorldTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetDefaultKieSessionWithNullName() throws Exception {
    // DROOLS-1276
    KieServices ks = KieServices.Factory.get();

    buildVersion(ks, "Hello World", "1.0");

    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "hello-world", "1.0");

    KieSession ksession = ks.newKieContainer(releaseId1).newKieSession((String)null);
    ksession.insert(new Message("Hello World"));
    assertEquals( 1, ksession.fireAllRules() );
}
 
Example 11
Source File: I18nTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testKieModuleJar() {
    String str = "package org.drools.compiler.i18ntest;\n" +
            "import org.drools.compiler.I18nPerson;\n" +
            "\n" +
            "global java.util.List list;\n" +
            "rule \"名称 is 山田花子\"\n" +
            "    when\n" +
            "        p : I18nPerson( 名称 == \"山田花子\" )\n" +
            "    then\n" +
            "        list.add( \"名称は山田花子です\" );\n" +
            "end\n";

    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "118ntest", "1.0.0");
    byte[] jar = createKJar(ks, releaseId, null, str);
    KieModule km = deployJar(ks, jar);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();

    List list = new ArrayList();
    ksession.setGlobal( "list", list );

    I18nPerson i18nPerson = new I18nPerson();
    i18nPerson.set名称("山田花子");
    ksession.insert(i18nPerson);
    ksession.fireAllRules();

    assertTrue(list.contains("名称は山田花子です"));

    ksession.dispose();
}
 
Example 12
Source File: KieBuilderTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testStatelessSessionDeclarativeChannelRegistration() {
    final String drl1 = "package org.drools.compiler\n" +
            "rule R1 when\n" +
            "   $m : Message()\n" +
            "then\n" +
            "end\n";

    final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" +
            "         xmlns=\"http://www.drools.org/xsd/kmodule\">\n" +
            "  <kbase name=\"kbase1\">\n" +
            "    <ksession name=\"ksession1\" default=\"true\" type=\"stateless\">\n" +
            "       <channels>\n" +
            "         <channel name=\"testChannel\" type=\"org.drools.compiler.integrationtests.KieBuilderTest$MockChannel\" />\n" +
            "       </channels>\n" +
            "    </ksession>" +
            "  </kbase>\n" +
            "</kmodule>";

    final KieServices ks = KieServices.Factory.get();

    final ReleaseId releaseId1 = ks.newReleaseId( "org.kie", "test-kie-builder", "1.0.0" );
    final Resource r1 = ResourceFactory.newByteArrayResource( drl1.getBytes() ).setResourceType( ResourceType.DRL ).setSourcePath( "kbase1/drl1.drl" );
    final KieModule km = createAndDeployJar( ks,
                                       kmodule,
                                       releaseId1,
                                       r1);

    KieContainer kieContainer = ks.newKieContainer( km.getReleaseId());
    
    StatelessKieSession statelessKieSession = kieContainer.newStatelessKieSession();
    assertEquals(1, statelessKieSession.getChannels().size());
    assertTrue(statelessKieSession.getChannels().keySet().contains("testChannel"));
}
 
Example 13
Source File: WireChannelTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testWireChannel() throws Exception {
    KieServices ks = KieServices.Factory.get();

    ReleaseId releaseId = ks.newReleaseId("org.kie", "listener-test", "1.0");
    build(ks, releaseId);
    KieContainer kieContainer = ks.newKieContainer(releaseId);

    KieSession ksession = kieContainer.newKieSession();
    ksession.fireAllRules();
    
    assertEquals(1, channelMessages.size());
    assertEquals("Test Message", channelMessages.get(0));
}
 
Example 14
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 15
Source File: WireListenerTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testWireListener() throws Exception {
    KieServices ks = KieServices.Factory.get();

    ReleaseId releaseId = ks.newReleaseId("org.kie", "listener-test", "1.0");
    build(ks, releaseId);
    KieContainer kieContainer = ks.newKieContainer(releaseId);

    KieSession ksession = kieContainer.newKieSession();
    ksession.fireAllRules();

    assertEquals(1, insertEvents.size());
    assertEquals(1, updateEvents.size());
    assertEquals(1, retractEvents.size());
}
 
Example 16
Source File: KieContainerTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testSharedTypeDeclarationsUsingFactTypes() throws Exception {
    String type = "package org.drools.test\n" +
                  "declare Message\n" +
                  "   message : String\n" +
                  "end\n";

    String drl1 = "package org.drools.test\n" +
                  "rule R1 when\n" +
                  "   $m : Message()\n" +
                  "then\n" +
                  "   if ($m.getClass() != new Message(\"Test\").getClass()) {\n" +
                  "       throw new RuntimeException();\n" +
                  "   }\n" +
                  "end\n";

    String drl2 = "package org.drools.test\n" +
                  "rule R2_2 when\n" +
                  "   $m : Message( message == \"Hello World\" )\n" +
                  "then\n" +
                  "   if ($m.getClass() != new Message(\"Test\").getClass()) {\n" +
                  "       throw new RuntimeException();\n" +
                  "   }\n" +
                  "end\n";

    KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-delete", "1.0.0");
    createAndDeployJar( ks, releaseId1, type, drl1, drl2 );

    KieContainer kieContainer = ks.newKieContainer(releaseId1);
    KieContainer kieContainer2 = ks.newKieContainer(releaseId1);

    KieSession ksession = kieContainer.newKieSession();
    KieSession ksession2 = kieContainer2.newKieSession();

    insertMessageFromTypeDeclaration( ksession );
    assertEquals( 2, ksession.fireAllRules() );

    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-delete", "1.0.1");
    createAndDeployJar( ks, releaseId2, type, null, drl2 );

    kieContainer.updateToVersion(releaseId2);

    // test with the old ksession ...
    ksession = kieContainer.newKieSession();
    insertMessageFromTypeDeclaration( ksession );
    assertEquals( 1, ksession.fireAllRules() );

    // ... and with a brand new one
    ksession = kieContainer.newKieSession();
    insertMessageFromTypeDeclaration (ksession );
    assertEquals( 1, ksession.fireAllRules() );

    // check that the second kieContainer hasn't been affected by the update of the first one
    insertMessageFromTypeDeclaration( ksession2 );
    assertEquals( 2, ksession2.fireAllRules() );

    ksession2 = kieContainer2.newKieSession();
    insertMessageFromTypeDeclaration( ksession2 );
    assertEquals( 2, ksession2.fireAllRules() );
}
 
Example 17
Source File: KieContainerTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testClassLoaderGetResources() throws IOException, URISyntaxException {
    KieServices kieServices = KieServices.Factory.get();
    String drl1 = "package org.drools.testdrl;\n" +
                 "rule R1 when\n" +
                 "   $m : Object()\n" +
                 "then\n" +
                 "end\n";
    Resource resource1 = kieServices.getResources().newReaderResource(new StringReader(drl1), "UTF-8");
    resource1.setTargetPath("org/drools/testdrl/rules1.drl");

    String drl2 = "package org.drools.testdrl;\n" +
                 "rule R2 when\n" +
                 "   $m : Object()\n" +
                 "then\n" +
                 "end\n";
    Resource resource2 = kieServices.getResources().newReaderResource(new StringReader(drl2), "UTF-8");
    resource2.setTargetPath("org/drools/testdrl/rules2.drl");

    String java3 = "package org.drools.testjava;\n" +
                 "public class Message {}";
    Resource resource3 = kieServices.getResources().newReaderResource(new StringReader(java3), "UTF-8");
    resource3.setTargetPath("org/drools/testjava/Message.java");
    resource3.setResourceType(ResourceType.JAVA);

    String kmodule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                     "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" +
                     "  <kbase name=\"testKbase\" packages=\"org.drools.testdrl\">\n" +
                     "    <ksession name=\"testKsession\"/>\n" +
                     "  </kbase>\n" +
                     "</kmodule>";

    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId = kieServices.newReleaseId("org.kie", "test-delete", "1.0.0");
    createAndDeployJar(kieServices, kmodule, releaseId, resource1, resource2, resource3);

    KieContainer kieContainer = kieServices.newKieContainer(releaseId);

    ClassLoader classLoader = kieContainer.getClassLoader();
    assertEnumerationSize(1, classLoader.getResources("org/drools/testjava")); // no trailing "/"

    assertEnumerationSize(1, classLoader.getResources("org/drools/testdrl/")); // trailing "/" to test both variants
    // make sure the package resource correctly lists all its child resources (files in this case)
    URL url = classLoader.getResources("org/drools/testdrl").nextElement();
    
    List<String> lines = read(url.openStream());
    assertThat(lines).contains("rules1.drl", "rules1.drl.properties", "rules2.drl", "rules2.drl.properties");

    assertUrlEnumerationContainsMatch("^mfs\\:/$", classLoader.getResources(""));
}
 
Example 18
Source File: DynamicRuleLoadTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testKJarUpgradeWithJavaClass() throws Exception {

    KieServices ks = KieServices.Factory.get();

    String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" +
            "         xmlns=\"http://www.drools.org/xsd/kmodule\">\n" +
            "  <kbase name=\"kbase1\">\n" +
            "    <ksession name=\"ksession1\" default=\"true\"/>\n" +
            "  </kbase>\n" +
            "</kmodule>";

    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId1 = ks.newReleaseId( "org.kie", "test-upgrade-java", "1.0.0" );
    Resource javaResource = ResourceFactory.newByteArrayResource(javaSrc.getBytes()).setResourceType( ResourceType.JAVA )
            .setSourcePath( "org/drools/compiler/test/PersonObject.java" );
    Resource drlResource = ResourceFactory.newByteArrayResource( person_drl.getBytes() ).setResourceType( ResourceType.DRL )
            .setSourcePath( "kbase1/person.drl" );
    KieModule km = createAndDeployJar( ks, kmodule, releaseId1, javaResource, drlResource );

    // Create a session and fire rules
    kieContainer = ks.newKieContainer( km.getReleaseId() );
    ksession = kieContainer.newKieSession();

    Class<?> clazz = kieContainer.getClassLoader().loadClass("org.drools.compiler.test.PersonObject");
    Object person = clazz.newInstance();

    ksession.insert( person );
    ksession.fireAllRules();

    assertNotNull(person);
    Object personId = valueOf(person, "id");
    assertNotNull(personId);
    assertEquals("Person from version 1", personId);

    ReleaseId releaseId2 = ks.newReleaseId( "org.kie", "test-upgrade-java", "1.1.0" );
    Resource javaResource2 = ResourceFactory.newByteArrayResource(javaSrc_2.getBytes()).setResourceType( ResourceType.JAVA )
            .setSourcePath( "org/drools/compiler/test/PersonObject.java" );
    Resource drlResource2 = ResourceFactory.newByteArrayResource( person_drl.getBytes() ).setResourceType( ResourceType.DRL )
            .setSourcePath( "kbase1/person.drl" );
    createAndDeployJar( ks, kmodule, releaseId2, javaResource2, drlResource2 );

    // update container
    kieContainer.updateToVersion(releaseId2);
    assertEquals(releaseId2, kieContainer.getReleaseId());
    // now let's run the rules
    ksession = kieContainer.newKieSession();

    person = kieContainer.getClassLoader().loadClass("org.drools.compiler.test.PersonObject").newInstance();

    ksession.insert( person );
    ksession.fireAllRules();

    assertNotNull(person);
    personId = valueOf(person, "id");
    assertNotNull(personId);
    assertEquals("Person from version 2", personId);
}
 
Example 19
Source File: KieFileSystemScannerTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testFixedVersion() throws Exception {
    Path tempDir = Files.createTempDirectory(FileSystems.getDefault().getPath("./target"), null);
    File file2 = null;
    File file3 = null;

    try {
        KieServices ks = KieServices.Factory.get();
        ReleaseId releaseId1 = ks.newReleaseId( "org.kie", "scanner-test", "1.0.0" );
        ReleaseId releaseId2 = ks.newReleaseId( "org.kie", "scanner-test", "1.0.1" );
        ReleaseId releaseId3 = ks.newReleaseId( "org.kie", "scanner-test", "1.1.0" );

        createKieJar( ks, releaseId1, "R1" );

        KieContainer kieContainer = ks.newKieContainer( releaseId1 );

        KieSession ksession = kieContainer.newKieSession();
        checkKSession( ksession, "R1" );

        KieScanner scanner = ks.newKieScanner( kieContainer, tempDir.toString() );
        scanner.scanNow();

        ksession = kieContainer.newKieSession();
        checkKSession( ksession, "R1" );

        file2 = write( createKieJar( ks, releaseId2, "R2" ), tempDir, releaseId2 );
        file3 = write( createKieJar( ks, releaseId2, "R3" ), tempDir, releaseId3 );
        ksession = kieContainer.newKieSession();
        checkKSession( ksession, "R1" );

        scanner.scanNow();
        ksession = kieContainer.newKieSession();
        checkKSession( ksession, "R3" );
    } finally {
        if (file2 != null) {
            file2.delete();
        }
        if (file3 != null) {
            file3.delete();
        }
        Files.delete(tempDir);
    }
}
 
Example 20
Source File: KieBaseIncludesTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
/**
 * Test the inclusion of a KieBase defined in one KJAR into the KieBase of another KJAR.
 * <p/>
 * The 2 KieBases use the same package names for the rules (i.e. "rules").
 */
@Test
public void testKieBaseIncludesCrossKJarSamePackageNames() {

    // @formatter:off
    String pomContent1 = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" +
                         "<modelVersion>4.0.0</modelVersion>\n" +
                         "<groupId>org.kie</groupId>\n" +
                         "<artifactId>rules-1</artifactId>\n" +
                         "<version>1.0.0</version>\n" +
                         "<packaging>jar</packaging>\n" +
                         "<dependencies>\n" +
                         "<dependency>\n" +
                         "<groupId>org.kie</groupId>\n" +
                         "<artifactId>rules-2</artifactId>\n" +
                         "<version>1.0.0</version>\n" +
                         "</dependency>\n" +
                         "</dependencies>\n" +
                         "</project>\n";


    String kmoduleContent1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                             "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" +
                             "<kbase name=\"kbase1\" equalsBehavior=\"equality\" default=\"true\" packages=\"rules\" includes=\"kbase2\">\n" +
                             "<ksession name=\"ksession1\" default=\"true\" type=\"stateful\"/>\n" +
                             "</kbase>\n"+
                             "</kmodule>";

    String drl1 = "package rules\n" +
                  "\n" +
                  "rule \"Rule in KieBase 1\"\n" +
                  "when\n" +
                  "then\n" +
                  "System.out.println(\"Rule in KieBase1\");\n" +
                  "end";


    String kmoduleContent2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                             "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" +
                             "<kbase name=\"kbase2\" equalsBehavior=\"equality\" default=\"false\" packages=\"rules\">\n" +
                             "<ksession name=\"ksession2\" default=\"false\" type=\"stateful\"/>\n" +
                             "</kbase>\n"+
                             "</kmodule>";

    String drl2 = "package rules\n" +
                  "\n" +
                  "rule \"Rule in KieBase 2\"\n" +
                  "when\n" +
                  "then\n" +
                  "System.out.println(\"Rule in KieBase2\");\n" +
                  "end";

    // @formatter:on
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "rules-1", "1.0.0");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "rules-2", "1.0.0");

    //First deploy the second KJAR on which the first one depends.
    KieFileSystem kfs2 = ks.newKieFileSystem()
                           .generateAndWritePomXML(releaseId2)
                           .write("src/main/resources/rules/rules.drl", drl2)
                           .writeKModuleXML(kmoduleContent2);

    ks.newKieBuilder(kfs2).buildAll();

    KieFileSystem kfs1 = ks.newKieFileSystem()
                           //.generateAndWritePomXML(releaseId1)
                           .writePomXML(pomContent1)
                           .write("src/main/resources/rules/rules.drl", drl1)
                           .writeKModuleXML(kmoduleContent1);

    ks.newKieBuilder(kfs1).buildAll();

    KieContainer kc = ks.newKieContainer(releaseId1);
    KieBase kieBase = kc.getKieBase();

    // Assert the number of rules in the KieBase.
    long nrOfRules = getNumberOfRules(kieBase);

    // We should have 2 rules in our KieBase. One from our own DRL and one from the DRL in the KieBase we've included.
    assertEquals(2, nrOfRules);
}