prograph 1.3.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- prograph/__init__.py +36 -0
- prograph/__main__.py +5 -0
- prograph/_version.py +375 -0
- prograph/cli/__init__.py +1 -0
- prograph/cli/_iri_utils.py +124 -0
- prograph/cli/emit_genie_space.py +107 -0
- prograph/cli/generate_views.py +140 -0
- prograph/cli/main.py +420 -0
- prograph/cli/schema_inferrer.py +746 -0
- prograph/cli/ttl_loader.py +289 -0
- prograph/cli/upload_genie_space.py +84 -0
- prograph/config/__init__.py +27 -0
- prograph/config/yaml_config.py +848 -0
- prograph/demos/__init__.py +14 -0
- prograph/demos/turbofan/README.md +74 -0
- prograph/demos/turbofan/SCENARIO.md +209 -0
- prograph/demos/turbofan/__init__.py +75 -0
- prograph/demos/turbofan/create_tables.sql +206 -0
- prograph/demos/turbofan/data/engine-instances.ttl +1123 -0
- prograph/demos/turbofan/data/sensor-timeseries.csv +90 -0
- prograph/demos/turbofan/demo.py +162 -0
- prograph/demos/turbofan/genie_qa_notebook.py +434 -0
- prograph/demos/turbofan/graph.yaml +1098 -0
- prograph/demos/turbofan/instance_graph_demo.py +32 -0
- prograph/demos/turbofan/load.py +223 -0
- prograph/demos/turbofan/mapping.r2rml.ttl +991 -0
- prograph/demos/turbofan/ontology/engine-maintenance.ttl +606 -0
- prograph/demos/turbofan/payload-shapes.ttl +33 -0
- prograph/demos/turbofan/queries/example-queries.sparql +215 -0
- prograph/demos/turbofan/shapes.ttl +75 -0
- prograph/demos/turbofan/tables.py +106 -0
- prograph/demos/turbofan/turbofan_schema.py +393 -0
- prograph/demos/turbofan/validation_demo.py +153 -0
- prograph/describe.py +231 -0
- prograph/endpoint/__init__.py +12 -0
- prograph/endpoint/app.py +722 -0
- prograph/endpoint/cli.py +280 -0
- prograph/endpoint/credentials.py +240 -0
- prograph/endpoint/engines.py +418 -0
- prograph/endpoint/negotiate.py +101 -0
- prograph/endpoint/results.py +181 -0
- prograph/endpoint/service.py +31 -0
- prograph/engine.py +2718 -0
- prograph/execution.py +455 -0
- prograph/executors/__init__.py +6 -0
- prograph/executors/base.py +11 -0
- prograph/executors/databricks_sql_executor.py +51 -0
- prograph/executors/duckdb_executor.py +24 -0
- prograph/executors/mysql_executor.py +20 -0
- prograph/executors/oracle_executor.py +24 -0
- prograph/executors/pandas_executor.py +72 -0
- prograph/executors/postgresql_executor.py +35 -0
- prograph/executors/spark_executor.py +88 -0
- prograph/executors/sqlserver_executor.py +35 -0
- prograph/extras/__init__.py +1 -0
- prograph/extras/graphql/__init__.py +33 -0
- prograph/extras/graphql/execute.py +25 -0
- prograph/extras/graphql/schema.py +314 -0
- prograph/extras/instance_graph.py +559 -0
- prograph/genie/__init__.py +83 -0
- prograph/genie/databricks_uploader.py +306 -0
- prograph/genie/deploy.py +393 -0
- prograph/genie/formats.py +414 -0
- prograph/genie/instruction_generator.py +375 -0
- prograph/genie/sample_query_generator.py +256 -0
- prograph/genie/shacl_views.py +318 -0
- prograph/genie/space_emitter.py +329 -0
- prograph/genie/view_generator.py +628 -0
- prograph/genie/view_writer.py +172 -0
- prograph/graph/__init__.py +1 -0
- prograph/graph/ansi_session.py +348 -0
- prograph/graph/databricks_sql_graph.py +300 -0
- prograph/graph/drivers.py +143 -0
- prograph/graph/duckdb_graph.py +68 -0
- prograph/graph/mysql_graph.py +38 -0
- prograph/graph/oracle_graph.py +285 -0
- prograph/graph/pandas_graph.py +343 -0
- prograph/graph/postgresql_graph.py +644 -0
- prograph/graph/spark_graph.py +556 -0
- prograph/graph/sql_graph.py +83 -0
- prograph/graph/sqlserver_graph.py +72 -0
- prograph/hints.py +95 -0
- prograph/logging.py +550 -0
- prograph/mcp/__init__.py +29 -0
- prograph/mcp/__main__.py +180 -0
- prograph/mcp/bootstrap.py +98 -0
- prograph/mcp/server.py +601 -0
- prograph/mcp/sparql.py +397 -0
- prograph/mcp/tools.py +504 -0
- prograph/nlp/__init__.py +79 -0
- prograph/nlp/client.py +305 -0
- prograph/nlp/databricks_tools.py +369 -0
- prograph/nlp/graph_context.py +313 -0
- prograph/nlp/interface.py +652 -0
- prograph/nlp/prompts/__init__.py +19 -0
- prograph/nlp/prompts/examples.py +258 -0
- prograph/nlp/prompts/system.py +239 -0
- prograph/nlp/providers/__init__.py +14 -0
- prograph/nlp/providers/anthropic.py +274 -0
- prograph/nlp/providers/databricks.py +312 -0
- prograph/nlp/providers/openai.py +203 -0
- prograph/nlp/schema_context.py +78 -0
- prograph/ontology/__init__.py +53 -0
- prograph/ontology/alignment.py +542 -0
- prograph/ontology/hierarchy.py +310 -0
- prograph/ontology/imports.py +587 -0
- prograph/ontology/layer.py +991 -0
- prograph/ontology/skos.py +61 -0
- prograph/owl/__init__.py +27 -0
- prograph/owl/defined_classes.py +323 -0
- prograph/parameters.py +242 -0
- prograph/parser/__init__.py +1 -0
- prograph/parser/cypher/CypherLexer.py +836 -0
- prograph/parser/cypher/CypherListener.py +966 -0
- prograph/parser/cypher/CypherParser.py +10854 -0
- prograph/parser/cypher/CypherVisitor.py +543 -0
- prograph/parser/cypher/__init__.py +1 -0
- prograph/parser/sparql/SparqlLexer.py +801 -0
- prograph/parser/sparql/SparqlParser.py +10146 -0
- prograph/parser/sparql/SparqlParserListener.py +1056 -0
- prograph/parser/sparql/SparqlParserVisitor.py +593 -0
- prograph/parser/sparql/__init__.py +8 -0
- prograph/plan/__init__.py +3 -0
- prograph/plan/ir/__init__.py +1 -0
- prograph/plan/ir/casts.py +338 -0
- prograph/plan/ir/consistency.py +397 -0
- prograph/plan/ir/construct.py +90 -0
- prograph/plan/ir/cost.py +234 -0
- prograph/plan/ir/cypher_answer.py +1081 -0
- prograph/plan/ir/cypher_apoc.py +717 -0
- prograph/plan/ir/cypher_apoc_convert.py +505 -0
- prograph/plan/ir/cypher_apoc_date.py +845 -0
- prograph/plan/ir/cypher_apoc_map.py +496 -0
- prograph/plan/ir/cypher_apoc_meta.py +384 -0
- prograph/plan/ir/cypher_apoc_path.py +597 -0
- prograph/plan/ir/cypher_apoc_procedures.py +163 -0
- prograph/plan/ir/cypher_apoc_text.py +785 -0
- prograph/plan/ir/cypher_front.py +5474 -0
- prograph/plan/ir/cypher_functions.py +565 -0
- prograph/plan/ir/cypher_lists.py +935 -0
- prograph/plan/ir/cypher_maps.py +370 -0
- prograph/plan/ir/cypher_mixed.py +604 -0
- prograph/plan/ir/cypher_ontology.py +226 -0
- prograph/plan/ir/cypher_procedures.py +191 -0
- prograph/plan/ir/cypher_runtime_errors.py +107 -0
- prograph/plan/ir/cypher_temporal.py +1357 -0
- prograph/plan/ir/cypher_temporal_arith.py +339 -0
- prograph/plan/ir/cypher_values.py +546 -0
- prograph/plan/ir/cypher_write.py +612 -0
- prograph/plan/ir/dataset.py +643 -0
- prograph/plan/ir/dates.py +102 -0
- prograph/plan/ir/entry.py +992 -0
- prograph/plan/ir/expr.py +260 -0
- prograph/plan/ir/facts.py +1171 -0
- prograph/plan/ir/fresh.py +291 -0
- prograph/plan/ir/freshness.py +625 -0
- prograph/plan/ir/identity.py +797 -0
- prograph/plan/ir/java_datetime.py +265 -0
- prograph/plan/ir/java_regex.py +391 -0
- prograph/plan/ir/join_order.py +39 -0
- prograph/plan/ir/key_checks.py +433 -0
- prograph/plan/ir/keys.py +454 -0
- prograph/plan/ir/lower.py +5323 -0
- prograph/plan/ir/ops.py +619 -0
- prograph/plan/ir/owl_rl.py +1119 -0
- prograph/plan/ir/owl_rl_rules.py +725 -0
- prograph/plan/ir/profiles.py +694 -0
- prograph/plan/ir/ql.py +987 -0
- prograph/plan/ir/rdfs.py +342 -0
- prograph/plan/ir/rewrite.py +1782 -0
- prograph/plan/ir/saturate.py +339 -0
- prograph/plan/ir/semantics.py +103 -0
- prograph/plan/ir/slot.py +509 -0
- prograph/plan/ir/sparql_front.py +3418 -0
- prograph/plan/ir/strings.py +534 -0
- prograph/plan/ir/term_functions.py +274 -0
- prograph/plan/ir/terms.py +84 -0
- prograph/plan/ir/xpath_regex.py +264 -0
- prograph/plan/run_record.py +65 -0
- prograph/plan/sql/__init__.py +21 -0
- prograph/plan/sql/dialect.py +3726 -0
- prograph/plan/sql/mysql_serializer.py +103 -0
- prograph/plan/sql/names.py +65 -0
- prograph/plan/sql/oracle_serializer.py +306 -0
- prograph/plan/sql/pg_serializer.py +501 -0
- prograph/plan/sql/rel_algebra.py +781 -0
- prograph/plan/sql/sql_serializer.py +1419 -0
- prograph/plan/sql/sqlserver_serializer.py +603 -0
- prograph/provenance.py +222 -0
- prograph/r2rml/__init__.py +48 -0
- prograph/r2rml/catalog.py +259 -0
- prograph/r2rml/compile.py +372 -0
- prograph/r2rml/direct.py +140 -0
- prograph/r2rml/export.py +495 -0
- prograph/r2rml/model.py +180 -0
- prograph/r2rml/parser.py +546 -0
- prograph/r2rml/schema.py +120 -0
- prograph/read_only.py +122 -0
- prograph/results.py +157 -0
- prograph/rules/__init__.py +168 -0
- prograph/rules/__main__.py +32 -0
- prograph/schema/__init__.py +60 -0
- prograph/schema/change_set.py +50 -0
- prograph/schema/collations.py +198 -0
- prograph/schema/compile_schema.py +388 -0
- prograph/schema/context.py +845 -0
- prograph/schema/examples.py +234 -0
- prograph/schema/iri.py +817 -0
- prograph/schema/literals.py +94 -0
- prograph/schema/lpg_view.py +617 -0
- prograph/schema/mapping.py +1803 -0
- prograph/schema/maps_schema.py +40 -0
- prograph/schema/naming.py +77 -0
- prograph/schema/query_sources.py +59 -0
- prograph/schema/rows.py +215 -0
- prograph/schema/source_map.py +1103 -0
- prograph/schema/store_view.py +459 -0
- prograph/schema/types.py +569 -0
- prograph/schema/xsd.py +569 -0
- prograph/shacl/__init__.py +30 -0
- prograph/shacl/dqv.py +58 -0
- prograph/shacl/generate.py +255 -0
- prograph/shacl/report.py +138 -0
- prograph/shacl/validator.py +164 -0
- prograph/store.py +1737 -0
- prograph/utils/__init__.py +6 -0
- prograph/utils/exceptions.py +308 -0
- prograph/utils/helpers.py +126 -0
- prograph/warnings.py +46 -0
- prograph/writes.py +369 -0
- prograph-1.3.3.dist-info/METADATA +281 -0
- prograph-1.3.3.dist-info/RECORD +237 -0
- prograph-1.3.3.dist-info/WHEEL +5 -0
- prograph-1.3.3.dist-info/entry_points.txt +3 -0
- prograph-1.3.3.dist-info/licenses/LICENSE +201 -0
- prograph-1.3.3.dist-info/licenses/NOTICE +10 -0
- prograph-1.3.3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
"""Class hierarchy index for efficient subclass/superclass lookups.
|
|
2
|
+
|
|
3
|
+
This module provides an indexed class hierarchy with precomputed
|
|
4
|
+
transitive closures for O(1) subclass/superclass queries.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from dataclasses import dataclass, field
|
|
8
|
+
from typing import Dict, Set, Optional, Any
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass
|
|
12
|
+
class ClassNode:
|
|
13
|
+
"""A node in the class hierarchy graph.
|
|
14
|
+
|
|
15
|
+
Attributes:
|
|
16
|
+
name: Local name of the class (e.g., 'Person')
|
|
17
|
+
uri: Full URI of the class (e.g., 'http://example.org/Person')
|
|
18
|
+
parents: Direct parent class names (rdfs:subClassOf targets)
|
|
19
|
+
children: Direct child class names
|
|
20
|
+
"""
|
|
21
|
+
name: str
|
|
22
|
+
uri: str
|
|
23
|
+
parents: Set[str] = field(default_factory=set)
|
|
24
|
+
children: Set[str] = field(default_factory=set)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class ClassHierarchy:
|
|
28
|
+
"""Indexed class hierarchy for O(1) subclass lookups.
|
|
29
|
+
|
|
30
|
+
Builds transitive closures at construction time for fast queries.
|
|
31
|
+
Handles multiple inheritance and cycles gracefully.
|
|
32
|
+
|
|
33
|
+
Usage:
|
|
34
|
+
>>> hierarchy = ClassHierarchy()
|
|
35
|
+
>>> hierarchy.add_class('Entity', 'http://example.org/Entity')
|
|
36
|
+
>>> hierarchy.add_class('Person', 'http://example.org/Person')
|
|
37
|
+
>>> hierarchy.add_class('Employee', 'http://example.org/Employee')
|
|
38
|
+
>>> hierarchy.add_subclass_relation('Person', 'Entity')
|
|
39
|
+
>>> hierarchy.add_subclass_relation('Employee', 'Person')
|
|
40
|
+
>>> hierarchy.build_closures()
|
|
41
|
+
>>>
|
|
42
|
+
>>> hierarchy.get_all_subclasses('Entity')
|
|
43
|
+
{'Entity', 'Person', 'Employee'}
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
def __init__(self):
|
|
47
|
+
self._nodes: Dict[str, ClassNode] = {}
|
|
48
|
+
self._subclass_closure: Dict[str, Set[str]] = {}
|
|
49
|
+
self._superclass_closure: Dict[str, Set[str]] = {}
|
|
50
|
+
self._is_built: bool = False
|
|
51
|
+
|
|
52
|
+
def add_class(self, name: str, uri: str) -> None:
|
|
53
|
+
"""Register a class in the hierarchy.
|
|
54
|
+
|
|
55
|
+
Args:
|
|
56
|
+
name: Local name of the class
|
|
57
|
+
uri: Full URI of the class
|
|
58
|
+
"""
|
|
59
|
+
if name not in self._nodes:
|
|
60
|
+
self._nodes[name] = ClassNode(name=name, uri=uri)
|
|
61
|
+
self._is_built = False
|
|
62
|
+
|
|
63
|
+
def add_subclass_relation(self, child: str, parent: str) -> None:
|
|
64
|
+
"""Register that child rdfs:subClassOf parent.
|
|
65
|
+
|
|
66
|
+
Args:
|
|
67
|
+
child: Name of the subclass
|
|
68
|
+
parent: Name of the superclass
|
|
69
|
+
|
|
70
|
+
Note:
|
|
71
|
+
Both classes must be added via add_class() first,
|
|
72
|
+
or they will be auto-created with empty URIs.
|
|
73
|
+
"""
|
|
74
|
+
# Auto-create nodes if they don't exist
|
|
75
|
+
if child not in self._nodes:
|
|
76
|
+
self._nodes[child] = ClassNode(name=child, uri='')
|
|
77
|
+
if parent not in self._nodes:
|
|
78
|
+
self._nodes[parent] = ClassNode(name=parent, uri='')
|
|
79
|
+
|
|
80
|
+
self._nodes[child].parents.add(parent)
|
|
81
|
+
self._nodes[parent].children.add(child)
|
|
82
|
+
self._is_built = False
|
|
83
|
+
|
|
84
|
+
def build_closures(self) -> None:
|
|
85
|
+
"""Compute transitive closures for all classes.
|
|
86
|
+
|
|
87
|
+
Must be called after all classes and relations are added.
|
|
88
|
+
Uses iterative algorithm to handle cycles gracefully.
|
|
89
|
+
|
|
90
|
+
After calling this method:
|
|
91
|
+
- get_all_subclasses() returns O(1) results
|
|
92
|
+
- get_all_superclasses() returns O(1) results
|
|
93
|
+
"""
|
|
94
|
+
# Compute subclass closure for each class
|
|
95
|
+
for class_name in self._nodes:
|
|
96
|
+
self._subclass_closure[class_name] = self._compute_descendants(class_name)
|
|
97
|
+
self._superclass_closure[class_name] = self._compute_ancestors(class_name)
|
|
98
|
+
|
|
99
|
+
self._is_built = True
|
|
100
|
+
|
|
101
|
+
def _compute_descendants(self, class_name: str) -> Set[str]:
|
|
102
|
+
"""Compute all descendants (subclasses) of a class using BFS."""
|
|
103
|
+
descendants = {class_name} # Include self
|
|
104
|
+
queue = list(self._nodes[class_name].children)
|
|
105
|
+
|
|
106
|
+
while queue:
|
|
107
|
+
child = queue.pop(0)
|
|
108
|
+
if child not in descendants:
|
|
109
|
+
descendants.add(child)
|
|
110
|
+
queue.extend(self._nodes[child].children)
|
|
111
|
+
|
|
112
|
+
return descendants
|
|
113
|
+
|
|
114
|
+
def _compute_ancestors(self, class_name: str) -> Set[str]:
|
|
115
|
+
"""Compute all ancestors (superclasses) of a class using BFS."""
|
|
116
|
+
ancestors = {class_name} # Include self
|
|
117
|
+
queue = list(self._nodes[class_name].parents)
|
|
118
|
+
|
|
119
|
+
while queue:
|
|
120
|
+
parent = queue.pop(0)
|
|
121
|
+
if parent not in ancestors:
|
|
122
|
+
ancestors.add(parent)
|
|
123
|
+
queue.extend(self._nodes[parent].parents)
|
|
124
|
+
|
|
125
|
+
return ancestors
|
|
126
|
+
|
|
127
|
+
def get_all_subclasses(self, class_name: str, include_self: bool = True) -> Set[str]:
|
|
128
|
+
"""Get all subclasses (transitive closure).
|
|
129
|
+
|
|
130
|
+
Args:
|
|
131
|
+
class_name: The class to get subclasses for
|
|
132
|
+
include_self: Whether to include the class itself (default: True)
|
|
133
|
+
|
|
134
|
+
Returns:
|
|
135
|
+
Set of all subclass names
|
|
136
|
+
|
|
137
|
+
Raises:
|
|
138
|
+
RuntimeError: If build_closures() hasn't been called
|
|
139
|
+
KeyError: If class_name is not in the hierarchy
|
|
140
|
+
"""
|
|
141
|
+
if not self._is_built:
|
|
142
|
+
raise RuntimeError("Must call build_closures() first")
|
|
143
|
+
|
|
144
|
+
if class_name not in self._subclass_closure:
|
|
145
|
+
return {class_name} if include_self else set()
|
|
146
|
+
|
|
147
|
+
result = self._subclass_closure[class_name].copy()
|
|
148
|
+
if not include_self:
|
|
149
|
+
result.discard(class_name)
|
|
150
|
+
return result
|
|
151
|
+
|
|
152
|
+
def get_all_superclasses(self, class_name: str, include_self: bool = True) -> Set[str]:
|
|
153
|
+
"""Get all superclasses (transitive closure).
|
|
154
|
+
|
|
155
|
+
Args:
|
|
156
|
+
class_name: The class to get superclasses for
|
|
157
|
+
include_self: Whether to include the class itself (default: True)
|
|
158
|
+
|
|
159
|
+
Returns:
|
|
160
|
+
Set of all superclass names
|
|
161
|
+
|
|
162
|
+
Raises:
|
|
163
|
+
RuntimeError: If build_closures() hasn't been called
|
|
164
|
+
"""
|
|
165
|
+
if not self._is_built:
|
|
166
|
+
raise RuntimeError("Must call build_closures() first")
|
|
167
|
+
|
|
168
|
+
if class_name not in self._superclass_closure:
|
|
169
|
+
return {class_name} if include_self else set()
|
|
170
|
+
|
|
171
|
+
result = self._superclass_closure[class_name].copy()
|
|
172
|
+
if not include_self:
|
|
173
|
+
result.discard(class_name)
|
|
174
|
+
return result
|
|
175
|
+
|
|
176
|
+
def get_direct_subclasses(self, class_name: str) -> Set[str]:
|
|
177
|
+
"""Get immediate children only.
|
|
178
|
+
|
|
179
|
+
Args:
|
|
180
|
+
class_name: The class to get children for
|
|
181
|
+
|
|
182
|
+
Returns:
|
|
183
|
+
Set of direct subclass names
|
|
184
|
+
"""
|
|
185
|
+
if class_name not in self._nodes:
|
|
186
|
+
return set()
|
|
187
|
+
return self._nodes[class_name].children.copy()
|
|
188
|
+
|
|
189
|
+
def get_direct_superclasses(self, class_name: str) -> Set[str]:
|
|
190
|
+
"""Get immediate parents only.
|
|
191
|
+
|
|
192
|
+
Args:
|
|
193
|
+
class_name: The class to get parents for
|
|
194
|
+
|
|
195
|
+
Returns:
|
|
196
|
+
Set of direct superclass names
|
|
197
|
+
"""
|
|
198
|
+
if class_name not in self._nodes:
|
|
199
|
+
return set()
|
|
200
|
+
return self._nodes[class_name].parents.copy()
|
|
201
|
+
|
|
202
|
+
def is_subclass_of(self, child: str, parent: str) -> bool:
|
|
203
|
+
"""Check if child is a (transitive) subclass of parent.
|
|
204
|
+
|
|
205
|
+
Args:
|
|
206
|
+
child: Potential subclass
|
|
207
|
+
parent: Potential superclass
|
|
208
|
+
|
|
209
|
+
Returns:
|
|
210
|
+
True if child is a subclass of parent (or they're the same)
|
|
211
|
+
"""
|
|
212
|
+
if not self._is_built:
|
|
213
|
+
raise RuntimeError("Must call build_closures() first")
|
|
214
|
+
|
|
215
|
+
if parent not in self._subclass_closure:
|
|
216
|
+
return child == parent
|
|
217
|
+
|
|
218
|
+
return child in self._subclass_closure[parent]
|
|
219
|
+
|
|
220
|
+
def get_common_ancestors(self, *class_names: str) -> Set[str]:
|
|
221
|
+
"""Find common superclasses of multiple classes.
|
|
222
|
+
|
|
223
|
+
Args:
|
|
224
|
+
*class_names: Variable number of class names
|
|
225
|
+
|
|
226
|
+
Returns:
|
|
227
|
+
Set of class names that are superclasses of all inputs
|
|
228
|
+
"""
|
|
229
|
+
if not self._is_built:
|
|
230
|
+
raise RuntimeError("Must call build_closures() first")
|
|
231
|
+
|
|
232
|
+
if not class_names:
|
|
233
|
+
return set()
|
|
234
|
+
|
|
235
|
+
# Start with ancestors of first class
|
|
236
|
+
common = self.get_all_superclasses(class_names[0])
|
|
237
|
+
|
|
238
|
+
# Intersect with ancestors of remaining classes
|
|
239
|
+
for class_name in class_names[1:]:
|
|
240
|
+
common &= self.get_all_superclasses(class_name)
|
|
241
|
+
|
|
242
|
+
return common
|
|
243
|
+
|
|
244
|
+
@property
|
|
245
|
+
def classes(self) -> Set[str]:
|
|
246
|
+
"""Get all class names in the hierarchy."""
|
|
247
|
+
return set(self._nodes.keys())
|
|
248
|
+
|
|
249
|
+
def __contains__(self, class_name: str) -> bool:
|
|
250
|
+
"""Check if a class exists in the hierarchy."""
|
|
251
|
+
return class_name in self._nodes
|
|
252
|
+
|
|
253
|
+
def __len__(self) -> int:
|
|
254
|
+
"""Return the number of classes in the hierarchy."""
|
|
255
|
+
return len(self._nodes)
|
|
256
|
+
|
|
257
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
258
|
+
"""Serialize hierarchy for caching/debugging.
|
|
259
|
+
|
|
260
|
+
Returns:
|
|
261
|
+
Dictionary representation of the hierarchy
|
|
262
|
+
"""
|
|
263
|
+
return {
|
|
264
|
+
'classes': {
|
|
265
|
+
name: {
|
|
266
|
+
'uri': node.uri,
|
|
267
|
+
'parents': list(node.parents),
|
|
268
|
+
'children': list(node.children)
|
|
269
|
+
}
|
|
270
|
+
for name, node in self._nodes.items()
|
|
271
|
+
},
|
|
272
|
+
'subclass_closure': {
|
|
273
|
+
k: list(v) for k, v in self._subclass_closure.items()
|
|
274
|
+
},
|
|
275
|
+
'superclass_closure': {
|
|
276
|
+
k: list(v) for k, v in self._superclass_closure.items()
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
@classmethod
|
|
281
|
+
def from_dict(cls, data: Dict[str, Any]) -> 'ClassHierarchy':
|
|
282
|
+
"""Deserialize hierarchy from dictionary.
|
|
283
|
+
|
|
284
|
+
Args:
|
|
285
|
+
data: Dictionary from to_dict()
|
|
286
|
+
|
|
287
|
+
Returns:
|
|
288
|
+
Reconstructed ClassHierarchy instance
|
|
289
|
+
"""
|
|
290
|
+
hierarchy = cls()
|
|
291
|
+
|
|
292
|
+
# Reconstruct nodes
|
|
293
|
+
for name, node_data in data['classes'].items():
|
|
294
|
+
hierarchy._nodes[name] = ClassNode(
|
|
295
|
+
name=name,
|
|
296
|
+
uri=node_data['uri'],
|
|
297
|
+
parents=set(node_data['parents']),
|
|
298
|
+
children=set(node_data['children'])
|
|
299
|
+
)
|
|
300
|
+
|
|
301
|
+
# Reconstruct closures
|
|
302
|
+
hierarchy._subclass_closure = {
|
|
303
|
+
k: set(v) for k, v in data['subclass_closure'].items()
|
|
304
|
+
}
|
|
305
|
+
hierarchy._superclass_closure = {
|
|
306
|
+
k: set(v) for k, v in data['superclass_closure'].items()
|
|
307
|
+
}
|
|
308
|
+
hierarchy._is_built = True
|
|
309
|
+
|
|
310
|
+
return hierarchy
|