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

Commit

Permalink
fix(#1710): Remove DistributedList from InMapRelation and InMapIndexR…
Browse files Browse the repository at this point in the history
…elation

... as these relations just needed a regular list
  • Loading branch information
matthewdunsdon committed Dec 21, 2020
1 parent e090cbb commit d8e3e0f
Show file tree
Hide file tree
Showing 21 changed files with 118 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public double weight() {
return weight;
}

public <F> WeightedElement<F> withMappedValue(Function<E, F> parse) {
return new WeightedElement<F>(parse.apply(element), weight);
}

public static <T> WeightedElement<T> withDefaultWeight(final T element) {
return new WeightedElement<>(element, DEFAULT_WEIGHT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,23 @@
package com.scottlogic.datahelix.generator.core.fieldspecs;

import com.scottlogic.datahelix.generator.common.profile.FieldType;
import com.scottlogic.datahelix.generator.common.profile.InSetRecord;
import com.scottlogic.datahelix.generator.common.util.Defaults;
import com.scottlogic.datahelix.generator.common.whitelist.DistributedList;
import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement;
import com.scottlogic.datahelix.generator.core.generation.fieldvaluesources.FieldValueSource;
import com.scottlogic.datahelix.generator.core.restrictions.TypedRestrictions;
import com.scottlogic.datahelix.generator.core.restrictions.bool.BooleanRestrictions;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static com.scottlogic.datahelix.generator.core.restrictions.linear.LinearRestrictionsFactory.*;
import static com.scottlogic.datahelix.generator.core.restrictions.string.StringRestrictionsFactory.forMaxLength;
import static java.util.Collections.singletonList;

public class FieldSpecFactory {
private static final NullOnlyFieldSpec NULL_ONLY_FIELD_SPEC = new NullOnlyFieldSpec();
Expand All @@ -36,6 +42,22 @@ private FieldSpecFactory() {
throw new IllegalArgumentException("Should not instantiate factory");
}

public static WhitelistFieldSpec fromAllowedList(Collection<Object> allowedList) {
return fromList(DistributedList.uniform(allowedList));
}

public static WhitelistFieldSpec fromInSetRecords(List<InSetRecord> inSetRecords) {
DistributedList<Object> distributedList = new DistributedList<>(inSetRecords.stream()
.map(v -> new WeightedElement<>(v.getElement(), v.getWeightValueOrDefault()))
.collect(Collectors.toList()));

return fromList(distributedList);
}

public static WhitelistFieldSpec fromAllowedSingleValue(Object value) {
return fromAllowedList(singletonList(value));
}

public static WhitelistFieldSpec fromList(DistributedList<Object> whitelist) {
return new WhitelistFieldSpec(whitelist, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,10 @@

import com.scottlogic.datahelix.generator.core.generation.databags.DataBagValue;

import static com.scottlogic.datahelix.generator.common.whitelist.DistributedList.singleton;

public class FieldSpecHelper {
public FieldSpec getFieldSpecForValue(DataBagValue fieldValue) {
if (fieldValue.getValue() == null) {
return FieldSpecFactory.nullOnly();
}

return FieldSpecFactory.fromList(singleton(fieldValue.getValue()))
.withNotNull();
return (fieldValue.getValue() == null)
? FieldSpecFactory.nullOnly()
: FieldSpecFactory.fromAllowedSingleValue(fieldValue.getValue()).withNotNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.scottlogic.datahelix.generator.core.generation.fieldvaluesources.FieldValueSource;

import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

public class WhitelistFieldSpec extends FieldSpec {
private final DistributedList<Object> whitelist;
Expand Down Expand Up @@ -52,6 +54,13 @@ public DistributedList<Object> getWhitelist() {
return whitelist;
}

public WhitelistFieldSpec withMappedValues(Function<Object, Object> parse) {
DistributedList<Object> allowedValuesList = new DistributedList<>(whitelist.distributedList().stream()
.map(value -> value.withMappedValue(parse)).collect(Collectors.toList()));

return new WhitelistFieldSpec(allowedValuesList, nullable);
}

@Override
public String toString() {
if (whitelist.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,11 @@
import com.scottlogic.datahelix.generator.common.profile.Field;
import com.scottlogic.datahelix.generator.common.profile.FieldType;
import com.scottlogic.datahelix.generator.common.profile.Granularity;
import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement;
import com.scottlogic.datahelix.generator.core.fieldspecs.*;
import com.scottlogic.datahelix.generator.common.whitelist.DistributedList;
import com.scottlogic.datahelix.generator.core.generation.databags.DataBagValue;
import com.scottlogic.datahelix.generator.core.profile.constraints.Constraint;
import com.scottlogic.datahelix.generator.core.restrictions.linear.LinearRestrictions;

import java.util.List;
import java.util.stream.Collectors;

public class EqualToOffsetRelation<T extends Comparable<T>> implements FieldSpecRelation {
private final Field main;
private final Field other;
Expand All @@ -52,13 +47,7 @@ public FieldSpec createModifierFromOtherFieldSpec(FieldSpec otherFieldSpec) {
}
if (otherFieldSpec instanceof WhitelistFieldSpec) {
WhitelistFieldSpec whitelistFieldSpec = (WhitelistFieldSpec) otherFieldSpec;
List<WeightedElement<T>> modified = whitelistFieldSpec
.getWhitelist()
.distributedList()
.stream()
.map(x -> new WeightedElement<>(offsetGranularity.getNext((T) x.element(), offset), x.weight()))
.collect(Collectors.toList());
return FieldSpecFactory.fromList((DistributedList) new DistributedList<>(modified));
return whitelistFieldSpec.withMappedValues(x-> offsetGranularity.getNext((T) x, offset));
}

LinearRestrictions<T> otherRestrictions = (LinearRestrictions) ((RestrictionsFieldSpec) otherFieldSpec).getRestrictions();
Expand All @@ -82,7 +71,7 @@ public FieldSpec createModifierFromOtherValue(DataBagValue otherFieldGeneratedVa
return FieldSpecFactory.fromType(FieldType.DATETIME);
}
T offsetValue = offsetGranularity.getNext(value, offset);
return FieldSpecFactory.fromList(DistributedList.singleton(offsetValue));
return FieldSpecFactory.fromAllowedSingleValue(offsetValue);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.scottlogic.datahelix.generator.common.profile.Field;
import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpec;
import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecFactory;
import com.scottlogic.datahelix.generator.common.whitelist.DistributedList;
import com.scottlogic.datahelix.generator.core.generation.databags.DataBagValue;
import com.scottlogic.datahelix.generator.core.profile.constraints.Constraint;

Expand All @@ -43,7 +42,7 @@ public FieldSpec createModifierFromOtherValue(DataBagValue otherFieldGeneratedVa
if (otherFieldGeneratedValue.getValue() == null){
return FieldSpecFactory.nullOnly();
}
return FieldSpecFactory.fromList(DistributedList.singleton(otherFieldGeneratedValue.getValue()));
return FieldSpecFactory.fromAllowedSingleValue(otherFieldGeneratedValue.getValue());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.scottlogic.datahelix.generator.common.profile.Field;
import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpec;
import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecFactory;
import com.scottlogic.datahelix.generator.common.whitelist.DistributedList;
import com.scottlogic.datahelix.generator.core.generation.databags.DataBagValue;
import com.scottlogic.datahelix.generator.core.profile.constraints.Constraint;

Expand All @@ -31,30 +30,30 @@ public class InMapIndexRelation implements FieldSpecRelation
{
private final Field main;
private final Field other;
private final DistributedList<Object> underlyingList;
private final List<Object> underlyingList;

public InMapIndexRelation(Field main, Field other, DistributedList<Object> underlyingList) {
public InMapIndexRelation(Field main, Field other, List<Object> underlyingList) {
this.main = main;
this.other = other;
this.underlyingList = underlyingList;
}

@Override
public FieldSpec createModifierFromOtherFieldSpec(FieldSpec otherFieldSpec) {
List<Object> whiteList = new ArrayList<>();
List<Object> allowedList = new ArrayList<>();

for (int i = 0; i < underlyingList.list().size(); i++) {
Object testingElement = underlyingList.list().get(i);
for (int i = 0; i < underlyingList.size(); i++) {
Object testingElement = underlyingList.get(i);
if (otherFieldSpec.canCombineWithWhitelistValue(testingElement)) {
whiteList.add(BigDecimal.valueOf(i));
allowedList.add(BigDecimal.valueOf(i));
}
}
return FieldSpecFactory.fromList(DistributedList.uniform(whiteList)).withNotNull();
return FieldSpecFactory.fromAllowedList(allowedList).withNotNull();
}

@Override
public FieldSpec createModifierFromOtherValue(DataBagValue otherFieldGeneratedValue) {
throw new UnsupportedOperationException("reduceToFieldSpec is unsuported in InMapIndexRelation");
throw new UnsupportedOperationException("reduceToFieldSpec is unsupported in InMapIndexRelation");
}

@Override
Expand All @@ -72,10 +71,6 @@ public Field other() {
return other;
}

public DistributedList<Object> getUnderlyingList() {
return this.underlyingList;
}

@Override
public Constraint negate() {
throw new UnsupportedOperationException("in map relations cannot currently be negated");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
import com.scottlogic.datahelix.generator.common.profile.Field;
import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpec;
import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecFactory;
import com.scottlogic.datahelix.generator.common.whitelist.DistributedList;
import com.scottlogic.datahelix.generator.core.generation.databags.DataBagValue;
import com.scottlogic.datahelix.generator.core.profile.constraints.Constraint;

import java.math.BigDecimal;
import java.util.List;

public class InMapRelation implements FieldSpecRelation
{
private final Field main;
private final Field other;
private final DistributedList<Object> underlyingList;
private final List<Object> underlyingList;

public InMapRelation(Field main, Field other, DistributedList<Object> underlyingList) {
public InMapRelation(Field main, Field other, List<Object> underlyingList) {
this.main = main;
this.other = other;
this.underlyingList = underlyingList;
Expand All @@ -46,8 +46,7 @@ public FieldSpec createModifierFromOtherFieldSpec(FieldSpec otherFieldSpec) {
public FieldSpec createModifierFromOtherValue(DataBagValue otherFieldGeneratedValue) {
BigDecimal value = (BigDecimal) otherFieldGeneratedValue.getValue();

DistributedList<Object> newList = DistributedList.singleton(underlyingList.list().get(value.intValue()));
return FieldSpecFactory.fromList(newList);
return FieldSpecFactory.fromAllowedSingleValue(underlyingList.get(value.intValue()));
}

@Override
Expand All @@ -65,10 +64,6 @@ public Field other() {
return other;
}

public DistributedList<Object> getUnderlyingList() {
return this.underlyingList;
}

@Override
public Constraint negate() {
throw new UnsupportedOperationException("in map relations cannot currently be negated");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.scottlogic.datahelix.generator.core.profile.constraints.atomic;

import com.scottlogic.datahelix.generator.common.profile.Field;
import com.scottlogic.datahelix.generator.common.whitelist.DistributedList;
import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpec;
import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecFactory;

Expand All @@ -43,7 +42,7 @@ public AtomicConstraint negate() {

@Override
public FieldSpec toFieldSpec() {
return FieldSpecFactory.fromList(DistributedList.singleton(value));
return FieldSpecFactory.fromAllowedSingleValue(value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import com.scottlogic.datahelix.generator.common.ValidationException;
import com.scottlogic.datahelix.generator.common.profile.Field;
import com.scottlogic.datahelix.generator.common.profile.InSetRecord;
import com.scottlogic.datahelix.generator.common.whitelist.DistributedList;
import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement;
import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpec;
import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecFactory;

Expand Down Expand Up @@ -69,9 +67,7 @@ public FieldSpec toFieldSpec() {
return FieldSpecFactory.fromType(field.getType())
.withBlacklist(legalValues.stream().map(InSetRecord::getElement).collect(Collectors.toSet()));
}
return FieldSpecFactory.fromList(new DistributedList<>(legalValues.stream()
.map(v -> new WeightedElement<>(v.getElement(), v.getWeightValueOrDefault()))
.collect(Collectors.toList())));
return FieldSpecFactory.fromInSetRecords(legalValues);
}

public String toString(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.scottlogic.datahelix.generator.common.profile.Field;
import com.scottlogic.datahelix.generator.common.profile.FieldBuilder;
import com.scottlogic.datahelix.generator.common.whitelist.DistributedList;
import com.scottlogic.datahelix.generator.core.generation.databags.DataBagValue;
import org.junit.jupiter.api.Test;

Expand All @@ -34,8 +33,7 @@ void getFieldSpecForValue() {

FieldSpec actual = fieldSpecHelper.getFieldSpecForValue(input);

FieldSpec expected = FieldSpecFactory.fromList(DistributedList.singleton("value"))
.withNotNull();
FieldSpec expected = FieldSpecFactory.fromAllowedSingleValue("value").withNotNull();

assertEquals(actual, expected);
}
Expand Down

0 comments on commit d8e3e0f

Please sign in to comment.