Search in sources :

Example 6 with CompositePackageDescr

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

the class ImpactModelBuilderImpl method addPackage.

@Override
public void addPackage(final PackageDescr packageDescr) {
    if (compositePackagesMap == null) {
        compositePackagesMap = new HashMap<>();
        if (compositePackages != null) {
            for (CompositePackageDescr pkg : compositePackages) {
                compositePackagesMap.put(pkg.getNamespace(), pkg);
            }
        } else {
            compositePackagesMap.put(packageDescr.getNamespace(), new CompositePackageDescr(packageDescr.getResource(), packageDescr));
        }
        compositePackages = null;
    }
    CompositePackageDescr pkgDescr = compositePackagesMap.get(packageDescr.getNamespace());
    if (pkgDescr == null) {
        compositePackagesMap.put(packageDescr.getNamespace(), new CompositePackageDescr(packageDescr.getResource(), packageDescr));
    } else {
        pkgDescr.addPackageDescr(packageDescr.getResource(), packageDescr);
    }
    PackageRegistry pkgRegistry = getOrCreatePackageRegistry(packageDescr);
    InternalKnowledgePackage pkg = pkgRegistry.getPackage();
    for (final ImportDescr importDescr : packageDescr.getImports()) {
        pkgRegistry.addImport(importDescr);
    }
    for (GlobalDescr globalDescr : packageDescr.getGlobals()) {
        try {
            Class<?> globalType = pkg.getTypeResolver().resolveType(globalDescr.getType());
            addGlobal(globalDescr.getIdentifier(), globalType);
            pkg.addGlobal(globalDescr.getIdentifier(), globalType);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 7 with CompositePackageDescr

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

the class ModelBuilderImpl method addPackage.

@Override
public void addPackage(final PackageDescr packageDescr) {
    if (compositePackagesMap == null) {
        compositePackagesMap = new HashMap<>();
    }
    CompositePackageDescr pkgDescr = compositePackagesMap.get(packageDescr.getNamespace());
    if (pkgDescr == null) {
        compositePackagesMap.put(packageDescr.getNamespace(), new CompositePackageDescr(packageDescr.getResource(), packageDescr));
    } else {
        pkgDescr.addPackageDescr(packageDescr.getResource(), packageDescr);
    }
    PackageRegistry pkgRegistry = getOrCreatePackageRegistry(packageDescr);
    InternalKnowledgePackage pkg = pkgRegistry.getPackage();
    for (final ImportDescr importDescr : packageDescr.getImports()) {
        pkgRegistry.addImport(importDescr);
    }
    for (GlobalDescr globalDescr : packageDescr.getGlobals()) {
        try {
            Class<?> globalType = pkg.getTypeResolver().resolveType(globalDescr.getType());
            addGlobal(globalDescr.getIdentifier(), globalType);
            pkg.addGlobal(globalDescr.getIdentifier(), globalType);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 8 with CompositePackageDescr

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

the class ModelBuilderImpl method buildDeclaredTypes.

private void buildDeclaredTypes(Collection<CompositePackageDescr> packages) {
    for (CompositePackageDescr packageDescr : packages) {
        PackageRegistry pkgRegistry = getPackageRegistry(packageDescr.getNamespace());
        generatePOJOs(packageDescr, pkgRegistry);
    }
    List<GeneratedClassWithPackage> allGeneratedPojos = packageModels.values().stream().flatMap(p -> p.getGeneratedPOJOsSource().stream().map(c -> new GeneratedClassWithPackage(c, p.getName(), p.getImports(), p.getStaticImports()))).collect(Collectors.toList());
    Map<String, Class<?>> allCompiledClasses = compileType(this, getBuilderConfiguration().getClassLoader(), allGeneratedPojos);
    ((CanonicalModelBuildContext) getBuildContext()).registerGeneratedPojos(allGeneratedPojos, allCompiledClasses);
}
Also used : TypeDeclaration(org.drools.core.rule.TypeDeclaration) DRLIdGenerator(org.drools.modelcompiler.builder.generator.DRLIdGenerator) TypeResolver(org.drools.core.addon.TypeResolver) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) ModelGenerator.generateModel(org.drools.modelcompiler.builder.generator.ModelGenerator.generateModel) HashMap(java.util.HashMap) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) TypeDeclarationFactory(org.drools.compiler.builder.impl.TypeDeclarationFactory) AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) Function(java.util.function.Function) POJOGenerator.compileType(org.drools.modelcompiler.builder.generator.declaredtype.POJOGenerator.compileType) ReleaseId(org.kie.api.builder.ReleaseId) HashSet(java.util.HashSet) ImportDescr(org.drools.drl.ast.descr.ImportDescr) Drools.hasMvel(org.drools.core.util.Drools.hasMvel) Map(java.util.Map) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) ResultSeverity(org.kie.internal.builder.ResultSeverity) GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) ImportDeclaration(org.drools.core.rule.ImportDeclaration) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) EnumDeclarationDescr(org.drools.drl.ast.descr.EnumDeclarationDescr) ClassDefinitionFactory.createClassDefinition(org.drools.compiler.builder.impl.ClassDefinitionFactory.createClassDefinition) Collectors(java.util.stream.Collectors) List(java.util.List) UnsupportedFeatureError(org.drools.modelcompiler.builder.errors.UnsupportedFeatureError) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) BuildContext(org.drools.compiler.kie.builder.impl.BuildContext) Optional(java.util.Optional) POJOGenerator(org.drools.modelcompiler.builder.generator.declaredtype.POJOGenerator) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) StaticJavaParser.parseImport(com.github.javaparser.StaticJavaParser.parseImport) StringUtils(org.drools.core.util.StringUtils) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 9 with CompositePackageDescr

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

