Search in sources :

Example 1 with SynthesisErrorReporter

use of org.lflang.diagram.synthesis.util.SynthesisErrorReporter in project lingua-franca by lf-lang.

the class LinguaFrancaSynthesis method transform.

// -------------------------------------------------------------------------
@Override
public KNode transform(final Model model) {
    KNode rootNode = _kNodeExtensions.createNode();
    try {
        // Find main
        Reactor main = IterableExtensions.findFirst(model.getReactors(), _utilityExtensions::isMainOrFederated);
        if (main != null) {
            ReactorInstance reactorInstance = new ReactorInstance(main, new SynthesisErrorReporter());
            rootNode.getChildren().addAll(createReactorNode(reactorInstance, true, null, null, new HashMap<>()));
        } else {
            KNode messageNode = _kNodeExtensions.createNode();
            _linguaFrancaShapeExtensions.addErrorMessage(messageNode, TEXT_NO_MAIN_REACTOR, null);
            rootNode.getChildren().add(messageNode);
        }
        // Show all reactors
        if (main == null || getBooleanValue(SHOW_ALL_REACTORS)) {
            List<KNode> reactorNodes = new ArrayList<>();
            for (Reactor reactor : model.getReactors()) {
                if (reactor == main)
                    continue;
                ReactorInstance reactorInstance = new ReactorInstance(reactor, new SynthesisErrorReporter(), new HashSet<>());
                reactorNodes.addAll(createReactorNode(reactorInstance, main == null, HashBasedTable.<ReactorInstance, PortInstance, KPort>create(), HashBasedTable.<ReactorInstance, PortInstance, KPort>create(), new HashMap<>()));
            }
            if (!reactorNodes.isEmpty()) {
                // To allow ordering, we need box layout but we also need layered layout for ports thus wrap all node
                // TODO use rect packing in the future
                reactorNodes.add(0, IterableExtensions.head(rootNode.getChildren()));
                int index = 0;
                for (KNode node : reactorNodes) {
                    if (node.getProperty(CoreOptions.COMMENT_BOX))
                        continue;
                    KNode child = _kNodeExtensions.createNode();
                    child.getChildren().add(node);
                    // Add comment nodes
                    for (KEdge edge : node.getIncomingEdges()) {
                        if (!edge.getSource().getProperty(CoreOptions.COMMENT_BOX))
                            continue;
                        child.getChildren().add(edge.getSource());
                    }
                    _kRenderingExtensions.addInvisibleContainerRendering(child);
                    setLayoutOption(child, CoreOptions.ALGORITHM, LayeredOptions.ALGORITHM_ID);
                    setLayoutOption(child, CoreOptions.PADDING, new ElkPadding(0));
                    // Order!
                    setLayoutOption(child, CoreOptions.PRIORITY, reactorNodes.size() - index);
                    rootNode.getChildren().add(child);
                    index++;
                }
                setLayoutOption(rootNode, CoreOptions.ALGORITHM, BoxLayouterOptions.ALGORITHM_ID);
                setLayoutOption(rootNode, CoreOptions.SPACING_NODE_NODE, 25.0);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        KNode messageNode = _kNodeExtensions.createNode();
        _linguaFrancaShapeExtensions.addErrorMessage(messageNode, "Error in Diagram Synthesis", e.getClass().getSimpleName() + " occurred. Could not create diagram.");
        rootNode.getChildren().add(messageNode);
    }
    return rootNode;
}
Also used : PortInstance(org.lflang.generator.PortInstance) SynthesisErrorReporter(org.lflang.diagram.synthesis.util.SynthesisErrorReporter) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KEdge(de.cau.cs.kieler.klighd.kgraph.KEdge) KPort(de.cau.cs.kieler.klighd.kgraph.KPort) KNode(de.cau.cs.kieler.klighd.kgraph.KNode) SizeConstraint(org.eclipse.elk.core.options.SizeConstraint) LayerConstraint(org.eclipse.elk.alg.layered.options.LayerConstraint) ReactorInstance(org.lflang.generator.ReactorInstance) Reactor(org.lflang.lf.Reactor) ElkPadding(org.eclipse.elk.core.math.ElkPadding)

Aggregations

KEdge (de.cau.cs.kieler.klighd.kgraph.KEdge)1 KNode (de.cau.cs.kieler.klighd.kgraph.KNode)1 KPort (de.cau.cs.kieler.klighd.kgraph.KPort)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LayerConstraint (org.eclipse.elk.alg.layered.options.LayerConstraint)1 ElkPadding (org.eclipse.elk.core.math.ElkPadding)1 SizeConstraint (org.eclipse.elk.core.options.SizeConstraint)1 SynthesisErrorReporter (org.lflang.diagram.synthesis.util.SynthesisErrorReporter)1 PortInstance (org.lflang.generator.PortInstance)1 ReactorInstance (org.lflang.generator.ReactorInstance)1 Reactor (org.lflang.lf.Reactor)1