Search in sources :

Example 1 with KieRepositoryImpl

use of org.drools.compiler.kie.builder.impl.KieRepositoryImpl in project drools by kiegroup.

the class KieModuleMavenTest method testKieModuleFromMavenNoDependencies.

@Test
public void testKieModuleFromMavenNoDependencies() throws Exception {
    final KieServices ks = new KieServicesImpl() {

        @Override
        public KieRepository getRepository() {
            // override repository to not store the artifact on deploy to trigger load from maven repo
            return new KieRepositoryImpl();
        }
    };
    ReleaseId releaseId = ks.newReleaseId("org.kie", "maven-test", "1.0-SNAPSHOT");
    InternalKieModule kJar1 = createKieJar(ks, releaseId, true, "rule1", "rule2");
    String pomText = getPom(releaseId);
    File pomFile = new File(System.getProperty("java.io.tmpdir"), MavenRepository.toFileName(releaseId, null) + ".pom");
    try {
        FileOutputStream fos = new FileOutputStream(pomFile);
        fos.write(pomText.getBytes());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    KieMavenRepository.getKieMavenRepository().installArtifact(releaseId, kJar1, pomFile);
    KieContainer kieContainer = ks.newKieContainer(releaseId);
    KieBaseModel kbaseModel = ((KieContainerImpl) kieContainer).getKieProject().getDefaultKieBaseModel();
    assertNotNull("Default kbase was not found", kbaseModel);
    String kbaseName = kbaseModel.getName();
    assertEquals("KBase1", kbaseName);
    // Check classloader
    assertUrlEnumerationContainsMatch(".*org/kie/maven\\-test/1.0\\-SNAPSHOT.*", kieContainer.getClassLoader().getResources(""));
    assertUrlEnumerationContainsMatch(".*org/kie/maven\\-test/1.0\\-SNAPSHOT.*", kieContainer.getClassLoader().getResources("KBase1/org/test"));
    assertUrlEnumerationContainsMatch(".*org/kie/maven\\-test/1.0\\-SNAPSHOT.*", kieContainer.getClassLoader().getResources("KBase1/org/test/"));
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieRepositoryImpl(org.drools.compiler.kie.builder.impl.KieRepositoryImpl) FileOutputStream(java.io.FileOutputStream) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) IOException(java.io.IOException) KieServicesImpl(org.drools.compiler.kie.builder.impl.KieServicesImpl) File(java.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 2 with KieRepositoryImpl

use of org.drools.compiler.kie.builder.impl.KieRepositoryImpl in project drools by kiegroup.

the class KieModuleMavenTest method testKieModuleFromMavenWithDependenciesProperties.

@Test
public void testKieModuleFromMavenWithDependenciesProperties() throws Exception {
    final KieServices ks = new KieServicesImpl() {

        @Override
        public KieRepository getRepository() {
            // override repository to not store the artifact on deploy to trigger load from maven repo
            return new KieRepositoryImpl();
        }
    };
    ReleaseId dependency = ks.newReleaseId("org.drools", "drools-core", "${version.org.drools}");
    ReleaseId releaseId = ks.newReleaseId("org.kie.test", "maven-test", "1.0-SNAPSHOT");
    InternalKieModule kJar1 = createKieJarWithProperties(ks, releaseId, true, "5.5.0.Final", new ReleaseId[] { dependency }, "rule1", "rule2");
    String pomText = generatePomXmlWithProperties(releaseId, "5.5.0.Final", dependency);
    File pomFile = new File(System.getProperty("java.io.tmpdir"), MavenRepository.toFileName(releaseId, null) + ".pom");
    try {
        FileOutputStream fos = new FileOutputStream(pomFile);
        fos.write(pomText.getBytes());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    KieMavenRepository.getKieMavenRepository().installArtifact(releaseId, kJar1, pomFile);
    KieContainer kieContainer = ks.newKieContainer(releaseId);
    KieBaseModel kbaseModel = ((KieContainerImpl) kieContainer).getKieProject().getDefaultKieBaseModel();
    assertNotNull("Default kbase was not found", kbaseModel);
    String kbaseName = kbaseModel.getName();
    assertEquals("KBase1", kbaseName);
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieRepositoryImpl(org.drools.compiler.kie.builder.impl.KieRepositoryImpl) FileOutputStream(java.io.FileOutputStream) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) IOException(java.io.IOException) KieServicesImpl(org.drools.compiler.kie.builder.impl.KieServicesImpl) File(java.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 3 with KieRepositoryImpl

use of org.drools.compiler.kie.builder.impl.KieRepositoryImpl in project drools by kiegroup.

the class KieModuleMavenTest method testKieModuleFromMavenWithDependencies.

@Test
public void testKieModuleFromMavenWithDependencies() throws Exception {
    final KieServices ks = new KieServicesImpl() {

        @Override
        public KieRepository getRepository() {
            // override repository to not store the artifact on deploy to trigger load from maven repo
            return new KieRepositoryImpl();
        }
    };
    ReleaseId dependency = ks.newReleaseId("org.drools", "drools-core", "5.5.0.Final");
    ReleaseId releaseId = ks.newReleaseId("org.kie", "maven-test", "1.0-SNAPSHOT");
    InternalKieModule kJar1 = createKieJar(ks, releaseId, true, "rule1", "rule2");
    String pomText = getPom(releaseId, dependency);
    File pomFile = new File(System.getProperty("java.io.tmpdir"), MavenRepository.toFileName(releaseId, null) + ".pom");
    try {
        FileOutputStream fos = new FileOutputStream(pomFile);
        fos.write(pomText.getBytes());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    KieMavenRepository.getKieMavenRepository().installArtifact(releaseId, kJar1, pomFile);
    KieContainer kieContainer = ks.newKieContainer(releaseId);
    KieBaseModel kbaseModel = ((KieContainerImpl) kieContainer).getKieProject().getDefaultKieBaseModel();
    assertNotNull("Default kbase was not found", kbaseModel);
    String kbaseName = kbaseModel.getName();
    assertEquals("KBase1", kbaseName);
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieRepositoryImpl(org.drools.compiler.kie.builder.impl.KieRepositoryImpl) FileOutputStream(java.io.FileOutputStream) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) IOException(java.io.IOException) KieServicesImpl(org.drools.compiler.kie.builder.impl.KieServicesImpl) File(java.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 4 with KieRepositoryImpl

use of org.drools.compiler.kie.builder.impl.KieRepositoryImpl 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 5 with KieRepositoryImpl

use of org.drools.compiler.kie.builder.impl.KieRepositoryImpl in project drools by kiegroup.

the class KieModuleMavenTest method testKieModuleFromMavenWithTransitiveDependencies.

@Test
public void testKieModuleFromMavenWithTransitiveDependencies() throws Exception {
    final KieServices ks = new KieServicesImpl() {

        @Override
        public KieRepository getRepository() {
            // override repository to not store the artifact on deploy to trigger load from maven repo
            return new KieRepositoryImpl();
        }
    };
    ReleaseId dependency = ks.newReleaseId("org.drools", "drools-core", "5.5.0.Final");
    ReleaseId releaseId = ks.newReleaseId("org.kie", "maven-test", "1.0-SNAPSHOT");
    String pomText = getPom(releaseId, dependency);
    InternalKieModule kJar1 = createKieJar(ks, releaseId, pomText, true, "rule1", "rule2");
    File pomFile = new File(System.getProperty("java.io.tmpdir"), MavenRepository.toFileName(releaseId, null) + ".pom");
    try {
        FileOutputStream fos = new FileOutputStream(pomFile);
        fos.write(pomText.getBytes());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    KieMavenRepository.getKieMavenRepository().installArtifact(releaseId, kJar1, pomFile);
    KieContainer kieContainer = ks.newKieContainer(releaseId);
    Collection<ReleaseId> expectedDependencies = new HashSet<ReleaseId>();
    expectedDependencies.add(ks.newReleaseId("org.drools", "knowledge-api", "5.5.0.Final"));
    expectedDependencies.add(ks.newReleaseId("org.drools", "knowledge-internal-api", "5.5.0.Final"));
    expectedDependencies.add(ks.newReleaseId("org.drools", "drools-core", "5.5.0.Final"));
    expectedDependencies.add(ks.newReleaseId("org.mvel", "mvel2", "2.1.3.Final"));
    expectedDependencies.add(ks.newReleaseId("org.slf4j", "slf4j-api", "1.6.4"));
    Collection<ReleaseId> dependencies = ((InternalKieModule) ((KieContainerImpl) kieContainer).getKieModuleForKBase("KBase1")).getJarDependencies(DependencyFilter.TAKE_ALL_FILTER);
    assertNotNull(dependencies);
    assertEquals(5, dependencies.size());
    boolean matchedAll = dependencies.containsAll(expectedDependencies);
    assertTrue(matchedAll);
}
Also used : KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) IOException(java.io.IOException) KieRepositoryImpl(org.drools.compiler.kie.builder.impl.KieRepositoryImpl) FileOutputStream(java.io.FileOutputStream) KieServicesImpl(org.drools.compiler.kie.builder.impl.KieServicesImpl) File(java.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

FileOutputStream (java.io.FileOutputStream)6 IOException (java.io.IOException)6 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)6 KieRepositoryImpl (org.drools.compiler.kie.builder.impl.KieRepositoryImpl)6 KieServices (org.kie.api.KieServices)6 ReleaseId (org.kie.api.builder.ReleaseId)6 KieContainer (org.kie.api.runtime.KieContainer)6 File (java.io.File)5 KieServicesImpl (org.drools.compiler.kie.builder.impl.KieServicesImpl)5 Test (org.junit.Test)5 KieBaseModel (org.kie.api.builder.model.KieBaseModel)3 FileNotFoundException (java.io.FileNotFoundException)1 HashSet (java.util.HashSet)1 ReleaseIdImpl (org.drools.compiler.kproject.ReleaseIdImpl)1 InternalRuleUnitExecutor (org.drools.core.impl.InternalRuleUnitExecutor)1 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)1 KieBase (org.kie.api.KieBase)1 KieRepository (org.kie.api.builder.KieRepository)1 Resource (org.kie.api.io.Resource)1 RuleUnitExecutor (org.kie.api.runtime.rule.RuleUnitExecutor)1