All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.boreal.component_builder.AlgorithmParameters Maven / Gradle / Ivy

There is a newer version: 1.6.2
Show newest version
package fr.boreal.component_builder;

import java.lang.reflect.Field;
import java.time.Duration;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fr.boreal.component_builder.api.algorithm.IAlgorithmParameters;
import fr.boreal.model.component.InteGraalKeywords;
import fr.boreal.model.component.InteGraalKeywords.Algorithms;
import fr.boreal.model.component.InteGraalKeywords.InternalStorageConfiguration;
import fr.boreal.model.component.InteGraalKeywords.Algorithms.Answers;
import fr.boreal.model.component.InteGraalKeywords.Algorithms.Parameters.Compilation;
import fr.boreal.model.component.InteGraalKeywords.Algorithms.Parameters.Chase.Application;
import fr.boreal.model.component.InteGraalKeywords.Algorithms.Parameters.Chase.Applier;
import fr.boreal.model.component.InteGraalKeywords.Algorithms.Parameters.Chase.Checker;
import fr.boreal.model.component.InteGraalKeywords.Algorithms.Parameters.Chase.Computer;
import fr.boreal.model.component.InteGraalKeywords.Algorithms.Parameters.Chase.Scheduler;
import fr.boreal.model.component.InteGraalKeywords.Algorithms.Parameters.Chase.Skolem;
import fr.boreal.model.component.InteGraalKeywords.InternalStorageConfiguration.DBMSDriverParameters;
import fr.boreal.model.component.InteGraalKeywords.InternalStorageConfiguration.DBType;
import fr.boreal.model.component.InteGraalKeywords.InternalStorageConfiguration.DriverType;
import fr.boreal.model.component.InteGraalKeywords.InternalStorageConfiguration.StorageLayout;
import fr.lirmm.boreal.util.enumerations.EnumUtils;
import fr.lirmm.boreal.util.externalHaltingConditions.ExternalAlgorithmHaltingConditions;
import fr.lirmm.boreal.util.string.StringUtils;

/**
 * Default implementation.
 */
public class AlgorithmParameters implements IAlgorithmParameters {

	static final Logger LOG = LoggerFactory.getLogger(AlgorithmParameters.class);

	protected final String name;

	private Scheduler scheduler;
	private Checker checker;
	private Computer computer;
	private Application application;
	private Applier applier;
	private Skolem skolem;

	private DBType dbtype;
	private DriverType driverType;
	private StorageLayout storageLayout;

	private Compilation compilation;

	private String DBMSDriverParameters_URL;
	private String DBMSDriverParameters_PORT;
	private String DBMSDriverParameters_DATABASE_NAME;
	private String DBMSDriverParameters_USER_NAME;
	private String DBMSDriverParameters_USER_PASSWORD;
	private String DBMSDriverParameters_CLEAR_DB;

	private ExternalAlgorithmHaltingConditions externalHaltingConditions;

	private final Algorithms algorithm;

	private boolean asksCountOnly;

	/////////////////////////////////////////
	//// PUBLIC METHODS
	/////////////////////////////////////////

	/**
	 * @param name          of the configuration
	 * @param algorithmType of the algorithm
	 */
	public AlgorithmParameters(String name, Algorithms algorithmType) {

		this.name = name;

		this.algorithm = algorithmType;

	}

	public String getName() {

		return this.name + "_" + this.algorithm.toString();

	}

	public InteGraalKeywords.Algorithms getAlgorithm() {

		return algorithm;
	}

	@Override
	public boolean usesSaturationAlgorithm() {

		return algorithm.equals(InteGraalKeywords.Algorithms.KB_CHASE)
				|| algorithm.equals(InteGraalKeywords.Algorithms.OMQA_CHASE)

				|| algorithm.equals(InteGraalKeywords.Algorithms.QUERY_ANSWERING_VIA_HYBRID_STRATEGY);

	}

	@Override
	public boolean usesQueryRewritingAlgorithm() {

		return algorithm.equals(InteGraalKeywords.Algorithms.OMQ_REWRITING)
				|| algorithm.equals(InteGraalKeywords.Algorithms.OMQA_REW);

	}

