Skip to content

Commit 9ad18ca

Browse files
Don't spoiler graphs, refactor heading nesting
1 parent 01fda64 commit 9ad18ca

File tree

1 file changed

+24
-44
lines changed

1 file changed

+24
-44
lines changed

tutorials/modules/ROOT/pages/graph-viz.adoc

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ setup(driver, SCHEMA, DATA)
9090
----
9191
====
9292

93-
== Background: Analyzing queries and PipelineStructure
93+
== Analyze & the query graph
9494

9595
TypeDB 3.7.0 introduces the `analyze` operation. Analyzing a query
9696
returns its pipeline structure, along with typing information for the
@@ -184,7 +184,7 @@ constraints
184184
----
185185
====
186186

187-
== From constraints to the query graph
187+
=== From constraints to the query graph
188188

189189
Before we get to visualising the data returned by queries, We’ll turn
190190
the constraints in our query into a query-graph and visualise it. We’ll
@@ -235,13 +235,9 @@ node_labels = {node: str(node) for node in query_graph.nodes()}
235235
networkx.draw(query_graph, labels=node_labels)
236236
pyplot.show()
237237
----
238-
.Output
239-
[%collapsible]
240-
====
241238
image:output_11_0.png[]
242-
====
243239

244-
== Better formatting
240+
=== Prettier drawings
245241

246242
That’s the query graph, but it’s of little use to the viewer. We’ll
247243
create a `node_style` function which specifies some style attributes,
@@ -288,11 +284,7 @@ node_styles = { n: node_style(pipeline, n) for n in nodes}
288284
# Draw
289285
draw(query_edges, node_styles)
290286
----
291-
.Output
292-
[%collapsible]
293-
====
294287
image:output_13_0.png[]
295-
====
296288

297289
== Visualising Data
298290

@@ -348,7 +340,7 @@ list(answers[0].query_structure().stages())
348340
----
349341
====
350342

351-
== From query structure and rows to graphs
343+
=== From query structure and rows to graphs
352344

353345
The graph view sees pattern matching as finding a homomorphism between
354346
the query-graph and some subgraph in the database. An answer is thus the
@@ -395,7 +387,7 @@ answers_as_data_edges
395387
----
396388
====
397389

398-
== Visualising
390+
=== Drawing the graph
399391

400392
We’ll need a new node style since our vertices are now concepts rather
401393
than `ConstraintVertex`es.
@@ -431,13 +423,9 @@ node_styles = { n: data_node_style(n) for n in nodes}
431423
draw(data_edges, node_styles)
432424
433425
----
434-
.Output
435-
[%collapsible]
436-
====
437426
image:output_20_0.png[]
438-
====
439427

440-
== Disjunctions and optionals
428+
=== Disjunctions and optionals
441429

