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