/** * @param kcontext never null, the magic variable in DRL * @param softWeight higher is better, negative for a penalty, positive for a reward */ public void addSoftConstraintMatch(RuleContext kcontext, int softWeight) { softScore += softWeight; registerConstraintMatch(kcontext, () -> softScore -= softWeight, () -> HardSoftScore.of(0, softWeight)); }
@Override public int compare(Score s1, Score s2) { HardSoftScore score1 = (HardSoftScore) s1; HardSoftScore score2 = (HardSoftScore) s2; long score1Side = (long) score1.getHardScore() * (long) hardWeight + (long) score1.getSoftScore(); long score2Side = (long) score2.getHardScore() * (long) hardWeight + (long) score2.getSoftScore(); return score1Side < score2Side ? -1 : (score1Side == score2Side ? 0 : 1); }
/** * Penalize a match by the {@link ConstraintWeight} negated and multiplied with the specific weightMultiplier per score level. * Slower than {@link #penalize(RuleContext, int)}. * @param kcontext never null, the magic variable in DRL * @param hardWeightMultiplier at least 0 * @param softWeightMultiplier at least 0 */ public void penalize(RuleContext kcontext, int hardWeightMultiplier, int softWeightMultiplier) { reward(kcontext, -hardWeightMultiplier, -softWeightMultiplier); }
@Override public HardSoftScore add(HardSoftScore augment) { return new HardSoftScore( initScore + augment.getInitScore(), hardScore + augment.getHardScore(), softScore + augment.getSoftScore()); }
public static HardSoftScore parseScore(String scoreString) { String[] scoreTokens = parseScoreTokens(HardSoftScore.class, scoreString, HARD_LABEL, SOFT_LABEL); int initScore = parseInitScore(HardSoftScore.class, scoreString, scoreTokens[0]); int hardScore = parseLevelAsInt(HardSoftScore.class, scoreString, scoreTokens[1]); int softScore = parseLevelAsInt(HardSoftScore.class, scoreString, scoreTokens[2]); return ofUninitialized(initScore, hardScore, softScore); }
@Override public HardSoftScore calculateScore() { return HardSoftScore.of(hardScore, softScore); }
@Override public HardSoftScore extractScore(int initScore) { return HardSoftScore.ofUninitialized(initScore, hardScore, softScore); }
@Override public HardSoftScore parseScore(String scoreString) { return HardSoftScore.parseScore(scoreString); }
@Override public HardSoftScore withInitScore(int newInitScore) { assertNoInitScore(); return new HardSoftScore(newInitScore, hardScore, softScore); }
@Override public HardSoftScoreHolder buildScoreHolder(boolean constraintMatchEnabled) { return new HardSoftScoreHolder(constraintMatchEnabled); }
@Override public HardSoftScore negate() { return new HardSoftScore(-initScore, -hardScore, -softScore); }
@Override public String toString() { return getInitPrefix() + hardScore + HARD_LABEL + "/" + softScore + SOFT_LABEL; }
/** * @param kcontext never null, the magic variable in DRL * @param hardWeight higher is better, negative for a penalty, positive for a reward */ public void addHardConstraintMatch(RuleContext kcontext, int hardWeight) { hardScore += hardWeight; registerConstraintMatch(kcontext, () -> hardScore -= hardWeight, () -> HardSoftScore.of(hardWeight, 0)); }
@Override public HardSoftScore subtract(HardSoftScore subtrahend) { return new HardSoftScore( initScore - subtrahend.getInitScore(), hardScore - subtrahend.getHardScore(), softScore - subtrahend.getSoftScore()); }
@Override public HardSoftScore unmarshal(String scoreString) { return HardSoftScore.parseScore(scoreString); }
/** * Penalize a match by the {@link ConstraintWeight} negated. * @param kcontext never null, the magic variable in DRL */ public void penalize(RuleContext kcontext) { reward(kcontext, -1); }
@Override public HardSoftScore toInitializedScore() { return initScore == 0 ? this : new HardSoftScore(0, hardScore, softScore); }
/** * @param kcontext never null, the magic variable in DRL * @param hardWeight higher is better, negative for a penalty, positive for a reward * @param softWeight higher is better, negative for a penalty, positive for a reward */ public void addMultiConstraintMatch(RuleContext kcontext, int hardWeight, int softWeight) { hardScore += hardWeight; softScore += softWeight; registerConstraintMatch(kcontext, () -> { hardScore -= hardWeight; softScore -= softWeight; }, () -> HardSoftScore.of(hardWeight, softWeight)); }
/** * 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, int weightMultiplier) { reward(kcontext, -weightMultiplier); }
/** * Reward a match by the {@link ConstraintWeight}. * @param kcontext never null, the magic variable in DRL */ public void reward(RuleContext kcontext) { reward(kcontext, 1); }