Search in sources :

Example 1 with ISolver

use of mb.nabl2.solver.ISolver in project nabl by metaborg.

the class BaseMultiFileSolver method solveIntra.

public ISolution solveIntra(GraphSolution initial, Collection<ITermVar> intfVars, @Nullable Collection<Scope> intfScopes, 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);
    intfVars.stream().forEach(activeVars::add);
    // guards -- intfScopes == null indicates we do not know the interface scopes, and resolution should be delayed.
    final Predicate2<Scope, Label> isEdgeClosed = (s, l) -> intfScopes != null && !intfScopes.contains(s);
    // more shared
    final IEsopScopeGraph.Transient<Scope, Label, Occurrence, ITerm> scopeGraph = initial.scopeGraph().melt();
    final IEsopNameResolution<Scope, Label, Occurrence> nameResolution = EsopNameResolution.of(config.getResolutionParams(), scopeGraph, isEdgeClosed);
    // 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 PolymorphismComponent polySolver = new PolymorphismComponent(core, Predicate1.never(), Predicate1.never(), nameResolutionSolver::getProperty);
    final RelationComponent relationSolver = new RelationComponent(core, Predicate1.never(), 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());
    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));
    solver.step().subscribe(r -> {
        if (!r.unifierDiff().isEmpty()) {
            try {
                nameResolutionSolver.update();
            } catch (InterruptedException ex) {
            // ignore here
            }
        }
    });
    try {
        nameResolutionSolver.update();
        final SolveResult solveResult = solver.solve(initial.constraints());
        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, solveResult.messages(), solveResult.constraints()).withNameResolutionCache(nameResolutionResult.resolutionCache());
    } catch (RuntimeException ex) {
        throw new SolverException("Internal solver error.", ex);
    }
}
Also used : VariantRelations(mb.nabl2.relations.variants.VariantRelations) ControlFlowComponent(mb.nabl2.solver.components.ControlFlowComponent) IUnifier(mb.nabl2.terms.unification.IUnifier) Function1(org.metaborg.util.functions.Function1) ITerm(mb.nabl2.terms.ITerm) IProgress(org.metaborg.util.task.IProgress) ImmutableSolution(mb.nabl2.solver.ImmutableSolution) Ref(org.metaborg.util.Ref) IConstraint(mb.nabl2.constraints.IConstraint) SetComponent(mb.nabl2.solver.components.SetComponent) Occurrence(mb.nabl2.scopegraph.terms.Occurrence) IEsopNameResolution(mb.nabl2.scopegraph.esop.IEsopNameResolution) NameResolutionResult(mb.nabl2.solver.components.NameResolutionComponent.NameResolutionResult) SolveResult(mb.nabl2.solver.ISolver.SolveResult) SolverCore(mb.nabl2.solver.SolverCore) IVariantRelation(mb.nabl2.relations.variants.IVariantRelation) RelationComponent(mb.nabl2.solver.components.RelationComponent) CFGNode(mb.nabl2.controlflow.terms.CFGNode) ICancel(org.metaborg.util.task.ICancel) Label(mb.nabl2.scopegraph.terms.Label) IFlowSpecSolution(mb.nabl2.controlflow.terms.IFlowSpecSolution) Map(java.util.Map) SymbolicComponent(mb.nabl2.solver.components.SymbolicComponent) ITermVar(mb.nabl2.terms.ITermVar) Properties(mb.nabl2.util.collections.Properties) SolverException(mb.nabl2.solver.SolverException) ISolution(mb.nabl2.solver.ISolution) ISymbolicConstraints(mb.nabl2.symbolic.ISymbolicConstraints) Nullable(javax.annotation.Nullable) EqualityComponent(mb.nabl2.solver.components.EqualityComponent) ActiveVars(mb.nabl2.solver.properties.ActiveVars) NameResolutionComponent(mb.nabl2.solver.components.NameResolutionComponent) BaseComponent(mb.nabl2.solver.components.BaseComponent) Collection(java.util.Collection) ImmutableFlowSpecSolution(mb.nabl2.controlflow.terms.ImmutableFlowSpecSolution) IEsopScopeGraph(mb.nabl2.scopegraph.esop.IEsopScopeGraph) SymbolicConstraints(mb.nabl2.symbolic.SymbolicConstraints) EsopNameResolution(mb.nabl2.scopegraph.esop.lazy.EsopNameResolution) Predicate2(org.metaborg.util.functions.Predicate2) Scope(mb.nabl2.scopegraph.terms.Scope) Predicate1(org.metaborg.util.functions.Predicate1) NaBL2DebugConfig(mb.nabl2.config.NaBL2DebugConfig) Iterables2(org.metaborg.util.iterators.Iterables2) PolymorphismComponent(mb.nabl2.solver.components.PolymorphismComponent) NameSetsComponent(mb.nabl2.solver.components.NameSetsComponent) ISolver(mb.nabl2.solver.ISolver) Optional(java.util.Optional) SolverConfig(mb.nabl2.solver.SolverConfig) SetComponent(mb.nabl2.solver.components.SetComponent) BaseComponent(mb.nabl2.solver.components.BaseComponent) IUnifier(mb.nabl2.terms.unification.IUnifier) SymbolicComponent(mb.nabl2.solver.components.SymbolicComponent) RelationComponent(mb.nabl2.solver.components.RelationComponent) Label(mb.nabl2.scopegraph.terms.Label) IEsopScopeGraph(mb.nabl2.scopegraph.esop.IEsopScopeGraph) ControlFlowComponent(mb.nabl2.solver.components.ControlFlowComponent) NameResolutionComponent(mb.nabl2.solver.components.NameResolutionComponent) ActiveVars(mb.nabl2.solver.properties.ActiveVars) ISymbolicConstraints(mb.nabl2.symbolic.ISymbolicConstraints) Occurrence(mb.nabl2.scopegraph.terms.Occurrence) SolverConfig(mb.nabl2.solver.SolverConfig) ISolver(mb.nabl2.solver.ISolver) Optional(java.util.Optional) CFGNode(mb.nabl2.controlflow.terms.CFGNode) NameSetsComponent(mb.nabl2.solver.components.NameSetsComponent) SolverCore(mb.nabl2.solver.SolverCore) PolymorphismComponent(mb.nabl2.solver.components.PolymorphismComponent) Ref(org.metaborg.util.Ref) SolveResult(mb.nabl2.solver.ISolver.SolveResult) Scope(mb.nabl2.scopegraph.terms.Scope) ITerm(mb.nabl2.terms.ITerm) EqualityComponent(mb.nabl2.solver.components.EqualityComponent) NameResolutionResult(mb.nabl2.solver.components.NameResolutionComponent.NameResolutionResult) SolverException(mb.nabl2.solver.SolverException)

