Search in sources :

Example 1 with CDistinct

use of mb.nabl2.constraints.sets.CDistinct 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)

Aggregations

Set (java.util.Set)1 IMessageInfo (mb.nabl2.constraints.messages.IMessageInfo)1 MessageContent (mb.nabl2.constraints.messages.MessageContent)1 CEvalSet (mb.nabl2.constraints.sets.CEvalSet)1 IElement (mb.nabl2.sets.IElement)1 ITerm (mb.nabl2.terms.ITerm)1