Search code examples
drools

drools rule to relate two facts without eval


So I'm still relatively new to drools. I know how to use eval to compare facts, but I'm under the impression I should be able to write the rules without the eval statement. I was hoping to get some help understanding how I would do so in the following situation?

I have a fact that a supervisor is being requested for a given user's email address:

declare SupervisorRequested
    email : String
end

and a map from users to their supervisor (potentially -- some users have no supervisors)

// Map<String, User>
knowledgeResources.add(supervisors);

And so the rule I have written is

rule "Supervisor Inclusion Requested"
    when
        request : SupervisorRequested()
        supervisors : Map()
        eval(supervisors.get(request.email) != null)
    then
        ...
end

So, the question is, how could I write this without resorting to using eval?


Solution

  • The below rule will fire for all instances where the map (assumed to be in working memory) contains a user mapped to the email and will not fire if $supervisors.get($email) returns null. One of the biggest conveniences of working with Drools in MVEL is that we should rarely have to do null checks.

    rule "Supervisor Inclusion Requested"
        when
            $request : SupervisorRequested($email: email)
            $supervisors: Map()
            $supervisorWithEmail : User() from $supervisors.get($email)
        then
            ...
    end
    

    Hope that helps, cheers.