Skip to content

Commit

Permalink
[468118] Implemented first version of the improved LS planner algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
imbur committed Jun 23, 2015
1 parent 9523ba2 commit 2c6e1c1
Show file tree
Hide file tree
Showing 11 changed files with 634 additions and 212 deletions.
Expand Up @@ -69,24 +69,24 @@ public class LocalSearchResultProvider implements IQueryResultProvider {
private final IQueryBackendHintProvider hintProvider;
private final PQuery query;
private Logger logger;
private IQueryRuntimeContext context;
private IQueryRuntimeContext runtimeContext;
private IQueryCacheContext cacheContext;

private static class Planner {

Map<List<ISearchOperation>, Map<PVariable, Integer>> operationListsWithVarMappings;
private POperationCompiler compiler;

public void createPlan(MatcherReference key, Logger logger, IQueryMetaContext context, final ISearchContext searchContext)
public void createPlan(MatcherReference key, Logger logger, IQueryMetaContext metaContext, IQueryRuntimeContext runtimeContext, final ISearchContext searchContext)
throws QueryProcessingException {
IFlattenCallPredicate flattenCallPredicate = new DefaultFlattenCallPredicate();
PQueryFlattener flattener = new PQueryFlattener(flattenCallPredicate);
PBodyNormalizer normalizer = new PBodyNormalizer(context, false);
PBodyNormalizer normalizer = new PBodyNormalizer(metaContext, false);
LocalSearchPlannerStrategy strategy = new LocalSearchPlannerStrategy();
compiler = new POperationCompiler();

LocalSearchPlanner planner = new LocalSearchPlanner();
planner.initializePlanner(flattener, logger, context, normalizer, strategy, compiler);
planner.initializePlanner(flattener, logger, metaContext, runtimeContext, normalizer, strategy, compiler);
operationListsWithVarMappings = planner.plan(key.getQuery(), key.getAdornment());

Collection<SearchPlanExecutor> executors = Collections2.transform(operationListsWithVarMappings.entrySet(),
Expand Down Expand Up @@ -142,11 +142,11 @@ public void collectDependencies(Set<MatcherReference> dependencies) {

}

public LocalSearchResultProvider(IQueryBackend backend, Logger logger, IQueryRuntimeContext context,
public LocalSearchResultProvider(IQueryBackend backend, Logger logger, IQueryRuntimeContext runtimeContext,
IQueryCacheContext cacheContext, IQueryBackendHintProvider hintProvider, PQuery query) {
this.backend = backend;
this.logger = logger;
this.context = context;
this.runtimeContext = runtimeContext;
this.cacheContext = cacheContext;
this.hintProvider = hintProvider;
this.query = query;
Expand Down Expand Up @@ -191,7 +191,7 @@ public LocalSearchMatcher newLocalSearchMatcher(Object[] parameters) throws IncQ
while (!todo.isEmpty()) {
final MatcherReference dependency = todo.iterator().next();
Planner planner = new Planner();
planner.createPlan(dependency, logger, context.getMetaContext(), searchContext);
planner.createPlan(dependency, logger, runtimeContext.getMetaContext(), runtimeContext, searchContext);
planner.collectElementsToIndex(classesToIndex, featuresToIndex, dataTypesToIndex);
planner.collectDependencies(dependencies);
processedDependencies.add(dependency);
Expand Down
Expand Up @@ -10,37 +10,22 @@
*******************************************************************************/
package org.eclipse.incquery.runtime.localsearch.planner;

import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

import org.apache.log4j.Logger;
import org.eclipse.incquery.runtime.emf.types.EClassTransitiveInstancesKey;
import org.eclipse.incquery.runtime.localsearch.operations.ISearchOperation;
import org.eclipse.incquery.runtime.localsearch.planner.rewriters.PBodyUnaryTypeNormalizer;
import org.eclipse.incquery.runtime.matchers.context.IInputKey;
import org.eclipse.incquery.runtime.matchers.context.IQueryMetaContext;
import org.eclipse.incquery.runtime.matchers.planning.IQueryPlannerStrategy;
import org.eclipse.incquery.runtime.matchers.context.IQueryRuntimeContext;
import org.eclipse.incquery.runtime.matchers.planning.QueryProcessingException;
import org.eclipse.incquery.runtime.matchers.planning.SubPlan;
import org.eclipse.incquery.runtime.matchers.planning.helpers.TypeHelper;
import org.eclipse.incquery.runtime.matchers.psystem.ITypeInfoProviderConstraint;
import org.eclipse.incquery.runtime.matchers.psystem.PBody;
import org.eclipse.incquery.runtime.matchers.psystem.PConstraint;
import org.eclipse.incquery.runtime.matchers.psystem.PVariable;
import org.eclipse.incquery.runtime.matchers.psystem.TypeJudgement;
import org.eclipse.incquery.runtime.matchers.psystem.basicenumerables.TypeConstraint;
import org.eclipse.incquery.runtime.matchers.psystem.basicenumerables.TypeUnary;
import org.eclipse.incquery.runtime.matchers.psystem.queries.PDisjunction;
import org.eclipse.incquery.runtime.matchers.psystem.queries.PQuery;
import org.eclipse.incquery.runtime.matchers.psystem.queries.PQuery.PQueryStatus;
import org.eclipse.incquery.runtime.matchers.psystem.rewriters.PBodyCopier;
import org.eclipse.incquery.runtime.matchers.psystem.rewriters.PBodyNormalizer;
import org.eclipse.incquery.runtime.matchers.psystem.rewriters.PDisjunctionRewriter;
import org.eclipse.incquery.runtime.matchers.psystem.rewriters.PQueryFlattener;
import org.eclipse.incquery.runtime.matchers.psystem.rewriters.RewriterException;

Expand All @@ -55,7 +40,7 @@
*/
public class LocalSearchPlanner {

// Fields to track and debug the workflow
// Fields to track and debug the workflow
// Internal data
private PDisjunction flatDisjunction;
private PDisjunction normalizedDisjunction;
Expand All @@ -75,86 +60,92 @@ public List<SubPlan> getPlansForBodies() {

// Externally set tools for planning
private PQueryFlattener flattener;
private IQueryPlannerStrategy plannerStrategy;
private LocalSearchPlannerStrategy plannerStrategy;
private PBodyNormalizer normalizer;
private POperationCompiler operationCompiler;
private Logger logger;
private IQueryMetaContext context;
private Logger logger;
private IQueryMetaContext metaContext;
private IQueryRuntimeContext runtimeContext;

public void initializePlanner(PQueryFlattener pQueryFlattener, Logger logger, IQueryMetaContext context,
PBodyNormalizer pBodyNormalizer, IQueryPlannerStrategy localSearchPlannerStrategy,
public void initializePlanner(PQueryFlattener pQueryFlattener, Logger logger, IQueryMetaContext metaContext, IQueryRuntimeContext runtimeContext,
PBodyNormalizer pBodyNormalizer, LocalSearchPlannerStrategy localSearchPlannerStrategy,
POperationCompiler pOperationCompiler) {
this.flattener = pQueryFlattener;
this.logger = logger;
this.context = context;
this.logger = logger;
this.metaContext = metaContext;
this.runtimeContext = runtimeContext;
this.normalizer = pBodyNormalizer;
this.plannerStrategy = localSearchPlannerStrategy;
this.operationCompiler = pOperationCompiler;
}

/**
* Creates executable plans for the provided query. It is required to call one of the
* <code>initializePlanner()</code> methods before calling this method.
*
* @param querySpec
* @param boundVarIndices
* a set of integers representing the variables that are bound
* @return a mapping between ISearchOperation list and a mapping, that holds a PVariable-Integer mapping for the
* list of ISearchOperations
* @throws QueryProcessingException
*/
/**
* Creates executable plans for the provided query. It is required to call one of the
* <code>initializePlanner()</code> methods before calling this method.
*
* @param querySpec
* @param boundVarIndices
* a set of integers representing the variables that are bound
* @return a mapping between ISearchOperation list and a mapping, that holds a PVariable-Integer mapping for the
* list of ISearchOperations
* @throws QueryProcessingException
*/
public Map<List<ISearchOperation>, Map<PVariable, Integer>> plan(PQuery querySpec, Set<Integer> boundVarIndices)
throws QueryProcessingException {

// Flatten
flatDisjunction = flattener.rewrite(querySpec.getDisjunctBodies());
Set<PBody> flatBodies = flatDisjunction.getBodies();
prepareFlatBodesForNormalize(flatBodies);
// 1. Preparation
Set<PBody> normalizedBodies = prepareNormalizedBodies(querySpec);

// Normalize
normalizedDisjunction = normalizer.rewrite(flatDisjunction);
PBodyUnaryTypeNormalizer unaryTypeNormalizer = new PBodyUnaryTypeNormalizer(context);
normalizedDisjunction = unaryTypeNormalizer.rewrite(normalizedDisjunction);

Set<PBody> normalizedBodies = normalizedDisjunction.getBodies();

// Create plans for normalized bodies
// 2. Plan creation
// Context has matchers for the referred Queries (IQuerySpecifications)
plansForBodies = Lists.newArrayList();

for (PBody normalizedBody : normalizedBodies) {
preparePatternAdornmentForPlanner(boundVarIndices, normalizedBody);
SubPlan plan = plannerStrategy.plan(normalizedBody, logger, context);
Set<PVariable> adornment = calculatePatternAdornmentForPlanner(boundVarIndices, normalizedBody);
SubPlan plan = plannerStrategy.plan(normalizedBody, logger, adornment, metaContext, runtimeContext);
plansForBodies.add(plan);
}

// Compile (from POperations to ISearchOperations)
Map<List<ISearchOperation>,Map<PVariable, Integer>> compiledSubPlans = Maps.newHashMap();
// 3. PConstraint -> POperation compilation step
Map<List<ISearchOperation>, Map<PVariable, Integer>> compiledSubPlans = Maps.newHashMap();
// TODO finish (revisit?) the implementation of the compile function
// Pay extra caution to extend operations, when more than one variables are unbound
// * Pay extra caution to extend operations, when more than one variables are unbound
for (SubPlan subPlan : plansForBodies) {
List<ISearchOperation> compiledOperations = operationCompiler.compile(subPlan, boundVarIndices);
// Store the variable mappings for the plans for debug purposes (traceability information)
compiledSubPlans.put(compiledOperations,operationCompiler.getVariableMappings());
compiledSubPlans.put(compiledOperations, operationCompiler.getVariableMappings());
}

return compiledSubPlans;
}

private void prepareFlatBodesForNormalize(Set<PBody> flatBodies) {
private Set<PBody> prepareNormalizedBodies(PQuery querySpec) throws RewriterException {
// Preparation steps
// Flatten
flatDisjunction = flattener.rewrite(querySpec.getDisjunctBodies());
Set<PBody> flatBodies = flatDisjunction.getBodies();
prepareFlatBodiesForNormalize(flatBodies);

// Normalize
normalizedDisjunction = normalizer.rewrite(flatDisjunction);
Set<PBody> normalizedBodies = normalizedDisjunction.getBodies();
return normalizedBodies;
}

private void prepareFlatBodiesForNormalize(Set<PBody> flatBodies) {
// Revert status to be able to rewrite
// XXX Needed because the current implementation of the Normalizer requires mutable bodies
for (PBody pBody : flatBodies) {
pBody.setStatus(PQueryStatus.UNINITIALIZED);
}
}

private void preparePatternAdornmentForPlanner(Set<Integer> boundVarIndices, PBody normalizedBody) {
private Set<PVariable> calculatePatternAdornmentForPlanner(Set<Integer> boundVarIndices, PBody normalizedBody) {
Set<PVariable> boundVariables = Sets.<PVariable> newHashSet();
for (Integer i : boundVarIndices) {
boundVariables.add(normalizedBody.getSymbolicParameterVariables().get(i));
}
((LocalSearchPlannerStrategy) plannerStrategy).setBoundVariables(boundVariables);
return boundVariables;
}

}

0 comments on commit 2c6e1c1

Please sign in to comment.