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);
}
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.");
}));
}
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()));
}
}
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));
}
}
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);
}
}
Aggregations