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

Commit

Permalink
fix(#1552): use user provided weights instead of uniform distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
Ro4052 committed Jul 15, 2020
1 parent da3646a commit 954273e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ public static <T> DistributedList<T> singleton(final T element) {
return DistributedList.uniform(Collections.singleton(element));
}

public static <T> DistributedList<T> weightedOrDefault(final Collection<T> underlyingSet) {
return new DistributedList<>(
underlyingSet.stream()
.map(element -> element instanceof WeightedElement
? (WeightedElement<T>) element
: WeightedElement.withDefaultWeight(element)
)
.collect(Collectors.toList()));
}

public static <T> DistributedList<T> uniform(final Collection<T> underlyingSet) {
return new DistributedList<>(
underlyingSet.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public InMapRelation createInMapRelation(InMapConstraintDTO dto, Fields fields)

private InSetConstraint createInSetConstraint(InSetConstraintDTO dto, Field field)
{
DistributedList<Object> values = DistributedList.uniform(dto.values.stream()
DistributedList<Object> values = DistributedList.weightedOrDefault(dto.values.stream()
.distinct()
.map(this::parseValue)
.collect(Collectors.toList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.scottlogic.datahelix.generator.common.profile.Field;
import com.scottlogic.datahelix.generator.common.profile.NumericGranularity;
import com.scottlogic.datahelix.generator.common.util.NumberUtils;
import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement;
import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.*;
import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.GranularToConstraintDTO;
import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.integer.LongerThanConstraintDTO;
Expand All @@ -38,6 +39,13 @@ public class NumericConstraintFactory extends AtomicConstraintFactory
@Override
Object parseValue(Object value)
{
if (value instanceof WeightedElement) {
WeightedElement weightedElement = (WeightedElement) value;
return new WeightedElement<>(
NumberUtils.coerceToBigDecimal(weightedElement.element()),
weightedElement.weight()
);
}
return NumberUtils.coerceToBigDecimal(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -84,7 +85,7 @@ private InMapConstraintDTO map(InMapFromFileConstraintDTO dto)

private InSetConstraintDTO map(InSetFromFileConstraintDTO dto)
{
List<Object> values = fileReader.setFromFile(getFile(dto.file)).stream().collect(Collectors.toList());
List<Object> values = new ArrayList<>(fileReader.setFromFile(getFile(dto.file)).distributedList());
InSetConstraintDTO inSetConstraintDTO = new InSetConstraintDTO();
inSetConstraintDTO.field = dto.field;
inSetConstraintDTO.values = values;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.scottlogic.datahelix.generator.common.profile.FieldType;
import com.scottlogic.datahelix.generator.common.validators.ValidationResult;
import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement;
import com.scottlogic.datahelix.generator.profile.dtos.FieldDTO;
import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.AtomicConstraintDTO;
import com.scottlogic.datahelix.generator.profile.validators.profile.ConstraintValidator;
Expand Down Expand Up @@ -59,7 +60,9 @@ ValidationResult fieldTypeMustMatchValueType(T dto, Object value)
{
return ValidationResult.failure("Value " + value + " must be a boolean" + getErrorInfo(dto));
}
if (!(value instanceof Number || value instanceof String && isNumber((String)value)) && fieldType == FieldType.NUMERIC)
if (!(value instanceof Number || value instanceof String && isNumber((String)value) ||
value instanceof WeightedElement && !(((WeightedElement) value).element() instanceof Number)) &&
fieldType == FieldType.NUMERIC)
{
return ValidationResult.failure("Value " + value + " must be a number" + getErrorInfo(dto));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement;
import com.scottlogic.datahelix.generator.profile.dtos.constraints.ConstraintDTO;
import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.EqualToConstraintDTO;
import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.GranularToConstraintDTO;
Expand Down Expand Up @@ -110,7 +111,7 @@ public void shouldDeserialiseInSetCsvFileWithoutException() throws IOException {
// Assert
InSetConstraintDTO expected = new InSetConstraintDTO();
expected.field = "country";
expected.values = Collections.singletonList("test");
expected.values = Collections.singletonList(new WeightedElement<>("test", 1.0));

assertThat(actual, sameBeanAs(expected));
}
Expand Down

0 comments on commit 954273e

Please sign in to comment.