drools not exists in collection - drools

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

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

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

Related

Make rule on drolls to check subdata

I have a package. Package includes a list of items. Item has a list of fields. I need to check if specific fields are set to correct value for all items in a package:
rule "Slapper"
dialect "mvel"
when
itm : item( ) from pkg.items
List( size() == itm.size() ) from collect (
field( fieldId == "111" , value == "1" ) from itm.fields
field( fieldId == "222" , value == "2" ) from itm.fields
) from itm
then
...
end
how I can get collection filtered by subfields?
Need to use accumulate function:
List( size == pkg.items.size() ) from accumulate (
$a: itemData( $field1: fields, $field2: fields ) from pkg.items
and
fieldData( fieldId == "111" , value == "1" ) from $field1
and
fieldData( fieldId == "222" , value == "2" ) from $field2;
collectList( $a )
)

Using OR in the WHEN conditions in DROOLS

I have a problem with a rule in Drools.
In a drl file i have a rule:
rule ""
when
$g: class1( $tm: list1 )
$p: class2() from $tm
$ph: class3(isEnd==false, $s: steps) from $p.list2
$st: Step(isEnd==false ) from $s
then
----
end
I want a OR with the 3th and 4th conditions.
How can i do it?
Try this - I haven't tested it:
rule "xxx"
when
$g: class1( $tm: list1 )
$p: class2() from $tm
( class3(isEnd==false ) from $p.list2
or
( $ph: class3(isEnd==true ) from $p.list2
and
Step(isEnd==false ) from $ph.getSteps() )
)
then
----
end

Crystal Reports Multiple if conditions in Record Selection

I want to create a report and filter it by two parameters as below;
Parameter values are;
Call_type = "All" , "Sale" , "Buy"
and
Call_status = "All" , "Sold" , "Pending" , "None" , "Closed"
I have used below formula;
(
if {?type} <> "All" then
{cars_call_log.type} = {?type}
else
true;
);
(
if {?status} <> "All" then
{cars_call_log.status} = {?status}
else
true;
);
But it will work only for first If condition. It doesn't work for both conditions.
I want to make it like, Filter by first parameter then (filtered records)--> filter by second parameter.
The proper, record-selection-formula syntax for your needs:
(
if {?type} <> "All" then
{cars_call_log.type} = {?type}
else
true
)
AND
(
if {?status} <> "All" then
{cars_call_log.status} = {?status}
else
true
)

Deleted-table used in instead of update trigger

I got the following code ( no chance to ask the person who wrote it ) and don't see the need of the inner join on deleted. Do I miss something?
I thought, the deleted-table is used in instead-of-delete triggers. In instead-of-inserted triggers it's empty everytime, isn't it in instead-of-update triggers?
CREATE TRIGGER "someTrigger" ON "dbo"."someView"
INSTEAD OF UPDATE
AS
BEGIN
INSERT INTO "someOtherTable"
(
"id"
, "startTime"
, "endTime"
, "duration"
, "resourceId"
, "resourceLocked"
, "timeLocked"
, "groupPrefix"
)
SELECT
"id" = i."id"
, "startTime" = i."startTime"
, "endTime" = i."endTime"
, "duration" = ISNULL( i."duration", 0 )
, "resourceId" = i."resourceId"
, "resourceLocked" = ISNULL( i."resourceLocked", 0 )
, "timeLocked" = ISNULL( i."timeLocked", 0 )
, "groupPrefix" = N'gp'
FROM inserted AS i
INNER JOIN deleted AS d
ON d."id" = i."id"
WHERE ( i."jobType" != 3 )
OR ( i."jobType" = 3 AND i."startTime" != d."startTime" );
END;
In an update trigger, both inserted and deleted are populated. inserted contains the new row values, deleted contains the old row values, i.e. the values from before the UPDATE statement executed.
Here, it seems to be being used in the WHERE clause:
WHERE ( i."jobType" != 3 )
OR ( i."jobType" = 3 AND i."startTime" != d."startTime" );
So, if jobType is 3, we only do the insert if the startTime has been changed.
It should be noted that correlating the rows between inserted and deleted is only possible if at least one key's column(s) are not subject to update. Here, id appears to be a key. Hopefully that id column isn't subject to update or else this trigger may misbehave when such updates occur.

Selecting a set of three distinct objects

Drools Planner used this to select two distinct objects, ensuring that a rule did not fire twice for a given pair of objects. If o1 and o2 made a match, it fired only once, not twice for (o1,o2) and (o2,01).
when
$stp1 : SubjectTeacherPeriod( $slno : slNo,
$id : id,
$period_day_order : period.dayOrder
)
$stp2 : SubjectTeacherPeriod( slNo > $slno,
id == $id,
period.dayOrder == $period_day_order
)
How do I select a set of three distinct objects? What is a good selection criteria for that?
Same approach should work:
$f1 : SomeFact( ... )
$f2 : SomeFact( id > $f1.id, ... )
$f3 : SomeFact( id > $f2.id, ... )