[jboss-jira] [JBoss JIRA] (DROOLS-600) ClassCastException when using FactTemplates

Stephanie Kroll (JIRA) issues at jboss.org
Tue Sep 23 17:19:02 EDT 2014


    [ https://issues.jboss.org/browse/DROOLS-600?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13005523#comment-13005523 ] 

Stephanie Kroll commented on DROOLS-600:
----------------------------------------

We are using FactTemplates from the org.drools.core.FactTemplates package.  This is an undocumented feature but has been in the Drools product since at least version 4.0 when we started using it.  It is critical to our integration since it allows the use of fact objects that don't implement the standard java bean getters/setters.  In our case, our facts are backed by hashmaps.

The code example below will reproduce the problem.  We load the FactTemplates and rules programmatically.  Yes, it is true that we are referencing the Drools non-public API's and internal objects.  This is because the FactTemplate feature is not exposed through the public API's and has become buried deeper in the releases since 4.0.

{noformat}
public class FactTemplateTest {

    public static final void main(final String[] args) {
        
        String ruleText = "package com.testfacttemplate;" +
                " rule \"test rule\" " +
                " dialect \"mvel\" " +
                " when " +
                " $test : TestFactTemplate( status == 1 ) " +
                " then " +
                " System.out.println( \"Hello World\" ); " +
                " end ";
        
        KnowledgePackageImpl kPackage = new KnowledgePackageImpl("com.testfacttemplate");
        FieldTemplate fieldTemplate = new FieldTemplateImpl("status", 0, Integer.class);
        FactTemplate factTemplate = new FactTemplateImpl(kPackage, "TestFactTemplate", new FieldTemplate[]{fieldTemplate});
        
        KnowledgeBuilder kBuilder = new KnowledgeBuilderImpl(kPackage);
        StringReader rule = new StringReader(ruleText);
        try {
            ((KnowledgeBuilderImpl) kBuilder).addPackageFromDrl(rule);
        } catch (DroolsParserException | IOException e) {
            e.printStackTrace();
        }
        
        System.out.println("Test Completed Successfully");
    }
}
{noformat}

Regardless of using the code above to reproduce the issue, the problem can be observed by reviewing the code in the PatternBuilder.build method shown in the ClassCastException referenced in this issue's description.  The FactTemplate feature within Drools still works and our integration with Drools is working correctly with version 6.1.0.Final once I added the instanceof check shown below to the PatternBuilder.build method:

{noformat}
        boolean duplicateBindings = false;
        if (objectType instanceof ClassObjectType) {
            duplicateBindings = context.getDeclarationResolver().isDuplicated( context.getRule(), 
                    patternDescr.getIdentifier(), ((ClassObjectType) objectType).getClassName() );
        }
{noformat}

Thank you

> ClassCastException when using FactTemplates
> -------------------------------------------
>
>                 Key: DROOLS-600
>                 URL: https://issues.jboss.org/browse/DROOLS-600
>             Project: Drools
>          Issue Type: Bug
>    Affects Versions: 6.1.0.Final
>            Reporter: Stephanie Kroll
>            Assignee: Mark Proctor
>
> We have embedded Drools in our project and use FactTemplates.  While attempting to upgrade from Drools 5.3 to 6.1, we are receiving a ClassCastException in org.drools.compiler.rule.builder.PatternBuilder when loading and compiling rules.
> The problem is evident upon inspection of the code.  In method build(RuleBuildContext, BaseDescr, Pattern), the local var objectType can be either a FactTemplateObjectType or a ClassObjectType, but line 258 does not check the type before attempting the cast.
> {noformat}
> java.lang.ClassCastException: org.drools.core.facttemplates.FactTemplateObjectType cannot be cast to org.drools.core.base.ClassObjectType
> 	at org.drools.compiler.rule.builder.PatternBuilder.build(PatternBuilder.java:258)
> 	at org.drools.compiler.rule.builder.PatternBuilder.build(PatternBuilder.java:138)
> 	at org.drools.compiler.rule.builder.GroupElementBuilder.build(GroupElementBuilder.java:66)
> 	at org.drools.compiler.rule.builder.RuleBuilder.build(RuleBuilder.java:89)
> 	at org.drools.compiler.builder.impl.KnowledgeBuilderImpl.addRule(KnowledgeBuilderImpl.java:1652)
> 	at org.drools.compiler.builder.impl.KnowledgeBuilderImpl.compileRules(KnowledgeBuilderImpl.java:968)
> 	at org.drools.compiler.builder.impl.KnowledgeBuilderImpl.compileAllRules(KnowledgeBuilderImpl.java:844)
> 	at org.drools.compiler.builder.impl.KnowledgeBuilderImpl.addPackage(KnowledgeBuilderImpl.java:838)
> 	at org.drools.compiler.builder.impl.KnowledgeBuilderImpl.addPackageFromDrl(KnowledgeBuilderImpl.java:339)
> 	at org.drools.compiler.builder.impl.KnowledgeBuilderImpl.addPackageFromDrl(KnowledgeBuilderImpl.java:315)
> 	...{noformat}



--
This message was sent by Atlassian JIRA
(v6.3.1#6329)


More information about the jboss-jira mailing list