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);
}
}
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;
}
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");
}
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");
}
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");
}
Aggregations