442430
TypeQL queries can be more than simple conjunctions of patterns. They
443431
can also contain disjunctions & optional patterns - meaning not every
@@ -484,6 +472,7 @@ trunk = list(pipeline.conjunction(trunk_id).constraints())
484472
.Output
485473
[%collapsible]
486474
====
475+
[source, rust]
487476
----
488477
(
489478
2,
@@ -506,6 +495,7 @@ branches
506495
.Output
507496
[%collapsible]
508497
====
498+
[source, rust]
509499
----
510500
{
511501
0: [
@@ -533,6 +523,7 @@ an `involved_conjunctions` method which tells us exactly this.
533523
.Output
534524
[%collapsible]
535525
====
526+
[source, rust]
536527
----
537528
[
538529
[1, 2],
@@ -604,12 +595,7 @@ node_styles = { n: data_node_style(n) for n in nodes }
604595
# Draw
605596
draw(flattened_data_edges, node_styles)
606597
----
607-
608-
.Output
609-
[%collapsible]
610-
====
611598
image:output_30_0.png[]
612-
====
613599

614600
This covers the basics of visualising TypeDB answers as graphs. The next
615601
section discusses a few cases where TypeDB constraints don’t directly
@@ -618,12 +604,12 @@ introduce a simple python library which computes involved constraints
618604
and substitutes answers into the constraints - allowing you to focus on
619605
converting these "`DataConstraints`" into your target representation.
620606

621-
== Visualising complex constraints
607+
=== Notes on visualising constraints
622608

623609
In this section, we discuss a few cases where visualisation is not as
624610
straightforward and the solutions we chose when building TypeDB Studio.
625611

626-
=== Functions
612+
==== Functions
627613

628614
A query can contain function calls. Functions have arguments and return
629615
values - all of which are concepts. Unlike a relation, the function call
@@ -633,19 +619,19 @@ introduce a `FunctionCall` vertex for each call. Two function calls
633619
are the same vertex if they have the same (1) function name, (2) tuple
634620
of argument concepts, and (3) tuple of returned concepts.
635621

636-
=== Expressions
622+
==== Expressions
637623

638624
We treat expressions as we did functions, except the string representing
639625
the expression is used in place of the function name.
640626

641-
=== Links constraints
627+
==== Links constraints
642628

643629
Links constraints are ternary constraints involving the relation
644630
instance, the player instance and the played role type. We choose to
645631
represent it as a binary edge from the relation to the player, with the
646632
role serving as the label of the edge.
647633

648-
=== Named roles
634+
==== Named roles
649635

650636
TypeDB allows the use of unscoped role names in links & relates
651637
constraints to specify the role. e.g. For the schema below, the query
@@ -667,24 +653,22 @@ variable introduced is anonymous, it is unavailable in the ConceptRow.
667653
Since it does not uniquely determine the type, it cannot be considered a
668654
label.)
669655

670-
=== Is & comparison constraints
656+
==== Is & comparison constraints
671657

672658
Since `is` constraints are always a self-edge on a vertex, we choose
673659
not to visualise it. We also skip visualising comparison constraints to
674660
reduce the number of edges, though they can be useful in certain cases -
675661
the comparator symbol is available from the comparison constraint.
676662

677-
== The library
663+
== The `typedb-graph-utils` library
678664

679-
In this section, we introduce the library developed along with the
680-
tutorial. It follows the structure of the TypeScript library we wrote
681-
for TypeDB studio. The essence remains the same as what we’ve covered in
682-
the tutorial - Find the constraints "`involved`" in each answer, and
683-
substitute in the concepts for the variables. Instead of converting a
684-
constraint into query edges, and then applying the substitution to form
685-
data-edges, We directly apply the substitution on the `Constraint`s to
686-
obtain the corresponding `DataConstraint` - These constrain
687-
`DataVertex`es instead of `ConstraintVertex`es.
665+
In this section, we introduce the `typedb-graph-utils` python library developed alongside this tutorial.
666+
It follows the structure of the TypeScript library we wrote for TypeDB studio.
667+
The essence remains the same as what we’ve covered in the tutorial -
668+
Find the constraints "`involved`" in each answer, and substitute in the concepts for the variables.
669+
Instead of converting a constraint into query edges, and then applying the substitution to form data-edges,
670+
We directly apply the substitution on the `Constraint`s to obtain the corresponding `DataConstraint` -
671+
These constrain `DataVertex`es instead of `ConstraintVertex`es.
688672

689673
Here’s the signature of an `Isa` `DataConstraint` from the library,
690674
and the `Isa` `Constraint` from the driver API to compare against:
@@ -715,7 +699,7 @@ We provide a sample implementation -
715699
builds a `MultiDiGraph` as in this tutorial. We also provide a basic
716700
`MatplotlibVisualizer` for inspiration.
717701

718-
== Visualising using the library
702+
=== Creating a visualiser using the library
719703

720704
We recreate the previous example by implementing the
721705
`TypeDBAnswerConverter` class provided by the library.
@@ -770,8 +754,4 @@ for (i, answer) in enumerate(answers):
770754
graph = builder.finish()
771755
MatplotlibVisualizer.draw(graph)
772756
----
773-
.Output
774-
[%collapsible]
775-
====
776757
image:output_35_0.png[]
777-
====

0 commit comments

Comments
 (0)