Skip to content
Permalink
Browse files

Extracted guard to decorator

  • Loading branch information...
No3x committed Jul 3, 2019
1 parent a063a21 commit eed874945993631981fbf1f568e04fca0371f33e
@@ -9,8 +9,10 @@
import com.google.common.eventbus.EventBus;
import de.kintel.ki.algorithm.KI;
import de.kintel.ki.algorithm.MoveClassifier;
import de.kintel.ki.algorithm.MoveMaker;
import de.kintel.ki.algorithm.Weighting;
import de.kintel.ki.algorithm.MoveMaker;
import de.kintel.ki.algorithm.MoveMakerGuardDecorator;
import de.kintel.ki.algorithm.MoveMakerImpl;
import de.kintel.ki.cli.TablePrinter;
import de.kintel.ki.model.Board;
import org.springframework.beans.factory.annotation.Autowired;
@@ -39,6 +41,12 @@ public Board board(@Value("${board.height}") int boardHeight, @Value("${board.wi
return new Board(boardHeight, boardWidth);
}

@Bean
@Scope("singleton")
public MoveMaker moveMaker(@Value("${guardEnabled}") final boolean guardEnabled, final MoveMakerGuardDecorator moveMakerGuardDecorator, final MoveMakerImpl moveMaker) {
return guardEnabled ? moveMakerGuardDecorator : moveMaker;
}

@Bean
@Scope("singleton")
public TablePrinter tablePrinter() {
@@ -30,9 +30,7 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
@@ -1,6 +1,6 @@
/*
* hsb-kintel-17
* Copyright (C) 2018 hsb-kintel-17
* Copyright (C) 2019 hsb-kintel-17
* This file is covered by the LICENSE file in the root of this project.
*/

@@ -0,0 +1,58 @@
/*
* hsb-kintel-17
* Copyright (C) 2019 hsb-kintel-17
* This file is covered by the LICENSE file in the root of this project.
*/

package de.kintel.ki.algorithm;

import de.kintel.ki.model.Board;
import de.kintel.ki.model.Move;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;
import java.util.HashMap;

/**
* The guard is intended to be used while development to check board is the same before doing and undoing a move
*/
@Component
@Scope("singleton")
public class MoveMakerGuardDecorator implements MoveMaker {

private final Logger logger = LoggerFactory.getLogger(MoveMakerImpl.class);
/**
* Debugging mechanism that stores board contents before the move for comparision after undo move. If the states differ then the undo was incorrect.
*/
private final HashMap<Move, String> guard = new HashMap<>();

@Qualifier("moveMakerBasic")
private final MoveMaker moveMaker;

@Autowired
public MoveMakerGuardDecorator(@Qualifier("moveMakerBasic") MoveMaker moveMakerBasic) {
this.moveMaker = moveMakerBasic;
logger.info("Guard is enabled");
}

@Override
public void makeMove(@Nonnull Move move, Board board) {
guard.put(move, board.toString());
moveMaker.makeMove(move, board);
}

@Override
public void undoMove(@Nonnull Move move, Board board) {
moveMaker.undoMove(move, board);
final String expected = guard.get(move);
final String actual = board.toString();
if (!expected.equals(actual)) {
throw new IllegalStateException(String.format("Incorrect undo of move %s! The field after the undo is not the same as before the do - but it should of course. move: %s", move.getUuid(), board.toString()));
}
}
}
@@ -1,6 +1,6 @@
/*
* hsb-kintel-17
* Copyright (C) 2018 hsb-kintel-17
* Copyright (C) 2019 hsb-kintel-17
* This file is covered by the LICENSE file in the root of this project.
*/

@@ -10,32 +10,25 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Optional;

@Component
@Qualifier("moveMakerBasic")
@Scope("prototype")
public class MoveMakerImpl implements MoveMaker {

private final Logger logger = LoggerFactory.getLogger(MoveMakerImpl.class);

/**
* Debugging mechanism that stores board contents before the move for comparision after undo move. If the states differ then the undo was incorrect.
*/
private final HashMap<Move, String> guard = new HashMap<>();
private final RankMakerImpl rankMaker;
/** guard is indented to be used while development to check board is the same before doing and un-doing a move */
@Value("${guardEnabled}")
private boolean guardEnabled = false;
private final RankMaker rankMaker;

@Autowired
public MoveMakerImpl(RankMakerImpl rankMaker) {
public MoveMakerImpl(RankMaker rankMaker) {
this.rankMaker = rankMaker;
}

@@ -46,9 +39,6 @@ public MoveMakerImpl(RankMakerImpl rankMaker) {
*/
@Override
public void makeMove(@Nonnull final Move move, Board board) {
if( guardEnabled ) {
guard.put(move, board.toString());
}
doMove(move, board, false);
}

@@ -60,14 +50,6 @@ public void makeMove(@Nonnull final Move move, Board board) {
@Override
public void undoMove(@Nonnull final Move move, Board board) {
doMove(move, board, true);
if( guardEnabled ) {
final String expected = guard.get(move);
final String actual = board.toString();
if (!expected.equals(actual)) {
String message = "Incorrect undo of move " + move.getUuid() + "! The field after the undo is not the same as before the do - but it should of course. move: " + board.toString();
throw new IllegalStateException(message);
}
}
}

/**
@@ -1,6 +1,6 @@
/*
* hsb-kintel-17
* Copyright (C) 2018 hsb-kintel-17
* Copyright (C) 2019 hsb-kintel-17
* This file is covered by the LICENSE file in the root of this project.
*/

@@ -16,7 +16,6 @@
import java.util.Optional;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

0 comments on commit eed8749

Please sign in to comment.
You can’t perform that action at this time.