Skip to content

Commit

Permalink
Fixes some Sonar complaints
Browse files Browse the repository at this point in the history
  • Loading branch information
fathzer committed Nov 26, 2024
1 parent 1c410e8 commit a606e6d
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 33 deletions.
27 changes: 26 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<version>1.0.8</version>
</parent>
<artifactId>jchess-uci</artifactId>
<version>2.0.3-SNAPSHOT</version>
<version>2.0.4-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -49,6 +49,31 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
</plugin>
<!--
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.11.1</version>
<configuration>
<source>${maven.compiler.release}</source>
<docencoding>UTF-8</docencoding>
<overview>${basedir}/overview.html</overview>
<header>${project.version}</header>
<bottom>${project.version}</bottom>
<links>
<link>https://docs.oracle.com/javase/17/docs/api/</link>
</links>
</configuration>
<executions>
<execution>
<id>javadoc_generation</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>-->
</plugins>
</build>
</project>
3 changes: 2 additions & 1 deletion src/main/java/com/fathzer/jchess/uci/StoppableTask.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.fathzer.jchess.uci;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

/** A task that can be stopped.
* <br>Please note that stoppable is different from {@link java.util.concurrent.Cancellable}
* <br>Please note that stoppable is a concept different from {@link Future#cancel(boolean)}
* When a task is cancelled, it produces no result (for example, a {@link java.util.concurrent.CancellationException} if the task was cancelled).
* The typical use case of a StoppableTask is a best move search engine that performs iterative deepening. You may want to stop its deepening and get the current result.
* @param <T> The result of the task
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/com/fathzer/jchess/uci/UCI.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class UCI implements Runnable, AutoCloseable {
@FunctionalInterface
/** A runnable that can throw an exception.
*/
public interface ThrowingRunnable {
public static interface ThrowingRunnable {
void run() throws Exception;
}

Expand Down Expand Up @@ -214,24 +214,26 @@ protected void doGo(Deque<String> tokens) {
final Optional<GoParameters> goOptions = parse(GoParameters::new, GoParameters.PARSER, tokens);
if (goOptions.isPresent()) {
final StoppableTask<GoReply> task = engine.go(goOptions.get());
final boolean started = doBackground(() -> {
final GoReply goReply = task.call();
final Optional<String> mainInfo = goReply.getMainInfoString();
if (mainInfo.isPresent()) {
this.out(mainInfo.get());
for (int i = 1; i <= goReply.getInfo().get().getExtraMoves().size(); i++) {
this.out(goReply.getInfoString(i).get());
}
}
out(goReply.toString());
}, task::stop, e -> err(GO_CMD, e));
final boolean started = doBackground(() -> processGo(task), task::stop, e -> err(GO_CMD, e));
if (!started) {
debug("Engine is already working");
}
}
}
}

private void processGo(final StoppableTask<GoReply> task) throws Exception {
final GoReply goReply = task.call();
final Optional<String> mainInfo = goReply.getMainInfoString();
if (mainInfo.isPresent()) {
this.out(mainInfo.get());
for (int i = 1; i <= goReply.getInfo().get().getExtraMoves().size(); i++) {
this.out(goReply.getInfoString(i).get());
}
}
out(goReply.toString());
}

protected <T> Optional<T> parse(Supplier<T> builder, Parser<T> parser, Deque<String> tokens) {
try {
final T result = builder.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ default String getBoardAsString() {
return getFEN();
}

/** Returns the <a href="https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation">FEN</a> representation of the board.
/** Gets the <a href="https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation">FEN</a> representation of the board.
* <br>Calling this method when no position is defined may lead to unpredictable results.
* @return a string representing the chess board or null if no position is defined.
*/
Expand Down
23 changes: 9 additions & 14 deletions src/main/java/com/fathzer/jchess/uci/extended/SpeedTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fathzer.jchess.uci.extended;

import java.util.List;
import java.util.function.Consumer;

import com.fathzer.games.MoveGenerator;
import com.fathzer.games.ai.evaluation.EvaluatedMove;
Expand All @@ -17,15 +18,7 @@
*/
public class SpeedTest<M, B extends MoveGenerator<M>> {

private static class Result<M> {
private String fen;
private List<EvaluatedMove<M>> moves;

public Result(String fen, List<EvaluatedMove<M>> moves) {
super();
this.fen = fen;
this.moves = moves;
}
private static record Result<M> (String fen, List<EvaluatedMove<M>> moves, Consumer<CharSequence> out) {

private void assertEquals(Object expected, Object actual) {
if (!expected.equals(actual)) {
Expand All @@ -42,24 +35,26 @@ private void assertTrue(boolean value) {
}

private void show() {
System.out.println(fen);
System.out.println(moves);
out.accept(fen);
out.accept(moves.toString());
}
}

private AbstractEngine<M, B> uciEngine;
private final AbstractEngine<M, B> uciEngine;
private final Consumer<CharSequence> out;

/** Creates the test.
* @param engine The engine to test
*/
public SpeedTest(AbstractEngine<M, B> engine) {
public SpeedTest(AbstractEngine<M, B> engine, Consumer<CharSequence> out) {
this.uciEngine = engine;
this.out = out;
}

private Result<M> fill(String fen) {
uciEngine.newGame();
uciEngine.setStartPosition(fen);
return new Result<>(fen, uciEngine.getEngine().getBestMoves(uciEngine.get()).getBestMoves());
return new Result<>(fen, uciEngine.getEngine().getBestMoves(uciEngine.get()).getBestMoves(), out);
}

/** Launches the test.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public GoReply call() {
final List<UCIMove> list = tt.collectPV(board, toMove(m), info.getDepth()).stream().map(x -> toUCI(x)).toList();
return list.isEmpty() ? Optional.empty() : Optional.of(list);
});
info.setExtraMoves(bestMoves.stream().filter(em -> !move.getContent().equals(em.getContent())).limit(engine.getDeepeningPolicy().getSize()-1).map(em->toUCI(em.getContent())).toList());
info.setExtraMoves(bestMoves.stream().filter(em -> !move.getContent().equals(em.getContent())).limit(engine.getDeepeningPolicy().getSize()-1L).map(em->toUCI(em.getContent())).toList());
goReply.setInfo(info);
return goReply;
} finally {
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/fathzer/jchess/uci/UCITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ public GoReply call() {

@Override
public void stop() {
// call immediately throws an exception, there's no way to stop it
}
});
uci.post("go", 10);
await().atMost(200, TimeUnit.MILLISECONDS).until(() -> uci.getExceptions().getOrDefault("go", new IllegalArgumentException()).getClass()==UnsupportedOperationException.class);
await().atMost(500, TimeUnit.MILLISECONDS).until(() -> uci.getExceptions().getOrDefault("go", new IllegalArgumentException()).getClass()==UnsupportedOperationException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public String getId() {

@Override
public void setStartPosition(String fen) {
// This is a fake engine, it ignores the position
}

@Override
Expand All @@ -72,9 +73,8 @@ protected String toMove(UCIMove move) {
}
};

GoParameters params = new GoParameters();
// Check settings changes engine config
params = new GoParameters();
GoParameters params = new GoParameters();
GoParameters.PARSER.parse(params, new LinkedList<>(Arrays.asList(("movetime 1000").split(" "))));
StoppableTask<GoReply> task = ae.go(params);
assertThrows(RuntimeException.class, () -> task.call());
Expand Down

0 comments on commit a606e6d

Please sign in to comment.