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

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

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

import java.util.Collection;
import java.util.Set;

import org.slf4j.LoggerFactory;

import fr.boreal.component_builder.api.IAlgorithmParameters;
import fr.boreal.component_builder.api.IInputDataScenario;
import fr.boreal.component_builder.api.IOperationResult;
import fr.boreal.component_builder.components.QueryAnsweringComponentBuilder;
import fr.boreal.component_builder.operations.ChaseRunOperationResult;
import fr.boreal.component_builder.utils.ComponentPrinter;
import fr.boreal.forward_chaining.chase.Chase;
import fr.boreal.model.query.api.FOQuery;
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.IOperationMetadata;
import tools.service.OperationMetadataFactory;

/**
 * OMQA based on Chase 
 */
public class InteGraal_OMQAChase extends AbstractInteGraalService {

	Chase chase;
	Iterable answers;
	private QueryEvaluator qa;

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

	}

	public void prepareServiceSpecificOperations() {

		setup(CommonOps.FACTBASE_LOADING, builder::trySetFactBase);
		setup(CommonOps.RULE_LOADING, builder::trySetRuleBase);
		setup(CommonOps.QUERY_LOADING, builder::trySetQueryBase);
		setup(CommonOps.BUILD_CHASE, this::buildChase);

		operation(CommonOps.EXECUTE_CHASE, this::executeChase);

		queries().forEach(query -> {
			operation(buildingDescription(query), () -> buildEvaluator(query));
			operation(evaluationDescription(query), this::evaluateQuery);
		});

	}

	private Chase buildChase() {

		chase = builder.buildOrGetChase();

		return chase;
	}

	private IOperationResult executeChase() {

		chase.execute();

		return new ChaseRunOperationResult(chase);
	}

	private void buildEvaluator(FOQuery q) {
		qa = QueryAnsweringComponentBuilder.prepareAndGetQueryAnsweringFrom(Set.of(q), builder.getFactbase(),
				getTimeout());

	}

	private Iterable evaluateQuery() {
		answers = qa.batchEvaluate();
		return answers;
	}

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

	private IOperationMetadata evaluationDescription(FOQuery q) {
		var description = "query evaluation for " + ObjectAnalizer.digest(q);
		var serializationKey = CommonOps.QUERY_EVALUATION.serializationKey();

		return OperationMetadataFactory.createMetadata(description, serializationKey);
	}

	private IOperationMetadata buildingDescription(FOQuery q) {
		var description = "query evaluation for " + ObjectAnalizer.digest(q);
		var serializationKey = CommonOps.BUILD_QUERY_EVALUATOR.serializationKey();

		return OperationMetadataFactory.createMetadata(description, serializationKey);
	}

	@Override
	protected void postProcessingTrial() {

		ComponentPrinter.writeUCQAnswersCountToLog(answers);

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy