Search in sources :

Example 6 with IMessageInfo

use of mb.nabl2.constraints.messages.IMessageInfo in project nabl by metaborg.

the class NameResolutionComponent method solve.

private Optional<SolveResult> solve(CAssoc a) {
    final ITerm declTerm = unifier().findRecursive(a.getDeclaration());
    if (!declTerm.isGround()) {
        return Optional.empty();
    }
    final Occurrence decl = Occurrence.matcher().match(declTerm, unifier()).orElseThrow(() -> new TypeException("Expected an occurrence as first argument to " + a));
    final Label label = a.getLabel();
    final List<Scope> scopes = Lists.newArrayList(scopeGraph.getExportEdges().get(decl, label));
    final SolveResult result;
    switch(scopes.size()) {
        case 0:
            {
                IMessageInfo message = a.getMessageInfo().withDefaultContent(MessageContent.builder().append(decl).append(" has no ").append(label).append(" associated scope.").build());
                result = SolveResult.messages(message);
                break;
            }
        case 1:
            {
                result = SolveResult.constraints(ImmutableCEqual.of(a.getScope(), scopes.get(0), a.getMessageInfo()));
                break;
            }
        default:
            {
                IMessageInfo message = a.getMessageInfo().withDefaultContent(MessageContent.builder().append(decl).append(" has multiple ").append(label).append(" associated scope.").build());
                result = SolveResult.messages(message);
                break;
            }
    }
    return Optional.of(result);
}
Also used : SolveResult(mb.nabl2.solver.ISolver.SolveResult) ImmutableSolveResult(mb.nabl2.solver.ImmutableSolveResult) Scope(mb.nabl2.scopegraph.terms.Scope) Label(mb.nabl2.scopegraph.terms.Label) ITerm(mb.nabl2.terms.ITerm) TypeException(mb.nabl2.solver.TypeException) Occurrence(mb.nabl2.scopegraph.terms.Occurrence) IMessageInfo(mb.nabl2.constraints.messages.IMessageInfo)

Example 7 with IMessageInfo

use of mb.nabl2.constraints.messages.IMessageInfo in project nabl by metaborg.

the class RelationComponent method solve.

// ------------------------------------------------------------------------------------------------------//
public Optional<SolveResult> solve(CBuildRelation c) {
    final ITerm left = unifier().findRecursive(c.getLeft());
    final ITerm right = unifier().findRecursive(c.getRight());
    if (!(left.isGround() && right.isGround())) {
        return Optional.empty();
    }
    return c.getRelation().match(IRelationName.Cases.of(// @formatter:off
    name -> {
        try {
            relation(name).add(left, right);
        } catch (RelationException e) {
            final IMessageInfo message = c.getMessageInfo().withDefaultContent(MessageContent.of(e.getMessage()));
            return Optional.of(SolveResult.messages(message));
        }
        return Optional.of(SolveResult.empty());
    }, extName -> {
        throw new IllegalArgumentException("Cannot add entries to external relations.");
    }));
}
Also used : VariantRelations(mb.nabl2.relations.variants.VariantRelations) RelationFunctions(mb.nabl2.relations.terms.FunctionName.RelationFunctions) ITerm(mb.nabl2.terms.ITerm) ImmutableCEqual(mb.nabl2.constraints.equality.ImmutableCEqual) PartialFunction1(org.metaborg.util.functions.PartialFunction1) SolveResult(mb.nabl2.solver.ISolver.SolveResult) SolverCore(mb.nabl2.solver.SolverCore) IVariantRelation(mb.nabl2.relations.variants.IVariantRelation) CCheckRelation(mb.nabl2.constraints.relations.CCheckRelation) RelationException(mb.nabl2.relations.RelationException) IRelation(mb.nabl2.relations.IRelation) Map(java.util.Map) ASolver(mb.nabl2.solver.ASolver) M(mb.nabl2.terms.matching.TermMatch.M) Constraints(mb.nabl2.constraints.Constraints) CEvalFunction(mb.nabl2.constraints.relations.CEvalFunction) FunctionUndefinedException(mb.nabl2.solver.FunctionUndefinedException) Predicate1(org.metaborg.util.functions.Predicate1) Maps(com.google.common.collect.Maps) IMessageInfo(mb.nabl2.constraints.messages.IMessageInfo) MessageContent(mb.nabl2.constraints.messages.MessageContent) IRelationConstraint(mb.nabl2.constraints.relations.IRelationConstraint) IFunctionName(mb.nabl2.relations.IFunctionName) MessageInfo(mb.nabl2.constraints.messages.MessageInfo) CBuildRelation(mb.nabl2.constraints.relations.CBuildRelation) SeedResult(mb.nabl2.solver.ISolver.SeedResult) Entry(java.util.Map.Entry) Optional(java.util.Optional) IRelationName(mb.nabl2.relations.IRelationName) RelationException(mb.nabl2.relations.RelationException) ITerm(mb.nabl2.terms.ITerm) IMessageInfo(mb.nabl2.constraints.messages.IMessageInfo)

Example 8 with IMessageInfo

use of mb.nabl2.constraints.messages.IMessageInfo in project nabl by metaborg.

the class SetComponent method makeMessages.