Example 2 with ISolver

use of mb.nabl2.solver.ISolver in project nabl by metaborg.

the class BaseSolver method solveGraph.

public GraphSolution solveGraph(BaseSolution initial, Function1<String, String> fresh, ICancel cancel, IProgress progress) throws SolverException, InterruptedException {
    // shared
    final Ref<IUnifier.Immutable> unifier = new Ref<>(initial.unifier());
    final IEsopScopeGraph.Transient<Scope, Label, Occurrence, ITerm> scopeGraph = EsopScopeGraph.Transient.of();
    // solver components
    final SolverCore core = new SolverCore(initial.config(), unifier, fresh, callExternal);
    final AstComponent astSolver = new AstComponent(core, Properties.Transient.of());
    final BaseComponent baseSolver = new BaseComponent(core);
    final EqualityComponent equalitySolver = new EqualityComponent(core, unifier);
    final ScopeGraphComponent scopeGraphSolver = new ScopeGraphComponent(core, scopeGraph);
    try {
        ISolver component = c -> c.matchOrThrow(IConstraint.CheckedCases.<Optional<SolveResult>, InterruptedException>builder().onAst(astSolver::solve).onBase(baseSolver::solve).onEquality(equalitySolver::solve).onScopeGraph(scopeGraphSolver::solve).otherwise(cc -> Optional.empty()));
        final FixedPointSolver solver = new FixedPointSolver(cancel, progress, component, Iterables2.empty());
        final SolveResult solveResult = solver.solve(initial.constraints());
        return ImmutableGraphSolution.of(initial.config(), astSolver.finish(), scopeGraphSolver.finish(), equalitySolver.finish(), solveResult.messages(), solveResult.constraints());
    } catch (RuntimeException ex) {
        throw new SolverException("Internal solver error.", ex);
    }
}
Also used : IUnifier(mb.nabl2.terms.unification.IUnifier) Function1(org.metaborg.util.functions.Function1) ITerm(mb.nabl2.terms.ITerm) IProgress(org.metaborg.util.task.IProgress) ImmutableSolution(mb.nabl2.solver.ImmutableSolution) Ref(org.metaborg.util.Ref) IConstraint(mb.nabl2.constraints.IConstraint) ScopeGraphComponent(mb.nabl2.solver.components.ScopeGraphComponent) Occurrence(mb.nabl2.scopegraph.terms.Occurrence) IProperties(mb.nabl2.util.collections.IProperties) SolveResult(mb.nabl2.solver.ISolver.SolveResult) SolverCore(mb.nabl2.solver.SolverCore) ICancel(org.metaborg.util.task.ICancel) Value(org.immutables.value.Value) Label(mb.nabl2.scopegraph.terms.Label) Properties(mb.nabl2.util.collections.Properties) IAstConstraint(mb.nabl2.constraints.ast.IAstConstraint) SolverException(mb.nabl2.solver.SolverException) ISolution(mb.nabl2.solver.ISolution) EqualityComponent(mb.nabl2.solver.components.EqualityComponent) EsopScopeGraph(mb.nabl2.scopegraph.esop.reference.EsopScopeGraph) ImmutableSet(com.google.common.collect.ImmutableSet) TermIndex(mb.nabl2.stratego.TermIndex) BaseComponent(mb.nabl2.solver.components.BaseComponent) IScopeGraphConstraint(mb.nabl2.constraints.scopegraph.IScopeGraphConstraint) IEsopScopeGraph(mb.nabl2.scopegraph.esop.IEsopScopeGraph) Set(java.util.Set) Messages(mb.nabl2.solver.messages.Messages) Scope(mb.nabl2.scopegraph.terms.Scope) Sets(com.google.common.collect.Sets) NaBL2DebugConfig(mb.nabl2.config.NaBL2DebugConfig) AstComponent(mb.nabl2.solver.components.AstComponent) Iterables2(org.metaborg.util.iterators.Iterables2) IMessages(mb.nabl2.solver.messages.IMessages) ISolver(mb.nabl2.solver.ISolver) Serial(org.immutables.serial.Serial) SeedResult(mb.nabl2.solver.ISolver.SeedResult) Optional(java.util.Optional) SolverConfig(mb.nabl2.solver.SolverConfig) Collections(java.util.Collections) ScopeGraphComponent(mb.nabl2.solver.components.ScopeGraphComponent) BaseComponent(mb.nabl2.solver.components.BaseComponent) AstComponent(mb.nabl2.solver.components.AstComponent) ISolver(mb.nabl2.solver.ISolver) Optional(java.util.Optional) Label(mb.nabl2.scopegraph.terms.Label) SolverCore(mb.nabl2.solver.SolverCore) IEsopScopeGraph(mb.nabl2.scopegraph.esop.IEsopScopeGraph) Ref(org.metaborg.util.Ref) SolveResult(mb.nabl2.solver.ISolver.SolveResult) Scope(mb.nabl2.scopegraph.terms.Scope) ITerm(mb.nabl2.terms.ITerm) EqualityComponent(mb.nabl2.solver.components.EqualityComponent) SolverException(mb.nabl2.solver.SolverException) Occurrence(mb.nabl2.scopegraph.terms.Occurrence)

