diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/walker/decisionbased/RowSpecTreeSolver.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/walker/decisionbased/RowSpecTreeSolver.java index 13d5f86df..933c5bf3b 100644 --- a/core/src/main/java/com/scottlogic/datahelix/generator/core/walker/decisionbased/RowSpecTreeSolver.java +++ b/core/src/main/java/com/scottlogic/datahelix/generator/core/walker/decisionbased/RowSpecTreeSolver.java @@ -107,19 +107,15 @@ private WeightedElement> 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 @@ -139,6 +135,17 @@ private WeightedElement> 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 getFields(ConstraintNode option) { return option.getAtomicConstraints().stream() .map(AtomicConstraint::getField)