IndexOutOfBoundsException when declaring a type in the DRL file

385 views
Skip to first unread message

Myrosia Dzikovska

unread,
Mar 11, 2015, 2:50:08 PM3/11/15
to drools...@googlegroups.com
I have a type declared in a Java file, and then a type in the DRL file that extends a Java class. What I wanted to do is to have some shared functionality (provided by the Java class), plus different fact types I could match against, and could easily declare in the DRL file. I am getting an unexpected exception and not very informative exception. I don't get this exception if I declare these types in the Java file, so I am guessing that I am doing something wrong/too complex with the declaration. Is there anything that I am missing here?

Java fragment:


    public static class TextOutput extends GenerationOutput {
       
private String value;
       
private final List<IceComTerm> entities = new ArrayList<>();
       
       
public TextOutput(String value) {
           
this.value = value;
       
}
       
       
public TextOutput(String value, List<IceComTerm> entities) {
           
this.value = value;
           
if (entities != null) {
               
this.entities.addAll(entities);
           
}
       
}
       
       
@Override
       
public IceComList getBugRepresentation() {
           
// ... some code I want all classes to be able to run
       
}
   
}



Then in the DRL file I have

// Value is declared on text output, but here we add the @key annotation to ensure a constructor is generated
declare
FeedbackOutput extends TextOutput
    value
: String @key
end

// .... Some rules ....

rule
"Input seen"
dialect
"mvel"
agenda
-group "process input"
salience
-1
   
when
        $input
: DMStudentInput()
   
then
        insert
(new FeedbackOutput("Oops: got unexpected input " + $input.toString() + ", type: " + $input.getType()))
        retract
($input)
end


The goal of this is to have multiple class types I can match against, but shared functionality that they all can use.

If I declare it this way, I get an IndexOutOfBound exception when that rule fires.

java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java
.lang.String.substring(String.java:1871)
    at org
.mvel2.util.ErrorUtil.rewriteIfNeeded(ErrorUtil.java:17)
    at org
.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getMethod(ReflectiveAccessorOptimizer.java:952)
    at org
.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:373)
    at org
.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.optimizeAccessor(ReflectiveAccessorOptimizer.java:140)
    at org
.mvel2.ast.ASTNode.optimize(ASTNode.java:159)
    at org
.mvel2.ast.ASTNode.getReducedValueAccelerated(ASTNode.java:115)
    at org
.mvel2.MVELRuntime.execute(MVELRuntime.java:86)
    at org
.mvel2.compiler.CompiledExpression.getDirectValue(CompiledExpression.java:123)
    at org
.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:119)
    at org
.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:113)
    at org
.mvel2.MVEL.executeExpression(MVEL.java:930)
    at org
.drools.core.base.mvel.MVELConsequence.evaluate(MVELConsequence.java:107)
    at org
.drools.core.common.DefaultAgenda.fireActivation(DefaultAgenda.java:1046)
    at org
.drools.core.phreak.RuleExecutor.fire(RuleExecutor.java:152)
    at org
.drools.core.phreak.RuleExecutor.evaluateNetworkAndFire(RuleExecutor.java:94)
    at org
.drools.core.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:964)
    at org
.drools.core.common.DefaultAgenda.fireAllRules(DefaultAgenda.java:1234)
    at org
.drools.core.impl.StatefulKnowledgeSessionImpl.fireAllRules(StatefulKnowledgeSessionImpl.java:1239)
    at org
.drools.core.impl.StatefulKnowledgeSessionImpl.fireAllRules(StatefulKnowledgeSessionImpl.java:1212)
    at droolsdm
.StandaloneDM.userInputLoop(StandaloneDM.java:113)
    at droolsdm
.StandaloneDM.main(StandaloneDM.java:134)



If I declare FeedbackOutput in a java file and import it, everything works and I don't get any exceptions. I thought you could re-declare types in a DRL file in order to add annotations. Am I using the type declaration not in the way it was intended?

Thanks

Myrosia

Davide Sottara

unread,
Mar 11, 2015, 3:01:27 PM3/11/15
to drools...@googlegroups.com
I agree that the exception is not very informative :)  but to extend a java class with a declare type please make sure you
- import the class (e.g. import com.foo.Text)
- declare it
  declare Text end // needed for compatiblity
- extend it
  declare SubText extends Text .... end

If you miss any of the 3 steps, you should get a compilation error. A KieBase with compilation errors may result in
unpredictable behavior at runtime.

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/drools-usage/00629e0a-d1d1-40cb-8760-888e57752ea8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Myrosia Dzikovska

unread,
Mar 11, 2015, 3:53:10 PM3/11/15
to drools...@googlegroups.com
Thanks. This produces different exceptions, though.

If I do the code fragment below, then I get the same index out of bounds exception

import droolsdm.GenerationOutput.TextOutput;

declare
TextOutput
end


// Value is declared on text output, but here we add the @key annotation to ensure a constructor is generated

declare
QuestionOutput extends TextOutput
    value
: String @key
end

....

java
.lang.StringIndexOutOfBoundsException: String index out of range: -1

    at java
.lang.String.substring(String.java:1871)
    at org
.mvel2.util.ErrorUtil.rewriteIfNeeded(ErrorUtil.java:17)

But if I try to mention value on text output, I get  a compilation exception


import droolsdm.GenerationOutput.TextOutput;

declare
TextOutput
    value
: String
end


// Value is declared on text output, but here we add the @key annotation to ensure a constructor is generated

declare
QuestionOutput extends TextOutput
    value
: String @key
end

....
New declaration of droolsdm.GenerationOutput$TextOutput can't declaredeclares a different set of fields
existing : {value=private java.lang.String droolsdm.GenerationOutput$TextOutput.value, hashCode=null, class=null, toString=null}
declared : {value=TypeField[ value : [Pattern: id=null; objectType=String] = null ]}

Davide Sottara

unread,
Mar 14, 2015, 5:55:18 PM3/14/15
to drools...@googlegroups.com
I see what happens here.. it's indeed a bug, I'll get back to you with more details
Davide

dda...@obopay.com

unread,
Aug 1, 2018, 6:20:12 AM8/1/18
to Drools Usage
I am facing the same issue, any further update ?

Derick Daniel

unread,
Aug 1, 2018, 7:31:22 AM8/1/18
to Drools Usage

Issue is fixed, it was a null pointer exception getting suppressed by drools framework. Thanks.

Reply all
Reply to author
Forward
0 new messages