OMCompiler/Compiler/FrontEnd/Graphviz.mo
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of OpenModelica. | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), | ||
| 5 | * c/o Linköpings universitet, Department of Computer and Information Science, | ||
| 6 | * SE-58183 Linköping, Sweden. | ||
| 7 | * | ||
| 8 | * All rights reserved. | ||
| 9 | * | ||
| 10 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR | ||
| 11 | * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. | ||
| 12 | * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES | ||
| 13 | * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL | ||
| 14 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 15 | * | ||
| 16 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) | ||
| 17 | * Public License (OSMC-PL) are obtained from OSMC, either from the above | ||
| 18 | * address, from the URLs: | ||
| 19 | * http://www.openmodelica.org or | ||
| 20 | * https://github.com/OpenModelica/ or | ||
| 21 | * http://www.ida.liu.se/projects/OpenModelica, | ||
| 22 | * and in the OpenModelica distribution. | ||
| 23 | * | ||
| 24 | * GNU AGPL version 3 is obtained from: | ||
| 25 | * https://www.gnu.org/licenses/licenses.html#GPL | ||
| 26 | * | ||
| 27 | * This program is distributed WITHOUT ANY WARRANTY; without | ||
| 28 | * even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 29 | * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH | ||
| 30 | * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL. | ||
| 31 | * | ||
| 32 | * See the full OSMC Public License conditions for more details. | ||
| 33 | * | ||
| 34 | */ | ||
| 35 | |||
| 36 | encapsulated package Graphviz | ||
| 37 | " file: Graphviz.mo | ||
| 38 | package: Graphviz | ||
| 39 | description: Graphviz is a tool for drawing graphs from a textual | ||
| 40 | representation. This module generates the textual input | ||
| 41 | to graphviz from a tree defined using the data structures | ||
| 42 | defined here, e.g. Node for tree nodes. | ||
| 43 | See | ||
| 44 | http://www.research.att.com/sw/tools/graphviz/. | ||
| 45 | |||
| 46 | |||
| 47 | Input: The tree constructed from data structures in Graphviz | ||
| 48 | Output: Textual input to graphviz, written to stdout." | ||
| 49 | |||
| 50 | type Type = String; | ||
| 51 | |||
| 52 | type Ident = String; | ||
| 53 | |||
| 54 | type Label = String; | ||
| 55 | |||
| 56 | uniontype Attribute "an Attribute is a pair of name an value." | ||
| 57 | record ATTR | ||
| 58 | String name "name" ; | ||
| 59 | String value "value" ; | ||
| 60 | end ATTR; | ||
| 61 | |||
| 62 | end Attribute; | ||
| 63 | |||
| 64 | type Attributes = list<Attribute>; | ||
| 65 | |||
| 66 | uniontype Node "A graphviz Node is a node of the graph. | ||
| 67 | It has a type and attributes and children. | ||
| 68 | It can also have a list of labels, provided by the LNODE | ||
| 69 | constructor." | ||
| 70 | record NODE | ||
| 71 | Type type_; | ||
| 72 | Attributes attributes; | ||
| 73 | list<Node> children; | ||
| 74 | end NODE; | ||
| 75 | |||
| 76 | record LNODE | ||
| 77 | Type type_; | ||
| 78 | list<Label> labelLst; | ||
| 79 | Attributes attributes; | ||
| 80 | list<Node> children; | ||
| 81 | end LNODE; | ||
| 82 | |||
| 83 | end Node; | ||
| 84 | |||
| 85 | type Children = list<Node>; | ||
| 86 | |||
| 87 | constant Attribute box=ATTR("shape","box"); | ||
| 88 | |||
| 89 | function dump "Relations | ||
| 90 | function: dump | ||
| 91 | Dumps a Graphviz Node on stdout." | ||
| 92 | input Node node; | ||
| 93 | protected | ||
| 94 | Label nm; | ||
| 95 | algorithm | ||
| 96 | ✗ | print("graph AST {\n"); | |
| 97 | ✗ | nm := dumpNode(node); | |
| 98 | ✗ | print("}\n"); | |
| 99 | end dump; | ||
| 100 | |||
| 101 | protected | ||
| 102 | |||
| 103 | function dumpNode "Dumps a node to a string." | ||
| 104 | input Node inNode; | ||
| 105 | output Ident outIdent; | ||
| 106 | algorithm | ||
| 107 | outIdent := match inNode | ||
| 108 | local | ||
| 109 | Label nm,typlbl,out,typ,lblstr; | ||
| 110 | Attributes newattr,attr; | ||
| 111 | Children children; | ||
| 112 | list<Label> lbl_1,lbl; | ||
| 113 | |||
| 114 | case NODE(type_ = typ,attributes = attr,children = children) | ||
| 115 | algorithm | ||
| 116 | ✗ | nm := nodename(typ); | |
| 117 | ✗ | typlbl := makeLabel({typ}); | |
| 118 | ✗ | newattr := ATTR("label",typlbl)::attr; | |
| 119 | ✗ | out := makeNode(nm, newattr); | |
| 120 | ✗ | print(out); | |
| 121 | ✗ | dumpChildren(nm, children); | |
| 122 | then | ||
| 123 | nm; | ||
| 124 | |||
| 125 | case LNODE(type_ = typ,labelLst = lbl,attributes = attr,children = children) | ||
| 126 | algorithm | ||
| 127 | ✗ | nm := nodename(typ); | |
| 128 | lbl_1 := typ::lbl; | ||
| 129 | ✗ | lblstr := makeLabel(lbl_1); | |
| 130 | ✗ | newattr := ATTR("label",lblstr)::attr; | |
| 131 | ✗ | out := makeNode(nm, newattr); | |
| 132 | ✗ | print(out); | |
| 133 | ✗ | dumpChildren(nm, children); | |
| 134 | then | ||
| 135 | nm; | ||
| 136 | end match; | ||
| 137 | end dumpNode; | ||
| 138 | |||
| 139 | function makeLabel "Creates a label from a list of strings." | ||
| 140 | input list<String> sl; | ||
| 141 | output String s2; | ||
| 142 | protected | ||
| 143 | Label s0,s1; | ||
| 144 | algorithm | ||
| 145 | ✗ | s0 := makeLabelReq(sl,""); | |
| 146 | ✗ | s1 := stringAppend("\"", s0); | |
| 147 | ✗ | s2 := stringAppend(s1, "\""); | |
| 148 | end makeLabel; | ||
| 149 | |||
| 150 | function makeLabelReq "Helper function to makeLabel" | ||
| 151 | input list<String> inStringLst; | ||
| 152 | input String inString; | ||
| 153 | output String outString; | ||
| 154 | algorithm | ||
| 155 | outString := match inStringLst | ||
| 156 | local | ||
| 157 | Label s,s1,s2; | ||
| 158 | list<Label> rest; | ||
| 159 | |||
| 160 | ✗ | case {s} then stringAppend(inString, s); | |
| 161 | |||
| 162 | case {s1,s2} | ||
| 163 | algorithm | ||
| 164 | ✗ | s := stringAppend(inString, s1); | |
| 165 | ✗ | s := stringAppend(s, "\\n"); | |
| 166 | ✗ | s := stringAppend(s, s2); | |
| 167 | then s; | ||
| 168 | |||
| 169 | case s1 :: rest | ||
| 170 | algorithm | ||
| 171 | ✗ | s := stringAppend(inString, s1); | |
| 172 | ✗ | s := stringAppend(s, "\\n"); | |
| 173 | ✗ | then | |
| 174 | makeLabelReq(rest, s); | ||
| 175 | end match; | ||
| 176 | end makeLabelReq; | ||
| 177 | |||
| 178 | function dumpChildren "Helper function to dumpNode" | ||
| 179 | input Ident inIdent; | ||
| 180 | input Children inChildren; | ||
| 181 | algorithm | ||
| 182 | () := match (inIdent,inChildren) | ||
| 183 | local | ||
| 184 | Label nm,parent; | ||
| 185 | Node node; | ||
| 186 | Children rest; | ||
| 187 | |||
| 188 | case (_,{}) then (); | ||
| 189 | |||
| 190 | case (parent,(node :: rest)) | ||
| 191 | algorithm | ||
| 192 | ✗ | nm := dumpNode(node); | |
| 193 | ✗ | printEdge(nm, parent); | |
| 194 | ✗ | dumpChildren(parent, rest); | |
| 195 | then | ||
| 196 | (); | ||
| 197 | end match; | ||
| 198 | end dumpChildren; | ||
| 199 | |||
| 200 | function nodename "Creates a unique node name, | ||
| 201 | changed use of str as part of nodename, since it may contain spaces" | ||
| 202 | input String str; | ||
| 203 | output String s; | ||
| 204 | protected | ||
| 205 | Integer i; | ||
| 206 | Label is; | ||
| 207 | algorithm | ||
| 208 | ✗ | i := tick(); | |
| 209 | ✗ | is := intString(i); | |
| 210 | ✗ | s := stringAppend("GVNOD", is); | |
| 211 | end nodename; | ||
| 212 | |||
| 213 | function printEdge "Prints an edge between two nodes." | ||
| 214 | input Ident n1; | ||
| 215 | input Ident n2; | ||
| 216 | protected | ||
| 217 | Label str; | ||
| 218 | algorithm | ||
| 219 | ✗ | str := makeEdge(n1, n2); | |
| 220 | ✗ | print(str); | |
| 221 | ✗ | print(";\n"); | |
| 222 | end printEdge; | ||
| 223 | |||
| 224 | function makeEdge "Creates a string representing an edge between two nodes." | ||
| 225 | input Ident n1; | ||
| 226 | input Ident n2; | ||
| 227 | output String str; | ||
| 228 | protected | ||
| 229 | Label s; | ||
| 230 | algorithm | ||
| 231 | ✗ | s := stringAppend(n1, " -- "); | |
| 232 | ✗ | str := stringAppend(s, n2); | |
| 233 | end makeEdge; | ||
| 234 | |||
| 235 | function makeNode "Creates string from a node." | ||
| 236 | input Ident nm; | ||
| 237 | input Attributes attr; | ||
| 238 | output String str; | ||
| 239 | protected | ||
| 240 | Label s,s_1; | ||
| 241 | algorithm | ||
| 242 | ✗ | s := makeAttr(attr); | |
| 243 | ✗ | s_1 := stringAppend(nm, s); | |
| 244 | ✗ | str := stringAppend(s_1, ";"); | |
| 245 | end makeNode; | ||
| 246 | |||
| 247 | function makeAttr "Creates a string from an Attribute list." | ||
| 248 | input list<Attribute> l; | ||
| 249 | output String str; | ||
| 250 | protected | ||
| 251 | Label res,s; | ||
| 252 | algorithm | ||
| 253 | ✗ | res := makeAttrReq(l, ""); | |
| 254 | ✗ | s := stringAppend("[", res); | |
| 255 | ✗ | str := stringAppend(s, "]"); | |
| 256 | end makeAttr; | ||
| 257 | |||
| 258 | function makeAttrReq "Helper function to makeAttr." | ||
| 259 | input list<Attribute> inAttributeLst; | ||
| 260 | input String inString; | ||
| 261 | output String outString; | ||
| 262 | algorithm | ||
| 263 | outString := match inAttributeLst | ||
| 264 | local | ||
| 265 | Label s,name,v; | ||
| 266 | list<Attribute> rest; | ||
| 267 | |||
| 268 | case {ATTR(name = name,value = v)} | ||
| 269 | algorithm | ||
| 270 | ✗ | s := stringAppend(inString, name); | |
| 271 | ✗ | s := stringAppend(s, "="); | |
| 272 | ✗ | then | |
| 273 | stringAppend(s, v); | ||
| 274 | |||
| 275 | case ATTR(name = name,value = v) :: rest | ||
| 276 | algorithm | ||
| 277 | ✗ | s := stringAppend(inString, name); | |
| 278 | ✗ | s := stringAppend(s, "="); | |
| 279 | ✗ | s := stringAppend(s, v); | |
| 280 | ✗ | s := stringAppend(s, ","); | |
| 281 | ✗ | then | |
| 282 | makeAttrReq(rest, s); | ||
| 283 | end match; | ||
| 284 | end makeAttrReq; | ||
| 285 | |||
| 286 | annotation(__OpenModelica_Interface="frontend_dump"); | ||
| 287 | end Graphviz; | ||
| 288 |