Search in sources :

Example 1 with ClassTypeResolver

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

the class CompilerTest method test.

default void test(Consumer<MvelCompilerContext> testFunction, String inputExpression, String expectedResult, Consumer<CompiledBlockResult> resultAssert) {
    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");
    imports.add(Person.class.getCanonicalName());
    imports.add(Gender.class.getCanonicalName());
    TypeResolver typeResolver = new ClassTypeResolver(imports, this.getClass().getClassLoader());
    MvelCompilerContext mvelCompilerContext = new MvelCompilerContext(typeResolver);
    testFunction.accept(mvelCompilerContext);
    CompiledBlockResult compiled = new MvelCompiler(mvelCompilerContext).compileStatement(inputExpression);
    verifyBodyWithBetterDiff(expectedResult, compiled.resultAsString());
    resultAssert.accept(compiled);
}
Also used : MvelCompilerContext(org.drools.mvelcompiler.context.MvelCompilerContext) TypeResolver(org.drools.core.addon.TypeResolver) ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) Gender(org.drools.Gender) Person(org.drools.Person) ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) HashSet(java.util.HashSet)

Example 2 with ClassTypeResolver

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

the class ConstraintCompilerTest method testExpression.

public void testExpression(Consumer<MvelCompilerContext> testFunction, String inputExpression, String expectedResult, Consumer<CompiledExpressionResult> resultAssert) {
    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");
    imports.add(Person.class.getCanonicalName());
    imports.add(Gender.class.getCanonicalName());
    TypeResolver typeResolver = new ClassTypeResolver(imports, this.getClass().getClassLoader());
    MvelCompilerContext mvelCompilerContext = new MvelCompilerContext(typeResolver);
    testFunction.accept(mvelCompilerContext);
    CompiledExpressionResult compiled = new ConstraintCompiler(mvelCompilerContext).compileExpression(inputExpression);
    verifyBodyWithBetterDiff(expectedResult, compiled.resultAsString());
    resultAssert.accept(compiled);
}
Also used : MvelCompilerContext(org.drools.mvelcompiler.context.MvelCompilerContext) ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) TypeResolver(org.drools.core.addon.TypeResolver) Gender(org.drools.Gender) Person(org.drools.Person) ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) HashSet(java.util.HashSet)

Example 3 with ClassTypeResolver

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

the class KnowledgePackageImpl method setClassLoader.

public void setClassLoader(ClassLoader classLoader) {
    if (typeResolver != null && typeResolver.getClassLoader() == classLoader) {
        return;
    }
    this.typeResolver = new ClassTypeResolver(new HashSet<>(getImports().keySet()), classLoader, getName());
    typeResolver.addImport(getName() + ".*");
    for (String implicitImport : getImplicitImports()) {
        typeResolver.addImplicitImport(implicitImport);
    }
    this.ruleUnitDescriptionLoader = new RuleUnitDescriptionLoader(this);
}
Also used : RuleUnitDescriptionLoader(org.drools.core.ruleunit.RuleUnitDescriptionLoader) ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) HashSet(java.util.HashSet)

Example 4 with ClassTypeResolver

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

the class ClassTypeResolverTest method testResolveArrayOfObjectsFromImport.

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

Example 5 with ClassTypeResolver

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

the class ClassTypeResolverTest method testResolveArrayOfPrimitiveTypes.

@Test
public void testResolveArrayOfPrimitiveTypes() 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)

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