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

Adjustable port sides for reactors #1807

Merged
merged 5 commits into from
Jun 10, 2023
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
8 changes: 8 additions & 0 deletions core/src/main/java/org/lflang/AttributeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ public static String getIconPath(EObject node) {
return getAttributeValue(node, "icon");
}

/**
* Return the {@code @side} annotation for the given node (presumably a port) or null if there is
* no such annotation.
*/
public static String getPortSide(EObject node) {
return getAttributeValue(node, "side");
}

/**
* Return the {@code @enclave} attribute annotated on the given node.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
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.postprocessor.ReactorPortAdjustment;
import org.lflang.diagram.synthesis.styles.LinguaFrancaShapeExtensions;
import org.lflang.diagram.synthesis.styles.LinguaFrancaStyleExtensions;
import org.lflang.diagram.synthesis.styles.ReactorFigureComponents;
Expand Down Expand Up @@ -163,6 +164,8 @@ public class LinguaFrancaSynthesis extends AbstractDiagramSynthesis<Model> {
"org.lflang.linguafranca.diagram.synthesis.reactor.recursive.instantiation", false);
public static final Property<Boolean> REACTOR_HAS_BANK_PORT_OFFSET =
new Property<>("org.lflang.linguafranca.diagram.synthesis.reactor.bank.offset", false);
public static final Property<Boolean> REACTOR_MULTIPORT =
new Property<>("org.lflang.linguafranca.diagram.synthesis.reactor.multiport", false);
public static final Property<Boolean> REACTOR_INPUT =
new Property<>("org.lflang.linguafranca.diagram.synthesis.reactor.input", false);
public static final Property<Boolean> REACTOR_OUTPUT =
Expand Down Expand Up @@ -244,6 +247,8 @@ public class LinguaFrancaSynthesis extends AbstractDiagramSynthesis<Model> {
SynthesisOption.<Integer>createRangeOption("Reactor Parameter/Variable Columns", 1, 10, 1)
.setCategory(APPEARANCE);

public static final SynthesisOption FIXED_PORT_SIDE =
SynthesisOption.createCheckOption("Fixed Port Sides", true).setCategory(LAYOUT);
public static final SynthesisOption SPACING =
SynthesisOption.<Integer>createRangeOption("Spacing (%)", 0, 150, 5, 75).setCategory(LAYOUT);

Expand Down Expand Up @@ -280,6 +285,7 @@ public List<SynthesisOption> getDisplayedSynthesisOptions() {
SHOW_STATE_VARIABLES,
REACTOR_BODY_TABLE_COLS,
LAYOUT,
FIXED_PORT_SIDE,
LayoutPostProcessing.MODEL_ORDER,
SPACING);
}
Expand Down Expand Up @@ -482,6 +488,11 @@ private Collection<KNode> createReactorNode(
}
_reactorIcons.handleIcon(comps.getReactor(), reactor, false);

if (!getBooleanValue(FIXED_PORT_SIDE)) {
// Port figures will need post-processing to fix IO indication if portside is not fixed
ReactorPortAdjustment.apply(node, comps.getFigures());
}

if (getBooleanValue(SHOW_HYPERLINKS)) {
// Collapse button
KText button =
Expand Down Expand Up @@ -608,6 +619,11 @@ private Collection<KNode> createReactorNode(
}
_reactorIcons.handleIcon(comps.getReactor(), reactor, true);

if (!getBooleanValue(FIXED_PORT_SIDE)) {
// Port figures will need post-processing to fix IO indication if portside is not fixed
ReactorPortAdjustment.apply(node, comps.getFigures());
}

if (getBooleanValue(SHOW_HYPERLINKS)) {
// Expand button
if (_utilityExtensions.hasContent(instance) && !instance.recursive) {
Expand Down Expand Up @@ -729,15 +745,22 @@ public KNode configureReactorNodeLayout(KNode node, boolean main) {
CoreOptions.NODE_SIZE_CONSTRAINTS,
EnumSet.of(SizeConstraint.MINIMUM_SIZE, SizeConstraint.PORTS));

// Allows to freely shuffle ports on each side
setLayoutOption(node, CoreOptions.PORT_CONSTRAINTS, PortConstraints.FIXED_SIDE);
// Adjust port label spacing to be closer to edge but not overlap with port figure
// TODO: Add PortLabelPlacement.NEXT_TO_PORT_IF_POSSIBLE back into the configuration, as soon as
// ELK provides a fix for LF issue #1273
if (getBooleanValue(FIXED_PORT_SIDE)) {
// Allows to freely shuffle ports on each side
setLayoutOption(node, CoreOptions.PORT_CONSTRAINTS, PortConstraints.FIXED_SIDE);
} else {
// Ports are no longer fixed based on input or output
setLayoutOption(node, CoreOptions.PORT_CONSTRAINTS, PortConstraints.FREE);
}

setLayoutOption(
node,
CoreOptions.PORT_LABELS_PLACEMENT,
EnumSet.of(PortLabelPlacement.ALWAYS_OTHER_SAME_SIDE, PortLabelPlacement.OUTSIDE));
// TODO: Add PortLabelPlacement.NEXT_TO_PORT_IF_POSSIBLE back into the configuration, as soon as
// ELK provides a fix for LF issue #1273

// Adjust port label spacing to be closer to edge but not overlap with port figure
setLayoutOption(node, CoreOptions.SPACING_LABEL_PORT_HORIZONTAL, 2.0);
setLayoutOption(node, CoreOptions.SPACING_LABEL_PORT_VERTICAL, -3.0);

Expand Down Expand Up @@ -1034,8 +1057,8 @@ private Collection<KNode> transformReactorNetwork(
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.
// adjusted to the pointy shape.
// However, this is only possible after the layout.
ReactionPortAdjustment.apply(node, figure);
}

Expand Down Expand Up @@ -1585,19 +1608,24 @@ private KPort addIOPort(
NamedInstanceUtil.linkInstance(port, lfPort);
_kPortExtensions.setPortSize(port, 6, 6);

if (input) {
// multiports are smaller by an offset at the right, hence compensate in inputs
double offset = multiport ? -3.4 : -3.3;
setLayoutOption(port, CoreOptions.PORT_SIDE, PortSide.WEST);
setLayoutOption(port, CoreOptions.PORT_BORDER_OFFSET, offset);
} else {
double offset = multiport ? -2.6 : -3.3; // multiports are smaller
offset =
bank
? offset - LinguaFrancaShapeExtensions.BANK_FIGURE_X_OFFSET_SUM
: offset; // compensate bank figure width
setLayoutOption(port, CoreOptions.PORT_SIDE, PortSide.EAST);
setLayoutOption(port, CoreOptions.PORT_BORDER_OFFSET, offset);
var side = input ? PortSide.WEST : PortSide.EAST;
var userSideAttr = AttributeUtils.getPortSide(lfPort.getDefinition());
if (userSideAttr != null) {
try {
var userSide = PortSide.valueOf(userSideAttr.toUpperCase());
if (userSide != null) {
side = userSide;
}
} catch (Exception e) {
// ignore and use default
}
}
double offset = getReactorPortOffset(side == PortSide.WEST, multiport, bank);
setLayoutOption(port, CoreOptions.PORT_SIDE, side);
setLayoutOption(port, CoreOptions.PORT_BORDER_OFFSET, offset);

if (multiport) {
node.setProperty(REACTOR_MULTIPORT, true);
}

if (bank && !node.getProperty(REACTOR_HAS_BANK_PORT_OFFSET)) { // compensate bank figure height
Expand All @@ -1608,7 +1636,9 @@ private KPort addIOPort(
node.setProperty(REACTOR_HAS_BANK_PORT_OFFSET, true); // only once
}

_linguaFrancaShapeExtensions.addTrianglePort(port, multiport);
// If fixed port sides are active and the port is put on the opposite side, reverse it
var reverse = getBooleanValue(FIXED_PORT_SIDE) && input != (side == PortSide.WEST);
_linguaFrancaShapeExtensions.addTrianglePort(port, multiport, reverse);

String label = lfPort.getName();
if (!getBooleanValue(SHOW_PORT_NAMES)) {
Expand All @@ -1623,6 +1653,21 @@ private KPort addIOPort(
return port;
}

public static double getReactorPortOffset(boolean sideLeft, boolean multiport, boolean bank) {
var offset = -3.3;

if (multiport) {
offset = sideLeft ? -3.4 : -2.6;
}

if (bank && !sideLeft) {
// compensate bank figure width
offset -= LinguaFrancaShapeExtensions.BANK_FIGURE_X_OFFSET_SUM;
}

return offset;
}

private KPort addInvisiblePort(KNode node) {
KPort port = _kPortExtensions.createPort();
node.getPorts().add(port);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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.postprocessor.ReactorPortAdjustment;
import org.lflang.diagram.synthesis.styles.LinguaFrancaShapeExtensions;
import org.lflang.diagram.synthesis.styles.LinguaFrancaStyleExtensions;
import org.lflang.diagram.synthesis.util.NamedInstanceUtil;
Expand Down Expand Up @@ -35,6 +36,7 @@ public void execute() {

// Style Mod
reg.registerStyleModifier(ReactionPortAdjustment.ID, new ReactionPortAdjustment());
reg.registerStyleModifier(ReactorPortAdjustment.ID, new ReactorPortAdjustment());

// Blacklist LF-specific properties that should be removed when a diagram is sent from the
// diagram server to a client.
Expand All @@ -46,8 +48,7 @@ public void execute() {
reg.registerBlacklistedProperty(ReactionPortAdjustment.PROCESSED);
reg.registerBlacklistedProperty(LinguaFrancaShapeExtensions.REACTOR_CONTENT_CONTAINER);
reg.registerBlacklistedProperty(LinguaFrancaStyleExtensions.LABEL_PARENT_BACKGROUND);
reg.registerBlacklistedProperty(
NamedInstanceUtil
.LINKED_INSTANCE); // Very important since its values can not be synthesized easily!
// Very important since its values can not be synthesized easily!
reg.registerBlacklistedProperty(NamedInstanceUtil.LINKED_INSTANCE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ public class ReactionPortAdjustment implements IStyleModifier {
public static void apply(KNode node, KRendering rendering) {
// Add modifier that fixes port positions such that edges are properly attached to the shape
var invisible = _kRenderingFactory.createKInvisibility();
invisible.setInvisible(false); // make it ineffective (just for purpose of holding modifier)
invisible.setModifierId(
ReactionPortAdjustment.ID); // Add modifier to receive callback after layout
// make it ineffective (just for purpose of holding modifier)
invisible.setInvisible(false);
// Add modifier to receive callback after layout
invisible.setModifierId(ReactionPortAdjustment.ID);
rendering.getStyles().add(invisible);
node.setProperty(PROCESSED, false);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*************
* Copyright (c) 2023, Kiel University.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
***************/
package org.lflang.diagram.synthesis.postprocessor;

import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Scopes;
import com.google.inject.TypeLiteral;
import de.cau.cs.kieler.klighd.IStyleModifier;
import de.cau.cs.kieler.klighd.IViewer;
import de.cau.cs.kieler.klighd.internal.ILayoutRecorder;
import de.cau.cs.kieler.klighd.kgraph.KGraphElement;
import de.cau.cs.kieler.klighd.kgraph.KGraphFactory;
import de.cau.cs.kieler.klighd.kgraph.KNode;
import de.cau.cs.kieler.klighd.krendering.KRendering;
import de.cau.cs.kieler.klighd.krendering.KRenderingFactory;
import de.cau.cs.kieler.klighd.krendering.ViewSynthesisShared;
import de.cau.cs.kieler.klighd.syntheses.AbstractDiagramSynthesis;
import java.util.List;
import javax.inject.Inject;
import org.eclipse.elk.core.options.CoreOptions;
import org.eclipse.elk.graph.properties.Property;
import org.eclipse.xtext.xbase.lib.Extension;
import org.lflang.diagram.synthesis.LinguaFrancaSynthesis;
import org.lflang.diagram.synthesis.styles.LinguaFrancaShapeExtensions;

/**
* Adjusts the port figures of reactors when fixed side are off to keep the input output indication
* correct.
*
* @author Alexander Schulz-Rosengarten
*/
public class ReactorPortAdjustment implements IStyleModifier {

public static final String ID =
"org.lflang.diagram.synthesis.postprocessor.ReactorPortAdjustment";

/** INTERNAL property to mark node as flipped. */
public static final Property<Boolean> FLIPPED =
new Property<>("org.lflang.diagram.synthesis.postprocessor.reactor.ports.flipped", false);

@Inject @Extension private LinguaFrancaShapeExtensions _linguaFrancaShapeExtensions;
@Extension private KGraphFactory _kGraphFactory = KGraphFactory.eINSTANCE;
private static KRenderingFactory _kRenderingFactory = KRenderingFactory.eINSTANCE;

/** Register this modifier on a reaction rendering. */
public static void apply(KNode node, List<KRendering> renderings) {
var rendering = renderings.get(0); // Get first in bank
// Add modifier that fixes port positions such that edges are properly attached to the shape
var invisible = _kRenderingFactory.createKRotation();
// make it ineffective (just for purpose of holding modifier)
invisible.setRotation(0);
// Add modifier to receive callback after layout
invisible.setModifierId(ID);
rendering.getStyles().add(invisible);
}

public ReactorPortAdjustment() {
// Inject extension
if (_linguaFrancaShapeExtensions == null) {
var injector =
Guice.createInjector(
new com.google.inject.Module() {
// This Module is created to satisfy ViewSynthesisShared scope of used
// synthesis-extensions
public void configure(Binder binder) {
binder.bindScope(ViewSynthesisShared.class, Scopes.SINGLETON);
binder
.bind(new TypeLiteral<AbstractDiagramSynthesis<?>>() {})
.toInstance(new LinguaFrancaSynthesis());
}
});
_linguaFrancaShapeExtensions = injector.getInstance(LinguaFrancaShapeExtensions.class);
}
}

@Override
public boolean modify(IStyleModifier.StyleModificationContext context) {
try {
KGraphElement node = context.getGraphElement();
if (node instanceof KNode) {
KNode knode = (KNode) node;

// Find root node
KNode parent = knode;
while (parent.eContainer() != null) {
parent = (KNode) parent.eContainer();
}

// Get viewer (this is a bit brittle because it fetches the viewer from some internal
// property)
Object viewer =
parent.getAllProperties().entrySet().stream()
.filter(
entry ->
entry.getKey().getId().equals("de.cau.cs.kieler.klighd.viewer")
|| entry.getKey().getId().equals("klighd.layout.viewer"))
.findAny()
.map(entry -> entry.getValue())
.orElse(null);

ILayoutRecorder recorder = null;
if (viewer instanceof IViewer) {
recorder = ((IViewer) viewer).getViewContext().getLayoutRecorder();
}

if (recorder != null) {
recorder.startRecording();
}
for (var port : knode.getPorts()) {
var isInput = port.getProperty(LinguaFrancaSynthesis.REACTOR_INPUT).booleanValue();
if (!isInput && !port.getProperty(LinguaFrancaSynthesis.REACTOR_OUTPUT)) {
continue; // skip
}

var xPos = port.getXpos();
var isLeft = xPos < 0;
var flip = isInput != isLeft;
var isFlipped = port.getProperty(FLIPPED).booleanValue();
var needsUpdate = flip != isFlipped;

if (needsUpdate) {
// Remove figure
port.getData().removeIf(it -> it instanceof KRendering);

// Get port type
var isMultiport = port.getProperty(LinguaFrancaSynthesis.REACTOR_MULTIPORT);
var isBank = port.getProperty(LinguaFrancaSynthesis.REACTOR_HAS_BANK_PORT_OFFSET);

// Add new figure
_linguaFrancaShapeExtensions.addTrianglePort(port, isMultiport, flip);
port.setProperty(FLIPPED, flip);

// Compute new offset
var oldOffset = port.getProperty(CoreOptions.PORT_BORDER_OFFSET);
var newOffset =
LinguaFrancaSynthesis.getReactorPortOffset(!isLeft, isMultiport, isBank);

// Apply offset directly
port.setPos((float) (port.getXpos() + (oldOffset - newOffset)), port.getYpos());
}
}
if (recorder != null) {
recorder.stopRecording(0);
}
}
} catch (Exception e) {
e.printStackTrace();
// do not disturb rendering process
}
return false;
}
}
Loading