Skip to content

Commit

Permalink
Merge pull request #669 from kusumotolab/introduce-parameter-object
Browse files Browse the repository at this point in the history
SourceCodeValidation#execにパラメータオブジェクトを導入
  • Loading branch information
mtj0928 authored Oct 13, 2019
2 parents f9db149 + 4a31ddd commit 384901a
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 41 deletions.
11 changes: 5 additions & 6 deletions src/main/java/jp/kusumotolab/kgenprog/Strategies.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import jp.kusumotolab.kgenprog.ga.selection.VariantSelection;
import jp.kusumotolab.kgenprog.ga.validation.Fitness;
import jp.kusumotolab.kgenprog.ga.validation.SourceCodeValidation;
import jp.kusumotolab.kgenprog.ga.validation.SourceCodeValidation.Input;
import jp.kusumotolab.kgenprog.ga.variant.Gene;
import jp.kusumotolab.kgenprog.ga.variant.Variant;
import jp.kusumotolab.kgenprog.ga.variant.VariantStore;
Expand Down Expand Up @@ -112,15 +113,13 @@ public Single<TestResults> execAsyncTestExecutor(final Single<Variant> variantSi

/**
* ソースコードの評価を行うメソッド.<br>
* 評価対象ソースコードと評価に利用するテスト実行結果を渡し,評価結果を返す.<br>
* ソースコードの評価に用いる情報を渡し,評価結果を返す.<br>
*
* @param sourceCode 評価対象ソースコード
* @param testResults 評価に利用するテスト実行結果
* @param input ソースコードの評価に用いる情報
* @return 評価結果
*/
public Fitness execSourceCodeValidation(final GeneratedSourceCode sourceCode,
final TestResults testResults) {
return sourceCodeValidation.exec(sourceCode, testResults);
public Fitness execSourceCodeValidation(final Input input) {
return sourceCodeValidation.exec(input);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package jp.kusumotolab.kgenprog.ga.validation;

import jp.kusumotolab.kgenprog.project.GeneratedSourceCode;
import jp.kusumotolab.kgenprog.project.test.TestResults;

/**
* テスト通過率をそのまま評価値とする評価方法
*/
public class DefaultCodeValidation implements SourceCodeValidation {

/**
* @param sourceCode 評価するソースコード
* @param testResults ソースコードのテストの結果
* @param input 評価値計算に利用する情報
* @return 評価値
*/
@Override
public Fitness exec(final GeneratedSourceCode sourceCode, final TestResults testResults) {
return new SimpleFitness(testResults.getSuccessRate());
public Fitness exec(final Input input) {
return new SimpleFitness(input.getTestResults()
.getSuccessRate());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,46 @@
*/
public interface SourceCodeValidation {

/**
* SourceCodeValidation#exec用のパラメータオブジェクト
*
*/
public class Input {

private final GeneratedSourceCode sourceCode;
private final TestResults testResults;

/**
*
* @param sourceCode 評価対象のソースコード
* @param testResults 評価対象のテスト結果
*/
public Input(final GeneratedSourceCode sourceCode, final TestResults testResults) {
this.sourceCode = sourceCode;
this.testResults = testResults;
}

/**
*
* @return 評価対象のソースコード
*/
public GeneratedSourceCode getSourceCode() {
return sourceCode;
}

/**
*
* @return 評価対象のテスト結果
*/
public TestResults getTestResults() {
return testResults;
}
}

/**
* @param sourceCode 評価するソースコード
* @param testResults ソースコードのテストの結果
* @return 評価値
*/
Fitness exec(GeneratedSourceCode sourceCode, TestResults testResults);
Fitness exec(Input input);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
import jp.kusumotolab.kgenprog.Strategies;
import jp.kusumotolab.kgenprog.fl.Suspiciousness;
import jp.kusumotolab.kgenprog.ga.validation.Fitness;
import jp.kusumotolab.kgenprog.ga.validation.SourceCodeValidation.Input;
import jp.kusumotolab.kgenprog.project.GeneratedSourceCode;
import jp.kusumotolab.kgenprog.project.test.EmptyTestResults;
import jp.kusumotolab.kgenprog.project.test.TestResults;

/**
*  kGenProg が生成する Variant を生成したり保持したりするクラス
* kGenProg が生成する Variant を生成したり保持したりするクラス
*/
public class VariantStore {

Expand Down Expand Up @@ -133,7 +134,7 @@ public List<Variant> getFoundSolutions(final int maxNumber) {
* @param variants 追加対象
* @see #addGeneratedVariants(Collection)
*/
// * @see addNextGenerationVariant(Variant)
// * @see addNextGenerationVariant(Variant)
public void addGeneratedVariants(final Variant... variants) {
addGeneratedVariants(Arrays.asList(variants));
}
Expand All @@ -149,7 +150,8 @@ public void addGeneratedVariants(final Collection<? extends Variant> variants) {
}

/**
* 引数を次世代のVariantとして追加する {@code variant.isCompleted() == true} の場合,foundSolutionとして追加され次世代のVariantには追加されない
* 引数を次世代のVariantとして追加する {@code variant.isCompleted() == true}
* の場合,foundSolutionとして追加され次世代のVariantには追加されない
*
* @param variant
*/
Expand Down Expand Up @@ -202,15 +204,14 @@ private Variant createVariant(final Gene gene, final GeneratedSourceCode sourceC
.cast(Variant.class)
.cache();

final Single<TestResults> resultsSingle = sourceCode.shouldBeTested()
? strategies.execAsyncTestExecutor(variantSingle)
.cache()
: Single.just(EmptyTestResults.instance);
final Single<TestResults> resultsSingle =
sourceCode.shouldBeTested() ? strategies.execAsyncTestExecutor(variantSingle)
.cache() : Single.just(EmptyTestResults.instance);
variant.setTestResultsSingle(resultsSingle);

final Single<Fitness> fitnessSingle = Single
.zip(variantSingle, resultsSingle,
(v, r) -> strategies.execSourceCodeValidation(sourceCode, r))
(v, r) -> strategies.execSourceCodeValidation(new Input(sourceCode, r)))
.cache();
variant.setFitnessSingle(fitnessSingle);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.nio.file.Paths;
import org.junit.Test;
import jp.kusumotolab.kgenprog.Configuration;
import jp.kusumotolab.kgenprog.ga.validation.SourceCodeValidation.Input;
import jp.kusumotolab.kgenprog.ga.variant.Variant;
import jp.kusumotolab.kgenprog.project.factory.TargetProject;
import jp.kusumotolab.kgenprog.project.factory.TargetProjectFactory;
Expand All @@ -20,7 +21,8 @@ public void testExec() {
final Variant initialVariant = TestUtil.createVariant(config);

final DefaultCodeValidation defaultCodeValidation = new DefaultCodeValidation();
final Fitness fitness = defaultCodeValidation.exec(null, initialVariant.getTestResults());
final Input input = new Input(null, initialVariant.getTestResults());
final Fitness fitness = defaultCodeValidation.exec(input);

final double expected = (double) 3 / 4; // 4 tests executed and 3 tests passed.
assertThat(fitness.getValue()).isEqualTo(expected);
Expand All @@ -34,7 +36,8 @@ public void testExecForBuildFailure() {
final Variant initialVariant = TestUtil.createVariant(config);

final DefaultCodeValidation defaultCodeValidation = new DefaultCodeValidation();
final Fitness fitness = defaultCodeValidation.exec(null, initialVariant.getTestResults());
final Input input = new Input(null, initialVariant.getTestResults());
final Fitness fitness = defaultCodeValidation.exec(input);

assertThat(fitness.getValue()).isNaN();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void testCreateVariant() {
when(strategies.execFaultLocalization(any(), any())).thenReturn(faultLocalizationResult);
when(strategies.execSourceCodeGeneration(any(), any())).thenReturn(sourceCodeGenerationResult);
when(strategies.execTestExecutor(any())).thenReturn(testExecutorResult);
when(strategies.execSourceCodeValidation(any(), any())).thenReturn(sourceCodeValidationResult);
when(strategies.execSourceCodeValidation(any())).thenReturn(sourceCodeValidationResult);
when(strategies.execASTConstruction(any())).thenReturn(astConstructionResult);
when(strategies.execVariantSelection(any(), any())).thenReturn(Collections.emptyList());
when(strategies.execAsyncTestExecutor(any())).thenReturn(Single.just(testExecutorResult));
Expand Down Expand Up @@ -196,10 +196,11 @@ private Strategies createMockStrategies(final Configuration config) {
final JDTASTConstruction jdtastConstruction = new JDTASTConstruction();
final Strategies strategies = mock(Strategies.class);

when(strategies.execASTConstruction(any())).then(v -> jdtastConstruction.constructAST(config.getTargetProject()));
when(strategies.execASTConstruction(any()))
.then(v -> jdtastConstruction.constructAST(config.getTargetProject()));
when(strategies.execVariantSelection(any(), any())).then(v -> v.getArgument(1));
when(strategies.execTestExecutor(any())).then(v -> EmptyTestResults.instance);
when(strategies.execSourceCodeValidation(any(), any())).then(v -> new SimpleFitness(1.0d));
when(strategies.execSourceCodeValidation(any())).then(v -> new SimpleFitness(1.0d));
when(strategies.execFaultLocalization(any(), any())).then(v -> Collections.emptyList());
when(strategies.execAsyncTestExecutor(any())).then(v -> {
final Single<Variant> variantSingle = v.getArgument(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
public class VariantStoreSerializerTest {

private Gson createGson(final Configuration config) {
return new GsonBuilder().registerTypeAdapter(VariantStore.class,
new VariantStoreSerializer(config))
return new GsonBuilder()
.registerTypeAdapter(VariantStore.class, new VariantStoreSerializer(config))
.registerTypeAdapter(Variant.class, new VariantSerializer())
.registerTypeHierarchyAdapter(TestResults.class, new TestResultsSerializer())
.registerTypeAdapter(TestResult.class, new TestResultSerializer())
Expand All @@ -64,8 +64,7 @@ private Gson createGson(final Configuration config) {
public void testVariantStoreSerializer() {
final Path rootPath = Paths.get("example/BuildSuccess01");
final TargetProject project = TargetProjectFactory.create(rootPath);
final Configuration config = new Configuration.Builder(project)
.build();
final Configuration config = new Configuration.Builder(project).build();
// gsonのセットアップ
final Gson gson = createGson(config);

Expand All @@ -81,7 +80,7 @@ public void testVariantStoreSerializer() {
when(strategies.execFaultLocalization(any(), any())).thenReturn(faultLocalizationResult);
when(strategies.execSourceCodeGeneration(any(), any())).thenReturn(sourceCodeGenerationResult);
when(strategies.execTestExecutor(any())).thenReturn(testExecutorResult);
when(strategies.execSourceCodeValidation(any(), any())).thenReturn(sourceCodeValidationResult);
when(strategies.execSourceCodeValidation(any())).thenReturn(sourceCodeValidationResult);
when(strategies.execASTConstruction(any())).thenReturn(astConstructionResult);
when(strategies.execVariantSelection(any(), any())).thenReturn(Collections.emptyList());
when(strategies.execAsyncTestExecutor(any())).then(v -> {
Expand Down Expand Up @@ -110,28 +109,25 @@ public void testVariantStoreSerializer() {
final JsonObject serializedVariantStore = gson.toJsonTree(variantStore)
.getAsJsonObject();
// キーのチェック
assertThat(serializedVariantStore.keySet()).containsOnly(
JsonKeyAlias.VariantStore.PROJECT_NAME,
JsonKeyAlias.VariantStore.VARIANTS,
JsonKeyAlias.VariantStore.CONFIGURATION);
assertThat(serializedVariantStore.keySet()).containsOnly(JsonKeyAlias.VariantStore.PROJECT_NAME,
JsonKeyAlias.VariantStore.VARIANTS, JsonKeyAlias.VariantStore.CONFIGURATION);

// 値のチェック
final String projectName = serializedVariantStore.get(JsonKeyAlias.VariantStore.PROJECT_NAME)
.getAsString();
assertThat(projectName).isEqualTo("BuildSuccess01");

final JsonArray serializedVariants = serializedVariantStore.get(
JsonKeyAlias.VariantStore.VARIANTS)
.getAsJsonArray();
final JsonArray serializedVariants =
serializedVariantStore.get(JsonKeyAlias.VariantStore.VARIANTS)
.getAsJsonArray();
assertThat(serializedVariants).hasSize(11);
}

@Test
public void testConfigurationSerialization() {
final Path rootPath = Paths.get("example/BuildSuccess01");
final TargetProject project = TargetProjectFactory.create(rootPath);
final Configuration config = new Configuration.Builder(project)
.build();
final Configuration config = new Configuration.Builder(project).build();
// gsonのセットアップ
final Gson gson = createGson(config);

Expand Down

0 comments on commit 384901a

Please sign in to comment.