tools.utils.InputInterpreter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of brunner-core-tools Show documentation
Show all versions of brunner-core-tools Show documentation
Tools module for the BRunner project
The newest version!
package tools.utils;
import java.util.Optional;
import api.definition.ITask;
import api.definition.config.inputdata.IKBScenario;
import constants.BRunnerKeywords;
import fr.boreal.component_builder.InputDataScenario;
import fr.boreal.component_builder.api.IInputDataScenario;
import utils.EnumerationUtils;
import utils.ExperimentTimeoutConditions;
/**
*
* Can convert BRunner inputs to the format desired by a specific reasoner.
*
*/
public class InputInterpreter {
/**
*
* Translation of BRunner inputs. Default choice is to make InteGraal objects.
*
* @param task
* @return the input kb scenario converted into the corresponding InteGraal
* object
*/
public static IInputDataScenario translateInputDataScenarioFor(ITask task) {
IInputDataScenario scenario = new InputDataScenario("Scenario Created via BRunner ");
var input = (IKBScenario) task.inputData();
var properties = input.getProperties();
for (String propertyName : properties) {
String value = input.getValues(propertyName).stream().findAny().get();
String trimmed = propertyName.replaceFirst(BRunnerKeywords.OuterLevel.INPUTDATA.kw + ".", "");
var e = EnumerationUtils.getEnumFromString(BRunnerKeywords.InnerLevel.class, trimmed);
switch (e) {
case DATA -> {
scenario.setFactbasePath(value);
}
case RULES -> {
scenario.setRulebasePath(value);
}
case WORKLOAD -> {
scenario.setQuerybasePath(value);
}
default -> {
}
}
;
}
return scenario;
}
public static ExperimentTimeoutConditions getExperimentTimeoutConditionsFor(ITask task) {
return new ExperimentTimeoutConditions(getTaskTimeout(task), getTaskRank(task));
}
/**
* returns the timeout set for this task
*
* @param task
* @return
*/
private static Integer getTaskTimeout(ITask task) {
return getFromTaskParametersOrNull(task, BRunnerKeywords.InnerLevel.ALGORITHM_TIMEOUT.full);
}
/**
* returns the timeout set for this task
*
* @param task
* @return
*/
private static Integer getTaskRank(ITask task) {
return getFromTaskParametersOrNull(task, BRunnerKeywords.InnerLevel.ALGORITHM_MAX_RANK.full);
}
private static Integer getFromTaskParametersOrNull(ITask task, String full) {
Optional val = Optional.empty();
val = task.toolParams().getValues(full).stream().findAny();
return val.isPresent() ? Integer.parseInt(val.get()) : null;
}
public static > T getServiceTypeFor(ITask task, Class enumClass) {
Optional serviceNameOpt = task.toolParams().getValues(BRunnerKeywords.InnerLevel.SERVICE.full).stream()
.findAny();
if (serviceNameOpt.isPresent()) {
String serviceName = serviceNameOpt.get();
return Enum.valueOf(enumClass, serviceName);
} else {
throw new IllegalArgumentException("No service name found in task parameters");
}
}
public static void checkServiceKeyAndValue(ITask task) {
if (!task.toolParams().containsProperty(BRunnerKeywords.InnerLevel.SERVICE.full)) {
throw new IllegalArgumentException(
"missing " + BRunnerKeywords.InnerLevel.SERVICE.full + " in benchmark configuration file");
}
}
}