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

Commit

Permalink
Merge branch 'master' into add-license-check-script
Browse files Browse the repository at this point in the history
  • Loading branch information
r-stuart authored Nov 8, 2019
2 parents f409d5d + 95bc5ab commit 4c66bf7
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 255 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,52 +24,59 @@
import com.scottlogic.deg.generator.profile.constraints.Constraint;
import com.scottlogic.deg.generator.restrictions.linear.LinearRestrictions;

public class AfterRelation<T extends Comparable<T>> implements FieldSpecRelation
{
import static com.scottlogic.deg.common.util.GranularityUtils.readGranularity;

public class AfterRelation<T extends Comparable<T>> implements FieldSpecRelation {
private final Field main;
private final Field other;
private final boolean inclusive;
private final LinearDefaults<T> defaults;
private final Granularity<T> offsetGranularity;
private final int offset;

public AfterRelation(Field main, Field other, boolean inclusive, LinearDefaults<T> defaults) {
public AfterRelation(Field main, Field other, boolean inclusive, LinearDefaults<T> defaults, Granularity<T> offsetGranularity, int offset) {
this.main = main;
this.other = other;
this.inclusive = inclusive;
this.defaults = defaults;
this.offsetGranularity = offsetGranularity != null ? offsetGranularity : readGranularity(main.getType(), null);
this.offset = offset;
}

@Override
public FieldSpec createModifierFromOtherFieldSpec(FieldSpec otherFieldSpec) {
if (otherFieldSpec instanceof NullOnlyFieldSpec){
if (otherFieldSpec instanceof NullOnlyFieldSpec) {
return FieldSpecFactory.nullOnly();
}
if (otherFieldSpec instanceof WhitelistFieldSpec) {
throw new UnsupportedOperationException("cannot combine sets with after relation, Issue #1489");
}

LinearRestrictions<T> lr = (LinearRestrictions)((RestrictionsFieldSpec) otherFieldSpec).getRestrictions();
return createFieldSpec(lr.getMin(), defaults.granularity());
LinearRestrictions<T> otherRestrictions = (LinearRestrictions) ((RestrictionsFieldSpec) otherFieldSpec).getRestrictions();
T min = otherRestrictions.getMin();
T offsetMin = offsetGranularity.getNext(min, offset);

return createFromMin(offsetMin, offsetGranularity);
}

@Override
public FieldSpec createModifierFromOtherValue(DataBagValue otherFieldGeneratedValue) {
if (otherFieldGeneratedValue.getValue() == null) return FieldSpecFactory.fromType(main.getType());
return createFieldSpec((T) otherFieldGeneratedValue.getValue(), defaults.granularity());

T offsetValue = offset > 0
? offsetGranularity.getNext((T) otherFieldGeneratedValue.getValue(), offset)
: (T) otherFieldGeneratedValue.getValue();
return createFromMin(offsetValue, defaults.granularity());
}

private FieldSpec createFieldSpec(T min, Granularity<T> granularity) {
if (!inclusive){
private FieldSpec createFromMin(T min, Granularity<T> granularity) {
if (!inclusive) {
min = granularity.getNext(min);
}

return FieldSpecFactory.fromRestriction(new LinearRestrictions<>(min, defaults.max(), granularity));
}

@Override
public FieldSpecRelation inverse() {
return new BeforeRelation(other, main, inclusive, defaults);
}

@Override
public Field main() {
return main;
Expand All @@ -80,13 +87,18 @@ public Field other() {
return other;
}

@Override
public FieldSpecRelation inverse() {
return new BeforeRelation(other, main, inclusive, defaults, offsetGranularity, -1 * offset);
}

@Override
public String toString() {
return String.format("%s is after %s%s", main, inclusive ? "or equal to " : "", other);
return String.format("%s is after %s%s %s %s", main, inclusive ? "or equal to " : "", other, offset >= 0 ? "plus" : "minus", Math.abs(offset));
}

@Override
public Constraint negate() {
return new BeforeRelation(main, other, !inclusive, defaults);
throw new UnsupportedOperationException("Negating relations with an offset is not supported");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,23 @@
import com.scottlogic.deg.generator.profile.constraints.Constraint;
import com.scottlogic.deg.generator.restrictions.linear.LinearRestrictions;

public class BeforeRelation<T extends Comparable<T>> implements FieldSpecRelation
{
import static com.scottlogic.deg.common.util.GranularityUtils.readGranularity;

public class BeforeRelation<T extends Comparable<T>> implements FieldSpecRelation {
private final Field main;
private final Field other;
private final boolean inclusive;
private final LinearDefaults<T> defaults;
private final Granularity<T> offsetGranularity;
private final int offset;

public BeforeRelation(Field main, Field other, boolean inclusive, LinearDefaults<T> defaults) {
public BeforeRelation(Field main, Field other, boolean inclusive, LinearDefaults<T> defaults, Granularity<T> offsetGranularity, int offset) {
this.main = main;
this.other = other;
this.inclusive = inclusive;
this.defaults = defaults;
this.offsetGranularity = offsetGranularity != null ? offsetGranularity : readGranularity(main.getType(), null);
this.offset = offset;
}

@Override
Expand All @@ -47,14 +52,22 @@ public FieldSpec createModifierFromOtherFieldSpec(FieldSpec otherFieldSpec) {
throw new UnsupportedOperationException("cannot combine sets with before relation, Issue #1489");
}

LinearRestrictions<T> lr = (LinearRestrictions)((RestrictionsFieldSpec) otherFieldSpec).getRestrictions();
return createFromMax(lr.getMax(), defaults.granularity());
LinearRestrictions<T> otherRestrictions = (LinearRestrictions)((RestrictionsFieldSpec) otherFieldSpec).getRestrictions();
T max = otherRestrictions.getMax();
T offsetMax = offsetGranularity.getPrevious(max, offset);

return createFromMax(offsetMax, offsetGranularity);
}

@Override
public FieldSpec createModifierFromOtherValue(DataBagValue otherFieldGeneratedValue) {
if (otherFieldGeneratedValue.getValue() == null) return FieldSpecFactory.fromType(main.getType());
return createFromMax((T) otherFieldGeneratedValue.getValue(), defaults.granularity());

T offsetValue = offset > 0
? offsetGranularity.getPrevious((T) otherFieldGeneratedValue.getValue(), offset)
: (T) otherFieldGeneratedValue.getValue();

return createFromMax(offsetValue, defaults.granularity());
}

private FieldSpec createFromMax(T max, Granularity<T> granularity) {
Expand All @@ -65,7 +78,6 @@ private FieldSpec createFromMax(T max, Granularity<T> granularity) {
return FieldSpecFactory.fromRestriction(new LinearRestrictions<>(defaults.min(), max, granularity));
}


@Override
public Field main() {
return main;
Expand All @@ -78,16 +90,16 @@ public Field other() {

@Override
public FieldSpecRelation inverse() {
return new AfterRelation(other, main, inclusive, defaults);
return new AfterRelation(other, main, inclusive, defaults, offsetGranularity,-1 * offset);
}

@Override
public String toString() {
return String.format("%s is before %s%s", main, inclusive ? "or equal to " : "", other);
return String.format("%s is before %s%s %s %s", main, inclusive ? "or equal to " : "", other, offset >= 0 ? "plus" : "minus", Math.abs(offset));
}

@Override
public Constraint negate() {
return new AfterRelation(main, other, !inclusive, defaults);
throw new UnsupportedOperationException("Negating relations with an offset is not supported");
}
}
Loading

0 comments on commit 4c66bf7

Please sign in to comment.