[rules-users] Test for null object, not null property

Stuart Moffatt stuart.moffatt at utah.edu
Tue Aug 14 10:23:30 EDT 2007


Edson,

Thanks for the tip. The "not Building()" works just fine, so long as I
don't assert the null building anyway.

I guess I was going for pure abstraction of all the rules, including
null even though deep down I knew it didn't make sense to assert a
null. The workaround is to hard code the null case like this:

    private void validateGetBuilding( Building building ) throws BOException
    {
        // set up an exception
        boException = new BOException();

        // send the exception into the BRE
        FactHandle boFact = businessRuleSession.insert( boException );

        // send the building into the BRE
        FactHandle buildingFact = null;
        if( building != null )
        {
            buildingFact = businessRuleSession.insert( building );
        }


        // trigger the validate building agenda items
        businessRuleSession.setFocus("validate delete or get building");

        // fire
        businessRuleSession.fireAllRules();

        // retract items
        businessRuleSession.retract(boFact);
        if( buildingFact != null )
        {
            businessRuleSession.retract(buildingFact);
        }

        // check on the exception (is set in the rule)
        if( boException.getCode() != null )
        {

            log.debug("Exception for building=" + building);

            String message = BOMessages.getProperty( boException.getCode() );
            boException.setMessage(message);
            throw boException;
        }
    }

With the rule in question being reconfigured to this:

rule "building is null"
	agenda-group "validate delete or get building"
	salience 10
	when
		boException : BOException()
		not Building()
	then
		boException.setCode("BO-0008");
end

This ends up throwing the expected BOException:
08:16:26,093 DEBUG BuildingBO:288 = Exception for building=null
08:16:26,119 ERROR InCastAPI:73 = BO-0008: The building is null

Don't you just love the green bar in junit?

Thanks for you help -- really enjoying developing this app with Rules.

On 8/14/07, Edson Tirelli <tirelli at post.com> wrote:
>
>    I don't think it makes sense to assert null into working memory... null does not matches any object type anyway...
>    Maybe what you want to do is to check for the non-existance of a fact:
>
> when
>     not Building()
> then
>    ...
> end
>
>    So, don't ever assert null into working memory.
>
>    Edson
>
>
> 2007/8/14, Stuart Moffatt <stuart.moffatt at utah.edu >:
> >
> > The incoming building was initialized like this:
> >
> > Building building = null;
> > FactHandle buildingFact = businessRuleSession.insert( building );
> > businessRuleSession.setFocus("validate delete or get building");
> > businessRuleSession.fireAllRules();
> >
> > Can drools test for this null building?
> > What should the rule look like?
> >
> > rule "building is null"
> >     agenda-group "validate delete or get building"
> >     salience 10
> >     when
> >         boException : BOException()
> >         Building() == null  /* this is the problem here -- not sure what this should look like */
> >     then
> >         boException.setCode("BO-0008");
> > end
> >
> >
> > _______________________________________________
> > rules-users mailing list
> > rules-users at lists.jboss.org
> >  https://lists.jboss.org/mailman/listinfo/rules-users
> >
> >
>
>
>
> --
>   Edson Tirelli
>   Software Engineer - JBoss Rules Core Developer
>   Office: +55 11 3529-6000
>   Mobile: +55 11 9287-5646
>   JBoss, a division of Red Hat @ www.jboss.com
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>



-- 
Thanks,
Stuart

Stuart Moffatt
Disaster Resistant University
University of Utah
(801) 585-9122
stuart.moffatt at utah.edu



More information about the rules-users mailing list