Example 3 with ISolver

use of mb.nabl2.solver.ISolver in project nabl by metaborg.

the class SemiIncrementalMultiFileSolver method solveInter.

public ISolution solveInter(ISolution initial, Iterable<? extends ISolution> unitSolutions, IMessageInfo message, 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());
    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);
    // 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);
    // solver components
    final SolverCore core = new SolverCore(config, unifier, fresh, callExternal);
    final AstComponent astSolver = new AstComponent(core, initial.astProperties().melt());
    final BaseComponent baseSolver = new BaseComponent(core);
    final EqualityComponent equalitySolver = new EqualityComponent(core, unifier);
    final NameResolutionComponent nameResolutionSolver = new NameResolutionComponent(core, scopeGraph, nameResolution, initial.declProperties().melt());
    final NameSetsComponent nameSetSolver = new NameSetsComponent(core, scopeGraph, nameResolution);
    final RelationComponent relationSolver = new RelationComponent(core, isRelationComplete, config.getFunctions(), VariantRelations.melt(initial.relations()));
    final SetComponent setSolver = new SetComponent(core, nameSetSolver.nameSets());
    final SymbolicComponent symSolver = new SymbolicComponent(core, initial.symbolic());
    final ControlFlowComponent cfgSolver = new ControlFlowComponent(core, ImmutableFlowSpecSolution.of());
    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 {
        // seed unit solutions
        final java.util.Set<IConstraint> constraints = Sets.newHashSet(initial.constraints());
        final IMessages.Transient messages = initial.messages().melt();
        for (ISolution unitSolution : unitSolutions) {
            seed(astSolver.seed(unitSolution.astProperties(), message), messages, constraints);
            seed(equalitySolver.seed(unitSolution.unifier(), message), messages, constraints);
            final NameResolutionResult nameResult = ImmutableNameResolutionResult.of(unitSolution.scopeGraph(), unitSolution.declProperties()).withResolutionCache(unitSolution.nameResolutionCache());
            seed(nameResolutionSolver.seed(nameResult, message), messages, constraints);
            seed(relationSolver.seed(unitSolution.relations(), message), messages, constraints);
            seed(symSolver.seed(unitSolution.symbolic(), message), messages, constraints);
            seed(cfgSolver.seed(unitSolution.flowSpecSolution(), message), messages, constraints);
            constraints.addAll(unitSolution.constraints());
            messages.addAll(unitSolution.messages());
        }
        // solve constraints
        nameResolutionSolver.update();
        SolveResult solveResult = solver.solve(constraints);
        messages.addAll(solveResult.messages());
        // build result
        IProperties.Immutable<TermIndex, ITerm, ITerm> astResult = astSolver.finish();
        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, astResult, 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);
    }
}
Also used : ControlFlowComponent(mb.nabl2.solver.components.ControlFlowComponent) IUnifier(mb.nabl2.terms.unification.IUnifier) ITerm(mb.nabl2.terms.ITerm) IProgress(org.metaborg.util.task.IProgress) Ref(org.metaborg.util.Ref) IConstraint(mb.nabl2.constraints.IConstraint) IProperties(mb.nabl2.util.collections.IProperties) SolverCore(mb.nabl2.solver.SolverCore) ICancel(org.metaborg.util.task.ICancel) IFlowSpecSolution(mb.nabl2.controlflow.terms.IFlowSpecSolution) Map(java.util.Map) SymbolicComponent(mb.nabl2.solver.components.SymbolicComponent) SolverException(mb.nabl2.solver.SolverException) ISolution(mb.nabl2.solver.ISolution) EqualityComponent(mb.nabl2.solver.components.EqualityComponent) ImmutableFlowSpecSolution(mb.nabl2.controlflow.terms.ImmutableFlowSpecSolution) IEsopScopeGraph(mb.nabl2.scopegraph.esop.IEsopScopeGraph) Predicate1(org.metaborg.util.functions.Predicate1) IMessageInfo(mb.nabl2.constraints.messages.IMessageInfo) Sets(com.google.common.collect.Sets) ImmutableNameResolutionResult(mb.nabl2.solver.components.ImmutableNameResolutionResult) AstComponent(mb.nabl2.solver.components.AstComponent) Iterables2(org.metaborg.util.iterators.Iterables2) NameSetsComponent(mb.nabl2.solver.components.NameSetsComponent) ISolver(mb.nabl2.solver.ISolver) Optional(java.util.Optional) VariantRelations(mb.nabl2.relations.variants.VariantRelations) Function1(org.metaborg.util.functions.Function1) ImmutableSolution(mb.nabl2.solver.ImmutableSolution) SetComponent(mb.nabl2.solver.components.SetComponent) Occurrence(mb.nabl2.scopegraph.terms.Occurrence) IEsopNameResolution(mb.nabl2.scopegraph.esop.IEsopNameResolution) NameResolutionResult(mb.nabl2.solver.components.NameResolutionComponent.NameResolutionResult) SolveResult(mb.nabl2.solver.ISolver.SolveResult) IVariantRelation(mb.nabl2.relations.variants.IVariantRelation) RelationComponent(mb.nabl2.solver.components.RelationComponent) CFGNode(mb.nabl2.controlflow.terms.CFGNode) Label(mb.nabl2.scopegraph.terms.Label) ActiveDeclTypes(mb.nabl2.solver.properties.ActiveDeclTypes) ISymbolicConstraints(mb.nabl2.symbolic.ISymbolicConstraints) ActiveVars(mb.nabl2.solver.properties.ActiveVars) NameResolutionComponent(mb.nabl2.solver.components.NameResolutionComponent) TermIndex(mb.nabl2.stratego.TermIndex) BaseComponent(mb.nabl2.solver.components.BaseComponent) EsopNameResolution(mb.nabl2.scopegraph.esop.lazy.EsopNameResolution) Scope(mb.nabl2.scopegraph.terms.Scope) NaBL2DebugConfig(mb.nabl2.config.NaBL2DebugConfig) PolymorphismComponent(mb.nabl2.solver.components.PolymorphismComponent) IMessages(mb.nabl2.solver.messages.IMessages) HasRelationBuildConstraints(mb.nabl2.solver.properties.HasRelationBuildConstraints) PolySafe(mb.nabl2.solver.properties.PolySafe) SolverConfig(mb.nabl2.solver.SolverConfig) SymbolicComponent(mb.nabl2.solver.components.SymbolicComponent) Label(mb.nabl2.scopegraph.terms.Label) IProperties(mb.nabl2.util.collections.IProperties) IEsopScopeGraph(mb.nabl2.scopegraph.esop.IEsopScopeGraph) ControlFlowComponent(mb.nabl2.solver.components.ControlFlowComponent) Occurrence(mb.nabl2.scopegraph.terms.Occurrence) SolverConfig(mb.nabl2.solver.SolverConfig) ActiveDeclTypes(mb.nabl2.solver.properties.ActiveDeclTypes) AstComponent(mb.nabl2.solver.components.AstComponent) ISolver(mb.nabl2.solver.ISolver) Optional(java.util.Optional) CFGNode(mb.nabl2.controlflow.terms.CFGNode) SolverCore(mb.nabl2.solver.SolverCore) IConstraint(mb.nabl2.constraints.IConstraint) ISolution(mb.nabl2.solver.ISolution) TermIndex(mb.nabl2.stratego.TermIndex) SolveResult(mb.nabl2.solver.ISolver.SolveResult) SetComponent(mb.nabl2.solver.components.SetComponent) BaseComponent(mb.nabl2.solver.components.BaseComponent) IUnifier(mb.nabl2.terms.unification.IUnifier) RelationComponent(mb.nabl2.solver.components.RelationComponent) NameResolutionComponent(mb.nabl2.solver.components.NameResolutionComponent) ActiveVars(mb.nabl2.solver.properties.ActiveVars) PolySafe(mb.nabl2.solver.properties.PolySafe) ISymbolicConstraints(mb.nabl2.symbolic.ISymbolicConstraints) NameSetsComponent(mb.nabl2.solver.components.NameSetsComponent) IMessages(mb.nabl2.solver.messages.IMessages) PolymorphismComponent(mb.nabl2.solver.components.PolymorphismComponent) Ref(org.metaborg.util.Ref) Scope(mb.nabl2.scopegraph.terms.Scope) HasRelationBuildConstraints(mb.nabl2.solver.properties.HasRelationBuildConstraints) ITerm(mb.nabl2.terms.ITerm) EqualityComponent(mb.nabl2.solver.components.EqualityComponent) ImmutableNameResolutionResult(mb.nabl2.solver.components.ImmutableNameResolutionResult) NameResolutionResult(mb.nabl2.solver.components.NameResolutionComponent.NameResolutionResult) SolverException(mb.nabl2.solver.SolverException)

