Search code examples
droolsoptaplanner

Drools: Check if list elements are a continuous range of integers


New to Optaplanner.

I have a many to many relationship between the planning entity and the planning variable, hence created an intermediate class and that is the planning entity.

Now, I want to write a rule where I am assigning 'n' number of objects to a variable and I need to check if all the 'n' objects being assigned have IDs that are continuous.

public class ObjectA {
    private int ID;
}

@PlanningEntity
public class ResourceAssignment {

    private ObjectA myObjectA;
    private ObjectB myObjectB;
}

My rule to get all the assigned objects:

rule "continuous objects assigned to a variable"
when
        $objectb: ObjectB()
        $ObjectaAssignedToObjectb : Object() from accumulate(ResourceAssignment(myObjectB == $objectb, $Assignments: myObjectA.getID),
            collectList($Assignments)
           )        
    then
        //scoreHolder.addHardConstraintMatch(kcontext, -1);
end

The above rule is giving me the list of all ObjectA's assigned to ObjectB. I just need to make sure all the IDs to the assigned ObjectA are a continuous range of integers. The start and end can be anything, they just need to be continuous.


Solution

  • How about something like:

    when
      Assignment($i : index)
      not Assignment(index == $i + 1)
      Assignment(index > $i + 1)
    then
      ...