Search in sources :

Example 1 with CompositePackageDescr

use of org.drools.compiler.lang.descr.CompositePackageDescr in project drools by kiegroup.

the class ModelBuilderImpl method registerTypeDeclarations.

private void registerTypeDeclarations(Collection<CompositePackageDescr> packages) {
    for (CompositePackageDescr packageDescr : packages) {
        InternalKnowledgePackage pkg = getOrCreatePackageRegistry(packageDescr).getPackage();
        for (TypeDeclarationDescr typeDescr : packageDescr.getTypeDeclarations()) {
            normalizeAnnotations(typeDescr, pkg.getTypeResolver(), false);
            TypeDeclaration type = new TypeDeclaration(typeDescr.getTypeName());
            type.setResource(typeDescr.getResource());
            TypeDeclarationFactory.processAnnotations(typeDescr, type);
            pkg.addTypeDeclaration(type);
        }
    }
}
Also used : TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 2 with CompositePackageDescr

use of org.drools.compiler.lang.descr.CompositePackageDescr in project drools by kiegroup.

the class DroolsModelProvider method getKiePMMLModelWithSourcesCompiled.

@Override
public KiePMMLModelWithSources getKiePMMLModelWithSourcesCompiled(final CompilationDTO<T> compilationDTO) {
    logger.trace("getKiePMMLModelWithSourcesCompiled {} {} {}", compilationDTO.getPackageName(), compilationDTO.getFields(), compilationDTO.getModel());
    if (!(compilationDTO.getHasClassloader() instanceof HasKnowledgeBuilder)) {
        throw new KiePMMLException(String.format(EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE, compilationDTO.getHasClassloader().getClass().getName()));
    }
    try {
        HasKnowledgeBuilder hasKnowledgeBuilder = (HasKnowledgeBuilder) compilationDTO.getHasClassloader();
        KnowledgeBuilderImpl knowledgeBuilder = (KnowledgeBuilderImpl) hasKnowledgeBuilder.getKnowledgeBuilder();
        final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
        KiePMMLDroolsAST kiePMMLDroolsAST = getKiePMMLDroolsASTCommon(compilationDTO.getFields(), compilationDTO.getModel(), fieldTypeMap);
        final DroolsCompilationDTO<T> droolsCompilationDTO = DroolsCompilationDTO.fromCompilationDTO(compilationDTO, fieldTypeMap);
        Map<String, String> sourcesMap = getKiePMMLDroolsModelSourcesMap(droolsCompilationDTO);
        compilationDTO.compileAndLoadClass(sourcesMap);
        PackageDescr packageDescr = getPackageDescr(kiePMMLDroolsAST, compilationDTO.getPackageName());
        String pkgUUID = getPkgUUID(knowledgeBuilder.getReleaseId(), compilationDTO.getPackageName());
        packageDescr.setPreferredPkgUUID(pkgUUID);
        Map<String, String> rulesSourceMap = Collections.unmodifiableMap(getRulesSourceMap(packageDescr));
        KiePMMLDroolsModelWithSources toReturn = new KiePMMLDroolsModelWithSources(compilationDTO.getModelName(), compilationDTO.getPackageName(), compilationDTO.getKieMiningFields(), compilationDTO.getKieOutputFields(), compilationDTO.getKieTargetFields(), sourcesMap, pkgUUID, rulesSourceMap);
        // Needed to compile Rules from PackageDescr
        CompositePackageDescr compositePackageDescr = new CompositePackageDescr(null, packageDescr);
        knowledgeBuilder.buildPackages(Collections.singletonList(compositePackageDescr));
        return toReturn;
    } catch (Exception e) {
        throw new KiePMMLException(e);
    }
}
Also used : KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) HashMap(java.util.HashMap) IOException(java.io.IOException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) HasKnowledgeBuilder(org.kie.pmml.kie.dependencies.HasKnowledgeBuilder) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) PackageDescr(org.drools.drl.ast.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) KiePMMLDroolsModelWithSources(org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources)

Example 3 with CompositePackageDescr

use of org.drools.compiler.lang.descr.CompositePackageDescr in project drools by kiegroup.

the class DroolsModelProvider method getKiePMMLModel.