Example 4 with ISolver

use of mb.nabl2.solver.ISolver 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);
    }
}
Also used : VariantRelations(mb.nabl2.relations.variants.VariantRelations) ControlFlowComponent(mb.nabl2.solver.components.ControlFlowComponent) IUnifier(mb.nabl2.terms.unification.IUnifier) Function1(org.metaborg.util.functions.Function1) ITerm(mb.nabl2.terms.ITerm) IProgress(org.metaborg.util.task.IProgress) ImmutableSolution(mb.nabl2.solver.ImmutableSolution) Ref(org.metaborg.util.Ref) IConstraint(mb.nabl2.constraints.IConstraint) SetComponent(mb.nabl2.solver.components.SetComponent) Occurrence(mb.nabl2.scopegraph.terms.Occurrence) IEsopNameResolution(mb.nabl2.scopegraph.esop.IEsopNameResolution) NameResolutionResult(mb.nabl2.solver.components.NameResolutionComponent.NameResolutionResult) SolveResult(mb.nabl2.solver.ISolver.SolveResult) SolverCore(mb.nabl2.solver.SolverCore) IVariantRelation(mb.nabl2.relations.variants.IVariantRelation) RelationComponent(mb.nabl2.solver.components.RelationComponent) CFGNode(mb.nabl2.controlflow.terms.CFGNode) ICancel(org.metaborg.util.task.ICancel) Label(mb.nabl2.scopegraph.terms.Label) IFlowSpecSolution(mb.nabl2.controlflow.terms.IFlowSpecSolution) Map(java.util.Map) SymbolicComponent(mb.nabl2.solver.components.SymbolicComponent) Properties(mb.nabl2.util.collections.Properties) ActiveDeclTypes(mb.nabl2.solver.properties.ActiveDeclTypes) SolverException(mb.nabl2.solver.SolverException) ISolution(mb.nabl2.solver.ISolution) ISymbolicConstraints(mb.nabl2.symbolic.ISymbolicConstraints) EqualityComponent(mb.nabl2.solver.components.EqualityComponent) ActiveVars(mb.nabl2.solver.properties.ActiveVars) NameResolutionComponent(mb.nabl2.solver.components.NameResolutionComponent) BaseComponent(mb.nabl2.solver.components.BaseComponent) ImmutableFlowSpecSolution(mb.nabl2.controlflow.terms.ImmutableFlowSpecSolution) IEsopScopeGraph(mb.nabl2.scopegraph.esop.IEsopScopeGraph) SymbolicConstraints(mb.nabl2.symbolic.SymbolicConstraints) EsopNameResolution(mb.nabl2.scopegraph.esop.lazy.EsopNameResolution) Scope(mb.nabl2.scopegraph.terms.Scope) Predicate1(org.metaborg.util.functions.Predicate1) NaBL2DebugConfig(mb.nabl2.config.NaBL2DebugConfig) Iterables2(org.metaborg.util.iterators.Iterables2) PolymorphismComponent(mb.nabl2.solver.components.PolymorphismComponent) IMessages(mb.nabl2.solver.messages.IMessages) NameSetsComponent(mb.nabl2.solver.components.NameSetsComponent) ISolver(mb.nabl2.solver.ISolver) HasRelationBuildConstraints(mb.nabl2.solver.properties.HasRelationBuildConstraints) PolySafe(mb.nabl2.solver.properties.PolySafe) Optional(java.util.Optional) SolverConfig(mb.nabl2.solver.SolverConfig) SymbolicComponent(mb.nabl2.solver.components.SymbolicComponent) Label(mb.nabl2.scopegraph.terms.Label) IEsopScopeGraph(mb.nabl2.scopegraph.esop.IEsopScopeGraph) ControlFlowComponent(mb.nabl2.solver.components.ControlFlowComponent) Occurrence(mb.nabl2.scopegraph.terms.Occurrence) SolverConfig(mb.nabl2.solver.SolverConfig) ActiveDeclTypes(mb.nabl2.solver.properties.ActiveDeclTypes) ISolver(mb.nabl2.solver.ISolver) Optional(java.util.Optional) CFGNode(mb.nabl2.controlflow.terms.CFGNode) SolverCore(mb.nabl2.solver.SolverCore) SolveResult(mb.nabl2.solver.ISolver.SolveResult) SetComponent(mb.nabl2.solver.components.SetComponent) BaseComponent(mb.nabl2.solver.components.BaseComponent) IUnifier(mb.nabl2.terms.unification.IUnifier) RelationComponent(mb.nabl2.solver.components.RelationComponent) NameResolutionComponent(mb.nabl2.solver.components.NameResolutionComponent) ActiveVars(mb.nabl2.solver.properties.ActiveVars) PolySafe(mb.nabl2.solver.properties.PolySafe) ISymbolicConstraints(mb.nabl2.symbolic.ISymbolicConstraints) NameSetsComponent(mb.nabl2.solver.components.NameSetsComponent) IMessages(mb.nabl2.solver.messages.IMessages) PolymorphismComponent(mb.nabl2.solver.components.PolymorphismComponent) Ref(org.metaborg.util.Ref) Scope(mb.nabl2.scopegraph.terms.Scope) HasRelationBuildConstraints(mb.nabl2.solver.properties.HasRelationBuildConstraints) ITerm(mb.nabl2.terms.ITerm) EqualityComponent(mb.nabl2.solver.components.EqualityComponent) NameResolutionResult(mb.nabl2.solver.components.NameResolutionComponent.NameResolutionResult) SolverException(mb.nabl2.solver.SolverException)

