cognite-neat 0.74.0__py3-none-any.whl → 0.75.1__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.
Potentially problematic release.
This version of cognite-neat might be problematic. Click here for more details.
- cognite/neat/_version.py +1 -1
- cognite/neat/app/api/routers/core.py +1 -2
- cognite/neat/app/api/routers/data_exploration.py +1 -1
- cognite/neat/app/api/routers/rules.py +13 -7
- cognite/neat/constants.py +2 -2
- cognite/neat/graph/extractors/_mock_graph_generator.py +4 -4
- cognite/neat/graph/stores/_base.py +2 -23
- cognite/neat/legacy/graph/__init__.py +3 -0
- cognite/neat/legacy/graph/examples/Knowledge-Graph-Nordic44-dirty.xml +20182 -0
- cognite/neat/legacy/graph/examples/Knowledge-Graph-Nordic44.xml +20163 -0
- cognite/neat/legacy/graph/examples/__init__.py +7 -0
- cognite/neat/legacy/graph/examples/skos-capturing-sheet-wind-topics.xlsx +0 -0
- cognite/neat/legacy/graph/exceptions.py +91 -0
- cognite/neat/{graph/extractor → legacy/graph/extractors}/_base.py +1 -1
- cognite/neat/{graph/extractor → legacy/graph/extractors}/_dexpi.py +2 -2
- cognite/neat/{graph/extractor → legacy/graph/extractors}/_graph_capturing_sheet.py +6 -6
- cognite/neat/{graph/extractor → legacy/graph/extractors}/_mock_graph_generator.py +6 -5
- cognite/neat/{graph/loader → legacy/graph/loaders}/__init__.py +1 -1
- cognite/neat/{graph/loader → legacy/graph/loaders}/_asset_loader.py +3 -3
- cognite/neat/{graph/loader → legacy/graph/loaders}/_base.py +4 -4
- cognite/neat/{graph/loader → legacy/graph/loaders}/core/labels.py +2 -2
- cognite/neat/{graph/loader → legacy/graph/loaders}/core/models.py +1 -1
- cognite/neat/{graph/loader → legacy/graph/loaders}/core/rdf_to_assets.py +3 -3
- cognite/neat/{graph/loader → legacy/graph/loaders}/core/rdf_to_relationships.py +5 -5
- cognite/neat/{graph/loader → legacy/graph/loaders}/rdf_to_dms.py +5 -5
- cognite/neat/legacy/graph/models.py +6 -0
- cognite/neat/legacy/graph/stores/__init__.py +13 -0
- cognite/neat/legacy/graph/stores/_base.py +384 -0
- cognite/neat/legacy/graph/stores/_graphdb_store.py +51 -0
- cognite/neat/legacy/graph/stores/_memory_store.py +43 -0
- cognite/neat/legacy/graph/stores/_oxigraph_store.py +147 -0
- cognite/neat/legacy/graph/stores/_oxrdflib.py +247 -0
- cognite/neat/legacy/graph/stores/_rdf_to_graph.py +40 -0
- cognite/neat/{graph/transformation → legacy/graph/transformations}/entity_matcher.py +1 -1
- cognite/neat/{graph/transformation → legacy/graph/transformations}/query_generator/sparql.py +9 -7
- cognite/neat/{graph/transformation → legacy/graph/transformations}/transformer.py +12 -5
- cognite/neat/legacy/rules/__init__.py +0 -0
- cognite/neat/{rules → legacy/rules}/analysis.py +2 -2
- cognite/neat/legacy/rules/examples/__init__.py +18 -0
- cognite/neat/legacy/rules/examples/wind-energy.owl +1511 -0
- cognite/neat/legacy/rules/exceptions.py +2972 -0
- cognite/neat/{rules/exporter → legacy/rules/exporters}/_base.py +2 -2
- cognite/neat/{rules/exporter → legacy/rules/exporters}/_core/rules2labels.py +1 -1
- cognite/neat/{rules/exporter → legacy/rules/exporters}/_rules2dms.py +6 -6
- cognite/neat/{rules/exporter → legacy/rules/exporters}/_rules2excel.py +1 -1
- cognite/neat/{rules/exporter → legacy/rules/exporters}/_rules2graphql.py +5 -5
- cognite/neat/{rules/exporter → legacy/rules/exporters}/_rules2ontology.py +6 -6
- cognite/neat/{rules/exporter → legacy/rules/exporters}/_rules2pydantic_models.py +8 -8
- cognite/neat/{rules/exporter → legacy/rules/exporters}/_rules2rules.py +3 -2
- cognite/neat/{rules/exporter → legacy/rules/exporters}/_rules2triples.py +3 -2
- cognite/neat/{rules/exporter → legacy/rules/exporters}/_validation.py +2 -2
- cognite/neat/{rules/importer → legacy/rules/importers}/_base.py +2 -2
- cognite/neat/{rules/importer → legacy/rules/importers}/_dict2rules.py +1 -1
- cognite/neat/{rules/importer → legacy/rules/importers}/_dms2rules.py +2 -2
- cognite/neat/{rules/importer → legacy/rules/importers}/_graph2rules.py +5 -4
- cognite/neat/{rules/importer → legacy/rules/importers}/_owl2rules/_owl2metadata.py +11 -7
- cognite/neat/{rules/importer → legacy/rules/importers}/_owl2rules/_owl2rules.py +5 -5
- cognite/neat/{rules/importer → legacy/rules/importers}/_spreadsheet2rules.py +2 -2
- cognite/neat/{rules/importer → legacy/rules/importers}/_xsd2rules.py +1 -1
- cognite/neat/{rules/importer → legacy/rules/importers}/_yaml2rules.py +1 -1
- cognite/neat/{rules → legacy/rules}/models/raw_rules.py +4 -4
- cognite/neat/legacy/rules/models/rdfpath.py +238 -0
- cognite/neat/{rules → legacy/rules}/models/rules.py +4 -4
- cognite/neat/{rules → legacy/rules}/models/value_types.py +1 -1
- cognite/neat/rules/_shared.py +1 -1
- cognite/neat/rules/analysis/__init__.py +3 -0
- cognite/neat/rules/{_analysis → analysis}/_base.py +1 -1
- cognite/neat/rules/{_analysis → analysis}/_information_rules.py +3 -3
- cognite/neat/rules/examples/__init__.py +0 -9
- cognite/neat/rules/exporters/_base.py +1 -1
- cognite/neat/rules/exporters/_rules2dms.py +4 -4
- cognite/neat/rules/exporters/_rules2excel.py +2 -2
- cognite/neat/rules/exporters/_rules2ontology.py +4 -4
- cognite/neat/rules/exporters/_rules2yaml.py +1 -1
- cognite/neat/rules/exporters/_validation.py +2 -2
- cognite/neat/rules/importers/_base.py +1 -1
- cognite/neat/rules/importers/_dms2rules.py +10 -10
- cognite/neat/rules/importers/_dtdl2rules/dtdl_converter.py +2 -2
- cognite/neat/rules/importers/_dtdl2rules/dtdl_importer.py +3 -3
- cognite/neat/rules/importers/_dtdl2rules/spec.py +1 -1
- cognite/neat/rules/importers/_owl2rules/_owl2classes.py +1 -1
- cognite/neat/rules/importers/_owl2rules/_owl2metadata.py +6 -6
- cognite/neat/rules/importers/_owl2rules/_owl2properties.py +1 -1
- cognite/neat/rules/importers/_owl2rules/_owl2rules.py +2 -2
- cognite/neat/rules/importers/_spreadsheet2rules.py +2 -2
- cognite/neat/rules/importers/_yaml2rules.py +1 -1
- cognite/neat/rules/models/_entity.py +142 -0
- cognite/neat/rules/models/rdfpath.py +6 -8
- cognite/neat/rules/models/{_rules → rules}/__init__.py +5 -5
- cognite/neat/rules/models/{_rules/base.py → rules/_base.py} +1 -1
- cognite/neat/rules/models/{_rules/dms_architect_rules.py → rules/_dms_architect_rules.py} +5 -5
- cognite/neat/rules/models/{_rules/dms_schema.py → rules/_dms_schema.py} +1 -1
- cognite/neat/rules/models/{_rules/domain_rules.py → rules/_domain_rules.py} +2 -2
- cognite/neat/rules/models/{_rules/information_rules.py → rules/_information_rules.py} +15 -15
- cognite/neat/rules/models/{_rules → rules}/_types/_base.py +18 -134
- cognite/neat/workflows/steps/data_contracts.py +4 -4
- cognite/neat/workflows/steps/lib/graph_store.py +1 -1
- cognite/neat/workflows/steps/lib/rules_exporter.py +1 -1
- cognite/neat/workflows/steps/lib/rules_importer.py +2 -2
- cognite/neat/workflows/steps/lib/rules_validator.py +2 -2
- cognite/neat/workflows/steps/lib/v1/graph_contextualization.py +1 -1
- cognite/neat/workflows/steps/lib/v1/graph_extractor.py +5 -5
- cognite/neat/workflows/steps/lib/v1/graph_loader.py +7 -7
- cognite/neat/workflows/steps/lib/v1/graph_store.py +7 -1
- cognite/neat/workflows/steps/lib/v1/graph_transformer.py +2 -2
- cognite/neat/workflows/steps/lib/v1/rules_exporter.py +7 -7
- cognite/neat/workflows/steps/lib/v1/rules_importer.py +13 -13
- {cognite_neat-0.74.0.dist-info → cognite_neat-0.75.1.dist-info}/METADATA +1 -1
- cognite_neat-0.75.1.dist-info/RECORD +258 -0
- cognite_neat-0.74.0.dist-info/RECORD +0 -237
- /cognite/neat/{graph/loader/core → legacy}/__init__.py +0 -0
- /cognite/neat/{graph/extractor → legacy/graph/extractors}/__init__.py +0 -0
- /cognite/neat/{graph/loader → legacy/graph/loaders}/_exceptions.py +0 -0
- /cognite/neat/{graph/transformation → legacy/graph/loaders/core}/__init__.py +0 -0
- /cognite/neat/{graph/loader → legacy/graph/loaders}/validator.py +0 -0
- /cognite/neat/{rules/_analysis → legacy/graph/transformations}/__init__.py +0 -0
- /cognite/neat/{graph/transformation → legacy/graph/transformations}/query_generator/__init__.py +0 -0
- /cognite/neat/{rules → legacy/rules}/examples/Rules-Nordic44-to-TNT.xlsx +0 -0
- /cognite/neat/{rules → legacy/rules}/examples/Rules-Nordic44-to-graphql.xlsx +0 -0
- /cognite/neat/{rules → legacy/rules}/examples/power-grid-containers.yaml +0 -0
- /cognite/neat/{rules → legacy/rules}/examples/power-grid-example.xlsx +0 -0
- /cognite/neat/{rules → legacy/rules}/examples/power-grid-model.yaml +0 -0
- /cognite/neat/{rules → legacy/rules}/examples/rules-template.xlsx +0 -0
- /cognite/neat/{rules → legacy/rules}/examples/sheet2cdf-transformation-rules.xlsx +0 -0
- /cognite/neat/{rules → legacy/rules}/examples/skos-rules.xlsx +0 -0
- /cognite/neat/{rules → legacy/rules}/examples/source-to-solution-mapping-rules.xlsx +0 -0
- /cognite/neat/{rules/exporter → legacy/rules/exporters}/__init__.py +0 -0
- /cognite/neat/{rules/exporter → legacy/rules/exporters}/_core/__init__.py +0 -0
- /cognite/neat/{rules/importer → legacy/rules/importers}/__init__.py +0 -0
- /cognite/neat/{rules/importer → legacy/rules/importers}/_json2rules.py +0 -0
- /cognite/neat/{rules/importer → legacy/rules/importers}/_owl2rules/__init__.py +0 -0
- /cognite/neat/{rules/importer → legacy/rules/importers}/_owl2rules/_owl2classes.py +0 -0
- /cognite/neat/{rules/importer → legacy/rules/importers}/_owl2rules/_owl2properties.py +0 -0
- /cognite/neat/{rules → legacy/rules}/models/__init__.py +0 -0
- /cognite/neat/{rules → legacy/rules}/models/_base.py +0 -0
- /cognite/neat/{rules → legacy/rules}/models/tables.py +0 -0
- /cognite/neat/rules/models/{_rules → rules}/_types/__init__.py +0 -0
- /cognite/neat/rules/models/{_rules → rules}/_types/_field.py +0 -0
- /cognite/neat/rules/models/{_rules → rules}/_types/_value.py +0 -0
- /cognite/neat/workflows/examples/{Export DMS → Export_DMS}/workflow.yaml +0 -0
- /cognite/neat/workflows/examples/{Export Rules to Ontology → Export_Rules_to_Ontology}/workflow.yaml +0 -0
- /cognite/neat/workflows/examples/{Extract DEXPI Graph and Export Rules → Extract_DEXPI_Graph_and_Export_Rules}/workflow.yaml +0 -0
- /cognite/neat/workflows/examples/{Extract RDF Graph and Generate Assets → Extract_RDF_Graph_and_Generate_Assets}/workflow.yaml +0 -0
- /cognite/neat/workflows/examples/{Import DMS → Import_DMS}/workflow.yaml +0 -0
- /cognite/neat/workflows/examples/{Ontology to Data Model → Ontology_to_Data_Model}/workflow.yaml +0 -0
- /cognite/neat/workflows/examples/{Validate Rules → Validate_Rules}/workflow.yaml +0 -0
- /cognite/neat/workflows/examples/{Validate Solution Model → Validate_Solution_Model}/workflow.yaml +0 -0
- /cognite/neat/workflows/examples/{Visualize Data Model Using Mock Graph → Visualize_Data_Model_Using_Mock_Graph}/workflow.yaml +0 -0
- /cognite/neat/workflows/examples/{Visualize Semantic Data Model → Visualize_Semantic_Data_Model}/workflow.yaml +0 -0
- {cognite_neat-0.74.0.dist-info → cognite_neat-0.75.1.dist-info}/LICENSE +0 -0
- {cognite_neat-0.74.0.dist-info → cognite_neat-0.75.1.dist-info}/WHEEL +0 -0
- {cognite_neat-0.74.0.dist-info → cognite_neat-0.75.1.dist-info}/entry_points.txt +0 -0
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import shutil
|
|
2
|
+
from collections.abc import Generator, Iterable, Iterator, Mapping
|
|
3
|
+
from typing import Any, cast
|
|
4
|
+
|
|
5
|
+
import pyoxigraph as ox
|
|
6
|
+
from rdflib import Graph
|
|
7
|
+
from rdflib.graph import DATASET_DEFAULT_GRAPH_ID
|
|
8
|
+
from rdflib.plugins.sparql.sparql import Query, Update
|
|
9
|
+
from rdflib.query import Result
|
|
10
|
+
from rdflib.store import VALID_STORE, Store
|
|
11
|
+
from rdflib.term import BNode, Identifier, Literal, Node, URIRef, Variable
|
|
12
|
+
|
|
13
|
+
__all__ = ["OxigraphStore"]
|
|
14
|
+
|
|
15
|
+
from typing import TypeAlias
|
|
16
|
+
|
|
17
|
+
_Triple: TypeAlias = tuple[Node, Node, Node]
|
|
18
|
+
_Quad: TypeAlias = tuple[Node, Node, Node, Graph]
|
|
19
|
+
_TriplePattern: TypeAlias = tuple[Node | None, Node | None, Node | None]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class OxigraphStore(Store):
|
|
23
|
+
context_aware: bool = True
|
|
24
|
+
formula_aware: bool = False
|
|
25
|
+
transaction_aware: bool = False
|
|
26
|
+
graph_aware: bool = True
|
|
27
|
+
|
|
28
|
+
def __init__(
|
|
29
|
+
self, configuration: str | None = None, identifier: Identifier | None = None, *, store: ox.Store | None = None
|
|
30
|
+
):
|
|
31
|
+
self._store = store
|
|
32
|
+
self._prefix_for_namespace: dict[URIRef, str] = {}
|
|
33
|
+
self._namespace_for_prefix: dict[str, URIRef] = {}
|
|
34
|
+
super().__init__(configuration, identifier)
|
|
35
|
+
|
|
36
|
+
def open(self, configuration: str, create: bool = False) -> int | None:
|
|
37
|
+
if self._store is not None:
|
|
38
|
+
raise ValueError("The open function should be called before any RDF operation")
|
|
39
|
+
self._store = ox.Store(configuration)
|
|
40
|
+
return VALID_STORE
|
|
41
|
+
|
|
42
|
+
def close(self, commit_pending_transaction: bool = False) -> None:
|
|
43
|
+
del self._store
|
|
44
|
+
|
|
45
|
+
def destroy(self, configuration: str) -> None:
|
|
46
|
+
shutil.rmtree(configuration)
|
|
47
|
+
|
|
48
|
+
def gc(self) -> None:
|
|
49
|
+
pass
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def _inner(self) -> ox.Store:
|
|
53
|
+
if self._store is None:
|
|
54
|
+
self._store = ox.Store()
|
|
55
|
+
return self._store
|
|
56
|
+
|
|
57
|
+
def add(self, triple: _Triple, context: Graph, quoted: bool = False) -> None:
|
|
58
|
+
if quoted:
|
|
59
|
+
raise ValueError("Oxigraph stores are not formula aware")
|
|
60
|
+
self._inner.add(_to_ox(triple, context))
|
|
61
|
+
super().add(triple, context, quoted)
|
|
62
|
+
|
|
63
|
+
def addN(self, quads: Iterable[_Quad]) -> None:
|
|
64
|
+
self._inner.extend([_to_ox(q) for q in quads])
|
|
65
|
+
for quad in quads:
|
|
66
|
+
(s, p, o, g) = quad
|
|
67
|
+
super().add((s, p, o), g)
|
|
68
|
+
|
|
69
|
+
def remove(self, triple: _TriplePattern, context: Graph | None = None) -> None:
|
|
70
|
+
for q in self._inner.quads_for_pattern(*_to_ox_quad_pattern(triple, context)):
|
|
71
|
+
self._inner.remove(q)
|
|
72
|
+
super().remove(triple, context)
|
|
73
|
+
|
|
74
|
+
def triples(
|
|
75
|
+
self, triple_pattern: _TriplePattern, context: Graph | None = None
|
|
76
|
+
) -> Iterator[tuple[_Triple, Iterator[Graph | None]]]:
|
|
77
|
+
return (_from_ox(q) for q in self._inner.quads_for_pattern(*_to_ox_quad_pattern(triple_pattern, context)))
|
|
78
|
+
|
|
79
|
+
def __len__(self, context: Graph | None = None) -> int:
|
|
80
|
+
if context is None:
|
|
81
|
+
# TODO: very bad
|
|
82
|
+
return len({q.triple for q in self._inner})
|
|
83
|
+
return sum(1 for _ in self._inner.quads_for_pattern(None, None, None, _to_ox(context)))
|
|
84
|
+
|
|
85
|
+
def contexts(self, triple: _Triple | None = None) -> Generator[Graph, None, None]:
|
|
86
|
+
if triple is None:
|
|
87
|
+
return (_from_ox(g) for g in self._inner.named_graphs())
|
|
88
|
+
return (_from_ox(q[3]) for q in self._inner.quads_for_pattern(*_to_ox_quad_pattern(triple)))
|
|
89
|
+
|
|
90
|
+
def query(
|
|
91
|
+
self,
|
|
92
|
+
query: Query | str,
|
|
93
|
+
initNs: Mapping[str, Any],
|
|
94
|
+
initBindings: Mapping[str, Identifier],
|
|
95
|
+
queryGraph: str,
|
|
96
|
+
**kwargs: Any,
|
|
97
|
+
) -> "Result":
|
|
98
|
+
if isinstance(queryGraph, Query) or kwargs:
|
|
99
|
+
raise NotImplementedError
|
|
100
|
+
init_ns = dict(self._namespace_for_prefix, **initNs)
|
|
101
|
+
if isinstance(query, Query):
|
|
102
|
+
query = str(query)
|
|
103
|
+
query = "".join(f"PREFIX {prefix}: <{namespace}>\n" for prefix, namespace in init_ns.items()) + query
|
|
104
|
+
if initBindings:
|
|
105
|
+
# Todo Anders: This is likely a bug as .n3 is not valid the Identifier.
|
|
106
|
+
# There are no tests reaching this code.
|
|
107
|
+
query += "\nVALUES ( {} ) {{ ({}) }}".format(
|
|
108
|
+
" ".join(f"?{k}" for k in initBindings),
|
|
109
|
+
" ".join(v.n3() for v in initBindings.values()), # type: ignore[attr-defined]
|
|
110
|
+
)
|
|
111
|
+
result = self._inner.query(
|
|
112
|
+
query,
|
|
113
|
+
use_default_graph_as_union=queryGraph == "__UNION__",
|
|
114
|
+
default_graph=_to_ox(queryGraph) if isinstance(queryGraph, Node) else None,
|
|
115
|
+
)
|
|
116
|
+
if isinstance(result, bool):
|
|
117
|
+
out = Result("ASK")
|
|
118
|
+
out.askAnswer = result
|
|
119
|
+
elif isinstance(result, ox.QuerySolutions):
|
|
120
|
+
out = Result("SELECT")
|
|
121
|
+
out.vars = [Variable(v.value) for v in result.variables]
|
|
122
|
+
out.bindings = [
|
|
123
|
+
{v: _from_ox(val) for v, val in zip(out.vars, solution, strict=False)} for solution in result
|
|
124
|
+
]
|
|
125
|
+
elif isinstance(result, ox.QueryTriples):
|
|
126
|
+
out = Result("CONSTRUCT")
|
|
127
|
+
out.graph = Graph()
|
|
128
|
+
out.graph += (_from_ox(t) for t in result)
|
|
129
|
+
else:
|
|
130
|
+
raise ValueError(f"Unexpected query result: {result}")
|
|
131
|
+
return out
|
|
132
|
+
|
|
133
|
+
def update(
|
|
134
|
+
self,
|
|
135
|
+
update: Update | str,
|
|
136
|
+
initNs: Mapping[str, Any],
|
|
137
|
+
initBindings: Mapping[str, Identifier],
|
|
138
|
+
queryGraph: str,
|
|
139
|
+
**kwargs: Any,
|
|
140
|
+
) -> None:
|
|
141
|
+
raise NotImplementedError
|
|
142
|
+
|
|
143
|
+
def commit(self) -> None:
|
|
144
|
+
# TODO: implement
|
|
145
|
+
pass
|
|
146
|
+
|
|
147
|
+
def rollback(self) -> None:
|
|
148
|
+
# TODO: implement
|
|
149
|
+
pass
|
|
150
|
+
|
|
151
|
+
def add_graph(self, graph: Graph) -> None:
|
|
152
|
+
self._inner.add_graph(_to_ox(graph))
|
|
153
|
+
|
|
154
|
+
def remove_graph(self, graph: Graph) -> None:
|
|
155
|
+
self._inner.remove_graph(_to_ox(graph))
|
|
156
|
+
|
|
157
|
+
def bind(self, prefix: str, namespace: URIRef, override: bool = True) -> None:
|
|
158
|
+
if not override and (prefix in self._namespace_for_prefix or namespace in self._prefix_for_namespace):
|
|
159
|
+
return # nothing to do
|
|
160
|
+
self._delete_from_prefix(prefix)
|
|
161
|
+
self._delete_from_namespace(namespace)
|
|
162
|
+
self._namespace_for_prefix[prefix] = namespace
|
|
163
|
+
self._prefix_for_namespace[namespace] = prefix
|
|
164
|
+
|
|
165
|
+
def _delete_from_prefix(self, prefix):
|
|
166
|
+
if prefix not in self._namespace_for_prefix:
|
|
167
|
+
return
|
|
168
|
+
namespace = self._namespace_for_prefix[prefix]
|
|
169
|
+
del self._namespace_for_prefix[prefix]
|
|
170
|
+
self._delete_from_namespace(namespace)
|
|
171
|
+
|
|
172
|
+
def _delete_from_namespace(self, namespace):
|
|
173
|
+
if namespace not in self._prefix_for_namespace:
|
|
174
|
+
return
|
|
175
|
+
prefix = self._prefix_for_namespace[namespace]
|
|
176
|
+
del self._prefix_for_namespace[namespace]
|
|
177
|
+
self._delete_from_prefix(prefix)
|
|
178
|
+
|
|
179
|
+
def prefix(self, namespace: URIRef) -> str | None:
|
|
180
|
+
return self._prefix_for_namespace.get(namespace)
|
|
181
|
+
|
|
182
|
+
def namespace(self, prefix: str) -> URIRef | None:
|
|
183
|
+
return self._namespace_for_prefix.get(prefix)
|
|
184
|
+
|
|
185
|
+
def namespaces(self) -> Iterator[tuple[str, URIRef]]:
|
|
186
|
+
yield from self._namespace_for_prefix.items()
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def _to_ox(term: Node | _Triple | _Quad | Graph, context: Graph | None = None):
|
|
190
|
+
if term is None:
|
|
191
|
+
return None
|
|
192
|
+
elif term == DATASET_DEFAULT_GRAPH_ID:
|
|
193
|
+
return ox.DefaultGraph()
|
|
194
|
+
elif isinstance(term, URIRef):
|
|
195
|
+
return ox.NamedNode(term)
|
|
196
|
+
elif isinstance(term, BNode):
|
|
197
|
+
return ox.BlankNode(term)
|
|
198
|
+
elif isinstance(term, Literal):
|
|
199
|
+
return ox.Literal(term, language=term.language, datatype=ox.NamedNode(term.datatype) if term.datatype else None)
|
|
200
|
+
elif isinstance(term, Graph):
|
|
201
|
+
return _to_ox(term.identifier)
|
|
202
|
+
elif isinstance(term, tuple) and len(term) == 3 and isinstance(context, Graph):
|
|
203
|
+
triple = cast(_Triple, term)
|
|
204
|
+
return ox.Quad(_to_ox(triple[0]), _to_ox(triple[1]), _to_ox(triple[2]), _to_ox(context))
|
|
205
|
+
elif isinstance(term, tuple) and len(term) == 4:
|
|
206
|
+
quad = cast(_Quad, term)
|
|
207
|
+
return ox.Quad(_to_ox(quad[0]), _to_ox(quad[1]), _to_ox(quad[2]), _to_ox(quad[3]))
|
|
208
|
+
raise ValueError(f"Unexpected rdflib term: {term!r}")
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def _to_ox_quad_pattern(triple: _TriplePattern, context: Graph | None = None):
|
|
212
|
+
(s, p, o) = triple
|
|
213
|
+
return _to_ox_term_pattern(s), _to_ox_term_pattern(p), _to_ox_term_pattern(o), _to_ox_term_pattern(context)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def _to_ox_term_pattern(term):
|
|
217
|
+
if term is None:
|
|
218
|
+
return None
|
|
219
|
+
if isinstance(term, URIRef):
|
|
220
|
+
return ox.NamedNode(term)
|
|
221
|
+
elif isinstance(term, BNode):
|
|
222
|
+
return ox.BlankNode(term)
|
|
223
|
+
elif isinstance(term, Literal):
|
|
224
|
+
return ox.Literal(term, language=term.language, datatype=ox.NamedNode(term.datatype) if term.datatype else None)
|
|
225
|
+
elif isinstance(term, Graph):
|
|
226
|
+
return _to_ox(term.identifier)
|
|
227
|
+
raise ValueError(f"Unexpected rdflib term: {term!r}")
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def _from_ox(term):
|
|
231
|
+
if term is None:
|
|
232
|
+
return None
|
|
233
|
+
if isinstance(term, ox.NamedNode):
|
|
234
|
+
return URIRef(term.value)
|
|
235
|
+
if isinstance(term, ox.BlankNode):
|
|
236
|
+
return BNode(term.value)
|
|
237
|
+
if isinstance(term, ox.Literal):
|
|
238
|
+
if term.language:
|
|
239
|
+
return Literal(term.value, lang=term.language)
|
|
240
|
+
return Literal(term.value, datatype=URIRef(term.datatype.value))
|
|
241
|
+
if isinstance(term, ox.DefaultGraph):
|
|
242
|
+
return None
|
|
243
|
+
if isinstance(term, ox.Triple):
|
|
244
|
+
return _from_ox(term.subject), _from_ox(term.predicate), _from_ox(term.object)
|
|
245
|
+
if isinstance(term, ox.Quad):
|
|
246
|
+
return (_from_ox(term.subject), _from_ox(term.predicate), _from_ox(term.object)), _from_ox(term.graph_name)
|
|
247
|
+
raise ValueError(f"Unexpected Oxigraph term: {term!r}")
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from rdflib import Graph, Namespace
|
|
4
|
+
|
|
5
|
+
from cognite.neat.constants import PREFIXES
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def rdf_file_to_graph(
|
|
9
|
+
graph: Graph,
|
|
10
|
+
filepath: Path,
|
|
11
|
+
base_prefix: str | None = None,
|
|
12
|
+
base_namespace: Namespace | None = None,
|
|
13
|
+
prefixes: dict[str, Namespace] = PREFIXES,
|
|
14
|
+
) -> Graph:
|
|
15
|
+
"""Created rdflib Graph instance loaded with RDF triples from file
|
|
16
|
+
|
|
17
|
+
Args:
|
|
18
|
+
filepath: Path to the RDF file
|
|
19
|
+
base_prefix: base prefix for URIs. Defaults to None.
|
|
20
|
+
base_namespace: base namespace for URIs . Defaults to None.
|
|
21
|
+
prefixes: Dictionary of prefixes to bind to graph. Defaults to PREFIXES.
|
|
22
|
+
graph: Graph instance to load RDF triples into. Defaults to None.
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
Graph instance loaded with RDF triples from file
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
if filepath.is_file():
|
|
29
|
+
graph.parse(filepath, publicID=base_namespace)
|
|
30
|
+
else:
|
|
31
|
+
for filename in filepath.iterdir():
|
|
32
|
+
if filename.is_file():
|
|
33
|
+
graph.parse(filename, publicID=base_namespace)
|
|
34
|
+
if base_prefix and base_namespace:
|
|
35
|
+
graph.bind(base_prefix, base_namespace)
|
|
36
|
+
if prefixes:
|
|
37
|
+
for prefix, namespace in prefixes.items():
|
|
38
|
+
graph.bind(prefix, namespace)
|
|
39
|
+
|
|
40
|
+
return graph
|
cognite/neat/{graph/transformation → legacy/graph/transformations}/query_generator/sparql.py
RENAMED
|
@@ -7,9 +7,9 @@ from rdflib import Graph, Namespace
|
|
|
7
7
|
from rdflib.term import URIRef
|
|
8
8
|
|
|
9
9
|
from cognite.neat.constants import PREFIXES
|
|
10
|
-
from cognite.neat.rules.analysis import get_classes_with_properties
|
|
11
|
-
from cognite.neat.rules.models._base import Triple
|
|
12
|
-
from cognite.neat.rules.models.rdfpath import (
|
|
10
|
+
from cognite.neat.legacy.rules.analysis import get_classes_with_properties
|
|
11
|
+
from cognite.neat.legacy.rules.models._base import Triple
|
|
12
|
+
from cognite.neat.legacy.rules.models.rdfpath import (
|
|
13
13
|
AllProperties,
|
|
14
14
|
AllReferences,
|
|
15
15
|
Hop,
|
|
@@ -20,7 +20,7 @@ from cognite.neat.rules.models.rdfpath import (
|
|
|
20
20
|
parse_rule,
|
|
21
21
|
parse_traversal,
|
|
22
22
|
)
|
|
23
|
-
from cognite.neat.rules.models.rules import Rules
|
|
23
|
+
from cognite.neat.legacy.rules.models.rules import Rules
|
|
24
24
|
from cognite.neat.utils.utils import remove_namespace
|
|
25
25
|
|
|
26
26
|
|
|
@@ -429,9 +429,11 @@ def _add_filter(class_instances, query_template):
|
|
|
429
429
|
|
|
430
430
|
def _triples2sparql_statement(triples: list[Triple]):
|
|
431
431
|
return [
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
432
|
+
(
|
|
433
|
+
f"OPTIONAL {{ {triple.subject} {triple.predicate} {triple.object} . }}"
|
|
434
|
+
if triple.optional
|
|
435
|
+
else f"{triple.subject} {triple.predicate} {triple.object} ."
|
|
436
|
+
)
|
|
435
437
|
for triple in triples
|
|
436
438
|
]
|
|
437
439
|
|
|
@@ -13,11 +13,18 @@ from pydantic import BaseModel
|
|
|
13
13
|
from rdflib import RDF, Graph
|
|
14
14
|
from rdflib.term import Literal, Node
|
|
15
15
|
|
|
16
|
-
from cognite.neat.graph.exceptions import NamespaceRequired
|
|
17
|
-
from cognite.neat.graph.
|
|
18
|
-
from cognite.neat.rules.models._base import EntityTypes
|
|
19
|
-
from cognite.neat.rules.models.rdfpath import
|
|
20
|
-
|
|
16
|
+
from cognite.neat.legacy.graph.exceptions import NamespaceRequired
|
|
17
|
+
from cognite.neat.legacy.graph.transformations.query_generator.sparql import build_sparql_query
|
|
18
|
+
from cognite.neat.legacy.rules.models._base import EntityTypes
|
|
19
|
+
from cognite.neat.legacy.rules.models.rdfpath import (
|
|
20
|
+
AllProperties,
|
|
21
|
+
AllReferences,
|
|
22
|
+
Query,
|
|
23
|
+
RawLookup,
|
|
24
|
+
Traversal,
|
|
25
|
+
parse_rule,
|
|
26
|
+
)
|
|
27
|
+
from cognite.neat.legacy.rules.models.rules import Rules
|
|
21
28
|
from cognite.neat.utils.utils import remove_namespace
|
|
22
29
|
|
|
23
30
|
prom_total_proc_rules_g = Gauge("neat_total_processed_rules", "Number of processed rules", ["state"])
|
|
File without changes
|
|
@@ -3,8 +3,8 @@ from collections import defaultdict
|
|
|
3
3
|
|
|
4
4
|
import pandas as pd
|
|
5
5
|
|
|
6
|
-
from cognite.neat.rules.models.rdfpath import TransformationRuleType
|
|
7
|
-
from cognite.neat.rules.models.rules import Property, Rules
|
|
6
|
+
from cognite.neat.legacy.rules.models.rdfpath import TransformationRuleType
|
|
7
|
+
from cognite.neat.legacy.rules.models.rules import Property, Rules
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
def get_defined_classes(transformation_rules: Rules) -> set[str]:
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
# we should make this a proper package that loads examples
|
|
4
|
+
# similar how they handle it in xarray:
|
|
5
|
+
# https://github.com/pydata/xarray/blob/main/xarray/tutorial.py
|
|
6
|
+
# Currently there are simple paths to the examples which are then easily loaded in the notebooks
|
|
7
|
+
|
|
8
|
+
_EXAMPLES = Path(__file__).parent
|
|
9
|
+
|
|
10
|
+
power_grid_model = _EXAMPLES / "power-grid-example.xlsx"
|
|
11
|
+
power_grid_containers = _EXAMPLES / "power-grid-containers.yaml"
|
|
12
|
+
power_grid_data_model = _EXAMPLES / "power-grid-model.yaml"
|
|
13
|
+
simple_example = _EXAMPLES / "sheet2cdf-transformation-rules.xlsx"
|
|
14
|
+
source_to_solution_mapping = _EXAMPLES / "source-to-solution-mapping-rules.xlsx"
|
|
15
|
+
nordic44 = _EXAMPLES / "Rules-Nordic44-to-TNT.xlsx"
|
|
16
|
+
nordic44_graphql = _EXAMPLES / "Rules-Nordic44-to-graphql.xlsx"
|
|
17
|
+
skos = _EXAMPLES / "skos-rules.xlsx"
|
|
18
|
+
wind_energy_ontology = _EXAMPLES / "wind-energy.owl"
|