Search in sources :

Example 1 with KieFileSystemImpl

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

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

the class KieBuilderTest method testNoPomXml.

@Test
public void testNoPomXml() throws ClassNotFoundException, InterruptedException, IOException {
    String namespace = "org.kie.test";
    KieModuleModel kProj = createKieProject(namespace);
    ReleaseId releaseId = KieServices.Factory.get().getRepository().getDefaultReleaseId();
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    generateKProjectXML(kfs, namespace, kProj);
    generateMessageClass(kfs, namespace);
    generateRule(kfs, namespace);
    MemoryFileSystem mfs = ((KieFileSystemImpl) kfs).asMemoryFileSystem();
    createAndTestKieContainer(releaseId, createKieBuilder(kfs), namespace);
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystem(org.kie.api.builder.KieFileSystem) KieFileSystemImpl(org.drools.compiler.kie.builder.impl.KieFileSystemImpl) KieModuleModel(org.kie.api.builder.model.KieModuleModel) ReleaseId(org.kie.api.builder.ReleaseId) Test(org.junit.Test)

Example 3 with KieFileSystemImpl

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

the class KieBuilderTest method testNoProjectXml.

@Test
public void testNoProjectXml() throws ClassNotFoundException, InterruptedException, IOException {
    String namespace = "org.kie.test";
    ReleaseId releaseId = KieServices.Factory.get().newReleaseId(namespace, "memory", "1.0-SNAPSHOT");
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    generatePomXML(kfs, releaseId);
    generateMessageClass(kfs, namespace);
    generateRule(kfs, namespace);
    MemoryFileSystem mfs = ((KieFileSystemImpl) kfs).asMemoryFileSystem();
    createAndTestKieContainer(releaseId, createKieBuilder(kfs), null);
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystem(org.kie.api.builder.KieFileSystem) KieFileSystemImpl(org.drools.compiler.kie.builder.impl.KieFileSystemImpl) ReleaseId(org.kie.api.builder.ReleaseId) Test(org.junit.Test)

Example 4 with KieFileSystemImpl

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

the class KieBuilderTest method testNoPomAndProjectXml.

@Test
public void testNoPomAndProjectXml() throws ClassNotFoundException, InterruptedException, IOException {
    String namespace = "org.kie.test";
    ReleaseId releaseId = KieServices.Factory.get().getRepository().getDefaultReleaseId();
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    generateMessageClass(kfs, namespace);
    generateRule(kfs, namespace);
    MemoryFileSystem mfs = ((KieFileSystemImpl) kfs).asMemoryFileSystem();
    createAndTestKieContainer(releaseId, createKieBuilder(kfs), null);
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystem(org.kie.api.builder.KieFileSystem) KieFileSystemImpl(org.drools.compiler.kie.builder.impl.KieFileSystemImpl) ReleaseId(org.kie.api.builder.ReleaseId) Test(org.junit.Test)

Example 5 with KieFileSystemImpl

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

the class KieRepositoryScannerTest method testKScannerWithRange_withListener_errors.

@Test
public void testKScannerWithRange_withListener_errors() throws Exception {
    KieServices ks = KieServices.Factory.get();
    KieMavenRepository repository = getKieMavenRepository();
    String artifactId = "scanner-range-test-withlistener-errors";
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", artifactId, "1.0.1");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", artifactId, "1.0.2");
    // Because KieScanner also check the local repo, ensure the artifact are not deployed on the local maven repo
    repository.removeLocalArtifact(releaseId1);
    repository.removeLocalArtifact(releaseId2);
    ReleaseId releaseRange = ks.newReleaseId("org.kie", artifactId, "[1.0.0,)");
    InternalKieModule kJar1 = createKieJar(ks, releaseId1, "rule1", "rule2");
    KieContainer kieContainer = ks.newKieContainer(releaseRange);
    List<KieScannerEvent> events = new ArrayList<>();
    InternalKieScanner scanner = (KieRepositoryScannerImpl) ks.newKieScanner(kieContainer);
    KieScannerEventListener listener = new KieScannerEventListener() {

        public void onKieScannerStatusChangeEvent(KieScannerStatusChangeEvent statusChange) {
            LOG.info("KieScanner event: {}", statusChange);
            events.add(statusChange);
        }

        public void onKieScannerUpdateResultsEvent(KieScannerUpdateResultsEvent updtaeResults) {
            LOG.info("KieScanner event: {}", updtaeResults);
            events.add(updtaeResults);
        }
    };
    scanner.addListener(listener);
    scanner.scanNow();
    // Because 1.0.2 does not exist, will perform a scan but will NOT update.
    assertThat(events, CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.SCANNING)));
    assertThat(events, CoreMatchers.not(CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.UPDATING))));
    events.clear();
    repository.installArtifact(releaseId1, kJar1, createKPom(fileManager, releaseId1));
    KieSession ksession = kieContainer.newKieSession("KSession1");
    checkKSession(ksession, "rule1", "rule2");
    // Create a wrong KJAR for releaseId2
    KieFileSystem kfs = ks.newKieFileSystem();
    String pomContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n" + "  <modelVersion>4.0.0</modelVersion>\n" + "\n" + "  <groupId>" + releaseId2.getGroupId() + "</groupId>\n" + "  <artifactId>" + releaseId2.getArtifactId() + "</artifactId>\n" + "  <version>" + releaseId2.getVersion() + "</version></project>\n";
    kfs.writePomXML(pomContent);
    kfs.write("rulebroken" + ".drl", "rule X when asd asd then end");
    kfs.write("META-INF/kmodule.xml", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "  <!-- This is a minimal kmodule.xml. Ref Drools documentation, see http://docs.jboss.org/drools/release/6.1.0.Final/drools-docs/html_single/index.html#d0e1112 -->\n" + "</kmodule>\n");
    File tempJar = File.createTempFile("org.kie" + artifactId + "1.0.2", "tmp");
    LOG.info("Writing to temp file: {}", tempJar);
    // please notice that writeAsJar would return a new file with .jar postfixed
    tempJar = ((KieFileSystemImpl) kfs).asMemoryFileSystem().writeAsJar(tempJar.getParentFile(), tempJar.getName());
    File tempPom = File.createTempFile("org.kie" + artifactId + "1.0.2", "tmp.xml");
    LOG.info("Writing to temp file: {}", tempPom);
    FileUtils.writeStringToFile(tempPom, pomContent, "UTF-8");
    repository.installArtifact(releaseId2, tempJar, tempPom);
    assertEquals(releaseId1, scanner.getCurrentReleaseId());
    assertEquals(InternalKieScanner.Status.STOPPED, scanner.getStatus());
    // scan the maven repo to get the new kjar version and deploy it on the kcontainer
    scanner.scanNow();
    // Keeping at previous release
    assertEquals(releaseId1, scanner.getCurrentReleaseId());
    assertEquals(InternalKieScanner.Status.STOPPED, scanner.getStatus());
    // there should be no update performed.
    assertThat(events, CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.SCANNING)));
    assertThat(events, CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.UPDATING)));
    assertTrue(events.get(2) instanceof KieScannerUpdateResultsEventImpl);
    assertTrue(((KieScannerUpdateResultsEventImpl) events.get(2)).getResults().hasMessages(Message.Level.ERROR));
    events.clear();
    ks.getRepository().removeKieModule(releaseId1);
    ks.getRepository().removeKieModule(releaseId2);
    scanner.removeListener(listener);
    assertEquals(0, scanner.getListeners().size());
    repository.removeLocalArtifact(releaseId1);
    repository.removeLocalArtifact(releaseId2);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieFileSystemImpl(org.drools.compiler.kie.builder.impl.KieFileSystemImpl) KieScannerEventListener(org.kie.api.event.kiescanner.KieScannerEventListener) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieScannerUpdateResultsEventImpl(org.kie.scanner.event.KieScannerUpdateResultsEventImpl) InternalKieScanner(org.drools.compiler.kie.builder.impl.InternalKieScanner) KieScannerStatusChangeEventImpl(org.kie.scanner.event.KieScannerStatusChangeEventImpl) KieMavenRepository.getKieMavenRepository(org.kie.scanner.KieMavenRepository.getKieMavenRepository) KieScannerEvent(org.kie.api.event.kiescanner.KieScannerEvent) KieScannerStatusChangeEvent(org.kie.api.event.kiescanner.KieScannerStatusChangeEvent) KieSession(org.kie.api.runtime.KieSession) File(java.io.File) KieScannerUpdateResultsEvent(org.kie.api.event.kiescanner.KieScannerUpdateResultsEvent) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

