| 1 |
-- This patched version of the createVertex function simply ensures that the |
| 2 |
-- path is never empty. This works fine for TikZ version 3.0.0. |
| 3 |
-- |
| 4 |
-- Later versions (should) have a more thorough patch by Till which is |
| 5 |
-- incompatible with this override unfortunately. |
| 6 |
-- |
| 7 |
-- ############################################################################# |
| 8 |
-- |
| 9 |
-- Copyright 2012 by Till Tantau |
| 10 |
-- |
| 11 |
-- This file may be distributed an/or modified |
| 12 |
-- |
| 13 |
-- 1. under the LaTeX Project Public License and/or |
| 14 |
-- 2. under the GNU Public License |
| 15 |
-- |
| 16 |
-- See the file doc/generic/pgf/licenses/LICENSE for more information |
| 17 |
|
| 18 |
-- @release $Header: /cvsroot/pgf/pgf/generic/pgf/graphdrawing/lua/pgf/gd/interface/InterfaceToDisplay.lua,v 1.12 2013/12/20 14:44:46 tantau Exp $ |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
--- |
| 23 |
-- This class provides the interface between a display |
| 24 |
-- layer (like \tikzname\ or a graph editor) and graph drawing |
| 25 |
-- system. Another class, |InterfaceToAlgorithms|, binds the algorithm |
| 26 |
-- layer (which are written in Lua) to the graph drawing system. |
| 27 |
-- |
| 28 |
-- The functions declared here are independent of the actual display |
| 29 |
-- layer. Rather, the differences between the layers are encapsulated |
| 30 |
-- by subclasses of the |Binding| class, see that class for |
| 31 |
-- details. Thus, when a new display layer is written, the present |
| 32 |
-- class is \emph{used}, but not \emph{modified}. Instead, only a new |
| 33 |
-- binding is created and all display layer specific interaction is |
| 34 |
-- put there. |
| 35 |
-- |
| 36 |
-- The job of this class is to provide convenient methods that can be |
| 37 |
-- called by the display layer. For instance, it provides methods for |
| 38 |
-- starting a graph drawing scope, managing the stack of such scope, |
| 39 |
-- adding a node to a graph and so on. |
| 40 |
|
| 41 |
local InterfaceToDisplay = {} |
| 42 |
|
| 43 |
-- Namespace |
| 44 |
require("pgf.gd.interface").InterfaceToDisplay = InterfaceToDisplay |
| 45 |
|
| 46 |
|
| 47 |
-- Imports |
| 48 |
local InterfaceCore = require "pgf.gd.interface.InterfaceCore" |
| 49 |
local Scope = require "pgf.gd.interface.Scope" |
| 50 |
|
| 51 |
local Binding = require "pgf.gd.bindings.Binding" |
| 52 |
|
| 53 |
local Sublayouts = require "pgf.gd.control.Sublayouts" |
| 54 |
local LayoutPipeline = require "pgf.gd.control.LayoutPipeline" |
| 55 |
|
| 56 |
local Digraph = require "pgf.gd.model.Digraph" |
| 57 |
local Vertex = require "pgf.gd.model.Vertex" |
| 58 |
local Edge = require "pgf.gd.model.Edge" |
| 59 |
local Collection = require "pgf.gd.model.Collection" |
| 60 |
|
| 61 |
local Storage = require "pgf.gd.lib.Storage" |
| 62 |
local LookupTable = require "pgf.gd.lib.LookupTable" |
| 63 |
local Event = require "pgf.gd.lib.Event" |
| 64 |
|
| 65 |
local lib = require "pgf.gd.lib" |
| 66 |
|
| 67 |
|
| 68 |
-- Forward declarations |
| 69 |
local get_current_options_table |
| 70 |
local render_collections |
| 71 |
local push_on_option_stack |
| 72 |
local vertex_created |
| 73 |
|
| 74 |
-- Local objects |
| 75 |
|
| 76 |
local phase_unique = {} -- a unique handle |
| 77 |
local collections_unique = {} -- a unique handle |
| 78 |
local option_cache = nil -- The option cache |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
--- |
| 84 |
-- Initiliaze the binding. This function is called once by the display |
| 85 |
-- layer at the very beginning. For instance, \tikzname\ does the |
| 86 |
-- following call: |
| 87 |
-- |
| 88 |
--\begin{codeexample}[code only, tikz syntax=false] |
| 89 |
--InterfaceToDisplay.bind(require "pgf.gd.bindings.BindingToPGF") |
| 90 |
--\end{codeexample} |
| 91 |
-- |
| 92 |
-- Inside this call, many standard declarations will be executed, that |
| 93 |
-- is, the declared binding will be used immediately. |
| 94 |
-- |
| 95 |
-- Subsequently, the |binding| field of the |InterfaceCore| can be used. |
| 96 |
-- |
| 97 |
-- @param class A subclass of |Binding|. |
| 98 |
|
| 99 |
function InterfaceToDisplay.bind(class) |
| 100 |
assert (not InterfaceCore.binding, "binding already initialized") |
| 101 |
|
| 102 |
-- Create a new object |
| 103 |
InterfaceCore.binding = setmetatable({}, class) |
| 104 |
|
| 105 |
-- Load these libraries, which contain many standard declarations: |
| 106 |
require "pgf.gd.model.library" |
| 107 |
require "pgf.gd.control.library" |
| 108 |
end |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
--- |
| 114 |
-- Start a graph drawing scope. Note that this is not the same as |
| 115 |
-- starting a subgraph / sublayout, which are local to a graph drawing |
| 116 |
-- scope: When a new graph drawing scope is started, it is pushed on |
| 117 |
-- top of a stack of graph drawing scopes and all other ``open'' |
| 118 |
-- scopes are no longer directly accessible. All method calls to an |
| 119 |
-- |Interface...| object will refer to this newly created scope until |
| 120 |
-- either a new scope is opened or until the current scope is closed |
| 121 |
-- once more. |
| 122 |
-- |
| 123 |
-- Each graph drawing scope comes with a syntactic digraph that is |
| 124 |
-- build using methods like |addVertex| or |addEdge|. |
| 125 |
-- |
| 126 |
-- @param height The to-be-used height of the options stack. All |
| 127 |
-- options above this height will be popped prior to attacking the |
| 128 |
-- options to the syntactic digraph. |
| 129 |
|
| 130 |
function InterfaceToDisplay.beginGraphDrawingScope(height) |
| 131 |
|
| 132 |
-- Create a new scope table |
| 133 |
local scope = Scope.new {} |
| 134 |
|
| 135 |
-- Setup syntactic digraph: |
| 136 |
local g = scope.syntactic_digraph |
| 137 |
|
| 138 |
g.options = get_current_options_table(height) |
| 139 |
g.syntactic_digraph = g |
| 140 |
g.scope = scope |
| 141 |
|
| 142 |
-- Push scope: |
| 143 |
InterfaceCore.scopes[#InterfaceCore.scopes + 1] = scope |
| 144 |
end |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
--- |
| 149 |
-- Arranges the current graph using the specified algorithm and options. |
| 150 |
-- |
| 151 |
-- This function should be called after the graph drawing scope has |
| 152 |
-- been opened and the syntactic digraph has been completely |
| 153 |
-- specified. It will now start running the algorithm specified |
| 154 |
-- through the |algorithm_phase| options. |
| 155 |
-- |
| 156 |
-- Internally, this function creates a coroutine that will run the current graph |
| 157 |
-- drawing algorithm. Coroutines are needed since a graph drawing |
| 158 |
-- algorithm may choose to create a new node. In this case, the |
| 159 |
-- algorithm needs to be suspended and control must be returned back |
| 160 |
-- to the display layer, so that the node can be typeset in order to |
| 161 |
-- determine the precise size information. Once this is done, control |
| 162 |
-- must be passed back to the exact point inside the algorithm where |
| 163 |
-- the node was created. Clearly, all of these actions are exactly |
| 164 |
-- what coroutines are for. |
| 165 |
-- |
| 166 |
-- @return Time it took to run the algorithm |
| 167 |
|
| 168 |
function InterfaceToDisplay.runGraphDrawingAlgorithm() |
| 169 |
|
| 170 |
-- Time things |
| 171 |
local start = os.clock() |
| 172 |
|
| 173 |
-- Setup |
| 174 |
local scope = InterfaceCore.topScope() |
| 175 |
assert(not scope.coroutine, "coroutine already created for current gd scope") |
| 176 |
|
| 177 |
-- The actual drawing function |
| 178 |
local function run () |
| 179 |
if #scope.syntactic_digraph.vertices == 0 then |
| 180 |
-- Nothing needs to be done |
| 181 |
return |
| 182 |
end |
| 183 |
|
| 184 |
LayoutPipeline.run(scope) |
| 185 |
end |
| 186 |
|
| 187 |
scope.coroutine = coroutine.create(run) |
| 188 |
|
| 189 |
-- Run it: |
| 190 |
InterfaceToDisplay.resumeGraphDrawingCoroutine() |
| 191 |
|
| 192 |
-- End timing: |
| 193 |
local stop = os.clock() |
| 194 |
|
| 195 |
return stop - start |
| 196 |
end |
| 197 |
|
| 198 |
|
| 199 |
--- |
| 200 |
-- Resume the graph drawing coroutine. |
| 201 |
-- |
| 202 |
-- This function is the work horse of the coroutine management. It |
| 203 |
-- gets called whenever control passes back from the display layer to |
| 204 |
-- the algorithm level. We resume the graph drawing coroutine so that the |
| 205 |
-- algorithm can start/proceed. The tricky part is when the algorithm |
| 206 |
-- yields, but is not done. In this case, the code needed for creating |
| 207 |
-- a new node is passed back to the display layer through the binding, |
| 208 |
-- which must then execute the code and then resuming the coroutine. |
| 209 |
-- |
| 210 |
function InterfaceToDisplay.resumeGraphDrawingCoroutine() |
| 211 |
|
| 212 |
-- Setup |
| 213 |
local scope = InterfaceCore.topScope() |
| 214 |
local binding = InterfaceCore.binding |
| 215 |
|
| 216 |
-- Asserts |
| 217 |
assert(scope.coroutine, "coroutine not created for current gd scope") |
| 218 |
|
| 219 |
-- Run |
| 220 |
local ok, text = coroutine.resume(scope.coroutine) |
| 221 |
assert(ok, text) |
| 222 |
if coroutine.status(scope.coroutine) ~= "dead" then |
| 223 |
-- Ok, ask binding to continue |
| 224 |
binding:resumeGraphDrawingCoroutine(text) |
| 225 |
end |
| 226 |
end |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
--- Ends the current graph drawing scope. |
| 231 |
-- |
| 232 |
function InterfaceToDisplay.endGraphDrawingScope() |
| 233 |
assert(#InterfaceCore.scopes > 0, "no gd scope open") |
| 234 |
InterfaceCore.scopes[#InterfaceCore.scopes] = nil -- pop |
| 235 |
end |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
--- |
| 241 |
-- Creates a new vertex in the syntactic graph of the current graph |
| 242 |
-- drawing scope. The display layer should call this function for each |
| 243 |
-- node of the graph. The |name| must be a unique string identifying |
| 244 |
-- the node. The newly created vertex will be added to the syntactic |
| 245 |
-- digraph. The binding function |everyVertexCreation| will then be |
| 246 |
-- called, allowing the binding to store information regarding the newly |
| 247 |
-- created vertex. |
| 248 |
-- |
| 249 |
-- For each vertex an event will be created in the event |
| 250 |
-- sequence. This event will have the kind |"node"| and its |
| 251 |
-- |parameter| will be the vertex. |
| 252 |
-- |
| 253 |
-- @param name Name of the vertex. |
| 254 |
-- |
| 255 |
-- @param shape The shape of the vertex such as |"circle"| or |
| 256 |
-- |"rectangle"|. This shape may help a graph drawing algorithm |
| 257 |
-- figuring out how the node should be placed. |
| 258 |
-- |
| 259 |
-- @param path A |Path| object representing the vertex's path. |
| 260 |
-- |
| 261 |
-- @param height The to-be-used height of the options stack. All |
| 262 |
-- options above this height will be popped prior to attacking the |
| 263 |
-- options to the syntactic digraph. |
| 264 |
-- |
| 265 |
-- @param binding_infos These options are passed to and are specific |
| 266 |
-- to the current |Binding|. |
| 267 |
-- |
| 268 |
-- @param anchors A table of anchors (mapping anchor positions to |
| 269 |
-- |Coordinates|). |
| 270 |
|
| 271 |
|
| 272 |
function InterfaceToDisplay.createVertex(name, shape, path, height, binding_infos, anchors) |
| 273 |
|
| 274 |
-- The path should never be empty, so we create a trivial path in the provided |
| 275 |
-- path is empty. This occurs with the ‘coordinate‘ shape for example. |
| 276 |
if #path == 0 then |
| 277 |
path:appendMoveto(0, 0) |
| 278 |
path:appendClosepath() |
| 279 |
end |
| 280 |
|
| 281 |
-- Setup |
| 282 |
local scope = InterfaceCore.topScope() |
| 283 |
local binding = InterfaceCore.binding |
| 284 |
|
| 285 |
-- Does vertex already exist? |
| 286 |
local v = scope.node_names[name] |
| 287 |
assert (not v or not v.created_on_display_layer, "node already created") |
| 288 |
|
| 289 |
-- Create vertex |
| 290 |
if not v then |
| 291 |
v = Vertex.new { |
| 292 |
name = name, |
| 293 |
shape = shape, |
| 294 |
kind = "node", |
| 295 |
path = path, |
| 296 |
options = get_current_options_table(height), |
| 297 |
anchors = anchors, |
| 298 |
} |
| 299 |
|
| 300 |
vertex_created(v,scope) |
| 301 |
else |
| 302 |
assert(v.kind == "subgraph node", "subgraph node expected") |
| 303 |
v.shape = shape |
| 304 |
v.path = path |
| 305 |
v.anchors = anchors |
| 306 |
end |
| 307 |
|
| 308 |
v.created_on_display_layer = true |
| 309 |
|
| 310 |
-- Call binding |
| 311 |
binding.storage[v] = binding_infos |
| 312 |
binding:everyVertexCreation(v) |
| 313 |
end |
| 314 |
|
| 315 |
|
| 316 |
-- This is a helper function |
| 317 |
function vertex_created(v,scope) |
| 318 |
|
| 319 |
-- Create Event |
| 320 |
local e = InterfaceToDisplay.createEvent ("node", v) |
| 321 |
v.event = e |
| 322 |
|
| 323 |
-- Create name lookup |
| 324 |
scope.node_names[v.name] = v |
| 325 |
|
| 326 |
-- Add vertex to graph |
| 327 |
scope.syntactic_digraph:add {v} |
| 328 |
|
| 329 |
-- Add to collections |
| 330 |
for _,c in ipairs(v.options.collections) do |
| 331 |
LookupTable.addOne(c.vertices, v) |
| 332 |
end |
| 333 |
|
| 334 |
end |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
--- |
| 339 |
-- Creates a new vertex in the syntactic graph of the current graph |
| 340 |
-- drawing scope that is a subgraph vertex. Such a vertex |
| 341 |
-- ``surrounds'' the vertices of a subgraph. The special property of a |
| 342 |
-- subgraph node opposed to a normal node is that it is created only |
| 343 |
-- after the subgraph has been laid out. However, the difference to a |
| 344 |
-- collection like |hyper| is that the node is availble immediately as |
| 345 |
-- a normal node in the sense that you can connect edges to it. |
| 346 |
-- |
| 347 |
-- What happens internally is that subgraph nodes get ``registered'' |
| 348 |
-- immediately both on the display level and on the algorithm level, |
| 349 |
-- but the actual node is only created inside the layout pipeline |
| 350 |
-- using a callback of the binding. The present function is used to |
| 351 |
-- perform this registering. The node creation happens when the |
| 352 |
-- innermost layout in which the subgraph node is declared has |
| 353 |
-- finished. For each subgraph node, a collection is created that |
| 354 |
-- contains all vertices (and edges) being part of the subgraph. For |
| 355 |
-- this reason, this method is a |push...| method, since it pushes |
| 356 |
-- something on the options stack. |
| 357 |
-- |
| 358 |
-- The |init| parameter will be used during the creation of the node, |
| 359 |
-- see |Binding:createVertex| for details on the fields. Note that |
| 360 |
-- |init.text| is often not displayed for such ``vast'' nodes as those |
| 361 |
-- created for whole subgraphs, but a shape may use it nevertheless |
| 362 |
-- (for instance, one might display this text at the top of the node |
| 363 |
-- or, in case of a \textsc{uml} package, in a special box above the |
| 364 |
-- actual node). |
| 365 |
-- |
| 366 |
-- The |init.generated_options| will be augmented by additional |
| 367 |
-- key--value pairs when the vertex is created: |
| 368 |
-- |
| 369 |
-- \begin{itemize} |
| 370 |
-- \item The key |subgraph point cloud| will have as its value a |
| 371 |
-- string that is be a list of points (without |
| 372 |
-- separating commas) like |"(10pt,20pt)(0pt,0pt)(30pt,40pt)"|, always in |
| 373 |
-- this syntax. The list will contain all points inside the |
| 374 |
-- subgraph. In particular, a bounding box around these points will |
| 375 |
-- encompass all nodes and bend points of the subgraph. |
| 376 |
-- The bounding box of this point cloud is guaranteed to be centered on |
| 377 |
-- the origin. |
| 378 |
-- \item The key |subgraph bounding box width| will have as its value |
| 379 |
-- the width of a bounding box (in \TeX\ points, as a string with the |
| 380 |
-- suffix |"pt"|). |
| 381 |
-- \item The key |subgraph bounding box height| stores the height of a |
| 382 |
-- bounding box. |
| 383 |
-- \end{itemize} |
| 384 |
-- |
| 385 |
-- @param name The name of the node. |
| 386 |
-- @param height Height of the options stack. Note that this method |
| 387 |
-- pushes something (namely a collection) on the options stack. |
| 388 |
-- @param info A table passed to |Binding:createVertex|, see that function. |
| 389 |
-- |
| 390 |
function InterfaceToDisplay.pushSubgraphVertex(name, height, info) |
| 391 |
|
| 392 |
-- Setup |
| 393 |
local scope = InterfaceCore.topScope() |
| 394 |
local binding = InterfaceCore.binding |
| 395 |
|
| 396 |
-- Does vertex already exist? |
| 397 |
assert (not scope.node_names[name], "node already created") |
| 398 |
|
| 399 |
-- Create vertex |
| 400 |
local v = Vertex.new { |
| 401 |
name = name, |
| 402 |
kind = "subgraph node", |
| 403 |
options = get_current_options_table(height-1) |
| 404 |
} |
| 405 |
|
| 406 |
vertex_created(v,scope) |
| 407 |
|
| 408 |
-- Store info |
| 409 |
info.generated_options = info.generated_options or {} |
| 410 |
info.name = name |
| 411 |
v.subgraph_info = info |
| 412 |
|
| 413 |
-- Create collection and link it to v |
| 414 |
local _, _, entry = InterfaceToDisplay.pushOption(InterfaceCore.subgraph_node_kind, nil, height) |
| 415 |
v.subgraph_collection = entry.value |
| 416 |
v.subgraph_collection.subgraph_node = v |
| 417 |
|
| 418 |
-- Find parent collection in options stack: |
| 419 |
local collections = v.options.collections |
| 420 |
for i=#collections,1,-1 do |
| 421 |
if collections[i].kind == InterfaceCore.sublayout_kind then |
| 422 |
v.subgraph_collection.parent_layout = collections[i] |
| 423 |
break |
| 424 |
end |
| 425 |
end |
| 426 |
end |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
--- |
| 431 |
-- Add options for an already existing vertex. |
| 432 |
-- |
| 433 |
-- This function allows you to add options to an already existing |
| 434 |
-- vertex. The options that will be added are all options on the |
| 435 |
-- current options stack; they will overwrite existing options of the |
| 436 |
-- same name. For collections, the vertex stays in all collections it |
| 437 |
-- used to, it is only added to all collections that are currently on |
| 438 |
-- the options stack. |
| 439 |
-- |
| 440 |
-- @param name Name of the vertex. |
| 441 |
-- @param height The option stack height. |
| 442 |
|
| 443 |
function InterfaceToDisplay.addToVertexOptions(name, height) |
| 444 |
|
| 445 |
-- Setup |
| 446 |
local scope = InterfaceCore.topScope() |
| 447 |
|
| 448 |
-- Does vertex already exist? |
| 449 |
local v = assert (scope.node_names[name], "node is missing, cannot add options") |
| 450 |
|
| 451 |
v.options = get_current_options_table(height, v.options) |
| 452 |
|
| 453 |
-- Add to collections |
| 454 |
for _,c in ipairs(v.options.collections) do |
| 455 |
LookupTable.addOne(c.vertices, v) |
| 456 |
end |
| 457 |
|
| 458 |
end |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
--- |
| 465 |
-- Creates a new edge in the syntactic graph of the current graph |
| 466 |
-- drawing scope. The display layer should call this function for each |
| 467 |
-- edge that is created. Both the |from| vertex and the |to| vertex |
| 468 |
-- must exist (have been created through |createVertex|) prior to your |
| 469 |
-- being able to call this function. |
| 470 |
-- |
| 471 |
-- After the edge has been created, the binding layer's function |
| 472 |
-- |everyEdgeCreation| will be called, allowing the binding layer to |
| 473 |
-- store information about the edge. |
| 474 |
-- |
| 475 |
-- For each edge an event is created, whose kind is |"edge"| and whose |
| 476 |
-- |parameter| is a two-element array whose first entry is the edge's |
| 477 |
-- arc in the syntactic digraph and whose second entry is the position |
| 478 |
-- of the edge in the arc's array of syntactic edges. |
| 479 |
-- |
| 480 |
-- @param tail Name of the node the edge begins at. |
| 481 |
-- @param head Name of the node the edge ends at. |
| 482 |
-- @param direction Direction of the edge (e.g. |--| for an undirected edge |
| 483 |
-- or |->| for a directed edge from the first to the second |
| 484 |
-- node). |
| 485 |
-- @param height The option stack height, see for instance |createVertex|. |
| 486 |
-- |
| 487 |
-- @param binding_infos These options will be stored in the |storage| |
| 488 |
-- of the vertex at the field index by the binding. |
| 489 |
|
| 490 |
function InterfaceToDisplay.createEdge(tail, head, direction, height, binding_infos) |
| 491 |
|
| 492 |
-- Setup |
| 493 |
local scope = InterfaceCore.topScope() |
| 494 |
local binding = InterfaceCore.binding |
| 495 |
|
| 496 |
-- Does vertex already exist? |
| 497 |
local h = scope.node_names[head] |
| 498 |
local t = scope.node_names[tail] |
| 499 |
assert (h and t, "attempting to create edge between nodes that are not in the graph") |
| 500 |
|
| 501 |
-- Create Arc object |
| 502 |
local arc = scope.syntactic_digraph:connect(t, h) |
| 503 |
|
| 504 |
-- Create Edge object |
| 505 |
local edge = Edge.new { |
| 506 |
head = h, |
| 507 |
tail = t, |
| 508 |
direction = direction, |
| 509 |
options = get_current_options_table(height) |
| 510 |
} |
| 511 |
|
| 512 |
-- Add to arc |
| 513 |
arc.syntactic_edges[#arc.syntactic_edges+1] = edge |
| 514 |
|
| 515 |
-- Create Event |
| 516 |
local e = InterfaceToDisplay.createEvent ("edge", { arc, #arc.syntactic_edges }) |
| 517 |
edge.event = e |
| 518 |
|
| 519 |
-- Make part of collections |
| 520 |
for _,c in ipairs(edge.options.collections) do |
| 521 |
LookupTable.addOne(c.edges, edge) |
| 522 |
end |
| 523 |
|
| 524 |
-- Call binding |
| 525 |
binding.storage[edge] = binding_infos |
| 526 |
binding:everyEdgeCreation(edge) |
| 527 |
|
| 528 |
end |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
--- |
| 535 |
-- Push an option to the stack of options. |
| 536 |
-- |
| 537 |
-- As a graph is parsed, a stack of ``current options'' |
| 538 |
-- is created. To add something to this table, the display layers may |
| 539 |
-- call the method |pushOption|. To pop something from this stack, |
| 540 |
-- just set the |height| value during the next push to the position to |
| 541 |
-- which you actually wish to push something; everything above and |
| 542 |
-- including this position will be popped from the stack. |
| 543 |
-- |
| 544 |
-- When an option is pushed, several additional options may also be |
| 545 |
-- pushed, namely whenever the option has a |use| field set. These |
| 546 |
-- additional options may, in turn, also push new options. Because of |
| 547 |
-- this, this function returns a new stack height, representing the |
| 548 |
-- resulting stack height. |
| 549 |
-- |
| 550 |
-- In addition to this stack height, this function returns a Boolean |
| 551 |
-- value indicating whether a ``main algorithm phase was set.'' This |
| 552 |
-- happens whenever a key is executed (directly or indirectly through |
| 553 |
-- the |use| field) that selects an algorithm for the ``main'' |
| 554 |
-- algorithm phase. This information may help the caller to setup the |
| 555 |
-- graph drawing scopes correctly. |
| 556 |
-- |
| 557 |
-- @param key A parameter (must be a string). |
| 558 |
-- @param value A value (can be anything). If it is a string, it will |
| 559 |
-- be converted to whatever the key expects. |
| 560 |
-- @param height A stack height at which to insert the key. Everything |
| 561 |
-- above this height will be removed. |
| 562 |
-- |
| 563 |
-- @return A new stack height |
| 564 |
-- @return A Boolean that is |true| iff the main algorithm phase was |
| 565 |
-- set by the option or one option |use|d by it. |
| 566 |
-- @return The newly created entry on the stack. If more entries are |
| 567 |
-- created through the use of the |use| field, the original entry is |
| 568 |
-- returned nevertheless. |
| 569 |
|
| 570 |
|
| 571 |
function InterfaceToDisplay.pushOption(key, value, height) |
| 572 |
assert(type(key) == "string", "illegal key") |
| 573 |
|
| 574 |
local key_record = assert(InterfaceCore.keys[key], "unknown key") |
| 575 |
local main_phase_set = false |
| 576 |
|
| 577 |
if value == nil and key_record.default then |
| 578 |
value = key_record.default |
| 579 |
end |
| 580 |
|
| 581 |
-- Find out what kind of key we are pushing: |
| 582 |
|
| 583 |
if key_record.algorithm then |
| 584 |
-- Push a phase |
| 585 |
if type(InterfaceCore.algorithm_classes[key]) == "function" then |
| 586 |
-- Call the constructor function |
| 587 |
InterfaceCore.algorithm_classes[key] = InterfaceCore.algorithm_classes[key]() |
| 588 |
end |
| 589 |
|
| 590 |
local algorithm = InterfaceCore.algorithm_classes[key] |
| 591 |
|
| 592 |
assert (algorithm, "algorithm class not found") |
| 593 |
|
| 594 |
push_on_option_stack(phase_unique, |
| 595 |
{ phase = key_record.phase, algorithm = algorithm }, |
| 596 |
height) |
| 597 |
|
| 598 |
if key_record.phase == "main" then |
| 599 |
main_phase_set = true |
| 600 |
end |
| 601 |
|
| 602 |
elseif key_record.layer then |
| 603 |
-- Push a collection |
| 604 |
local stack = InterfaceCore.option_stack |
| 605 |
local scope = InterfaceCore.topScope() |
| 606 |
|
| 607 |
-- Get the stack above "height": |
| 608 |
local options = get_current_options_table(height-1) |
| 609 |
|
| 610 |
-- Create the collection event |
| 611 |
local event = InterfaceToDisplay.createEvent ("collection", key) |
| 612 |
|
| 613 |
-- Create collection object: |
| 614 |
local collection = Collection.new { kind = key, options = options, event = event } |
| 615 |
|
| 616 |
-- Store in collections table of current scope: |
| 617 |
local collections = scope.collections[key] or {} |
| 618 |
collections[#collections + 1] = collection |
| 619 |
scope.collections[key] = collections |
| 620 |
|
| 621 |
-- Build collection tree |
| 622 |
collection:registerAsChildOf(options.collections[#options.collections]) |
| 623 |
|
| 624 |
-- Push on stack |
| 625 |
push_on_option_stack(collections_unique, collection, height) |
| 626 |
|
| 627 |
else |
| 628 |
|
| 629 |
-- A normal key |
| 630 |
push_on_option_stack(key, InterfaceCore.convert(value, InterfaceCore.keys[key].type), height) |
| 631 |
|
| 632 |
end |
| 633 |
|
| 634 |
local newly_created = InterfaceCore.option_stack[#InterfaceCore.option_stack] |
| 635 |
|
| 636 |
-- Now, push use keys: |
| 637 |
local use = key_record.use |
| 638 |
if key_record.use then |
| 639 |
local flag |
| 640 |
for _,u in ipairs(InterfaceCore.keys[key].use) do |
| 641 |
local use_k = u.key |
| 642 |
local use_v = u.value |
| 643 |
if type(use_k) == "function" then |
| 644 |
use_k = use_k(value) |
| 645 |
end |
| 646 |
if type(use_v) == "function" then |
| 647 |
use_v = use_v(value) |
| 648 |
end |
| 649 |
height, flag = InterfaceToDisplay.pushOption(use_k, use_v, height+1) |
| 650 |
main_phase_set = main_phase_set or flag |
| 651 |
end |
| 652 |
end |
| 653 |
|
| 654 |
return height, main_phase_set, newly_created |
| 655 |
end |
| 656 |
|
| 657 |
|
| 658 |
--- |
| 659 |
-- Push a layout on the stack of options. As long as this layout is on |
| 660 |
-- the stack, all vertices and edges will be part of this layout. For |
| 661 |
-- details on layouts, please see |Sublayouts|. |
| 662 |
-- |
| 663 |
-- @param height A stack height at which to insert the key. Everything |
| 664 |
-- above this height will be removed. |
| 665 |
|
| 666 |
function InterfaceToDisplay.pushLayout(height) |
| 667 |
InterfaceToDisplay.pushOption(InterfaceCore.sublayout_kind, nil, height) |
| 668 |
end |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
--- |
| 673 |
-- Creates an event and adds it to the event string of the current scope. |
| 674 |
-- |
| 675 |
-- @param kind Name/kind of the event. |
| 676 |
-- @param parameters Parameters of the event. |
| 677 |
-- |
| 678 |
-- @return The newly pushed event |
| 679 |
-- |
| 680 |
function InterfaceToDisplay.createEvent(kind, param) |
| 681 |
local scope = InterfaceCore.topScope() |
| 682 |
local n = #scope.events + 1 |
| 683 |
local e = Event.new { kind = kind, parameters = param, index = n } |
| 684 |
scope.events[n] = e |
| 685 |
|
| 686 |
return e |
| 687 |
end |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
--- |
| 692 |
-- This method allows you to query the table of all declared keys. It |
| 693 |
-- contains them both as an array and also as a table index by the |
| 694 |
-- keys's names. In particular, you can then iterate over it using |
| 695 |
-- |ipairs| and you can check whether a key is defined by accessing |
| 696 |
-- the table at the key's name. Each entry of the table is the |
| 697 |
-- original table passed to |InterfaceToAlgorithms.declare|. |
| 698 |
-- |
| 699 |
-- @return A lookup table of all declared keys. |
| 700 |
|
| 701 |
function InterfaceToDisplay.getDeclaredKeys() |
| 702 |
return InterfaceCore.keys |
| 703 |
end |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
--- |
| 709 |
-- Renders the graph. |
| 710 |
-- |
| 711 |
-- This function is called after the graph has been laid out by the |
| 712 |
-- graph drawing algorithms. It will trigger a sequence of calls to |
| 713 |
-- the binding layer that will, via callbacks, start rendering the |
| 714 |
-- whole graph. |
| 715 |
-- |
| 716 |
-- In detail, this function calls: |
| 717 |
-- |
| 718 |
--\begin{codeexample}[code only, tikz syntax=false] |
| 719 |
--local binding = InterfaceCore.binding |
| 720 |
-- |
| 721 |
--binding:renderStart() |
| 722 |
--render_vertices() |
| 723 |
--render_edges() |
| 724 |
--render_collections() |
| 725 |
--binding:renderStop() |
| 726 |
--\end{codeexample} |
| 727 |
-- |
| 728 |
-- Here, the |render_...| functions are local, internal functions that are, |
| 729 |
-- nevertheless, documented here. |
| 730 |
-- |
| 731 |
-- @param name Returns the algorithm class that has been declared using |
| 732 |
-- |declare| under the given name. |
| 733 |
|
| 734 |
function InterfaceToDisplay.renderGraph() |
| 735 |
local scope = InterfaceCore.topScope() |
| 736 |
local syntactic_digraph = scope.syntactic_digraph |
| 737 |
|
| 738 |
local binding = InterfaceCore.binding |
| 739 |
|
| 740 |
binding:renderStart() |
| 741 |
render_vertices(syntactic_digraph.vertices) |
| 742 |
render_edges(syntactic_digraph.arcs) |
| 743 |
render_collections(scope.collections) |
| 744 |
binding:renderStop() |
| 745 |
end |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
--- |
| 752 |
-- Render the vertices after the graph drawing algorithm has |
| 753 |
-- finished. This function is local and internal and included only for |
| 754 |
-- documenting the call graph. |
| 755 |
-- |
| 756 |
-- When the graph drawing algorithm is done, the interface will start |
| 757 |
-- rendering the vertices by calling appropriate callbacks of the |
| 758 |
-- binding layer. |
| 759 |
-- |
| 760 |
-- Consider the following code: |
| 761 |
--\begin{codeexample}[code only] |
| 762 |
--\graph [... layout] { |
| 763 |
-- a -- b -- c -- d; |
| 764 |
--}; |
| 765 |
--\end{codeexample} |
| 766 |
-- |
| 767 |
-- In this case, after the graph drawing algorithm has run, the |
| 768 |
-- present function will call: |
| 769 |
-- |
| 770 |
--\begin{codeexample}[code only, tikz syntax=false] |
| 771 |
--local binding = InterfaceCore.binding |
| 772 |
-- |
| 773 |
--binding:renderVerticesStart() |
| 774 |
--binding:renderVertex(vertex_a) |
| 775 |
--binding:renderVertex(vertex_b) |
| 776 |
--binding:renderVertex(vertex_c) |
| 777 |
--binding:renderVertex(vertex_d) |
| 778 |
--binding:renderVerticesStop() |
| 779 |
--\end{codeexample} |
| 780 |
-- |
| 781 |
-- @param vertices An array of all vertices in the syntactic digraph. |
| 782 |
|
| 783 |
function render_vertices(vertices) |
| 784 |
InterfaceCore.binding:renderVerticesStart() |
| 785 |
for _,vertex in ipairs(vertices) do |
| 786 |
InterfaceCore.binding:renderVertex(vertex) |
| 787 |
end |
| 788 |
InterfaceCore.binding:renderVerticesStop() |
| 789 |
end |
| 790 |
|
| 791 |
|
| 792 |
--- |
| 793 |
-- Render the collections whose layer is not |0|. This local, internal |
| 794 |
-- function is called to render the different collection kinds. |
| 795 |
-- |
| 796 |
-- Collection kinds rendered in the order provided by the |layer| |
| 797 |
-- field passed to |declare| during the declaration of the colleciton |
| 798 |
-- kind, see also |declare_collection|. If several collection kinds |
| 799 |
-- have the same layer, they are rendered in lexicographical ordering |
| 800 |
-- (to ensure that they are always rendered in the same order). |
| 801 |
-- |
| 802 |
-- Consider the following code: |
| 803 |
--\begin{codeexample}[code only, tikz syntax=false] |
| 804 |
--declare { key = "hyper", layer = 1 } |
| 805 |
--\end{codeexample} |
| 806 |
-- you can say on the \tikzname\ layer |
| 807 |
--\begin{codeexample}[code only] |
| 808 |
--\graph { |
| 809 |
-- a, b, c, d; |
| 810 |
-- { [hyper] a, b, c } |
| 811 |
-- { [hyper] b, c, d } |
| 812 |
--}; |
| 813 |
--\end{codeexample} |
| 814 |
-- |
| 815 |
-- In this case, after the graph drawing algorithm has run, the |
| 816 |
-- present function will call: |
| 817 |
-- |
| 818 |
--\begin{codeexample}[code only, tikz syntax=false] |
| 819 |
--local binding = InterfaceCore.binding |
| 820 |
-- |
| 821 |
--binding:renderCollectionStartKind("hyper", 1) |
| 822 |
--binding:renderCollection(collection_containing_abc) |
| 823 |
--binding:renderCollection(collection_containing_bcd) |
| 824 |
--binding:renderCollectionStopKind("hyper", 1) |
| 825 |
--\end{codeexample} |
| 826 |
-- |
| 827 |
-- @param collections The |collections| table of the current scope. |
| 828 |
|
| 829 |
function render_collections(collections) |
| 830 |
local kinds = InterfaceCore.collection_kinds |
| 831 |
local binding = InterfaceCore.binding |
| 832 |
|
| 833 |
for i=1,#kinds do |
| 834 |
local kind = kinds[i].kind |
| 835 |
local layer = kinds[i].layer |
| 836 |
|
| 837 |
if layer ~= 0 then |
| 838 |
binding:renderCollectionStartKind(kind, layer) |
| 839 |
for _,c in ipairs(collections[kind] or {}) do |
| 840 |
binding:renderCollection(c) |
| 841 |
end |
| 842 |
binding:renderCollectionStopKind(kind, layer) |
| 843 |
end |
| 844 |
end |
| 845 |
end |
| 846 |
|
| 847 |
|
| 848 |
--- |
| 849 |
-- Render the syntactic edges of a graph after the graph drawing |
| 850 |
-- algorithm has finished. This function is local and internal and included only |
| 851 |
-- for documenting the call graph. |
| 852 |
-- |
| 853 |
-- When the graph drawing algorithm is done, the interface will first |
| 854 |
-- rendering the vertices using |render_vertices|, followed by calling |
| 855 |
-- this function, which in turn calls appropriate callbacks to the |
| 856 |
-- binding layer. |
| 857 |
-- |
| 858 |
-- Consider the following code: |
| 859 |
--\begin{codeexample}[code only] |
| 860 |
-- \graph [... layout] { |
| 861 |
-- a -- b -- c -- d; |
| 862 |
-- }; |
| 863 |
--\end{codeexample} |
| 864 |
-- |
| 865 |
-- In this case, after the graph drawing algorithm has run, the |
| 866 |
-- present function will call: |
| 867 |
-- |
| 868 |
--\begin{codeexample}[code only, tikz syntax=false] |
| 869 |
-- local binding = InterfaceCore.binding |
| 870 |
-- |
| 871 |
-- binding:renderEdgesStart() |
| 872 |
-- binding:renderEdge(edge_from_a_to_b) |
| 873 |
-- binding:renderEdge(edge_from_b_to_c) |
| 874 |
-- binding:renderEdge(edge_from_c_to_d) |
| 875 |
-- binding:renderEdgesStop() |
| 876 |
--\end{codeexample} |
| 877 |
-- |
| 878 |
-- @param arcs The array of arcs of the syntactic digraph. |
| 879 |
|
| 880 |
function render_edges(arcs) |
| 881 |
InterfaceCore.binding:renderEdgesStart() |
| 882 |
for _,a in ipairs(arcs) do |
| 883 |
for _,e in ipairs (a.syntactic_edges) do |
| 884 |
InterfaceCore.binding:renderEdge(e) |
| 885 |
end |
| 886 |
end |
| 887 |
InterfaceCore.binding:renderEdgesStop() |
| 888 |
end |
| 889 |
|
| 890 |
|
| 891 |
local aliases = InterfaceCore.option_aliases |
| 892 |
local option_initial = InterfaceCore.option_initial |
| 893 |
|
| 894 |
local option_metatable = { |
| 895 |
__index = |
| 896 |
function (t, key) |
| 897 |
local k = aliases[key] |
| 898 |
if k then |
| 899 |
local v = (type(k) == "string" and t[k]) or (type(k) == "function" and k(t)) or nil |
| 900 |
if v ~= nil then |
| 901 |
return v |
| 902 |
end |
| 903 |
end |
| 904 |
return option_initial[key] |
| 905 |
end |
| 906 |
} |
| 907 |
|
| 908 |
|
| 909 |
--- |
| 910 |
-- Get the current options table. |
| 911 |
-- |
| 912 |
-- An option table can be accessed like a normal table; however, there |
| 913 |
-- is a global fallback for this table. If an index is not defined, |
| 914 |
-- the value of this index in the global fallback table is used. (This |
| 915 |
-- reduces the overall amount of option keys that need to be stored |
| 916 |
-- with object.) |
| 917 |
-- |
| 918 |
-- (This function is local and internal and included only for documentation |
| 919 |
-- purposes.) |
| 920 |
-- |
| 921 |
-- @param height The stack height for which the option table is |
| 922 |
-- required. |
| 923 |
-- @param table If non |nil|, the options will be added to this |
| 924 |
-- table. |
| 925 |
-- |
| 926 |
-- @return The option table as described above. |
| 927 |
|
| 928 |
function get_current_options_table (height, table) |
| 929 |
local stack = InterfaceCore.option_stack |
| 930 |
assert (height >= 0 and height <= #stack, "height value out of bounds") |
| 931 |
|
| 932 |
if height == InterfaceCore.option_cache_height and not table then |
| 933 |
return option_cache |
| 934 |
else |
| 935 |
-- Clear superfluous part of stack |
| 936 |
for i=#stack,height+1,-1 do |
| 937 |
stack[i] = nil |
| 938 |
end |
| 939 |
|
| 940 |
-- Build options table |
| 941 |
local cache |
| 942 |
if not table then |
| 943 |
cache = setmetatable( |
| 944 |
{ |
| 945 |
algorithm_phases = setmetatable({}, InterfaceCore.option_initial.algorithm_phases), |
| 946 |
collections = {} |
| 947 |
}, option_metatable) |
| 948 |
else |
| 949 |
cache = lib.copy(table) |
| 950 |
cache.algorithm_phases = lib.copy(cache.algorithm_phases) |
| 951 |
cache.collections = lib.copy(cache.collections) |
| 952 |
end |
| 953 |
|
| 954 |
local algorithm_phases = cache.algorithm_phases |
| 955 |
local collections = cache.collections |
| 956 |
local keys = InterfaceCore.keys |
| 957 |
|
| 958 |
local function handle (k, v) |
| 959 |
if k == phase_unique then |
| 960 |
algorithm_phases[v.phase] = v.algorithm |
| 961 |
elseif k == collections_unique then |
| 962 |
LookupTable.addOne(collections, v) |
| 963 |
else |
| 964 |
cache[k] = v |
| 965 |
end |
| 966 |
end |
| 967 |
|
| 968 |
for _,s in ipairs(stack) do |
| 969 |
handle (s.key, s.value) |
| 970 |
end |
| 971 |
|
| 972 |
-- Cache it, if this was not added: |
| 973 |
if not table then |
| 974 |
InterfaceCore.option_cache_height = height |
| 975 |
option_cache = cache |
| 976 |
end |
| 977 |
|
| 978 |
return cache |
| 979 |
end |
| 980 |
end |
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
-- A helper function |
| 985 |
|
| 986 |
function push_on_option_stack(key, value, height) |
| 987 |
local stack = InterfaceCore.option_stack |
| 988 |
|
| 989 |
assert (type(height) == "number" and height > 0 and height <= #stack + 1, |
| 990 |
"height value out of bounds") |
| 991 |
|
| 992 |
-- Clear superfluous part of stack |
| 993 |
for i=#stack,height+1,-1 do |
| 994 |
stack[i] = nil |
| 995 |
end |
| 996 |
|
| 997 |
stack[height] = { key = key, value = value } |
| 998 |
InterfaceCore.option_cache_height = nil -- invalidate cache |
| 999 |
end |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
-- Done |
| 1004 |
|
| 1005 |
return InterfaceToDisplay |