Skip to content

Commit

Permalink
refactor: using java-decita v0.7.0
Browse files Browse the repository at this point in the history
Refs: #23
  • Loading branch information
nergal-perm committed Jun 14, 2024
1 parent b6e3ed4 commit b7c4ee0
Show file tree
Hide file tree
Showing 19 changed files with 197 additions and 324 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
<dependency>
<groupId>io.github.nergal-perm</groupId>
<artifactId>java-decita</artifactId>
<version>0.5.1</version>
<version>0.7.0-RC</version>
</dependency>
<dependency>
<groupId>com.renomad</groupId>
Expand Down
97 changes: 8 additions & 89 deletions src/main/java/ru/ewc/checklogic/Computation.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,7 @@
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import ru.ewc.commands.CommandsFacade;
import ru.ewc.decisions.api.ComputationContext;
import ru.ewc.decisions.api.DecitaException;
import ru.ewc.decisions.api.DecitaFacade;
import ru.ewc.decisions.api.Locator;
import ru.ewc.decisions.api.Locators;

/**
* I am a unique instance of a decision table computation.
Expand All @@ -44,35 +37,12 @@
@SuppressWarnings("PMD.ProhibitPublicStaticMethods")
public final class Computation {
/**
* Facade for making all the decisions.
* The context of the computation.
*/
private final DecitaFacade decisions;
private final ComputationContext context;

/**
* Facade for executing all the commands.
*/
private final CommandsFacade commands;

/**
* The current state of the system.
*/
private final Locators state;

/**
* Ctor.
*
* @param decisions An instance of {@link DecitaFacade} to make decisions.
* @param commands An instance of {@link CommandsFacade} to execute commands.
* @param locators An instance of {@link Locators} to store the state.
*/
public Computation(
final DecitaFacade decisions,
final CommandsFacade commands,
final Locators locators
) {
this.decisions = decisions;
this.commands = commands;
this.state = locators;
public Computation(final ComputationContext context) {
this.context = context;
}

/**
Expand All @@ -91,70 +61,19 @@ public static URI uriFrom(final String path) {
return URI.create(result.toString());
}

/**
* Computes the decision for a specified table.
*
* @param table Name of the tables to make a decision against.
* @param locators The locators to use for the decision.
* @return The collection of outcomes from the specified table.
* @throws DecitaException If the table could not be found or computed.
*/
public Map<String, String> decideFor(final String table, final Locators locators)
throws DecitaException {
return this.decisions.decisionFor(table, this.state.mergedWith(locators));
}

public Map<String, String> decideFor(final String table) throws DecitaException {
return this.decisions.decisionFor(table, this.state);
}

public void perform(final Transition command) {
this.commands.perform(command.name(), this.state.mergedWith(command.request()));
}

public boolean hasStateFor(final String table) {
return this.state.hasLocator(table);
public void perform(final String command) {
this.context.perform(command);
}

public Map<String, String> stateFor(final String table, final Map<String, String> entities) {
final Locator locator = this.state.locatorFor(table);
final ComputationContext context = new ComputationContext(this.state);
final Map<String, String> actual = HashMap.newHashMap(entities.size());
for (final String fragment : entities.keySet()) {
actual.put(fragment, locator.fragmentBy(fragment, context));
actual.put(fragment, this.context.valueFor(table, fragment));
}
return actual;
}

public Computation withState(final Map<String, Map<String, Object>> incoming) {
return new Computation(this.decisions, this.commands, stateFrom(incoming));
}

public Map<String, Map<String, Object>> storedState() {
return this.state.state();
}

/**
* Loads the state from the specified {@code InputStream}.
*
* @param stream InputStream containing state info.
* @return Collection of {@link Locator} objects, containing desired state.
*/
private static Locators stateFrom(final Map<String, Map<String, Object>> stream) {
return new Locators(
stream
.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getKey, entryToLocator()))
);
}

/**
* Converts a {@link Map.Entry} to a {@link Locator} object.
*
* @return A function that converts a {@link Map.Entry} to a {@link Locator} object.
*/
private static Function<Map.Entry<String, Map<String, Object>>, Locator> entryToLocator() {
return e -> new InMemoryStorage(e.getValue());
return this.context.state();
}
}
127 changes: 127 additions & 0 deletions src/main/java/ru/ewc/checklogic/FileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* MIT License
*
* Copyright (c) 2024 Decision-Driven Development
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package ru.ewc.checklogic;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
import lombok.SneakyThrows;
import org.yaml.snakeyaml.Yaml;

/**
* I am a utility class that provides methods for file operations.
*
* @since 0.3.0
*/
public final class FileUtils {
/**
* Primary hidden constructor.
*/
private FileUtils() {
// Utility class
}

static InputStream applicationConfig(final String root) throws IOException {
return Files.newInputStream(Path.of(root, "application.yaml"));
}

/**
* Gets the path to a folder with specified resources.
*
* @param resource Resource name to get its folder.
* @return Path to a resource folder as a String.
*/
static String getFinalPathTo(final String resource) {
final String states;
if (System.getProperties().containsKey("sources")) {
states = String.format("%s/%s", System.getProperty("sources"), resource);
} else if (System.getProperties().containsKey(resource)) {
states = System.getProperty(resource);
} else {
states = String.format(
"%s%s%s",
System.getProperty("user.dir"),
"/src/test/resources/",
resource
);
}
return states;
}

@SneakyThrows
static Stream<LogicChecker.TestData> readFileNames() {
return Files.walk(Paths.get(Computation.uriFrom(getFinalPathTo("states"))))
.filter(Files::isRegularFile)
.map(
path -> path.toFile().getAbsolutePath()
).map(
path -> {
final InputStream stream;
try {
stream = Files.newInputStream(Paths.get(path));
} catch (final IOException exception) {
throw new IllegalStateException(exception);
}
return FileUtils.createTestData(path, stream);
});
}

@SuppressWarnings("unchecked")
private static LogicChecker.TestData createTestData(
final String path,
final InputStream stream
) {
final Iterator<Object> iterator = new Yaml().loadAll(stream).iterator();
iterator.next();
final String command;
if (iterator.hasNext()) {
command = FileUtils.extractCommand(iterator.next());
} else {
command = "";
}
Map<String, Map<String, String>> expectations = new HashMap<>();
if (iterator.hasNext()) {
expectations = (Map<String, Map<String, String>>) iterator.next();
}
return new LogicChecker.TestData(path, command, expectations);
}

@SuppressWarnings("unchecked")
private static String extractCommand(final Object next) {
final Map<String, String> map = (Map<String, String>) next;
final String command;
if (map == null) {
command = "";
} else {
command = map.getOrDefault("command", "");
}
return command;
}
}
Loading

1 comment on commit b7c4ee0

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on b7c4ee0 Jun 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 23-18966f95 disappeared from src/main/java/ru/ewc/checklogic/Computation.java), that's why I closed #24. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.