use of org.drools.modelcompiler.builder.GeneratedFile in project drools by kiegroup.
the class DroolsModelProvider method generateRulesFiles.
/**
* This method depends on exec-model. Be aware in case of refactoring
* @param packageDescr
* @return
*/
protected List<GeneratedFile> generateRulesFiles(PackageDescr packageDescr) {
ModelBuilderImpl<PackageSources> modelBuilder = new ModelBuilderImpl<>(PackageSources::dumpSources, new KnowledgeBuilderConfigurationImpl(getClass().getClassLoader()), new ReleaseIdImpl("dummy:dummy:0.0.0"), false);
CompositeKnowledgeBuilder batch = modelBuilder.batch();
batch.add(new DescrResource(packageDescr), ResourceType.DESCR);
try {
batch.build();
if (modelBuilder.hasErrors()) {
StringBuilder builder = new StringBuilder();
for (DroolsError error : modelBuilder.getErrors().getErrors()) {
logger.error(error.toString());
builder.append(error.toString()).append(" ");
}
throw new KiePMMLException(builder.toString());
}
} catch (Exception e) {
logger.error(e.getMessage());
StringBuilder builder = new StringBuilder(e.getMessage()).append(" ");
for (DroolsError error : modelBuilder.getErrors().getErrors()) {
logger.error(error.toString());
builder.append(error.toString()).append(" ");
}
throw new RuntimeException(builder.toString(), e);
}
return generateModels(modelBuilder).stream().map(f -> new GeneratedFile(GeneratedFile.Type.RULE, f.getPath(), new String(f.getData()))).collect(toList());
}
use of org.drools.modelcompiler.builder.GeneratedFile in project drools by kiegroup.
the class DroolsModelProviderTest method generateRulesFiles.
@Test
public void generateRulesFiles() {
KnowledgeBuilderImpl knowledgeBuilder = new KnowledgeBuilderImpl();
final CommonCompilationDTO<Scorecard> compilationDTO = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, scorecard, new HasKnowledgeBuilderMock(knowledgeBuilder));
droolsModelProvider.getKiePMMLModelWithSources(compilationDTO);
String expectedPackageName = compilationDTO.getPackageName();
PackageDescr packageDescr = knowledgeBuilder.getPackageDescrs(expectedPackageName).get(0);
final List<GeneratedFile> retrieved = droolsModelProvider.generateRulesFiles(packageDescr);
assertNotNull(retrieved);
final String rootPath = expectedPackageName.replace('.', '/') + "/";
packageDescr.getTypeDeclarations().forEach(typeDeclarationDescr -> {
String expectedPath = rootPath + typeDeclarationDescr.getTypeName() + ".java";
assertTrue(retrieved.stream().anyMatch(generatedFile -> generatedFile.getPath().equals(expectedPath)));
});
String pkgUUID = packageDescr.getPreferredPkgUUID().get();
String expectedRule = rootPath + "Rules" + pkgUUID + ".java";
assertTrue(retrieved.stream().anyMatch(generatedFile -> generatedFile.getPath().equals(expectedRule)));
String expectedDomain = rootPath + "DomainClassesMetadata" + pkgUUID + ".java";
assertTrue(retrieved.stream().anyMatch(generatedFile -> generatedFile.getPath().equals(expectedDomain)));
}
Aggregations