private Iterable<IMessageInfo> makeMessages(IMessageInfo template, Collection<IElement<ITerm>> elements) {
    boolean nameOrigin = M.appl0(NAME_OP).match(template.getOriginTerm(), unifier()).isPresent();
    if (nameOrigin && !elements.isEmpty()) {
        return elements.stream().<IMessageInfo>map(e -> {
            Function1<ITerm, ITerm> f = T.sometd(t -> M.appl0(NAME_OP, a -> e.getValue()).match(t, unifier()));
            return ImmutableMessageInfo.of(template.getKind(), template.getContent().apply(f), e.getPosition());
        }).collect(Collectors.toList());
    } else {
        ITerm es = B.newList(elements.stream().map(e -> e.getValue()).collect(Collectors.toList()));
        Function1<ITerm, ITerm> f = T.sometd(t -> M.appl0(NAME_OP, a -> es).match(t, unifier()));
        return Iterables2.singleton(ImmutableMessageInfo.of(template.getKind(), template.getContent().apply(f), template.getOriginTerm()));
    }
}
Also used : ISetConstraint(mb.nabl2.constraints.sets.ISetConstraint) Function1(org.metaborg.util.functions.Function1) ITerm(mb.nabl2.terms.ITerm) ImmutableCEqual(mb.nabl2.constraints.equality.ImmutableCEqual) Multimap(com.google.common.collect.Multimap) SolveResult(mb.nabl2.solver.ISolver.SolveResult) SolverCore(mb.nabl2.solver.SolverCore) CEvalSet(mb.nabl2.constraints.sets.CEvalSet) HashMultimap(com.google.common.collect.HashMultimap) Lists(com.google.common.collect.Lists) SetEvaluator(mb.nabl2.sets.SetEvaluator) T(mb.nabl2.terms.matching.Transform.T) Unit(org.metaborg.util.unit.Unit) B(mb.nabl2.terms.build.TermBuild.B) ImmutableMessageInfo(mb.nabl2.constraints.messages.ImmutableMessageInfo) ASolver(mb.nabl2.solver.ASolver) M(mb.nabl2.terms.matching.TermMatch.M) Collection(java.util.Collection) Set(java.util.Set) CDistinct(mb.nabl2.constraints.sets.CDistinct) Collectors(java.util.stream.Collectors) IMessageInfo(mb.nabl2.constraints.messages.IMessageInfo) MessageContent(mb.nabl2.constraints.messages.MessageContent) CSubsetEq(mb.nabl2.constraints.sets.CSubsetEq) Iterables2(org.metaborg.util.iterators.Iterables2) List(java.util.List) Optional(java.util.Optional) IMatcher(mb.nabl2.terms.matching.TermMatch.IMatcher) IElement(mb.nabl2.sets.IElement) Function1(org.metaborg.util.functions.Function1) ITerm(mb.nabl2.terms.ITerm)

Example 9 with IMessageInfo

use of mb.nabl2.constraints.messages.IMessageInfo in project nabl by metaborg.

the class SetComponent method solve.

// ------------------------------------------------------------------------------------------------------//
private Optional<SolveResult> solve(CSubsetEq constraint) {
    ITerm left = unifier().findRecursive(constraint.getLeft());
    ITerm right = unifier().findRecursive(constraint.getRight());
    if (!left.isGround() && right.isGround()) {
        return Optional.empty();
    }
    Optional<Set<IElement<ITerm>>> maybeLeftSet = evaluator.match(left, unifier());
    Optional<Set<IElement<ITerm>>> maybeRightSet = evaluator.match(right, unifier());
    if (!(maybeLeftSet.isPresent() && maybeRightSet.isPresent())) {
        return Optional.empty();
    }
    Multimap<Object, IElement<ITerm>> leftProj = SetEvaluator.project(maybeLeftSet.get(), constraint.getProjection());
    Multimap<Object, IElement<ITerm>> rightProj = SetEvaluator.project(maybeRightSet.get(), constraint.getProjection());
    Multimap<Object, IElement<ITerm>> result = HashMultimap.create();
    result.putAll(leftProj);
    result.keySet().removeAll(rightProj.keySet());
    if (result.isEmpty()) {
        return Optional.of(SolveResult.empty());
    } else {
        MessageContent content = MessageContent.builder().append(B.newAppl(NAME_OP)).append(" not in ").append(right).build();
        Iterable<IMessageInfo> messages = makeMessages(constraint.getMessageInfo().withDefaultContent(content), result.values());
        return Optional.of(SolveResult.messages(messages));
    }
}
Also used : MessageContent(mb.nabl2.constraints.messages.MessageContent) IElement(mb.nabl2.sets.IElement) CEvalSet(mb.nabl2.constraints.sets.CEvalSet) Set(java.util.Set) ITerm(mb.nabl2.terms.ITerm) IMessageInfo(mb.nabl2.constraints.messages.IMessageInfo)

Example 10 with IMessageInfo

use of mb.nabl2.constraints.messages.IMessageInfo 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)

Aggregations

IMessageInfo (mb.nabl2.constraints.messages.IMessageInfo)10 ITerm (mb.nabl2.terms.ITerm)10 MessageContent (mb.nabl2.constraints.messages.MessageContent)7 SolveResult (mb.nabl2.solver.ISolver.SolveResult)6 Optional (java.util.Optional)4 Map (java.util.Map)3 Set (java.util.Set)3 CEvalSet (mb.nabl2.constraints.sets.CEvalSet)3 Scope (mb.nabl2.scopegraph.terms.Scope)3 IElement (mb.nabl2.sets.IElement)3 SolverCore (mb.nabl2.solver.SolverCore)3 HashMultimap (com.google.common.collect.HashMultimap)2 Lists (com.google.common.collect.Lists)2 Maps (com.google.common.collect.Maps)2 Multimap (com.google.common.collect.Multimap)2 Sets (com.google.common.collect.Sets)2 Collection (java.util.Collection)2 List (java.util.List)2 NaBL2DebugConfig (mb.nabl2.config.NaBL2DebugConfig)2 ImmutableCEqual (mb.nabl2.constraints.equality.ImmutableCEqual)2