@@ -90,7 +90,7 @@ setup(driver, SCHEMA, DATA)
9090----
9191====
9292
93- == Background: Analyzing queries and PipelineStructure
93+ == Analyze & the query graph
9494
9595TypeDB 3.7.0 introduces the `analyze` operation. Analyzing a query
9696returns 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
189189Before we get to visualising the data returned by queries, We’ll turn
190190the 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()}
235235networkx.draw(query_graph, labels=node_labels)
236236pyplot.show()
237237----
238- .Output
239- [%collapsible]
240- ====
241238image:output_11_0.png[]
242- ====
243239
244- == Better formatting
240+ === Prettier drawings
245241
246242That’s the query graph, but it’s of little use to the viewer. We’ll
247243create 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
289285draw(query_edges, node_styles)
290286----
291- .Output
292- [%collapsible]
293- ====
294287image: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
353345The graph view sees pattern matching as finding a homomorphism between
354346the 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
400392We’ll need a new node style since our vertices are now concepts rather
401393than `ConstraintVertex`es.
@@ -431,13 +423,9 @@ node_styles = { n: data_node_style(n) for n in nodes}
431423draw(data_edges, node_styles)
432424
433425----
434- .Output
435- [%collapsible]
436- ====
437426image:output_20_0.png[]
438- ====
439427
440- == Disjunctions and optionals
428+ === Disjunctions and optionals
441429
442430TypeQL queries can be more than simple conjunctions of patterns. They
443431can 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
605596draw(flattened_data_edges, node_styles)
606597----
607-
608- .Output
609- [%collapsible]
610- ====
611598image:output_30_0.png[]
612- ====
613599
614600This covers the basics of visualising TypeDB answers as graphs. The next
615601section discusses a few cases where TypeDB constraints don’t directly
@@ -618,12 +604,12 @@ introduce a simple python library which computes involved constraints
618604and substitutes answers into the constraints - allowing you to focus on
619605converting these "`DataConstraints`" into your target representation.
620606
621- == Visualising complex constraints
607+ === Notes on visualising constraints
622608
623609In this section, we discuss a few cases where visualisation is not as
624610straightforward and the solutions we chose when building TypeDB Studio.
625611
626- === Functions
612+ ==== Functions
627613
628614A query can contain function calls. Functions have arguments and return
629615values - 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
633619are the same vertex if they have the same (1) function name, (2) tuple
634620of argument concepts, and (3) tuple of returned concepts.
635621
636- === Expressions
622+ ==== Expressions
637623
638624We treat expressions as we did functions, except the string representing
639625the expression is used in place of the function name.
640626
641- === Links constraints
627+ ==== Links constraints
642628
643629Links constraints are ternary constraints involving the relation
644630instance, the player instance and the played role type. We choose to
645631represent it as a binary edge from the relation to the player, with the
646632role serving as the label of the edge.
647633
648- === Named roles
634+ ==== Named roles
649635
650636TypeDB allows the use of unscoped role names in links & relates
651637constraints 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.
667653Since it does not uniquely determine the type, it cannot be considered a
668654label.)
669655
670- === Is & comparison constraints
656+ ==== Is & comparison constraints
671657
672658Since `is` constraints are always a self-edge on a vertex, we choose
673659not to visualise it. We also skip visualising comparison constraints to
674660reduce the number of edges, though they can be useful in certain cases -
675661the 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
689673Here’s the signature of an `Isa` `DataConstraint` from the library,
690674and the `Isa` `Constraint` from the driver API to compare against:
@@ -715,7 +699,7 @@ We provide a sample implementation -
715699builds 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
720704We recreate the previous example by implementing the
721705`TypeDBAnswerConverter` class provided by the library.
@@ -770,8 +754,4 @@ for (i, answer) in enumerate(answers):
770754graph = builder.finish()
771755MatplotlibVisualizer.draw(graph)
772756----
773- .Output
774- [%collapsible]
775- ====
776757image:output_35_0.png[]
777- ====
0 commit comments