Search in sources :

Example 1 with MemoryKieModule

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

the class GenerateModel method generate.

public void generate() throws IOException {
    KieServices ks = KieServices.Factory.get();
    final KieBuilderImpl kieBuilder = (KieBuilderImpl) ks.newKieBuilder(kieDmnValidationBaseDir);
    kieBuilder.buildAll(ValidationBootstrapProject::new, s -> !s.contains("src/test/java") && !s.contains("src\\test\\java") && // <- to break circularity which is only caused by the KieBuilder trying to early compile everything by itself
    !s.contains("DMNValidator") && !s.contains("dtanalysis"));
    Results results = kieBuilder.getResults();
    results.getMessages().forEach(m -> LOG.info("{}", m.toString()));
    InternalKieModule kieModule = (InternalKieModule) kieBuilder.getKieModule();
    List<String> generatedFiles = kieModule.getFileNames().stream().filter(f -> f.endsWith("java")).collect(Collectors.toList());
    LOG.info("Executable model will result in {} code generated files...", generatedFiles.size());
    generatedFiles.forEach(LOG::debug);
    MemoryFileSystem mfs = ((MemoryKieModule) ((CanonicalKieModule) kieModule).getInternalKieModule()).getMemoryFileSystem();
    for (String generatedFile : generatedFiles) {
        final MemoryFile f = (MemoryFile) mfs.getFile(generatedFile);
        final Path newFile = Paths.get(kieDmnValidationBaseDir.getAbsolutePath(), "target", "generated-sources", "bootstrap", f.getPath().asString());
        try {
            // NOSONAR javasecurity:S2083 base dir kieDmnValidationBaseDir is provided as configuration by design, static analysis exclusion applies to these 3 lines
            Files.deleteIfExists(newFile);
            // NOSONAR
            Files.createDirectories(newFile.getParent());
            // NOSONAR
            Files.copy(f.getContents(), newFile, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Unable to write file", e);
        }
    }
    byte[] droolsModelFileContent = mfs.getMap().entrySet().stream().filter(kv -> kv.getKey().startsWith(CanonicalKieModule.MODEL_FILE_DIRECTORY) && kv.getKey().endsWith(CanonicalKieModule.MODEL_FILE_NAME)).map(Map.Entry::getValue).findFirst().orElseThrow(RuntimeException::new);
    List<String> lines = new BufferedReader(new StringReader(new String(droolsModelFileContent))).lines().collect(Collectors.toList());
    lines.forEach(LOG::debug);
    String vbMain = new String(IoUtils.readBytesFromInputStream(ValidationBootstrapMain.class.getResourceAsStream("ValidationBootstrapModels.java")));
    String v1x = lines.stream().filter(x -> x.startsWith("org.kie.dmn.validation.DMNv1x.Rules")).findFirst().orElseThrow(RuntimeException::new);
    String v11 = lines.stream().filter(x -> x.startsWith("org.kie.dmn.validation.DMNv1_1.Rules")).findFirst().orElseThrow(RuntimeException::new);
    String v12 = lines.stream().filter(x -> x.startsWith("org.kie.dmn.validation.DMNv1_2.Rules")).findFirst().orElseThrow(RuntimeException::new);
    vbMain = vbMain.replaceAll("\\$V1X_MODEL\\$", v1x);
    vbMain = vbMain.replaceAll("\\$V11_MODEL\\$", v11);
    vbMain = vbMain.replaceAll("\\$V12_MODEL\\$", v12);
    final Path validationEntryPointFile = Paths.get(kieDmnValidationBaseDir.getAbsolutePath(), "target", "generated-sources", "bootstrap", "org", "kie", "dmn", "validation", "bootstrap", "ValidationBootstrapModels.java");
    LOG.info("Writing code generated ValidationBootstrapModels class into: {}", validationEntryPointFile);
    try {
        // NOSONAR javasecurity:S2083 base dir kieDmnValidationBaseDir is provided as configuration by design, static analysis exclusion applies to these 3 lines
        Files.deleteIfExists(validationEntryPointFile);
        // NOSONAR
        Files.createDirectories(validationEntryPointFile.getParent());
        // NOSONAR
        Files.copy(new ByteArrayInputStream(vbMain.getBytes()), validationEntryPointFile, StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Unable to write file", e);
    }
}
Also used : InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) LoggerFactory(org.slf4j.LoggerFactory) Folder(org.drools.compiler.compiler.io.Folder) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) IoUtils(org.drools.core.util.IoUtils) ModelWriter(org.drools.modelcompiler.builder.ModelWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) KieBuilderImpl(org.drools.compiler.kie.builder.impl.KieBuilderImpl) Map(java.util.Map) KieServices(org.kie.api.KieServices) MemoryFile(org.drools.compiler.compiler.io.memory.MemoryFile) Path(java.nio.file.Path) ModelBuilderImpl(org.drools.modelcompiler.builder.ModelBuilderImpl) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) Logger(org.slf4j.Logger) Files(java.nio.file.Files) Results(org.kie.api.builder.Results) IOException(java.io.IOException) CanonicalKieModule(org.drools.modelcompiler.CanonicalKieModule) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) StringReader(java.io.StringReader) CanonicalModelKieProject(org.drools.modelcompiler.builder.CanonicalModelKieProject) Paths(java.nio.file.Paths) BuildContext(org.drools.compiler.kie.builder.impl.BuildContext) BufferedReader(java.io.BufferedReader) MemoryFile(org.drools.compiler.compiler.io.memory.MemoryFile) Path(java.nio.file.Path) KieBuilderImpl(org.drools.compiler.kie.builder.impl.KieBuilderImpl) KieServices(org.kie.api.KieServices) IOException(java.io.IOException) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) Results(org.kie.api.builder.Results) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 2 with MemoryKieModule

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

