use of mb.nabl2.solver.ISolution in project nabl by metaborg.
the class SingleFileSolver method solve.
public ISolution solve(GraphSolution initial, Function1<String, String> fresh, ICancel cancel, IProgress progress) throws SolverException, InterruptedException {
final SolverConfig config = initial.config();
// shared
final Ref<IUnifier.Immutable> unifier = new Ref<>(initial.unifier());
// constraint set properties
final ActiveVars activeVars = new ActiveVars(unifier);
final ActiveDeclTypes activeDeclTypes = new ActiveDeclTypes(unifier);
final HasRelationBuildConstraints hasRelationBuildConstraints = new HasRelationBuildConstraints();
// guards
final Predicate1<String> isRelationComplete = r -> !hasRelationBuildConstraints.contains(r);
// more shared
final IEsopScopeGraph.Transient<Scope, Label, Occurrence, ITerm> scopeGraph = initial.scopeGraph().melt();
final IEsopNameResolution<Scope, Label, Occurrence> nameResolution = EsopNameResolution.of(config.getResolutionParams(), scopeGraph, (s, l) -> true);
// solver components
final SolverCore core = new SolverCore(config, unifier, fresh, callExternal);
final BaseComponent baseSolver = new BaseComponent(core);
final EqualityComponent equalitySolver = new EqualityComponent(core, unifier);
final NameResolutionComponent nameResolutionSolver = new NameResolutionComponent(core, scopeGraph, nameResolution, Properties.Transient.of());
final NameSetsComponent nameSetSolver = new NameSetsComponent(core, scopeGraph, nameResolution);
final RelationComponent relationSolver = new RelationComponent(core, isRelationComplete, config.getFunctions(), VariantRelations.transientOf(config.getRelations()));
final SetComponent setSolver = new SetComponent(core, nameSetSolver.nameSets());
final SymbolicComponent symSolver = new SymbolicComponent(core, SymbolicConstraints.of());
final ControlFlowComponent cfgSolver = new ControlFlowComponent(core, ImmutableFlowSpecSolution.of());
// polymorphism solver
final PolySafe polySafe = new PolySafe(activeVars, activeDeclTypes, nameResolutionSolver);
final PolymorphismComponent polySolver = new PolymorphismComponent(core, polySafe::isGenSafe, polySafe::isInstSafe, nameResolutionSolver::getProperty);
final ISolver component = c -> c.matchOrThrow(IConstraint.CheckedCases.<Optional<SolveResult>, InterruptedException>builder().onBase(baseSolver::solve).onEquality(equalitySolver::solve).onNameResolution(nameResolutionSolver::solve).onPoly(polySolver::solve).onRelation(relationSolver::solve).onSet(setSolver::solve).onSym(symSolver::solve).onControlflow(cfgSolver::solve).otherwise(ISolver.deny("Not allowed in this phase")));
final FixedPointSolver solver = new FixedPointSolver(cancel, progress, component, Iterables2.from(activeVars, hasRelationBuildConstraints));
solver.step().subscribe(r -> {
if (!r.unifierDiff().isEmpty()) {
try {
nameResolutionSolver.update();
} catch (InterruptedException ex) {
// ignore here
}
}
});
try {
nameResolutionSolver.update();
SolveResult solveResult = solver.solve(initial.constraints());
final IMessages.Transient messages = initial.messages().melt();
messages.addAll(solveResult.messages());
NameResolutionResult nameResolutionResult = nameResolutionSolver.finish();
IUnifier.Immutable unifierResult = equalitySolver.finish();
Map<String, IVariantRelation.Immutable<ITerm>> relationResult = relationSolver.finish();
ISymbolicConstraints symbolicConstraints = symSolver.finish();
IFlowSpecSolution<CFGNode> fsSolution = cfgSolver.finish();
return ImmutableSolution.of(config, initial.astProperties(), nameResolutionResult.scopeGraph(), nameResolutionResult.declProperties(), relationResult, unifierResult, symbolicConstraints, fsSolution, messages.freeze(), solveResult.constraints()).withNameResolutionCache(nameResolutionResult.resolutionCache());
} catch (RuntimeException ex) {
throw new SolverException("Internal solver error.", ex);
}
}
Aggregations