@Override
public E getKiePMMLModel(final CompilationDTO<T> compilationDTO) {
    logger.trace("getKiePMMLModel {} {} {}", compilationDTO.getPackageName(), compilationDTO.getFields(), compilationDTO.getModel());
    if (!(compilationDTO.getHasClassloader() instanceof HasKnowledgeBuilder)) {
        throw new KiePMMLException(String.format(EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE, compilationDTO.getHasClassloader().getClass().getName()));
    }
    HasKnowledgeBuilder hasKnowledgeBuilder = (HasKnowledgeBuilder) compilationDTO.getHasClassloader();
    KnowledgeBuilderImpl knowledgeBuilder = (KnowledgeBuilderImpl) hasKnowledgeBuilder.getKnowledgeBuilder();
    final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
    KiePMMLDroolsAST kiePMMLDroolsAST = getKiePMMLDroolsASTCommon(compilationDTO.getFields(), compilationDTO.getModel(), fieldTypeMap);
    final DroolsCompilationDTO<T> droolsCompilationDTO = DroolsCompilationDTO.fromCompilationDTO(compilationDTO, fieldTypeMap);
    E toReturn = getKiePMMLDroolsModel(droolsCompilationDTO);
    PackageDescr packageDescr = getPackageDescr(kiePMMLDroolsAST, toReturn.getKModulePackageName());
    // Needed to compile Rules from PackageDescr
    CompositePackageDescr compositePackageDescr = new CompositePackageDescr(null, packageDescr);
    knowledgeBuilder.buildPackages(Collections.singletonList(compositePackageDescr));
    return toReturn;
}
Also used : KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) HashMap(java.util.HashMap) EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE(org.kie.pmml.commons.Constants.EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) HasKnowledgeBuilder(org.kie.pmml.kie.dependencies.HasKnowledgeBuilder) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) PackageDescr(org.drools.drl.ast.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 4 with CompositePackageDescr

use of org.drools.compiler.lang.descr.CompositePackageDescr in project drools by kiegroup.

the class DroolsModelProviderTest method getKiePMMLModelWithKnowledgeBuilder.

@Test
public void getKiePMMLModelWithKnowledgeBuilder() {
    KnowledgeBuilderImpl knowledgeBuilder = new KnowledgeBuilderImpl();
    final CommonCompilationDTO<Scorecard> compilationDTO = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, scorecard, new HasKnowledgeBuilderMock(knowledgeBuilder));
    KiePMMLDroolsModel retrieved = droolsModelProvider.getKiePMMLModel(compilationDTO);
    assertNotNull(retrieved);
    assertTrue(retrieved instanceof KiePMMLDroolsModelTest);
    KiePMMLDroolsModelTest retrievedTest = (KiePMMLDroolsModelTest) retrieved;
    final List<DataField> originalDataFields = pmml.getDataDictionary().getDataFields();
    final List<DataField> retrievedDataFields = retrievedTest.dataDictionary.getDataFields();
    assertEquals(originalDataFields.size(), retrievedTest.dataDictionary.getDataFields().size());
    originalDataFields.forEach(dataField -> {
        Optional<DataField> optRet = retrievedDataFields.stream().filter(retrievedDataField -> dataField.getName().equals(retrievedDataField.getName())).findFirst();
        assertTrue(optRet.isPresent());
        assertEquals(dataField.getDataType(), optRet.get().getDataType());
    });
    assertEquals(pmml.getTransformationDictionary(), retrievedTest.transformationDictionary);
    assertEquals(scorecard, retrievedTest.model);
    String expectedPackageName = getSanitizedPackageName(PACKAGE_NAME);
    assertEquals(expectedPackageName, retrievedTest.getKModulePackageName());
    assertEquals(PACKAGE_NAME, retrievedTest.getName());
    PackageDescr packageDescr = knowledgeBuilder.getPackageDescrs("packagename").get(0);
    assertTrue(packageDescr instanceof CompositePackageDescr);
}
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) KiePMMLDroolsModel(org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModel) DataField(org.dmg.pmml.DataField) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) 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) Test(org.junit.Test)

Example 5 with CompositePackageDescr

use of org.drools.compiler.lang.descr.CompositePackageDescr in project drools by kiegroup.

the class ImpactModelBuilderImpl method buildRules.

protected void buildRules(Collection<CompositePackageDescr> packages) {
    if (hasErrors()) {
        // if Error while generating pojo do not try compile rule as they very likely depends hence fail too.
        return;
    }
    for (CompositePackageDescr packageDescr : packages) {
        setAssetFilter(packageDescr.getFilter());
        PackageRegistry pkgRegistry = getPackageRegistry(packageDescr.getNamespace());
        PackageModel packageModel = getPackageModel(packageDescr, pkgRegistry, packageDescr.getName());
        PackageModel.initPackageModel(this, pkgRegistry.getPackage(), pkgRegistry.getTypeResolver(), packageDescr, packageModel);
        analysisModel.addPackage(new PackageParser(this, packageModel, packageDescr, pkgRegistry).parse());
    }
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) PackageParser(org.drools.impact.analysis.parser.impl.PackageParser) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) PackageModel(org.drools.modelcompiler.builder.PackageModel)

Aggregations

CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)16 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)8 HashMap (java.util.HashMap)6 PackageDescr (org.drools.drl.ast.descr.PackageDescr)6 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)5 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)5 ImportDescr (org.drools.drl.ast.descr.ImportDescr)5 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)4 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3 Optional (java.util.Optional)3 TypeDeclaration (org.drools.core.rule.TypeDeclaration)3 AbstractClassTypeDeclarationDescr (org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr)3 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)3 KiePMMLDroolsAST (org.kie.pmml.models.drools.ast.KiePMMLDroolsAST)3 KiePMMLOriginalTypeGeneratedType (org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType)3 StaticJavaParser.parseImport (com.github.javaparser.StaticJavaParser.parseImport)2 IOException (java.io.IOException)2 Collection (java.util.Collection)2