I have been working on converting my Drools DRL code into ConstraintProvider code for OptaPlanner. While I managed to convert some of the rules, I am facing difficulties with a few specific ones. Below are the rules that I am struggling to convert. I hope someone can help me figure out how to persist these rules in OptaPlanner Constraint Streams.
Here are the rules:
rule “eachTeamCanPlayNoOfTimesPerTime”
when
Game($leftId : id, gamePeriod != null, $matchDate : matchDate, $startTime : startTime, $teamPlayGamesPerTime : teamPlayGamesPerTime, $homeTeam : homeTeam, $awayTeam : awayTeam)
$teamCanPlayPerTime : Number(intValue > $teamPlayGamesPerTime) from accumulate(
Game(id < $leftId, gamePeriod != null, matchDate == $matchDate, startTime == $startTime, $teamTimeCount : gameStaticCount,
(homeTeam == $homeTeam || awayTeam == $homeTeam || homeTeam == $awayTeam || awayTeam == $awayTeam)),
sum($teamTimeCount)
)
then
scoreHolder.addHardConstraintMatch(kcontext, -$teamCanPlayPerTime.intValue());
end
rule “maxmimumNoOfMatchesPerDay”
when
$day : Day(isPoolOrLeague == 0, $matchDate : matchDate, $maxNoofMatchesDay : maxNoofMatchesDay)
$totalNoOfMatchesPerDay : Number(intValue > $maxNoofMatchesDay) from accumulate(
Game(gamePeriod != null, matchDate == $matchDate, $gameStaticCount : gameStaticCount),
sum($gameStaticCount)
)
exists Game(gamePeriod != null, day == $day)
then
scoreHolder.addHardConstraintMatch(kcontext, -1);
end
rule “eachTeamCanPlayNoOfTimesPerDay”
when
Game($leftId : id, gamePeriod != null, $matchDate : matchDate, $teamPlayGamesPerDay : teamPlayGamesPerDay, $homeTeam : homeTeam, $awayTeam : awayTeam)
$teamCanPlayPerDay : Number(intValue > $teamPlayGamesPerDay) from accumulate(
Game(id < $leftId, gamePeriod != null, matchDate == $matchDate, $teamDayCount : gameStaticCount,
(homeTeam == $homeTeam || awayTeam == $homeTeam || homeTeam == $awayTeam || awayTeam == $awayTeam)),
sum($teamDayCount)
)
then
scoreHolder.addHardConstraintMatch(kcontext, -$teamCanPlayPerDay.intValue());
end
rule “eachTeamCanPlayNoOfTimesPerWeekForOne”
when
Game($leftId : id, gamePeriod != null, $matchDate : matchDate, isPoolOrLeague == 0, teamPlayGamesPerWeek == 1, $teamPlayGamesPerWeek : teamPlayGamesPerWeek, $homeTeam : homeTeam, $awayTeam : awayTeam)
$weekDays : WeeksList($weekStartDate : weekStartDate, $weekEndDate : weekEndDate, $matchDate >= weekStartDate && $matchDate <= weekEndDate)
$teamCanPlayPerWeek : Number(intValue > $teamPlayGamesPerWeek) from accumulate(
Game(gamePeriod != null, (matchDate >= $weekStartDate && matchDate <= $weekEndDate), $teamWeekCount : gameStaticCount,
(homeTeam == $homeTeam || awayTeam == $homeTeam || homeTeam == $awayTeam || awayTeam == $awayTeam)),
sum($teamWeekCount)
)
then
scoreHolder.addHardConstraintMatch(kcontext, -$teamCanPlayPerWeek.intValue());
end
rule “eachTeamCanPlayNoOfTimesPerWeek”
when
Game($leftId : id, gamePeriod != null, $matchDate : matchDate, isPoolOrLeague == 0, teamPlayGamesPerWeek > 1, $teamPlayGamesPerWeek : teamPlayGamesPerWeek, $homeTeam : homeTeam, $awayTeam : awayTeam)
$weekDays : WeeksList($weekStartDate : weekStartDate, $weekEndDate : weekEndDate, $matchDate >= weekStartDate && $matchDate <= weekEndDate)
$teamCanPlayPerWeek : Number(intValue > $teamPlayGamesPerWeek) from accumulate(
Game(id < $leftId, gamePeriod != null, (matchDate >= $weekStartDate && $matchDate <= $weekEndDate), $teamWeekCount : gameStaticCount,
(homeTeam == $homeTeam || awayTeam == $homeTeam || homeTeam == $awayTeam || awayTeam == $awayTeam)),
sum($teamWeekCount)
)
then
scoreHolder.addHardConstraintMatch(kcontext, -$teamCanPlayPerWeek.intValue());
end
I've been able to convert some rules, but these particular ones are giving me trouble. Any guidance or examples on how to convert these specific rules to ConstraintProvider would be greatly appreciated. Thank you!
I attempted to convert the above Drools rules into OptaPlanner ConstraintProvider code. Here is what I did:
- Analyzed the Drools rules to understand the conditions and actions.
- Tried to map these conditions to Constraint Streams by creating appropriate constraint functions.
- Expected the rules to work similarly in the Constraint Streams format, enforcing the same constraints.
- For some rules, I was successful and the constraints behaved as expected.
- However, for the above rules, I encountered issues and was unable to convert them correctly. The constraints did not enforce the rules as they did in the DRL format.
I would appreciate any guidance or examples on how to correctly convert these specific rules to OptaPlanner Constraint Streams. Thank you!
Sairam Reddy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.