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

Commit

Permalink
Merge pull request #1463 from finos/1430-generic-linear-value-source
Browse files Browse the repository at this point in the history
1430 generic linear value source
  • Loading branch information
pdaulbyscottlogic authored Oct 21, 2019
2 parents 5ed83ee + 76265c0 commit d5226be
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 336 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.scottlogic.deg.common.ValidationException;
import com.scottlogic.deg.common.date.TemporalAdjusterGenerator;
import com.scottlogic.deg.generator.utils.RandomNumberGenerator;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
Expand Down Expand Up @@ -78,6 +80,19 @@ public OffsetDateTime getPrevious(OffsetDateTime value) {
return trimToGranularity(value);
}

@Override
public OffsetDateTime getRandom(OffsetDateTime min, OffsetDateTime max, RandomNumberGenerator randomNumberGenerator) {
long generatedLong = (long) randomNumberGenerator.nextDouble(getMilli(min), getMilli(max));

OffsetDateTime generatedDate = Instant.ofEpochMilli(generatedLong).atZone(ZoneOffset.UTC).toOffsetDateTime();

return trimToGranularity(generatedDate);
}

private long getMilli(OffsetDateTime date) {
return date.toInstant().toEpochMilli();
}

private static int nanoToMilli(int nano) {
int factor = NANOS_IN_MILLIS;
return (nano / factor) * factor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.scottlogic.deg.common.profile;

import com.scottlogic.deg.generator.utils.RandomNumberGenerator;

public interface Granularity<T> {

boolean isCorrectScale(T value);
Expand All @@ -31,4 +33,6 @@ public interface Granularity<T> {
default T getNext(T value){
return getNext(value, 1);
};

T getRandom(T min, T max, RandomNumberGenerator randomNumberGenerator);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.scottlogic.deg.common.ValidationException;
import com.scottlogic.deg.common.util.NumberUtils;

import com.scottlogic.deg.generator.utils.RandomNumberGenerator;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
Expand Down Expand Up @@ -72,6 +74,12 @@ public BigDecimal getNext(BigDecimal value) {
return value.add(BigDecimal.ONE.scaleByPowerOfTen(decimalPlaces * -1));
}

@Override
public BigDecimal getRandom(BigDecimal min, BigDecimal max, RandomNumberGenerator randomNumberGenerator) {
BigDecimal value = randomNumberGenerator.nextBigDecimal(max, max);
return trimToGranularity(value);
}

@Override
public BigDecimal getPrevious(BigDecimal value) {
if (!isCorrectScale(value)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.scottlogic.deg.common.profile.FieldType;
import com.scottlogic.deg.generator.fieldspecs.FieldSpec;
import com.scottlogic.deg.generator.generation.fieldvaluesources.*;
import com.scottlogic.deg.generator.generation.fieldvaluesources.datetime.DateTimeFieldValueSource;
import com.scottlogic.deg.generator.generation.fieldvaluesources.LinearFieldValueSource;
import com.scottlogic.deg.generator.generation.string.generators.RegexStringGenerator;
import com.scottlogic.deg.generator.generation.string.generators.StringGenerator;
import com.scottlogic.deg.generator.restrictions.*;
Expand All @@ -28,6 +28,7 @@
import java.math.BigDecimal;
import java.time.OffsetDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static com.scottlogic.deg.generator.restrictions.linear.LinearRestrictionsFactory.createDateTimeRestrictions;
import static com.scottlogic.deg.generator.restrictions.linear.LinearRestrictionsFactory.createNumericRestrictions;
Expand Down Expand Up @@ -65,21 +66,20 @@ private Optional<FieldValueSource> getSource(FieldType type, FieldSpec fieldSpec

private FieldValueSource getRestrictionSource(FieldType type, FieldSpec fieldSpec) {
switch (type) {
case DATETIME:
return getDateTimeSource(fieldSpec);
case STRING:
return getStringSource(fieldSpec);
case DATETIME:
case NUMERIC:
return getNumericSource(fieldSpec);
return getLinearSource(fieldSpec);
default:
throw new UnsupportedOperationException("unexpected type");
}
}

private FieldValueSource getNumericSource(FieldSpec fieldSpec) {
LinearRestrictions<BigDecimal> restrictions = (LinearRestrictions<BigDecimal>) fieldSpec.getRestrictions();

return new RealNumberFieldValueSource(restrictions, fieldSpec.getBlacklist());
private <T extends Comparable<T>> FieldValueSource getLinearSource(FieldSpec fieldSpec) {
LinearRestrictions<T> restrictions = (LinearRestrictions) fieldSpec.getRestrictions();
Set<T> blacklist = fieldSpec.getBlacklist().stream().map(d -> (T) d).collect(Collectors.toSet());
return new LinearFieldValueSource(restrictions, blacklist);
}

private FieldValueSource getStringSource(FieldSpec fieldSpec) {
Expand All @@ -93,10 +93,4 @@ private FieldValueSource getStringSource(FieldSpec fieldSpec) {

return generator;
}

private FieldValueSource getDateTimeSource(FieldSpec fieldSpec) {
LinearRestrictions<OffsetDateTime> restrictions = (LinearRestrictions<OffsetDateTime>) fieldSpec.getRestrictions();

return new DateTimeFieldValueSource(restrictions, fieldSpec.getBlacklist());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2019 Scott Logic Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.scottlogic.deg.generator.generation.fieldvaluesources;

import com.scottlogic.deg.generator.restrictions.linear.LinearRestrictions;
import com.scottlogic.deg.generator.utils.RandomNumberGenerator;

import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.scottlogic.deg.generator.utils.SetUtils.stream;

public class LinearFieldValueSource<T extends Comparable<T>> implements FieldValueSource {

private final LinearRestrictions<T> restrictions;
private final Set<T> blacklist;

public LinearFieldValueSource(LinearRestrictions<T> restrictions, Set<T> blacklist) {
this.restrictions = restrictions;
this.blacklist = blacklist.stream()
.map(i -> restrictions.getGranularity().trimToGranularity(i))
.collect(Collectors.toSet());
}

@Override
public Stream<T> generateAllValues() {
return stream(new LinearIterator<>(restrictions))
.filter(this::notInBlacklist);
}

@Override
public Stream<T> generateInterestingValues() {
return Stream.of(restrictions.getMin(), restrictions.getMax())
.distinct()
.filter(this::notInBlacklist);
}

@Override
public Stream<T> generateRandomValues(RandomNumberGenerator randomNumberGenerator) {
return Stream.generate(() -> restrictions.getGranularity()
.getRandom(restrictions.getMin(), restrictions.getMax(), randomNumberGenerator))
.filter(this::notInBlacklist);
}

// we call this instead of .contains because .contains doesn't treat 2 and 2.0 as equal
private boolean notInBlacklist(T t) {
return blacklist.stream().noneMatch(x->x.compareTo(t)==0);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;

LinearFieldValueSource otherSource = (LinearFieldValueSource) obj;
return restrictions.equals(otherSource.restrictions) &&
blacklist.equals(otherSource.blacklist);
}

@Override
public int hashCode() {
return Objects.hash(restrictions, blacklist);
}
}

This file was deleted.

Loading

0 comments on commit d5226be

Please sign in to comment.