the class CompositeKnowledgeBuilderImpl method registerPackageDescr.

private void registerPackageDescr(ResourceDescr resourceDescr, Map<String, CompositePackageDescr> packages, Resource resource, PackageDescr packageDescr) {
    if (packageDescr != null) {
        CompositePackageDescr compositePackageDescr = packages.get(packageDescr.getNamespace());
        if (compositePackageDescr == null) {
            compositePackageDescr = packageDescr instanceof CompositePackageDescr ? ((CompositePackageDescr) packageDescr) : new CompositePackageDescr(resource, packageDescr);
            packages.put(packageDescr.getNamespace(), compositePackageDescr);
        } else {
            compositePackageDescr.addPackageDescr(resource, packageDescr);
        }
        compositePackageDescr.addFilter(resourceDescr.getFilter());
    }
}
Also used : CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 10 with CompositePackageDescr

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

the class KnowledgeBuilderImpl method decisionTableToPackageDescr.

PackageDescr decisionTableToPackageDescr(Resource resource, ResourceConfiguration configuration) throws DroolsParserException {
    DecisionTableConfiguration dtableConfiguration = configuration instanceof DecisionTableConfiguration ? (DecisionTableConfiguration) configuration : new DecisionTableConfigurationImpl();
    if (!dtableConfiguration.getRuleTemplateConfigurations().isEmpty()) {
        List<String> generatedDrls = DecisionTableFactory.loadFromInputStreamWithTemplates(resource, dtableConfiguration);
        if (generatedDrls.size() == 1) {
            return generatedDrlToPackageDescr(resource, generatedDrls.get(0));
        }
        CompositePackageDescr compositePackageDescr = null;
        for (String generatedDrl : generatedDrls) {
            PackageDescr packageDescr = generatedDrlToPackageDescr(resource, generatedDrl);
            if (packageDescr != null) {
                if (compositePackageDescr == null) {
                    compositePackageDescr = new CompositePackageDescr(resource, packageDescr);
                } else {
                    compositePackageDescr.addPackageDescr(resource, packageDescr);
                }
            }
        }
        return compositePackageDescr;
    }
    dtableConfiguration.setTrimCell(this.configuration.isTrimCellsInDTable());
    String generatedDrl = DecisionTableFactory.loadFromResource(resource, dtableConfiguration);
    return generatedDrlToPackageDescr(resource, generatedDrl);
}
Also used : CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) DecisionTableConfiguration(org.kie.internal.builder.DecisionTableConfiguration) PackageDescr(org.drools.drl.ast.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) DecisionTableConfigurationImpl(org.drools.core.builder.conf.impl.DecisionTableConfigurationImpl)

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