the class KieRepositoryScannerImpl method loadPomArtifact.

private KieModule loadPomArtifact(ReleaseId releaseId) {
    ArtifactResolver resolver = ArtifactResolver.getResolverFor(releaseId, false);
    if (resolver == null) {
        return null;
    }
    MemoryKieModule kieModule = new MemoryKieModule(releaseId);
    addDependencies(kieModule, resolver, resolver.getPomDirectDependencies(DependencyFilter.COMPILE_FILTER));
    return kieModule;
}
Also used : MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) ArtifactResolver(org.kie.maven.integration.ArtifactResolver)

Example 3 with MemoryKieModule

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

the class MemoryKieModuleResourceProviderTest method testGetResourceForFolderWithOnlySubFolders.

@Test
public void testGetResourceForFolderWithOnlySubFolders() throws Exception {
    MemoryFileSystem mfs = new MemoryFileSystem();
    mfs.createFolder(new MemoryFolder(mfs, KiePath.of("src/main/java")));
    mfs.createFolder(new MemoryFolder(mfs, KiePath.of("src/main/java/org")));
    mfs.createFolder(new MemoryFolder(mfs, KiePath.of("src/main/java/com")));
    MemoryKieModule mkm = new MemoryKieModule(releaseId, kieModuleModel, mfs);
    ResourceProvider resourceProvider = mkm.createResourceProvider();
    String folderContents = IOUtils.toString(resourceProvider.getResource("src/main/java").openStream());
    Assertions.assertThat(folderContents).hasLineCount(2).contains("com", "org");
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) ResourceProvider(org.drools.wiring.api.ResourceProvider) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) MemoryFolder(org.drools.compiler.compiler.io.memory.MemoryFolder) Test(org.junit.Test)

Example 4 with MemoryKieModule

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

the class MemoryKieModuleResourceProviderTest method testGetResourceForFolderWithFilesAndSubFolders.

@Test
public void testGetResourceForFolderWithFilesAndSubFolders() throws Exception {
    MemoryFileSystem mfs = new MemoryFileSystem();
    mfs.createFolder(new MemoryFolder(mfs, KiePath.of("src/main/java")));
    mfs.createFolder(new MemoryFolder(mfs, KiePath.of("src/main/java/org")));
    mfs.createFolder(new MemoryFolder(mfs, KiePath.of("src/main/java/com")));
    mfs.setFileContents(new MemoryFile(mfs, "my-file1", mfs.getFolder("src/main/java")), new byte[10]);
    mfs.setFileContents(new MemoryFile(mfs, "my-file2", mfs.getFolder("src/main/java")), new byte[10]);
    MemoryKieModule mkm = new MemoryKieModule(releaseId, kieModuleModel, mfs);
    ResourceProvider resourceProvider = mkm.createResourceProvider();
    String folderContents = IOUtils.toString(resourceProvider.getResource("src/main/java").openStream());
    Assertions.assertThat(folderContents).hasLineCount(4).contains("com", "org", "my-file1", "my-file2");
}
Also used : MemoryFile(org.drools.compiler.compiler.io.memory.MemoryFile) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) ResourceProvider(org.drools.wiring.api.ResourceProvider) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) MemoryFolder(org.drools.compiler.compiler.io.memory.MemoryFolder) Test(org.junit.Test)

Example 5 with MemoryKieModule

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

the class MemoryKieModuleResourceProviderTest method testGetResourceAsStreamFolderWithOnlySubFolders.

@Test
public void testGetResourceAsStreamFolderWithOnlySubFolders() throws Exception {
    MemoryFileSystem mfs = new MemoryFileSystem();
    mfs.createFolder(new MemoryFolder(mfs, KiePath.of("src/main/java")));
    mfs.createFolder(new MemoryFolder(mfs, KiePath.of("src/main/java/org")));
    mfs.createFolder(new MemoryFolder(mfs, KiePath.of("src/main/java/com")));
    MemoryKieModule mkm = new MemoryKieModule(releaseId, kieModuleModel, mfs);
    ResourceProvider resourceProvider = mkm.createResourceProvider();
    String folderContents = IOUtils.toString(resourceProvider.getResourceAsStream("src/main/java"));
    Assertions.assertThat(folderContents).hasLineCount(2).contains("com", "org");
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) ResourceProvider(org.drools.wiring.api.ResourceProvider) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) MemoryFolder(org.drools.compiler.compiler.io.memory.MemoryFolder) Test(org.junit.Test)

Aggregations

MemoryKieModule (org.drools.compiler.kie.builder.impl.MemoryKieModule)12 MemoryFileSystem (org.drools.compiler.compiler.io.memory.MemoryFileSystem)11 Test (org.junit.Test)8 ResourceProvider (org.drools.wiring.api.ResourceProvider)6 KieServices (org.kie.api.KieServices)5 MemoryFile (org.drools.compiler.compiler.io.memory.MemoryFile)4 MemoryFolder (org.drools.compiler.compiler.io.memory.MemoryFolder)4 ReleaseId (org.kie.api.builder.ReleaseId)4 StringReader (java.io.StringReader)3 Folder (org.drools.compiler.compiler.io.Folder)3 Resource (org.kie.api.io.Resource)3 InternalKieContainer (org.drools.core.impl.InternalKieContainer)2 KieBuilder (org.kie.api.builder.KieBuilder)2 KieModule (org.kie.api.builder.KieModule)2 KieBaseModel (org.kie.api.builder.model.KieBaseModel)2 KieModuleModel (org.kie.api.builder.model.KieModuleModel)2 KieContainer (org.kie.api.runtime.KieContainer)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1