
fr.boreal.component_builder.InputDataScenario Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of integraal-component Show documentation
Show all versions of integraal-component Show documentation
Components for the main functionalities of InteGraal
package fr.boreal.component_builder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.boreal.component_builder.api.scenario.IInputDataScenario;
import fr.boreal.model.component.InteGraalKeywords;
import fr.boreal.model.kb.api.FactBase;
import fr.boreal.model.kb.api.RuleBase;
import fr.boreal.model.query.api.Query;
import fr.boreal.views.FederatedFactBase;
import radicchio.FileUtils;
/**
* Represents a scenario for specifying the input of an InteGraal service (e.g.,
* saturation). Provides mechanisms to set and retrieve data (facts, rules,
* queries, mapping, and federation).
*/
public class InputDataScenario implements IInputDataScenario {
private static final Logger LOG = LoggerFactory.getLogger(InputDataScenario.class);
private final String name;
// Note we use mutable wrappers for code concision.
// KB elements from files
private MW> factbasePaths = new MW<>(null, false, "Factbase");
private MW> rulebasePaths = new MW<>(null, false, "Rulebase");
private MW> querybasePaths = new MW<>(null, false, "Querybase");
private MW> mappingbasePaths = new MW<>(null, false, "MappingBase");
// KB elements from objects
private MW factbase = new MW<>(null, false, "Factbase");
private MW rulebase = new MW<>(null, false, "Rulebase");
private MW> querybase = new MW<>(null, false, "Querybase");
// Hybrid RuleBase
private MW hybridRulebase = new MW<>(null, false, "Hybrid Rulebase");
private MW hybridRulebaseFilePath = new MW<>(null, false, "Hybrid Rulebase");
/**
* Constructs a new InputDataScenario with the specified name.
*
* @param scenarioName The name of the scenario.
*/
public InputDataScenario(String scenarioName) {
this.name = scenarioName;
}
@Override
public String getName() {
return name;
}
// FactBase Methods
@Override
public Optional getFactBase() {
return Optional.ofNullable(factbase.getValue());
}
@Override
public Optional> getFactbasePaths() {
return Optional.ofNullable(factbasePaths.getValue());
}
@Override
public void setFactbase(FactBase fb) {
setBase(factbase, fb);
}
@Override
public void setFactbasePaths(String... paths) {
setBasePaths(factbasePaths, paths, InteGraalKeywords.SupportedFileExtensions.Factbase.class);
}
// RuleBase Methods
@Override
public Optional getRuleBase() {
return Optional.ofNullable(rulebase.getValue());
}
@Override
public Optional> getRulebasePath() {
return Optional.ofNullable(rulebasePaths.getValue());
}
@Override
public void setRulebase(RuleBase rb) {
setBase(rulebase, rb);
}
@Override
public void setRulebasePaths(String... paths) {
setBasePaths(rulebasePaths, paths, InteGraalKeywords.SupportedFileExtensions.Rulebase.class);
}
// QueryBase Methods
@Override
public Optional> getQueryBase() {
return Optional.ofNullable(querybase.getValue());
}
@Override
public Optional> getQuerybasePaths() {
return Optional.ofNullable(querybasePaths.getValue());
}
@Override
public void setQuerybase(Collection qb) {
setBase(querybase, qb);
}
@Override
public void setQuerybasePaths(String... paths) {
setBasePaths(querybasePaths, paths, InteGraalKeywords.SupportedFileExtensions.Querybase.class);
}
// MappingBase Methods
@Override
public Optional> getMappingbasePaths() {
return Optional.ofNullable(mappingbasePaths.getValue());
}
@Override
public void setMappingbasePaths(String... paths) {
setBasePaths(mappingbasePaths, paths, InteGraalKeywords.SupportedFileExtensions.Mappingbase.class);
}
// Federated Factbase Methods
@Override
public Optional getFederatedFactBase() throws Exception {
return Optional.ofNullable(
factbase.getValue() instanceof FederatedFactBase federatedFactBase ? federatedFactBase : null);
}
@Override
public void setFederatedFactBase(FederatedFactBase fb) {
setBase(this.factbase, fb);
}
// Hybrid RuleBase Methods
@Override
public Optional getHybridRulebaseSat() {
return Optional.ofNullable(hybridRulebase.getValue().saturationRules());
}
@Override
public Optional getHybridRulebaseRew() {
return Optional.ofNullable(hybridRulebase.getValue().rewriteRules());
}
@Override
public Optional> getHybridRulebaseSatPaths() {
return Optional.ofNullable(hybridRulebaseFilePath.getValue().saturationRulesFile());
}
@Override
public Optional> getHybridRulebaseRewPaths() {
return Optional.ofNullable(hybridRulebaseFilePath.getValue().rewriteRulesFile());
}
@Override
public void setHybridRulebase(RuleBase rbSat, RuleBase rbRew) {
setBase(hybridRulebase, new HybridRuleBase(rbSat, rbRew));
}
@Override
public void setHybridRulebasePaths(Collection rulebasePathsSat, Collection rulebasePathsRew) {
setBase(hybridRulebaseFilePath, new HybridRuleBaseFilePath(rulebasePathsSat, rulebasePathsRew));
}
// Helper methods
/**
* Helper method to set a base object (e.g., FactBase, RuleBase) and update the
* corresponding flag.
*
* @param The type of the base value (FactBase, RuleBase, etc.).
* @param wrapper The wrapper containing the value and the flag.
* @param newValue The new value to set.
*/
private void setBase(MW wrapper, T newValue) {
if (!checkAndWarn(wrapper)) {
wrapper.setValue(newValue);
wrapper.setFlagToTrue();
}
}
/**
* Helper method to set base file paths.
*
* @param wrapper The wrapper for the collection of paths.
* @param paths The new paths to set.
* @param extensionClass The class for the file extension (e.g.,
* Rulebase.class).
*/
private void setBasePaths(MW> wrapper, String[] paths, Class extends Enum>> extensionClass) {
if (!checkAndWarn(wrapper)) {
Collection basePaths = new ArrayList<>();
for (String path : paths) {
basePaths.addAll(getFilesAndCheckExtensions(path, extensionClass));
}
if (!basePaths.isEmpty()) {
wrapper.setValue(basePaths);
wrapper.setFlagToTrue();
}
}
}
/**
* Helper method to check if a base has already been set, and log a warning if
* it has.
*
* @param wrapper The wrapper containing the value and flag.
* @return true if the base was already set, false otherwise.
*/
private boolean checkAndWarn(MW> wrapper) {
if (wrapper.isSet()) {
LOG.warn("{} was already set! Ignoring attempt to set {}.", wrapper.getBaseName(), wrapper.getBaseName());
return true;
}
return false;
}
/**
* Auxiliary method to retrieve file names and check extensions.
*
* @param path The file path.
* @param extensionClass The extension class used for validation.
* @return A collection of file names matching the extensions.
*/
private Collection getFilesAndCheckExtensions(String path, Class extends Enum>> extensionClass) {
return FileUtils.getFileNames(path, extensionClass);
}
// Inner classes for Hybrid RuleBase and its file paths
private record HybridRuleBase(RuleBase saturationRules, RuleBase rewriteRules) {
}
private record HybridRuleBaseFilePath(Collection saturationRulesFile, Collection rewriteRulesFile) {
}
@Override
public Collection getAllReferencedFilePaths() {
Collection fileList = new ArrayList<>();
getFactbasePaths().ifPresent(fileList::addAll);
getQuerybasePaths().ifPresent(fileList::addAll);
getRulebasePath().ifPresent(fileList::addAll);
getMappingbasePaths().ifPresent(fileList::addAll);
getHybridRulebaseSatPaths().ifPresent(fileList::addAll);
getHybridRulebaseRewPaths().ifPresent(fileList::addAll);
return fileList;
}
// MutableWrapper class
private static class MW {
private T value;
private boolean isSet;
private final String baseName;
public MW(T value, boolean isSet, String baseName) {
this.value = value;
this.isSet = isSet;
this.baseName = baseName;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public boolean isSet() {
return isSet;
}
public void setFlagToTrue() {
this.isSet = true;
}
public String getBaseName() {
return baseName;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy