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/plan/ir/ql.py ADDED
@@ -0,0 +1,987 @@
1
+ """OWL 2 QL answered by rewriting (profile="ql"; OWL 2 Profiles §3, Direct semantics: Entailment Regimes §7).
2
+
3
+ An ontology's QL axioms are read into DL-Lite's terms (Calvanese et al. 2007; Profiles §3 is DL-Lite_R): **basic
4
+ concepts** (a named class, owl:Thing, `∃R` for a property or its inverse, `∃d.DR` for a data property) and **roles**
5
+ (a property `p`, or its inverse `p⁻`), and closed once:
6
+
7
+ - subclass axioms whose subclass is a basic concept and whose superclass is a basic concept, an intersection of them,
8
+ `∃R.C` (one edge `B ⊑ ∃R`: the witness `∃R.C` names is unnamed and never bound, so its class only matters to the
9
+ consistency checks) or a complement (a negative inclusion); equivalences as both directions; domain and range
10
+ (`∃p ⊑ C`, `∃p⁻ ⊑ C`); subproperty and equivalent properties between roles (`p ⊑ q⁻` included), owl:inverseOf
11
+ (`p ≡ q⁻`), symmetric (`p ⊑ p⁻`) and reflexive properties (`⊤ ⊑ ∃R`, `⊤ ⊑ ∃R⁻`).
12
+ - From the closure (`QLTBox`): each named class's superclasses and each property's super-properties (the maps'
13
+ saturation, `saturate.saturate`), each property's inverse sub-properties (`inverse_subs`: `?s R ?o` also reads
14
+ `?o p ?s`), the classes each property's subjects and objects are (`domains`, `ranges`), the classes every
15
+ individual is (`everything`: `⊤ ⊑ C`), the properties every individual has to itself (`reflexive`), and the
16
+ data properties whose values of some datatypes type their subject (`data_typed`).
17
+ - **Every individual** (owl:Thing's extension, what a reflexive property relates each to itself, what `⊤ ⊑ C` types):
18
+ every IRI that is the subject of a triple of the mapped data, or the object of one whose predicate is not
19
+ rdf:type (an rdf:type object is a class), in the default graph; and every IRI the ontology itself asserts about or
20
+ declares an owl:NamedIndividual (`individuals`: an annotation asserts nothing, and the ontology, a class, a property
21
+ or a datatype is no individual). Literals are values, never individuals; a blank node is not one
22
+ either (OWL 2 QL has no anonymous individuals, Profiles §3.2.1).
23
+ - A data range is matched by value where the numeric value spaces nest (`in_range`: "1.0"^^xsd:decimal is an
24
+ xsd:integer), by datatype otherwise.
25
+ - The negative axioms (disjoint classes, owl:complementOf as a superclass, disjoint properties, asymmetric properties,
26
+ owl:differentFrom and owl:AllDifferent, a data range) are consistency checks (`checks`), each a SPARQL query whose
27
+ solutions are the facts that violate it, answered under the same rewriting, plus the basic concepts the TBox alone
28
+ makes empty (`unsatisfiable`: their members violate it; an empty owl:Thing is a check with no query, violated by
29
+ the ontology alone). `violations(engine)` runs them; nothing raises here. Their one answer is owl:differentFrom:
30
+ `?a owl:differentFrom ?b` reads `difference`'s pattern, the pairs of named individuals a = b would make
31
+ inconsistent (`different`).
32
+
33
+ Nothing is materialized from the data. The ontology's own assertions (an ontology may state facts) are closed here,
34
+ in memory, as its other triples are (`abox`), and join the RDFS core's closure (`saturate.read_ontology`)."""
35
+
36
+ from __future__ import annotations
37
+
38
+ from dataclasses import dataclass, field
39
+ from typing import Any, Dict, FrozenSet, Iterable, List, Optional, Set, Tuple
40
+
41
+ from prograph.rules import module_home
42
+ from prograph.schema import xsd
43
+
44
+ module_home("owl-ql-rewriting", "OWL 2 QL's axioms answered by rewriting: the TBox closed over basic concepts and "
45
+ "roles, every individual, the negative axioms as consistency checks",
46
+ terms=("OWL 2 QL", "DL-Lite", "basic concept", "role", "inverse property", "owl:inverseOf",
47
+ "symmetric property", "reflexive property", "asymmetric property", "disjoint classes",
48
+ "disjoint properties", "owl:differentFrom", "owl:AllDifferent", "complementOf",
49
+ "existential", "someValuesFrom", "owl:Thing", "every individual", "consistency check",
50
+ "violation", "unsatisfiable", "negative inclusion", "inverse_subs", "QLTBox", "different individuals",
51
+ "a ≠ b"))
52
+
53
+ RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
54
+ RDFS = "http://www.w3.org/2000/01/rdf-schema#"
55
+ OWL = "http://www.w3.org/2002/07/owl#"
56
+ XSD = xsd.XSD
57
+ TYPE = RDF + "type"
58
+ THING, NOTHING = OWL + "Thing", OWL + "Nothing"
59
+ SUB_CLASS, SUB_PROPERTY = RDFS + "subClassOf", RDFS + "subPropertyOf"
60
+ DOMAIN, RANGE = RDFS + "domain", RDFS + "range"
61
+ EQ_CLASS, EQ_PROPERTY = OWL + "equivalentClass", OWL + "equivalentProperty"
62
+ _VOCABULARY = (RDF, RDFS, OWL, XSD)
63
+
64
+ # A basic concept: ("c", iri) a named class, ("top",) owl:Thing, ("bot",) owl:Nothing, ("e", role) ∃R, ("d", property,
65
+ # datatypes) ∃d.DR for a data range of those datatypes. A role: (property, inverted).
66
+ Concept = tuple
67
+ Role = Tuple[str, bool]
68
+ TOP, BOT = ("top",), ("bot",)
69
+
70
+ # The datatypes each OWL 2 QL datatype's value space holds, as a literal's datatype IRI names them (XML Schema 1.1
71
+ # Part 2 §3's derivation; Profiles §3.2.1's map). rdfs:Literal is any value (None).
72
+ _STRINGS = tuple(XSD + k for k in ("string", "normalizedString", "token", "language", "Name", "NCName", "NMTOKEN",
73
+ "ENTITY", "ID", "IDREF"))
74
+ _INTEGERS = tuple(sorted(xsd.INTEGER_DATATYPES))
75
+ _DERIVED: Dict[str, Tuple[str, ...]] = {
76
+ OWL + "real": (OWL + "real", OWL + "rational", XSD + "decimal") + _INTEGERS,
77
+ OWL + "rational": (OWL + "rational", XSD + "decimal") + _INTEGERS,
78
+ XSD + "decimal": (XSD + "decimal",) + _INTEGERS,
79
+ XSD + "integer": _INTEGERS,
80
+ XSD + "nonNegativeInteger": tuple(XSD + k for k in ("nonNegativeInteger", "positiveInteger", "unsignedLong",
81
+ "unsignedInt", "unsignedShort", "unsignedByte")),
82
+ XSD + "string": _STRINGS,
83
+ XSD + "normalizedString": _STRINGS[1:],
84
+ XSD + "token": _STRINGS[2:],
85
+ XSD + "Name": (XSD + "Name", XSD + "NCName", XSD + "ENTITY", XSD + "ID", XSD + "IDREF"),
86
+ XSD + "NCName": (XSD + "NCName", XSD + "ENTITY", XSD + "ID", XSD + "IDREF"),
87
+ XSD + "dateTime": (XSD + "dateTime", XSD + "dateTimeStamp"),
88
+ RDF + "PlainLiteral": _STRINGS + (RDF + "langString",),
89
+ }
90
+
91
+
92
+ from prograph.plan.ir.profiles import _NOT_QL_DT as _NOT_QL # noqa: E402 - the datatype map's home
93
+
94
+
95
+ def datatypes_of(name: str) -> Optional[FrozenSet[str]]:
96
+ """The literal datatypes whose values a data range named `name` holds; None for any value (rdfs:Literal)."""
97
+ if name == RDFS + "Literal":
98
+ return None
99
+ return frozenset(_DERIVED.get(name, (name,)))
100
+
101
+
102
+ @dataclass(frozen=True)
103
+ class Check:
104
+ """One consistency check: what it checks (`kind`), the axiom in short form, a SPARQL SELECT whose solutions
105
+ are the facts that violate it (None: the ontology alone violates it, whatever the data), and those facts as a
106
+ pattern over its variables (`facts`, short Turtle; `consistency.fill`)."""
107
+ kind: str
108
+ axiom: str
109
+ query: Optional[str]
110
+ facts: str = ""
111
+ # A data range's check: (the property, the datatypes of the range), read by `range_holds` and `range_hint`.
112
+ range: Optional[Tuple[str, FrozenSet[str]]] = None
113
+
114
+
115
+ @dataclass(frozen=True)
116
+ class Violation:
117
+ """A check and the solutions that violate it (empty for a check with no query: the ontology alone): `rows`
118
+ all of them, or the first `limit` (`violations`), `count` how many there are."""
119
+ check: Check
120
+ rows: Tuple[Dict[str, Any], ...] = ()
121
+ count: int = 0
122
+ # Why the check could not run (a data error its query met, R2RML 11): no verdict, the queries that read
123
+ # that data raise as before.
124
+ error: str = ""
125
+
126
+
127
+ @dataclass(frozen=True)
128
+ class QLTBox:
129
+ """What the plan reads of an ontology's QL axioms (the module's docstring): named terms only in the maps'
130
+ fields; `consumed` the triples of the axioms read; `checks` the consistency checks."""
131
+ superclasses: Dict[str, FrozenSet[str]] = field(default_factory=dict)
132
+ superproperties: Dict[str, FrozenSet[str]] = field(default_factory=dict)
133
+ domains: Dict[str, FrozenSet[str]] = field(default_factory=dict)
134
+ ranges: Dict[str, FrozenSet[str]] = field(default_factory=dict)
135
+ inverse_subs: Dict[str, FrozenSet[str]] = field(default_factory=dict)
136
+ everything: FrozenSet[str] = frozenset()
137
+ reflexive: FrozenSet[str] = frozenset()
138
+ data_typed: Tuple[Tuple[str, FrozenSet[str], FrozenSet[str]], ...] = ()
139
+ individuals: FrozenSet[str] = frozenset()
140
+ abox: FrozenSet[Tuple[Any, Any, Any]] = field(default=frozenset(), compare=False, repr=False)
141
+ checks: Tuple[Check, ...] = ()
142
+ unsatisfiable: FrozenSet[Concept] = frozenset()
143
+ consumed: FrozenSet[Tuple[Any, Any, Any]] = field(default=frozenset(), compare=False, repr=False)
144
+ # The group pattern whose solutions are the (?qlnd_a, ?qlnd_b) pairs of named individuals Direct semantics
145
+ # makes different (`difference`): `?a owl:differentFrom ?b` reads it.
146
+ different: str = ""
147
+ # What each basic concept implies of unnamed individuals (`B ⊑ ∃R.F`, closed): per concept, the roles R it has an
148
+ # R-successor by and, for each, the named classes and the concepts that successor is in (`witnessed`).
149
+ generates: Dict[Concept, Tuple[Tuple[Role, FrozenSet[str], FrozenSet[Concept]], ...]] = \
150
+ field(default_factory=dict, compare=False, repr=False)
151
+ role_up: Dict[Role, FrozenSet[Role]] = field(default_factory=dict, compare=False, repr=False)
152
+
153
+ def witnessed(self, role: Any, classes: FrozenSet[str], children: Tuple[Any, ...], depth: int = 8
154
+ ) -> FrozenSet[Concept]:
155
+ """The basic concepts whose every member has an unnamed `role`-successor of every class of `classes` from
156
+ which each of `children` ((role, classes, children) trees) follows too: the tree witnesses of a query tree
157
+ hanging from a term by `role` (Kikot, Kontchakov, Zakharyaschev 2012). An implied successor is never named,
158
+ so it only satisfies a hidden term. `role` may be a tuple of roles the one successor has (a term reached
159
+ by several atoms): one axiom's role is under each of them."""
160
+ roles = role if isinstance(role, tuple) and role and isinstance(role[0], tuple) else (role,)
161
+ if depth <= 0:
162
+ return frozenset()
163
+ out = set()
164
+ for concept, implied in self.generates.items():
165
+ for r0, named_fill, fill in implied:
166
+ if not all(r in self.role_up.get(r0, {r0}) for r in roles) or not classes <= named_fill:
167
+ continue
168
+ if all(any(c in fill for c in self.witnessed(*child, depth=depth - 1)) for child in children):
169
+ out.add(concept)
170
+ break
171
+ return frozenset(out)
172
+
173
+
174
+ def inverse(r: Role) -> Role:
175
+ return (r[0], not r[1])
176
+
177
+
178
+ class _Reader:
179
+ """An rdflib graph's QL axioms in DL-Lite's terms (`read`)."""
180
+
181
+ def __init__(self, graph: Any) -> None:
182
+ from rdflib import BNode, Literal, URIRef
183
+ self.g, self.BNode, self.Literal, self.URIRef = graph, BNode, Literal, URIRef
184
+ self.sub: Set[Tuple[Concept, Concept]] = set() # B ⊑ B'
185
+ self.qualified: Set[Tuple[Concept, Role, Concept]] = set() # B ⊑ ∃R.F
186
+ self.disjoint: Set[Tuple[Concept, Concept]] = set() # B ⊑ ¬B'
187
+ self.role_sub: Set[Tuple[Role, Role]] = set() # R ⊑ S
188
+ self.role_disjoint: Set[Tuple[Role, Role]] = set()
189
+ self.reflexive: Set[Role] = set()
190
+ self.asymmetric: Set[str] = set()
191
+ self.different: Set[Tuple[str, str]] = set()
192
+ self.data_ranges: List[Tuple[str, Optional[FrozenSet[str]], str]] = []
193
+ self.consumed: Set[Tuple[Any, Any, Any]] = set()
194
+ self.texts: Dict[Any, str] = {} # a negative axiom's key -> its short form
195
+ self.bottom: List[str] = [] # assertions of a bottom property: inconsistent whatever the data
196
+ # Datatype definitions (`ex:D owl:equivalentClass xsd:integer`): a defined datatype's literal datatypes.
197
+ self.defined: Dict[str, Optional[FrozenSet[str]]] = {}
198
+
199
+ def one(self, s: Any, p: str) -> Any:
200
+ return self.g.value(s, self.URIRef(p))
201
+
202
+ def items(self, head: Any) -> List[Any]:
203
+ out, seen = [], set()
204
+ while head is not None and str(head) != RDF + "nil" and head not in seen:
205
+ seen.add(head)
206
+ first = self.one(head, RDF + "first")
207
+ if first is None:
208
+ return []
209
+ out.append(first)
210
+ head = self.one(head, RDF + "rest")
211
+ return out
212
+
213
+ def short(self, x: Any) -> str:
214
+ if isinstance(x, self.URIRef):
215
+ s = str(x)
216
+ for ns, prefix in ((OWL, "owl:"), (RDFS, "rdfs:"), (RDF, "rdf:"), (XSD, "xsd:")):
217
+ if s.startswith(ns):
218
+ return prefix + s[len(ns):]
219
+ return f"<{s}>"
220
+ if isinstance(x, self.Literal):
221
+ return x.n3()
222
+ inv = self.one(x, OWL + "inverseOf")
223
+ if inv is not None:
224
+ return f"[ owl:inverseOf {self.short(inv)} ]"
225
+ return "[ ... ]"
226
+
227
+ # -- expressions ---------------------------------------------------------------------------------------------
228
+ def role(self, node: Any) -> Optional[Role]:
229
+ if isinstance(node, self.URIRef):
230
+ return (str(node), False)
231
+ if isinstance(node, self.BNode):
232
+ inv = self.one(node, OWL + "inverseOf")
233
+ if isinstance(inv, self.URIRef):
234
+ return (str(inv), True)
235
+ return None
236
+
237
+ def data_range(self, node: Any) -> Tuple[bool, Optional[FrozenSet[str]]]:
238
+ """(is a QL data range, its datatypes: None for any value)."""
239
+ if isinstance(node, self.URIRef):
240
+ name = str(node)
241
+ if name in self.defined:
242
+ return True, self.defined[name]
243
+ return True, datatypes_of(name)
244
+ head = self.one(node, OWL + "intersectionOf") if isinstance(node, self.BNode) else None
245
+ if head is not None:
246
+ held: Optional[FrozenSet[str]] = None
247
+ for part in self.items(head):
248
+ ok, dts = self.data_range(part)
249
+ if not ok:
250
+ return False, None
251
+ held = dts if held is None else (held if dts is None else held & dts)
252
+ return True, held
253
+ return False, None
254
+
255
+ def is_data_range(self, node: Any) -> bool:
256
+ if isinstance(node, self.URIRef):
257
+ s = str(node)
258
+ return s.startswith(XSD) or s in (RDFS + "Literal", RDF + "PlainLiteral", RDF + "langString",
259
+ RDF + "XMLLiteral", OWL + "real", OWL + "rational") or \
260
+ (node, self.URIRef(TYPE), self.URIRef(RDFS + "Datatype")) in self.g
261
+ return isinstance(node, self.BNode) and (
262
+ (node, self.URIRef(TYPE), self.URIRef(RDFS + "Datatype")) in self.g
263
+ or self.one(node, OWL + "onDatatype") is not None)
264
+
265
+ def sub_concept(self, node: Any) -> Optional[Concept]:
266
+ """A QL subclass expression (Profiles §3.2.3) as a basic concept; None for any other."""
267
+ if isinstance(node, self.URIRef) and not self.is_data_range(node):
268
+ s = str(node)
269
+ return TOP if s == THING else BOT if s == NOTHING else ("c", s)
270
+ if not isinstance(node, self.BNode):
271
+ return None
272
+ for kind in ("intersectionOf", "unionOf"):
273
+ one = self.items(self.one(node, OWL + kind))
274
+ if len(one) == 1:
275
+ # One operand is that operand (as `profiles` reads it, after the OWL 2 test cases' declarations).
276
+ return self.sub_concept(one[0]) if one[0] != node else None
277
+ on = self.one(node, OWL + "onProperty")
278
+ filler = self.one(node, OWL + "someValuesFrom")
279
+ if on is None or filler is None:
280
+ return None
281
+ r = self.role(on)
282
+ if r is None:
283
+ return None
284
+ if self.is_data_range(filler):
285
+ ok, dts = self.data_range(filler)
286
+ if not ok or r[1]:
287
+ return None
288
+ return ("e", r) if dts is None else ("d", r[0], dts)
289
+ return ("e", r) if str(filler) == THING else None
290
+
291
+ def super_parts(self, node: Any) -> Optional[List[tuple]]:
292
+ """A QL superclass expression as parts: ("is", B), ("not", B), ("some", R, F): F a named class, owl:Thing
293
+ or owl:Nothing; None for any other expression."""
294
+ b = self.sub_concept(node)
295
+ if b is not None:
296
+ return [("is", b)]
297
+ if not isinstance(node, self.BNode):
298
+ return None
299
+ head = self.one(node, OWL + "intersectionOf")
300
+ if head is not None:
301
+ parts: List[tuple] = []
302
+ for x in self.items(head):
303
+ got = self.super_parts(x)
304
+ if got is None:
305
+ return None
306
+ parts += got
307
+ return parts or None
308
+ neg = self.one(node, OWL + "complementOf")
309
+ if neg is not None:
310
+ b = self.sub_concept(neg)
311
+ return [("not", b)] if b is not None else None
312
+ on, filler = self.one(node, OWL + "onProperty"), self.one(node, OWL + "someValuesFrom")
313
+ if on is not None and isinstance(filler, self.URIRef) and not self.is_data_range(filler):
314
+ r = self.role(on)
315
+ f = self.sub_concept(filler)
316
+ if r is not None and f is not None and f[0] in ("c", "top", "bot"):
317
+ return [("some", r, f)]
318
+ return None
319
+
320
+ def include(self, b: Concept, parts: List[tuple], text: str) -> None:
321
+ for part in parts:
322
+ if part[0] == "is":
323
+ self.sub.add((b, part[1]))
324
+ elif part[0] == "not":
325
+ key = (b, part[1])
326
+ self.disjoint.add(key)
327
+ self.texts.setdefault(("d",) + key, text)
328
+ else:
329
+ self.sub.add((b, ("e", part[1])))
330
+ self.qualified.add((b, part[1], part[2]))
331
+
332
+ # -- the run -------------------------------------------------------------------------------------------------
333
+ def run(self, extra: Iterable[Tuple[str, str, str]] = ()) -> None:
334
+ U = self.URIRef
335
+ inner = {o for o in self.g.objects() if isinstance(o, self.BNode)}
336
+ for s, pred, o in self.g: # datatype definitions first: a data range may name one
337
+ if str(pred) == EQ_CLASS and isinstance(s, U) and self.is_data_range(s) and self.is_data_range(o):
338
+ ok, dts = self.data_range(o)
339
+ if ok:
340
+ self.defined[str(s)] = dts
341
+ self.consumed.add((s, pred, o))
342
+ for s, pred, o in self.g:
343
+ p = str(pred)
344
+ text = f"{self.short(s)} {self.short(pred)} {self.short(o)}"
345
+ done = True
346
+ if (s, pred, o) in self.consumed:
347
+ continue
348
+ if p in (OWL + "topObjectProperty", OWL + "topDataProperty"):
349
+ pass # an assertion of a top property holds of every pair: nothing to apply
350
+ elif p in (OWL + "bottomObjectProperty", OWL + "bottomDataProperty"):
351
+ self.bottom.append(text)
352
+ elif p == SUB_CLASS:
353
+ b, parts = self.sub_concept(s), self.super_parts(o)
354
+ done = b is not None and parts is not None
355
+ if done:
356
+ self.include(b, parts, text)
357
+ elif p == EQ_CLASS:
358
+ a, b = self.sub_concept(s), self.sub_concept(o)
359
+ done = a is not None and b is not None
360
+ if done:
361
+ self.sub |= {(a, b), (b, a)}
362
+ elif p == OWL + "disjointWith":
363
+ a, b = self.sub_concept(s), self.sub_concept(o)
364
+ done = a is not None and b is not None
365
+ if done:
366
+ self.disjoint.add((a, b))
367
+ self.texts.setdefault(("d", a, b), text)
368
+ elif p in (SUB_PROPERTY, EQ_PROPERTY, OWL + "inverseOf", OWL + "propertyDisjointWith"):
369
+ if p == OWL + "inverseOf" and isinstance(s, self.BNode) and s in inner:
370
+ continue # an inverse property expression, read where it is used
371
+ a, b = self.role(s), self.role(o)
372
+ done = a is not None and b is not None
373
+ if done and p == SUB_PROPERTY:
374
+ self.role_sub.add((a, b))
375
+ elif done and p == EQ_PROPERTY:
376
+ self.role_sub |= {(a, b), (b, a)}
377
+ elif done and p == OWL + "inverseOf":
378
+ self.role_sub |= {(a, inverse(b)), (inverse(b), a)}
379
+ elif done:
380
+ self.role_disjoint.add((a, b))
381
+ self.texts.setdefault(("r", a, b), text)
382
+ elif p in (DOMAIN, RANGE):
383
+ r = self.role(s)
384
+ if r is None:
385
+ done = False
386
+ elif p == RANGE and self.is_data_range(o):
387
+ ok, dts = self.data_range(o)
388
+ # A datatype outside OWL 2 QL's map (xsd:double ...) is plain RDFS here: the RDFS core reads
389
+ # it, no QL check is made of it.
390
+ named = {str(o)} if isinstance(o, U) else {str(x) for x in self.items(
391
+ self.one(o, OWL + "intersectionOf"))}
392
+ done = ok and not named & _NOT_QL
393
+ if done and dts is not None:
394
+ self.data_ranges.append((r[0], dts, text))
395
+ else:
396
+ parts = self.super_parts(o)
397
+ done = parts is not None
398
+ if done:
399
+ self.include(("e", r if p == DOMAIN else inverse(r)), parts, text)
400
+ elif p == TYPE and str(o) in (OWL + "SymmetricProperty", OWL + "ReflexiveProperty",
401
+ OWL + "AsymmetricProperty"):
402
+ r = self.role(s)
403
+ done = r is not None
404
+ if done and str(o) == OWL + "SymmetricProperty":
405
+ self.role_sub.add((r, inverse(r)))
406
+ elif done and str(o) == OWL + "ReflexiveProperty":
407
+ self.reflexive.add(r)
408
+ elif done:
409
+ self.asymmetric.add(r[0])
410
+ self.texts.setdefault(("a", r[0]), text)
411
+ elif p == TYPE and str(o) in (OWL + "AllDisjointClasses", OWL + "AllDisjointProperties",
412
+ OWL + "AllDifferent"):
413
+ head = self.one(s, OWL + "members") if str(o) != OWL + "AllDifferent" else \
414
+ (self.one(s, OWL + "distinctMembers") or self.one(s, OWL + "members"))
415
+ members = self.items(head)
416
+ kind = str(o)[len(OWL):]
417
+ if kind == "AllDisjointClasses":
418
+ cs = [self.sub_concept(x) for x in members]
419
+ done = bool(cs) and all(c is not None for c in cs)
420
+ for i, a in enumerate(cs if done else ()):
421
+ for b in cs[i + 1:]:
422
+ self.disjoint.add((a, b))
423
+ self.texts.setdefault(("d", a, b), f"[ a owl:AllDisjointClasses ; owl:members ( "
424
+ f"{' '.join(self.short(m) for m in members)} ) ]")
425
+ elif kind == "AllDisjointProperties":
426
+ rs = [self.role(x) for x in members]
427
+ done = bool(rs) and all(r is not None for r in rs)
428
+ for i, a in enumerate(rs if done else ()):
429
+ for b in rs[i + 1:]:
430
+ self.role_disjoint.add((a, b))
431
+ self.texts.setdefault(("r", a, b), f"[ a owl:AllDisjointProperties ; owl:members ( "
432
+ f"{' '.join(self.short(m) for m in members)} ) ]")
433
+ else:
434
+ done = bool(members) and all(isinstance(x, U) for x in members)
435
+ for i, a in enumerate(members if done else ()):
436
+ for b in members[i + 1:]:
437
+ self.different.add((str(a), str(b)))
438
+ elif p == OWL + "differentFrom":
439
+ done = isinstance(s, U) and isinstance(o, U)
440
+ if done:
441
+ self.different.add((str(s), str(o)))
442
+ else:
443
+ continue
444
+ if done:
445
+ self.consumed.add((s, pred, o))
446
+ for a, p, b in extra:
447
+ if p in (SUB_CLASS, EQ_CLASS):
448
+ ca, cb = self.sub_concept(U(a)), self.sub_concept(U(b))
449
+ if ca is not None and cb is not None:
450
+ self.sub.add((ca, cb))
451
+ if p == EQ_CLASS:
452
+ self.sub.add((cb, ca))
453
+ elif p in (SUB_PROPERTY, EQ_PROPERTY):
454
+ self.role_sub.add(((a, False), (b, False)))
455
+ if p == EQ_PROPERTY:
456
+ self.role_sub.add(((b, False), (a, False)))
457
+ elif p in (DOMAIN, RANGE):
458
+ c = self.sub_concept(U(b))
459
+ if c is not None:
460
+ self.sub.add((("e", (a, p == RANGE)), c))
461
+
462
+
463
+ def _reach(edges: Dict[Any, Set[Any]], start: Any) -> Set[Any]:
464
+ seen, todo = {start}, [start]
465
+ while todo:
466
+ for n in edges.get(todo.pop(), ()):
467
+ if n not in seen:
468
+ seen.add(n)
469
+ todo.append(n)
470
+ return seen
471
+
472
+
473
+ def read(graph: Any, extra: Iterable[Tuple[str, str, str]] = ()) -> QLTBox:
474
+ """The QL axioms of an rdflib graph, closed (`QLTBox`); `extra` (subject, predicate, object) IRIs of
475
+ rdfs:subClassOf, rdfs:subPropertyOf, rdfs:domain, rdfs:range and the equivalences the RDFS core adds."""
476
+ r = _Reader(graph)
477
+ r.run(extra)
478
+ # Roles: closed under inverses (R ⊑ S gives R⁻ ⊑ S⁻).
479
+ role_edges: Dict[Role, Set[Role]] = {}
480
+ props = {x[0] for e in r.role_sub for x in e} | {x[0] for e in r.role_disjoint for x in e} | \
481
+ {x[0] for x in r.reflexive} | set(r.asymmetric)
482
+ for a, b in r.role_sub:
483
+ role_edges.setdefault(a, set()).add(b)
484
+ role_edges.setdefault(inverse(a), set()).add(inverse(b))
485
+ roles = {(p, i) for p in props for i in (False, True)}
486
+ for x in [c for e in r.sub for c in e] + [c for e in r.disjoint for c in e]:
487
+ if x[0] == "e":
488
+ roles |= {x[1], inverse(x[1])}
489
+ elif x[0] == "d":
490
+ roles.add((x[1], False))
491
+ for _, role, _ in r.qualified:
492
+ roles |= {role, inverse(role)}
493
+ role_up = {x: _reach(role_edges, x) for x in roles}
494
+ # Concepts.
495
+ edges: Dict[Concept, Set[Concept]] = {}
496
+ for a, b in r.sub:
497
+ edges.setdefault(a, set()).add(b)
498
+ for x, ups in role_up.items():
499
+ for y in ups:
500
+ if y != x:
501
+ edges.setdefault(("e", x), set()).add(("e", y))
502
+ for a, b in list(r.sub) + list(r.disjoint):
503
+ for c in (a, b):
504
+ if c[0] == "d":
505
+ edges.setdefault(c, set()).add(("e", (c[1], False)))
506
+ for up in role_up.get((c[1], False), ()):
507
+ if not up[1] and up[0] != c[1]:
508
+ edges.setdefault(c, set()).add(("d", up[0], c[2]))
509
+ reflexive_roles = {y for x in r.reflexive for z in (x, inverse(x)) for y in role_up.get(z, {z})}
510
+ for x in reflexive_roles:
511
+ edges.setdefault(TOP, set()).add(("e", x))
512
+ nodes = set(edges) | {c for v in edges.values() for c in v} | {c for e in r.disjoint for c in e} | {TOP}
513
+ nodes |= {("e", x) for x in roles}
514
+ up = {n: _reach(edges, n) for n in nodes}
515
+
516
+ def named(cs: Iterable[Concept]) -> FrozenSet[str]:
517
+ return frozenset(c[1] for c in cs if c[0] == "c")
518
+
519
+ superclasses = {n[1]: named(up[n]) for n in nodes if n[0] == "c"}
520
+ domains = {x[0]: named(up[("e", x)]) for x in roles if not x[1] and named(up[("e", x)])}
521
+ ranges = {x[0]: named(up[("e", x)]) for x in roles if x[1] and named(up[("e", x)])}
522
+ superproperties = {x[0]: frozenset(y[0] for y in ups if not y[1]) for x, ups in role_up.items() if not x[1]}
523
+ # `?s R ?o` reads `?o p ?s` for each p ⊑ R⁻, but only the greatest such p (the saturated maps of a
524
+ # super-property give a sub-property's triples already): one of each equivalence class.
525
+ inverse_subs: Dict[str, FrozenSet[str]] = {}
526
+ for x, ups in role_up.items():
527
+ if x[1]:
528
+ continue
529
+ for y in ups:
530
+ if y[1]:
531
+ inverse_subs.setdefault(y[0], set()).add(x[0]) # x[0] ⊑ y[0]⁻
532
+ for target, subs in list(inverse_subs.items()):
533
+ keep = set()
534
+ for p in sorted(subs):
535
+ above = {q for q in subs if q != p and q in superproperties.get(p, ())}
536
+ if any(p not in superproperties.get(q, ()) for q in above):
537
+ continue # a strictly greater one is kept
538
+ if any(q < p and p in superproperties.get(q, ()) for q in above):
539
+ continue # an equivalent one with a smaller name is kept
540
+ keep.add(p)
541
+ inverse_subs[target] = frozenset(keep)
542
+ everything = named(up[TOP])
543
+ reflexive = frozenset(x[0] for x in reflexive_roles)
544
+ data_typed = tuple(sorted((n[1], n[2], named(up[n]) - domains.get(n[1], frozenset()))
545
+ for n in nodes if n[0] == "d" and named(up[n]) - domains.get(n[1], frozenset())))
546
+ unsat = _unsatisfiable(r, up, role_up, reflexive_roles)
547
+ abox, individuals = _abox(graph, r, up, role_up, everything)
548
+ from rdflib import URIRef
549
+ for a, b in r.different: # owl:differentFrom is symmetric, owl:AllDifferent its pairs
550
+ abox |= {(URIRef(a), URIRef(OWL + "differentFrom"), URIRef(b)),
551
+ (URIRef(b), URIRef(OWL + "differentFrom"), URIRef(a))}
552
+ different = difference(r, up, graph)
553
+ # Each concept's implied successors: ∃R in its closure (filler ⊤, typed by R's range) and each qualified
554
+ # B ⊑ ∃R.F of a concept in its closure (filler F's closure, and the range's).
555
+ generates: Dict[Concept, List[Tuple[Role, FrozenSet[str], FrozenSet[Concept]]]] = {}
556
+ for n in nodes:
557
+ found = []
558
+ for c in up[n]:
559
+ if c[0] == "e":
560
+ fill = frozenset(up.get(("e", inverse(c[1])), {("e", inverse(c[1]))}))
561
+ found.append((c[1], named(fill), fill))
562
+ for b, role, f in r.qualified:
563
+ if b in up[n]:
564
+ fill = frozenset(up.get(f, {f})) | frozenset(up.get(("e", inverse(role)), ()))
565
+ found.append((role, named(fill), fill))
566
+ if found:
567
+ generates[n] = found
568
+ return QLTBox(superclasses, superproperties, domains, ranges, inverse_subs, everything, reflexive, data_typed,
569
+ frozenset(individuals), frozenset(abox), tuple(_checks(r, unsat)), frozenset(unsat),
570
+ frozenset(r.consumed), different,
571
+ generates={k: tuple(v) for k, v in generates.items()},
572
+ role_up={k: frozenset(v) for k, v in role_up.items()})
573
+
574
+
575
+ def _unsatisfiable(r: _Reader, up: Dict[Concept, Set[Concept]], role_up: Dict[Role, Set[Role]],
576
+ reflexive_roles: Set[Role]) -> Set[Any]:
577
+ """The basic concepts (and roles, as ("r", R)) the TBox alone makes empty, to a fixpoint: one under owl:Nothing,
578
+ under two disjoint concepts, under an empty one, or `∃R.F` where R is empty or `∃R⁻ ⊓ F` is; a role under two
579
+ disjoint roles, or under an asymmetric property both ways; owl:Thing where a reflexive role is empty."""
580
+ disjoint = {frozenset(e) if e[0] != e[1] else frozenset({e[0]}) for e in r.disjoint}
581
+ role_disjoint = {frozenset(e) for e in r.role_disjoint} | {frozenset((inverse(a), inverse(b)))
582
+ for a, b in r.role_disjoint}
583
+
584
+ def clash(cs: Set[Concept]) -> bool:
585
+ return BOT in cs or any(d <= cs for d in disjoint)
586
+
587
+ out: Set[Any] = set()
588
+ for x, ups in role_up.items():
589
+ if any(d <= ups for d in role_disjoint) or any((p, False) in ups and (p, True) in ups
590
+ for p in r.asymmetric):
591
+ out |= {("r", x), ("r", inverse(x))}
592
+ changed = True
593
+ while changed:
594
+ changed = False
595
+ for n, ups in up.items():
596
+ if n in out:
597
+ continue
598
+ bad = clash(ups) or any(u in out for u in ups if u != n) or \
599
+ any(u[0] == "e" and ("r", u[1]) in out for u in ups)
600
+ if not bad:
601
+ for b, role, f in r.qualified:
602
+ if b in ups and (("r", role) in out or f in out or
603
+ clash(up.get(("e", inverse(role)), {("e", inverse(role))}) | up.get(f, {f}))):
604
+ bad = True
605
+ break
606
+ if not bad and n == TOP:
607
+ bad = any(("r", x) in out for x in reflexive_roles) or \
608
+ any((p, False) in reflexive_roles for p in r.asymmetric)
609
+ if bad:
610
+ out.add(n)
611
+ changed = True
612
+ if n[0] == "e":
613
+ out |= {("r", n[1]), ("r", inverse(n[1])), ("e", inverse(n[1]))}
614
+ for x in list(role_up):
615
+ if ("r", x) not in out and (("e", x) in out or ("e", inverse(x)) in out):
616
+ out |= {("r", x), ("r", inverse(x))}
617
+ changed = True
618
+ return out
619
+
620
+
621
+ def _pattern(c: Concept, x: str, n: List[int]) -> str:
622
+ """A SPARQL group pattern for "?x is a member of basic concept c"."""
623
+ n[0] += 1
624
+ if c[0] == "c":
625
+ return f"?{x} a <{c[1]}> ."
626
+ if c == TOP:
627
+ return f"?{x} a <{THING}> ."
628
+ if c[0] == "e":
629
+ p, inv = c[1]
630
+ return f"?qlnd_v{n[0]} <{p}> ?{x} ." if inv else f"?{x} <{p}> ?qlnd_v{n[0]} ."
631
+ return f"?{x} <{c[1]}> ?qlnd_v{n[0]} . FILTER({in_range(f'qlnd_v{n[0]}', c[2])})"
632
+
633
+
634
+ _NUMBERS = frozenset((XSD + "decimal",) + _INTEGERS)
635
+ # A decimal's lexical form of a whole value (XML Schema 1.1 Part 2 3.3.3); as a
636
+ # SPARQL string, its backslashes doubled.
637
+ _WHOLE = r"^[+-]?([0-9]+(\\.0*)?|\\.0+)$"
638
+
639
+
640
+ def in_range(v: str, datatypes: FrozenSet[str]) -> str:
641
+ """A SPARQL condition: `?v` is a value of the data range of `datatypes` (`datatypes_of`), by value where the
642
+ numeric value spaces nest (OWL 2 Structural Specification 4: xsd:integer's values are xsd:decimal's whole ones,
643
+ so "1.0"^^xsd:decimal is an xsd:integer, "5"^^xsd:integer an xsd:int and an xsd:nonNegativeInteger); an integer
644
+ type's range by a whole number of the decimals' value space within its bounds (XML Schema 1.1 Part 2 §3.4,
645
+ `xsd.integer_range`: 2147483647 is an xsd:int, 2147483648 is not); by its datatype otherwise. xsd:float and
646
+ xsd:double are value spaces of their own. A literal's lexical form is not checked (an ill-typed literal is not
647
+ told apart)."""
648
+ others = sorted(datatypes - _NUMBERS)
649
+ parts = [f"DATATYPE(?{v}) IN ({', '.join(f'<{d}>' for d in others)})"] if others else []
650
+ numbers = f"DATATYPE(?{v}) IN ({', '.join(f'<{d}>' for d in sorted(_NUMBERS))})"
651
+ if XSD + "decimal" in datatypes:
652
+ parts.append(numbers)
653
+ elif datatypes & _NUMBERS: # integer types only: a whole value (read from the lexical form) within bounds
654
+ bounds = [xsd.integer_range(t) for t in datatypes & _NUMBERS]
655
+ low = None if any(b[0] is None for b in bounds) else min(b[0] for b in bounds)
656
+ high = None if any(b[1] is None for b in bounds) else max(b[1] for b in bounds)
657
+ test = [numbers, f'REGEX(STR(?{v}), "{_WHOLE}")']
658
+ # Compared as an xsd:decimal of the lexical form: an integer past 64 bits keeps its value there.
659
+ value = f"<{XSD}decimal>(STR(?{v}))"
660
+ test += [f"{value} >= {low}"] if low is not None else []
661
+ test += [f"{value} <= {high}"] if high is not None else []
662
+ parts.append("(" + " && ".join(test) + ")")
663
+ return " || ".join(parts) if parts else "false"
664
+
665
+
666
+ def _holds_value(dt: str, datatypes: FrozenSet[str]) -> bool:
667
+ """Whether every value of datatype `dt` is in the data range of `datatypes` (`in_range`, known statically)."""
668
+ if dt in datatypes or (XSD + "decimal" in datatypes and dt in _NUMBERS):
669
+ return True
670
+ if dt in _NUMBERS and dt != XSD + "decimal" and datatypes & _NUMBERS and XSD + "decimal" not in datatypes:
671
+ bounds = [xsd.integer_range(t) for t in datatypes & _NUMBERS]
672
+ low = None if any(b[0] is None for b in bounds) else min(b[0] for b in bounds)
673
+ high = None if any(b[1] is None for b in bounds) else max(b[1] for b in bounds)
674
+ lo, hi = xsd.integer_range(dt)
675
+ return (low is None or (lo is not None and lo >= low)) and (high is None or (hi is not None and hi <= high))
676
+ return False
677
+
678
+
679
+ def _giver_datatype(giver: Any, held: Any) -> Optional[str]:
680
+ """The one datatype every literal a giver's object map makes has, known from the mapping and the column's
681
+ type (an R2RML text column declared a number is valid or a data error, R2RML 10.3); None where it may vary
682
+ or is not a literal (an IRI, a language tag, a constant)."""
683
+ tm = giver.predicate_object.object
684
+ if getattr(tm, "term_type", None) != "literal" or tm.language or tm.language_column or tm.constant is not None:
685
+ return None
686
+ if tm.template is not None:
687
+ return tm.datatype or xsd.STRING
688
+ if tm.column is None:
689
+ return None
690
+ try:
691
+ return tm.datatype or held(giver.map.source.table, tm.column)[0]
692
+ except Exception: # noqa: BLE001 - a column whose type is not recorded: not known
693
+ return None
694
+
695
+
696
+ def range_holds(maps: Any, held: Any, predicate: str, datatypes: FrozenSet[str]) -> bool:
697
+ """Whether a data range's check cannot fail over the mapped data: every map that gives `predicate` (its
698
+ sub-properties' too, the maps saturated) makes literals of one datatype whose every value is in the range
699
+ (an INTEGER column under a range xsd:integer or xsd:decimal, a text column under xsd:string). The ontology's
700
+ own assertions of the property are the caller's to rule out. A range no map gives holds."""
701
+ givers = maps.by_predicate.get(predicate, ())
702
+ return all((dt := _giver_datatype(g, held)) is not None and _holds_value(dt, datatypes) for g in givers)
703
+
704
+
705
+ def range_hint(maps: Any, held: Any, predicate: str, datatypes: FrozenSet[str]) -> str:
706
+ """The maps whose values fall outside a data range by their datatype, named with the way out: a float or
707
+ double column under a range of the decimals (OWL 2 keeps the value spaces apart), or another datatype."""
708
+ named = []
709
+ for g in maps.by_predicate.get(predicate, ()):
710
+ dt = _giver_datatype(g, held)
711
+ if dt is None or _holds_value(dt, datatypes):
712
+ continue
713
+ tm = g.predicate_object.object
714
+ where = f"column {tm.column} of {g.map.source.table}" if tm.column else f"the object map of {g.map.id}"
715
+ named.append(f"{where} gives {dt.replace(XSD, 'xsd:')} values")
716
+ if not named:
717
+ return ""
718
+ if any("xsd:double" in n or "xsd:float" in n for n in named) and datatypes & _NUMBERS:
719
+ return (f"{'; '.join(named)}: a float or double is outside the decimals' value space (OWL 2's datatype map "
720
+ f"keeps them apart). CAST the column AS DECIMAL in an rr:sqlQuery (an rr:datatype xsd:decimal on a "
721
+ f"DOUBLE column is itself a data error, R2RML 10.3), or build the engine with inconsistency='report'")
722
+ return (f"{'; '.join(named)}, outside the range: declare the range's datatype on the column (rr:datatype), "
723
+ f"cast it in an rr:sqlQuery, or build the engine with inconsistency='report'")
724
+
725
+
726
+ def _role_pattern(role: Role, a: str, b: str) -> str:
727
+ return f"?{b} <{role[0]}> ?{a} ." if role[1] else f"?{a} <{role[0]}> ?{b} ."
728
+
729
+
730
+ def _words(c: Any) -> str:
731
+ if c[0] == "c":
732
+ return f"<{c[1]}>"
733
+ if c == TOP:
734
+ return "owl:Thing"
735
+ if c == BOT:
736
+ return "owl:Nothing"
737
+ if c[0] == "r":
738
+ return f"the property {'inverse of ' if c[1][1] else ''}<{c[1][0]}>"
739
+ if c[0] == "e":
740
+ return f"[ owl:onProperty {'[ owl:inverseOf ' if c[1][1] else ''}<{c[1][0]}>{' ]' if c[1][1] else ''} ; " \
741
+ f"owl:someValuesFrom owl:Thing ]"
742
+ return f"[ owl:onProperty <{c[1]}> ; owl:someValuesFrom ( {' '.join(sorted(c[2]))} ) ]"
743
+
744
+
745
+ def _checks(r: _Reader, unsat: Set[Any]) -> List[Check]:
746
+ out: List[Check] = []
747
+ if TOP in unsat:
748
+ out.append(Check("an empty owl:Thing", "owl:Thing is unsatisfiable", None))
749
+ out += [Check("an assertion of a bottom property", text, None) for text in sorted(r.bottom)]
750
+ for a, b in sorted(r.disjoint):
751
+ if BOT in (a, b):
752
+ continue # the other one is empty: an unsatisfiable class, below
753
+ n = [0]
754
+ out.append(Check("disjoint classes", r.texts.get(("d", a, b), f"{_words(a)} owl:disjointWith {_words(b)}"),
755
+ f"SELECT DISTINCT ?x WHERE {{ {_pattern(a, 'x', n)} {_pattern(b, 'x', n)} }}",
756
+ f"?x a {_words(a)} , {_words(b)}"))
757
+ for a, b in sorted(r.role_disjoint):
758
+ out.append(Check("disjoint properties", r.texts.get(("r", a, b), ""),
759
+ f"SELECT DISTINCT ?x ?y WHERE {{ {_role_pattern(a, 'x', 'y')} {_role_pattern(b, 'x', 'y')} }}",
760
+ f"{_role_pattern(a, 'x', 'y')} {_role_pattern(b, 'x', 'y')}"))
761
+ for p in sorted(r.asymmetric):
762
+ out.append(Check("an asymmetric property", r.texts.get(("a", p), f"<{p}> a owl:AsymmetricProperty"),
763
+ f"SELECT DISTINCT ?x ?y WHERE {{ ?x <{p}> ?y . ?y <{p}> ?x . }}", f"?x <{p}> ?y . ?y <{p}> ?x"))
764
+ for a, b in sorted(r.different):
765
+ if a == b:
766
+ out.append(Check("different individuals", f"<{a}> owl:differentFrom <{b}>", None))
767
+ for p, dts, text in r.data_ranges:
768
+ out.append(Check("a data range", text,
769
+ f"SELECT DISTINCT ?x ?v WHERE {{ ?x <{p}> ?v . FILTER(!isLITERAL(?v) || "
770
+ f"!({in_range('v', dts)})) }}", f"?x <{p}> ?v", (p, dts)))
771
+ for c in sorted((c for c in unsat if c[0] in ("c", "e", "d")), key=repr):
772
+ n = [0]
773
+ out.append(Check("an unsatisfiable class", f"{_words(c)} is unsatisfiable",
774
+ f"SELECT DISTINCT ?x WHERE {{ {_pattern(c, 'x', n)} }}", f"?x a {_words(c)}"))
775
+ return out
776
+
777
+
778
+ # The types that make a term a schema term (the ontology itself, a class, a property, a datatype), and the
779
+ # annotation vocabularies whose properties never assert anything of an individual.
780
+ _SCHEMA_TYPES = {OWL + k for k in ("Ontology", "Class", "ObjectProperty", "DatatypeProperty", "AnnotationProperty",
781
+ "OntologyProperty", "Restriction", "DeprecatedClass", "DeprecatedProperty",
782
+ "TransitiveProperty", "SymmetricProperty", "AsymmetricProperty",
783
+ "ReflexiveProperty", "IrreflexiveProperty", "FunctionalProperty",
784
+ "InverseFunctionalProperty", "DataRange")} | \
785
+ {RDFS + "Class", RDFS + "Datatype", RDF + "Property"}
786
+ _PROPERTY_TYPES = {OWL + k for k in ("ObjectProperty", "DatatypeProperty", "TransitiveProperty", "SymmetricProperty",
787
+ "AsymmetricProperty", "ReflexiveProperty", "IrreflexiveProperty",
788
+ "FunctionalProperty", "InverseFunctionalProperty")} | {RDF + "Property"}
789
+ # The annotation properties: the built-in ones (OWL 2 Structural Specification 5.5), those an ontology declares
790
+ # owl:AnnotationProperty, and the documentation properties of Dublin Core and SKOS (labels and notes: annotations by
791
+ # their vocabularies' own use). SKOS's semantic relations (skos:broader, skos:related ...) are not: they relate
792
+ # individuals.
793
+ _SKOS, _DC = "http://www.w3.org/2004/02/skos/core#", ("http://purl.org/dc/terms/", "http://purl.org/dc/elements/1.1/")
794
+ _ANNOTATIONS = {RDFS + k for k in ("label", "comment", "seeAlso", "isDefinedBy")} | \
795
+ {OWL + k for k in ("versionInfo", "priorVersion", "backwardCompatibleWith", "incompatibleWith", "deprecated")} | \
796
+ {_SKOS + k for k in ("prefLabel", "altLabel", "hiddenLabel", "notation", "note", "definition", "scopeNote",
797
+ "example", "editorialNote", "changeNote", "historyNote")}
798
+ # The axiom predicates whose subject and object are classes, properties or datatypes (never an individual): a term
799
+ # is a schema term by its rdf:type (`_SCHEMA_TYPES`) or by its place in one of these, never by an annotation.
800
+ _TBOX = {RDFS + k for k in ("subClassOf", "subPropertyOf", "domain", "range")} | \
801
+ {OWL + k for k in ("equivalentClass", "equivalentProperty", "disjointWith", "propertyDisjointWith", "inverseOf",
802
+ "onProperty", "someValuesFrom", "allValuesFrom", "complementOf", "onClass", "onDataRange",
803
+ "propertyChainAxiom", "hasKey", "disjointUnionOf", "onDatatype")}
804
+
805
+
806
+ def _abox(graph: Any, r: _Reader, up: Dict[Concept, Set[Concept]], role_up: Dict[Role, Set[Role]],
807
+ everything: FrozenSet[str]) -> Tuple[Set[Tuple[Any, Any, Any]], Set[str]]:
808
+ """The ontology's own assertions closed under the TBox (types and property assertions about its named
809
+ individuals; a reflexive property's and owl:Thing's are the front end's, over every individual), and its named
810
+ individuals."""
811
+ from rdflib import BNode, Literal, URIRef
812
+ T = URIRef(TYPE)
813
+ out: Set[Tuple[Any, Any, Any]] = set()
814
+ individuals: Set[str] = set()
815
+
816
+ def typed(x: Any, c: Concept) -> None:
817
+ for u in up.get(c, {c}):
818
+ if u[0] == "c":
819
+ out.add((x, T, URIRef(u[1])))
820
+
821
+ # An annotation is no assertion, and an ontology, class, property or datatype is no individual: their triples
822
+ # (`ex:C skos:prefLabel "C"`, `<onto> dct:creator "me"`) name no individual.
823
+ annotations = {str(x) for x in graph.subjects(T, URIRef(OWL + "AnnotationProperty"))} | _ANNOTATIONS
824
+ schema = {str(x) for x in graph.subjects(T, None)
825
+ if any(str(t) in _SCHEMA_TYPES for t in graph.objects(x, T))}
826
+ for x, q, y in graph:
827
+ if str(q) in _TBOX:
828
+ schema |= {str(z) for z in (x, y) if isinstance(z, URIRef)}
829
+ for a, b in r.different: # named in owl:differentFrom or owl:AllDifferent: an individual
830
+ individuals |= {a, b}
831
+ ontologies = {str(x) for x in graph.subjects(T, URIRef(OWL + "Ontology"))}
832
+ # The properties the ontology declares or names in an axiom: only those assert something of a schema term.
833
+ properties = {str(x) for x in graph.subjects(T, None)
834
+ if any(str(t) in _PROPERTY_TYPES for t in graph.objects(x, T))}
835
+ for x, q, y in graph:
836
+ if str(q) in (SUB_PROPERTY, EQ_PROPERTY, OWL + "inverseOf", OWL + "propertyDisjointWith"):
837
+ properties |= {str(z) for z in (x, y) if isinstance(z, URIRef)}
838
+ elif str(q) in (DOMAIN, RANGE) and isinstance(x, URIRef):
839
+ properties.add(str(x))
840
+ elif str(q) == OWL + "onProperty" and isinstance(y, URIRef):
841
+ properties.add(str(y))
842
+ for s, pred, o in graph:
843
+ if not isinstance(s, URIRef) or str(s) in ontologies:
844
+ continue
845
+ p = str(pred)
846
+ if p in annotations or p.startswith(_DC):
847
+ continue
848
+ if str(s) in schema and (p == TYPE or p.startswith(_VOCABULARY) or p not in properties):
849
+ # A class or property is an individual only where an assertion by a property the ontology declares (or
850
+ # an axiom names) is about it (punning); else the triple annotates it (skos:exactMatch, ex:status ...).
851
+ continue
852
+ if p == TYPE:
853
+ t = str(o)
854
+ if isinstance(o, URIRef) and (not t.startswith(_VOCABULARY) or t in (THING, OWL + "NamedIndividual")):
855
+ individuals.add(str(s))
856
+ if not t.startswith(_VOCABULARY):
857
+ typed(s, ("c", t))
858
+ continue
859
+ if p.startswith(_VOCABULARY) or isinstance(o, BNode):
860
+ if p == OWL + "differentFrom" and isinstance(o, URIRef):
861
+ individuals |= {str(s), str(o)}
862
+ continue
863
+ individuals.add(str(s))
864
+ if isinstance(o, URIRef):
865
+ if str(o) not in schema:
866
+ individuals.add(str(o))
867
+ for role in role_up.get((p, False), {(p, False)}):
868
+ if not role[1]:
869
+ out.add((s, URIRef(role[0]), o))
870
+ elif not isinstance(o, Literal):
871
+ out.add((o, URIRef(role[0]), s))
872
+ typed(s, ("e", (p, False)))
873
+ if not isinstance(o, Literal):
874
+ typed(o, ("e", (p, True)))
875
+ for i in individuals:
876
+ for c in everything:
877
+ out.add((URIRef(i), T, URIRef(c)))
878
+ return out, individuals
879
+
880
+
881
+ # A stated owl:differentFrom of an individual with itself (eq-diff1's a ≠ a): read from the stated triples, since the
882
+ # rewriting of owl:differentFrom never gives a ≠ a (`Translator.different_branch` reads a pattern whose two sides are
883
+ # one term as stated).
884
+ SELF_DIFFERENT = Check("different individuals", "?x owl:differentFrom ?x: an individual different from itself",
885
+ f"SELECT DISTINCT ?x WHERE {{ ?x <{OWL}differentFrom> ?x . }}", f"?x <{OWL}differentFrom> ?x")
886
+
887
+
888
+ def violations(engine: Any, limit: Optional[int] = None) -> List[Violation]:
889
+ """The engine's ontology's consistency checks under profile 'ql' (`QLTBox.checks`, and `SELF_DIFFERENT` where the
890
+ data may state owl:differentFrom), each with the solutions that violate it (with `limit`, the first `limit` of
891
+ them and a count of all); only the violated ones. Nothing raises: what to do with them is the caller's."""
892
+ from prograph.plan.ir.dataset import maps_seen
893
+ from prograph.plan.ir.entry import ontology_of
894
+ onto = ontology_of(engine)
895
+ tbox = getattr(onto, "ql", None)
896
+ if tbox is None:
897
+ return []
898
+ from prograph.plan.ir.entry import _held
899
+ from prograph.r2rml.compile import R2RMLDataError
900
+ maps = maps_seen(engine, onto)
901
+ held = _held(engine._schema, None, True)
902
+ stated = {str(t[1]) for t in tbox.abox}
903
+ checks = [c for c in tbox.checks
904
+ if c.range is None or c.range[0] in stated or not range_holds(maps, held, *c.range)]
905
+ if OWL + "differentFrom" in maps.by_predicate:
906
+ checks.append(SELF_DIFFERENT)
907
+ out: List[Violation] = []
908
+ for check in checks:
909
+ if check.query is None:
910
+ out.append(Violation(check))
911
+ continue
912
+ query = check.query if limit is None else f"{check.query} LIMIT {int(limit)}"
913
+ try:
914
+ rows = engine.execute(query, language="sparql", return_type="list")
915
+ except R2RMLDataError as exc:
916
+ out.append(Violation(check, error=str(exc).splitlines()[0]))
917
+ continue
918
+ if not rows:
919
+ continue
920
+ count = len(rows)
921
+ if limit is not None and count >= limit:
922
+ got = engine.execute(f"SELECT (COUNT(*) AS ?pgc_n) WHERE {{ {check.query} }}", language="sparql",
923
+ return_type="list")
924
+ count = int(got[0]["pgc_n"])
925
+ out.append(Violation(check, tuple(rows), count))
926
+ return out
927
+
928
+
929
+ DIFFERENT = OWL + "differentFrom"
930
+ A_, B_ = "qlnd_a", "qlnd_b"
931
+
932
+
933
+ def difference(r: _Reader, up: Dict[Concept, Set[Concept]], graph: Any) -> str:
934
+ """The group pattern of the pairs (?qlnd_a, ?qlnd_b) of named individuals OWL 2 Direct semantics makes
935
+ different under the QL axioms: a ≠ b is entailed exactly where the ontology with a = b is inconsistent (the
936
+ ontology itself consistent), which in OWL 2 QL (no equality, no functional property) is one of:
937
+
938
+ - a stated owl:differentFrom between them, either way, or an owl:AllDifferent naming both;
939
+ - a member of B1 and b of B2 with B1 and B2 disjoint (owl:disjointWith, owl:AllDisjointClasses,
940
+ owl:complementOf), B1 and B2 basic concepts, each with everything under it (a subclass, `∃p` under a domain,
941
+ `C ⊑ ∃p` under `∃p`);
942
+ - facts S1(a, y1) and S2(b, y2), S1 and S2 disjoint properties (or their inverses), that a = b makes one pair:
943
+ y1 = y2, or y1 and y2 each a or b. A property disjoint with a property gives different pairs, so two
944
+ individuals only where the rest of the pair is shared; an asymmetric property is disjoint with its inverse
945
+ (R(a, y) and R(y, b), or R(a, b) alone). A data property's value is the shared y;
946
+ - never a and a, and only IRIs (named individuals).
947
+ Every pattern is matched under the same rewriting (`?a a C` reads C's subclasses, sub-properties and so on)."""
948
+ from rdflib import URIRef
949
+ data = {str(s) for s in graph.subjects(URIRef(TYPE), URIRef(OWL + "DatatypeProperty"))}
950
+ down: Dict[Concept, Set[Concept]] = {}
951
+ for n, ups in up.items():
952
+ for u in ups:
953
+ down.setdefault(u, set()).add(n)
954
+ n = [0]
955
+
956
+ def members(c: Concept, x: str) -> str:
957
+ if c[0] == "c" or c == TOP:
958
+ return _pattern(c, x, n) # rewritten: subclasses, domains, ranges, ⊤ ⊑ C
959
+ under = [c] + sorted((d for d in down.get(c, ()) if d != c and (d[0] in ("c", "e", "d") or d == TOP)),
960
+ key=repr)
961
+ return " UNION ".join("{ " + _pattern(d, x, n) + " }" for d in under)
962
+
963
+ def side(a: str, b: str) -> List[str]:
964
+ out = [f"{{ ?{a} <{DIFFERENT}> ?{b} . }}"]
965
+ for b1, b2 in sorted(r.disjoint, key=repr):
966
+ if BOT in (b1, b2) or b1 == b2:
967
+ continue # an empty class: no individual of it
968
+ out.append(f"{{ {{ {members(b1, a)} }} {{ {members(b2, b)} }} }}")
969
+ pairs: Set[Tuple[Role, Role]] = set()
970
+ for s1, s2 in r.role_disjoint:
971
+ pairs.add((s1, s2))
972
+ if s1[0] not in data and s2[0] not in data:
973
+ pairs.add((inverse(s1), inverse(s2)))
974
+ for p in r.asymmetric:
975
+ pairs |= {((p, False), (p, True)), ((p, True), (p, False))}
976
+ y = "qlnd_y"
977
+ for s1, s2 in sorted(pairs):
978
+ out.append(f"{{ {_role_pattern(s1, a, y)} {_role_pattern(s2, b, y)} }}")
979
+ for y1, y2 in ((a, a), (a, b), (b, a), (b, b)):
980
+ if y1 == y2:
981
+ continue # y1 = y2 is the shared y above
982
+ out.append(f"{{ {_role_pattern(s1, a, y1)} {_role_pattern(s2, b, y2)} }}")
983
+ return out
984
+
985
+ branches = side(A_, B_) + side(B_, A_)
986
+ return "{ " + " UNION ".join(dict.fromkeys(branches)) + f" }} FILTER(isIRI(?{A_}) && isIRI(?{B_}) && " \
987
+ f"!sameTerm(?{A_}, ?{B_}))"