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/plan/ir/rdfs.py
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
"""The RDFS core: an ontology's own triples closed under RDFS entailment's rules (RDF 1.1 Semantics 8 and 9).
|
|
2
|
+
|
|
3
|
+
With an ontology the default graph holds its triples (`dataset.ontology_triples`), closed here, read-only:
|
|
4
|
+
|
|
5
|
+
- rdf1 (a predicate is an `rdf:Property`), rdfs1 (a recognized datatype is an `rdfs:Datatype`: rdf:langString and
|
|
6
|
+
xsd:string, each where the dataset names it or it is RDF's own vocabulary), rdfs2 and rdfs3 (domain, range:
|
|
7
|
+
an IRI or blank-node object only, as a literal is never a subject), rdfs4a and rdfs4b (every subject, and every
|
|
8
|
+
IRI and blank-node object, an `rdfs:Resource`), rdfs5 and rdfs11 (transitive subPropertyOf and subClassOf),
|
|
9
|
+
rdfs6 and rdfs10 (each property and class its own sub-property and subclass), rdfs7 and rdfs9, rdfs8 (a class
|
|
10
|
+
a subclass of `rdfs:Resource`), rdfs12 (a container membership property a sub-property of `rdfs:member`) and
|
|
11
|
+
rdfs13 (a datatype a subclass of `rdfs:Literal`).
|
|
12
|
+
- The RDF and RDFS axiomatic triples (8.1, 9.1), within SPARQL 1.1 Entailment Regimes' condition C2: the
|
|
13
|
+
`rdf:_n` ones only for an `rdf:_n` the dataset names, so the closure, and every answer, is finite.
|
|
14
|
+
- owl:equivalentClass and owl:equivalentProperty as subClassOf and subPropertyOf both ways (OWL 2 RL's scm-eqc1
|
|
15
|
+
and scm-eqp1), as the maps apply them (`saturate`), and under profile="ql" back (scm-eqc2, scm-eqp2); under
|
|
16
|
+
profile="rl" all of OWL 2 RL's schema rules
|
|
17
|
+
(Profiles 4.3, table 9: scm-cls, scm-eqc2, scm-op, scm-dp, scm-eqp2, scm-dom1/2, scm-rng1/2, scm-hv, scm-svf1/2,
|
|
18
|
+
scm-avf1/2, scm-int, scm-uni).
|
|
19
|
+
|
|
20
|
+
Triples with a literal subject (generalized RDF) are never made. The mapped data's vocabulary joins through
|
|
21
|
+
`extend`: a class or predicate the maps give is a class or property only where it has a triple, so the triples it
|
|
22
|
+
adds are kept apart, by the term whose triples witness them (`dataset.ontology_triples`)."""
|
|
23
|
+
|
|
24
|
+
from __future__ import annotations
|
|
25
|
+
|
|
26
|
+
from typing import Dict, FrozenSet, Iterable, List, Optional, Set, Tuple
|
|
27
|
+
|
|
28
|
+
from prograph.rules import module_home
|
|
29
|
+
from prograph.schema import xsd
|
|
30
|
+
|
|
31
|
+
module_home("rdfs-core", "The ontology's triples closed under RDFS's rules and its axiomatic triples (C2)",
|
|
32
|
+
terms=("RDFS core", "RDFS entailment", "rdfs1", "rdfs4a", "rdfs4b", "rdfs5", "rdfs6", "rdfs7",
|
|
33
|
+
"rdfs8", "rdfs9", "rdfs10", "rdfs11", "rdfs12", "rdfs13", "rdf1", "axiomatic triples",
|
|
34
|
+
"condition C2", "rdfs:Resource", "rdfs:Class", "rdf:Property", "ontology closure",
|
|
35
|
+
"schema rules", "scm-eqc2", "scm-cls"))
|
|
36
|
+
module_home("typed-variables", "A query variable typed owl:Class, owl:ObjectProperty, owl:DatatypeProperty or "
|
|
37
|
+
"owl:NamedIndividual under profile ql: the declared and the built-in entities of its kind",
|
|
38
|
+
terms=("typed variable", "variable typing", "owl:Class", "owl:ObjectProperty", "owl:DatatypeProperty",
|
|
39
|
+
"owl:NamedIndividual", "built-in entity", "owl:Thing", "owl:Nothing", "topObjectProperty",
|
|
40
|
+
"Direct Semantics regime", "table 5"))
|
|
41
|
+
|
|
42
|
+
RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
|
43
|
+
RDFS = "http://www.w3.org/2000/01/rdf-schema#"
|
|
44
|
+
OWL = "http://www.w3.org/2002/07/owl#"
|
|
45
|
+
XSD_STRING = xsd.STRING
|
|
46
|
+
TYPE, PROPERTY = RDF + "type", RDF + "Property"
|
|
47
|
+
RESOURCE, CLASS, LITERAL, DATATYPE = RDFS + "Resource", RDFS + "Class", RDFS + "Literal", RDFS + "Datatype"
|
|
48
|
+
SUB_CLASS, SUB_PROPERTY, DOMAIN, RANGE = RDFS + "subClassOf", RDFS + "subPropertyOf", RDFS + "domain", RDFS + "range"
|
|
49
|
+
MEMBER, MEMBERSHIP = RDFS + "member", RDFS + "ContainerMembershipProperty"
|
|
50
|
+
EQ_CLASS, EQ_PROPERTY = OWL + "equivalentClass", OWL + "equivalentProperty"
|
|
51
|
+
FIRST, REST, NIL = RDF + "first", RDF + "rest", RDF + "nil"
|
|
52
|
+
|
|
53
|
+
# A term: ("i", iri) an IRI, ("b", label) a blank node, ("l", lexical, datatype, language) a literal.
|
|
54
|
+
Term = tuple
|
|
55
|
+
Triple = Tuple[Term, Term, Term]
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def iri(text: str) -> Term:
|
|
59
|
+
return ("i", text)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _axioms() -> List[Tuple[str, str, str]]:
|
|
63
|
+
"""RDF 1.1 Semantics 8.1 and 9.1, the `rdf:_n` ones aside (`_membership`)."""
|
|
64
|
+
out = [(RDF + p, TYPE, PROPERTY) for p in ("type", "subject", "predicate", "object", "first", "rest", "value")]
|
|
65
|
+
out.append((NIL, TYPE, RDF + "List"))
|
|
66
|
+
domains = {TYPE: RESOURCE, DOMAIN: PROPERTY, RANGE: PROPERTY, SUB_PROPERTY: PROPERTY, SUB_CLASS: CLASS,
|
|
67
|
+
RDF + "subject": RDF + "Statement", RDF + "predicate": RDF + "Statement",
|
|
68
|
+
RDF + "object": RDF + "Statement", MEMBER: RESOURCE, FIRST: RDF + "List", REST: RDF + "List",
|
|
69
|
+
RDFS + "seeAlso": RESOURCE, RDFS + "isDefinedBy": RESOURCE, RDFS + "comment": RESOURCE,
|
|
70
|
+
RDFS + "label": RESOURCE, RDF + "value": RESOURCE}
|
|
71
|
+
ranges = {TYPE: CLASS, DOMAIN: CLASS, RANGE: CLASS, SUB_PROPERTY: PROPERTY, SUB_CLASS: CLASS,
|
|
72
|
+
RDF + "subject": RESOURCE, RDF + "predicate": RESOURCE, RDF + "object": RESOURCE, MEMBER: RESOURCE,
|
|
73
|
+
FIRST: RESOURCE, REST: RDF + "List", RDFS + "seeAlso": RESOURCE, RDFS + "isDefinedBy": RESOURCE,
|
|
74
|
+
RDFS + "comment": LITERAL, RDFS + "label": LITERAL, RDF + "value": RESOURCE}
|
|
75
|
+
out += [(p, DOMAIN, c) for p, c in domains.items()] + [(p, RANGE, c) for p, c in ranges.items()]
|
|
76
|
+
out += [(RDF + c, SUB_CLASS, RDFS + "Container") for c in ("Alt", "Bag", "Seq")]
|
|
77
|
+
out += [(MEMBERSHIP, SUB_CLASS, PROPERTY), (RDFS + "isDefinedBy", SUB_PROPERTY, RDFS + "seeAlso"),
|
|
78
|
+
(DATATYPE, SUB_CLASS, CLASS)]
|
|
79
|
+
return out
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
AXIOMS = tuple((iri(s), iri(p), iri(o)) for s, p, o in _axioms())
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _membership(text: str) -> bool:
|
|
86
|
+
"""An `rdf:_n` IRI (n a positive integer, no leading zero)."""
|
|
87
|
+
rest = text[len(RDF) + 1:] if text.startswith(RDF + "_") else ""
|
|
88
|
+
return rest.isdigit() and not rest.startswith("0")
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _membership_axioms(n: Term) -> List[Triple]:
|
|
92
|
+
return [(n, iri(TYPE), iri(PROPERTY)), (n, iri(TYPE), iri(MEMBERSHIP)), (n, iri(DOMAIN), iri(RESOURCE)),
|
|
93
|
+
(n, iri(RANGE), iri(RESOURCE))]
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
# The recognized datatypes (rdfs1): RDFS entailment recognizes rdf:langString and xsd:string (RDF 1.1 Semantics 9).
|
|
97
|
+
RECOGNIZED = (xsd.RDF_LANGSTRING, XSD_STRING)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _rdf_vocabulary(text: str) -> bool:
|
|
101
|
+
"""Whether `text` is RDF's or RDFS's own vocabulary (Entailment Regimes' V-minus, `rdf:_n` aside)."""
|
|
102
|
+
return (text.startswith(RDF) or text.startswith(RDFS)) and not _membership(text)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class Closure:
|
|
106
|
+
"""A set of triples closed under the rules this module states; `extend` closes more triples on top of it
|
|
107
|
+
without changing it (the triples a witnessed term adds)."""
|
|
108
|
+
|
|
109
|
+
def __init__(self, rl: bool = False, base: Optional["Closure"] = None, ql: bool = False,
|
|
110
|
+
direct: bool = False) -> None:
|
|
111
|
+
self.rl = rl
|
|
112
|
+
self.ql = ql
|
|
113
|
+
# Under ql, OWL 2 Direct semantics' consequences for typed variables too (`typed-variables`): the
|
|
114
|
+
# built-ins, scm-cls, scm-op and scm-dp. `direct_only` (set by the reader) holds what they add.
|
|
115
|
+
self.direct = direct
|
|
116
|
+
self.direct_only: FrozenSet[Triple] = frozenset()
|
|
117
|
+
self.plain: Optional["Closure"] = None
|
|
118
|
+
self.base = base
|
|
119
|
+
self.triples: Set[Triple] = set()
|
|
120
|
+
self.by_p: Dict[Term, Set[Tuple[Term, Term]]] = {}
|
|
121
|
+
self.by_ps: Dict[Tuple[Term, Term], Set[Term]] = {}
|
|
122
|
+
self.by_po: Dict[Tuple[Term, Term], Set[Term]] = {}
|
|
123
|
+
self.named: Set[Term] = set() # rdf:_n IRIs whose axioms are in
|
|
124
|
+
|
|
125
|
+
# -- the indexes, through the base --------------------------------------------------------------------
|
|
126
|
+
def has(self, t: Triple) -> bool:
|
|
127
|
+
return t in self.triples or (self.base is not None and self.base.has(t))
|
|
128
|
+
|
|
129
|
+
def pairs(self, p: Term) -> List[Tuple[Term, Term]]:
|
|
130
|
+
out = list(self.by_p.get(p, ()))
|
|
131
|
+
return out + self.base.pairs(p) if self.base is not None else out
|
|
132
|
+
|
|
133
|
+
def objects(self, s: Term, p: Term) -> List[Term]:
|
|
134
|
+
out = list(self.by_ps.get((p, s), ()))
|
|
135
|
+
return out + self.base.objects(s, p) if self.base is not None else out
|
|
136
|
+
|
|
137
|
+
def subjects(self, p: Term, o: Term) -> List[Term]:
|
|
138
|
+
out = list(self.by_po.get((p, o), ()))
|
|
139
|
+
return out + self.base.subjects(p, o) if self.base is not None else out
|
|
140
|
+
|
|
141
|
+
def all(self) -> Set[Triple]:
|
|
142
|
+
return self.triples | (self.base.all() if self.base is not None else set())
|
|
143
|
+
|
|
144
|
+
def _named(self, t: Term) -> bool:
|
|
145
|
+
return t in self.named or (self.base is not None and self.base._named(t))
|
|
146
|
+
|
|
147
|
+
# -- closing ------------------------------------------------------------------------------------------
|
|
148
|
+
def add(self, triples: Iterable[Triple]) -> "Closure":
|
|
149
|
+
"""Add `triples` and every consequence (semi-naive: each new triple joined with what is held)."""
|
|
150
|
+
todo = [t for t in triples]
|
|
151
|
+
while todo:
|
|
152
|
+
t = todo.pop()
|
|
153
|
+
if t[0][0] == "l" or self.has(t):
|
|
154
|
+
continue
|
|
155
|
+
self.triples.add(t)
|
|
156
|
+
s, p, o = t
|
|
157
|
+
self.by_p.setdefault(p, set()).add((s, o))
|
|
158
|
+
self.by_ps.setdefault((p, s), set()).add(o)
|
|
159
|
+
self.by_po.setdefault((p, o), set()).add(s)
|
|
160
|
+
for term in t:
|
|
161
|
+
if term[0] == "i" and _membership(term[1]) and not self._named(term):
|
|
162
|
+
self.named.add(term)
|
|
163
|
+
todo += _membership_axioms(term)
|
|
164
|
+
todo += self._consequences(s, p, o)
|
|
165
|
+
return self
|
|
166
|
+
|
|
167
|
+
def _consequences(self, s: Term, p: Term, o: Term) -> List[Triple]:
|
|
168
|
+
T, out = iri(TYPE), []
|
|
169
|
+
resource = iri(RESOURCE)
|
|
170
|
+
out.append((p, T, iri(PROPERTY))) # rdf1
|
|
171
|
+
out.append((s, T, resource)) # rdfs4a
|
|
172
|
+
if o[0] != "l":
|
|
173
|
+
out.append((o, T, resource)) # rdfs4b
|
|
174
|
+
out += [(s, T, c) for c in self.objects(p, iri(DOMAIN))] # rdfs2
|
|
175
|
+
if o[0] != "l":
|
|
176
|
+
out += [(o, T, c) for c in self.objects(p, iri(RANGE))] # rdfs3
|
|
177
|
+
out += [(s, q, o) for q in self.objects(p, iri(SUB_PROPERTY))] # rdfs7
|
|
178
|
+
pt = p[1] if p[0] == "i" else None
|
|
179
|
+
if pt == DOMAIN:
|
|
180
|
+
out += [(x, T, o) for x, _ in self.pairs(s)] # rdfs2
|
|
181
|
+
if self.rl:
|
|
182
|
+
out += [(s, p, d) for d in self.objects(o, iri(SUB_CLASS))] # scm-dom1
|
|
183
|
+
out += [(q, p, o) for q in self.subjects(iri(SUB_PROPERTY), s)] # scm-dom2
|
|
184
|
+
elif pt == RANGE:
|
|
185
|
+
out += [(y, T, o) for _, y in self.pairs(s) if y[0] != "l"] # rdfs3
|
|
186
|
+
if self.rl:
|
|
187
|
+
out += [(s, p, d) for d in self.objects(o, iri(SUB_CLASS))] # scm-rng1
|
|
188
|
+
out += [(q, p, o) for q in self.subjects(iri(SUB_PROPERTY), s)] # scm-rng2
|
|
189
|
+
elif pt == SUB_PROPERTY:
|
|
190
|
+
out += [(x, o, y) for x, y in self.pairs(s)] # rdfs7
|
|
191
|
+
out += [(s, p, r) for r in self.objects(o, p)] # rdfs5
|
|
192
|
+
out += [(z, p, o) for z in self.subjects(p, s)]
|
|
193
|
+
if (self.rl or (self.ql and s != o)) and self.has((o, p, s)):
|
|
194
|
+
out += [(s, iri(EQ_PROPERTY), o), (o, iri(EQ_PROPERTY), s)] # scm-eqp2
|
|
195
|
+
if self.rl:
|
|
196
|
+
out += [(s, iri(DOMAIN), c) for c in self.objects(o, iri(DOMAIN))] # scm-dom2
|
|
197
|
+
out += [(s, iri(RANGE), c) for c in self.objects(o, iri(RANGE))] # scm-rng2
|
|
198
|
+
elif pt == SUB_CLASS:
|
|
199
|
+
out += [(x, T, o) for x in self.subjects(T, s)] # rdfs9
|
|
200
|
+
out += [(s, p, d) for d in self.objects(o, p)] # rdfs11
|
|
201
|
+
out += [(z, p, o) for z in self.subjects(p, s)]
|
|
202
|
+
if (self.rl or (self.ql and s != o)) and self.has((o, p, s)):
|
|
203
|
+
out += [(s, iri(EQ_CLASS), o), (o, iri(EQ_CLASS), s)] # scm-eqc2
|
|
204
|
+
if self.rl:
|
|
205
|
+
out += [(q, iri(DOMAIN), o) for q in self.subjects(iri(DOMAIN), s)] # scm-dom1
|
|
206
|
+
out += [(q, iri(RANGE), o) for q in self.subjects(iri(RANGE), s)] # scm-rng1
|
|
207
|
+
elif pt == EQ_CLASS:
|
|
208
|
+
out += [(s, iri(SUB_CLASS), o), (o, iri(SUB_CLASS), s)] # scm-eqc1
|
|
209
|
+
elif pt == EQ_PROPERTY:
|
|
210
|
+
out += [(s, iri(SUB_PROPERTY), o), (o, iri(SUB_PROPERTY), s)] # scm-eqp1
|
|
211
|
+
elif pt == TYPE:
|
|
212
|
+
out += [(s, T, d) for d in self.objects(o, iri(SUB_CLASS))] # rdfs9
|
|
213
|
+
ot = o[1] if o[0] == "i" else None
|
|
214
|
+
if ot == CLASS:
|
|
215
|
+
out += [(s, iri(SUB_CLASS), resource), (s, iri(SUB_CLASS), s)] # rdfs8, rdfs10
|
|
216
|
+
elif ot == PROPERTY:
|
|
217
|
+
out.append((s, iri(SUB_PROPERTY), s)) # rdfs6
|
|
218
|
+
elif ot == MEMBERSHIP:
|
|
219
|
+
out.append((s, iri(SUB_PROPERTY), iri(MEMBER))) # rdfs12
|
|
220
|
+
elif ot == DATATYPE:
|
|
221
|
+
out.append((s, iri(SUB_CLASS), iri(LITERAL))) # rdfs13
|
|
222
|
+
elif (self.rl or self.direct) and ot == OWL + "Class": # scm-cls
|
|
223
|
+
out += [(s, iri(SUB_CLASS), s), (s, iri(EQ_CLASS), s), (s, iri(SUB_CLASS), iri(OWL + "Thing")),
|
|
224
|
+
(iri(OWL + "Nothing"), iri(SUB_CLASS), s)]
|
|
225
|
+
elif (self.rl or self.direct) and ot in (OWL + "ObjectProperty", OWL + "DatatypeProperty"): # scm-op/dp
|
|
226
|
+
out += [(s, iri(SUB_PROPERTY), s), (s, iri(EQ_PROPERTY), s)]
|
|
227
|
+
if self.direct:
|
|
228
|
+
# Direct semantics: the bottom object (data) property is under every one, and every object
|
|
229
|
+
# property under the top one (a TBox answer). The top property's own pairs are every pair of
|
|
230
|
+
# individuals (`Translator.top_pairs`), never the ones rdfs7 writes from stated pairs here.
|
|
231
|
+
kind = "Object" if ot == OWL + "ObjectProperty" else "Data"
|
|
232
|
+
out.append((iri(f"{OWL}bottom{kind}Property"), iri(SUB_PROPERTY), s))
|
|
233
|
+
if kind == "Object" and s != iri(OWL + "topObjectProperty"):
|
|
234
|
+
out.append((s, iri(SUB_PROPERTY), iri(OWL + "topObjectProperty")))
|
|
235
|
+
return out
|
|
236
|
+
|
|
237
|
+
# -- OWL 2 RL's schema rules over class expressions (rl only) ------------------------------------------
|
|
238
|
+
def members(self, head: Term) -> List[Term]:
|
|
239
|
+
"""An RDF list's members, [] where it is not a whole list."""
|
|
240
|
+
out, seen = [], set()
|
|
241
|
+
while head != iri(NIL):
|
|
242
|
+
if head in seen:
|
|
243
|
+
return []
|
|
244
|
+
seen.add(head)
|
|
245
|
+
first, rest = self.objects(head, iri(FIRST)), self.objects(head, iri(REST))
|
|
246
|
+
if len(first) != 1 or len(rest) != 1:
|
|
247
|
+
return []
|
|
248
|
+
out.append(first[0])
|
|
249
|
+
head = rest[0]
|
|
250
|
+
return out
|
|
251
|
+
|
|
252
|
+
def expressions(self) -> List[Triple]:
|
|
253
|
+
"""scm-int, scm-uni, scm-hv, scm-svf1/2 and scm-avf1/2 over every class expression held (`close` runs
|
|
254
|
+
them to a fixpoint with the rest)."""
|
|
255
|
+
out: List[Triple] = []
|
|
256
|
+
sub = iri(SUB_CLASS)
|
|
257
|
+
for c, head in self.pairs(iri(OWL + "intersectionOf")):
|
|
258
|
+
out += [(c, sub, m) for m in self.members(head)]
|
|
259
|
+
for c, head in self.pairs(iri(OWL + "unionOf")):
|
|
260
|
+
out += [(m, sub, c) for m in self.members(head)]
|
|
261
|
+
on: Dict[Term, List[Term]] = {}
|
|
262
|
+
for c, p in self.pairs(iri(OWL + "onProperty")):
|
|
263
|
+
on.setdefault(c, []).append(p)
|
|
264
|
+
|
|
265
|
+
def subprop(p1: Term, p2: Term) -> bool:
|
|
266
|
+
return p1 == p2 or self.has((p1, iri(SUB_PROPERTY), p2))
|
|
267
|
+
|
|
268
|
+
def subclass(y1: Term, y2: Term) -> bool:
|
|
269
|
+
return y1 == y2 or self.has((y1, sub, y2))
|
|
270
|
+
for kind in ("hasValue", "someValuesFrom", "allValuesFrom"):
|
|
271
|
+
held = [(c, v, p) for c, v in self.pairs(iri(OWL + kind)) for p in on.get(c, ())]
|
|
272
|
+
for c1, v1, p1 in held:
|
|
273
|
+
for c2, v2, p2 in held:
|
|
274
|
+
if c1 == c2:
|
|
275
|
+
continue
|
|
276
|
+
if kind == "hasValue" and v1 == v2 and subprop(p1, p2):
|
|
277
|
+
out.append((c1, sub, c2)) # scm-hv
|
|
278
|
+
elif kind == "someValuesFrom" and ((p1 == p2 and subclass(v1, v2))
|
|
279
|
+
or (v1 == v2 and subprop(p1, p2))):
|
|
280
|
+
out.append((c1, sub, c2)) # scm-svf1, scm-svf2
|
|
281
|
+
elif kind == "allValuesFrom":
|
|
282
|
+
if p1 == p2 and subclass(v1, v2):
|
|
283
|
+
out.append((c1, sub, c2)) # scm-avf1
|
|
284
|
+
if v1 == v2 and subprop(p1, p2):
|
|
285
|
+
out.append((c2, sub, c1)) # scm-avf2
|
|
286
|
+
return [t for t in out if not self.has(t)]
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
# The built-in entities OWL 2 declares in every ontology (Structural Specification 5.8.1, table 5), which SPARQL 1.1
|
|
290
|
+
# Entailment Regimes 7.3 takes as declared in the queried ontology: under profile 'ql' (Direct semantics) a typed
|
|
291
|
+
# variable ranges over them too.
|
|
292
|
+
BUILT_INS = [(iri(OWL + x), iri(TYPE), iri(OWL + k)) for x, k in (
|
|
293
|
+
("Thing", "Class"), ("Nothing", "Class"), ("topObjectProperty", "ObjectProperty"),
|
|
294
|
+
("bottomObjectProperty", "ObjectProperty"), ("topDataProperty", "DatatypeProperty"),
|
|
295
|
+
("bottomDataProperty", "DatatypeProperty"))]
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def close(triples: Iterable[Triple], rl: bool = False, ql: bool = False, direct: bool = False) -> Closure:
|
|
299
|
+
"""`triples` with the axiomatic triples, closed (`Closure`); rdfs1 for each recognized datatype the triples
|
|
300
|
+
name, or RDF's own (rdf:langString). `ql`: subClassOf and subPropertyOf both ways between two terms are their
|
|
301
|
+
equivalence too (scm-eqc2, scm-eqp2: OWL 2 Direct semantics entails it; a term's equivalence to itself is not
|
|
302
|
+
added)."""
|
|
303
|
+
triples = list(triples)
|
|
304
|
+
named = {t for x in triples for t in x if t[0] == "i"}
|
|
305
|
+
seeds = [(iri(d), iri(TYPE), iri(DATATYPE)) for d in RECOGNIZED
|
|
306
|
+
if iri(d) in named or _rdf_vocabulary(d)] # rdfs1, within C2
|
|
307
|
+
if direct:
|
|
308
|
+
seeds += BUILT_INS # declared in every OWL 2 ontology (Structural Specification, table 5)
|
|
309
|
+
out = Closure(rl, ql=ql, direct=direct).add(list(AXIOMS) + seeds + triples)
|
|
310
|
+
while rl:
|
|
311
|
+
more = out.expressions()
|
|
312
|
+
if not more:
|
|
313
|
+
break
|
|
314
|
+
out.add(more)
|
|
315
|
+
return out
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
def extend(base: Closure, seeds: Iterable[Triple]) -> FrozenSet[Triple]:
|
|
319
|
+
"""The triples `seeds` add to the closed `base`, closed with it (`base` unchanged)."""
|
|
320
|
+
out = Closure(base.rl, base, base.ql, base.direct).add(seeds)
|
|
321
|
+
while base.rl:
|
|
322
|
+
more = out.expressions()
|
|
323
|
+
if not more:
|
|
324
|
+
break
|
|
325
|
+
out.add(more)
|
|
326
|
+
return frozenset(out.triples)
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def from_rdflib(graph, label: str = "o") -> List[Triple]:
|
|
330
|
+
"""An rdflib graph's triples as this module's terms; a blank node's label scoped by `label`."""
|
|
331
|
+
return [(term(s, label), term(p, label), term(o, label)) for s, p, o in graph]
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
def term(x, label: str = "o") -> Term:
|
|
335
|
+
from rdflib import BNode, Literal
|
|
336
|
+
if isinstance(x, BNode):
|
|
337
|
+
return ("b", f"{label}{x}")
|
|
338
|
+
if isinstance(x, Literal):
|
|
339
|
+
language = x.language.lower() if x.language else None
|
|
340
|
+
datatype = None if language else (str(x.datatype) if x.datatype is not None else XSD_STRING)
|
|
341
|
+
return ("l", str(x), datatype, language)
|
|
342
|
+
return ("i", str(x))
|