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

Commit

Permalink
refactor(#1534): Use composition to control generation behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Laing committed Jan 31, 2020
1 parent d0525de commit c77df08
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
package com.scottlogic.datahelix.generator.core.generation;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.scottlogic.datahelix.generator.common.output.GeneratedObject;
import com.scottlogic.datahelix.generator.core.profile.Profile;
import com.scottlogic.datahelix.generator.core.config.detail.VisualiserLevel;
import com.scottlogic.datahelix.generator.core.decisiontree.DecisionTree;
import com.scottlogic.datahelix.generator.core.decisiontree.DecisionTreeFactory;
Expand All @@ -29,6 +27,7 @@
import com.scottlogic.datahelix.generator.core.generation.databags.DataBag;
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;
import com.scottlogic.datahelix.generator.core.walker.DecisionTreeWalker;

import java.util.function.Supplier;
Expand All @@ -45,7 +44,6 @@ public class DecisionTreeDataGenerator implements DataGenerator {
private final CombinationStrategy partitionCombiner;
private final UpfrontTreePruner upfrontTreePruner;
private final VisualiserFactory visualiserFactory;
private final long maxRows;

@Inject
public DecisionTreeDataGenerator(
Expand All @@ -56,8 +54,7 @@ public DecisionTreeDataGenerator(
DataGeneratorMonitor monitor,
CombinationStrategy combinationStrategy,
UpfrontTreePruner upfrontTreePruner,
VisualiserFactory visualiserFactory,
@Named("config:maxRows") long maxRows) {
VisualiserFactory visualiserFactory) {
this.decisionTreeGenerator = decisionTreeGenerator;
this.treePartitioner = treePartitioner;
this.treeOptimiser = optimiser;
Expand All @@ -66,13 +63,10 @@ public DecisionTreeDataGenerator(
this.partitionCombiner = combinationStrategy;
this.upfrontTreePruner = upfrontTreePruner;
this.visualiserFactory = visualiserFactory;
this.maxRows = maxRows;
}

@Override
public Stream<GeneratedObject> generateData(Profile profile) {
monitor.generationStarting();

DecisionTree decisionTree = decisionTreeGenerator.analyse(profile);
visualiseTree(decisionTree, INITIAL_TREE_VISUALISER_TITLE);

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

//noinspection RedundantCast
return partitionCombiner.permute(partitionedDataBags)
.map(d->(GeneratedObject)d)
.limit(maxRows)
.peek(monitor::rowEmitted);
.map(d-> (GeneratedObject)d);
}

private void visualiseTree(DecisionTree decisionTree, String title) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.scottlogic.datahelix.generator.core.generation;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.scottlogic.datahelix.generator.common.output.GeneratedObject;
import com.scottlogic.datahelix.generator.core.profile.Profile;

import java.util.stream.Stream;

public class LimitingDataGenerator implements DataGenerator {
private final DataGenerator dataGenerator;
private final long maxRows;

//created by DataGeneratorProvider
public LimitingDataGenerator(
DataGenerator dataGenerator,
long maxRows) {
this.dataGenerator = dataGenerator;
this.maxRows = maxRows;
}

@Override
public Stream<GeneratedObject> generateData(Profile profile) {
return dataGenerator.generateData(profile)
.limit(maxRows);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.scottlogic.datahelix.generator.core.generation;

import com.google.inject.Inject;
import com.scottlogic.datahelix.generator.common.output.GeneratedObject;
import com.scottlogic.datahelix.generator.core.profile.Profile;

import java.util.stream.Stream;

public class MonitoringDataGenerator implements DataGenerator {
private final DataGenerator dataGenerator;
private final DataGeneratorMonitor monitor;

//created by DataGeneratorProvider
public MonitoringDataGenerator(
DataGenerator dataGenerator,
DataGeneratorMonitor monitor) {
this.dataGenerator = dataGenerator;
this.monitor = monitor;
}

@Override
public Stream<GeneratedObject> generateData(Profile profile) {
monitor.generationStarting();

return dataGenerator.generateData(profile)
.peek(monitor::rowEmitted);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.scottlogic.datahelix.generator.core.guice;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.name.Named;
import com.scottlogic.datahelix.generator.core.generation.*;

public class DataGeneratorProvider implements Provider<DataGenerator> {
private final DataGenerator coreGenerator;
private final long maxRows;
private final DataGeneratorMonitor monitor;

@Inject
public DataGeneratorProvider(
DecisionTreeDataGenerator coreGenerator,
@Named("config:maxRows") long maxRows,
DataGeneratorMonitor monitor) {
this.coreGenerator = coreGenerator;
this.maxRows = maxRows;
this.monitor = monitor;
}

@Override
public DataGenerator get() {
return new MonitoringDataGenerator(
new LimitingDataGenerator(coreGenerator, maxRows),
monitor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected void configure() {

// Bind known implementations - no user input required
bind(DataGeneratorMonitor.class).to(AbstractDataGeneratorMonitor.class);
bind(DataGenerator.class).to(DecisionTreeDataGenerator.class);
bind(DataGenerator.class).toProvider(DataGeneratorProvider.class);

bind(JavaUtilRandomNumberGenerator.class)
.toInstance(new JavaUtilRandomNumberGenerator(OffsetDateTime.now().getNano()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ void setup() {
combinationStrategy = Mockito.mock(CombinationStrategy.class);
upfrontTreePruner = Mockito.mock(UpfrontTreePruner.class);
visualiserFactory = Mockito.mock(VisualiserFactory.class);
long maxRows = 10;
generator = new DecisionTreeDataGenerator(
factory,
treeWalker,
Expand All @@ -72,8 +71,7 @@ void setup() {
monitor,
combinationStrategy,
upfrontTreePruner,
visualiserFactory,
maxRows
visualiserFactory
);
}

Expand Down

0 comments on commit c77df08

Please sign in to comment.