This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into 1162-copyright-doc
- Loading branch information
Showing
21 changed files
with
455 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
The Decision Based Tree solver generates row specs by: | ||
1. choosing and removing a decision from the tree | ||
2. selecting an option from that decision | ||
3. adding the constraints from the chosen option to the root of the tree | ||
- adding the sub decisions from the chosen option to the root of the tree | ||
4. "pruning" the tree by removing any options from the tree that contradict with the new root node | ||
- any decisions that only have 1 remaining option will have that option also moved up the tree, and pruned again. | ||
5. restarting from 1, until there are no decision left | ||
6. creating a rowspec from the constraints in the remaining root node. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,5 +18,6 @@ | |
|
||
public enum TreeWalkerType { | ||
CARTESIAN_PRODUCT, | ||
REDUCTIVE | ||
REDUCTIVE, | ||
DECISION_BASED | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
generator/src/main/java/com/scottlogic/deg/generator/guice/OptionPickerProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.scottlogic.deg.generator.guice; | ||
|
||
import com.google.inject.Inject; | ||
import com.google.inject.Provider; | ||
import com.scottlogic.deg.generator.config.detail.DataGenerationType; | ||
import com.scottlogic.deg.generator.generation.GenerationConfigSource; | ||
import com.scottlogic.deg.generator.walker.decisionbased.OptionPicker; | ||
import com.scottlogic.deg.generator.walker.decisionbased.RandomOptionPicker; | ||
import com.scottlogic.deg.generator.walker.decisionbased.SequentialOptionPicker; | ||
|
||
public class OptionPickerProvider implements Provider<OptionPicker> { | ||
|
||
private final GenerationConfigSource config; | ||
private final RandomOptionPicker randomOptionPicker; | ||
private final SequentialOptionPicker sequentialOptionPicker; | ||
|
||
@Inject | ||
public OptionPickerProvider(GenerationConfigSource config, RandomOptionPicker randomOptionPicker, SequentialOptionPicker sequentialOptionPicker){ | ||
this.config = config; | ||
this.randomOptionPicker = randomOptionPicker; | ||
this.sequentialOptionPicker = sequentialOptionPicker; | ||
} | ||
|
||
@Override | ||
public OptionPicker get() { | ||
if (config.getGenerationType() == DataGenerationType.RANDOM){ | ||
return randomOptionPicker; | ||
} | ||
|
||
return sequentialOptionPicker; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
generator/src/main/java/com/scottlogic/deg/generator/guice/RowSpecTreeSolverProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.scottlogic.deg.generator.guice; | ||
|
||
import com.google.inject.Inject; | ||
import com.google.inject.Provider; | ||
import com.scottlogic.deg.common.ValidationException; | ||
import com.scottlogic.deg.generator.generation.GenerationConfigSource; | ||
import com.scottlogic.deg.generator.walker.decisionbased.DecisionBasedSolver; | ||
import com.scottlogic.deg.generator.walker.rowspec.CartesianProductRowSpecTreeSolver; | ||
import com.scottlogic.deg.generator.walker.rowspec.RowSpecTreeSolver; | ||
|
||
public class RowSpecTreeSolverProvider implements Provider<RowSpecTreeSolver> { | ||
|
||
private final GenerationConfigSource config; | ||
private final CartesianProductRowSpecTreeSolver cartesianProductRowSpecTreeSolver; | ||
private final DecisionBasedSolver decisionBasedSolver; | ||
|
||
@Inject | ||
public RowSpecTreeSolverProvider(GenerationConfigSource config, CartesianProductRowSpecTreeSolver cartesianProductRowSpecTreeSolver, DecisionBasedSolver decisionBasedSolver) { | ||
this.config = config; | ||
this.cartesianProductRowSpecTreeSolver = cartesianProductRowSpecTreeSolver; | ||
this.decisionBasedSolver = decisionBasedSolver; | ||
} | ||
|
||
@Override | ||
public RowSpecTreeSolver get() { | ||
switch (config.getWalkerType()) { | ||
case REDUCTIVE: | ||
case DECISION_BASED: | ||
return decisionBasedSolver; | ||
case CARTESIAN_PRODUCT: | ||
return cartesianProductRowSpecTreeSolver; | ||
default: | ||
throw new ValidationException("no WalkerType selected"); | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
.../src/main/java/com/scottlogic/deg/generator/walker/decisionbased/DecisionBasedSolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.scottlogic.deg.generator.walker.decisionbased; | ||
|
||
import com.google.inject.Inject; | ||
import com.scottlogic.deg.common.profile.Field; | ||
import com.scottlogic.deg.common.profile.ProfileFields; | ||
import com.scottlogic.deg.common.profile.constraints.atomic.AtomicConstraint; | ||
import com.scottlogic.deg.common.util.FlatMappingSpliterator; | ||
import com.scottlogic.deg.generator.decisiontree.ConstraintNode; | ||
import com.scottlogic.deg.generator.decisiontree.DecisionNode; | ||
import com.scottlogic.deg.generator.decisiontree.DecisionTree; | ||
import com.scottlogic.deg.generator.fieldspecs.FieldSpec; | ||
import com.scottlogic.deg.generator.fieldspecs.RowSpec; | ||
import com.scottlogic.deg.generator.reducer.ConstraintReducer; | ||
import com.scottlogic.deg.generator.walker.reductive.Merged; | ||
import com.scottlogic.deg.generator.walker.reductive.ReductiveTreePruner; | ||
import com.scottlogic.deg.generator.walker.rowspec.RowSpecTreeSolver; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class DecisionBasedSolver implements RowSpecTreeSolver { | ||
|
||
private final ConstraintReducer constraintReducer; | ||
private final ReductiveTreePruner reductiveTreePruner; | ||
private final OptionPicker optionPicker; | ||
|
||
@Inject | ||
public DecisionBasedSolver(ConstraintReducer constraintReducer, ReductiveTreePruner reductiveTreePruner, OptionPicker optionPicker) { | ||
this.constraintReducer = constraintReducer; | ||
this.reductiveTreePruner = reductiveTreePruner; | ||
this.optionPicker = optionPicker; | ||
} | ||
|
||
@Override | ||
public Stream<RowSpec> createRowSpecs(DecisionTree tree) { | ||
return reduceToRowNodes(tree.rootNode) | ||
.map(rootNode -> toRowspec(tree.fields, rootNode)); | ||
} | ||
|
||
private RowSpec toRowspec(ProfileFields fields, ConstraintNode rootNode){ | ||
return constraintReducer | ||
.reduceConstraintsToRowSpec(fields, rootNode.getAtomicConstraints()).get(); | ||
} | ||
|
||
private Stream<ConstraintNode> reduceToRowNodes(ConstraintNode rootNode){ | ||
if (rootNode.getDecisions().isEmpty()){ | ||
return Stream.of(rootNode); | ||
} | ||
|
||
DecisionNode decisionNode = optionPicker.pickDecision(rootNode); | ||
ConstraintNode rootWithoutDecision = rootNode.removeDecisions(Collections.singleton(decisionNode)); | ||
|
||
Stream<ConstraintNode> rootOnlyConstraintNodes = optionPicker.streamOptions(decisionNode) | ||
.map(option -> combineWithRootNode(rootWithoutDecision, option)) | ||
.filter(newNode -> !newNode.isContradictory()) | ||
.map(Merged::get); | ||
|
||
return FlatMappingSpliterator.flatMap( | ||
rootOnlyConstraintNodes, | ||
this::reduceToRowNodes); | ||
} | ||
|
||
private Merged<ConstraintNode> combineWithRootNode(ConstraintNode rootNode, ConstraintNode option) { | ||
ConstraintNode constraintNode = rootNode | ||
.addDecisions(option.getDecisions()) | ||
.addAtomicConstraints(option.getAtomicConstraints()); | ||
|
||
return reductiveTreePruner.pruneConstraintNode(constraintNode, getFields(option)); | ||
} | ||
|
||
private Map<Field, FieldSpec> getFields(ConstraintNode option) { | ||
return option.getAtomicConstraints().stream() | ||
.map(AtomicConstraint::getField) | ||
.distinct() | ||
.collect(Collectors.toMap( | ||
Function.identity(), | ||
field-> FieldSpec.Empty)); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
generator/src/main/java/com/scottlogic/deg/generator/walker/decisionbased/OptionPicker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.scottlogic.deg.generator.walker.decisionbased; | ||
|
||
import com.scottlogic.deg.generator.decisiontree.ConstraintNode; | ||
import com.scottlogic.deg.generator.decisiontree.DecisionNode; | ||
|
||
import java.util.stream.Stream; | ||
|
||
public interface OptionPicker { | ||
DecisionNode pickDecision (ConstraintNode constraintNode); | ||
Stream<ConstraintNode> streamOptions(DecisionNode decisionNode); | ||
} |
33 changes: 33 additions & 0 deletions
33
...r/src/main/java/com/scottlogic/deg/generator/walker/decisionbased/RandomOptionPicker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.scottlogic.deg.generator.walker.decisionbased; | ||
|
||
import com.google.inject.Inject; | ||
import com.scottlogic.deg.generator.decisiontree.ConstraintNode; | ||
import com.scottlogic.deg.generator.decisiontree.DecisionNode; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.stream.Stream; | ||
|
||
public class RandomOptionPicker implements OptionPicker { | ||
private final Random random; | ||
|
||
public RandomOptionPicker() { | ||
this.random = new Random(); | ||
} | ||
|
||
@Override | ||
public DecisionNode pickDecision(ConstraintNode constraintNode) { | ||
return constraintNode.getDecisions().stream() | ||
.skip(random.nextInt(constraintNode.getDecisions().size())) | ||
.findFirst().get(); | ||
} | ||
|
||
@Override | ||
public Stream<ConstraintNode> streamOptions(DecisionNode decisionNode) { | ||
List<ConstraintNode> options = new ArrayList<>(decisionNode.getOptions()); | ||
Collections.shuffle(options); | ||
return options.stream(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...c/main/java/com/scottlogic/deg/generator/walker/decisionbased/SequentialOptionPicker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.scottlogic.deg.generator.walker.decisionbased; | ||
|
||
import com.scottlogic.deg.generator.decisiontree.ConstraintNode; | ||
import com.scottlogic.deg.generator.decisiontree.DecisionNode; | ||
|
||
import java.util.stream.Stream; | ||
|
||
public class SequentialOptionPicker implements OptionPicker { | ||
@Override | ||
public DecisionNode pickDecision(ConstraintNode constraintNode) { | ||
return constraintNode.getDecisions().iterator().next(); | ||
} | ||
|
||
@Override | ||
public Stream<ConstraintNode> streamOptions(DecisionNode decisionNode) { | ||
return decisionNode.getOptions().stream(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.