Search in sources :

Example 6 with SolveResult

use of mb.nabl2.solver.ISolver.SolveResult in project nabl by metaborg.

the class FixedPointSolver method solve.

public SolveResult solve(Iterable<? extends IConstraint> initialConstraints) throws InterruptedException {
    propertiesAddAll(initialConstraints);
    final IMessages.Transient messages = Messages.Transient.of();
    final Multimap<String, String> dependencies = HashMultimap.create();
    final Set<IConstraint> constraints = Sets.newHashSet(initialConstraints);
    boolean progress;
    do {
        progress = false;
        final Set<IConstraint> newConstraints = Sets.newHashSet();
        final Iterator<IConstraint> it = constraints.iterator();
        while (it.hasNext()) {
            cancel.throwIfCancelled();
            final IConstraint constraint = it.next();
            final SolveResult result;
            // property only on other constraints
            propertiesRemove(constraint);
            if ((result = component.apply(constraint).orElse(null)) != null) {
                messages.addAll(result.messages());
                dependencies.putAll(result.dependencies());
                propertiesAddAll(result.constraints());
                newConstraints.addAll(result.constraints());
                updateVars(result.unifierDiff().varSet());
                it.remove();
                stepSubject.onNext(result);
                this.progress.work(1);
                progress |= true;
            } else {
                propertiesAdd(constraint);
            }
        }
        constraints.addAll(newConstraints);
    } while (progress);
    return ImmutableSolveResult.builder().messages(messages.freeze()).dependencies(dependencies).constraints(constraints).build();
}
Also used : ImmutableSolveResult(mb.nabl2.solver.ImmutableSolveResult) SolveResult(mb.nabl2.solver.ISolver.SolveResult) IMessages(mb.nabl2.solver.messages.IMessages) IConstraint(mb.nabl2.constraints.IConstraint)

Example 7 with SolveResult

use of mb.nabl2.solver.ISolver.SolveResult 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 SolveResult

use of mb.nabl2.solver.ISolver.SolveResult in project nabl by metaborg.

the class RelationComponent method solve.

public Optional<SolveResult> solve(CCheckRelation 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 -> {
        if (!isComplete.test(name)) {
            return Optional.empty();
        }
        if (relation(name).contains(left, right)) {
            return Optional.of(SolveResult.empty());
        } else {
            return Optional.empty();
        }
    }, extName -> {
        final ITerm msginfo = MessageInfo.build(c.getMessageInfo());
        return callExternal(extName, left, right, msginfo).map(csTerm -> {
            return Constraints.matchConstraintOrList().match(csTerm, unifier()).map(SolveResult::constraints).orElseThrow(() -> new IllegalArgumentException("Expected list of constraints, got " + csTerm));
        });
    }));
}
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) SolveResult(mb.nabl2.solver.ISolver.SolveResult) ITerm(mb.nabl2.terms.ITerm)

Example 9 with SolveResult

use of mb.nabl2.solver.ISolver.SolveResult 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 10 with SolveResult

use of mb.nabl2.solver.ISolver.SolveResult 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

ITerm (mb.nabl2.terms.ITerm)16 SolveResult (mb.nabl2.solver.ISolver.SolveResult)14 IMessageInfo (mb.nabl2.constraints.messages.IMessageInfo)11 Occurrence (mb.nabl2.scopegraph.terms.Occurrence)9 Optional (java.util.Optional)8 MessageContent (mb.nabl2.constraints.messages.MessageContent)8 SolverCore (mb.nabl2.solver.SolverCore)8 IConstraint (mb.nabl2.constraints.IConstraint)7 Label (mb.nabl2.scopegraph.terms.Label)7 Scope (mb.nabl2.scopegraph.terms.Scope)7 Map (java.util.Map)5 IVariantRelation (mb.nabl2.relations.variants.IVariantRelation)5 VariantRelations (mb.nabl2.relations.variants.VariantRelations)5 IEsopScopeGraph (mb.nabl2.scopegraph.esop.IEsopScopeGraph)5 IUnifier (mb.nabl2.terms.unification.IUnifier)5 Function1 (org.metaborg.util.functions.Function1)5 Predicate1 (org.metaborg.util.functions.Predicate1)5 Iterables2 (org.metaborg.util.iterators.Iterables2)5 Set (java.util.Set)4 NaBL2DebugConfig (mb.nabl2.config.NaBL2DebugConfig)4