KieFileSystemImpl (org.drools.compiler.kie.builder.impl.KieFileSystemImpl)8 ReleaseId (org.kie.api.builder.ReleaseId)8 MemoryFileSystem (org.drools.compiler.compiler.io.memory.MemoryFileSystem)7 Test (org.junit.Test)7 KieFileSystem (org.kie.api.builder.KieFileSystem)7 KieModuleModel (org.kie.api.builder.model.KieModuleModel)5 KieServices (org.kie.api.KieServices)3 File (java.io.File)2 KieBuilder (org.kie.api.builder.KieBuilder)2 ArrayList (java.util.ArrayList)1 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)1 InternalKieScanner (org.drools.compiler.kie.builder.impl.InternalKieScanner)1 MemoryKieModule (org.drools.compiler.kie.builder.impl.MemoryKieModule)1 ReleaseIdImpl (org.drools.compiler.kproject.ReleaseIdImpl)1 KieModuleModelImpl (org.drools.compiler.kproject.models.KieModuleModelImpl)1 KieBaseModel (org.kie.api.builder.model.KieBaseModel)1 KieScannerEvent (org.kie.api.event.kiescanner.KieScannerEvent)1 KieScannerEventListener (org.kie.api.event.kiescanner.KieScannerEventListener)1 KieScannerStatusChangeEvent (org.kie.api.event.kiescanner.KieScannerStatusChangeEvent)1 KieScannerUpdateResultsEvent (org.kie.api.event.kiescanner.KieScannerUpdateResultsEvent)1