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

Commit

Permalink
fix(#841): Relational data
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Laing committed May 15, 2020
1 parent 811d925 commit 53425fc
Show file tree
Hide file tree
Showing 48 changed files with 1,527 additions and 73 deletions.
10 changes: 10 additions & 0 deletions .idea/runConfigurations/Example_generation__relational_.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
/** A set of values representing one complete, discrete output (eg, this could be used to make a full CSV row) */
public interface GeneratedObject {
Object getFormattedValue(Field field);
Object getValue(Field field);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.scottlogic.datahelix.generator.output.guice;
package com.scottlogic.datahelix.generator.common.output;

public enum OutputFormat {
CSV,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.datahelix.generator.common.output;

import java.util.Map;

public interface RelationalGeneratedObject {
Map<String, SubGeneratedObject> getSubObjects();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.datahelix.generator.common.output;

import com.scottlogic.datahelix.generator.common.profile.Field;

import java.util.List;

public interface SubGeneratedObject {
List<Field> getFields();
List<GeneratedObject> getData();
boolean isArray();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.scottlogic.datahelix.generator.core.decisiontree.treepartitioning.TreePartitioner;
import com.scottlogic.datahelix.generator.core.generation.combinationstrategies.CombinationStrategy;
import com.scottlogic.datahelix.generator.core.generation.databags.DataBag;
import com.scottlogic.datahelix.generator.core.generation.relationships.RelationshipsProcessor;
import com.scottlogic.datahelix.generator.core.generation.visualiser.Visualiser;
import com.scottlogic.datahelix.generator.core.generation.visualiser.VisualiserFactory;
import com.scottlogic.datahelix.generator.core.profile.Profile;
Expand All @@ -44,6 +45,7 @@ public class DecisionTreeDataGenerator implements DataGenerator {
private final CombinationStrategy partitionCombiner;
private final UpfrontTreePruner upfrontTreePruner;
private final VisualiserFactory visualiserFactory;
private final RelationshipsProcessor relationshipsProcessor;

@Inject
public DecisionTreeDataGenerator(
Expand All @@ -54,7 +56,8 @@ public DecisionTreeDataGenerator(
DataGeneratorMonitor monitor,
CombinationStrategy combinationStrategy,
UpfrontTreePruner upfrontTreePruner,
VisualiserFactory visualiserFactory) {
VisualiserFactory visualiserFactory,
RelationshipsProcessor relationshipsProcessor) {
this.decisionTreeGenerator = decisionTreeGenerator;
this.treePartitioner = treePartitioner;
this.treeOptimiser = optimiser;
Expand All @@ -63,6 +66,7 @@ public DecisionTreeDataGenerator(
this.partitionCombiner = combinationStrategy;
this.upfrontTreePruner = upfrontTreePruner;
this.visualiserFactory = visualiserFactory;
this.relationshipsProcessor = relationshipsProcessor;
}

@Override
Expand All @@ -81,9 +85,12 @@ public Stream<GeneratedObject> generateData(Profile profile) {
.map(treeOptimiser::optimiseTree)
.map(tree -> () -> treeWalker.walk(tree));

//noinspection RedundantCast
return partitionCombiner.permute(partitionedDataBags)
.map(d-> (GeneratedObject)d);
.map(generatedObject -> relationshipsProcessor.produceRelationalObjects(
profile.getFields(),
generatedObject,
profile.getRelationships(),
this));
}

private void visualiseTree(DecisionTree decisionTree, String title) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ public DataBag(Map<Field, DataBagValue> fieldToValue) {
this.fieldToValue = fieldToValue;
}

@Override
public Object getValue(Field field) {
return getDataBagValue(field).getValue();
}

@Override
public Object getFormattedValue(Field field) {
Object value = getDataBagValue(field).getValue();
Object value = getValue(field);
String formatting = field.getFormatting();

if (formatting == null || value == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.datahelix.generator.core.generation.relationships;

import com.scottlogic.datahelix.generator.common.profile.Field;
import com.scottlogic.datahelix.generator.common.profile.FieldType;
import com.scottlogic.datahelix.generator.common.profile.Fields;
import com.scottlogic.datahelix.generator.common.profile.SpecificFieldType;

public class ExtentAugmentedFields extends Fields {
private static final SpecificFieldType integer = new SpecificFieldType("integer", FieldType.NUMERIC, null);

public static final String minField = "min";
public static final String maxField = "max";
public static final Field min = new Field(minField, integer, false, null, false, true, null);
public static final Field max = new Field(maxField, integer, false, null, false, true, null);

public ExtentAugmentedFields(Fields fields) {
super(fields.asList());
}

@Override
public Field getByName(String fieldName) {
if (fieldName.equals(minField)) {
return min;
}

if (fieldName.equals(maxField)) {
return max;
}

return super.getByName(fieldName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.datahelix.generator.core.generation.relationships;

import com.scottlogic.datahelix.generator.common.output.GeneratedObject;
import com.scottlogic.datahelix.generator.common.output.RelationalGeneratedObject;
import com.scottlogic.datahelix.generator.common.output.SubGeneratedObject;
import com.scottlogic.datahelix.generator.common.profile.Field;
import com.scottlogic.datahelix.generator.core.profile.relationships.Relationship;

import java.util.HashMap;
import java.util.Map;

public class GeneratedRelationalData implements GeneratedObject, RelationalGeneratedObject {
private final GeneratedObject underlyingObject;
private final Map<String, SubGeneratedObject> subObjects = new HashMap<>();

public GeneratedRelationalData(GeneratedObject underlyingObject) {
this.underlyingObject = underlyingObject;
}

@Override
public Object getFormattedValue(Field field) {
return underlyingObject.getFormattedValue(field);
}

@Override
public Object getValue(Field field) {
return underlyingObject.getValue(field);
}

@Override
public Map<String, SubGeneratedObject> getSubObjects() {
return subObjects;
}

public void addSubObject(Relationship relationship, SubGeneratedObject subObject) {
if (!subObjects.containsKey(relationship.getName())) {
subObjects.put(relationship.getName(), subObject);
} else {
throw new RuntimeException("Sub-object for this relationship already exists");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.datahelix.generator.core.generation.relationships;

public class OneToManyRange {
private final int min;
private final Integer max;

public OneToManyRange(int min, Integer max) {
this.min = min;
this.max = max;
}

public int getMin() {
return min;
}

public Integer getMax() {
return max;
}

public OneToManyRange withMin(int min) {
if (min > this.min) {
return new OneToManyRange(min, max);
}

return this;
}

public OneToManyRange withMax(int max) {
if (this.max == null || max < this.max) {
return new OneToManyRange(min, max);
}

return this;
}

public boolean isEmpty() {
return this.max != null && this.min >= this.max;
}

@Override
public String toString() {
if (max == null) {
return min + "..";
}

return min + ".." + max;
}
}
Loading

0 comments on commit 53425fc

Please sign in to comment.