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 #1446 from cdowding-sl/feature/profile-module-update
Browse files Browse the repository at this point in the history
Refactored Types class
  • Loading branch information
cdowding-sl authored Oct 10, 2019
2 parents ff2ef9e + 31c07e6 commit 83deaa3
Show file tree
Hide file tree
Showing 28 changed files with 85 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

public class Field {
public final String name;
private final Types type;
private final FieldType type;
private final boolean unique;
private final String formatting;
private final boolean internal;

public Field(String name, Types type, boolean unique, String formatting, boolean internal) {
public Field(String name, FieldType type, boolean unique, String formatting, boolean internal) {
this.name = name;
this.type = type;
this.unique = unique;
Expand Down Expand Up @@ -66,7 +66,7 @@ public String getFormatting() {
return formatting;
}

public Types getType() {
public FieldType getType() {
return type;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.scottlogic.deg.common.profile;

public enum FieldType
{
NUMERIC,
STRING,
DATETIME
}
20 changes: 0 additions & 20 deletions common/src/main/java/com/scottlogic/deg/common/profile/Types.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

public class FieldBuilder {
public static Field createField(String name) {
return createField(name, Types.STRING);
return createField(name, FieldType.STRING);
}
public static Field createInternalField(String name) {
return createInternalField(name, Types.STRING);
return createInternalField(name, FieldType.STRING);
}
public static Field createField(String name, Types type) {
public static Field createField(String name, FieldType type) {
return new Field(name, type, false, null, false);
}
public static Field createInternalField(String name, Types type) {
public static Field createInternalField(String name, FieldType type) {
return new Field(name, type, false, null, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.scottlogic.deg.generator.fieldspecs;

import com.scottlogic.deg.common.profile.Types;
import com.scottlogic.deg.common.profile.FieldType;
import com.scottlogic.deg.generator.fieldspecs.whitelist.DistributedList;
import com.scottlogic.deg.generator.restrictions.*;

Expand All @@ -41,7 +41,7 @@ public static FieldSpec fromList(DistributedList<Object> whitelist) {
public static FieldSpec fromRestriction(TypedRestrictions restrictions) {
return new FieldSpec(null, restrictions, true, Collections.emptySet());
}
public static FieldSpec fromType(Types type) {
public static FieldSpec fromType(FieldType type) {
switch (type) {
case NUMERIC:
return new FieldSpec(null, createDefaultNumericRestrictions(), true, Collections.emptySet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.scottlogic.deg.generator.fieldspecs;

import com.scottlogic.deg.common.profile.Types;
import com.scottlogic.deg.generator.restrictions.*;
import com.scottlogic.deg.generator.restrictions.linear.LinearRestrictionsMerger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,26 @@

package com.scottlogic.deg.generator.generation;

import com.scottlogic.deg.common.profile.Types;
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.string.generators.RegexStringGenerator;
import com.scottlogic.deg.generator.generation.string.generators.StringGenerator;
import com.scottlogic.deg.generator.restrictions.*;
import com.scottlogic.deg.generator.restrictions.linear.LinearRestrictions;
import com.scottlogic.deg.common.profile.constraintdetail.NumericGranularity;

import java.math.BigDecimal;
import java.time.OffsetDateTime;
import java.util.*;

import static com.scottlogic.deg.common.util.Defaults.*;
import static com.scottlogic.deg.generator.restrictions.linear.LinearRestrictionsFactory.createDateTimeRestrictions;
import static com.scottlogic.deg.generator.restrictions.linear.LinearRestrictionsFactory.createNumericRestrictions;
import static com.scottlogic.deg.generator.utils.Defaults.*;

public class FieldValueSourceEvaluator {
private static final FieldValueSource NULL_ONLY_SOURCE = new NullOnlySource();

public FieldValueSource getFieldValueSources(Types type, FieldSpec fieldSpec){
public FieldValueSource getFieldValueSources(FieldType type, FieldSpec fieldSpec){

Optional<FieldValueSource> source = getSource(type, fieldSpec);

Expand All @@ -54,7 +51,7 @@ public FieldValueSource getFieldValueSources(Types type, FieldSpec fieldSpec){
return new NullAppendingValueSource(source.get());
}

private Optional<FieldValueSource> getSource(Types type, FieldSpec fieldSpec) {
private Optional<FieldValueSource> getSource(FieldType type, FieldSpec fieldSpec) {
if (fieldSpec.getWhitelist() != null){
if (fieldSpec.getWhitelist().isEmpty()){
return Optional.empty();
Expand All @@ -66,7 +63,7 @@ private Optional<FieldValueSource> getSource(Types type, FieldSpec fieldSpec) {
return Optional.of(getRestrictionSource(type, fieldSpec));
}

private FieldValueSource getRestrictionSource(Types type, FieldSpec fieldSpec) {
private FieldValueSource getRestrictionSource(FieldType type, FieldSpec fieldSpec) {
switch (type) {
case DATETIME:
return getDateTimeSource(fieldSpec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.scottlogic.deg.generator.restrictions;

import com.scottlogic.deg.common.profile.Types;
import com.scottlogic.deg.generator.generation.string.generators.StringGenerator;

import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.scottlogic.deg.generator.restrictions.linear;

import com.scottlogic.deg.common.profile.Types;
import com.scottlogic.deg.common.profile.constraintdetail.Granularity;

import java.math.BigDecimal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@

import com.scottlogic.deg.common.profile.Field;
import com.scottlogic.deg.common.profile.ProfileFields;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.util.Collections;

import static com.scottlogic.deg.common.profile.FieldBuilder.createField;
import static com.scottlogic.deg.generator.builders.TestConstraintNodeBuilder.constraintNode;
import static com.shazam.shazamcrest.MatcherAssert.assertThat;
import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
import static com.scottlogic.deg.common.profile.FieldBuilder.createField;

class DecisionTreeOptimiserTest {

Expand Down Expand Up @@ -94,79 +93,4 @@ public void optimise_oneCommonIf(){

assertThat(actual, sameBeanAs(original));
}

@Disabled ("what???")
@Test
public void optimise_oneCommonIfTwoFields() {
ConstraintNode original = constraintNode()
.where(A).isInSet("a1", "a2")
.where(B).isInSet("b1", "b2")
.where(C).isInSet("c1", "c2")
.where(D).isInSet("d1", "d2")
.where(E).isInSet("e1", "e2")
.where(F).isInSet("f1", "f2")
.withDecision(
constraintNode()
.where(A).isInSet("a1")
.where(B).isInSet("b1")
.where(C).isInSet("c1")
.where(E).isInSet("e1"),
constraintNode()
.where(A).isNotInSet("a1"),
constraintNode()
.where(B).isNotInSet("b1"),
constraintNode()
.where(C).isNotInSet("c1"))
.withDecision(
constraintNode()
.where(A).isInSet("a1")
.where(B).isInSet("b1")
.where(D).isInSet("d1")
.where(F).isInSet("f1"),
constraintNode()
.where(A).isNotInSet("a1"),
constraintNode()
.where(B).isNotInSet("b1"),
constraintNode()
.where(D).isNotInSet("d1"))
.build();

ConstraintNode actual = optimiser.optimiseTree(new DecisionTree(original, new ProfileFields(Collections.EMPTY_LIST)))
.getRootNode();


ConstraintNode expected = constraintNode()
.where(A).isInSet("a1", "a2")
.where(B).isInSet("b1", "b2")
.where(C).isInSet("c1", "c2")
.where(D).isInSet("d1", "d2")
.where(E).isInSet("e1", "e2")
.where(F).isInSet("f1", "f2")
.withDecision(
constraintNode()
.where(A).isInSet("a1")
.withDecision(
constraintNode()
.where(B).isInSet("b1")
.withDecision(
constraintNode()
.where(C).isInSet("c1")
.where(E).isInSet("e1"),
constraintNode()
.where(C).isNotInSet("c1")
)
.withDecision(
constraintNode()
.where(D).isInSet("d1")
.where(F).isInSet("f1"),
constraintNode()
.where(D).isNotInSet("d1")),
constraintNode()
.where(B).isNotInSet("b1")),
constraintNode()
.where(A).isNotInSet("a1")
).build();

assertThat(actual, sameBeanAs(expected));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.scottlogic.deg.generator.fieldspecs;

import com.scottlogic.deg.common.profile.Types;
import com.scottlogic.deg.common.profile.FieldType;
import com.scottlogic.deg.generator.fieldspecs.whitelist.DistributedList;
import com.scottlogic.deg.generator.generation.string.generators.StringGenerator;
import com.scottlogic.deg.generator.restrictions.*;
Expand All @@ -30,7 +30,7 @@
import java.util.Arrays;
import java.util.Optional;

import static com.scottlogic.deg.common.profile.Types.*;
import static com.scottlogic.deg.common.profile.FieldType.*;
import static com.scottlogic.deg.generator.restrictions.StringRestrictionsFactory.forMaxLength;
import static com.scottlogic.deg.generator.utils.Defaults.*;
import static org.hamcrest.core.IsEqual.equalTo;
Expand All @@ -42,7 +42,7 @@ class FieldSpecTests {

@Test
void equals_objTypeIsNotFieldSpec_returnsFalse() {
FieldSpec fieldSpec = FieldSpec.fromType(Types.STRING);
FieldSpec fieldSpec = FieldSpec.fromType(FieldType.STRING);

boolean result = fieldSpec.equals("Test");

Expand All @@ -56,7 +56,7 @@ void equals_objTypeIsNotFieldSpec_returnsFalse() {
void equals_fieldSpecHasSetRestrictionsAndOtherObjectSetRestrictionsNull_returnsFalse() {
FieldSpec fieldSpec = FieldSpec.fromList(DistributedList.singleton("whitelist"));

boolean result = fieldSpec.equals(FieldSpec.fromType(Types.STRING));
boolean result = fieldSpec.equals(FieldSpec.fromType(FieldType.STRING));

assertFalse(
"Expected that when the field spec has set restrictions and the other object set restrictions are null a false value should be returned but was true",
Expand All @@ -66,7 +66,7 @@ void equals_fieldSpecHasSetRestrictionsAndOtherObjectSetRestrictionsNull_returns

@Test
void equals_fieldSpecSetRestrictionsNullAndOtherObjectHasSetRestrictions_returnsFalse() {
FieldSpec fieldSpec = FieldSpec.fromType(Types.STRING);
FieldSpec fieldSpec = FieldSpec.fromType(FieldType.STRING);

boolean result = fieldSpec.equals(
FieldSpec.fromList(DistributedList.singleton("whitelist")));
Expand Down Expand Up @@ -112,16 +112,16 @@ void equals_fieldSpecNumericRestrictionsNotNullAndOtherObjectNumericRestrictions

@Test
public void shouldCreateNewInstanceWithNullRestrictions() {
FieldSpec original = FieldSpec.fromType(Types.STRING);
FieldSpec original = FieldSpec.fromType(FieldType.STRING);
FieldSpec augmentedFieldSpec = original.withNotNull();

Assert.assertNotSame(original, augmentedFieldSpec);
}

@Test
public void emptyFieldSpecsShouldBeEqualAndHaveSameHashCode() {
FieldSpec first = FieldSpec.fromType(Types.STRING);
FieldSpec second = FieldSpec.fromType(Types.STRING);
FieldSpec first = FieldSpec.fromType(FieldType.STRING);
FieldSpec second = FieldSpec.fromType(FieldType.STRING);

Assert.assertThat(first, equalTo(second));
Assert.assertThat(first.hashCode(), equalTo(second.hashCode()));
Expand Down Expand Up @@ -167,25 +167,25 @@ public void fieldSpecsWithUnequalRestrictionsShouldBeUnequal() {

@Test
public void fieldSpecsWithEqualTypeRestrictionsShouldBeEqual() {
FieldSpec a = FieldSpec.fromType(Types.STRING);
FieldSpec b = FieldSpec.fromType(Types.STRING);
FieldSpec a = FieldSpec.fromType(FieldType.STRING);
FieldSpec b = FieldSpec.fromType(FieldType.STRING);

Assert.assertThat(a, equalTo(b));
Assert.assertThat(a.hashCode(), equalTo(b.hashCode()));
}

@Test
public void fieldSpecsWithEqualNullRestrictionsShouldBeEqual() {
FieldSpec a = FieldSpec.fromType(Types.STRING).withNotNull();
FieldSpec b = FieldSpec.fromType(Types.STRING).withNotNull();
FieldSpec a = FieldSpec.fromType(FieldType.STRING).withNotNull();
FieldSpec b = FieldSpec.fromType(FieldType.STRING).withNotNull();

Assert.assertThat(a, equalTo(b));
Assert.assertThat(a.hashCode(), equalTo(b.hashCode()));
}

@Test
public void fieldSpecsWithUnequalNullRestrictionsShouldBeUnequal() {
FieldSpec a = FieldSpec.fromType(Types.STRING).withNotNull();
FieldSpec a = FieldSpec.fromType(FieldType.STRING).withNotNull();
FieldSpec b = FieldSpec.nullOnly();

Assert.assertThat(a, not(equalTo(b)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.scottlogic.deg.generator.fieldspecs;

import com.scottlogic.deg.common.profile.Types;
import com.scottlogic.deg.generator.restrictions.StringRestrictions;
import com.scottlogic.deg.generator.restrictions.StringRestrictionsFactory;
import com.scottlogic.deg.generator.restrictions.StringRestrictionsMerger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.google.common.collect.ImmutableMap;
import com.scottlogic.deg.common.profile.Field;
import com.scottlogic.deg.common.profile.ProfileFields;
import com.scottlogic.deg.common.profile.Types;
import com.scottlogic.deg.common.profile.FieldType;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
Expand All @@ -17,7 +17,7 @@ class RowSpecMergerTest {
RowSpecMerger rowSpecMerger = new RowSpecMerger(new FieldSpecMerger());

FieldSpec isNull = FieldSpec.nullOnly();
FieldSpec notNull = FieldSpec.fromType(Types.STRING).withNotNull();
FieldSpec notNull = FieldSpec.fromType(FieldType.STRING).withNotNull();
Field A = createField("A");
Field B = createField("B");
ProfileFields fields = new ProfileFields(Arrays.asList(A, B));
Expand Down
Loading

0 comments on commit 83deaa3

Please sign in to comment.