Search in sources :

Example 11 with TypeResolver

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

the class ClassTypeResolverTest method testResolveFullTypeName.

@Test
public void testResolveFullTypeName() throws Exception {
    final TypeResolver resolver = new ClassTypeResolver(new HashSet(), Thread.currentThread().getContextClassLoader());
    resolver.addImport("org.drools.core.test.model.Cheese");
    resolver.addImport("org.drools.core.test.model.FirstClass");
    assertEquals("org.drools.core.test.model.Cheese", resolver.getFullTypeName("Cheese"));
    assertEquals("org.drools.core.test.model.FirstClass", resolver.getFullTypeName("FirstClass"));
}
Also used : ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) TypeResolver(org.drools.core.addon.TypeResolver) ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 12 with TypeResolver

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

the class Evaluator method compileWithMvelCompiler.

private static CompiledResult compileWithMvelCompiler(Object compiledExpression, Map<String, Object> vars, ClassLoader classLoader) {
    String expressionString = compiledExpression.toString();
    Set<String> imports = new HashSet<>();
    imports.add("java.util.List");
    imports.add("java.util.ArrayList");
    imports.add("java.util.HashMap");
    imports.add("java.util.Map");
    imports.add("java.math.BigDecimal");
    imports.add("org.drools.Address");
    TypeResolver classTypeResolver = new ClassTypeResolver(imports, classLoader);
    MvelCompilerContext context = new MvelCompilerContext(classTypeResolver);
    for (Map.Entry<String, Object> o : vars.entrySet()) {
        Object value1 = o.getValue();
        if (value1 != null) {
            context.addDeclaration(o.getKey(), value1.getClass());
        }
    }
    MvelCompiler mvelCompiler = new MvelCompiler(context);
    ConstraintCompiler constraintCompiler = new ConstraintCompiler(context);
    if (isAStatement(expressionString)) {
        String expressionStringWithBraces = String.format("{%s}", expressionString);
        return mvelCompiler.compileStatement(expressionStringWithBraces);
    } else {
        return constraintCompiler.compileExpression(expressionString);
    }
}
Also used : MvelCompiler(org.drools.mvelcompiler.MvelCompiler) MvelCompilerContext(org.drools.mvelcompiler.context.MvelCompilerContext) TypeResolver(org.drools.core.addon.TypeResolver) ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) ConstraintCompiler(org.drools.mvelcompiler.ConstraintCompiler) ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) Map(java.util.Map) 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