Skip to content
Snippets Groups Projects
Commit 4d149828 authored by dgelessus's avatar dgelessus
Browse files

Add :table command to display an expression as a table (closes #7)

parent 5cdf73e5
Branches
Tags
No related merge requests found
%% Cell type:code id: tags:
``` prob
:help :table
```
%% Output
```
:table EXPRESSION
```
Display an expression as a table.
:table EXPRESSION
Display an expression as a table.
%% Cell type:code id: tags:
``` prob
:init
```
%% Output
Machine initialised using operation 0: $initialise_machine()
%% Cell type:code id: tags:
``` prob
:table {1, 2, 3, 4}
```
%% Output
[2018-06-04 14:38:09,730, T+6763] "ProB Output Logger for instance 37c36608" de.prob.cli.ProBInstance.readAndLog(ConsoleListener.java:48): [INFO] finding_trace_from_to(root)
[2018-06-04 14:38:09,734, T+6767] "ProB Output Logger for instance 37c36608" de.prob.cli.ProBInstance.readAndLog(ConsoleListener.java:48): [INFO] 
[2018-06-04 14:38:09,843, T+6876] "ProB Output Logger for instance 37c36608" de.prob.cli.ProBInstance.readAndLog(ConsoleListener.java:48): [INFO] call_command(table,expr_as_table,_85889,[set_extension(pos(2,-1,1,1,1,13),[integer(pos(3,-1,1,2,1,3),1),integer(pos(4,-1,1,5,1,6),2),integer(pos(5,-1,1,8,1,9),3),integer(pos(6,-1,1,11,1,12),4)]),_84193],[])
[2018-06-04 14:38:09,844, T+6877] "ProB Output Logger for instance 37c36608" de.prob.cli.ProBInstance.readAndLog(ConsoleListener.java:48): [INFO] result(list([list([Nr,Elements]),list([1,1]),list([2,2]),list([3,3]),list([4,4])]))
|Nr|Elements|
|---|---|
|1|1|
|2|2|
|3|3|
|4|4|
Nr Elements
1 1
2 2
3 3
4 4
%% Cell type:code id: tags:
``` prob
:table [1, 2, 3, 4]
```
%% Output
[2018-06-04 14:38:09,968, T+7001] "ProB Output Logger for instance 37c36608" de.prob.cli.ProBInstance.readAndLog(ConsoleListener.java:48): [INFO] call_command(table,expr_as_table,_98723,[sequence_extension(pos(2,-1,1,1,1,13),[integer(pos(3,-1,1,2,1,3),1),integer(pos(4,-1,1,5,1,6),2),integer(pos(5,-1,1,8,1,9),3),integer(pos(6,-1,1,11,1,12),4)]),_97041],[])
[2018-06-04 14:38:09,970, T+7003] "ProB Output Logger for instance 37c36608" de.prob.cli.ProBInstance.readAndLog(ConsoleListener.java:48): [INFO] result(list([list([Nr,prj1,prj2]),list([1,1,1]),list([2,2,2]),list([3,3,3]),list([4,4,4])]))
|Nr|prj1|prj2|
|---|---|---|
|1|1|1|
|2|2|2|
|3|3|3|
|4|4|4|
Nr prj1 prj2
1 1 1
2 2 2
3 3 3
4 4 4
%% Cell type:code id: tags:
``` prob
:table {(1, 2, 3), (4, 5, 6), (7, 8, 9)}
```
%% Output
[2018-06-04 14:38:10,074, T+7107] "ProB Output Logger for instance 37c36608" de.prob.cli.ProBInstance.readAndLog(ConsoleListener.java:48): [INFO] call_command(table,expr_as_table,_116255,[set_extension(pos(2,-1,1,1,1,34),[couple(pos(3,-1,1,2,1,11),[integer(pos(4,-1,1,3,1,4),1),integer(pos(5,-1,1,6,1,7),2),integer(pos(6,-1,1,9,1,10),3)]),couple(pos(7,-1,1,13,1,22),[integer(pos(8,-1,1,14,1,15),4),integer(pos(9,-1,1,17,1,18),5),integer(pos(10,-1,1,20,1,21),6)]),couple(pos(11,-1,1,24,1,33),[integer(pos(12,-1,1,25,1,26),7),integer(pos(13,-1,1,28,1,29),8),integer(pos(14,-1,1,31,1,32),9)])]),_112973],[])
[2018-06-04 14:38:10,075, T+7108] "ProB Output Logger for instance 37c36608" de.prob.cli.ProBInstance.readAndLog(ConsoleListener.java:48): [INFO] result(list([list([Nr,prj11,prj12,prj2]),list([1,1,2,3]),list([2,4,5,6]),list([3,7,8,9])]))
|Nr|prj11|prj12|prj2|
|---|---|---|---|
|1|1|2|3|
|2|4|5|6|
|3|7|8|9|
Nr prj11 prj12 prj2
1 1 2 3
2 4 5 6
3 7 8 9
%% Cell type:code id: tags:
``` prob
:table {({}, {})}
```
%% Output
[2018-06-04 14:38:10,310, T+7343] "ProB Output Logger for instance 37c36608" de.prob.cli.ProBInstance.readAndLog(ConsoleListener.java:48): [INFO] call_command(table,expr_as_table,_129729,[set_extension(pos(2,-1,1,1,1,11),[couple(pos(3,-1,1,2,1,10),[empty_set(pos(4,-1,1,3,1,5)),empty_set(pos(5,-1,1,7,1,9))])]),_128283],[])
[2018-06-04 14:38:10,312, T+7345] "ProB Output Logger for instance 37c36608" de.prob.cli.ProBInstance.readAndLog(ConsoleListener.java:48): [INFO] result(list([list([Nr,prj1,prj2]),list([1,{},{}])]))
|Nr|prj1|prj2|
|---|---|---|
|1|{}|{}|
Nr prj1 prj2
1 {} {}
...@@ -30,6 +30,7 @@ import de.prob2.jupyter.commands.PrefCommand; ...@@ -30,6 +30,7 @@ import de.prob2.jupyter.commands.PrefCommand;
import de.prob2.jupyter.commands.PrettyPrintCommand; import de.prob2.jupyter.commands.PrettyPrintCommand;
import de.prob2.jupyter.commands.RenderCommand; import de.prob2.jupyter.commands.RenderCommand;
import de.prob2.jupyter.commands.SolveCommand; import de.prob2.jupyter.commands.SolveCommand;
import de.prob2.jupyter.commands.TableCommand;
import de.prob2.jupyter.commands.TimeCommand; import de.prob2.jupyter.commands.TimeCommand;
import de.prob2.jupyter.commands.VersionCommand; import de.prob2.jupyter.commands.VersionCommand;
...@@ -58,6 +59,7 @@ public final class ProBKernel extends BaseKernel { ...@@ -58,6 +59,7 @@ public final class ProBKernel extends BaseKernel {
this.commands.put(":help", help); this.commands.put(":help", help);
this.commands.put(":version", injector.getInstance(VersionCommand.class)); this.commands.put(":version", injector.getInstance(VersionCommand.class));
this.commands.put(":eval", injector.getInstance(EvalCommand.class)); this.commands.put(":eval", injector.getInstance(EvalCommand.class));
this.commands.put(":table", injector.getInstance(TableCommand.class));
this.commands.put(":solve", injector.getInstance(SolveCommand.class)); this.commands.put(":solve", injector.getInstance(SolveCommand.class));
this.commands.put(":load", injector.getInstance(LoadFileCommand.class)); this.commands.put(":load", injector.getInstance(LoadFileCommand.class));
this.commands.put("::load", injector.getInstance(LoadCellCommand.class)); this.commands.put("::load", injector.getInstance(LoadCellCommand.class));
......
package de.prob2.jupyter.commands;
import java.util.Collections;
import java.util.List;
import com.google.inject.Inject;
import de.prob.animator.command.GetAllTableCommands;
import de.prob.animator.command.GetTableForVisualizationCommand;
import de.prob.animator.domainobjects.DynamicCommandItem;
import de.prob.animator.domainobjects.FormulaExpand;
import de.prob.animator.domainobjects.IEvalElement;
import de.prob.animator.domainobjects.TableData;
import de.prob.statespace.AnimationSelector;
import de.prob.statespace.Trace;
import de.prob2.jupyter.ProBKernel;
import io.github.spencerpark.jupyter.kernel.display.DisplayData;
import org.jetbrains.annotations.NotNull;
public final class TableCommand implements Command {
private final @NotNull AnimationSelector animationSelector;
@Inject
private TableCommand(final @NotNull AnimationSelector animationSelector) {
super();
this.animationSelector = animationSelector;
}
@Override
public @NotNull String getSyntax() {
return ":table EXPRESSION";
}
@Override
public @NotNull String getShortHelp() {
return "Display an expression as a table.";
}
@Override
public @NotNull DisplayData run(final @NotNull ProBKernel kernel, final @NotNull String argString) {
final Trace trace = this.animationSelector.getCurrentTrace();
final IEvalElement formula = trace.getModel().parseFormula(argString, FormulaExpand.EXPAND);
final GetAllTableCommands cmd1 = new GetAllTableCommands(trace.getCurrentState());
trace.getStateSpace().execute(cmd1);
final DynamicCommandItem dc = cmd1.getCommands().stream()
.filter(c -> "expr_as_table".equals(c.getCommand()))
.findAny()
.orElseThrow(AssertionError::new);
final GetTableForVisualizationCommand cmd2 = new GetTableForVisualizationCommand(trace.getCurrentState(), dc, Collections.singletonList(formula));
trace.getStateSpace().execute(cmd2);
final TableData table = cmd2.getTable();
final StringBuilder sbPlain = new StringBuilder();
final StringBuilder sbMarkdown = new StringBuilder();
sbPlain.append(String.join("\t", table.getHeader()));
sbPlain.append('\n');
sbMarkdown.append('|');
sbMarkdown.append(String.join("|", table.getHeader()));
sbMarkdown.append("|\n|");
for (int i = 0; i < table.getHeader().size(); i++) {
sbMarkdown.append("---|");
}
sbMarkdown.append('\n');
for (final List<String> row : table.getRows()) {
sbPlain.append(String.join("\t", row));
sbPlain.append('\n');
sbMarkdown.append('|');
sbMarkdown.append(String.join("|", row));
sbMarkdown.append("|\n");
}
final DisplayData res = new DisplayData(sbPlain.toString());
res.putMarkdown(sbMarkdown.toString());
return res;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment