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
prograph/r2rml/export.py
ADDED
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
"""A mapping's source maps written as R2RML (docs/architecture/r2rml-without-the-bridge.md).
|
|
2
|
+
|
|
3
|
+
`to_r2rml(schema)` gives the `R2RMLMapping` (`prograph.r2rml.model`) of a schema's source maps,
|
|
4
|
+
a YAML or Python `GraphSchema` or an R2RML one; `to_turtle(mapping)` writes it, sorted, so two
|
|
5
|
+
exports of one mapping are the same text. The contract is the round trip: an R2RML mapping
|
|
6
|
+
exported and compiled again gives equal source maps, and an exported YAML mapping gives the same
|
|
7
|
+
SPARQL answers as the YAML (`tests/r2rml/test_export.py`).
|
|
8
|
+
|
|
9
|
+
What each map becomes:
|
|
10
|
+
- a node map is a triples map: its source a `rr:tableName`, or an `rr:sqlQuery` for a query or a
|
|
11
|
+
row filter (the filter's SQL from `schema.rows.owned_conditions`, in `dialect`); its subject, classes,
|
|
12
|
+
and each predicate-object map (a literal's datatype or language, a predicate or graph map);
|
|
13
|
+
- an edge map's end that is a foreign key to one node map (`_end_join`) is a referencing object
|
|
14
|
+
map (R2RML 8): `rr:parentTriplesMap` that node's triples map, an `rr:joinCondition` per key
|
|
15
|
+
column; the join keeps what the end's existence check keeps (a dangling key gives no triple).
|
|
16
|
+
The edge is a predicate-object map of its start's triples map where it reads that map's own
|
|
17
|
+
rows and adds no graph to it, else a triples map over its own rows, the start its subject. A
|
|
18
|
+
start or an end that must name an existing node (`EndRef.exists`) and that no join states (a
|
|
19
|
+
start in another table, an end of two node maps, `peers`) restricts the rows by an EXISTS on
|
|
20
|
+
that node's rows, in an rr:sqlQuery; an edge of an R2RML referencing object map is a
|
|
21
|
+
referencing object map again (`rr:parentTriplesMap`, its join conditions).
|
|
22
|
+
|
|
23
|
+
A foreign key whose column's type differs from its key's (an INTEGER edge column and a VARCHAR key,
|
|
24
|
+
a DOUBLE and an INTEGER, a column under a case-blind collation; a DECIMAL and an INTEGER only under
|
|
25
|
+
`decimal_text: database`, where equal values write two texts, `5.0` and `5`) warns, once per edge
|
|
26
|
+
(`ForeignKeyTypeWarning`): R2RML 8's join compares the two by SQL equality, so the exported mapping
|
|
27
|
+
gives a triple per parent row SQL-equal to the row's value (VARCHAR '0100' and INTEGER 100 on some
|
|
28
|
+
databases; 'a' and 'A' under a case-blind collation), and builds the end from the parent's value, where
|
|
29
|
+
the mapping built it from the edge's. The types are the ones an engine recorded on the schema
|
|
30
|
+
(`GraphSchema.column_types`, read when an engine is built over it); a schema no engine has read, or a
|
|
31
|
+
column of no recorded type, gives no warning.
|
|
32
|
+
|
|
33
|
+
Not R2RML, refused by name (`R2RMLExportError`): a language column, an edge's own properties.
|
|
34
|
+
A YAML node with no `iri_template` exports its convention template (`iri.node_iri_template`). A YAML map's term text is ProGraph's (a value's text, IRI-safe in a
|
|
35
|
+
template); R2RML's natural form differs for a non-integer number (a double key `2.0` is
|
|
36
|
+
`2.0E0`), which the answers test would show.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
from __future__ import annotations
|
|
40
|
+
|
|
41
|
+
from dataclasses import replace
|
|
42
|
+
from types import SimpleNamespace
|
|
43
|
+
from typing import Any, Dict, List, Optional, Tuple
|
|
44
|
+
|
|
45
|
+
from prograph.r2rml.model import (
|
|
46
|
+
GraphMap, JoinCondition, LogicalTable, ObjectMap, PredicateMap, PredicateObjectMap,
|
|
47
|
+
R2RMLMapping, SubjectMap, TriplesMap,
|
|
48
|
+
)
|
|
49
|
+
from prograph.rules import home
|
|
50
|
+
from prograph.schema.source_map import BLANK, IRI, LITERAL
|
|
51
|
+
|
|
52
|
+
RR = "http://www.w3.org/ns/r2rml#"
|
|
53
|
+
_TERM = {IRI: "IRI", BLANK: "BlankNode", LITERAL: "Literal"}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class R2RMLExportError(ValueError):
|
|
57
|
+
"""A map R2RML cannot state (the module's doc)."""
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class ForeignKeyTypeWarning(UserWarning):
|
|
61
|
+
"""An exported foreign key whose column and key differ in type: R2RML 8's join may give other
|
|
62
|
+
triples than the mapping (the module's doc)."""
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def to_r2rml(schema: Any, *, dialect: str = "postgresql",
|
|
66
|
+
prefixes: Optional[Dict[str, str]] = None) -> R2RMLMapping:
|
|
67
|
+
"""`schema`'s source maps as an R2RML mapping; `dialect` writes a filter's or an edge's
|
|
68
|
+
restriction as `rr:sqlQuery` text; `prefixes` are added to the schema's."""
|
|
69
|
+
from prograph.plan.ir.entry import source_maps
|
|
70
|
+
from prograph.plan.sql.dialect import get_dialect_instance
|
|
71
|
+
maps = source_maps(schema)
|
|
72
|
+
d = get_dialect_instance(dialect)
|
|
73
|
+
nodes = {m.id: m for m in maps.nodes}
|
|
74
|
+
out: List[TriplesMap] = []
|
|
75
|
+
for m in maps.nodes:
|
|
76
|
+
out.append(TriplesMap(
|
|
77
|
+
uri=_uri(m.id), logical_table=_table(m.source, d), regular_columns=m.source.regular_columns,
|
|
78
|
+
subject_map=SubjectMap(**_term_fields(m.subject, f"map {m.id}"), classes=sorted(m.classes),
|
|
79
|
+
graph_maps=_graph_constant(m.graph)),
|
|
80
|
+
predicate_object_maps=[_predicate_object(po, f"map {m.id}") for po in m.predicate_objects]))
|
|
81
|
+
by_id = {m.id: tm for m, tm in zip(maps.nodes, out)}
|
|
82
|
+
for e in maps.edges:
|
|
83
|
+
if e.property_columns:
|
|
84
|
+
raise R2RMLExportError(f"edge {e.id}: an edge's own properties have no R2RML form")
|
|
85
|
+
tm = _edge(e, nodes, d)
|
|
86
|
+
tm.regular_columns = (e.join.child if e.join is not None else e.source).regular_columns
|
|
87
|
+
_warn_join_types(schema, e, tm, nodes)
|
|
88
|
+
# An edge over its start map's own rows is a predicate-object map of that triples map
|
|
89
|
+
# (R2RML 6.3, 8), as `compile_r2rml` reads one.
|
|
90
|
+
own = by_id.get(e.start.map_id) if e.start.map_id else None
|
|
91
|
+
start = nodes.get(e.start.map_id) if e.start.map_id else None
|
|
92
|
+
if own is not None and start is not None and tm.logical_table == own.logical_table \
|
|
93
|
+
and _term_fields(e.start.term, "") == _term_fields(start.subject, "") \
|
|
94
|
+
and _within_graphs(own.subject_map.graph_maps, tm.predicate_object_maps):
|
|
95
|
+
own.predicate_object_maps.extend(tm.predicate_object_maps)
|
|
96
|
+
else:
|
|
97
|
+
out.append(tm)
|
|
98
|
+
written = dict(getattr(schema, "prefixes", None) or {})
|
|
99
|
+
written.update(prefixes or {})
|
|
100
|
+
written.setdefault("rr", RR)
|
|
101
|
+
return R2RMLMapping(triples_maps=out, prefixes=written, dialect=dialect)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _uri(map_id: str) -> str:
|
|
105
|
+
"""A triples map's IRI: its map's id, relative (`<#Person>`)."""
|
|
106
|
+
if "://" in map_id or map_id.startswith(("urn:", "#")):
|
|
107
|
+
return map_id
|
|
108
|
+
return "#" + "".join(c if c.isalnum() or c in "-_." else "_" for c in map_id)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _escaped(text: str) -> str:
|
|
112
|
+
"""`text` with R2RML 7.3's escapes, a placeholder's column name included."""
|
|
113
|
+
return text.replace("\\", "\\\\").replace("{", "\\{").replace("}", "\\}")
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _template(parts, ident=lambda name: name) -> str:
|
|
117
|
+
"""An R2RML template's text (R2RML 7.3): a column in braces, `\\`, `{` and `}` escaped."""
|
|
118
|
+
out = []
|
|
119
|
+
for text, is_column in parts:
|
|
120
|
+
out.append("{" + _escaped(ident(text)) + "}" if is_column else _escaped(text))
|
|
121
|
+
return "".join(out)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _term_fields(tm, where: str) -> Dict[str, Any]:
|
|
125
|
+
if tm.language_column is not None:
|
|
126
|
+
raise R2RMLExportError(f"{where}: a language column has no R2RML form (rr:language is a constant)")
|
|
127
|
+
fields: Dict[str, Any] = {"term_type": _TERM[tm.term_type]}
|
|
128
|
+
if tm.template is not None:
|
|
129
|
+
fields["template"] = _template(tm.template)
|
|
130
|
+
elif tm.column is not None:
|
|
131
|
+
fields["column"] = tm.column
|
|
132
|
+
else:
|
|
133
|
+
fields["constant"] = tm.constant
|
|
134
|
+
return fields
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _object(tm, where: str) -> ObjectMap:
|
|
138
|
+
fields = _term_fields(tm, where)
|
|
139
|
+
return ObjectMap(**fields, datatype=tm.datatype if tm.term_type == LITERAL else None,
|
|
140
|
+
language=tm.language if tm.term_type == LITERAL else None)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _graph_maps(maps) -> List[GraphMap]:
|
|
144
|
+
out = []
|
|
145
|
+
for g in maps or ():
|
|
146
|
+
if g.template is not None:
|
|
147
|
+
out.append(GraphMap(template=_template(g.template)))
|
|
148
|
+
elif g.column is not None:
|
|
149
|
+
out.append(GraphMap(column=g.column))
|
|
150
|
+
else:
|
|
151
|
+
out.append(GraphMap(constant=g.constant))
|
|
152
|
+
return out
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def _graph_constant(iri: Optional[str]) -> List[GraphMap]:
|
|
156
|
+
return [GraphMap(constant=iri)] if iri else []
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def _predicate_object(po, where: str) -> PredicateObjectMap:
|
|
160
|
+
if po.predicate is not None:
|
|
161
|
+
pm = PredicateMap(constant=po.predicate)
|
|
162
|
+
elif po.predicate_map.template is not None:
|
|
163
|
+
pm = PredicateMap(template=_template(po.predicate_map.template))
|
|
164
|
+
else:
|
|
165
|
+
pm = PredicateMap(column=po.predicate_map.column)
|
|
166
|
+
return PredicateObjectMap(predicate_maps=[pm], object_maps=[_object(po.object, where)],
|
|
167
|
+
graph_maps=_graph_maps(po.graph_maps) + _graph_constant(po.graph))
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _from_table(qualified: str, d) -> str:
|
|
171
|
+
"""A table in a FROM: a name the mapping delimited as it wrote it (`Dialect.table_name`), any other
|
|
172
|
+
each part quoted."""
|
|
173
|
+
return d.table_name(qualified) if '"' in qualified else d.quote_qualified(qualified)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _written(source) -> Optional[str]:
|
|
177
|
+
"""rr:tableName as the database reads the table (R2RML 5): its qualified name where that differs
|
|
178
|
+
from the name (a schema, a delimited part), each undelimited part delimited where `_ident` would."""
|
|
179
|
+
if source.qualified == source.table:
|
|
180
|
+
return None
|
|
181
|
+
if '"' in source.qualified:
|
|
182
|
+
return source.qualified
|
|
183
|
+
return ".".join(_ident(part) for part in source.qualified.split("."))
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def _table(source, d, restrict: Optional[List[str]] = None) -> LogicalTable:
|
|
187
|
+
"""A source as a logical table: its name, or a query (its own, its row filter, `restrict`)."""
|
|
188
|
+
from prograph.schema.rows import owned_conditions
|
|
189
|
+
conditions = list(restrict or [])
|
|
190
|
+
if source.filter:
|
|
191
|
+
conditions += owned_conditions(SimpleNamespace(filter_predicate=dict(source.filter)), d,
|
|
192
|
+
alias=d.quote("__r"))
|
|
193
|
+
if source.query is None and not conditions:
|
|
194
|
+
return LogicalTable(table_name=source.table, written=_written(source))
|
|
195
|
+
if not conditions:
|
|
196
|
+
return LogicalTable(sql_query=source.query)
|
|
197
|
+
rows = f"({source.query})" if source.query is not None else _from_table(source.qualified, d)
|
|
198
|
+
where = f" WHERE {' AND '.join(conditions)}" if conditions else ""
|
|
199
|
+
return LogicalTable(sql_query=f"SELECT {d.quote('__r')}.* FROM {rows} AS {d.quote('__r')}{where}")
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _edge(e, nodes, d) -> TriplesMap:
|
|
203
|
+
where = f"edge {e.id}"
|
|
204
|
+
if e.join is not None:
|
|
205
|
+
# An R2RML referencing object map (R2RML 8): the child's subject, the parent's.
|
|
206
|
+
parent = e.end.map_id
|
|
207
|
+
om = ObjectMap(parent_triples_map=_uri(parent), term_type="IRI",
|
|
208
|
+
join_conditions=[JoinCondition(child=c, parent=p, parent_regular=p in e.join.parent.regular_columns)
|
|
209
|
+
for c, p in e.join.conditions])
|
|
210
|
+
return TriplesMap(uri=_uri(e.id), logical_table=_table(e.join.child, d),
|
|
211
|
+
subject_map=SubjectMap(**_term_fields(e.start.term, where)),
|
|
212
|
+
predicate_object_maps=[PredicateObjectMap(
|
|
213
|
+
predicate_maps=[_edge_predicate(e)], object_maps=[om],
|
|
214
|
+
graph_maps=_graph_maps(e.graph_maps) + _graph_constant(e.graph))])
|
|
215
|
+
if e.start.term is None:
|
|
216
|
+
raise R2RMLExportError(f"{where}: an edge whose start node builds its IRI from a column beyond "
|
|
217
|
+
f"its key is not exported yet; in R2RML, write the edge's triples map over an "
|
|
218
|
+
f"rr:sqlQuery joining the edge table to the node table on the edge's column = "
|
|
219
|
+
f"the node's key, its subject template reading the node's IRI column")
|
|
220
|
+
restrict = []
|
|
221
|
+
end_join = _end_join(e, nodes)
|
|
222
|
+
for ref in (e.start, e.end):
|
|
223
|
+
node = nodes.get(ref.map_id) if ref.map_id else None
|
|
224
|
+
if not ref.exists or node is None:
|
|
225
|
+
continue
|
|
226
|
+
if ref.term is None or (ref is e.end and end_join is not None):
|
|
227
|
+
continue # joined to its node's rows below: the join is the row's existence
|
|
228
|
+
if node.source.table == e.source.table and node.source.filter == e.source.filter \
|
|
229
|
+
and node.source.query == e.source.query and tuple(ref.columns) == tuple(node.key.columns):
|
|
230
|
+
continue # its own rows: every row names a node of the map
|
|
231
|
+
# The end exists where its map or one of its peers (the label's maps giving its subject) holds the key
|
|
232
|
+
# (rule edge-ends), as SPARQL checks it.
|
|
233
|
+
held = [_exists(ref, node, d)] + [_exists(ref, nodes[p], d) for p in ref.peers if p in nodes]
|
|
234
|
+
restrict.append(held[0] if len(held) == 1 else "(" + " OR ".join(held) + ")")
|
|
235
|
+
if end_join is not None:
|
|
236
|
+
# A foreign key to the end's node, as R2RML writes one (R2RML 8): its triples map the parent,
|
|
237
|
+
# joined on edge column = key. The join keeps the rows the end's existence check kept.
|
|
238
|
+
end = ObjectMap(parent_triples_map=_uri(e.end.map_id), term_type="IRI",
|
|
239
|
+
join_conditions=[JoinCondition(child=c, parent=k, parent_regular=k in nodes[e.end.map_id].source.regular_columns)
|
|
240
|
+
for c, k in end_join])
|
|
241
|
+
elif e.end.term is None:
|
|
242
|
+
# The end's node builds its IRI from a column beyond its key: the node's
|
|
243
|
+
# subject through a referencing object map joined on edge column = key (R2RML 8).
|
|
244
|
+
end = ObjectMap(parent_triples_map=_uri(e.end.map_id), term_type="IRI",
|
|
245
|
+
join_conditions=[JoinCondition(child=c, parent=k, parent_regular=k in nodes[e.end.map_id].source.regular_columns)
|
|
246
|
+
for c, k in zip(e.end.columns, nodes[e.end.map_id].key.columns)])
|
|
247
|
+
else:
|
|
248
|
+
end = ObjectMap(**_term_fields(e.end.term, where))
|
|
249
|
+
return TriplesMap(uri=_uri(e.id), logical_table=_table(e.source, d, restrict),
|
|
250
|
+
subject_map=SubjectMap(**_term_fields(e.start.term, where)),
|
|
251
|
+
predicate_object_maps=[PredicateObjectMap(
|
|
252
|
+
predicate_maps=[_edge_predicate(e)],
|
|
253
|
+
object_maps=[end],
|
|
254
|
+
graph_maps=_graph_maps(e.graph_maps) + _graph_constant(e.graph))])
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
@home("export-foreign-key", "An exported edge whose end is a foreign key is a referencing object map (R2RML 8)",
|
|
258
|
+
terms=("to_r2rml", "export", "foreign key", "referencing object map", "rr:parentTriplesMap",
|
|
259
|
+
"rr:joinCondition", "existence check", "EXISTS", "rr:sqlQuery"))
|
|
260
|
+
def _end_join(e, nodes) -> Optional[List[Tuple[str, str]]]:
|
|
261
|
+
"""The (edge column, end key column) pairs of an edge whose end is a foreign key to its node map,
|
|
262
|
+
where a referencing object map to that node's triples map (R2RML 8) gives exactly the triples the
|
|
263
|
+
edge does: the end must exist (`EndRef.exists`; a dangling key gives no triple, as the join gives
|
|
264
|
+
none), in that one node map (no `peers`: a join has one parent), the end's term the node's subject
|
|
265
|
+
over the edge's columns in its key's place (the parent's subject is then the term the edge's row
|
|
266
|
+
built), and no graph read from the row (a joint query's graph map is not read back). Else None,
|
|
267
|
+
and the edge keeps its check as a query (`_exists`)."""
|
|
268
|
+
ref = e.end
|
|
269
|
+
node = nodes.get(ref.map_id) if ref.map_id else None
|
|
270
|
+
if not ref.exists or node is None or ref.peers or ref.term is None:
|
|
271
|
+
return None
|
|
272
|
+
key = tuple(node.key.columns)
|
|
273
|
+
if not key or len(ref.columns) != len(key) or len(set(ref.columns)) != len(key):
|
|
274
|
+
return None
|
|
275
|
+
if any(g.constant is None for g in e.graph_maps):
|
|
276
|
+
return None
|
|
277
|
+
rename = dict(zip(ref.columns, key))
|
|
278
|
+
if not set(ref.term.columns()) <= set(rename):
|
|
279
|
+
return None
|
|
280
|
+
term = ref.term
|
|
281
|
+
if term.template is not None:
|
|
282
|
+
term = replace(term, template=tuple((rename[t] if c else t, c) for t, c in term.template))
|
|
283
|
+
elif term.column is not None:
|
|
284
|
+
term = replace(term, column=rename[term.column])
|
|
285
|
+
if term.language_column is not None or node.subject.language_column is not None \
|
|
286
|
+
or _term_fields(term, "") != _term_fields(node.subject, ""):
|
|
287
|
+
return None
|
|
288
|
+
return list(zip(ref.columns, key))
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def _warn_join_types(schema: Any, e, tm: TriplesMap, nodes) -> None:
|
|
292
|
+
"""`ForeignKeyTypeWarning` for an edge written as a referencing object map whose join pairs a child
|
|
293
|
+
column with a parent column of another type (the module's doc), from the types an engine recorded
|
|
294
|
+
on `schema`; nothing where either type is not recorded."""
|
|
295
|
+
import warnings
|
|
296
|
+
joined = [om for po in tm.predicate_object_maps for om in po.object_maps if om.parent_triples_map]
|
|
297
|
+
if not joined or not hasattr(schema, "table_column_type"):
|
|
298
|
+
return
|
|
299
|
+
if e.join is not None:
|
|
300
|
+
child, parent = e.join.child.table, e.join.parent.table
|
|
301
|
+
else:
|
|
302
|
+
child, parent = e.source.table, nodes[e.end.map_id].source.table
|
|
303
|
+
differ = []
|
|
304
|
+
for j in joined[0].join_conditions:
|
|
305
|
+
a, b = schema.table_column_type(child, j.child), schema.table_column_type(parent, j.parent)
|
|
306
|
+
canonical = getattr(schema, "decimal_text", "canonical") == "canonical"
|
|
307
|
+
if a is not None and b is not None and _type_name(a) and _type_name(b) \
|
|
308
|
+
and _join_type(a, canonical) != _join_type(b, canonical):
|
|
309
|
+
shown = _type_name if canonical else (lambda k: _join_type(k, False))
|
|
310
|
+
differ.append(f"{child}.{j.child} ({shown(a)}) = {parent}.{j.parent} ({shown(b)})")
|
|
311
|
+
if differ:
|
|
312
|
+
warnings.warn(
|
|
313
|
+
f"edge {e.id}: its foreign key and the key it names differ in type ({'; '.join(differ)}). The "
|
|
314
|
+
f"exported rr:joinCondition compares them by SQL equality (R2RML 8), so the R2RML mapping gives "
|
|
315
|
+
f"a triple for each parent row SQL-equal to the row's value, its object built from the parent's "
|
|
316
|
+
f"value, where this mapping built it from the row's. Give the two columns one type (a view or "
|
|
317
|
+
f"rr:sqlQuery that casts one) for the same triples.", ForeignKeyTypeWarning, stacklevel=3)
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def _type_name(kind: Any) -> Optional[str]:
|
|
321
|
+
"""A column type as the join's equality and the term's text read it: its datatype (an integer of any
|
|
322
|
+
width one), CHAR(n) padding, a collation that does not compare bytes, a date-time's zone; None for
|
|
323
|
+
a type of no datatype."""
|
|
324
|
+
from prograph.schema import xsd
|
|
325
|
+
dt = xsd.datatype_of_type(kind)
|
|
326
|
+
if dt is None:
|
|
327
|
+
return None
|
|
328
|
+
if dt in xsd.INTEGER_DATATYPES:
|
|
329
|
+
dt = xsd.INTEGER
|
|
330
|
+
name = dt.replace(xsd.XSD, "xsd:")
|
|
331
|
+
if getattr(kind, "padded", False):
|
|
332
|
+
name += ", CHAR(n)"
|
|
333
|
+
if getattr(kind, "collated", False):
|
|
334
|
+
name += ", under a collation that does not compare bytes"
|
|
335
|
+
if dt == xsd.DATETIME:
|
|
336
|
+
name += ", with a zone" if getattr(kind, "zoned", False) else ", local"
|
|
337
|
+
return name
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
def _join_type(kind: Any, canonical: bool) -> Optional[str]:
|
|
341
|
+
"""`_type_name`, an integer and a decimal one type under the canonical decimal text (D3): SQL
|
|
342
|
+
equality meets them where their values are equal, and there both write one text (5.0 is `5`).
|
|
343
|
+
Under `decimal_text: database` they write `5.0` and `5`, two types, and so do decimals of two
|
|
344
|
+
scales (`5.0`, `5.00`)."""
|
|
345
|
+
name = _type_name(kind)
|
|
346
|
+
if name == "xsd:decimal":
|
|
347
|
+
digits = getattr(kind, "digits", None)
|
|
348
|
+
return "xsd:integer" if canonical else name + (f" of scale {digits[1]}" if digits else "")
|
|
349
|
+
return name
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def _within_graphs(subject_graphs, predicate_objects) -> bool:
|
|
353
|
+
"""A predicate-object map moved onto another triples map keeps its graphs: its triples are also in
|
|
354
|
+
that map's subject graphs (R2RML 11.1), so those must be among its own."""
|
|
355
|
+
return all(all(g in po.graph_maps for g in subject_graphs) for po in predicate_objects)
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
def _edge_predicate(e) -> PredicateMap:
|
|
359
|
+
if e.predicate is not None:
|
|
360
|
+
return PredicateMap(constant=e.predicate)
|
|
361
|
+
if e.predicate_map.template is not None:
|
|
362
|
+
return PredicateMap(template=_template(e.predicate_map.template))
|
|
363
|
+
return PredicateMap(column=e.predicate_map.column)
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
def _exists(ref, node, d) -> str:
|
|
367
|
+
"""The edge row's end names a row of `node` (its key, and its filter on a shared table)."""
|
|
368
|
+
from prograph.schema.rows import owned_conditions
|
|
369
|
+
n = d.quote("__n")
|
|
370
|
+
conds = [f"{n}.{d.quote(k)} = {d.quote('__r')}.{d.quote(c)}" for k, c in zip(node.key.columns, ref.columns)]
|
|
371
|
+
if node.source.filter:
|
|
372
|
+
conds += owned_conditions(SimpleNamespace(filter_predicate=dict(node.source.filter)), d, alias=n)
|
|
373
|
+
rows = f"({node.source.query})" if node.source.query is not None else _from_table(node.source.qualified, d)
|
|
374
|
+
return f"EXISTS (SELECT 1 FROM {rows} AS {n} WHERE {' AND '.join(conds)})"
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
# -- Turtle ---------------------------------------------------------------------------------
|
|
378
|
+
|
|
379
|
+
def to_turtle(mapping: R2RMLMapping, dialect: Optional[str] = None) -> str:
|
|
380
|
+
"""`mapping` as Turtle: prefixes, then each triples map, sorted by IRI, with nested blank
|
|
381
|
+
nodes; the same mapping is the same text. A column the model names undelimited
|
|
382
|
+
(`TriplesMap.regular_columns`, `JoinCondition.parent_regular`) is written bare, as its author wrote
|
|
383
|
+
it, and the database folds it as it did; a bare name that is a reserved word is written delimited
|
|
384
|
+
in the folded form `dialect` gives it (by default the database the mapping was exported for,
|
|
385
|
+
`R2RMLMapping.dialect`, else PostgreSQL's lower case). Every other name is exact and delimited
|
|
386
|
+
where a database could read it as another name."""
|
|
387
|
+
dialect = dialect or mapping.dialect or "postgresql"
|
|
388
|
+
prefixes = dict(sorted(mapping.prefixes.items()))
|
|
389
|
+
lines = [f"@prefix {p}: <{ns}> ." for p, ns in prefixes.items()]
|
|
390
|
+
if mapping.base:
|
|
391
|
+
lines.append(f"@base <{mapping.base}> .")
|
|
392
|
+
for tm in sorted(mapping.triples_maps, key=lambda t: t.uri):
|
|
393
|
+
lines.append("")
|
|
394
|
+
lines.append(_turtle_map(tm, prefixes, dialect))
|
|
395
|
+
return "\n".join(lines) + "\n"
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
def _ident(name: str, regular: bool = False, dialect: str = "postgresql") -> str:
|
|
399
|
+
"""A column name as the Turtle writes it, an R2RML SQL identifier (R2RML 5), the same text in
|
|
400
|
+
rr:column, a template's braces and a join condition (rr:sqlQuery text is quoted by `Dialect.quote`).
|
|
401
|
+
A name the mapping wrote undelimited (`regular`) stays bare, so the database folds it as it did, unless
|
|
402
|
+
it is a reserved word: then its folded form (`Dialect.fold_identifier`) is delimited. Any other name is
|
|
403
|
+
exact: `Dialect.r2rml_identifier` delimits it where a database could read the bare name as another."""
|
|
404
|
+
from prograph.plan.sql.dialect import Dialect, get_dialect_instance, _WORDS_AN_ALIAS_AVOIDS
|
|
405
|
+
if regular:
|
|
406
|
+
if name.lower() in _WORDS_AN_ALIAS_AVOIDS:
|
|
407
|
+
return Dialect.r2rml_identifier(get_dialect_instance(dialect).fold_identifier(name))
|
|
408
|
+
return name
|
|
409
|
+
return Dialect.r2rml_identifier(name)
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
def _template_text(template: str, regular=frozenset(), dialect: str = "postgresql") -> str:
|
|
413
|
+
"""A model's template, its column placeholders written as `_ident` writes them."""
|
|
414
|
+
from prograph.schema.iri import template_parts
|
|
415
|
+
return _template(template_parts(template, escapes=True), lambda n: _ident(n, n in regular, dialect))
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
def _lit(text: str) -> str:
|
|
419
|
+
escaped = text.replace("\\", "\\\\").replace('"', '\\"').replace("\n", "\\n").replace("\r", "\\r")
|
|
420
|
+
return f'"{escaped}"'
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
def _iri(iri: str, prefixes: Dict[str, str]) -> str:
|
|
424
|
+
for p, ns in sorted(prefixes.items(), key=lambda kv: -len(kv[1])):
|
|
425
|
+
local = iri[len(ns):] if iri.startswith(ns) else None
|
|
426
|
+
if local is not None and local and all(c.isalnum() or c in "_-" for c in local) and local[0].isalpha():
|
|
427
|
+
return f"{p}:{local}"
|
|
428
|
+
return f"<{iri}>"
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
def _block(props: List[str], indent: str) -> str:
|
|
432
|
+
return "[ " + f" ;\n{indent} ".join(props) + " ]"
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
def _term_props(t, prefixes, regular=frozenset(), dialect: str = "postgresql") -> List[str]:
|
|
436
|
+
props = []
|
|
437
|
+
if t.template is not None:
|
|
438
|
+
props.append(f"rr:template {_lit(_template_text(t.template, regular, dialect))}")
|
|
439
|
+
if t.column is not None:
|
|
440
|
+
props.append(f"rr:column {_lit(_ident(t.column, t.column in regular, dialect))}")
|
|
441
|
+
if getattr(t, "constant", None) is not None:
|
|
442
|
+
if getattr(t, "term_type", "IRI") == "Literal":
|
|
443
|
+
# A constant literal carries its tag or datatype (R2RML 7.1).
|
|
444
|
+
if getattr(t, "language", None):
|
|
445
|
+
props.append(f"rr:constant {_lit(t.constant)}@{t.language}")
|
|
446
|
+
elif getattr(t, "datatype", None):
|
|
447
|
+
props.append(f"rr:constant {_lit(t.constant)}^^{_iri(t.datatype, prefixes)}")
|
|
448
|
+
else:
|
|
449
|
+
props.append(f"rr:constant {_lit(t.constant)}")
|
|
450
|
+
else:
|
|
451
|
+
props.append(f"rr:constant {_iri(t.constant, prefixes)}")
|
|
452
|
+
return props
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
def _turtle_map(tm: TriplesMap, prefixes, dialect: str = "postgresql") -> str:
|
|
456
|
+
out = [f"<{tm.uri}>" if not tm.uri.startswith("#") else f"<{tm.uri}>"]
|
|
457
|
+
body = []
|
|
458
|
+
lt = tm.logical_table
|
|
459
|
+
table = f"rr:tableName {_lit(lt.written or lt.table_name)}" if lt.table_name else f"rr:sqlQuery {_lit(lt.sql_query or '')}"
|
|
460
|
+
body.append("rr:logicalTable " + _block([table], " "))
|
|
461
|
+
sm = tm.subject_map
|
|
462
|
+
sprops = _term_props(sm, prefixes, tm.regular_columns, dialect)
|
|
463
|
+
if sm.term_type != "IRI":
|
|
464
|
+
sprops.append(f"rr:termType rr:{sm.term_type}")
|
|
465
|
+
sprops += [f"rr:class {_iri(c, prefixes)}" for c in sm.classes]
|
|
466
|
+
sprops += [f"rr:graphMap {_block(_term_props(g, prefixes, tm.regular_columns, dialect), ' ')}" for g in sm.graph_maps]
|
|
467
|
+
body.append("rr:subjectMap " + _block(sprops, " "))
|
|
468
|
+
for po in tm.predicate_object_maps:
|
|
469
|
+
props = []
|
|
470
|
+
for pm in po.predicate_maps:
|
|
471
|
+
if pm.constant is not None:
|
|
472
|
+
props.append(f"rr:predicate {_iri(pm.constant, prefixes)}")
|
|
473
|
+
else:
|
|
474
|
+
props.append("rr:predicateMap " + _block(_term_props(pm, prefixes, tm.regular_columns, dialect), " "))
|
|
475
|
+
for om in po.object_maps:
|
|
476
|
+
if om.parent_triples_map is not None:
|
|
477
|
+
oprops = [f"rr:parentTriplesMap <{om.parent_triples_map}>"]
|
|
478
|
+
oprops += ["rr:joinCondition " + _block([f"rr:child {_lit(_ident(j.child, j.child in tm.regular_columns, dialect))}", f"rr:parent {_lit(_ident(j.parent, j.parent_regular, dialect))}"],
|
|
479
|
+
" ") for j in om.join_conditions]
|
|
480
|
+
else:
|
|
481
|
+
oprops = _term_props(om, prefixes, tm.regular_columns, dialect)
|
|
482
|
+
# R2RML 7.4: a column, a datatype or a language is a literal by default, a template
|
|
483
|
+
# an IRI; a constant is the kind of term it is.
|
|
484
|
+
if om.constant is None:
|
|
485
|
+
default = "Literal" if (om.column or om.datatype or om.language) else "IRI"
|
|
486
|
+
if om.term_type != default:
|
|
487
|
+
oprops.append(f"rr:termType rr:{om.term_type}")
|
|
488
|
+
if om.datatype and om.constant is None:
|
|
489
|
+
oprops.append(f"rr:datatype {_iri(om.datatype, prefixes)}")
|
|
490
|
+
if om.language and om.constant is None:
|
|
491
|
+
oprops.append(f"rr:language {_lit(om.language)}")
|
|
492
|
+
props.append("rr:objectMap " + _block(oprops, " "))
|
|
493
|
+
props += [f"rr:graphMap {_block(_term_props(g, prefixes, tm.regular_columns, dialect), ' ')}" for g in po.graph_maps]
|
|
494
|
+
body.append("rr:predicateObjectMap " + _block(props, " "))
|
|
495
|
+
return out[0] + "\n " + " ;\n ".join(body) + " ."
|
prograph/r2rml/model.py
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"""R2RML data model.
|
|
2
|
+
|
|
3
|
+
Dataclass representations of R2RML mapping constructs as defined in
|
|
4
|
+
the W3C R2RML specification (https://www.w3.org/TR/r2rml/).
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from dataclasses import dataclass, field
|
|
8
|
+
from typing import FrozenSet, Dict, List, Optional
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass
|
|
12
|
+
class LogicalTable:
|
|
13
|
+
"""R2RML logical table, either a base table or SQL query.
|
|
14
|
+
|
|
15
|
+
Attributes:
|
|
16
|
+
table_name: Base table name (rr:tableName)
|
|
17
|
+
sql_query: SQL query defining a virtual table (rr:sqlQuery)
|
|
18
|
+
sql_versions: The SQL version identifiers the document gives the
|
|
19
|
+
query (rr:sqlVersion, R2RML 5.2), as IRIs
|
|
20
|
+
"""
|
|
21
|
+
table_name: Optional[str] = None
|
|
22
|
+
sql_query: Optional[str] = None
|
|
23
|
+
sql_versions: List[str] = field(default_factory=list)
|
|
24
|
+
written: Optional[str] = None
|
|
25
|
+
"""rr:tableName as written, its delimited parts in their quotes: the name SQL
|
|
26
|
+
reads the table by; None means `table_name`, undelimited."""
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass
|
|
30
|
+
class GraphMap:
|
|
31
|
+
"""R2RML graph map, assigns triples to a named graph.
|
|
32
|
+
|
|
33
|
+
Attributes:
|
|
34
|
+
template: IRI template for the graph (rr:template)
|
|
35
|
+
column: Column whose value becomes the graph IRI (rr:column)
|
|
36
|
+
constant: Constant graph IRI (rr:constant / rr:graph)
|
|
37
|
+
term_type: The term type the document declares (rr:termType):
|
|
38
|
+
'IRI', 'BlankNode' or 'Literal'; a graph map must give IRIs
|
|
39
|
+
(R2RML 7.4), which `prograph.r2rml.compile` checks
|
|
40
|
+
"""
|
|
41
|
+
template: Optional[str] = None
|
|
42
|
+
column: Optional[str] = None
|
|
43
|
+
constant: Optional[str] = None
|
|
44
|
+
term_type: str = 'IRI'
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass
|
|
48
|
+
class SubjectMap:
|
|
49
|
+
"""R2RML subject map, generates the subject of RDF triples.
|
|
50
|
+
|
|
51
|
+
Attributes:
|
|
52
|
+
template: IRI template with column references (rr:template)
|
|
53
|
+
column: Column whose value becomes the subject (rr:column)
|
|
54
|
+
constant: Constant subject IRI (rr:constant)
|
|
55
|
+
term_type: Term type, 'IRI' or 'BlankNode' (rr:termType); 'Literal'
|
|
56
|
+
where the document declares it, which no subject may be (R2RML
|
|
57
|
+
7.4, checked by `prograph.r2rml.compile`)
|
|
58
|
+
classes: RDF types for the subject (rr:class)
|
|
59
|
+
graph_maps: Named graph assignments for triples with this subject
|
|
60
|
+
"""
|
|
61
|
+
template: Optional[str] = None
|
|
62
|
+
column: Optional[str] = None
|
|
63
|
+
constant: Optional[str] = None
|
|
64
|
+
term_type: str = 'IRI'
|
|
65
|
+
classes: List[str] = field(default_factory=list)
|
|
66
|
+
graph_maps: List[GraphMap] = field(default_factory=list)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
@dataclass
|
|
70
|
+
class PredicateMap:
|
|
71
|
+
"""R2RML predicate map, generates the predicate of RDF triples.
|
|
72
|
+
|
|
73
|
+
Attributes:
|
|
74
|
+
constant: Constant predicate IRI (rr:constant / rr:predicate)
|
|
75
|
+
column: Column whose value becomes the predicate (rr:column)
|
|
76
|
+
template: IRI template for the predicate (rr:template)
|
|
77
|
+
"""
|
|
78
|
+
constant: Optional[str] = None
|
|
79
|
+
column: Optional[str] = None
|
|
80
|
+
template: Optional[str] = None
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
@dataclass
|
|
84
|
+
class JoinCondition:
|
|
85
|
+
"""R2RML join condition for referencing object maps.
|
|
86
|
+
|
|
87
|
+
Attributes:
|
|
88
|
+
child: Column in the child (current) logical table (rr:child)
|
|
89
|
+
parent: Column in the parent (referenced) logical table (rr:parent)
|
|
90
|
+
"""
|
|
91
|
+
child: str
|
|
92
|
+
parent: str
|
|
93
|
+
parent_regular: bool = False
|
|
94
|
+
"""rr:parent was written undelimited, so the database folds it."""
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
@dataclass
|
|
98
|
+
class ObjectMap:
|
|
99
|
+
"""R2RML object map, generates the object of RDF triples.
|
|
100
|
+
|
|
101
|
+
Attributes:
|
|
102
|
+
template: IRI template for the object (rr:template)
|
|
103
|
+
column: Column whose value becomes the object (rr:column)
|
|
104
|
+
constant: Constant object value (rr:constant / rr:object)
|
|
105
|
+
parent_triples_map: URI of referenced TriplesMap (rr:parentTriplesMap)
|
|
106
|
+
join_conditions: Join conditions for referencing object maps
|
|
107
|
+
term_type: Term type, 'IRI', 'BlankNode', or 'Literal' (rr:termType)
|
|
108
|
+
datatype: XSD datatype IRI for literal objects (rr:datatype)
|
|
109
|
+
language: Language tag for literal objects (rr:language)
|
|
110
|
+
"""
|
|
111
|
+
template: Optional[str] = None
|
|
112
|
+
column: Optional[str] = None
|
|
113
|
+
constant: Optional[str] = None
|
|
114
|
+
parent_triples_map: Optional[str] = None
|
|
115
|
+
join_conditions: List[JoinCondition] = field(default_factory=list)
|
|
116
|
+
term_type: str = 'Literal'
|
|
117
|
+
datatype: Optional[str] = None
|
|
118
|
+
language: Optional[str] = None
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
@dataclass
|
|
122
|
+
class PredicateObjectMap:
|
|
123
|
+
"""R2RML predicate-object map, pairs predicates with objects.
|
|
124
|
+
|
|
125
|
+
Attributes:
|
|
126
|
+
predicate_maps: Predicate maps for this pair
|
|
127
|
+
object_maps: Object maps for this pair
|
|
128
|
+
graph_maps: Named graph assignments for triples from this pair
|
|
129
|
+
"""
|
|
130
|
+
predicate_maps: List[PredicateMap] = field(default_factory=list)
|
|
131
|
+
object_maps: List[ObjectMap] = field(default_factory=list)
|
|
132
|
+
graph_maps: List[GraphMap] = field(default_factory=list)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
@dataclass
|
|
136
|
+
class TriplesMap:
|
|
137
|
+
"""R2RML triples map, the top-level mapping construct.
|
|
138
|
+
|
|
139
|
+
A TriplesMap defines how rows from a logical table produce RDF triples.
|
|
140
|
+
|
|
141
|
+
Attributes:
|
|
142
|
+
uri: The URI identifying this TriplesMap in the R2RML document
|
|
143
|
+
logical_table: The source table or SQL query
|
|
144
|
+
subject_map: How to generate the subject
|
|
145
|
+
predicate_object_maps: How to generate predicate-object pairs
|
|
146
|
+
graph_maps: Default named graph assignments for all triples
|
|
147
|
+
subject_map_count: How many subject maps the document gives the
|
|
148
|
+
map (rr:subjectMap and rr:subject); R2RML 6 requires exactly
|
|
149
|
+
one, which `prograph.r2rml.compile` checks
|
|
150
|
+
"""
|
|
151
|
+
uri: str
|
|
152
|
+
logical_table: LogicalTable = field(default_factory=LogicalTable)
|
|
153
|
+
subject_map: SubjectMap = field(default_factory=SubjectMap)
|
|
154
|
+
predicate_object_maps: List[PredicateObjectMap] = field(default_factory=list)
|
|
155
|
+
graph_maps: List[GraphMap] = field(default_factory=list)
|
|
156
|
+
subject_map_count: int = 1
|
|
157
|
+
regular_columns: FrozenSet[str] = frozenset()
|
|
158
|
+
"""The columns of its logical table this map names undelimited (rr:column, a template's
|
|
159
|
+
placeholder, rr:child), which the database folds; the others are exact."""
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
@dataclass
|
|
163
|
+
class R2RMLMapping:
|
|
164
|
+
"""Complete R2RML mapping document.
|
|
165
|
+
|
|
166
|
+
Attributes:
|
|
167
|
+
triples_maps: All TriplesMap definitions in the document
|
|
168
|
+
prefixes: Namespace prefix declarations from the document
|
|
169
|
+
base: The document's own base IRI (Turtle's @base or BASE), the
|
|
170
|
+
base prepended to a relative IRI the maps make (R2RML 11,
|
|
171
|
+
"base IRI"); None where the document declares none.
|
|
172
|
+
dialect: The database whose SQL the mapping's rr:sqlQuery texts are
|
|
173
|
+
written in, where an export made them (`to_r2rml`, a Direct
|
|
174
|
+
Mapping); `to_turtle` folds a reserved bare name the way it
|
|
175
|
+
does. None for a mapping read from a document.
|
|
176
|
+
"""
|
|
177
|
+
triples_maps: List[TriplesMap] = field(default_factory=list)
|
|
178
|
+
prefixes: Dict[str, str] = field(default_factory=dict)
|
|
179
|
+
base: Optional[str] = None
|
|
180
|
+
dialect: Optional[str] = None
|