Search in sources :

Example 1 with GeneratedFile

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());
}
Also used : ModelBuilderImpl(org.drools.modelcompiler.builder.ModelBuilderImpl) KiePMMLDroolsModel(org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModel) DescrResource(org.drools.core.io.impl.DescrResource) EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE(org.kie.pmml.commons.Constants.EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Model(org.dmg.pmml.Model) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) DroolsCompilationDTO(org.kie.pmml.models.drools.dto.DroolsCompilationDTO) KiePMMLDescrFactory.getBaseDescr(org.kie.pmml.models.drools.commons.factories.KiePMMLDescrFactory.getBaseDescr) KiePMMLDroolsType(org.kie.pmml.models.drools.ast.KiePMMLDroolsType) LoggerFactory(org.slf4j.LoggerFactory) StringUtils.getPkgUUID(org.drools.core.util.StringUtils.getPkgUUID) HashMap(java.util.HashMap) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ModelImplementationProvider(org.kie.pmml.compiler.api.provider.ModelImplementationProvider) ResourceType(org.kie.api.io.ResourceType) ArrayList(java.util.ArrayList) Field(org.dmg.pmml.Field) Map(java.util.Map) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) KiePMMLModelWithSources(org.kie.pmml.commons.model.KiePMMLModelWithSources) ModelBuilderImpl(org.drools.modelcompiler.builder.ModelBuilderImpl) CompilationDTO(org.kie.pmml.compiler.api.dto.CompilationDTO) PackageSources(org.drools.modelcompiler.builder.PackageSources) Logger(org.slf4j.Logger) PackageDescr(org.drools.drl.ast.descr.PackageDescr) IOException(java.io.IOException) HasKnowledgeBuilder(org.kie.pmml.kie.dependencies.HasKnowledgeBuilder) Collectors(java.util.stream.Collectors) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) GeneratedFile(org.drools.modelcompiler.builder.GeneratedFile) KiePMMLDataDictionaryASTFactory(org.kie.pmml.models.drools.ast.factories.KiePMMLDataDictionaryASTFactory) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ReleaseIdImpl(org.kie.util.maven.support.ReleaseIdImpl) DroolsError(org.drools.drl.parser.DroolsError) KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) KiePMMLDroolsModelWithSources(org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) Collections(java.util.Collections) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) ReleaseIdImpl(org.kie.util.maven.support.ReleaseIdImpl) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DescrResource(org.drools.core.io.impl.DescrResource) IOException(java.io.IOException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) GeneratedFile(org.drools.modelcompiler.builder.GeneratedFile) DroolsError(org.drools.drl.parser.DroolsError) PackageSources(org.drools.modelcompiler.builder.PackageSources) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 2 with GeneratedFile

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)));
}
Also used : KiePMMLDroolsModel(org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModel) BeforeClass(org.junit.BeforeClass) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) DroolsCompilationDTO(org.kie.pmml.models.drools.dto.DroolsCompilationDTO) Scorecard(org.dmg.pmml.scorecard.Scorecard) KiePMMLDroolsType(org.kie.pmml.models.drools.ast.KiePMMLDroolsType) HashMap(java.util.HashMap) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) Field(org.dmg.pmml.Field) TransformationDictionary(org.dmg.pmml.TransformationDictionary) PMML_MODEL(org.kie.pmml.api.enums.PMML_MODEL) DerivedField(org.dmg.pmml.DerivedField) Map(java.util.Map) KiePMMLModelUtils.getSanitizedPackageName(org.kie.pmml.commons.utils.KiePMMLModelUtils.getSanitizedPackageName) PMML(org.dmg.pmml.PMML) PACKAGE_NAME(org.kie.pmml.commons.Constants.PACKAGE_NAME) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) DataDictionary(org.dmg.pmml.DataDictionary) Test(org.junit.Test) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) CommonTestingUtils.getFieldsFromDataDictionaryAndTransformationDictionaryAndLocalTransformations(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionaryAndTransformationDictionaryAndLocalTransformations) GeneratedFile(org.drools.modelcompiler.builder.GeneratedFile) HasKnowledgeBuilderMock(org.kie.pmml.models.drools.commons.implementations.HasKnowledgeBuilderMock) DataField(org.dmg.pmml.DataField) TestUtils(org.kie.pmml.compiler.api.testutils.TestUtils) List(java.util.List) CommonCompilationDTO(org.kie.pmml.compiler.api.dto.CommonCompilationDTO) Optional(java.util.Optional) HasClassLoaderMock(org.kie.pmml.compiler.commons.mocks.HasClassLoaderMock) KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) KiePMMLDroolsModelWithSources(org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) Collections(java.util.Collections) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) Assert.assertEquals(org.junit.Assert.assertEquals) KiePMMLModelUtils.getSanitizedClassName(org.kie.pmml.commons.utils.KiePMMLModelUtils.getSanitizedClassName) HasKnowledgeBuilderMock(org.kie.pmml.models.drools.commons.implementations.HasKnowledgeBuilderMock) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) Scorecard(org.dmg.pmml.scorecard.Scorecard) PackageDescr(org.drools.drl.ast.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) GeneratedFile(org.drools.modelcompiler.builder.GeneratedFile) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Field (org.dmg.pmml.Field)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)2 PackageDescr (org.drools.drl.ast.descr.PackageDescr)2 GeneratedFile (org.drools.modelcompiler.builder.GeneratedFile)2 DATA_TYPE (org.kie.pmml.api.enums.DATA_TYPE)2 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)2 KiePMMLDroolsAST (org.kie.pmml.models.drools.ast.KiePMMLDroolsAST)2 KiePMMLDroolsType (org.kie.pmml.models.drools.ast.KiePMMLDroolsType)2 KiePMMLDroolsModel (org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModel)2 KiePMMLDroolsModelWithSources (org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources)2 DroolsCompilationDTO (org.kie.pmml.models.drools.dto.DroolsCompilationDTO)2 KiePMMLOriginalTypeGeneratedType (org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType)2 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1