use of org.drools.compiler.kie.builder.impl.InternalKieModule in project drools by kiegroup.
the class DMNUpdateTest method test_as_kie_wb_common_services_backend_Builder2.
@Test
public void test_as_kie_wb_common_services_backend_Builder2() throws Exception {
final KieServices ks = KieServices.Factory.get();
final ReleaseId v100 = ks.newReleaseId("org.kie", "dmn-test", "1.0.0");
final KieModule kieModule = KieHelper.createAndDeployJar(ks, v100, ks.getResources().newClassPathResource("v1_2/dmn-hotcold.dmn", this.getClass()));
final KieContainer kieContainer = ks.newKieContainer(v100);
final KieSession kieSession = kieContainer.newKieSession();
final DMNRuntime runtime = kieSession.getKieRuntime(DMNRuntime.class);
Assert.assertNotNull(runtime);
assertThat(runtime.getModels(), hasSize(1));
checkDMNHotColdDMN12WithNSScattered(runtime);
// the below is performed by the WB at: https://github.com/kiegroup/kie-wb-common/blob/9e6b6da145e61ac8f5a9f7c0259d44aa9d090a2b/kie-wb-common-services/kie-wb-common-services-backend/src/main/java/org/kie/workbench/common/services/backend/builder/core/Builder.java#L592-L620
final KieProject kieProject = new KieModuleKieProject((InternalKieModule) kieModule, null);
final KieContainer kieContainer2 = new KieContainerImpl(kieProject, ks.getRepository(), v100);
// exhibit the issue.
final KieSession kieSession2 = kieContainer2.newKieSession();
final DMNRuntime runtime2 = kieSession2.getKieRuntime(DMNRuntime.class);
Assert.assertNotNull(runtime2);
assertThat(runtime2.getModels(), hasSize(1));
checkDMNHotColdDMN12WithNSScattered(runtime2);
}
use of org.drools.compiler.kie.builder.impl.InternalKieModule in project drools by kiegroup.
the class DMNRuntimeUtil method createJarIgnoringErrors.
public static byte[] createJarIgnoringErrors(KieServices ks, ReleaseId releaseId, Resource... resources) {
KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId);
for (int i = 0; i < resources.length; i++) {
if (resources[i] != null) {
kfs.write(resources[i]);
}
}
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
((InternalKieBuilder) kieBuilder).buildAll(o -> true);
InternalKieModule kieModule = (InternalKieModule) ((InternalKieBuilder) kieBuilder).getKieModuleIgnoringErrors();
byte[] jar = kieModule.getBytes();
return jar;
}
use of org.drools.compiler.kie.builder.impl.InternalKieModule in project drools by kiegroup.
the class GeneratedClassNamesTest method testKjarWithoutGeneratedClassNames.
@Test
public void testKjarWithoutGeneratedClassNames() {
// Build a kjar without generated-class-names file (= simulating a build by old drools version)
ProjectClassLoaderTestUtil.setEnableStoreFirst(false);
String str = "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + " $p : Person(name == \"Mario\")\n" + "then\n" + " System.out.println(\"hello\");\n" + "end";
KieServices ks = KieServices.get();
ReleaseId releaseId = ks.newReleaseId("org.kie", "kjar-test-" + UUID.randomUUID(), "1.0");
KieBuilder kieBuilder = createKieBuilder(ks, getDefaultKieModuleModel(ks), releaseId, toKieFiles(new String[] { str }));
final InternalKieModule kieModule = (InternalKieModule) kieBuilder.getKieModule();
byte[] kjar = kieModule.getBytes();
Resource kjarResource = ks.getResources().newByteArrayResource(kjar);
// cleanup
ks.getRepository().removeKieModule(releaseId);
// Load the kjar with the current drools version
ProjectClassLoaderTestUtil.setEnableStoreFirst(true);
ks.getRepository().addKieModule(kjarResource);
KieContainer kcontainer = ks.newKieContainer(releaseId);
KieModule kieModule2 = ((KieContainerImpl) kcontainer).getKieModuleForKBase("kbase");
assertTrue(kieModule2 instanceof CanonicalKieModule);
KieSession ksession = kcontainer.newKieSession();
Set<String> generatedClassNames = ((CanonicalKieModule) kieModule2).getGeneratedClassNames();
assertTrue(generatedClassNames.isEmpty());
Person me = new Person("Mario", 40);
ksession.insert(me);
int fired = ksession.fireAllRules();
assertEquals(1, fired);
}
use of org.drools.compiler.kie.builder.impl.InternalKieModule in project drools by kiegroup.
the class CanonicalModelKieProject method writeProjectOutput.
@Override
public void writeProjectOutput(MemoryFileSystem trgMfs, BuildContext buildContext) {
MemoryFileSystem srcMfs = new MemoryFileSystem();
ModelWriter modelWriter = new ModelWriter();
Collection<String> modelFiles = new HashSet<>();
Collection<String> sourceFiles = new HashSet<>();
Map<String, List<String>> modelsByKBase = new HashMap<>();
for (Map.Entry<String, ModelBuilderImpl> modelBuilder : modelBuilders.entrySet()) {
ModelWriter.Result result = modelWriter.writeModel(srcMfs, modelBuilder.getValue().getPackageSources());
modelFiles.addAll(result.getModelFiles());
sourceFiles.addAll(result.getSourceFiles());
List<String> modelFilesForKieBase = new ArrayList<>();
modelFilesForKieBase.addAll(result.getModelFiles());
modelFilesForKieBase.addAll(((CanonicalModelBuildContext) buildContext).getNotOwnedModelFiles(modelBuilders, modelBuilder.getKey()));
modelsByKBase.put(modelBuilder.getKey(), modelFilesForKieBase);
}
InternalKieModule kieModule = getInternalKieModule();
ModelSourceClass modelSourceClass = new ModelSourceClass(kieModule.getReleaseId(), kieModule.getKieModuleModel().getKieBaseModels(), modelsByKBase, hasDynamicClassLoader());
String projectSourcePath = modelWriter.getBasePath().asString() + "/" + modelSourceClass.getName();
srcMfs.write(projectSourcePath, modelSourceClass.generate().getBytes());
sourceFiles.add(projectSourcePath);
Set<KiePath> origFileNames = new HashSet<>(trgMfs.getFilePaths());
String[] sources = sourceFiles.toArray(new String[sourceFiles.size()]);
if (sources.length != 0) {
CompilationResult res = getCompiler().compile(sources, srcMfs, trgMfs, getClassLoader());
Stream.of(res.getErrors()).collect(groupingBy(CompilationProblem::getFileName)).forEach((name, errors) -> {
errors.forEach(m -> buildContext.getMessages().addMessage(new CompilationProblemAdapter(m)));
File srcFile = srcMfs.getFile(name);
if (srcFile instanceof MemoryFile) {
String src = new String(srcMfs.getFileContents((MemoryFile) srcFile));
buildContext.getMessages().addMessage(Message.Level.ERROR, name, "Java source of " + name + " in error:\n" + src);
}
});
for (CompilationProblem problem : res.getWarnings()) {
buildContext.getMessages().addMessage(new CompilationProblemAdapter(problem));
}
}
if (ProjectClassLoader.isEnableStoreFirst()) {
Set<KiePath> generatedClassPaths = new HashSet<>(trgMfs.getFilePaths());
generatedClassPaths.removeAll(origFileNames);
Set<String> generatedClassNames = generatedClassPaths.stream().map(KiePath::asString).map(ClassUtils::convertResourceToClassName).collect(Collectors.toSet());
modelWriter.writeGeneratedClassNamesFile(generatedClassNames, trgMfs, getInternalKieModule().getReleaseId());
}
modelWriter.writeModelFile(modelFiles, trgMfs, getInternalKieModule().getReleaseId());
}
use of org.drools.compiler.kie.builder.impl.InternalKieModule in project drools by kiegroup.
the class CanonicalKieModule method findProcesses.
private List<Process> findProcesses(InternalKieModule kieModule, KieBaseModelImpl kBaseModel) {
List<Process> processes = new ArrayList<>();
Collection<KiePackage> pkgs = kieModule.getKnowledgePackagesForKieBase(kBaseModel.getName());
if (pkgs == null) {
List<Resource> processResources = kieModule.getFileNames().stream().filter(fileName -> {
ResourceType resourceType = determineResourceType(fileName);
return resourceType == ResourceType.DRF || resourceType == ResourceType.BPMN2;
}).map(fileName -> {
final Resource processResource = kieModule.getResource(fileName);
processResource.setResourceType(determineResourceType(fileName));
return processResource;
}).collect(toList());
if (!processResources.isEmpty()) {
KnowledgeBuilderImpl kbuilder = (KnowledgeBuilderImpl) KnowledgeBuilderFactory.newKnowledgeBuilder(createBuilderConfiguration(kBaseModel, moduleClassLoader));
for (Resource processResource : processResources) {
kbuilder.add(processResource, processResource.getResourceType());
}
for (InternalKnowledgePackage knowledgePackage : kbuilder.getPackages()) {
processes.addAll(knowledgePackage.getProcesses());
}
}
} else {
for (KiePackage pkg : pkgs) {
processes.addAll(pkg.getProcesses());
}
}
return processes;
}
Aggregations