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