Skip to content

Commit

Permalink
fix inheritance of type declaration in executable model
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed Jan 16, 2018
1 parent ae68417 commit 09b0df1
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 66 deletions.
Expand Up @@ -31,6 +31,7 @@
import java.util.Map;

import org.drools.core.phreak.Reactive;
import org.drools.core.util.ClassUtils;
import org.kie.api.definition.type.Annotation;
import org.kie.api.definition.type.FactField;
import org.kie.api.definition.type.FactType;
Expand Down Expand Up @@ -256,11 +257,15 @@ public Object get(Object bean,
if (fieldDefinition != null) {
return fieldDefinition.getFieldAccessor().getValue( bean );
}
try {
java.lang.reflect.Field f = definedClass.getDeclaredField( field );
java.lang.reflect.Field f = ClassUtils.getField( definedClass, field );
if (f != null) {
f.setAccessible( true );
return f.get( bean );
} catch (IllegalAccessException | NoSuchFieldException e) { }
try {
return f.get( bean );
} catch (IllegalAccessException e) {
throw new RuntimeException( e );
}
}
return null;
}

Expand All @@ -271,11 +276,15 @@ public void set(Object bean,
if (fieldDefinition != null) {
fieldDefinition.getFieldAccessor().setValue( bean, value );
} else {
try {
java.lang.reflect.Field f = definedClass.getDeclaredField( field );
java.lang.reflect.Field f = ClassUtils.getField( definedClass, field );
if (f != null) {
f.setAccessible( true );
f.set( bean, value );
} catch (IllegalAccessException | NoSuchFieldException e) { }
try {
f.set( bean, value );
} catch (IllegalAccessException e) {
throw new RuntimeException( e );
}
}
}
}

Expand Down
Expand Up @@ -449,6 +449,14 @@ public static List<String> getAccessibleProperties( Class<?> clazz ) {
return accessibleProperties;
}

public static Field getField(Class<?> clazz, String field) {
try {
return clazz.getDeclaredField( field );
} catch (NoSuchFieldException e) {
return clazz.getSuperclass() != null ? getField(clazz.getSuperclass(), field) : null;
}
}

public static Method getAccessor(Class<?> clazz, String field) {
try {
return clazz.getMethod("get" + ucFirst(field));
Expand Down
Expand Up @@ -23,7 +23,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Stream;

Expand Down Expand Up @@ -128,13 +127,10 @@ public class KiePackagesBuilder {

private final Map<String, KiePackage> packages = new HashMap<>();

private final Set<Class<?>> patternClasses = new HashSet<>();

private final Map<Class<?>, ClassObjectType> objectTypeCache = new HashMap<>();

private final Collection<Model> models;
private final ChainedProperties chainedProperties;
private ClassLoader typesClassLoader;

public KiePackagesBuilder(KieBaseConfiguration conf, ProjectClassLoader moduleClassLoader) {
this(conf, new ArrayList<>(), moduleClassLoader);
Expand All @@ -143,8 +139,7 @@ public KiePackagesBuilder(KieBaseConfiguration conf, ProjectClassLoader moduleCl
public KiePackagesBuilder(KieBaseConfiguration conf, Collection<Model> models, ProjectClassLoader moduleClassLoader) {
this.configuration = ((RuleBaseConfiguration) conf);
this.models = models;
typesClassLoader = moduleClassLoader.getTypesClassLoader();
this.chainedProperties = ChainedProperties.getChainedProperties(typesClassLoader);
this.chainedProperties = ChainedProperties.getChainedProperties( moduleClassLoader.getTypesClassLoader() );
}

public void addModel( Model model ) {
Expand Down Expand Up @@ -558,7 +553,6 @@ private org.kie.api.runtime.rule.AccumulateFunction createRuntimeAccumulateFunct

private Pattern addPatternForVariable( RuleContext ctx, GroupElement group, Variable patternVariable ) {
Class<?> patternClass = patternVariable.getType().asClass();
patternClasses.add( patternClass );
Pattern pattern = new Pattern( ctx.getNextPatternIndex(),
0, // offset will be set by ReteooBuilder
getObjectType( patternClass ),
Expand Down
Expand Up @@ -69,6 +69,7 @@ public static void generatePOJO(InternalKnowledgePackage pkg, PackageDescr packa
processType( packageModel, typeDescr, typeResolver.resolveType( typeDescr.getTypeName() ));
} catch (ClassNotFoundException e) {
packageModel.addGeneratedPOJO(POJOGenerator.toClassDeclaration(typeDescr));
packageModel.addTypeMetaDataExpressions( registerTypeMetaData( packageModel, pkg.getName(), typeDescr.getTypeName() ) );
}
}
}
Expand All @@ -85,9 +86,7 @@ public static void registerType(TypeResolver typeResolver, Map<String, Class<?>>
}

private static void processType(PackageModel packageModel, TypeDeclarationDescr typeDescr, Class<?> type) {
MethodCallExpr typeMetaDataCall = new MethodCallExpr(null, TYPE_META_DATA_CALL);
typeMetaDataCall.addArgument( new StringLiteralExpr(type.getPackage().getName()) );
typeMetaDataCall.addArgument( new StringLiteralExpr(type.getSimpleName()) );
MethodCallExpr typeMetaDataCall = registerTypeMetaData( packageModel, type.getPackage().getName(), type.getSimpleName() );

for (AnnotationDescr ann : typeDescr.getAnnotations()) {
typeMetaDataCall = new MethodCallExpr(typeMetaDataCall, "addAnnotation");
Expand All @@ -99,9 +98,17 @@ private static void processType(PackageModel packageModel, TypeDeclarationDescr
typeMetaDataCall.addArgument( annotationValueCall );
}
}

packageModel.addTypeMetaDataExpressions(typeMetaDataCall);
}

private static MethodCallExpr registerTypeMetaData( PackageModel packageModel, String pkg, String name ) {
MethodCallExpr typeMetaDataCall = new MethodCallExpr(null, TYPE_META_DATA_CALL);
typeMetaDataCall.addArgument( new StringLiteralExpr(pkg) );
typeMetaDataCall.addArgument( new StringLiteralExpr(name) );
return typeMetaDataCall;
}

/**
*
*/
Expand All @@ -111,6 +118,10 @@ public static ClassOrInterfaceDeclaration toClassDeclaration(TypeDeclarationDesc
ClassOrInterfaceDeclaration generatedClass = new ClassOrInterfaceDeclaration(classModifiers, false, generatedClassName);
generatedClass.addImplementedType( GeneratedFact.class.getName() );

if (typeDeclaration.getSuperTypeName() != null) {
generatedClass.addExtendedType( typeDeclaration.getSuperTypeName() );
}

List<AnnotationDescr> softAnnotations = new ArrayList<>();
for (AnnotationDescr ann : typeDeclaration.getAnnotations()) {
final String annFqn = Optional.ofNullable(ann.getFullyQualifiedName())
Expand Down
Expand Up @@ -378,52 +378,4 @@ public void testDeclareAndExpires() throws Exception {
ksession.fireAllRules();
assertEquals(0, ksession.getObjects().size());
}

@Test
public void testNot() throws Exception {
String str =
"package org.drools.testcoverage.regression;\n" +
"\n" +
"declare BaseEvent\n" +
" @role(event)\n" +
"end\n" +
"\n" +
"declare Event extends BaseEvent\n" +
" @role(event)\n" +
" property : String\n" +
"end\n" +
"\n" +
"declare NotEvent extends BaseEvent\n" +
" @role(event)\n" +
" property : String\n" +
"end\n" +
"\n" +
"rule \"not equal\" when\n" +
" not (\n" +
" ( and\n" +
" $e : BaseEvent( ) over window:length(3) from entry-point entryPoint\n" +
" NotEvent( this == $e, property == \"value\" ) from entry-point entryPoint\n" +
" )\n" +
" )\n" +
"then\n" +
"end\n" +
"\n" +
/*
"rule \"not equal 2\" when\n" +
" not (\n" +
" $e : NotEvent( ) over window:length(3) and\n" +
" NotEvent( this == $e, property == \"value\" )\n" +
" )\n" +
"then\n" +
"end\n" +
"\n" +
"rule \"different\" when\n" +
" NotEvent( property != \"value\" ) over window:length(3) from entry-point entryPoint\n" +
"then\n" +
"end\n" +
*/
"";

KieSession ksession = getKieSession( getCepKieModuleModel(), str );
}
}
Expand Up @@ -384,4 +384,36 @@ public void testFactType() throws Exception {
assertEquals( 1, results.size() );
assertEquals( "Mario", results.iterator().next() );
}

@Test
public void testFactTypeNotUsedInRule() throws Exception {
String str =
"package org.test;\n" +
"import " + Person.class.getCanonicalName() + ";" +
"declare Name\n" +
" value : String\n" +
"end\n" +
"declare ExtendedName extends Name\n" +
"end\n" +
"rule R when\n" +
" Name($v : value == \"Mario\")\n" +
"then\n" +
" insert($v);" +
"end";

KieSession ksession = getKieSession( str );

FactType nameType = ksession.getKieBase().getFactType("org.test", "ExtendedName");
Object name = nameType.newInstance();
nameType.set(name, "value", "Mario");

ksession.insert(name);
ksession.fireAllRules();

assertEquals( "Mario", nameType.get( name, "value" ) );

Collection<String> results = getObjectsIntoList(ksession, String.class);
assertEquals( 1, results.size() );
assertEquals( "Mario", results.iterator().next() );
}
}

0 comments on commit 09b0df1

Please sign in to comment.