Search code examples
javadroolsoptaplanner

Different generic types in Drools score calculation


I am developing a program to create altar server schedules using Optaplanner. The domain is derived in large parts from the nurse rostering example.

There are many problem facts which are requests by a server, e.g.:

class DateOffRequest {
    private Server server;
    private LocalDate date;
}

and

class DayOffRequest {
    private Server server;
    private DayOfWeek day;
}

To simplify the code, I would like to replace these with AbstractMap.SimpleImmutableEntry.

The problem is that one cannot specify the type of a generic class in the Drools rule file like so:

rule "dateOffRequest"
    when
        Map.Entry<Server,LocalDate>($server : server, $date : date)
...

It runs without specifying the types, but then Drools will be using all kinds of requests for all kinds of rules as he cannot differentiate between them, which does not seem to be very efficient to me.

I thought of just wrapping Map.Entries in seperate classes and wiring through the functionality, but this would produce much boilerplate code and eliminate the advantage of using generics.

It would be awesome, if one could specify the problem fact collections to be used by a rule. I thought of it similar to the way ValueRangeProviders specify ids, but I did not find any way to achieve such behaviour.

Is there any best practice to cope with this kind of situation?


Solution

  • This is a limitation due to Java's reified generics. This means that if a Map instance is in the problem facts, there is no way to detect the generic types used to construct that Map instance. Generic types are removed from instances in java at runtime. (Note that the generic types are not removed from method signatures at runtime, but that's another case).

    Workaround: Create a strongly typed combination class, such as ServerLocalDatePair.