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