use of org.lflang.generator.PortInstance in project lingua-franca by lf-lang.
the class RangeTests method createRange.
@Test
public void createRange() throws Exception {
Reactor main = factory.createReactor();
ReactorInstance maini = new ReactorInstance(main, reporter);
Reactor a = factory.createReactor();
a.setName("A");
ReactorInstance ai = new ReactorInstance(a, maini, reporter);
ai.setWidth(2);
Reactor b = factory.createReactor();
b.setName("B");
ReactorInstance bi = new ReactorInstance(b, ai, reporter);
bi.setWidth(2);
Port p = factory.createPort();
p.setName("P");
PortInstance pi = new PortInstance(p, bi, reporter);
pi.setWidth(2);
Assertions.assertEquals(".A.B.P", pi.getFullName());
RuntimeRange<PortInstance> range = new RuntimeRange.Port(pi, 3, 4, null);
Assertions.assertEquals(8, range.maxWidth);
Assertions.assertEquals(".A.B.P(3,4)", range.toString());
// The results expected below are derived from the class comment for RuntimeRange,
// which includes this example.
List<Integer> instances = range.instances();
Assertions.assertEquals(List.of(3, 4, 5, 6), instances);
Set<Integer> parents = range.parentInstances(1);
Assertions.assertEquals(Set.of(1, 2, 3), parents);
parents = range.parentInstances(2);
Assertions.assertEquals(Set.of(0, 1), parents);
// Test startMR().getDigits.
Assertions.assertEquals(List.of(1, 1, 0), range.startMR().getDigits());
// Create a SendRange sending from and to this range.
SendRange sendRange = new SendRange(pi, 3, 4, null, null);
sendRange.destinations.add(range);
// Test getNumberOfDestinationReactors.
Assertions.assertEquals(3, sendRange.getNumberOfDestinationReactors());
// Make first interleaved version.
range = range.toggleInterleaved(bi);
instances = range.instances();
Assertions.assertEquals(List.of(3, 4, 6, 5), instances);
// Test startMR().getDigits.
Assertions.assertEquals(List.of(1, 1, 0), range.startMR().getDigits());
// Make second interleaved version.
range = range.toggleInterleaved(ai);
instances = range.instances();
Assertions.assertEquals(List.of(6, 1, 5, 3), instances);
// Test startMR().getDigits.
Assertions.assertEquals(List.of(0, 1, 1), range.startMR().getDigits());
// Test instances of the parent.
Assertions.assertEquals(Set.of(3, 0, 2, 1), range.parentInstances(1));
// Add this range to the sendRange destinations and verify
// that the number of destination reactors becomes 4.
sendRange.addDestination(range);
Assertions.assertEquals(4, sendRange.getNumberOfDestinationReactors());
// Make third interleaved version.
range = range.toggleInterleaved(bi);
instances = range.instances();
Assertions.assertEquals(List.of(5, 2, 6, 3), instances);
// Test startMR().getDigits.
Assertions.assertEquals(List.of(1, 0, 1), range.startMR().getDigits());
}
use of org.lflang.generator.PortInstance in project lingua-franca by lf-lang.
the class LinguaFrancaSynthesis method createReactorNode.
private Collection<KNode> createReactorNode(ReactorInstance reactorInstance, boolean expandDefault, Table<ReactorInstance, PortInstance, KPort> inputPortsReg, Table<ReactorInstance, PortInstance, KPort> outputPortsReg, Map<ReactorInstance, KNode> allReactorNodes) {
Reactor reactor = reactorInstance.reactorDefinition;
KNode node = _kNodeExtensions.createNode();
allReactorNodes.put(reactorInstance, node);
associateWith(node, reactor);
_utilityExtensions.setID(node, reactorInstance.uniqueID());
// save to distinguish nodes associated with the same reactor
NamedInstanceUtil.linkInstance(node, reactorInstance);
List<KNode> nodes = new ArrayList<>();
nodes.add(node);
String label = createReactorLabel(reactorInstance);
if (reactorInstance.recursive) {
// Mark this node
node.setProperty(REACTOR_RECURSIVE_INSTANTIATION, true);
// Mark root
allReactorNodes.get(reactorInstance.root()).setProperty(REACTOR_RECURSIVE_INSTANTIATION, true);
}
if (reactor == null) {
_linguaFrancaShapeExtensions.addErrorMessage(node, TEXT_REACTOR_NULL, null);
} else if (reactorInstance.isMainOrFederated()) {
KRoundedRectangle figure = _linguaFrancaShapeExtensions.addMainReactorFigure(node, reactorInstance, label);
if (getObjectValue(REACTOR_PARAMETER_MODE) == ReactorParameterDisplayModes.TABLE && !reactorInstance.parameters.isEmpty()) {
KRectangle rectangle = _kContainerRenderingExtensions.addRectangle(figure);
_kRenderingExtensions.setInvisible(rectangle, true);
_kRenderingExtensions.to(_kRenderingExtensions.from(_kRenderingExtensions.setGridPlacementData(rectangle), _kRenderingExtensions.LEFT, 8, 0, _kRenderingExtensions.TOP, 0, 0), _kRenderingExtensions.RIGHT, 8, 0, _kRenderingExtensions.BOTTOM, 4, 0);
_kRenderingExtensions.setHorizontalAlignment(rectangle, HorizontalAlignment.LEFT);
addParameterList(rectangle, reactorInstance.parameters);
}
if (reactorInstance.recursive) {
nodes.add(addErrorComment(node, TEXT_ERROR_RECURSIVE));
_linguaFrancaStyleExtensions.errorStyle(figure);
} else {
_kContainerRenderingExtensions.addChildArea(figure);
node.getChildren().addAll(transformReactorNetwork(reactorInstance, new HashMap<>(), new HashMap<>(), allReactorNodes));
}
Iterables.addAll(nodes, createUserComments(reactor, node));
configureReactorNodeLayout(node);
// Additional layout adjustment for main node
setLayoutOption(node, CoreOptions.ALGORITHM, LayeredOptions.ALGORITHM_ID);
setLayoutOption(node, CoreOptions.DIRECTION, Direction.RIGHT);
setLayoutOption(node, CoreOptions.NODE_SIZE_CONSTRAINTS, EnumSet.of(SizeConstraint.MINIMUM_SIZE));
setLayoutOption(node, LayeredOptions.NODE_PLACEMENT_BK_FIXED_ALIGNMENT, FixedAlignment.BALANCED);
setLayoutOption(node, LayeredOptions.NODE_PLACEMENT_BK_EDGE_STRAIGHTENING, EdgeStraighteningStrategy.IMPROVE_STRAIGHTNESS);
setLayoutOption(node, LayeredOptions.SPACING_EDGE_NODE, LayeredOptions.SPACING_EDGE_NODE.getDefault() * 1.1f);
setLayoutOption(node, LayeredOptions.SPACING_EDGE_NODE_BETWEEN_LAYERS, LayeredOptions.SPACING_EDGE_NODE_BETWEEN_LAYERS.getDefault() * 1.1f);
if (!getBooleanValue(SHOW_HYPERLINKS)) {
setLayoutOption(node, CoreOptions.PADDING, new ElkPadding(-1, 6, 6, 6));
setLayoutOption(node, LayeredOptions.SPACING_COMPONENT_COMPONENT, LayeredOptions.SPACING_COMPONENT_COMPONENT.getDefault() * 0.5f);
}
} else {
ReactorInstance instance = reactorInstance;
// Expanded Rectangle
ReactorFigureComponents comps = _linguaFrancaShapeExtensions.addReactorFigure(node, reactorInstance, label);
comps.getOuter().setProperty(KlighdProperties.EXPANDED_RENDERING, true);
for (KRendering figure : comps.getFigures()) {
associateWith(figure, reactor);
_kRenderingExtensions.addDoubleClickAction(figure, MemorizingExpandCollapseAction.ID);
}
_reactorIcons.handleIcon(comps.getReactor(), reactor, false);
if (getBooleanValue(SHOW_HYPERLINKS)) {
// Collapse button
KText button = _linguaFrancaShapeExtensions.addTextButton(comps.getReactor(), TEXT_HIDE_ACTION);
_kRenderingExtensions.to(_kRenderingExtensions.from(_kRenderingExtensions.setGridPlacementData(button), _kRenderingExtensions.LEFT, 8, 0, _kRenderingExtensions.TOP, 0, 0), _kRenderingExtensions.RIGHT, 8, 0, _kRenderingExtensions.BOTTOM, 0, 0);
_kRenderingExtensions.addSingleClickAction(button, MemorizingExpandCollapseAction.ID);
_kRenderingExtensions.addDoubleClickAction(button, MemorizingExpandCollapseAction.ID);
}
if (getObjectValue(REACTOR_PARAMETER_MODE) == ReactorParameterDisplayModes.TABLE && !instance.parameters.isEmpty()) {
KRectangle rectangle = _kContainerRenderingExtensions.addRectangle(comps.getReactor());
_kRenderingExtensions.setInvisible(rectangle, true);
if (!getBooleanValue(SHOW_HYPERLINKS)) {
_kRenderingExtensions.to(_kRenderingExtensions.from(_kRenderingExtensions.setGridPlacementData(rectangle), _kRenderingExtensions.LEFT, 8, 0, _kRenderingExtensions.TOP, 0, 0), _kRenderingExtensions.RIGHT, 8, 0, _kRenderingExtensions.BOTTOM, 4, 0);
} else {
_kRenderingExtensions.to(_kRenderingExtensions.from(_kRenderingExtensions.setGridPlacementData(rectangle), _kRenderingExtensions.LEFT, 8, 0, _kRenderingExtensions.TOP, 4, 0), _kRenderingExtensions.RIGHT, 8, 0, _kRenderingExtensions.BOTTOM, 0, 0);
}
_kRenderingExtensions.setHorizontalAlignment(rectangle, HorizontalAlignment.LEFT);
addParameterList(rectangle, instance.parameters);
}
if (instance.recursive) {
comps.getFigures().forEach(_linguaFrancaStyleExtensions::errorStyle);
} else {
_kContainerRenderingExtensions.addChildArea(comps.getReactor());
}
// Collapse Rectangle
comps = _linguaFrancaShapeExtensions.addReactorFigure(node, reactorInstance, label);
comps.getOuter().setProperty(KlighdProperties.COLLAPSED_RENDERING, true);
for (KRendering figure : comps.getFigures()) {
associateWith(figure, reactor);
if (_utilityExtensions.hasContent(instance) && !instance.recursive) {
_kRenderingExtensions.addDoubleClickAction(figure, MemorizingExpandCollapseAction.ID);
}
}
_reactorIcons.handleIcon(comps.getReactor(), reactor, true);
if (getBooleanValue(SHOW_HYPERLINKS)) {
// Expand button
if (_utilityExtensions.hasContent(instance) && !instance.recursive) {
KText button = _linguaFrancaShapeExtensions.addTextButton(comps.getReactor(), TEXT_SHOW_ACTION);
_kRenderingExtensions.to(_kRenderingExtensions.from(_kRenderingExtensions.setGridPlacementData(button), _kRenderingExtensions.LEFT, 8, 0, _kRenderingExtensions.TOP, 0, 0), _kRenderingExtensions.RIGHT, 8, 0, _kRenderingExtensions.BOTTOM, 8, 0);
_kRenderingExtensions.addSingleClickAction(button, MemorizingExpandCollapseAction.ID);
_kRenderingExtensions.addDoubleClickAction(button, MemorizingExpandCollapseAction.ID);
}
}
if (instance.recursive) {
comps.getFigures().forEach(_linguaFrancaStyleExtensions::errorStyle);
}
// Create ports
Map<PortInstance, KPort> inputPorts = new HashMap<>();
Map<PortInstance, KPort> outputPorts = new HashMap<>();
for (PortInstance input : ListExtensions.reverseView(instance.inputs)) {
inputPorts.put(input, addIOPort(node, input, true, input.isMultiport(), reactorInstance.isBank()));
}
for (PortInstance output : instance.outputs) {
outputPorts.put(output, addIOPort(node, output, false, output.isMultiport(), reactorInstance.isBank()));
}
// Mark ports
inputPorts.values().forEach(it -> it.setProperty(REACTOR_INPUT, true));
outputPorts.values().forEach(it -> it.setProperty(REACTOR_OUTPUT, true));
// Add content
if (_utilityExtensions.hasContent(instance) && !instance.recursive) {
node.getChildren().addAll(transformReactorNetwork(instance, inputPorts, outputPorts, allReactorNodes));
}
// Pass port to given tables
if (!_utilityExtensions.isRoot(instance)) {
if (inputPortsReg != null) {
for (Map.Entry<PortInstance, KPort> entry : inputPorts.entrySet()) {
inputPortsReg.put(instance, entry.getKey(), entry.getValue());
}
}
if (outputPortsReg != null) {
for (Map.Entry<PortInstance, KPort> entry : outputPorts.entrySet()) {
outputPortsReg.put(instance, entry.getKey(), entry.getValue());
}
}
}
if (instance.recursive) {
setLayoutOption(node, KlighdProperties.EXPAND, false);
nodes.add(addErrorComment(node, TEXT_ERROR_RECURSIVE));
} else {
setLayoutOption(node, KlighdProperties.EXPAND, expandDefault);
// Interface Dependencies
_interfaceDependenciesVisualization.addInterfaceDependencies(node, expandDefault);
}
if (!_utilityExtensions.isRoot(instance)) {
// add the annotation now.
if (!getBooleanValue(SHOW_ALL_REACTORS)) {
Iterables.addAll(nodes, createUserComments(reactor, node));
}
} else {
Iterables.addAll(nodes, createUserComments(reactor, node));
}
configureReactorNodeLayout(node);
}
// Find and annotate cycles
if (getBooleanValue(CYCLE_DETECTION) && _utilityExtensions.isRoot(reactorInstance)) {
KNode errNode = detectAndAnnotateCycles(node, reactorInstance, allReactorNodes);
if (errNode != null) {
nodes.add(errNode);
}
}
return nodes;
}
use of org.lflang.generator.PortInstance in project lingua-franca by lf-lang.
the class PortInstanceTests method multiportDestination.
@Test
public void multiportDestination() throws Exception {
Reactor main = factory.createReactor();
ReactorInstance maini = new ReactorInstance(main, reporter);
ReactorInstance a = newReactor("A", maini);
ReactorInstance b = newReactor("B", maini);
b.setWidth(4);
PortInstance p = newOutputPort("p", a);
PortInstance q = newInputPort("q", b);
connect(p, 0, 1, q, 0, 4);
List<SendRange> sr = p.eventualDestinations();
// Destination has no reactions, so empty list is right.
Assertions.assertEquals("[]", sr.toString());
maini.clearCaches();
newReaction(q);
sr = p.eventualDestinations();
Assertions.assertEquals("[.A.p(0,1)->[.B.q(0,4)]]", sr.toString());
}
use of org.lflang.generator.PortInstance in project lingua-franca by lf-lang.
the class PortInstanceTests method newInputPort.
protected PortInstance newInputPort(String name, ReactorInstance container) {
PortInstance pi = newPort(name, container);
container.inputs.add(pi);
return pi;
}
use of org.lflang.generator.PortInstance in project lingua-franca by lf-lang.
the class PortInstanceTests method newOutputPort.
protected PortInstance newOutputPort(String name, ReactorInstance container) {
PortInstance pi = newPort(name, container);
container.outputs.add(pi);
return pi;
}
Aggregations