Skip to content
Snippets Groups Projects
Commit 4000c4e9 authored by dgelessus's avatar dgelessus
Browse files

Add cell command to load a B machine from the notebook

parent f6c5903d
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.inject.Inject;
import com.google.inject.Injector;
import de.prob.animator.domainobjects.AbstractEvalResult;
import de.prob.animator.domainobjects.FormulaExpand;
......@@ -21,6 +22,7 @@ import de.prob2.jupyter.commands.CommandExecutionException;
import de.prob2.jupyter.commands.EchoCellCommand;
import de.prob2.jupyter.commands.HelpCommand;
import de.prob2.jupyter.commands.LineCommand;
import de.prob2.jupyter.commands.LoadCellCommand;
import de.prob2.jupyter.commands.NoSuchCommandException;
import io.github.spencerpark.jupyter.kernel.BaseKernel;
......@@ -38,7 +40,7 @@ public final class ProBKernel extends BaseKernel {
private @NotNull Trace trace;
@Inject
private ProBKernel(final @NotNull ClassicalBFactory classicalBFactory) {
private ProBKernel(final @NotNull Injector injector, final @NotNull ClassicalBFactory classicalBFactory) {
super();
this.lineCommands = new HashMap<>();
......@@ -48,6 +50,7 @@ public final class ProBKernel extends BaseKernel {
this.cellCommands = new HashMap<>();
this.cellCommands.put("::echo", new EchoCellCommand());
this.cellCommands.put("::load", injector.getInstance(LoadCellCommand.class));
this.trace = new Trace(classicalBFactory.create("MACHINE repl END").load());
}
......@@ -60,6 +63,14 @@ public final class ProBKernel extends BaseKernel {
return Collections.unmodifiableMap(this.lineCommands);
}
public @NotNull Trace getTrace() {
return this.trace;
}
public void setTrace(final @NotNull Trace trace) {
this.trace = trace;
}
@Override
public @NotNull String getBanner() {
return "ProB Interactive Expression and Predicate Evaluator (on Jupyter)\nType \":help\" for more information.";
......
package de.prob2.jupyter.commands;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.inject.Inject;
import de.prob.scripting.ClassicalBFactory;
import de.prob.statespace.Trace;
import de.prob2.jupyter.ProBKernel;
import io.github.spencerpark.jupyter.messages.DisplayData;
import org.jetbrains.annotations.NotNull;
public final class LoadCellCommand implements CellCommand {
private final @NotNull ClassicalBFactory classicalBFactory;
@Inject
private LoadCellCommand(final @NotNull ClassicalBFactory classicalBFactory) {
super();
this.classicalBFactory = classicalBFactory;
}
@Override
public @NotNull String getSyntax() {
return "::load\nMACHINE\n...\nEND";
}
@Override
public @NotNull String getShortHelp() {
return "Load the machine source code from the body.";
}
@Override
public @NotNull DisplayData run(final @NotNull ProBKernel kernel, final @NotNull String name, final @NotNull List<@NotNull String> args, final @NotNull String body) {
final Map<String, String> preferences = new HashMap<>();
for (final String arg : args) {
final String[] split = arg.split("=", 2);
if (split.length == 1) {
throw new CommandExecutionException(name, "Missing value for preference " + split[0]);
}
preferences.put(split[0], split[1]);
}
kernel.setTrace(new Trace(this.classicalBFactory.create(body).load(preferences)));
return new DisplayData("Loaded machine: " + kernel.getTrace().getModel());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment