Search in sources :

Example 1 with IElement

use of mb.nabl2.sets.IElement 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 2 with IElement

use of mb.nabl2.sets.IElement in project nabl by metaborg.

the class SetComponent method solve.

private Optional<SolveResult> solve(CEvalSet 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();
    }
    List<ITerm> set = maybeSet.get().stream().map(i -> unifier().findRecursive(i.getValue())).collect(Collectors.toList());
    return Optional.of(SolveResult.constraints(ImmutableCEqual.of(constraint.getResult(), B.newList(set), constraint.getMessageInfo())));
}
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) CEvalSet(mb.nabl2.constraints.sets.CEvalSet) Set(java.util.Set) ITerm(mb.nabl2.terms.ITerm)

Example 3 with IElement

use of mb.nabl2.sets.IElement 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 4 with IElement

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

Aggregations

Set (java.util.Set)4 IMessageInfo (mb.nabl2.constraints.messages.IMessageInfo)4 MessageContent (mb.nabl2.constraints.messages.MessageContent)4 CEvalSet (mb.nabl2.constraints.sets.CEvalSet)4 IElement (mb.nabl2.sets.IElement)4 ITerm (mb.nabl2.terms.ITerm)4 HashMultimap (com.google.common.collect.HashMultimap)2 Lists (com.google.common.collect.Lists)2 Multimap (com.google.common.collect.Multimap)2 Collection (java.util.Collection)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 ImmutableCEqual (mb.nabl2.constraints.equality.ImmutableCEqual)2 ImmutableMessageInfo (mb.nabl2.constraints.messages.ImmutableMessageInfo)2 CDistinct (mb.nabl2.constraints.sets.CDistinct)2 CSubsetEq (mb.nabl2.constraints.sets.CSubsetEq)2 ISetConstraint (mb.nabl2.constraints.sets.ISetConstraint)2 SetEvaluator (mb.nabl2.sets.SetEvaluator)2 ASolver (mb.nabl2.solver.ASolver)2