	@Override
	public boolean usesQueryAnsweringAlgorithm() {

		return algorithm.equals(InteGraalKeywords.Algorithms.QUERY_ANSWERING);

	}

	@Override
	public boolean usesRuleCompilationAlgorithm() {

		return algorithm.equals(InteGraalKeywords.Algorithms.RULE_COMPILATION) || usesQueryRewritingAlgorithm();

	}

	@Override
	public boolean usesOMQASaturationAlgorithm() {

		return algorithm.equals(InteGraalKeywords.Algorithms.OMQA_CHASE);

	}

	@Override
	public boolean usesOMQARewritingAlgorithm() {

		return algorithm.equals(InteGraalKeywords.Algorithms.OMQA_REW);

	}

	@Override
	public boolean usesOMQAHybridAlgorithm() {

		return algorithm.equals(InteGraalKeywords.Algorithms.QUERY_ANSWERING_VIA_HYBRID_STRATEGY);

	}

	@Override
	public boolean usesStorage() {

		return usesSaturationAlgorithm() || usesQueryAnsweringAlgorithm() || usesOMQASaturationAlgorithm()
				|| usesOMQARewritingAlgorithm() || usesOMQAHybridAlgorithm();

	}

	@Override
	public Optional getRuleApplication() {
		return Optional.ofNullable(application);
	}

	@Override
	public Optional getRuleApplier() {

		return Optional.ofNullable(applier);
	}

	@Override
	public Optional getScheduler() {
		return Optional.ofNullable(scheduler);
	}

	@Override
	public Optional getCriterion() {
		return Optional.ofNullable(checker);
	}

	@Override
	public Optional getComputer() {
		return Optional.ofNullable(computer);
	}

	@Override
	public Optional getSkolemization() {
		return Optional.ofNullable(skolem);
	}

	@Override
	public Optional getStorageType() {
		return Optional.ofNullable(dbtype);
	}

	@Override
	public Optional getDBDriver() {
		return Optional.ofNullable(driverType);
	}

	@Override
	public Optional getDBStrategy() {
		return Optional.ofNullable(storageLayout);
	}

	@Override
	public Optional getCompilation() {
		return Optional.ofNullable(compilation);
	}

	@Override
	public Optional getRank() {
		return Optional.ofNullable(externalHaltingConditions.rank());
	}

	@Override
	public Optional getTimeout() {
		return Optional.ofNullable(externalHaltingConditions.timeout());
	}

	/**
	 * SETTERS FOR STORAGE
	 */

	public IAlgorithmParameters setStorageType(String storageDBTypeName) {
		return setParameter(EnumUtils.findEnumFromString(storageDBTypeName,
				InternalStorageConfiguration.DBType.class));
	}

	public IAlgorithmParameters setDBDriver(String storageDriverName) {
		return setParameter(EnumUtils.findEnumFromString(storageDriverName,
				InternalStorageConfiguration.DriverType.class));
	}

	public IAlgorithmParameters setDBStrategy(String storageStrategyName) {
		return setParameter(EnumUtils.findEnumFromString(storageStrategyName,
				InternalStorageConfiguration.StorageLayout.class));
	}

	@Override
	public IAlgorithmParameters setStorageType(DBType storage) {
		return setParameter(storage);

	}

	@Override
	public IAlgorithmParameters setDBDriver(DriverType storageDriver) {
		return setParameter(storageDriver);
	}

	@Override
	public IAlgorithmParameters setDBStrategy(StorageLayout storageStrategy) {
		return setParameter(storageStrategy);
	}

	/**
	 * SETTERS FOR RULE COMPILATION
	 */

	public IAlgorithmParameters setCompilation(String compilationName) {
		return setParameter(EnumUtils.findEnumFromString(compilationName,
				InteGraalKeywords.Algorithms.Parameters.Compilation.class));
	}

	@Override
	public IAlgorithmParameters setCompilation(Compilation compilation) {
		return setParameter(compilation);
	}

	/**
	 * SETTERS FOR CHASE
	 */

	public IAlgorithmParameters setRuleApplication(String applicationName) {
		return setParameter(EnumUtils.findEnumFromString(applicationName,
				InteGraalKeywords.Algorithms.Parameters.Chase.Application.class));
	}

	public IAlgorithmParameters setRuleApplier(String applierName) {

		return setParameter(
				EnumUtils.findEnumFromString(applierName, InteGraalKeywords.Algorithms.Parameters.Chase.Applier.class));
	}

	public IAlgorithmParameters setScheduler(String scheduler) {

		return setParameter(
				EnumUtils.findEnumFromString(scheduler, InteGraalKeywords.Algorithms.Parameters.Chase.Scheduler.class));
	}

	public IAlgorithmParameters setCriterion(String criterionName) {

		return setParameter(EnumUtils.findEnumFromString(criterionName,
				InteGraalKeywords.Algorithms.Parameters.Chase.Checker.class));

	}

	public IAlgorithmParameters setComputer(String computerName) {

		return setParameter(EnumUtils.findEnumFromString(computerName,
				InteGraalKeywords.Algorithms.Parameters.Chase.Computer.class));

	}

	public IAlgorithmParameters setSkolemization(String skolemizationName) {

		return setParameter(EnumUtils.findEnumFromString(skolemizationName,
				InteGraalKeywords.Algorithms.Parameters.Chase.Skolem.class));
	}

	@Override
	public IAlgorithmParameters setScheduler(Scheduler scheduler) {

		return setParameter(scheduler);
	}

	@Override
	public IAlgorithmParameters setCriterion(Checker criterion) {

		return setParameter(criterion);

	}

	@Override
	public IAlgorithmParameters setComputer(Computer computer) {

		return setParameter(computer);

	}

	@Override
	public IAlgorithmParameters setRuleApplication(Application application) {

		return setParameter(application);

	}

	@Override
	public IAlgorithmParameters setRuleApplier(Applier applier) {

		return setParameter(applier);

	}

	@Override
	public IAlgorithmParameters setSkolemization(Skolem skolemization) {
		return setParameter(skolemization);

	}

	/**
	 * SETTERS FOR OTHER PARAMETERS
	 */

	@Override
	public IAlgorithmParameters setRank(Long rank) {
		this.externalHaltingConditions = this.externalHaltingConditions.withModified(rank, null);
		return this;
	}

	@Override
	public IAlgorithmParameters setTimeout(Duration timeout) {
		this.externalHaltingConditions = this.externalHaltingConditions.withModified(null, timeout);
		return this;
	}

	/**
	 * Sets the value of a parameter based on the provided enum constant. This
	 * method uses reflection to dynamically identify and set the field within this
	 * class that corresponds to the enum type of the given constant. The field name
	 * is expected to match the simple name of the enum's class, with the first
	 * letter lowercased (following Java naming conventions).
	 * 

* This approach allows for a more flexible and scalable way of handling * parameters of various enum types without the need for specific setter methods * for each parameter type. It reduces boilerplate code and enhances * maintainability. *

* * @param enumValue The enum constant to set as the value of the corresponding * field. The enum's class simple name (lowercased) should * match the name of the field in this class. * @throws RuntimeException If no matching field is found for the enum type, or * if an error occurs while attempting to set the * field's value. This includes cases where the field * is not accessible or if the field name does not * match the expected naming convention. * @see Field#setAccessible(boolean) */ public IAlgorithmParameters setParameter(Enum enumValue) { LOG.debug("Setting parameter {} for {}", enumValue, this.name); validateParameter(enumValue); String fieldName = enumValue.getClass().getSimpleName(); try { Field field = null; for (Field f : this.getClass().getDeclaredFields()) { if (fieldName.equalsIgnoreCase(f.getName())) { field = f; break; } } if (field == null) { LOG.error("Cannot set parameter {}", enumValue); throw new RuntimeException( String.format( "[%s::setParameter] Cannot set parameter: %s.", this.getClass(), enumValue)); } else { field.setAccessible(true); field.set(this, enumValue); } } catch (IllegalAccessException e) { LOG.error("Failed to set parameter: {}\n{}", fieldName, e.getMessage()); throw new RuntimeException( String.format( "[%s::setParameter] Failed to set parameter: %s.", this.getClass(), fieldName), e); } return this; } public String toString() { return StringUtils.print(this); } @Override public boolean asksCountOnly() { return this.asksCountOnly; } ///////////////////////////////////////// //// PRIVATE METHODS ///////////////////////////////////////// private void validateParameter(Enum enumValue) { if (enumValue.getClass().getEnclosingClass() == InteGraalKeywords.Algorithms.Parameters.Chase.class && !usesSaturationAlgorithm()) { throw new IllegalArgumentException( "Chase parameter " + enumValue + " cannot be set for " + getAlgorithm() + " algorithms."); } if (enumValue.getClass().getEnclosingClass() == InteGraalKeywords.Algorithms.Parameters.Compilation.class && !usesRuleCompilationAlgorithm()) { throw new IllegalArgumentException( "Compilation parameter " + enumValue + " cannot be set for " + getAlgorithm() + " algorithms."); } if (enumValue.getClass().getEnclosingClass() == InteGraalKeywords.InternalStorageConfiguration.class && !usesStorage()) { throw new IllegalArgumentException( "Storage parameter " + enumValue + " cannot be set for " + getAlgorithm() + " algorithms."); } } @Override public Optional> getDBMSDriverParameters() { Map result = new LinkedHashMap<>(); if (DBMSDriverParameters_URL != null) { result.put(DBMSDriverParameters.URL, DBMSDriverParameters_URL); } if (DBMSDriverParameters_PORT != null) { result.put(DBMSDriverParameters.PORT, DBMSDriverParameters_PORT); } if (DBMSDriverParameters_DATABASE_NAME != null) { result.put(DBMSDriverParameters.URL, DBMSDriverParameters_DATABASE_NAME); } if (DBMSDriverParameters_USER_NAME != null) { result.put(DBMSDriverParameters.USER_NAME, DBMSDriverParameters_USER_NAME); } if (DBMSDriverParameters_USER_PASSWORD != null) { result.put(DBMSDriverParameters.USER_PASSWORD, DBMSDriverParameters_USER_PASSWORD); } if (DBMSDriverParameters_CLEAR_DB != null) { result.put(DBMSDriverParameters.CLEAR_DB, DBMSDriverParameters_CLEAR_DB); } if (result.isEmpty()) { return Optional.empty(); } else { return Optional.of(result); } } @Override public IAlgorithmParameters setDBMSDriverParameters(DBMSDriverParameters paramName, String value) { switch (paramName) { case DATABASE_NAME -> this.DBMSDriverParameters_DATABASE_NAME = value; case PORT -> this.DBMSDriverParameters_PORT = value; case URL -> this.DBMSDriverParameters_URL = value; case USER_NAME -> this.DBMSDriverParameters_USER_NAME = value; case USER_PASSWORD -> this.DBMSDriverParameters_USER_PASSWORD = value; case CLEAR_DB -> this.DBMSDriverParameters_CLEAR_DB = value; default -> throw new IllegalArgumentException("Unexpected parameter: " + paramName); } return this; } public void setDBMSDriverParameters(String paramName, String paramValue) { setDBMSDriverParameters(EnumUtils.findEnumFromString(paramName, DBMSDriverParameters.class), paramValue); } public IAlgorithmParameters setParameter(Class> className, String propertyName, String value) { LOG.debug("Setting parameter {} for {} with value {} for {} ", propertyName, className.getSimpleName(), value, this.name); switch (className.getSimpleName()) { case "DBMSDriverParameters" -> setDBMSDriverParameters(propertyName, value); default -> throw new IllegalArgumentException("Unexpected value: " + className); } return this; } @Override public boolean asksLists() { // TODO Auto-generated method stub return false; } @Override public boolean asksSet() { // TODO Auto-generated method stub return false; } @Override public void setResultType(Answers type) { // TODO Auto-generated method stub } @Override public ExternalAlgorithmHaltingConditions getExternalHaltingConditions() { if (this.externalHaltingConditions == null) { this.externalHaltingConditions = ExternalAlgorithmHaltingConditions.defaultConditions(); } return this.externalHaltingConditions; } @Override public IAlgorithmParameters setExternalHaltingConditions(ExternalAlgorithmHaltingConditions hc) { this.externalHaltingConditions = hc; return this; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy