package org.simantics.scl.compiler.elaboration.query; import java.util.ArrayList; import java.util.Set; import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext; import org.simantics.scl.compiler.elaboration.expressions.EApply; import org.simantics.scl.compiler.elaboration.expressions.ESimpleLambda; import org.simantics.scl.compiler.elaboration.expressions.ESimpleLet; import org.simantics.scl.compiler.elaboration.expressions.EVariable; import org.simantics.scl.compiler.elaboration.expressions.Expression; import org.simantics.scl.compiler.elaboration.expressions.QueryTransformer; import org.simantics.scl.compiler.elaboration.expressions.Variable; import org.simantics.scl.compiler.elaboration.query.compilation.ConstraintCollectionContext; import org.simantics.scl.compiler.elaboration.query.compilation.DerivateException; import org.simantics.scl.compiler.elaboration.query.compilation.QueryCompilationContext; import org.simantics.scl.compiler.elaboration.query.compilation.QueryConstraint; import org.simantics.scl.compiler.elaboration.query.compilation.UnsolvableQueryException; import org.simantics.scl.compiler.elaboration.relations.LocalRelation; import org.simantics.scl.compiler.elaboration.relations.SCLRelation; import org.simantics.scl.compiler.errors.Locations; import org.simantics.scl.compiler.types.Types; import gnu.trove.map.hash.THashMap; import gnu.trove.map.hash.TLongObjectHashMap; import gnu.trove.set.hash.TIntHashSet; public class QDisjunction extends QAbstractCombiner { public QDisjunction(Query ... queries) { super(queries); } private static class CachedPlan { Variable[] variables; QueryCompilationContext[] subplans; double totalBranching; double totalCost; public CachedPlan(Variable[] variables, QueryCompilationContext[] subplans, double totalBranching, double totalCost) { this.variables = variables; this.subplans = subplans; this.totalBranching = totalBranching; this.totalCost = totalCost; } } @Override public void collectConstraints(final ConstraintCollectionContext context) { TIntHashSet vars = new TIntHashSet(); collectVars(context.getVariableMap(), vars); final Variable continuationFunction = new Variable("continuation"); int[] variables = vars.toArray(); long variableMask_ = 0L; for(int v : variables) variableMask_ |= 1L << v; final long variableMask = variableMask_; context.addConstraint(new QueryConstraint(variables) { TLongObjectHashMap cache = new TLongObjectHashMap(); private CachedPlan create(long boundVariables) { QueryCompilationContext[] subplans = new QueryCompilationContext[queries.length]; double totalBranching = 1.0; double totalCost = 0.0; ArrayList solvedVariablesList = new ArrayList(); for(int v : variables) if( ((boundVariables >> v)&1) == 0 ) solvedVariablesList.add(context.getVariable(v)); Variable[] solvedVariables = solvedVariablesList.toArray(new Variable[solvedVariablesList.size()]); for(int i=0;i=0;--i) { functionDefinition = new ESimpleLambda( first ? Types.PROC /* FIXME */ : Types.NO_EFFECTS, newVariables[i], functionDefinition); first = false; } continuationFunction.setType(functionDefinition.getType()); context.setContinuation(new ESimpleLet( continuationFunction, functionDefinition, result)); } }); } @Override public Diff[] derivate(THashMap diffables) throws DerivateException { Diff[][] diffs = new Diff[queries.length][]; int totalDiffCount = 0; for(int i=0;i relations) { for(int i=0;i newQueries = new ArrayList(queries.length); for(int j=0;j