Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
chore(#1704): Refactor logic into specific functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Laing committed Jan 18, 2021
1 parent 0fef0a4 commit e94a048
Showing 1 changed file with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,15 @@ private WeightedElement<Merged<ConstraintNode>> combineWithRootNode(ConstraintNo
.map(rootAtomicConstraint -> (InSetConstraint)rootAtomicConstraint)
.findFirst()
.map(matchingRootAtomicConstraint -> {
double totalWeighting = matchingRootAtomicConstraint.legalValues.stream()
.mapToDouble(InSetRecord::getWeightValueOrDefault).sum();

double relevantWeighting = matchingRootAtomicConstraint.legalValues.stream()
.filter(legalValue -> optionAtomicConstraint.toFieldSpec().canCombineWithLegalValue(legalValue.getElement()))
.mapToDouble(InSetRecord::getWeightValueOrDefault).sum();
double totalWeighting = getWeightOfAllLegalValues(matchingRootAtomicConstraint);
double relevantWeighting = getWeightOfAllPermittedLegalValues(matchingRootAtomicConstraint, optionAtomicConstraint);

return relevantWeighting / totalWeighting;
})
.orElse(1d))
.sum();

if (applicabilityOfThisOption > 1){
if (applicabilityOfThisOption > 1) {
double applicabilityFraction = applicabilityOfThisOption - (int) applicabilityOfThisOption;
applicabilityOfThisOption = applicabilityFraction == 0
? 1
Expand All @@ -139,6 +135,17 @@ private WeightedElement<Merged<ConstraintNode>> combineWithRootNode(ConstraintNo
);
}

private static double getWeightOfAllLegalValues(InSetConstraint matchingRootAtomicConstraint){
return matchingRootAtomicConstraint.legalValues.stream()
.mapToDouble(InSetRecord::getWeightValueOrDefault).sum();
}

private static double getWeightOfAllPermittedLegalValues(InSetConstraint matchingRootAtomicConstraint, AtomicConstraint optionAtomicConstraint){
return matchingRootAtomicConstraint.legalValues.stream()
.filter(legalValue -> optionAtomicConstraint.toFieldSpec().canCombineWithLegalValue(legalValue.getElement()))
.mapToDouble(InSetRecord::getWeightValueOrDefault).sum();
}

private Map<Field, FieldSpec> getFields(ConstraintNode option) {
return option.getAtomicConstraints().stream()
.map(AtomicConstraint::getField)
Expand Down

0 comments on commit e94a048

Please sign in to comment.