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.
Files changed (237) hide show
  1. prograph/__init__.py +36 -0
  2. prograph/__main__.py +5 -0
  3. prograph/_version.py +375 -0
  4. prograph/cli/__init__.py +1 -0
  5. prograph/cli/_iri_utils.py +124 -0
  6. prograph/cli/emit_genie_space.py +107 -0
  7. prograph/cli/generate_views.py +140 -0
  8. prograph/cli/main.py +420 -0
  9. prograph/cli/schema_inferrer.py +746 -0
  10. prograph/cli/ttl_loader.py +289 -0
  11. prograph/cli/upload_genie_space.py +84 -0
  12. prograph/config/__init__.py +27 -0
  13. prograph/config/yaml_config.py +848 -0
  14. prograph/demos/__init__.py +14 -0
  15. prograph/demos/turbofan/README.md +74 -0
  16. prograph/demos/turbofan/SCENARIO.md +209 -0
  17. prograph/demos/turbofan/__init__.py +75 -0
  18. prograph/demos/turbofan/create_tables.sql +206 -0
  19. prograph/demos/turbofan/data/engine-instances.ttl +1123 -0
  20. prograph/demos/turbofan/data/sensor-timeseries.csv +90 -0
  21. prograph/demos/turbofan/demo.py +162 -0
  22. prograph/demos/turbofan/genie_qa_notebook.py +434 -0
  23. prograph/demos/turbofan/graph.yaml +1098 -0
  24. prograph/demos/turbofan/instance_graph_demo.py +32 -0
  25. prograph/demos/turbofan/load.py +223 -0
  26. prograph/demos/turbofan/mapping.r2rml.ttl +991 -0
  27. prograph/demos/turbofan/ontology/engine-maintenance.ttl +606 -0
  28. prograph/demos/turbofan/payload-shapes.ttl +33 -0
  29. prograph/demos/turbofan/queries/example-queries.sparql +215 -0
  30. prograph/demos/turbofan/shapes.ttl +75 -0
  31. prograph/demos/turbofan/tables.py +106 -0
  32. prograph/demos/turbofan/turbofan_schema.py +393 -0
  33. prograph/demos/turbofan/validation_demo.py +153 -0
  34. prograph/describe.py +231 -0
  35. prograph/endpoint/__init__.py +12 -0
  36. prograph/endpoint/app.py +722 -0
  37. prograph/endpoint/cli.py +280 -0
  38. prograph/endpoint/credentials.py +240 -0
  39. prograph/endpoint/engines.py +418 -0
  40. prograph/endpoint/negotiate.py +101 -0
  41. prograph/endpoint/results.py +181 -0
  42. prograph/endpoint/service.py +31 -0
  43. prograph/engine.py +2718 -0
  44. prograph/execution.py +455 -0
  45. prograph/executors/__init__.py +6 -0
  46. prograph/executors/base.py +11 -0
  47. prograph/executors/databricks_sql_executor.py +51 -0
  48. prograph/executors/duckdb_executor.py +24 -0
  49. prograph/executors/mysql_executor.py +20 -0
  50. prograph/executors/oracle_executor.py +24 -0
  51. prograph/executors/pandas_executor.py +72 -0
  52. prograph/executors/postgresql_executor.py +35 -0
  53. prograph/executors/spark_executor.py +88 -0
  54. prograph/executors/sqlserver_executor.py +35 -0
  55. prograph/extras/__init__.py +1 -0
  56. prograph/extras/graphql/__init__.py +33 -0
  57. prograph/extras/graphql/execute.py +25 -0
  58. prograph/extras/graphql/schema.py +314 -0
  59. prograph/extras/instance_graph.py +559 -0
  60. prograph/genie/__init__.py +83 -0
  61. prograph/genie/databricks_uploader.py +306 -0
  62. prograph/genie/deploy.py +393 -0
  63. prograph/genie/formats.py +414 -0
  64. prograph/genie/instruction_generator.py +375 -0
  65. prograph/genie/sample_query_generator.py +256 -0
  66. prograph/genie/shacl_views.py +318 -0
  67. prograph/genie/space_emitter.py +329 -0
  68. prograph/genie/view_generator.py +628 -0
  69. prograph/genie/view_writer.py +172 -0
  70. prograph/graph/__init__.py +1 -0
  71. prograph/graph/ansi_session.py +348 -0
  72. prograph/graph/databricks_sql_graph.py +300 -0
  73. prograph/graph/drivers.py +143 -0
  74. prograph/graph/duckdb_graph.py +68 -0
  75. prograph/graph/mysql_graph.py +38 -0
  76. prograph/graph/oracle_graph.py +285 -0
  77. prograph/graph/pandas_graph.py +343 -0
  78. prograph/graph/postgresql_graph.py +644 -0
  79. prograph/graph/spark_graph.py +556 -0
  80. prograph/graph/sql_graph.py +83 -0
  81. prograph/graph/sqlserver_graph.py +72 -0
  82. prograph/hints.py +95 -0
  83. prograph/logging.py +550 -0
  84. prograph/mcp/__init__.py +29 -0
  85. prograph/mcp/__main__.py +180 -0
  86. prograph/mcp/bootstrap.py +98 -0
  87. prograph/mcp/server.py +601 -0
  88. prograph/mcp/sparql.py +397 -0
  89. prograph/mcp/tools.py +504 -0
  90. prograph/nlp/__init__.py +79 -0
  91. prograph/nlp/client.py +305 -0
  92. prograph/nlp/databricks_tools.py +369 -0
  93. prograph/nlp/graph_context.py +313 -0
  94. prograph/nlp/interface.py +652 -0
  95. prograph/nlp/prompts/__init__.py +19 -0
  96. prograph/nlp/prompts/examples.py +258 -0
  97. prograph/nlp/prompts/system.py +239 -0
  98. prograph/nlp/providers/__init__.py +14 -0
  99. prograph/nlp/providers/anthropic.py +274 -0
  100. prograph/nlp/providers/databricks.py +312 -0
  101. prograph/nlp/providers/openai.py +203 -0
  102. prograph/nlp/schema_context.py +78 -0
  103. prograph/ontology/__init__.py +53 -0
  104. prograph/ontology/alignment.py +542 -0
  105. prograph/ontology/hierarchy.py +310 -0
  106. prograph/ontology/imports.py +587 -0
  107. prograph/ontology/layer.py +991 -0
  108. prograph/ontology/skos.py +61 -0
  109. prograph/owl/__init__.py +27 -0
  110. prograph/owl/defined_classes.py +323 -0
  111. prograph/parameters.py +242 -0
  112. prograph/parser/__init__.py +1 -0
  113. prograph/parser/cypher/CypherLexer.py +836 -0
  114. prograph/parser/cypher/CypherListener.py +966 -0
  115. prograph/parser/cypher/CypherParser.py +10854 -0
  116. prograph/parser/cypher/CypherVisitor.py +543 -0
  117. prograph/parser/cypher/__init__.py +1 -0
  118. prograph/parser/sparql/SparqlLexer.py +801 -0
  119. prograph/parser/sparql/SparqlParser.py +10146 -0
  120. prograph/parser/sparql/SparqlParserListener.py +1056 -0
  121. prograph/parser/sparql/SparqlParserVisitor.py +593 -0
  122. prograph/parser/sparql/__init__.py +8 -0
  123. prograph/plan/__init__.py +3 -0
  124. prograph/plan/ir/__init__.py +1 -0
  125. prograph/plan/ir/casts.py +338 -0
  126. prograph/plan/ir/consistency.py +397 -0
  127. prograph/plan/ir/construct.py +90 -0
  128. prograph/plan/ir/cost.py +234 -0
  129. prograph/plan/ir/cypher_answer.py +1081 -0
  130. prograph/plan/ir/cypher_apoc.py +717 -0
  131. prograph/plan/ir/cypher_apoc_convert.py +505 -0
  132. prograph/plan/ir/cypher_apoc_date.py +845 -0
  133. prograph/plan/ir/cypher_apoc_map.py +496 -0
  134. prograph/plan/ir/cypher_apoc_meta.py +384 -0
  135. prograph/plan/ir/cypher_apoc_path.py +597 -0
  136. prograph/plan/ir/cypher_apoc_procedures.py +163 -0
  137. prograph/plan/ir/cypher_apoc_text.py +785 -0
  138. prograph/plan/ir/cypher_front.py +5474 -0
  139. prograph/plan/ir/cypher_functions.py +565 -0
  140. prograph/plan/ir/cypher_lists.py +935 -0
  141. prograph/plan/ir/cypher_maps.py +370 -0
  142. prograph/plan/ir/cypher_mixed.py +604 -0
  143. prograph/plan/ir/cypher_ontology.py +226 -0
  144. prograph/plan/ir/cypher_procedures.py +191 -0
  145. prograph/plan/ir/cypher_runtime_errors.py +107 -0
  146. prograph/plan/ir/cypher_temporal.py +1357 -0
  147. prograph/plan/ir/cypher_temporal_arith.py +339 -0
  148. prograph/plan/ir/cypher_values.py +546 -0
  149. prograph/plan/ir/cypher_write.py +612 -0
  150. prograph/plan/ir/dataset.py +643 -0
  151. prograph/plan/ir/dates.py +102 -0
  152. prograph/plan/ir/entry.py +992 -0
  153. prograph/plan/ir/expr.py +260 -0
  154. prograph/plan/ir/facts.py +1171 -0
  155. prograph/plan/ir/fresh.py +291 -0
  156. prograph/plan/ir/freshness.py +625 -0
  157. prograph/plan/ir/identity.py +797 -0
  158. prograph/plan/ir/java_datetime.py +265 -0
  159. prograph/plan/ir/java_regex.py +391 -0
  160. prograph/plan/ir/join_order.py +39 -0
  161. prograph/plan/ir/key_checks.py +433 -0
  162. prograph/plan/ir/keys.py +454 -0
  163. prograph/plan/ir/lower.py +5323 -0
  164. prograph/plan/ir/ops.py +619 -0
  165. prograph/plan/ir/owl_rl.py +1119 -0
  166. prograph/plan/ir/owl_rl_rules.py +725 -0
  167. prograph/plan/ir/profiles.py +694 -0
  168. prograph/plan/ir/ql.py +987 -0
  169. prograph/plan/ir/rdfs.py +342 -0
  170. prograph/plan/ir/rewrite.py +1782 -0
  171. prograph/plan/ir/saturate.py +339 -0
  172. prograph/plan/ir/semantics.py +103 -0
  173. prograph/plan/ir/slot.py +509 -0
  174. prograph/plan/ir/sparql_front.py +3418 -0
  175. prograph/plan/ir/strings.py +534 -0
  176. prograph/plan/ir/term_functions.py +274 -0
  177. prograph/plan/ir/terms.py +84 -0
  178. prograph/plan/ir/xpath_regex.py +264 -0
  179. prograph/plan/run_record.py +65 -0
  180. prograph/plan/sql/__init__.py +21 -0
  181. prograph/plan/sql/dialect.py +3726 -0
  182. prograph/plan/sql/mysql_serializer.py +103 -0
  183. prograph/plan/sql/names.py +65 -0
  184. prograph/plan/sql/oracle_serializer.py +306 -0
  185. prograph/plan/sql/pg_serializer.py +501 -0
  186. prograph/plan/sql/rel_algebra.py +781 -0
  187. prograph/plan/sql/sql_serializer.py +1419 -0
  188. prograph/plan/sql/sqlserver_serializer.py +603 -0
  189. prograph/provenance.py +222 -0
  190. prograph/r2rml/__init__.py +48 -0
  191. prograph/r2rml/catalog.py +259 -0
  192. prograph/r2rml/compile.py +372 -0
  193. prograph/r2rml/direct.py +140 -0
  194. prograph/r2rml/export.py +495 -0
  195. prograph/r2rml/model.py +180 -0
  196. prograph/r2rml/parser.py +546 -0
  197. prograph/r2rml/schema.py +120 -0
  198. prograph/read_only.py +122 -0
  199. prograph/results.py +157 -0
  200. prograph/rules/__init__.py +168 -0
  201. prograph/rules/__main__.py +32 -0
  202. prograph/schema/__init__.py +60 -0
  203. prograph/schema/change_set.py +50 -0
  204. prograph/schema/collations.py +198 -0
  205. prograph/schema/compile_schema.py +388 -0
  206. prograph/schema/context.py +845 -0
  207. prograph/schema/examples.py +234 -0
  208. prograph/schema/iri.py +817 -0
  209. prograph/schema/literals.py +94 -0
  210. prograph/schema/lpg_view.py +617 -0
  211. prograph/schema/mapping.py +1803 -0
  212. prograph/schema/maps_schema.py +40 -0
  213. prograph/schema/naming.py +77 -0
  214. prograph/schema/query_sources.py +59 -0
  215. prograph/schema/rows.py +215 -0
  216. prograph/schema/source_map.py +1103 -0
  217. prograph/schema/store_view.py +459 -0
  218. prograph/schema/types.py +569 -0
  219. prograph/schema/xsd.py +569 -0
  220. prograph/shacl/__init__.py +30 -0
  221. prograph/shacl/dqv.py +58 -0
  222. prograph/shacl/generate.py +255 -0
  223. prograph/shacl/report.py +138 -0
  224. prograph/shacl/validator.py +164 -0
  225. prograph/store.py +1737 -0
  226. prograph/utils/__init__.py +6 -0
  227. prograph/utils/exceptions.py +308 -0
  228. prograph/utils/helpers.py +126 -0
  229. prograph/warnings.py +46 -0
  230. prograph/writes.py +369 -0
  231. prograph-1.3.3.dist-info/METADATA +281 -0
  232. prograph-1.3.3.dist-info/RECORD +237 -0
  233. prograph-1.3.3.dist-info/WHEEL +5 -0
  234. prograph-1.3.3.dist-info/entry_points.txt +3 -0
  235. prograph-1.3.3.dist-info/licenses/LICENSE +201 -0
  236. prograph-1.3.3.dist-info/licenses/NOTICE +10 -0
  237. prograph-1.3.3.dist-info/top_level.txt +1 -0
