Skip to content

Commit 500da06

Browse files
committed
Add graph builder.
1 parent 4831413 commit 500da06

File tree

1 file changed

+80
-0
lines changed
  • metricflow-semantics/metricflow_semantics/experimental/semantic_graph/builder

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from __future__ import annotations
2+
3+
import logging
4+
import time
5+
from typing import Type
6+
7+
from typing_extensions import override
8+
9+
from metricflow_semantics.collection_helpers.mf_type_aliases import AnyLengthTuple
10+
from metricflow_semantics.experimental.dsi.manifest_object_lookup import ManifestObjectLookup
11+
from metricflow_semantics.experimental.semantic_graph.builder.categorical_dimension_subgraph import (
12+
CategoricalDimensionSubgraphGenerator,
13+
)
14+
from metricflow_semantics.experimental.semantic_graph.builder.entity_join_subgraph import EntityJoinSubgraphGenerator
15+
from metricflow_semantics.experimental.semantic_graph.builder.entity_key_subgraph import (
16+
EntityKeySubgraphGenerator,
17+
)
18+
from metricflow_semantics.experimental.semantic_graph.builder.measure_subgraph import (
19+
MeasureSubgraphGenerator,
20+
)
21+
from metricflow_semantics.experimental.semantic_graph.builder.metric_subgraph import MetricSubgraphGenerator
22+
from metricflow_semantics.experimental.semantic_graph.builder.subgraph_generator import SemanticSubgraphGenerator
23+
from metricflow_semantics.experimental.semantic_graph.builder.time_dimension_subgraph import (
24+
TimeDimensionSubgraphGenerator,
25+
)
26+
from metricflow_semantics.experimental.semantic_graph.builder.time_entity_subgraph import TimeEntitySubgraphGenerator
27+
from metricflow_semantics.experimental.semantic_graph.sg_interfaces import MutableSemanticGraph, SemanticGraph
28+
from metricflow_semantics.mf_logging.lazy_formattable import LazyFormat
29+
30+
logger = logging.getLogger(__name__)
31+
32+
33+
class SemanticGraphBuilder:
34+
"""Convenience class that builds a semantic graph using all subgraph generators.
35+
36+
This will replace `PartialSemanticGraphBuilder` once the PRs that depend on it are merged.
37+
"""
38+
39+
_ALL_SUBGRAPH_GENERATORS: AnyLengthTuple[Type[SemanticSubgraphGenerator]] = (
40+
CategoricalDimensionSubgraphGenerator,
41+
EntityKeySubgraphGenerator,
42+
EntityJoinSubgraphGenerator,
43+
MeasureSubgraphGenerator,
44+
TimeDimensionSubgraphGenerator,
45+
TimeEntitySubgraphGenerator,
46+
MetricSubgraphGenerator,
47+
)
48+
49+
@override
50+
def __init__(self, manifest_object_lookup: ManifestObjectLookup) -> None:
51+
self._manifest_object_lookup = manifest_object_lookup
52+
self._verbose_debug_logs = True
53+
54+
def build(self) -> SemanticGraph: # noqa: D102
55+
current_graph = MutableSemanticGraph.create()
56+
for generator in self._ALL_SUBGRAPH_GENERATORS:
57+
start_time = time.perf_counter()
58+
generator_instance = generator(self._manifest_object_lookup)
59+
60+
start_node_count = len(current_graph.nodes)
61+
start_edge_count = len(current_graph.edges)
62+
generated_edges = generator_instance.generate_edges()
63+
generated_edge_count = len(generated_edges)
64+
current_graph.add_edges(generated_edges)
65+
added_node_count = len(current_graph.nodes) - start_node_count
66+
added_edge_count = len(current_graph.edges) - start_edge_count
67+
runtime = time.perf_counter() - start_time
68+
if self._verbose_debug_logs:
69+
logger.debug(
70+
LazyFormat(
71+
"Generated subgraph",
72+
generator=generator.__name__,
73+
runtime=f"{runtime:.2f}s",
74+
added_node_count=added_node_count,
75+
added_edge_count=added_edge_count,
76+
generated_edge_count=generated_edge_count,
77+
)
78+
)
79+
80+
return current_graph

0 commit comments

Comments
 (0)