Search in sources :

Example 1 with MessageContent

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

the class EqualityComponent method solve.

private Optional<SolveResult> solve(CInequal constraint) {
    ITerm left = constraint.getLeft();
    ITerm right = constraint.getRight();
    if (unifier().areEqual(left, right)) {
        MessageContent content = MessageContent.builder().append(constraint.getLeft().toString()).append(" and ").append(constraint.getRight().toString()).append(" must be inequal, but are not.").build();
        IMessageInfo message = constraint.getMessageInfo().withDefaultContent(content);
        return Optional.of(SolveResult.messages(message));
    } else {
        return unifier().areUnequal(left, right) ? Optional.of(SolveResult.empty()) : Optional.empty();
    }
}
Also used : MessageContent(mb.nabl2.constraints.messages.MessageContent) ITerm(mb.nabl2.terms.ITerm) IMessageInfo(mb.nabl2.constraints.messages.IMessageInfo)

Example 2 with MessageContent

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

the class SetComponent method solve.

private Optional<SolveResult> solve(CDistinct constraint) {
    ITerm setTerm = unifier().findRecursive(constraint.getSet());
    if (!setTerm.isGround()) {
        return Optional.empty();
    }
    Optional<Set<IElement<ITerm>>> maybeSet = evaluator.match(setTerm, unifier());
    if (!(maybeSet.isPresent())) {
        return Optional.empty();
    }
    Multimap<Object, IElement<ITerm>> proj = SetEvaluator.project(maybeSet.get(), constraint.getProjection());
    List<IElement<ITerm>> duplicates = Lists.newArrayList();
    for (Object key : proj.keySet()) {
        Collection<IElement<ITerm>> values = proj.get(key);
        if (values.size() > 1) {
            duplicates.addAll(values);
        }
    }
    if (duplicates.isEmpty()) {
        return Optional.of(SolveResult.empty());
    } else {
        MessageContent content = MessageContent.builder().append(B.newAppl(NAME_OP)).append(" has duplicates in ").append(setTerm).build();
        Iterable<IMessageInfo> messages = makeMessages(constraint.getMessageInfo().withDefaultContent(content), duplicates);
        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 3 with MessageContent

use of mb.nabl2.constraints.messages.MessageContent 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 4 with MessageContent

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

the class EqualityComponent method solve.

// ------------------------------------------------------------------------------------------------------//
private Optional<SolveResult> solve(CEqual constraint) {
    final ITerm left = constraint.getLeft();
    final ITerm right = constraint.getRight();
    try {
        final IUnifier.Transient unifier = this.unifier.get().melt();
        final IUnifier.Immutable unifyResult = unifier.unify(left, right);
        final SolveResult solveResult = ImmutableSolveResult.builder().unifierDiff(unifyResult).build();
        this.unifier.set(unifier.freeze());
        return Optional.of(solveResult);
    } catch (UnificationException ex) {
        final MessageContent content = MessageContent.builder().append("Cannot unify ").append(left).append(" with ").append(right).build();
        final IMessageInfo message = (constraint.getMessageInfo().withDefaultContent(content));
        final SolveResult solveResult = SolveResult.messages(message);
        return Optional.of(solveResult);
    }
}
Also used : MessageContent(mb.nabl2.constraints.messages.MessageContent) IUnifier(mb.nabl2.terms.unification.IUnifier) ImmutableSolveResult(mb.nabl2.solver.ImmutableSolveResult) SolveResult(mb.nabl2.solver.ISolver.SolveResult) ITerm(mb.nabl2.terms.ITerm) UnificationException(mb.nabl2.terms.unification.UnificationException) IMessageInfo(mb.nabl2.constraints.messages.IMessageInfo)

Aggregations

IMessageInfo (mb.nabl2.constraints.messages.IMessageInfo)4 MessageContent (mb.nabl2.constraints.messages.MessageContent)4 ITerm (mb.nabl2.terms.ITerm)4 Set (java.util.Set)2 CEvalSet (mb.nabl2.constraints.sets.CEvalSet)2 IElement (mb.nabl2.sets.IElement)2 SolveResult (mb.nabl2.solver.ISolver.SolveResult)1 ImmutableSolveResult (mb.nabl2.solver.ImmutableSolveResult)1 IUnifier (mb.nabl2.terms.unification.IUnifier)1 UnificationException (mb.nabl2.terms.unification.UnificationException)1