Search in sources :

Example 6 with TypeResolver

use of org.drools.core.addon.TypeResolver in project drools by kiegroup.

the class ModelGenerator method generateModel.

public static void generateModel(KnowledgeBuilderImpl kbuilder, InternalKnowledgePackage pkg, PackageDescr packageDescr, PackageModel packageModel) {
    TypeResolver typeResolver = pkg.getTypeResolver();
    List<RuleDescr> ruleDescrs = packageDescr.getRules();
    if (ruleDescrs.isEmpty()) {
        return;
    }
    for (RuleDescr descr : ruleDescrs) {
        RuleContext context = new RuleContext(kbuilder, packageModel, typeResolver, descr);
        context.setDialectFromAttributes(packageDescr.getAttributes());
        if (descr instanceof QueryDescr) {
            QueryGenerator.processQueryDef(packageModel, context);
        }
    }
    int parallelRulesBuildThreshold = kbuilder.getBuilderConfiguration().getParallelRulesBuildThreshold();
    boolean parallelRulesBuild = parallelRulesBuildThreshold != -1 && ruleDescrs.size() > parallelRulesBuildThreshold;
    if (parallelRulesBuild) {
        List<RuleContext> ruleContexts = new ArrayList<>();
        int i = 0;
        for (RuleDescr ruleDescr : packageDescr.getRules()) {
            ruleContexts.add(new RuleContext(kbuilder, packageModel, typeResolver, ruleDescr, i++));
        }
        KnowledgeBuilderImpl.ForkJoinPoolHolder.COMPILER_POOL.submit(() -> ruleContexts.parallelStream().forEach(context -> processRuleDescr(context, packageDescr))).join();
    } else {
        int i = 0;
        for (RuleDescr ruleDescr : packageDescr.getRules()) {
            processRuleDescr(new RuleContext(kbuilder, packageModel, typeResolver, ruleDescr, i++), packageDescr);
        }
    }
}
Also used : QueryDescr(org.drools.drl.ast.descr.QueryDescr) TypeResolver(org.drools.core.addon.TypeResolver) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ArrayList(java.util.ArrayList)

Example 7 with TypeResolver

use of org.drools.core.addon.TypeResolver in project drools by kiegroup.

the class POJOGenerator method findPOJOorGenerate.

public void findPOJOorGenerate() {
    TypeResolver typeResolver = pkg.getTypeResolver();
    Set<String> generatedPojos = new HashSet<>();
    for (TypeDeclarationDescr typeDescr : packageDescr.getTypeDeclarations()) {
        if (!generatedPojos.add(typeDescr.getFullTypeName())) {
            builder.addBuilderResult(new DuplicatedDeclarationError(typeDescr.getFullTypeName()));
            continue;
        }
        try {
            Class<?> type = typeResolver.resolveType(typeDescr.getFullTypeName());
            checkRedeclarationCompatibility(type, typeDescr);
            processTypeMetadata(type, typeDescr.getAnnotations());
        } catch (ClassNotFoundException e) {
            createPOJO(typeDescr);
        }
    }
    for (EnumDeclarationDescr enumDescr : packageDescr.getEnumDeclarations()) {
        try {
            Class<?> type = typeResolver.resolveType(enumDescr.getFullTypeName());
            processTypeMetadata(type, enumDescr.getAnnotations());
        } catch (ClassNotFoundException e) {
            TypeDeclaration generatedEnum = new EnumGenerator().generate(enumDescr);
            packageModel.addGeneratedPOJO(generatedEnum);
            addTypeMetadata(enumDescr.getTypeName());
        }
    }
}
Also used : DuplicatedDeclarationError(org.drools.modelcompiler.builder.errors.DuplicatedDeclarationError) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) TypeResolver(org.drools.core.addon.TypeResolver) EnumDeclarationDescr(org.drools.drl.ast.descr.EnumDeclarationDescr) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) HashSet(java.util.HashSet)

Example 8 with TypeResolver

use of org.drools.core.addon.TypeResolver in project drools by kiegroup.

the class TypeDeclarationNameResolver method resolveTypes.

public void resolveTypes(Collection<? extends PackageDescr> packageDescrs, List<TypeDefinition> unresolvedTypes) {
    for (PackageDescr packageDescr : packageDescrs) {
        TypeResolver typeResolver = kbuilder.getPackageRegistry(packageDescr.getName()).getTypeResolver();
        ensureQualifiedDeclarationName(unresolvedTypes, packageDescr, typeResolver);
    }
    for (PackageDescr packageDescr : packageDescrs) {
        TypeResolver typeResolver = kbuilder.getPackageRegistry(packageDescr.getName()).getTypeResolver();
        qualifyNames(unresolvedTypes, packageDescr, typeResolver);
    }
}
Also used : TypeResolver(org.drools.core.addon.TypeResolver) PackageDescr(org.drools.drl.ast.descr.PackageDescr)

Example 9 with TypeResolver

use of org.drools.core.addon.TypeResolver in project drools by kiegroup.

the class ConstraintParserTest method setup.

@Before
public void setup() {
    PackageModel packageModel = new PackageModel("org.kie.test:constraint-parser-test:1.0.0", "org.kie.test", null, null, null);
    Set<String> imports = new HashSet<>();
    imports.add("org.drools.modelcompiler.domain.Person");
    TypeResolver typeResolver = new ClassTypeResolver(imports, getClass().getClassLoader());
    RuleContext context = new RuleContext(null, packageModel, typeResolver, null, 0);
    parser = ConstraintParser.defaultConstraintParser(context, packageModel);
}
Also used : RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) TypeResolver(org.drools.core.addon.TypeResolver) ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) PackageModel(org.drools.modelcompiler.builder.PackageModel) ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 10 with TypeResolver

use of org.drools.core.addon.TypeResolver in project drools by kiegroup.

the class GeneratorHelper method getTypeResolver.

static TypeResolver getTypeResolver(final InvokerStub stub, final ReteEvaluator reteEvaluator, final ClassLoader classLoader) {
    InternalKnowledgePackage pkg = reteEvaluator.getKnowledgeBase().getPackage(stub.getPackageName());
    TypeResolver typeResolver = pkg == null ? null : pkg.getTypeResolver();
    if (typeResolver == null) {
        Set<String> imports = new HashSet<String>();
        for (String imp : stub.getPackageImports()) {
            imports.add(imp);
        }
        typeResolver = new ClassTypeResolver(imports, classLoader, stub.getPackageName());
    }
    return typeResolver;
}
Also used : TypeResolver(org.drools.core.addon.TypeResolver) ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) HashSet(java.util.HashSet)

Aggregations

TypeResolver (org.drools.core.addon.TypeResolver)12 HashSet (java.util.HashSet)7 ClassTypeResolver (org.drools.core.addon.ClassTypeResolver)6 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)3 MvelCompilerContext (org.drools.mvelcompiler.context.MvelCompilerContext)3 ArrayList (java.util.ArrayList)2 Gender (org.drools.Gender)2 Person (org.drools.Person)2 TypeDeclarationDescr (org.drools.drl.ast.descr.TypeDeclarationDescr)2 TypeFieldDescr (org.drools.drl.ast.descr.TypeFieldDescr)2 TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)1 RuleBuildError (org.drools.compiler.compiler.RuleBuildError)1 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)1 ClassDefinition (org.drools.core.factmodel.ClassDefinition)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1 EnumDeclarationDescr (org.drools.drl.ast.descr.EnumDeclarationDescr)1