prograph/describe.py ADDED
@@ -0,0 +1,231 @@
1
+ """A mapped graph described as data: DCAT 3, VoID and the SPARQL 1.1 Service Description.
2
+
3
+ What the source maps say without a read of the data (`describe`): the dataset, its classes
4
+ (`void:classPartition`), its properties (`void:propertyPartition`), the vocabularies of both
5
+ (`void:vocabulary`), the URI space of its subjects (`void:uriSpace`), its named graphs
6
+ (`sd:namedGraph`), and, given an endpoint, the service (`sd:Service` and `dcat:DataService`) with its
7
+ language, result formats and default graph. A class or property made from a row (a predicate map of a
8
+ column or template) and a graph made from a column have no IRI to list and are left out: the
9
+ description is of what the maps fix.
10
+
11
+ Counts (`void:triples`, `void:entities`, ...) are numbers of the data. They come from `count`, a
12
+ function that runs one SPARQL COUNT query and gives its number: the engine's own (`ProGraph.describe_dataset`),
13
+ so no second path counts. Without `count` the description holds none.
14
+ """
15
+
16
+ from __future__ import annotations
17
+
18
+ from typing import Any, Callable, Iterable, List, Optional, Sequence, Set
19
+
20
+ from prograph.rules import module_home
21
+ from prograph.schema.source_map import IRI, RR_DEFAULT_GRAPH
22
+
23
+ module_home("dataset-description", "A mapped graph described as DCAT, VoID and a SPARQL Service Description",
24
+ terms=("service description", "void", "dcat", "sd:Service", "sparql endpoint", "classPartition",
25
+ "propertyPartition", "uriSpace", "named graphs", "describe"))
26
+
27
+ SD = "http://www.w3.org/ns/sparql-service-description#"
28
+ VOID = "http://rdfs.org/ns/void#"
29
+ DCAT = "http://www.w3.org/ns/dcat#"
30
+ RDF_TYPE = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
31
+ FORMATS = "http://www.w3.org/ns/formats/"
32
+ # What the SPARQL endpoint writes (`prograph.endpoint.results`), as the W3C's format IRIs.
33
+ RESULT_FORMAT_IRIS = tuple(FORMATS + name for name in (
34
+ "SPARQL_Results_JSON", "SPARQL_Results_XML", "SPARQL_Results_CSV", "SPARQL_Results_TSV",
35
+ "Turtle", "N-Triples", "RDF_XML", "JSON-LD"))
36
+ _BAD_IRI = set('<>"{}|\\^` \t\r\n')
37
+
38
+
39
+ def describe(maps: Any = None, *, default_graph: str = "unnamed", endpoint: Optional[str] = None,
40
+ dataset_iri: Optional[str] = None, formats: Iterable[str] = (),
41
+ count: Optional[Callable[[str], int]] = None, validation: Any = None,
42
+ include_report: bool = False) -> Any:
43
+ """The description, an rdflib Graph.
44
+
45
+ Args:
46
+ maps: The `SourceMaps` described; None gives the service alone (no dataset facts).
47
+ default_graph: The engine's `default_graph`: `union` states `sd:UnionDefaultGraph`.
48
+ endpoint: The SPARQL endpoint's IRI; none, no service is described.
49
+ dataset_iri: The dataset's IRI; by default `<endpoint>#dataset`, or a blank node.
50
+ formats: Result format IRIs (`http://www.w3.org/ns/formats/...`) the service writes.
51
+ count: Runs a SPARQL query of one COUNT column and gives the number; None adds no counts.
52
+ validation: A SHACL `ValidationReport` of this dataset: its DQV measurements are linked to the dataset
53
+ (`prograph.shacl.dqv`). Needs `maps`.
54
+ include_report: With `validation`, also add the `sh:ValidationReport` graph, each measurement's `dcterms:source`.
55
+ """
56
+ from rdflib import BNode, Graph, Literal, Namespace, URIRef
57
+ from rdflib.namespace import DCTERMS, RDF
58
+ sd, void, dcat = Namespace(SD), Namespace(VOID), Namespace(DCAT)
59
+ g = Graph()
60
+ for prefix, ns in (("sd", sd), ("void", void), ("dcat", dcat), ("dcterms", DCTERMS)):
61
+ g.bind(prefix, ns)
62
+
63
+ service = BNode() if endpoint else None
64
+ if service is not None:
65
+ g.add((service, RDF.type, sd.Service))
66
+ g.add((service, RDF.type, dcat.DataService))
67
+ g.add((service, sd.endpoint, URIRef(endpoint)))
68
+ g.add((service, dcat.endpointURL, URIRef(endpoint)))
69
+ g.add((service, sd.supportedLanguage, sd.SPARQL11Query))
70
+ for iri in formats:
71
+ g.add((service, sd.resultFormat, URIRef(iri)))
72
+ if default_graph == "union":
73
+ g.add((service, sd.feature, sd.UnionDefaultGraph))
74
+ if maps is None:
75
+ if validation is not None:
76
+ raise ValueError("a validation report is linked to a dataset: describe needs the source maps")
77
+ sd_dataset, graph = BNode(), BNode()
78
+ if service is not None:
79
+ g.add((service, sd.defaultDataset, sd_dataset))
80
+ g.add((sd_dataset, RDF.type, sd.Dataset))
81
+ g.add((sd_dataset, sd.defaultGraph, graph))
82
+ g.add((graph, RDF.type, sd.Graph))
83
+ return g
84
+
85
+ facts = _facts(maps)
86
+ dataset = URIRef(dataset_iri) if dataset_iri else (URIRef(endpoint + "#dataset") if endpoint else BNode())
87
+ g.add((dataset, RDF.type, void.Dataset))
88
+ g.add((dataset, RDF.type, dcat.Dataset))
89
+ if endpoint:
90
+ g.add((dataset, void.sparqlEndpoint, URIRef(endpoint)))
91
+ distribution = BNode()
92
+ g.add((dataset, dcat.distribution, distribution))
93
+ g.add((distribution, RDF.type, dcat.Distribution))
94
+ g.add((distribution, dcat.accessService, service))
95
+ g.add((service, dcat.servesDataset, dataset))
96
+ if facts.uri_space:
97
+ g.add((dataset, void.uriSpace, Literal(facts.uri_space)))
98
+ for ns in facts.vocabularies:
99
+ g.add((dataset, void.vocabulary, URIRef(ns)))
100
+ for c in facts.classes:
101
+ part = BNode()
102
+ g.add((dataset, void.classPartition, part))
103
+ g.add((part, void["class"], URIRef(c)))
104
+ if count and _safe(c):
105
+ g.add((part, void.entities, _number(count(f"SELECT (COUNT(DISTINCT ?s) AS ?n) WHERE "
106
+ f"{_all('?s a <' + c + '> FILTER(isIRI(?s))', default_graph)}")))) # VoID 4.6: entities have URIs
107
+ for p in facts.properties:
108
+ part = BNode()
109
+ g.add((dataset, void.propertyPartition, part))
110
+ g.add((part, void.property, URIRef(p)))
111
+ if count and _safe(p):
112
+ where = _all(f"?s <{p}> ?o", default_graph)
113
+ for term, agg in ((void.triples, "COUNT(*)"), (void.distinctSubjects, "COUNT(DISTINCT ?s)"),
114
+ (void.distinctObjects, "COUNT(DISTINCT ?o)")):
115
+ g.add((part, term, _number(count(f"SELECT ({agg} AS ?n) WHERE {where}"))))
116
+ if count:
117
+ where = _all("?s ?p ?o", default_graph)
118
+ for term, agg, extra in (
119
+ (void.triples, "COUNT(*)", ""), (void.distinctSubjects, "COUNT(DISTINCT ?s)", ""),
120
+ (void.distinctObjects, "COUNT(DISTINCT ?o)", ""), (void.properties, "COUNT(DISTINCT ?p)", ""),
121
+ (void.entities, "COUNT(DISTINCT ?s)", " FILTER(isIRI(?s))")):
122
+ q = where if not extra else where[:-1] + extra + " }"
123
+ g.add((dataset, term, _number(count(f"SELECT ({agg} AS ?n) WHERE {q}"))))
124
+ g.add((dataset, void.classes, _number(count(
125
+ f"SELECT (COUNT(DISTINCT ?c) AS ?n) WHERE {_all('?s a ?c', default_graph)}"))))
126
+
127
+ if validation is not None:
128
+ from prograph.shacl.dqv import add_measurements
129
+ add_measurements(g, dataset, validation, include_report=include_report)
130
+
131
+ sd_dataset, graph = BNode(), BNode()
132
+ g.add((sd_dataset, RDF.type, sd.Dataset))
133
+ g.add((sd_dataset, sd.defaultGraph, graph))
134
+ g.add((graph, RDF.type, sd.Graph))
135
+ for name in facts.graphs:
136
+ named, ng = BNode(), BNode()
137
+ g.add((sd_dataset, sd.namedGraph, named))
138
+ g.add((named, RDF.type, sd.NamedGraph))
139
+ g.add((named, sd.name, URIRef(name)))
140
+ g.add((named, sd.graph, ng))
141
+ g.add((ng, RDF.type, sd.Graph))
142
+ if service is not None:
143
+ g.add((service, sd.defaultDataset, sd_dataset))
144
+ else:
145
+ g.add((dataset, sd.defaultDataset, sd_dataset))
146
+ return g
147
+
148
+
149
+ def _all(pattern: str, default_graph: str) -> str:
150
+ """The pattern over the whole dataset, a group: the default graph, and under `unnamed` the named
151
+ graphs too (under `union` the default graph already holds them)."""
152
+ if default_graph == "union":
153
+ return "{ " + pattern + " }"
154
+ return "{ { " + pattern + " } UNION { GRAPH ?g { " + pattern + " } } }"
155
+
156
+
157
+ def _safe(iri: str) -> bool:
158
+ return not (_BAD_IRI & set(iri))
159
+
160
+
161
+ def _number(n: Any) -> Any:
162
+ from rdflib import Literal
163
+ from rdflib.namespace import XSD
164
+ return Literal(int(n), datatype=XSD.integer)
165
+
166
+
167
+ class _Facts:
168
+ def __init__(self) -> None:
169
+ self.classes: List[str] = []
170
+ self.properties: List[str] = []
171
+ self.vocabularies: List[str] = []
172
+ self.graphs: List[str] = []
173
+ self.uri_space: str = ""
174
+
175
+
176
+ def _facts(maps: Any) -> _Facts:
177
+ """What the maps fix: sorted, so two descriptions of one mapping are alike."""
178
+ classes: Set[str] = set()
179
+ properties: Set[str] = set()
180
+ graphs: Set[str] = set()
181
+ prefixes: List[str] = []
182
+
183
+ def graph_of(constant: Optional[str], graph_maps: Sequence[Any]) -> None:
184
+ if constant and constant != RR_DEFAULT_GRAPH:
185
+ graphs.add(constant)
186
+ for gm in graph_maps or ():
187
+ if gm.constant and gm.constant != RR_DEFAULT_GRAPH:
188
+ graphs.add(gm.constant)
189
+
190
+ for n in maps.nodes:
191
+ classes.update(n.classes)
192
+ if n.classes:
193
+ properties.add(RDF_TYPE)
194
+ graph_of(n.graph, n.graph_maps)
195
+ for po in n.predicate_objects:
196
+ if po.predicate:
197
+ properties.add(po.predicate)
198
+ graph_of(po.graph, po.graph_maps)
199
+ sub = n.subject
200
+ if sub.term_type == IRI and not sub.relative_iri():
201
+ prefixes.append(sub.prefix())
202
+ for e in maps.edges:
203
+ if e.predicate:
204
+ properties.add(e.predicate)
205
+ graph_of(e.graph, e.graph_maps)
206
+ f = _Facts()
207
+ f.classes, f.properties, f.graphs = sorted(classes), sorted(properties), sorted(graphs)
208
+ f.vocabularies = sorted({ns for iri in classes | properties if (ns := _namespace(iri))})
209
+ f.uri_space = _common_space(prefixes)
210
+ return f
211
+
212
+
213
+ def _namespace(iri: str) -> str:
214
+ cut = max(iri.rfind("#"), iri.rfind("/")) + 1
215
+ return iri[:cut] if cut else ""
216
+
217
+
218
+ def _common_space(prefixes: Sequence[str]) -> str:
219
+ """The longest prefix of every subject IRI, cut back to a whole segment (its last `/`, `#` or `:`);
220
+ "" where the subjects share none, or a subject's prefix is empty."""
221
+ if not prefixes or not all(prefixes):
222
+ return ""
223
+ first, last = min(prefixes), max(prefixes)
224
+ n = 0
225
+ while n < len(first) and first[n] == last[n]:
226
+ n += 1
227
+ common = first[:n]
228
+ if common in prefixes:
229
+ return common
230
+ cut = max(common.rfind("/"), common.rfind("#"), common.rfind(":")) + 1
231
+ return common[:cut]
@@ -0,0 +1,12 @@
1
+ """A SPARQL 1.1 Protocol endpoint over a ProGraph engine.
2
+
3
+ pip install "prograph[endpoint]"
4
+ prograph serve --config graph.yaml
5
+
6
+ docs/guides/sparql-endpoint.md has the options, formats, authentication
7
+ and deployment.
8
+ """
9
+
10
+ from prograph.endpoint.app import Options, ProtocolError, create_app
11
+
12
+ __all__ = ["Options", "ProtocolError", "create_app"]