lives
lives

Reputation: 1195

drools not exists in collection

I need to fire a rule if a collection does not have a specific object.

AuditAssignment is available as problem fact.

AuditAssignment has a property "requiredSkill"

Audit Assignment has a property "auditor"

Auditor object has a list of "qualifications" which is a collection of "requiredSkill "

Now , I need to check , if the qualifications of the auditor in the auditassignment object has the requiredSkill

Below is sample rule which I tried but does not work .

rule "checkIfAuditSkillIsMatching"
    when
        $auditAssignment : AuditAssignment( $neededSkill : requiredSkill.getSkillCode())

        $auditor : Auditor( $auditorSkills : qualifications)
        not exists ( Skill ( skillCode == $neededSkill ) from $auditorSkills  )  

    then
        System.out.println( " **** " + $neededSkill);
        scoreHolder.addHardConstraintMatch(kcontext, -1 );
end

I have tried the below one as well

rule "checkIfAuditSkillIsMatching"
    when

        $validAuditorCount : Number ( intValue < 1  ) from accumulate (
            $auditor : Auditor( $auditorSkills: qualifications )
            and   exists AuditAssignment( auditor == $auditor , 
                    $auditorSkills.contains(requiredSkill) )   ,
            count($auditor)

        ) 

    then
        scoreHolder.addHardConstraintMatch(kcontext, -1 );
end

Upvotes: 2

Views: 1073

Answers (2)

lives
lives

Reputation: 1195

The below configuration worked

rule "checkIfAuditSkillIsMatching"
    when
        $auditAssignment : AuditAssignment( $neededSkill : requiredSkill ,
                    $assignedAuditor : auditor )

        $auditor : Auditor( this == $assignedAuditor , !qualifications.contains($neededSkill) ) 

    then
        scoreHolder.addHardConstraintMatch(kcontext, -1 );
end

Upvotes: 1

laune
laune

Reputation: 31300

Here it is advisable to use a property method of Collection to obtain the logical value you need.

rule "checkIfAuditSkillIsMatching"
when
    $auditAssignment: AuditAssignment( $neededSkill: requiredSkill.getSkillCode() )
    $auditor: Auditor( $auditorSkills: qualifications,
                       ! $auditorSkills.contains( $neededSkill ) )  
then
    //...no suitably qualified auditor
end

Upvotes: 3

Related Questions