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

integraal.services.concrete.InteGraal_OMQARew Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
package integraal.services.concrete;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.slf4j.LoggerFactory;

import fr.boreal.backward_chaining.evaluators.QueryRewriter;
import fr.boreal.backward_chaining.evaluators.RewritingOutput;
import fr.boreal.component_builder.api.IAlgorithmParameters;
import fr.boreal.component_builder.api.IInputDataScenario;
import fr.boreal.component_builder.components.QueryAnsweringComponentBuilder;
import fr.boreal.component_builder.components.RewritingComponentBuilder;
import fr.boreal.component_builder.utils.ComponentPrinter;
import fr.boreal.model.query.api.FOQuery;
import fr.boreal.model.ruleCompilation.NoRuleCompilation;
import fr.boreal.query_evaluation.component.QueryEvaluationOutput;
import fr.lirmm.boreal.util.object_analyzer.ObjectAnalizer;
import integraal.services.AbstractInteGraalService;
import tools.service.CommonOps;
import tools.service.OperationMetadataFactory;

/**
 * 
 * Inputs : 1. a factbase 2. a query workload 3. a rulebase
 * 
 * Protocol :
 * 
 * For every query Q, we first compute the UCQ-rewriting of Q, and then we
 * answer the UCQ.
 * 
 */

public class InteGraal_OMQARew extends AbstractInteGraalService {

	Iterable rewriting_result;
	List> ucq_answers = new ArrayList<>();

	/**
	 * @param ids input data scenario
	 * @param iap algorithm parameters
	 */
	public InteGraal_OMQARew(IInputDataScenario ids, IAlgorithmParameters iap) {
		super(ids, iap);
		LOG = LoggerFactory.getLogger(InteGraal_OMQARew.class);

	}

	public void prepareServiceSpecificOperations() {

		setup(CommonOps.FACTBASE_LOADING, builder::trySetFactBase);
		setup(CommonOps.RULE_LOADING, builder::trySetRuleBase);
		setup(CommonOps.QUERY_LOADING, builder::trySetQueryBase);
		setup(CommonOps.COMPILE_RULES, builder::tryCompileRuleset);

		queries().forEach(q -> {

			var description_r = "query rewriting for " + ObjectAnalizer.digest(q);
			var serializationKey_r = CommonOps.QUERY_REWRITING.serializationKey();

			var metadata_r = OperationMetadataFactory.createMetadata(description_r, serializationKey_r);

			operation(metadata_r, () -> rewriteOMQintoUCQ(q));

			var description = "UCQ query evaluation for " + ObjectAnalizer.digest(q);
			var serializationKey = CommonOps.QUERY_EVALUATION.serializationKey();

			var metadata = OperationMetadataFactory.createMetadata(description, serializationKey);

			operation(metadata, () -> evaluateUCQ(q));

		});

	}

	private Iterable rewriteOMQintoUCQ(FOQuery q) {

		QueryRewriter rewriter = RewritingComponentBuilder.prepareAndGetRewriterFrom(Set.of(q), builder.getRulebase(),
				NoRuleCompilation.instance(), getTimeout());
		rewriting_result = rewriter.batchEvaluate();

		return rewriting_result;

	}

	private Iterable evaluateUCQ(FOQuery q) {
		var ucq = rewriting_result.iterator().next().rewritings();

		if (inputAlgorithmParameters.asksCountOnly()) {

			var qa = QueryAnsweringComponentBuilder.prepareAndGetCountingQueryAnsweringFrom(List.of(ucq), builder.getFactbase(),
					getTimeout());

			var result = qa.batchEvaluate();
			ucq_answers.add(result);
			return result;

		} else {
			var qa = QueryAnsweringComponentBuilder.prepareAndGetQueryAnsweringFrom(List.of(ucq), builder.getFactbase(),
					getTimeout());

			var result = qa.batchEvaluate();
			ucq_answers.add(result);
			return result;
		}
	}

	@Override
	protected void postProcessingTrial() {

		ucq_answers.forEach(ComponentPrinter::writeUCQAnswersCountToLog);

	}

	private Collection queries() {
		builder.trySetQueryBase();
		return builder.getQueries();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy