# Wiki source code of Ptolemy Browser

Last modified by Richard Kreissig on 2023/09/14 10:31
Export
Office Formats
Export as PDF Export as ODT Export as RTF
Other Formats

Select the pages to export:

select all/ none

Legend:
  Created Page
  Modified Extension Page
  Clean Extension Page

Export as HTML

Show last authors

author | version | line-number | content
--- | --- | --- | ---
|  | 1 | {{warning title="Moved to GitHub"}}
|  | 2 | This project moved to [[its own GitHub repository>>url:https://github.com/kieler/ptbrowser||shape="rect"]].
|  | 3 | {{/warning}}
|  | 4 |
|  | 5 | == Project Information ==
|  | 6 | Responsible:
|  | 7 |
|  | 8 | * [[Christoph Daniel Schulze>>url:http://www.informatik.uni-kiel.de/rtsys/kontakt/christoph-daniel-schulze/||shape="rect"]]
|  | 9 |
|  | 10 | Demo videos:
|  | 11 |
|  | 12 | * [[Standalone Ptolemy Viewer Based on KLighD>>url:http://rtsys.informatik.uni-kiel.de/~~kieler/videos/ptolemyViewer/PtolemyViewerHQ.html||shape="rect"]]
|  | 13 | * [[Navigating and Customizing Views with KLighD>>url:http://rtsys.informatik.uni-kiel.de/~~kieler/videos/klighd-ptolemy/||shape="rect"]]
|  | 14 |
|  | 15 |
|  | 16 | [[doc:KIELER.Development.Downloads Subpages.Downloads - KIELER Ptolemy Browser.WebHome]]
|  | 17 |
|  | 18 |
|  | 19 | A complex example for using KLighD technology, the Ptolemy Visualization project is about generating transient views of Ptolemy II models that are easy to browse through. [[Ptolemy II>>url:http://ptolemy.eecs.berkeley.edu/ptolemyII/index.htm||shape="rect"]] is an open source program developed at UC Berkeley for experimenting with actor-oriented modelling. Actors are graphical representations of components with a certain functionality that generate outputs as a function of the inputs they receive. To use the output of one actor as the input of another, actors are connected by links. A typical Ptolemy model might for instance look like this:
|  | 20 |
|  | 21 | [[image:attach:ptolemy.png]]
|  | 22 |
|  | 23 | In some respect, Ptolemy models are a typical example of graphical modelling languages:
|  | 24 |
|  | 25 | * (% style="line-height:1.4285715" %)Actors are represented as boxes, with edges connecting the actors to each other. The edges are routed orthogonally: each edge segment is aligned either horizontally or vertically.
|  | 26 | * Actors can have more than one input, and more than one output. To be able to distinguish the data coming in and going out, inputs and outputs are routed to dedicated connection points, called ports. Thus, links never connect two actors directly, but rather connect them through their ports.
|  | 27 | * Actors can contain further models that define their functionality (we call such actors //hierarchical actors//). To be able to see the model inside an actor, Ptolemy provides the possibility to look inside the actor. This effectively opens a new Ptolemy window displaying the actor's implementation.
|  | 28 |
|  | 29 | (% style="line-height:1.4285715" %)However, Ptolemy models have some features that are not that common:
|  | 30 |
|  | 31 | * Ports are not restricted to be either input ports or output ports: they can be both. As a consequence, links are undirected and can transport data in both directions.
|  | 32 | * Models have a director that defines the model of computation used to execute the model. In fact, if an actor in turn has another model inside it, that model, too, has a director. One of the research goals of the Ptolemy project is to find out how different models of computation can be composed.
|  | 33 | * All edges are part of relations. A relation is a set of edges that are treated as a hyperedge: an edge that connects not two, but arbitrarily many ports.
|  | 34 |
|  | 35 | = KLighD Visualization =
|  | 36 |
|  | 37 | With our KLighD visualization, we aim to develop a Ptolemy model browser that is particularly good at helping users read through Ptolemy models. It is able to directly open Ptolemy files and display them in a way very similar to what Ptolemy users are used to seeing. The biggest change with respect to how Ptolemy shows models is that our model viewer allows to expand hierarchical actors in-place, without having to open a new window. This helps users understand the context a hierarchical actor is placed in: by understanding where inputs come from and where outputs go to, users also better understand what the hierarchical actor is doing and what it is used for.
|  | 38 |
|  | 39 | This is a screenshot of the model browser displaying the model that Ptolemy showed before, with some actors expanded:
|  | 40 |
|  | 41 | [[image:attach:kieler.png]]
|  | 42 |
|  | 43 | {{info title="Try This"}}
|  | 44 | Open and browse a Ptolemy model:
|  | 45 |
|  | 46 | 1. Start Eclipse with the //KIELER Ptolemy Viewer// feature installed.
|  | 47 | 1. Import the CarTracking Ptolemy model. You can download it from the [[Ptolemy SVN repository>>url:https://source.eecs.berkeley.edu/svn/chess/ptII/trunk/ptolemy/domains/continuous/demo/CarTracking/CarTracking.xml||shape="rect"]].**
|  | 48 | **
|  | 49 | 1. Open the model by double-clicking it in Eclipse. If that doesn't show a graphical view of the model, right-click the file, click //Open With -> Other...// and select //Ptolemy Model Viewer// from the dialog that pops up.
|  | 50 | 1. Open and close some of the hierarchical actors by double-clicking them.
|  | 51 | {{/info}}
|  | 52 |
|  | 53 | We have also added further features that may be helpful when browsing models:
|  | 54 |
|  | 55 | * Annotations can be shown or hidden.
|  | 56 | * A heuristic tries to find out which actor an annotation is providing the documentation for. This is based on the distance between actors and annotations, with a threshold value after which an annotation is considered to stand alone. This attachment between annotations and actors is displayed by a dashed line, and the layout algorithm makes sure the two are placed near each other.
|  | 57 | * Relations in Ptolemy are visualized by small diamond-shaped nodes. Often, these are only used to influence the edge routing. Since we use a layout algorithm to route edges properly, these //relation vertices// are not necessary. We replace all of them by hyperedges and route the edges properly. An option lets you disable this behaviour: then, only relation vertices with exactly one incoming and one outgoing edge are removed and replaced by a simple edge.
|  | 58 | * Expanded hierarchical nodes have a background color that gets darker with each level of hierarchy. How dark this color is can be changed to adjust for projectors and printers that are not as good at showing sublte colors.
|  | 59 | * The properties of actors and ports are shown as tool tips. This does not include advanced properties, which are those properties that start with an underscore.
|  | 60 |
|  | 61 | = How This Stuff Works =
|  | 62 |
|  | 63 | All of this functionality is implemented in one main plug-in and two support plug-ins:
|  | 64 |
|  | 65 | * {{code language="none"}}de.cau.cs.kieler.ptolemy.klighd{{/code}} contains the actual transformation that turns Ptolemy files into something we can display with KLighD.
|  | 66 | * {{code language="none"}}org.ptolemy{{/code}} contains a reduced Ptolemy version that we use to render actors. Not all actors are available in this distribution.
|  | 67 | * {{code language="none"}}org.ptolemy.moml{{/code}} contains an EMF model that we use to load Ptolemy files.
|  | 68 |
|  | 69 | For the rest of this page, we will describe the basics of how the transformation works and the challenges it presented.
|  | 70 |
|  | 71 | == Architecture of the Transformation ==
|  | 72 |
|  | 73 | The transformation is mostly written in Xtend. Its entry point is the [[PtolemyDiagramSynthesis>>url:https://git.rtsys.informatik.uni-kiel.de/projects/KIELER/repos/pragmatics/browse/plugins/de.cau.cs.kieler.ptolemy.klighd/src/de/cau/cs/kieler/ptolemy/klighd/PtolemyDiagramSynthesis.xtend||shape="rect"]] class, which is registered with KLighD for the visualization of MOML models. It triggers the three different stages of the transformation:
|  | 74 |
|  | 75 | [[image:attach:synthesis.png]]
|  | 76 |
|  | 77 | 1. The basic transformation. This is what turns a given MOML model into a KGraph model.
|  | 78 | 1. An optimization step. A direct transformation of the MOML model doesn't give the best results, which makes this postprocessing step necessary.
|  | 79 | 1. The visualization. This step adds the KRendering information necessary for KLighD to actually display the model.
|  | 80 |
|  | 81 | It would certainly be possible – and perhaps even faster – to combine these stages into one. Doing this, however, would result in code that only John Carmack would understand. And even he would curse reading it. Who wouldn't.
|  | 82 |
|  | 83 | == Generating the Basic KGraph Model ==
|  | 84 |
|  | 85 | The basic KGraph model is generated by turning all entities and relations into nodes, all ports into... well, ports, and all links into edges. The logic for all of this is implemented in the [[Ptolemy2KGraphTransformation>>url:https://git.rtsys.informatik.uni-kiel.de/projects/KIELER/repos/pragmatics/browse/plugins/de.cau.cs.kieler.ptolemy.klighd/src/de/cau/cs/kieler/ptolemy/klighd/transformation/Ptolemy2KGraphTransformation.xtend||shape="rect"]] class and is actually a bit more complex than we just made it sound.
|  | 86 |
|  | 87 | While the model lists a number of entities that are instances of a given actor class, it usually doesn't contain any information about the ports this kind of actor has. To work around this, the transformation tries to use our Ptolemy library to instantiate the actor and query it for its ports. If this succeeds, we add the ports to the actor's representation in the KGraph model and annotate them with information about whether they are input or output ports. If it doesn't – which happens when our Ptolemy library doesn't contain the actor – we add ports to the actor as they are referenced by the links incident to them. In this case, we don't know whether a port is an input port or an output port. Deriving this information is actually (the hardest) part of the next stage.
|  | 88 |
|  | 89 | Just about everything in Ptolemy can have properties. Even properties can have further properties. These properties carry important semantic information that someone browsing the model would want to know, so we copy them into the KGraph for later visualization.
|  | 90 |
|  | 91 | == Optimizing the KGraph Model ==
|  | 92 |
|  | 93 | Being the most complex stage, the optimization is necessary for the model to be properly displayed. The code can be found in the [[Ptolemy2KGraphOptimization>>url:https://git.rtsys.informatik.uni-kiel.de/projects/KIELER/repos/pragmatics/browse/plugins/de.cau.cs.kieler.ptolemy.klighd/src/de/cau/cs/kieler/ptolemy/klighd/transformation/Ptolemy2KGraphOptimization.xtend||shape="rect"]] class and solves the following problems:
|  | 94 |
|  | 95 | 1. Infer edge directions. Links in Ptolemy are undirected, while edges in the KGraph metamodel are directed. Our layout algorithms, too, assume directed input graphs and emphasize the flow of data. This requires us to infer the correct direction of edges, which is a hard thing to do and doesn't always give correct results. We use a heuristic that first fixes the direction of edges connected to ports that are known to be either input ports or output ports. Going from there, we try to infer the direction of further edges. For example, if a relation has only two incident edges of which one has a known direction, the direction of the second edge is easy to infer. If this second edge goes into a port of unknown type, we can safely assume the port to be an input port and then infer the direction of other edges incident to the port. This system starts to give wrong results once we have many ports of unknown type.
|  | 96 | 1. Remove relation vertices. In Ptolemy, all links connect either two relations, or one port and one relation. If a relation only connects two ports, we can replace it by a simple edge, which improves layout and clarity. Since our main layout algorithm, KLay Layered, supports routing hyperedges (edges that connect more than two ports), we can even go a step further: for each relation, we collect the input ports and the output ports it connects. We then connect each input port to each output port directly, thereby removing the relation.
|  | 97 | 1. Remove ports from states of modal models. States don't need dedicated connection points since transitions between states don't model the flow of data, but the flow of control from one state to the next.
|  | 98 | 1. Convert certain properties to dedicated nodes. Each Ptolemy model has a director that specifies the model of computation to use when executing the model. In Ptolemy, the director is drawn as a green rectangle, very much like a node. In the model, however, the director is a property of the model entity and thus wasn't transformed into a node in the KGraph during the first stage. Also, each model can have parameters also saved as properties. We collect the parameters and turn them into a node that can display them later.
|  | 99 | 1. Extract annotations from the model. In Ptolemy, models can be annotated (in programming languages, annotations would be comments; if you are a programmer and have no idea what comments are, you haven't found the right field of work for you yet). The text of annotations is sometimes a simple property of the annotation, in which case visualizing it is straightforward. Often enough, however, the text is specified as an SVG image. This is a problem because the SVG metamodel is not part of the MOML metamodel, which in turn causes the EMF parser that loads the model to panic. We thus wrote code that finds such SVG images and tries to extract the annotation text from them. This usually works.
|  | 100 |
|  | 101 | == Adding Rendering Information to the KGraph Model ==
|  | 102 |
|  | 103 | If we fed the KGraph coming out of stage 2 into KLighD, it wouldn't look at all like a Ptolemy model – if anything was displayed at all. For KLighD to know how to render our model, we need to attach KRendering information to the nodes, ports, and edges. The final stage, implemented in the [[Ptolemy2GraphVisualization>>url:https://git.rtsys.informatik.uni-kiel.de/projects/KIELER/repos/pragmatics/browse/plugins/de.cau.cs.kieler.ptolemy.klighd/src/de/cau/cs/kieler/ptolemy/klighd/transformation/Ptolemy2KGraphVisualization.xtend||shape="rect"]] class, adds these information. The implementation is actually very simple: for each entity, a big switch statement decides what kind of rendering the entity requires and delegates to a corresponding method. The method then adds layout information to the entity and delegates the creation of the actual KRenderings to another method defined in the [[KRenderingFigureProvider>>url:https://git.rtsys.informatik.uni-kiel.de/projects/KIELER/repos/pragmatics/browse/plugins/de.cau.cs.kieler.ptolemy.klighd/src/de/cau/cs/kieler/ptolemy/klighd/transformation/KRenderingFigureProvider.xtend||shape="rect"]] class. Then, the entity's ports and outgoing edges are treated similarly. Finally, labels and tool tips are handled.
|  | 104 |
|  | 105 | Only one detail deserves some more explanation: how we get our hands at how entities are rendered in Ptolemy. This is done through a mechanism similar to what we already used in the first stage: we simply instantiate the entities using our Ptolemy library, ask Ptolemy to render them, and convert the rendering information into something KLighD supports. This can either be SVG code (if the rendering was originally specified as an SVG image) or a bitmap image. If an actor cannot be instantiated, we have no rendering information and display the actor as a simple rectangle with a black border and white background.

# Quick Links

KIELER on GitHub
RTSYS Homepage

XWiki 14.4.7