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

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

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

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.forward_chaining.chase.Chase;
import fr.boreal.model.query.api.FOQuery;
import fr.boreal.model.ruleCompilation.NoRuleCompilation;
import fr.boreal.query_evaluation.component.QueryEvaluationOutput;
import fr.boreal.query_evaluation.component.QueryEvaluator;
import fr.lirmm.boreal.util.object_analyzer.ObjectAnalizer;
import integraal.services.AbstractInteGraalService;
import tools.service.CommonOps;
import tools.service.metadata.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_OMQAHybridChaseAndRewriting extends AbstractInteGraalService {

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

	}

	Chase chase;
	Iterable rewriting_result;
	Iterable answers;

	public void prepareServiceSpecificOperations() {

		operations.setupRunnable(CommonOps.FACTBASE_LOADING, builder::trySetFactBase);
		operations.setupRunnable(CommonOps.RULE_LOADING, builder::trySetRuleBase);
		operations.setupRunnable(CommonOps.QUERY_LOADING, builder::trySetQueryBase);

		operations.addRunnable(CommonOps.COMPILE_RULES, builder::tryCompileRuleset);

		operations.addRunnable(CommonOps.BUILD_CHASE, this::buildChase);

		operations.addRunnable(CommonOps.EXECUTE_CHASE, this::executeChase);

		builder.trySetQueryBase();

		for (FOQuery q : builder.getQueries()) {

			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 void buildChase() {

		chase = builder.buildOrGetChase();
	}

	private void executeChase() {

		chase.execute();

	}

	private void rewriteOMQintoUCQ(FOQuery q) {

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

	private void evaluateUCQ(FOQuery q) {
		var ucq = rewriting_result.iterator().next().rewritings();
		QueryEvaluator qa = QueryAnsweringComponentBuilder.prepareAndGetQueryAnsweringFrom(List.of(ucq), builder.getFactbase(),
				getTimeout());
		answers = qa.batchEvaluate();
	}

	@Override
	protected void postProcessingTrial() {

		ComponentPrinter.writeUCQAnswersCountToLog(answers);

	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy