Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for state variables in diagrams #1100

Merged
merged 6 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,6 @@
***************/
package org.lflang.diagram.synthesis;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
import com.google.common.collect.Table;
import de.cau.cs.kieler.klighd.DisplayedActionData;
import de.cau.cs.kieler.klighd.SynthesisOption;
import de.cau.cs.kieler.klighd.kgraph.KEdge;
import de.cau.cs.kieler.klighd.kgraph.KLabel;
import de.cau.cs.kieler.klighd.kgraph.KNode;
import de.cau.cs.kieler.klighd.kgraph.KPort;
import de.cau.cs.kieler.klighd.krendering.Colors;
import de.cau.cs.kieler.klighd.krendering.HorizontalAlignment;
import de.cau.cs.kieler.klighd.krendering.KContainerRendering;
import de.cau.cs.kieler.klighd.krendering.KPolyline;
import de.cau.cs.kieler.klighd.krendering.KRectangle;
import de.cau.cs.kieler.klighd.krendering.KRendering;
import de.cau.cs.kieler.klighd.krendering.KRoundedRectangle;
import de.cau.cs.kieler.klighd.krendering.KStyle;
import de.cau.cs.kieler.klighd.krendering.KText;
import de.cau.cs.kieler.klighd.krendering.LineCap;
import de.cau.cs.kieler.klighd.krendering.LineStyle;
import de.cau.cs.kieler.klighd.krendering.ViewSynthesisShared;
import de.cau.cs.kieler.klighd.krendering.extensions.KContainerRenderingExtensions;
import de.cau.cs.kieler.klighd.krendering.extensions.KEdgeExtensions;
import de.cau.cs.kieler.klighd.krendering.extensions.KLabelExtensions;
import de.cau.cs.kieler.klighd.krendering.extensions.KNodeExtensions;
import de.cau.cs.kieler.klighd.krendering.extensions.KPolylineExtensions;
import de.cau.cs.kieler.klighd.krendering.extensions.KPortExtensions;
import de.cau.cs.kieler.klighd.krendering.extensions.KRenderingExtensions;
import de.cau.cs.kieler.klighd.syntheses.AbstractDiagramSynthesis;
import de.cau.cs.kieler.klighd.util.KlighdProperties;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
Expand All @@ -65,7 +33,11 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.inject.Inject;

import org.eclipse.elk.alg.layered.options.EdgeStraighteningStrategy;
import org.eclipse.elk.alg.layered.options.FixedAlignment;
import org.eclipse.elk.alg.layered.options.GreedySwitchType;
Expand All @@ -92,11 +64,13 @@
import org.eclipse.xtext.xbase.lib.Pair;
import org.eclipse.xtext.xbase.lib.StringExtensions;
import org.lflang.ASTUtils;
import org.lflang.InferredType;
import org.lflang.diagram.synthesis.action.CollapseAllReactorsAction;
import org.lflang.diagram.synthesis.action.ExpandAllReactorsAction;
import org.lflang.diagram.synthesis.action.FilterCycleAction;
import org.lflang.diagram.synthesis.action.MemorizingExpandCollapseAction;
import org.lflang.diagram.synthesis.action.ShowCycleAction;
import org.lflang.diagram.synthesis.postprocessor.ReactionPortAdjustment;
import org.lflang.diagram.synthesis.styles.LinguaFrancaShapeExtensions;
import org.lflang.diagram.synthesis.styles.LinguaFrancaStyleExtensions;
import org.lflang.diagram.synthesis.styles.ReactorFigureComponents;
Expand All @@ -119,8 +93,43 @@
import org.lflang.lf.Connection;
import org.lflang.lf.Model;
import org.lflang.lf.Reactor;
import org.lflang.lf.StateVar;
import org.lflang.util.FileUtil;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
import com.google.common.collect.Table;

import de.cau.cs.kieler.klighd.DisplayedActionData;
import de.cau.cs.kieler.klighd.SynthesisOption;
import de.cau.cs.kieler.klighd.kgraph.KEdge;
import de.cau.cs.kieler.klighd.kgraph.KLabel;
import de.cau.cs.kieler.klighd.kgraph.KNode;
import de.cau.cs.kieler.klighd.kgraph.KPort;
import de.cau.cs.kieler.klighd.krendering.Colors;
import de.cau.cs.kieler.klighd.krendering.HorizontalAlignment;
import de.cau.cs.kieler.klighd.krendering.KContainerRendering;
import de.cau.cs.kieler.klighd.krendering.KPolyline;
import de.cau.cs.kieler.klighd.krendering.KRectangle;
import de.cau.cs.kieler.klighd.krendering.KRendering;
import de.cau.cs.kieler.klighd.krendering.KRoundedRectangle;
import de.cau.cs.kieler.klighd.krendering.KStyle;
import de.cau.cs.kieler.klighd.krendering.KText;
import de.cau.cs.kieler.klighd.krendering.LineCap;
import de.cau.cs.kieler.klighd.krendering.LineStyle;
import de.cau.cs.kieler.klighd.krendering.ViewSynthesisShared;
import de.cau.cs.kieler.klighd.krendering.extensions.KContainerRenderingExtensions;
import de.cau.cs.kieler.klighd.krendering.extensions.KEdgeExtensions;
import de.cau.cs.kieler.klighd.krendering.extensions.KLabelExtensions;
import de.cau.cs.kieler.klighd.krendering.extensions.KNodeExtensions;
import de.cau.cs.kieler.klighd.krendering.extensions.KPolylineExtensions;
import de.cau.cs.kieler.klighd.krendering.extensions.KPortExtensions;
import de.cau.cs.kieler.klighd.krendering.extensions.KRenderingExtensions;
import de.cau.cs.kieler.klighd.syntheses.AbstractDiagramSynthesis;
import de.cau.cs.kieler.klighd.util.KlighdProperties;

/**
* Diagram synthesis for Lingua Franca programs.
*
Expand Down Expand Up @@ -193,7 +202,8 @@ public class LinguaFrancaSynthesis extends AbstractDiagramSynthesis<Model> {
public static final SynthesisOption SHOW_REACTOR_HOST = SynthesisOption.createCheckOption("Reactor Host Addresses", true).setCategory(APPEARANCE);
public static final SynthesisOption SHOW_INSTANCE_NAMES = SynthesisOption.createCheckOption("Reactor Instance Names", false).setCategory(APPEARANCE);
public static final SynthesisOption REACTOR_PARAMETER_MODE = SynthesisOption.createChoiceOption("Reactor Parameters", ((List<?>)Conversions.doWrapArray(ReactorParameterDisplayModes.values())), ReactorParameterDisplayModes.NONE).setCategory(APPEARANCE);
public static final SynthesisOption REACTOR_PARAMETER_TABLE_COLS = SynthesisOption.<Integer>createRangeOption("Reactor Parameter Table Columns", 1, 10, 1).setCategory(APPEARANCE);
public static final SynthesisOption SHOW_STATE_VARIABLES = SynthesisOption.createCheckOption("Reactor State Variables", false).setCategory(APPEARANCE);
public static final SynthesisOption REACTOR_BODY_TABLE_COLS = SynthesisOption.<Integer>createRangeOption("Reactor Parameter/Variable Columns", 1, 10, 1).setCategory(APPEARANCE);

/** Synthesis actions */
public static final DisplayedActionData COLLAPSE_ALL = DisplayedActionData.create(CollapseAllReactorsAction.ID, "Hide all Details");
Expand All @@ -220,7 +230,8 @@ public List<SynthesisOption> getDisplayedSynthesisOptions() {
SHOW_REACTOR_HOST,
SHOW_INSTANCE_NAMES,
REACTOR_PARAMETER_MODE,
REACTOR_PARAMETER_TABLE_COLS
SHOW_STATE_VARIABLES,
REACTOR_BODY_TABLE_COLS
);
}

Expand Down Expand Up @@ -337,13 +348,30 @@ private Collection<KNode> createReactorNode(
_kRenderingExtensions.to(
_kRenderingExtensions.from(
_kRenderingExtensions.setGridPlacementData(rectangle),
_kRenderingExtensions.LEFT, 8, 0,
_kRenderingExtensions.LEFT, 6, 0,
_kRenderingExtensions.TOP, 0, 0),
_kRenderingExtensions.RIGHT, 8, 0,
_kRenderingExtensions.RIGHT, 6, 0,
_kRenderingExtensions.BOTTOM, 4, 0);
_kRenderingExtensions.setHorizontalAlignment(rectangle, HorizontalAlignment.LEFT);
addParameterList(rectangle, reactorInstance.parameters);
}

if (getBooleanValue(SHOW_STATE_VARIABLES)) {
var variables = ASTUtils.allStateVars(reactor);
if (!variables.isEmpty()) {
KRectangle rectangle = _kContainerRenderingExtensions.addRectangle(figure);
_kRenderingExtensions.setInvisible(rectangle, true);
_kRenderingExtensions.to(
_kRenderingExtensions.from(
_kRenderingExtensions.setGridPlacementData(rectangle),
_kRenderingExtensions.LEFT, 6, 0,
_kRenderingExtensions.TOP, 0, 0),
_kRenderingExtensions.RIGHT, 6, 0,
_kRenderingExtensions.BOTTOM, 4, 0);
_kRenderingExtensions.setHorizontalAlignment(rectangle, HorizontalAlignment.LEFT);
addStateVariableList(rectangle, variables);
}
}

if (reactorInstance.recursive) {
nodes.add(addErrorComment(node, TEXT_ERROR_RECURSIVE));
Expand Down Expand Up @@ -404,22 +432,49 @@ private Collection<KNode> createReactorNode(
_kRenderingExtensions.to(
_kRenderingExtensions.from(
_kRenderingExtensions.setGridPlacementData(rectangle),
_kRenderingExtensions.LEFT, 8, 0,
_kRenderingExtensions.LEFT, 6, 0,
_kRenderingExtensions.TOP, 0, 0),
_kRenderingExtensions.RIGHT, 8, 0,
_kRenderingExtensions.RIGHT, 6, 0,
_kRenderingExtensions.BOTTOM, 4, 0);
} else {
_kRenderingExtensions.to(
_kRenderingExtensions.from(
_kRenderingExtensions.setGridPlacementData(rectangle),
_kRenderingExtensions.LEFT, 8, 0,
_kRenderingExtensions.LEFT, 6, 0,
_kRenderingExtensions.TOP, 4, 0),
_kRenderingExtensions.RIGHT, 8, 0,
_kRenderingExtensions.RIGHT, 6, 0,
_kRenderingExtensions.BOTTOM, 0, 0);
}
_kRenderingExtensions.setHorizontalAlignment(rectangle, HorizontalAlignment.LEFT);
addParameterList(rectangle, instance.parameters);
}

if (getBooleanValue(SHOW_STATE_VARIABLES)) {
var variables = ASTUtils.allStateVars(reactor);
if (!variables.isEmpty()) {
KRectangle rectangle = _kContainerRenderingExtensions.addRectangle(comps.getReactor());
_kRenderingExtensions.setInvisible(rectangle, true);
if (!getBooleanValue(SHOW_HYPERLINKS)) {
_kRenderingExtensions.to(
_kRenderingExtensions.from(
_kRenderingExtensions.setGridPlacementData(rectangle),
_kRenderingExtensions.LEFT, 6, 0,
_kRenderingExtensions.TOP, 0, 0),
_kRenderingExtensions.RIGHT, 6, 0,
_kRenderingExtensions.BOTTOM, 4, 0);
} else {
_kRenderingExtensions.to(
_kRenderingExtensions.from(
_kRenderingExtensions.setGridPlacementData(rectangle),
_kRenderingExtensions.LEFT, 6, 0,
_kRenderingExtensions.TOP, 4, 0),
_kRenderingExtensions.RIGHT, 6, 0,
_kRenderingExtensions.BOTTOM, 0, 0);
}
_kRenderingExtensions.setHorizontalAlignment(rectangle, HorizontalAlignment.LEFT);
addStateVariableList(rectangle, variables);
}
}

if (instance.recursive) {
comps.getFigures().forEach(_linguaFrancaStyleExtensions::errorStyle);
Expand Down Expand Up @@ -717,7 +772,15 @@ private Collection<KNode> transformReactorNetwork(
setLayoutOption(node, CoreOptions.PORT_CONSTRAINTS, PortConstraints.FIXED_SIDE);
setLayoutOption(node, LayeredOptions.POSITION, new KVector(0, idx + 1)); // try order reactions vertically if in one layer (+1 to account for startup)

_linguaFrancaShapeExtensions.addReactionFigure(node, reaction);
var figure = _linguaFrancaShapeExtensions.addReactionFigure(node, reaction);

int inputSize = Stream.concat(reaction.triggers.stream(), reaction.sources.stream()).collect(Collectors.toSet()).size();
int outputSize = reaction.effects.size();
if (!getBooleanValue(REACTIONS_USE_HYPEREDGES) && (inputSize > 1 || outputSize > 1)) {
// If this node will have more than one input/output port, the port positions must be adjusted to the
// pointy shape. However, this is only possible after the layout.
ReactionPortAdjustment.apply(node, figure);
}

// connect input
KPort port = null;
Expand All @@ -727,9 +790,7 @@ private Collection<KNode> transformReactorNetwork(
port = addInvisiblePort(node);
setLayoutOption(port, CoreOptions.PORT_SIDE, PortSide.WEST);

int triggersSize = reaction.triggers != null ? reaction.triggers.size() : 0;
int sourcesSize = reaction.sources != null ? reaction.sources.size() : 0;
if (getBooleanValue(REACTIONS_USE_HYPEREDGES) || triggersSize + sourcesSize == 1) {
if (getBooleanValue(REACTIONS_USE_HYPEREDGES) || inputSize == 1) {
// manual adjustment disabling automatic one
setLayoutOption(port, CoreOptions.PORT_BORDER_OFFSET,
(double) -LinguaFrancaShapeExtensions.REACTION_POINTINESS);
Expand Down Expand Up @@ -776,9 +837,7 @@ private Collection<KNode> transformReactorNetwork(
port = addInvisiblePort(node);
setLayoutOption(port, CoreOptions.PORT_SIDE, PortSide.WEST);

int triggersSize = reaction.triggers != null ? reaction.triggers.size() : 0;
int sourcesSize = reaction.sources != null ? reaction.sources.size() : 0;
if (getBooleanValue(REACTIONS_USE_HYPEREDGES) || triggersSize + sourcesSize == 1) {
if (getBooleanValue(REACTIONS_USE_HYPEREDGES) || inputSize == 1) {
// manual adjustment disabling automatic one
setLayoutOption(port, CoreOptions.PORT_BORDER_OFFSET,
(double) -LinguaFrancaShapeExtensions.REACTION_POINTINESS);
Expand Down Expand Up @@ -1040,7 +1099,7 @@ private String createReactorLabel(ReactorInstance reactorInstance) {
private void addParameterList(KContainerRendering container, List<ParameterInstance> parameters) {
int cols = 1;
try {
cols = getIntValue(REACTOR_PARAMETER_TABLE_COLS);
cols = getIntValue(REACTOR_BODY_TABLE_COLS);
} catch (Exception e) {} // ignore
if (cols > parameters.size()) {
cols = parameters.size();
Expand All @@ -1050,13 +1109,14 @@ private void addParameterList(KContainerRendering container, List<ParameterInsta
KText paramText = _kContainerRenderingExtensions.addText(container, createParameterLabel(param, true));
_kRenderingExtensions.setFontSize(paramText, 8);
_kRenderingExtensions.setHorizontalAlignment(paramText, HorizontalAlignment.LEFT);
_kRenderingExtensions.setSurroundingSpaceGrid(paramText, 2, 0, 0, 0);
}
}

private String createParameterLabel(ParameterInstance param, boolean bullet) {
StringBuilder b = new StringBuilder();
if (bullet) {
b.append("\u2022 ");
b.append("\u2219 ");
}
b.append(param.getName());
String t = param.type.toText();
Expand All @@ -1071,6 +1131,42 @@ private String createParameterLabel(ParameterInstance param, boolean bullet) {
return b.toString();
}

private void addStateVariableList(KContainerRendering container, List<StateVar> variables) {
int cols = 1;
try {
cols = getIntValue(REACTOR_BODY_TABLE_COLS);
} catch (Exception e) {} // ignore
if (cols > variables.size()) {
cols = variables.size();
}
_kContainerRenderingExtensions.setGridPlacement(container, cols);
for (var variable : variables) {
KText varText = _kContainerRenderingExtensions.addText(container, createStateVariableLabel(variable, true));
_kRenderingExtensions.setFontSize(varText, 8);
_kRenderingExtensions.setHorizontalAlignment(varText, HorizontalAlignment.LEFT);
_kRenderingExtensions.setSurroundingSpaceGrid(varText, 2, 0, 0, 0);
associateWith(varText, variable);
}
}

private String createStateVariableLabel(StateVar variable, boolean bullet) {
StringBuilder b = new StringBuilder();
if (bullet) {
b.append("\u229a ");
}
b.append(variable.getName());
if (variable.getType() != null) {
var t = InferredType.fromAST(variable.getType());
b.append(":").append(t.toText());
}
if (!IterableExtensions.isNullOrEmpty(variable.getInit())) {
b.append("(");
b.append(IterableExtensions.join(variable.getInit(), ", ", _utilityExtensions::toText));
b.append(")");
}
return b.toString();
}

private KEdge createDelayEdge(Object associate) {
KEdge edge = _kEdgeExtensions.createEdge();
associateWith(edge, associate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,7 @@ public KPolygon addReactionFigure(KNode node, ReactionInstance reaction) {
_kRenderingExtensions.createKPosition(LEFT, REACTION_POINTINESS, 0, BOTTOM, 0, 0.5f)
)
);

// For a shape like this, ports can only positioned correctly after the layout.
ReactionPortAdjustment.apply(node, baseShape);


KRectangle contentContainer = _kContainerRenderingExtensions.addRectangle(baseShape);
associateWith(contentContainer, reaction);
_kRenderingExtensions.setInvisible(contentContainer, true);
Expand Down
Loading