@Override public HardMediumSoftLongScoreHolder buildScoreHolder(boolean constraintMatchEnabled) { return new HardMediumSoftLongScoreHolder(constraintMatchEnabled); }
/** * Penalize a match by the {@link ConstraintWeight} negated. * @param kcontext never null, the magic variable in DRL */ public void penalize(RuleContext kcontext) { reward(kcontext, -1L); }
public void addConstraintMatch(boolean constraintMatchEnabled) { HardMediumSoftLongScoreHolder scoreHolder = new HardMediumSoftLongScoreHolder(constraintMatchEnabled); scoreHolder.addHardConstraintMatch(hard1, -1L); assertEquals(HardMediumSoftLongScore.of(-1L, 0L, 0L), scoreHolder.extractScore(0)); scoreHolder.addHardConstraintMatch(hard2Undo, -8L); assertEquals(HardMediumSoftLongScore.of(-9L, 0L, 0L), scoreHolder.extractScore(0)); callOnDelete(hard2Undo); assertEquals(HardMediumSoftLongScore.of(-1L, 0L, 0L), scoreHolder.extractScore(0)); scoreHolder.addMediumConstraintMatch(medium1, -10L); callOnUpdate(medium1); scoreHolder.addMediumConstraintMatch(medium1, -20L); // Overwrite existing scoreHolder.addSoftConstraintMatch(soft1, -100L); callOnUpdate(soft1); scoreHolder.addSoftConstraintMatch(soft1, -300L); // Overwrite existing scoreHolder.addMultiConstraintMatch(multi1, -1000L, -10000L, -100000L); callOnUpdate(multi1); scoreHolder.addMultiConstraintMatch(multi1, -4000L, -50000L, -600000L); // Overwrite existing scoreHolder.addHardConstraintMatch(hard3, -1000000L); callOnUpdate(hard3); scoreHolder.addHardConstraintMatch(hard3, -7000000L); // Overwrite existing scoreHolder.addSoftConstraintMatch(soft2Undo, -99L); callOnDelete(soft2Undo);
@Override public void configureConstraintWeight(Rule rule, HardMediumSoftLongScore constraintWeight) { super.configureConstraintWeight(rule, constraintWeight); BiConsumer<RuleContext, Long> matchExecutor; if (constraintWeight.equals(HardMediumSoftLongScore.ZERO)) { matchExecutor = (RuleContext kcontext, Long matchWeight) -> {}; } else if (constraintWeight.getMediumScore() == 0 && constraintWeight.getSoftScore() == 0) { matchExecutor = (RuleContext kcontext, Long matchWeight) -> addHardConstraintMatch(kcontext, constraintWeight.getHardScore() * matchWeight); } else if (constraintWeight.getHardScore() == 0 && constraintWeight.getSoftScore() == 0) { matchExecutor = (RuleContext kcontext, Long matchWeight) -> addMediumConstraintMatch(kcontext, constraintWeight.getMediumScore() * matchWeight); } else if (constraintWeight.getHardScore() == 0 && constraintWeight.getMediumScore() == 0) { matchExecutor = (RuleContext kcontext, Long matchWeight) -> addSoftConstraintMatch(kcontext, constraintWeight.getSoftScore() * matchWeight); } else { matchExecutor = (RuleContext kcontext, Long matchWeight) -> addMultiConstraintMatch(kcontext, constraintWeight.getHardScore() * matchWeight, constraintWeight.getMediumScore() * matchWeight, constraintWeight.getSoftScore() * matchWeight); } matchExecutorByNumberMap.put(rule, matchExecutor); matchExecutorByScoreMap.put(rule, (RuleContext kcontext, HardMediumSoftLongScore weightMultiplier) -> addMultiConstraintMatch(kcontext, constraintWeight.getHardScore() * weightMultiplier.getHardScore(), constraintWeight.getMediumScore() * weightMultiplier.getMediumScore(), constraintWeight.getSoftScore() * weightMultiplier.getSoftScore())); }
public void rewardPenalize(boolean constraintMatchEnabled) { HardMediumSoftLongScoreHolder scoreHolder = new HardMediumSoftLongScoreHolder(constraintMatchEnabled); Rule hard1 = mockRule("hard1"); scoreHolder.configureConstraintWeight(hard1, HardMediumSoftLongScore.ofHard(10L)); Rule hard2 = mockRule("hard2"); scoreHolder.configureConstraintWeight(hard2, HardMediumSoftLongScore.ofHard(100L)); Rule medium1 = mockRule("medium1"); scoreHolder.configureConstraintWeight(medium1, HardMediumSoftLongScore.ofMedium(10L)); Rule soft1 = mockRule("soft1"); scoreHolder.configureConstraintWeight(soft1, HardMediumSoftLongScore.ofSoft(10L)); Rule soft2 = mockRule("soft2"); scoreHolder.configureConstraintWeight(soft2, HardMediumSoftLongScore.ofSoft(100L)); scoreHolder.penalize(mockRuleContext(hard1)); assertEquals(HardMediumSoftLongScore.of(-10L, 0L, 0L), scoreHolder.extractScore(0)); scoreHolder.penalize(mockRuleContext(hard2), 2L); assertEquals(HardMediumSoftLongScore.of(-210L, 0L, 0L), scoreHolder.extractScore(0)); scoreHolder.penalize(mockRuleContext(medium1), 9L); assertEquals(HardMediumSoftLongScore.of(-210L, -90L, 0L), scoreHolder.extractScore(0)); scoreHolder.reward(mockRuleContext(soft1)); assertEquals(HardMediumSoftLongScore.of(-210L, -90L, 10L), scoreHolder.extractScore(0)); scoreHolder.reward(mockRuleContext(soft2), 3L); assertEquals(HardMediumSoftLongScore.of(-210L, -90L, 310L), scoreHolder.extractScore(0)); }
/** * Penalize a match by the {@link ConstraintWeight} negated and multiplied with the weightMultiplier for all score levels. * @param kcontext never null, the magic variable in DRL * @param weightMultiplier at least 0 */ public void penalize(RuleContext kcontext, long weightMultiplier) { reward(kcontext, -weightMultiplier); }
/** * Penalize a match by the {@link ConstraintWeight} negated and multiplied with the specific weightMultiplier per score level. * Slower than {@link #penalize(RuleContext, long)}. * @param kcontext never null, the magic variable in DRL * @param hardWeightMultiplier at least 0 * @param mediumWeightMultiplier at least 0 * @param softWeightMultiplier at least 0 */ public void penalize(RuleContext kcontext, long hardWeightMultiplier, long mediumWeightMultiplier, long softWeightMultiplier) { reward(kcontext, -hardWeightMultiplier, -mediumWeightMultiplier, -softWeightMultiplier); }
/** * Reward a match by the {@link ConstraintWeight}. * @param kcontext never null, the magic variable in DRL */ public void reward(RuleContext kcontext) { reward(kcontext, 1L); }