Skip to content
Snippets Groups Projects
Commit 5b74f2a9 authored by dgelessus's avatar dgelessus
Browse files

Add optional limit parameter to CommandUtils.splitArgs

parent cf407eb7
No related branches found
No related tags found
No related merge requests found
......@@ -25,8 +25,8 @@ import org.slf4j.LoggerFactory;
public final class CommandUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(CommandUtils.class);
public static @NotNull List<@NotNull String> splitArgs(final @NotNull String args) {
final String[] split = args.split("\\h+");
public static @NotNull List<@NotNull String> splitArgs(final @NotNull String args, final int limit) {
final String[] split = args.split("\\h+", limit);
if (split.length == 1 && split[0].isEmpty()) {
return Collections.emptyList();
} else {
......@@ -34,6 +34,10 @@ public final class CommandUtils {
}
}
public static @NotNull List<@NotNull String> splitArgs(final @NotNull String args) {
return splitArgs(args, 0);
}
public static @NotNull Map<@NotNull String, @NotNull String> parsePreferences(final @NotNull List<@NotNull String> args) {
final Map<String, String> preferences = new HashMap<>();
for (final String arg : args) {
......
......@@ -40,9 +40,9 @@ public final class ExecCommand implements LineCommand {
@Override
public @NotNull DisplayData run(final @NotNull ProBKernel kernel, final @NotNull String argString) {
final String[] split = argString.split("\\h", 2);
assert split.length >= 1;
final String opNameOrId = split[0];
final List<String> split = CommandUtils.splitArgs(argString, 2);
assert !split.isEmpty();
final String opNameOrId = split.get(0);
final Trace trace = this.animationSelector.getCurrentTrace();
// Check if the argument is an ID, by searching for a transition with that ID.
......@@ -50,13 +50,13 @@ public final class ExecCommand implements LineCommand {
final Transition op;
if (opt.isPresent()) {
// Transition found, nothing else needs to be done.
if (split.length != 1) {
if (split.size() != 1) {
throw new UserErrorException("Cannot specify a predicate when executing an operation by ID");
}
op = opt.get();
} else {
// Transition not found, assume that the argument is an operation name instead.
final List<String> predicates = split.length < 2 ? Collections.emptyList() : Collections.singletonList(split[1]);
final List<String> predicates = split.size() < 2 ? Collections.emptyList() : Collections.singletonList(split.get(1));
op = trace.getCurrentState().findTransition(opNameOrId, predicates);
}
......
package de.prob2.jupyter.commands;
import java.util.List;
import com.google.inject.Inject;
import de.prob.animator.command.CbcSolveCommand;
......@@ -37,14 +39,14 @@ public final class SolveCommand implements LineCommand {
@Override
public @NotNull DisplayData run(final @NotNull ProBKernel kernel, final @NotNull String argString) {
final String[] split = argString.split("\\h+", 2);
if (split.length != 2) {
final List<String> split = CommandUtils.splitArgs(argString, 2);
if (split.size() != 2) {
throw new UserErrorException("Expected 2 arguments, got 1");
}
final Trace trace = this.animationSelector.getCurrentTrace();
final CbcSolveCommand.Solvers solver;
switch (split[0]) {
switch (split.get(0)) {
case "prob":
solver = CbcSolveCommand.Solvers.PROB;
break;
......@@ -62,9 +64,9 @@ public final class SolveCommand implements LineCommand {
break;
default:
throw new UserErrorException("Unknown solver: " + split[0]);
throw new UserErrorException("Unknown solver: " + split.get(0));
}
final IEvalElement predicate = trace.getModel().parseFormula(split[1], FormulaExpand.EXPAND);
final IEvalElement predicate = trace.getModel().parseFormula(split.get(1), FormulaExpand.EXPAND);
final CbcSolveCommand cmd = new CbcSolveCommand(predicate, solver, this.animationSelector.getCurrentTrace().getCurrentState());
trace.getStateSpace().execute(cmd);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment