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,181 @@
|
|
|
1
|
+
"""SPARQL results in the W3C formats, written from what `execute()` returns.
|
|
2
|
+
|
|
3
|
+
SELECT rows arrive as Arrow batches whose every column is a term struct
|
|
4
|
+
`{value, type, datatype, lang}` (the `terms=all` hint,
|
|
5
|
+
`prograph.plan.ir.lower.answer`): the query decided each term's
|
|
6
|
+
kind, so nothing here reads a column's type. A null struct is an unbound
|
|
7
|
+
variable. A term's kinds and its text in TSV are this module's
|
|
8
|
+
(`TERM_URI`, `TERM_BNODE`, `TERM_LITERAL`, `term_ntriples`), the home of
|
|
9
|
+
a SPARQL result term (docs/architecture/rules.md).
|
|
10
|
+
|
|
11
|
+
The writers are generators: each batch is written as it arrives
|
|
12
|
+
the header first.
|
|
13
|
+
|
|
14
|
+
- JSON: SPARQL 1.1 Query Results JSON Format (W3C Rec 2013-03-21).
|
|
15
|
+
- XML: SPARQL Query Results XML Format (Second Edition, W3C Rec 2013-03-21).
|
|
16
|
+
- CSV and TSV: SPARQL 1.1 Query Results CSV and TSV Formats (W3C Rec
|
|
17
|
+
2013-03-21): CSV plain values, TSV each term in its Turtle/N-Triples
|
|
18
|
+
syntax.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
import json
|
|
24
|
+
from typing import Any, Dict, Iterable, Iterator, List, Optional
|
|
25
|
+
from xml.sax.saxutils import escape, quoteattr
|
|
26
|
+
|
|
27
|
+
JSON = "application/sparql-results+json"
|
|
28
|
+
XML = "application/sparql-results+xml"
|
|
29
|
+
CSV = "text/csv"
|
|
30
|
+
TSV = "text/tab-separated-values"
|
|
31
|
+
SOLUTION_TYPES = (JSON, XML, CSV, TSV)
|
|
32
|
+
|
|
33
|
+
# CONSTRUCT: media type to rdflib's serializer name.
|
|
34
|
+
GRAPH_TYPES = {
|
|
35
|
+
"text/turtle": "turtle",
|
|
36
|
+
"application/n-triples": "nt",
|
|
37
|
+
"application/rdf+xml": "xml",
|
|
38
|
+
"application/ld+json": "json-ld",
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
from prograph.schema import xsd as _xsd # noqa: E402
|
|
42
|
+
|
|
43
|
+
from prograph.plan.ir.lower import ANSWER_TYPE as _ANSWER_TYPE # noqa: E402
|
|
44
|
+
from prograph.schema.source_map import BLANK as _BLANK, IRI as _IRI, LITERAL as _LITERAL # noqa: E402
|
|
45
|
+
from prograph.rules import module_home
|
|
46
|
+
|
|
47
|
+
module_home("sparql-result-formats", "A SPARQL result written in a W3C format",
|
|
48
|
+
terms=("write_json", "write_xml", "write_csv", "write_tsv", "SPARQL results format", "ASK", "CONSTRUCT"))
|
|
49
|
+
|
|
50
|
+
# A term struct's `type`, the JSON format's words (3.2.2).
|
|
51
|
+
TERM_URI, TERM_BNODE, TERM_LITERAL = (_ANSWER_TYPE[_IRI], _ANSWER_TYPE[_BLANK],
|
|
52
|
+
_ANSWER_TYPE[_LITERAL])
|
|
53
|
+
# Datatypes a result writes as a simple literal (no datatype written).
|
|
54
|
+
SIMPLE_LITERAL_DATATYPES = (None, _xsd.STRING, _xsd.RDF_LANGSTRING)
|
|
55
|
+
_ECHAR = {"\\": "\\\\", '"': '\\"', "\n": "\\n", "\r": "\\r", "\t": "\\t",
|
|
56
|
+
"\b": "\\b", "\f": "\\f"}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def term_ntriples(value: str, kind: str, datatype: Optional[str] = None,
|
|
60
|
+
lang: Optional[str] = None) -> str:
|
|
61
|
+
"""A result term in N-Triples term syntax, on one line: `<iri>`,
|
|
62
|
+
`_:label`, `"text"`, `"text"@lang`, `"text"^^<datatype>`, the text's
|
|
63
|
+
quote, backslash and line breaks escaped (ECHAR). The form Turtle shares
|
|
64
|
+
with N-Triples, which the TSV results format writes (its section 3). The
|
|
65
|
+
lexical form is written as given: "01"^^xsd:integer stays "01"."""
|
|
66
|
+
if kind == TERM_URI:
|
|
67
|
+
return f"<{value}>"
|
|
68
|
+
if kind == TERM_BNODE:
|
|
69
|
+
return f"_:{value}"
|
|
70
|
+
text = '"' + "".join(_ECHAR.get(c, c) for c in value) + '"'
|
|
71
|
+
if lang:
|
|
72
|
+
return f"{text}@{lang}"
|
|
73
|
+
if datatype in SIMPLE_LITERAL_DATATYPES:
|
|
74
|
+
return text
|
|
75
|
+
return f"{text}^^<{datatype}>"
|
|
76
|
+
|
|
77
|
+
def _binding(term: Optional[Dict[str, Any]]) -> Optional[Dict[str, Any]]:
|
|
78
|
+
"""A term struct as the JSON format's binding, None for unbound."""
|
|
79
|
+
if term is None or term.get("value") is None or term.get("type") is None:
|
|
80
|
+
return None
|
|
81
|
+
out = {"type": term["type"], "value": term["value"]}
|
|
82
|
+
if term["type"] == TERM_LITERAL:
|
|
83
|
+
if term.get("lang"):
|
|
84
|
+
out["xml:lang"] = term["lang"]
|
|
85
|
+
elif term.get("datatype") not in SIMPLE_LITERAL_DATATYPES:
|
|
86
|
+
out["datatype"] = term["datatype"]
|
|
87
|
+
return out
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def solutions(batches: Iterable[Any]) -> Iterator[List[Optional[Dict[str, Any]]]]:
|
|
91
|
+
"""Each row of each batch, as its bindings in column order."""
|
|
92
|
+
for batch in batches:
|
|
93
|
+
columns = [batch.column(i).to_pylist() for i in range(batch.num_columns)]
|
|
94
|
+
for row in zip(*columns):
|
|
95
|
+
yield [_binding(term) for term in row]
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def write_json(names: List[str], rows: Iterable[List[Optional[Dict[str, Any]]]]) -> Iterator[str]:
|
|
99
|
+
yield '{"head":{"vars":' + json.dumps(names) + '},"results":{"bindings":['
|
|
100
|
+
first = True
|
|
101
|
+
for row in rows:
|
|
102
|
+
binding = {n: b for n, b in zip(names, row) if b is not None}
|
|
103
|
+
yield ("" if first else ",") + json.dumps(binding, ensure_ascii=False)
|
|
104
|
+
first = False
|
|
105
|
+
yield "]}}\n"
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def write_xml(names: List[str], rows: Iterable[List[Optional[Dict[str, Any]]]]) -> Iterator[str]:
|
|
109
|
+
yield ('<?xml version="1.0"?>\n<sparql xmlns="http://www.w3.org/2005/sparql-results#">\n'
|
|
110
|
+
'<head>' + "".join(f"<variable name={quoteattr(n)}/>" for n in names) + '</head>\n<results>\n')
|
|
111
|
+
for row in rows:
|
|
112
|
+
parts = []
|
|
113
|
+
for name, b in zip(names, row):
|
|
114
|
+
if b is None:
|
|
115
|
+
continue
|
|
116
|
+
if b["type"] == TERM_URI:
|
|
117
|
+
inner = f"<uri>{escape(b['value'])}</uri>"
|
|
118
|
+
elif b["type"] == TERM_BNODE:
|
|
119
|
+
inner = f"<bnode>{escape(b['value'])}</bnode>"
|
|
120
|
+
elif "xml:lang" in b:
|
|
121
|
+
inner = f"<literal xml:lang={quoteattr(b['xml:lang'])}>{escape(b['value'])}</literal>"
|
|
122
|
+
elif "datatype" in b:
|
|
123
|
+
inner = f"<literal datatype={quoteattr(b['datatype'])}>{escape(b['value'])}</literal>"
|
|
124
|
+
else:
|
|
125
|
+
inner = f"<literal>{escape(b['value'])}</literal>"
|
|
126
|
+
parts.append(f"<binding name={quoteattr(name)}>{inner}</binding>")
|
|
127
|
+
yield "<result>" + "".join(parts) + "</result>\n"
|
|
128
|
+
yield "</results>\n</sparql>\n"
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _csv_field(text: str) -> str:
|
|
132
|
+
if any(c in text for c in ',"\r\n'):
|
|
133
|
+
return '"' + text.replace('"', '""') + '"'
|
|
134
|
+
return text
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def write_csv(names: List[str], rows: Iterable[List[Optional[Dict[str, Any]]]]) -> Iterator[str]:
|
|
138
|
+
"""Plain values (CSV format 2): an IRI as its text, a blank node as
|
|
139
|
+
`_:label`, a literal as its lexical form; rows end in CRLF."""
|
|
140
|
+
yield ",".join(_csv_field(n) for n in names) + "\r\n"
|
|
141
|
+
for row in rows:
|
|
142
|
+
fields = []
|
|
143
|
+
for b in row:
|
|
144
|
+
if b is None:
|
|
145
|
+
fields.append("")
|
|
146
|
+
elif b["type"] == TERM_BNODE:
|
|
147
|
+
fields.append(_csv_field("_:" + b["value"]))
|
|
148
|
+
else:
|
|
149
|
+
fields.append(_csv_field(b["value"]))
|
|
150
|
+
yield ",".join(fields) + "\r\n"
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def write_tsv(names: List[str], rows: Iterable[List[Optional[Dict[str, Any]]]]) -> Iterator[str]:
|
|
154
|
+
"""Each term in its Turtle syntax (TSV format 3), variables as `?name`."""
|
|
155
|
+
yield "\t".join("?" + n for n in names) + "\n"
|
|
156
|
+
for row in rows:
|
|
157
|
+
yield "\t".join("" if b is None else term_ntriples(b["value"], b["type"], b.get("datatype"),
|
|
158
|
+
b.get("xml:lang"))
|
|
159
|
+
for b in row) + "\n"
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
SOLUTION_WRITERS = {JSON: write_json, XML: write_xml, CSV: write_csv, TSV: write_tsv}
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
# The CSV and TSV Recommendation defines them for SELECT only (its section
|
|
166
|
+
# 1), so an ASK is answered in JSON or XML.
|
|
167
|
+
BOOLEAN_TYPES = (JSON, XML)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def write_boolean(media_type: str, answer: bool) -> str:
|
|
171
|
+
"""An ASK answer in the JSON or XML results format."""
|
|
172
|
+
if media_type == JSON:
|
|
173
|
+
return json.dumps({"head": {}, "boolean": bool(answer)}) + "\n"
|
|
174
|
+
return ('<?xml version="1.0"?>\n<sparql xmlns="http://www.w3.org/2005/sparql-results#">\n'
|
|
175
|
+
f"<head/>\n<boolean>{'true' if answer else 'false'}</boolean>\n</sparql>\n")
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def write_graph(graph: Any, media_type: str) -> bytes:
|
|
179
|
+
"""A CONSTRUCT graph by rdflib's serializer for the media type."""
|
|
180
|
+
out = graph.serialize(format=GRAPH_TYPES[media_type])
|
|
181
|
+
return out.encode("utf-8") if isinstance(out, str) else out
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""The SPARQL 1.1 Service Description (W3C Rec 2013-03-21) of a server,
|
|
2
|
+
answered at GET /sparql with no query. The row cap and timeout are stated in a `prograph:` namespace, as the Recommendation
|
|
3
|
+
lets a description extend its vocabulary."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from prograph.describe import RESULT_FORMAT_IRIS, describe
|
|
10
|
+
|
|
11
|
+
PROGRAPH = "http://prograph.dev/ns/endpoint#"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def service_description(options: Any, engine: Any = None) -> Any:
|
|
15
|
+
"""The Service Description of a server: its endpoint, languages, result formats and limits, and,
|
|
16
|
+
where one engine serves every request (`engine`), the dataset it maps (`ProGraph.describe_dataset`: classes,
|
|
17
|
+
properties, vocabularies, URI space and named graphs, no counts: a GET stays cheap)."""
|
|
18
|
+
from rdflib import Literal, Namespace
|
|
19
|
+
from rdflib.namespace import RDF, XSD
|
|
20
|
+
pg = Namespace(PROGRAPH)
|
|
21
|
+
if engine is None:
|
|
22
|
+
g = describe(endpoint=options.endpoint_iri, formats=RESULT_FORMAT_IRIS)
|
|
23
|
+
else:
|
|
24
|
+
g = engine.describe_dataset(endpoint=options.endpoint_iri)
|
|
25
|
+
g.bind("prograph", pg)
|
|
26
|
+
(service,) = g.subjects(RDF.type, Namespace("http://www.w3.org/ns/sparql-service-description#").Service)
|
|
27
|
+
if options.row_cap is not None:
|
|
28
|
+
g.add((service, pg.rowCap, Literal(options.row_cap, datatype=XSD.integer)))
|
|
29
|
+
if options.timeout is not None:
|
|
30
|
+
g.add((service, pg.timeoutSeconds, Literal(options.timeout, datatype=XSD.decimal)))
|
|
31
|
+
return g
|