[rules-users] PatternBuilder createRestriction bug on Drools 5.1.1
Wolfgang Laun
wolfgang.laun at gmail.com
Thu Dec 22 04:34:58 EST 2011
Using an undeclared static variable (Message.HELLO) wasn't handled
cleanly in 5.1.1. This has been fixed in 5.2.0.
-W
2011/12/22 Manuel Ortiz <manuel.ortizramos at gmail.com>:
> Dear Wolfgang:
>
> Just change the "Hello World" rule in the "Hello World" example by this
> one...
>
> rule "Hello World"
> when
> m : Message( status == Message.HELLO || != "WrongType", myMessage : message
> )
> then
> System.out.println( myMessage );
> m.setMessage( "Goodbye cruel world" );
> m.setStatus( Message.GOODBYE );
> update( m );
> end
>
> ...and you will get the following building error:
>
> Description Resource Path Location Type
> Error: java.lang.NullPointerException Sample.drl /DroolsTest2/src/main/rules
> Unknown Drools Error
>
> This NullPointerException breaks the normal drools compiler execution flow.
> In contrast, if you use the following "Hello World" rule...
>
> rule "Hello World"
> when
> m : Message( status != "WrongType", myMessage : message )
> then
> System.out.println( myMessage );
> m.setMessage( "Goodbye cruel world" );
> m.setStatus( Message.GOODBYE );
> update( m );
> end
>
> ...then you get the following building errors:
>
> Description Resource Path Location Type
> BuildError: Unable to create a Field value of type 'ValueType = 'int'' and
> value 'WrongType' Sample.drl /DroolsTest2/src/main/rules Unknown Drools
> Error
> BuildError: Unable to create restriction '[LiteralRestriction: !=
> WrongType]' for field 'status' in the rule 'Hello World' Sample.drl
> /DroolsTest2/src/main/rules line 7 Drools Error
>
> which don't break the drools compiler execution flow.
>
> I think this one should be the right output for the first example.
>
> Below you can find my fix to this problem, based on the assumption that
> createRestriction must return null if the restriction (neither composite nor
> simple) cannot be constructed.
>
> At PatternBuilder.java, substitute createRestriction by...
>
> private Restriction createRestriction(final RuleBuildContext context,
> final Pattern pattern,
> final FieldConstraintDescr
> fieldConstraintDescr,
> final RestrictionConnectiveDescr
> top,
> final InternalReadAccessor
> extractor) {
> Restriction[] restrictions = new
> Restriction[top.getRestrictions().size()];
> int index = 0;
>
> for ( Iterator it = top.getRestrictions().iterator(); it.hasNext();
> ) {
> RestrictionDescr restrictionDescr = (RestrictionDescr)
> it.next();
>
> if ( restrictionDescr instanceof RestrictionConnectiveDescr ) {
> restrictions[index] = this.createRestriction( context,
> pattern,
>
> fieldConstraintDescr,
>
> (RestrictionConnectiveDescr) restrictionDescr,
> extractor );
>
> } else {
> restrictions[index] = buildRestriction( context,
> pattern,
> extractor,
>
> fieldConstraintDescr,
> restrictionDescr );
> if ( restrictions[index] == null ) {
> context.getErrors().add( new DescrBuildError(
> context.getParentDescr(),
>
> fieldConstraintDescr,
> null,
> "Unable to
> create restriction '" + restrictionDescr.toString() + "' for field '" +
> fieldConstraintDescr.getFieldName() + "' in the rule '" +
> context.getRule().getName() + "'" ) );
> }
> }
>
> if(restrictions[index] == null)
> {
> index = 0;
> break;
> }
> else
> {
> index++;
> }
> }
>
> if(index == 0) {
> return null;
> } else if ( restrictions.length > 1 ) {
> AbstractCompositeRestriction composite = null;
> if ( top.getConnective() == RestrictionConnectiveDescr.AND ) {
> composite = new AndCompositeRestriction( restrictions );
> } else if ( top.getConnective() == RestrictionConnectiveDescr.OR
> ) {
> composite = new OrCompositeRestriction( restrictions );
> } else {
> context.getErrors().add( new DescrBuildError(
> context.getParentDescr(),
>
> fieldConstraintDescr,
> null,
> "This is a
> bug: Impossible to create a composite restriction for connective: " +
> top.getConnective() + "' for field '" + fieldConstraintDescr.getFieldName()
> + "' in the rule '"
> +
> context.getRule().getName() + "'" ) );
> }
>
> return composite;
> } else if ( restrictions.length == 1 ) {
> return restrictions[0];
> }
> context.getErrors().add( new DescrBuildError(
> context.getParentDescr(),
> fieldConstraintDescr,
> null,
> "This is a bug: trying
> to create a restriction for an empty restriction list for field '" +
> fieldConstraintDescr.getFieldName() + "' in the rule '" +
> context.getRule().getName() + "'" ) );
> return null;
> }
>
> Kind regards,
>
> Manuel Ortiz.
>
>
> 2011/12/22 Wolfgang Laun <wolfgang.laun at gmail.com>
>>
>> Now would you mind posting an example reproducing this error...
>> -W
>>
>> On 22/12/2011, Manuel Ortiz <manuel.ortizramos at gmail.com> wrote:
>> > Dear Sirs:
>> >
>> > I'm working on an application which writes a set of rules using
>> > information
>> > which is stored in BDD tables. This information can be 'wrong'
>> > concerning
>> > rule compilation, and I expect that Drools reports the corresponding
>> > compilation errors.
>> >
>> > I've found that PatternBuilder.createRestriction() fails to process
>> > composite restrictions when one of the single restrictions cannot be
>> > implemented. The problem is that although one of the sigle restrictions
>> > is
>> > returned null, the composite restriction array is succesfully returned,
>> > one
>> > of its elements being null, wich causes a NullPointerException when the
>> > composite restriction is processed in
>> > PatternBuilder.build(AbstractCompositeConstraint version) to obtain a
>> > MultiRestrictionFieldConstraint object.
>> >
>> > I would like to know if this bug has yet been reported and fixed in some
>> > Drools version beyond 5.1.1
>> >
>> > Thank you very much for your time.
>> >
>> > Kind regards,
>> >
>> > Manuel Ortiz
>> >
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
More information about the rules-users
mailing list