|
1
|
|
|
from abc import ABC, abstractmethod |
|
2
|
|
|
from collections.abc import Mapping |
|
3
|
|
|
from types import MappingProxyType |
|
4
|
|
|
from typing import Any |
|
5
|
|
|
|
|
6
|
|
|
from ..converters import edge_label_converter, node_label_converter |
|
7
|
|
|
from ..enums import GraphType |
|
8
|
|
|
from ..modeling import GraphModel |
|
9
|
|
|
from ..typing import GraphRepresentation |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class Builder(ABC): |
|
13
|
|
|
"""Abstract Base Class for Graph Builders. |
|
14
|
|
|
|
|
15
|
|
|
This class acts as a blueprint for all concrete builders that generate graph-like |
|
16
|
|
|
data structures from a given `GraphModel`. |
|
17
|
|
|
|
|
18
|
|
|
Attributes: |
|
19
|
|
|
default_node_attributes (Mapping): Default attributes for all nodes. |
|
20
|
|
|
default_edge_attributes (Mapping): Default attributes for all edges. |
|
21
|
|
|
""" |
|
22
|
|
|
|
|
23
|
|
|
default_node_attributes: Mapping = MappingProxyType({ |
|
24
|
|
|
'type': 'node', |
|
25
|
|
|
'label': node_label_converter, |
|
26
|
|
|
'value': [], |
|
27
|
|
|
'lineage': None |
|
28
|
|
|
}) |
|
29
|
|
|
|
|
30
|
|
|
default_edge_attributes: Mapping = MappingProxyType({ |
|
31
|
|
|
'type': 'edge', |
|
32
|
|
|
'label': edge_label_converter, |
|
33
|
|
|
'value': [], |
|
34
|
|
|
'weight': 1.0 |
|
35
|
|
|
}) |
|
36
|
|
|
|
|
37
|
|
|
def __init__(self, model: GraphModel, graph_type: GraphType = GraphType.Graph): |
|
38
|
|
|
"""Initialize a Builder instance with a specific graph model and type. |
|
39
|
|
|
|
|
40
|
|
|
Args: |
|
41
|
|
|
model (GraphModel): The model defining the graph's structure and data. |
|
42
|
|
|
graph_type (GraphType): Enum specifying the type of the graph. |
|
43
|
|
|
""" |
|
44
|
|
|
|
|
45
|
|
|
self._cached_build_kwargs: dict[str, Any] = {} |
|
46
|
|
|
self.model = model |
|
47
|
|
|
self.graph_type = graph_type |
|
48
|
|
|
|
|
49
|
|
|
@abstractmethod |
|
50
|
|
|
def build(self, **kwargs: Any) -> GraphRepresentation: |
|
51
|
|
|
"""Build the graph representation. |
|
52
|
|
|
|
|
53
|
|
|
Subclasses must override this method to implement specific build logic. |
|
54
|
|
|
|
|
55
|
|
|
Args: |
|
56
|
|
|
**kwargs: Any additional parameters for the build process. |
|
57
|
|
|
""" |
|
58
|
|
|
self._cached_build_kwargs = MappingProxyType(kwargs) |
|
59
|
|
|
|