Aggregations

Optional (java.util.Optional)4 NaBL2DebugConfig (mb.nabl2.config.NaBL2DebugConfig)4 IConstraint (mb.nabl2.constraints.IConstraint)4 IEsopScopeGraph (mb.nabl2.scopegraph.esop.IEsopScopeGraph)4 Label (mb.nabl2.scopegraph.terms.Label)4 Occurrence (mb.nabl2.scopegraph.terms.Occurrence)4 Scope (mb.nabl2.scopegraph.terms.Scope)4 ISolution (mb.nabl2.solver.ISolution)4 ISolver (mb.nabl2.solver.ISolver)4 SolveResult (mb.nabl2.solver.ISolver.SolveResult)4 ImmutableSolution (mb.nabl2.solver.ImmutableSolution)4 SolverConfig (mb.nabl2.solver.SolverConfig)4 SolverCore (mb.nabl2.solver.SolverCore)4 SolverException (mb.nabl2.solver.SolverException)4 BaseComponent (mb.nabl2.solver.components.BaseComponent)4 EqualityComponent (mb.nabl2.solver.components.EqualityComponent)4 ITerm (mb.nabl2.terms.ITerm)4 IUnifier (mb.nabl2.terms.unification.IUnifier)4 Ref (org.metaborg.util.Ref)4 Function1 (org.metaborg.util.functions.Function1)4