|
1
|
|
|
"""Builder Classes: Abstraction Layer to Generate Graph Data Structures |
|
2
|
|
|
|
|
3
|
|
|
This module defines builder base classes and implementations that construct graph |
|
4
|
|
|
data structures from a `GraphModel`. It supports generating various graph formats, |
|
5
|
|
|
including NetworkX, D3, Mermaid, and GraphQL schema representations. |
|
6
|
|
|
|
|
7
|
|
|
Attributes: |
|
8
|
|
|
- **GraphRepresentation:** Types of representations the builder can produce. |
|
9
|
|
|
- **GraphType:** Enumeration for different graph types (directed, undirected, etc.). |
|
10
|
|
|
|
|
11
|
|
|
Main Classes: |
|
12
|
|
|
- `GraphType` : Enum defining networkx-compatible graph types. |
|
13
|
|
|
- `Builder` : Abstract base class for custom graph builders. |
|
14
|
|
|
- `NetworkxBuilder` : A builder class for constructing Graph representations using NetworkX. |
|
15
|
|
|
- `D3Builder` : Builder class transforming graphs into D3-compatible structures. |
|
16
|
|
|
- `MermaidBuilder`: Supports MermaidJS diagram generation. |
|
17
|
|
|
- `GraphQLBuilder`: Constructs GraphQL schema representations of graphs. |
|
18
|
|
|
""" |
|
19
|
|
|
|
|
20
|
|
|
__all__ = ['Builder', 'D3Builder', 'GraphQLBuilder', 'MermaidBuilder', 'NetworkxBuilder', 'build'] |
|
21
|
|
|
|
|
22
|
|
|
from collections.abc import Mapping |
|
23
|
|
|
from typing import Any |
|
24
|
|
|
|
|
25
|
|
|
from ..enums import GraphType |
|
26
|
|
|
from ..modeling import GraphModel |
|
27
|
|
|
from ._builder import Builder |
|
28
|
|
|
from ._d3 import D3Builder |
|
29
|
|
|
from ._graphql import GraphQLBuilder |
|
30
|
|
|
from ._mermaid import MermaidBuilder |
|
31
|
|
|
from ._networkx import NetworkxBuilder |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
def build(builder_cls: type[Builder], |
|
35
|
|
|
graph_model: GraphModel, |
|
36
|
|
|
graph_type: GraphType = GraphType.Graph, |
|
37
|
|
|
default_node_attributes: Mapping | None = None, |
|
38
|
|
|
**kwargs: Any) -> Any: |
|
39
|
|
|
""" |
|
40
|
|
|
Build a graph from a graph model |
|
41
|
|
|
|
|
42
|
|
|
Args: |
|
43
|
|
|
builder_cls: builder class type |
|
44
|
|
|
graph_model: a GraphModel instance |
|
45
|
|
|
graph_type: type of the generated graph |
|
46
|
|
|
default_node_attributes: default node attributes |
|
47
|
|
|
**kwargs: node id values |
|
48
|
|
|
|
|
49
|
|
|
Returns: |
|
50
|
|
|
Graph data structure |
|
51
|
|
|
""" |
|
52
|
|
|
|
|
53
|
|
|
builder = builder_cls(graph_model, graph_type) |
|
54
|
|
|
materialized_graph = builder.build(default_node_attributes=default_node_attributes, **kwargs) |
|
55
|
|
|
return materialized_graph |
|
56
|
|
|
|