Search in sources :

Example 21 with ClassTypeResolver

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

the class ClassTypeResolverTest method testResolvePrimtiveTypes.

@Test
public void testResolvePrimtiveTypes() throws Exception {
    final ClassTypeResolver resolver = new ClassTypeResolver(new HashSet(), Thread.currentThread().getContextClassLoader());
    assertEquals(boolean.class, resolver.resolveType("boolean"));
    assertEquals(double.class, resolver.resolveType("double"));
    assertEquals(float.class, resolver.resolveType("float"));
    assertEquals(int.class, resolver.resolveType("int"));
    assertEquals(char.class, resolver.resolveType("char"));
    assertEquals(long.class, resolver.resolveType("long"));
    assertEquals(byte.class, resolver.resolveType("byte"));
    assertEquals(short.class, resolver.resolveType("short"));
}
Also used : ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 22 with ClassTypeResolver

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

the class ClassTypeResolverTest method testResolveMultidimensionnalArrayOfPrimitiveTypes.

@Test
public void testResolveMultidimensionnalArrayOfPrimitiveTypes() throws Exception {
    final ClassTypeResolver resolver = new ClassTypeResolver(new HashSet(), Thread.currentThread().getContextClassLoader());
    assertEquals(int[][].class, resolver.resolveType("int[][]"));
    assertEquals(int[][][].class, resolver.resolveType("int[][][]"));
    assertEquals(int[][][][].class, resolver.resolveType("int[][][][]"));
}
Also used : ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 23 with ClassTypeResolver

use of org.drools.core.addon.ClassTypeResolver 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)

Example 24 with ClassTypeResolver

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

the class PackageHeaderLoader method findImportsFromJars.

private void findImportsFromJars(Collection<String> imports, List<JarInputStream> jarInputStreams) throws IOException {
    ClassTypeResolver resolver = new ClassTypeResolver(new HashSet<String>(), new VerifierMapBackedClassLoader(jarInputStreams));
    for (String className : imports) {
        classNames.add(className);
        try {
            Class clazz = resolver.resolveType(className);
            addFields(clazz);
        } catch (ClassNotFoundException e) {
            missingClasses.add(className);
        }
    }
}
Also used : ClassTypeResolver(org.drools.core.addon.ClassTypeResolver)

Aggregations

ClassTypeResolver (org.drools.core.addon.ClassTypeResolver)24 HashSet (java.util.HashSet)22 Test (org.junit.Test)16 TypeResolver (org.drools.core.addon.TypeResolver)6 MvelCompilerContext (org.drools.mvelcompiler.context.MvelCompilerContext)3 Gender (org.drools.Gender)2 Person (org.drools.Person)2 Cheese (org.drools.core.test.model.Cheese)2 FirstClass (org.drools.core.test.model.FirstClass)2 PackageModel (org.drools.modelcompiler.builder.PackageModel)2 Before (org.junit.Before)2 Map (java.util.Map)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 RuleUnitDescriptionLoader (org.drools.core.ruleunit.RuleUnitDescriptionLoader)1 SecondClass (org.drools.core.test.model.SecondClass)1 RuleContext (org.drools.modelcompiler.builder.generator.RuleContext)1 ConstraintCompiler (org.drools.mvelcompiler.ConstraintCompiler)1 MvelCompiler (org.drools.mvelcompiler.MvelCompiler)1