Search in sources :

Example 61 with ReleaseId

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

the class KieRepositoryImpl method checkClasspathForKieModule.

private KieModule checkClasspathForKieModule(ReleaseId releaseId) {
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    URL kmoduleUrl = contextClassLoader.getResource(KieModuleModelImpl.KMODULE_JAR_PATH);
    if (kmoduleUrl == null) {
        return null;
    }
    String pomPropertiesPath = ReleaseIdImpl.getPomPropertiesPath(releaseId);
    URL pomPropertiesUrl = contextClassLoader.getResource(pomPropertiesPath);
    if (pomPropertiesUrl == null) {
        return null;
    }
    ReleaseId pomReleaseId = fromPropertiesStream(contextClassLoader.getResourceAsStream(pomPropertiesPath), pomPropertiesUrl.getPath());
    if (pomReleaseId.equals(releaseId)) {
        String path = pomPropertiesUrl.getPath();
        String pathToJar = path.substring(0, path.indexOf(".jar!") + 4);
        URL pathToKmodule;
        try {
            pathToKmodule = new URL(pomPropertiesUrl.getProtocol(), pomPropertiesUrl.getHost(), pomPropertiesUrl.getPort(), pathToJar + "!/" + KieModuleModelImpl.KMODULE_JAR_PATH);
            // if URL backed by 'file:' then FileURLConnection.getContentLength() returns 0, as per java.io.File.length() returns 0L if the file does not exist. (the same also for WildFly's VFS FileURLConnection)
            if (pathToKmodule.openConnection().getContentLength() <= 0) {
                return null;
            }
        } catch (MalformedURLException e) {
            log.error("Unable to reconstruct path to kmodule for " + releaseId);
            return null;
        } catch (IOException e) {
            log.error("Unable to read from path to kmodule for " + releaseId);
            return null;
        }
        log.info("Adding KieModule from classpath: " + pathToJar);
        return ClasspathKieProject.fetchKModule(pathToKmodule);
    }
    return null;
}
Also used : MalformedURLException(java.net.MalformedURLException) ReleaseId(org.kie.api.builder.ReleaseId) IOException(java.io.IOException) URL(java.net.URL)

Example 62 with ReleaseId

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

the class KieCDIExtension method addKContainerBean.

public void addKContainerBean(AfterBeanDiscovery abd, KieCDIEntry entry) {
    ReleaseId releaseId = entry.getReleaseId();
    // default to classpath, but allow it to be overriden
    KieContainerImpl kieContainer = classpathKContainer;
    if (releaseId != null) {
        kieContainer = (KieContainerImpl) gavs.get(releaseId);
        if (kieContainer == null) {
            log.error("Could not retrieve KieContainer for ReleaseId {}", entry.getValue(), releaseId.toString());
            return;
        }
    }
    KContainerBean bean = new KContainerBean(kieContainer, entry.getKReleaseId(), entry.getName(), entry.getInjectionPoints());
    if (log.isDebugEnabled()) {
        log.debug("Added Bean for @KContainer({})", releaseId);
    }
    abd.addBean(bean);
}
Also used : KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) KReleaseId(org.kie.api.cdi.KReleaseId) ReleaseId(org.kie.api.builder.ReleaseId)

Example 63 with ReleaseId

use of org.kie.api.builder.ReleaseId 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 64 with ReleaseId

use of org.kie.api.builder.ReleaseId 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 65 with ReleaseId

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

the class KieBaseIncludesTest method testKieBaseIncludesCrossKJarDifferentPackageNames.

/**
 * Test the inclusion of a KieBase defined in one KJAR into the KieBase of another KJAR.
 * <p/>
 * The 2 KieBases use different package names for the rules (i.e. "rules" and "rules2").
 */
@Test
public void testKieBaseIncludesCrossKJarDifferentPackageNames() {
    // @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://jboss.org/kie/6.0.0/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 org.jboss.ddoyle.drools.rules1\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://jboss.org/kie/6.0.0/kmodule\">\n" + "<kbase name=\"kbase2\" equalsBehavior=\"equality\" default=\"false\" packages=\"rules2\">\n" + "<ksession name=\"ksession2\" default=\"false\" type=\"stateful\"/>\n" + "</kbase>\n" + "</kmodule>";
    String drl2 = "package org.jboss.ddoyle.drools.rules2\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/rules2/rules.drl", drl2).writeKModuleXML(kmoduleContent2);
    ks.newKieBuilder(kfs2).buildAll();
    KieFileSystem kfs1 = ks.newKieFileSystem().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);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieBase(org.kie.api.KieBase) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

ReleaseId (org.kie.api.builder.ReleaseId)273 Test (org.junit.Test)219 KieServices (org.kie.api.KieServices)214 KieContainer (org.kie.api.runtime.KieContainer)176 KieSession (org.kie.api.runtime.KieSession)137 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)117 KieModule (org.kie.api.builder.KieModule)84 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)73 KieFileSystem (org.kie.api.builder.KieFileSystem)64 KieModuleModel (org.kie.api.builder.model.KieModuleModel)40 KieBuilder (org.kie.api.builder.KieBuilder)34 ArrayList (java.util.ArrayList)32 Message (org.drools.compiler.Message)32 KieMavenRepository.getKieMavenRepository (org.kie.scanner.KieMavenRepository.getKieMavenRepository)27 Resource (org.kie.api.io.Resource)23 Results (org.kie.api.builder.Results)20 KieBaseModel (org.kie.api.builder.model.KieBaseModel)20 InternalKieScanner (org.drools.compiler.kie.builder.impl.InternalKieScanner)18 KieScanner (org.kie.api.builder.KieScanner)18 IncrementalResults (org.kie.internal.builder.IncrementalResults)18