Skip to content

Commit

Permalink
Refactor flux stream to collect results to map
Browse files Browse the repository at this point in the history
  • Loading branch information
Elscrux committed Jun 18, 2024
1 parent 861a0a6 commit 7777160
Showing 1 changed file with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;

/**
* This problem solver solves the {@link DeadFeatureConfiguration#FEATURE_MODEL_ANOMALY_DEAD}
Expand Down Expand Up @@ -80,20 +82,28 @@ private static Mono<Solution<String>> checkDeadFeatures(
}

return Flux.fromIterable(dimacsCnf.getVariables())
.filterWhen(feature -> checkFeatureDead(dimacsCnf, feature, subRoutineResolver))
.reduceWith(
StringBuilder::new,
(builder, feature) -> builder.append(feature.name()).append('\n')
)
.map(builder -> {
if (builder.isEmpty()) {
builder.append("No features are dead features!\n");
} else {
builder.insert(0, "The following features are dead features:\n");
.flatMap(x -> checkFeatureDead(dimacsCnf, x, subRoutineResolver)
.map(isVoid -> Tuples.of(x, isVoid)))
.collectMap(Tuple2::getT1, Tuple2::getT2)
.map(featureIsVoidMap -> {
var stringBuilder = new StringBuilder();

for (var entry : featureIsVoidMap.entrySet()) {
Variable feature = entry.getKey();
boolean isVoid = entry.getValue();

if (isVoid) {
stringBuilder.append(feature.name()).append('\n');
}
}

var solution = new Solution<String>();
solution.setSolutionData(builder.toString());
if (stringBuilder.isEmpty()) {
solution.setSolutionData("No features are dead features!\n");
} else {
solution.setSolutionData("The following features are dead features:\n" + stringBuilder);
}

solution.complete();
return solution;
});
Expand All @@ -103,7 +113,8 @@ private static Mono<Solution<String>> checkDeadFeatures(
* Checks if a given {@code feature} is dead in a given DIMACS {@code cnf} formula.
*
* @param subRoutineResolver used to evaluate a SAT formula for the check.
* @return whether the given {@code feature} is dead.
* @return the solution of the given {@code feature}.
* Use {@link DimacsCnfSolution#isVoid()} to check the feature.
*/
private static Mono<Boolean> checkFeatureDead(
DimacsCnf cnf,
Expand Down

0 comments on commit 7777160

Please sign in to comment.