cognite-neat 0.87.6__py3-none-any.whl → 0.88.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/data_classes/rest.py +0 -19
- cognite/neat/app/api/explorer.py +6 -4
- cognite/neat/app/api/routers/configuration.py +1 -1
- cognite/neat/app/api/routers/crud.py +11 -21
- cognite/neat/app/api/routers/workflows.py +24 -94
- cognite/neat/app/ui/neat-app/build/asset-manifest.json +7 -7
- cognite/neat/app/ui/neat-app/build/index.html +1 -1
- cognite/neat/app/ui/neat-app/build/static/css/{main.38a62222.css → main.72e3d92e.css} +2 -2
- cognite/neat/app/ui/neat-app/build/static/css/main.72e3d92e.css.map +1 -0
- cognite/neat/app/ui/neat-app/build/static/js/main.5a52cf09.js +3 -0
- cognite/neat/app/ui/neat-app/build/static/js/{main.ec7f72e2.js.LICENSE.txt → main.5a52cf09.js.LICENSE.txt} +0 -9
- cognite/neat/app/ui/neat-app/build/static/js/main.5a52cf09.js.map +1 -0
- cognite/neat/config.py +44 -27
- cognite/neat/exceptions.py +6 -0
- cognite/neat/graph/extractors/_classic_cdf/_assets.py +21 -73
- cognite/neat/graph/extractors/_classic_cdf/_base.py +102 -0
- cognite/neat/graph/extractors/_classic_cdf/_events.py +46 -42
- cognite/neat/graph/extractors/_classic_cdf/_files.py +41 -45
- cognite/neat/graph/extractors/_classic_cdf/_labels.py +75 -52
- cognite/neat/graph/extractors/_classic_cdf/_relationships.py +49 -27
- cognite/neat/graph/extractors/_classic_cdf/_sequences.py +47 -50
- cognite/neat/graph/extractors/_classic_cdf/_timeseries.py +47 -49
- cognite/neat/graph/queries/_base.py +22 -29
- cognite/neat/graph/queries/_shared.py +1 -1
- cognite/neat/graph/stores/_base.py +24 -11
- cognite/neat/graph/transformers/_rdfpath.py +3 -2
- cognite/neat/issues.py +8 -0
- cognite/neat/rules/exporters/_rules2ontology.py +28 -20
- cognite/neat/rules/exporters/_validation.py +15 -21
- cognite/neat/rules/importers/_inference2rules.py +31 -35
- cognite/neat/rules/importers/_owl2rules/_owl2metadata.py +3 -7
- cognite/neat/rules/importers/_spreadsheet2rules.py +30 -27
- cognite/neat/rules/issues/dms.py +20 -0
- cognite/neat/rules/issues/importing.py +15 -0
- cognite/neat/rules/issues/ontology.py +298 -0
- cognite/neat/rules/issues/spreadsheet.py +48 -0
- cognite/neat/rules/issues/tables.py +72 -0
- cognite/neat/rules/models/_rdfpath.py +4 -4
- cognite/neat/rules/models/_types/_field.py +9 -19
- cognite/neat/rules/models/information/_rules.py +5 -4
- cognite/neat/utils/rdf_.py +17 -9
- cognite/neat/utils/regex_patterns.py +52 -0
- cognite/neat/workflows/steps/data_contracts.py +17 -43
- cognite/neat/workflows/steps/lib/current/graph_extractor.py +28 -24
- cognite/neat/workflows/steps/lib/current/graph_loader.py +4 -21
- cognite/neat/workflows/steps/lib/current/graph_store.py +18 -134
- cognite/neat/workflows/steps_registry.py +5 -7
- {cognite_neat-0.87.6.dist-info → cognite_neat-0.88.1.dist-info}/METADATA +2 -6
- cognite_neat-0.88.1.dist-info/RECORD +209 -0
- cognite/neat/app/api/routers/core.py +0 -91
- cognite/neat/app/api/routers/data_exploration.py +0 -336
- cognite/neat/app/api/routers/rules.py +0 -203
- cognite/neat/app/ui/neat-app/build/static/css/main.38a62222.css.map +0 -1
- cognite/neat/app/ui/neat-app/build/static/js/main.ec7f72e2.js +0 -3
- cognite/neat/app/ui/neat-app/build/static/js/main.ec7f72e2.js.map +0 -1
- cognite/neat/graph/stores/_oxrdflib.py +0 -247
- cognite/neat/legacy/__init__.py +0 -0
- cognite/neat/legacy/graph/__init__.py +0 -3
- cognite/neat/legacy/graph/examples/Knowledge-Graph-Nordic44-dirty.xml +0 -20182
- cognite/neat/legacy/graph/examples/Knowledge-Graph-Nordic44.xml +0 -20163
- cognite/neat/legacy/graph/examples/__init__.py +0 -10
- cognite/neat/legacy/graph/examples/skos-capturing-sheet-wind-topics.xlsx +0 -0
- cognite/neat/legacy/graph/exceptions.py +0 -90
- cognite/neat/legacy/graph/extractors/__init__.py +0 -6
- cognite/neat/legacy/graph/extractors/_base.py +0 -14
- cognite/neat/legacy/graph/extractors/_dexpi.py +0 -44
- cognite/neat/legacy/graph/extractors/_graph_capturing_sheet.py +0 -403
- cognite/neat/legacy/graph/extractors/_mock_graph_generator.py +0 -361
- cognite/neat/legacy/graph/loaders/__init__.py +0 -23
- cognite/neat/legacy/graph/loaders/_asset_loader.py +0 -511
- cognite/neat/legacy/graph/loaders/_base.py +0 -67
- cognite/neat/legacy/graph/loaders/_exceptions.py +0 -85
- cognite/neat/legacy/graph/loaders/core/__init__.py +0 -0
- cognite/neat/legacy/graph/loaders/core/labels.py +0 -58
- cognite/neat/legacy/graph/loaders/core/models.py +0 -136
- cognite/neat/legacy/graph/loaders/core/rdf_to_assets.py +0 -1046
- cognite/neat/legacy/graph/loaders/core/rdf_to_relationships.py +0 -559
- cognite/neat/legacy/graph/loaders/rdf_to_dms.py +0 -309
- cognite/neat/legacy/graph/loaders/validator.py +0 -87
- cognite/neat/legacy/graph/models.py +0 -6
- cognite/neat/legacy/graph/stores/__init__.py +0 -13
- cognite/neat/legacy/graph/stores/_base.py +0 -400
- cognite/neat/legacy/graph/stores/_graphdb_store.py +0 -52
- cognite/neat/legacy/graph/stores/_memory_store.py +0 -43
- cognite/neat/legacy/graph/stores/_oxigraph_store.py +0 -151
- cognite/neat/legacy/graph/stores/_oxrdflib.py +0 -247
- cognite/neat/legacy/graph/stores/_rdf_to_graph.py +0 -42
- cognite/neat/legacy/graph/transformations/__init__.py +0 -0
- cognite/neat/legacy/graph/transformations/entity_matcher.py +0 -101
- cognite/neat/legacy/graph/transformations/query_generator/__init__.py +0 -3
- cognite/neat/legacy/graph/transformations/query_generator/sparql.py +0 -575
- cognite/neat/legacy/graph/transformations/transformer.py +0 -322
- cognite/neat/legacy/rules/__init__.py +0 -0
- cognite/neat/legacy/rules/analysis.py +0 -231
- cognite/neat/legacy/rules/examples/Rules-Nordic44-to-graphql.xlsx +0 -0
- cognite/neat/legacy/rules/examples/Rules-Nordic44.xlsx +0 -0
- cognite/neat/legacy/rules/examples/__init__.py +0 -18
- cognite/neat/legacy/rules/examples/power-grid-containers.yaml +0 -124
- cognite/neat/legacy/rules/examples/power-grid-example.xlsx +0 -0
- cognite/neat/legacy/rules/examples/power-grid-model.yaml +0 -224
- cognite/neat/legacy/rules/examples/rules-template.xlsx +0 -0
- cognite/neat/legacy/rules/examples/sheet2cdf-transformation-rules.xlsx +0 -0
- cognite/neat/legacy/rules/examples/skos-rules.xlsx +0 -0
- cognite/neat/legacy/rules/examples/source-to-solution-mapping-rules.xlsx +0 -0
- cognite/neat/legacy/rules/examples/wind-energy.owl +0 -1511
- cognite/neat/legacy/rules/exceptions.py +0 -2972
- cognite/neat/legacy/rules/exporters/__init__.py +0 -20
- cognite/neat/legacy/rules/exporters/_base.py +0 -45
- cognite/neat/legacy/rules/exporters/_core/__init__.py +0 -5
- cognite/neat/legacy/rules/exporters/_core/rules2labels.py +0 -24
- cognite/neat/legacy/rules/exporters/_rules2dms.py +0 -885
- cognite/neat/legacy/rules/exporters/_rules2excel.py +0 -213
- cognite/neat/legacy/rules/exporters/_rules2graphql.py +0 -183
- cognite/neat/legacy/rules/exporters/_rules2ontology.py +0 -524
- cognite/neat/legacy/rules/exporters/_rules2pydantic_models.py +0 -748
- cognite/neat/legacy/rules/exporters/_rules2rules.py +0 -105
- cognite/neat/legacy/rules/exporters/_rules2triples.py +0 -38
- cognite/neat/legacy/rules/exporters/_validation.py +0 -146
- cognite/neat/legacy/rules/importers/__init__.py +0 -22
- cognite/neat/legacy/rules/importers/_base.py +0 -66
- cognite/neat/legacy/rules/importers/_dict2rules.py +0 -158
- cognite/neat/legacy/rules/importers/_dms2rules.py +0 -194
- cognite/neat/legacy/rules/importers/_graph2rules.py +0 -308
- cognite/neat/legacy/rules/importers/_json2rules.py +0 -39
- cognite/neat/legacy/rules/importers/_owl2rules/__init__.py +0 -3
- cognite/neat/legacy/rules/importers/_owl2rules/_owl2classes.py +0 -239
- cognite/neat/legacy/rules/importers/_owl2rules/_owl2metadata.py +0 -260
- cognite/neat/legacy/rules/importers/_owl2rules/_owl2properties.py +0 -217
- cognite/neat/legacy/rules/importers/_owl2rules/_owl2rules.py +0 -290
- cognite/neat/legacy/rules/importers/_spreadsheet2rules.py +0 -45
- cognite/neat/legacy/rules/importers/_xsd2rules.py +0 -20
- cognite/neat/legacy/rules/importers/_yaml2rules.py +0 -39
- cognite/neat/legacy/rules/models/__init__.py +0 -5
- cognite/neat/legacy/rules/models/_base.py +0 -151
- cognite/neat/legacy/rules/models/raw_rules.py +0 -316
- cognite/neat/legacy/rules/models/rdfpath.py +0 -237
- cognite/neat/legacy/rules/models/rules.py +0 -1289
- cognite/neat/legacy/rules/models/tables.py +0 -9
- cognite/neat/legacy/rules/models/value_types.py +0 -118
- cognite/neat/legacy/workflows/examples/Export_DMS/workflow.yaml +0 -89
- cognite/neat/legacy/workflows/examples/Export_Rules_to_Ontology/workflow.yaml +0 -152
- cognite/neat/legacy/workflows/examples/Extract_DEXPI_Graph_and_Export_Rules/workflow.yaml +0 -139
- cognite/neat/legacy/workflows/examples/Extract_RDF_Graph_and_Generate_Assets/workflow.yaml +0 -270
- cognite/neat/legacy/workflows/examples/Import_DMS/workflow.yaml +0 -65
- cognite/neat/legacy/workflows/examples/Ontology_to_Data_Model/workflow.yaml +0 -116
- cognite/neat/legacy/workflows/examples/Validate_Rules/workflow.yaml +0 -67
- cognite/neat/legacy/workflows/examples/Validate_Solution_Model/workflow.yaml +0 -64
- cognite/neat/legacy/workflows/examples/Visualize_Data_Model_Using_Mock_Graph/workflow.yaml +0 -95
- cognite/neat/legacy/workflows/examples/Visualize_Semantic_Data_Model/workflow.yaml +0 -111
- cognite/neat/rules/exceptions.py +0 -2972
- cognite/neat/rules/models/_types/_base.py +0 -16
- cognite/neat/workflows/examples/Export_Rules_to_Ontology/workflow.yaml +0 -152
- cognite/neat/workflows/examples/Extract_DEXPI_Graph_and_Export_Rules/workflow.yaml +0 -139
- cognite/neat/workflows/examples/Extract_RDF_Graph_and_Generate_Assets/workflow.yaml +0 -270
- cognite/neat/workflows/examples/Ontology_to_Data_Model/workflow.yaml +0 -116
- cognite/neat/workflows/migration/__init__.py +0 -0
- cognite/neat/workflows/migration/steps.py +0 -91
- cognite/neat/workflows/migration/wf_manifests.py +0 -33
- cognite/neat/workflows/steps/lib/legacy/__init__.py +0 -7
- cognite/neat/workflows/steps/lib/legacy/graph_contextualization.py +0 -82
- cognite/neat/workflows/steps/lib/legacy/graph_extractor.py +0 -746
- cognite/neat/workflows/steps/lib/legacy/graph_loader.py +0 -606
- cognite/neat/workflows/steps/lib/legacy/graph_store.py +0 -307
- cognite/neat/workflows/steps/lib/legacy/graph_transformer.py +0 -58
- cognite/neat/workflows/steps/lib/legacy/rules_exporter.py +0 -511
- cognite/neat/workflows/steps/lib/legacy/rules_importer.py +0 -612
- cognite_neat-0.87.6.dist-info/RECORD +0 -319
- {cognite_neat-0.87.6.dist-info → cognite_neat-0.88.1.dist-info}/LICENSE +0 -0
- {cognite_neat-0.87.6.dist-info → cognite_neat-0.88.1.dist-info}/WHEEL +0 -0
- {cognite_neat-0.87.6.dist-info → cognite_neat-0.88.1.dist-info}/entry_points.txt +0 -0
|
@@ -1,247 +0,0 @@
|
|
|
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}")
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
from pathlib import Path
|
|
2
|
-
|
|
3
|
-
from rdflib import Graph, Namespace
|
|
4
|
-
|
|
5
|
-
from cognite.neat.constants import get_default_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] | None = None,
|
|
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 internal set of 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
|
-
prefixes = prefixes if prefixes else get_default_prefixes()
|
|
29
|
-
|
|
30
|
-
if filepath.is_file():
|
|
31
|
-
graph.parse(filepath, publicID=base_namespace)
|
|
32
|
-
else:
|
|
33
|
-
for filename in filepath.iterdir():
|
|
34
|
-
if filename.is_file():
|
|
35
|
-
graph.parse(filename, publicID=base_namespace)
|
|
36
|
-
if base_prefix and base_namespace:
|
|
37
|
-
graph.bind(base_prefix, base_namespace)
|
|
38
|
-
if prefixes:
|
|
39
|
-
for prefix, namespace in prefixes.items():
|
|
40
|
-
graph.bind(prefix, namespace)
|
|
41
|
-
|
|
42
|
-
return graph
|
|
File without changes
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import logging
|
|
2
|
-
from typing import cast
|
|
3
|
-
|
|
4
|
-
from rdflib import Literal, URIRef
|
|
5
|
-
|
|
6
|
-
from cognite.neat.legacy.graph.stores import NeatGraphStoreBase
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
def simple_entity_matcher(
|
|
10
|
-
graph_store: NeatGraphStoreBase,
|
|
11
|
-
source_class: str,
|
|
12
|
-
source_property: str,
|
|
13
|
-
source_value_type: str = "single_value_str",
|
|
14
|
-
target_class: str | None = None,
|
|
15
|
-
target_property: str | None = None,
|
|
16
|
-
relationship_name: str = "link",
|
|
17
|
-
link_direction: str = "target_to_source", # source_to_target, bidirectional
|
|
18
|
-
matching_method: str = "regexp", # exact_match, similarity
|
|
19
|
-
link_namespace: str = "http://purl.org/cognite/neat#",
|
|
20
|
-
) -> int:
|
|
21
|
-
"""simple_entity_matcher performs a simple entity matching between two classes in the graph using
|
|
22
|
-
either exact match or regular expression matching.
|
|
23
|
-
The matching is performed on the values of the source_property and target_property.
|
|
24
|
-
The matching is performed in the direction specified by link_direction.
|
|
25
|
-
The matching_method can be either exact_match or regexp.
|
|
26
|
-
If the source_value_type is multi_value_str, the values are split on comma and added as separate triples.
|
|
27
|
-
Args:
|
|
28
|
-
graph_store (NeatGraphStoreBase): The graph store to perform the matching on and add the links to
|
|
29
|
-
source_class (str): The class of the source entities
|
|
30
|
-
source_property (str): The property of the source entities to match on
|
|
31
|
-
source_value_type (str, optional): The value type of the source property. Defaults to "single_value_str".
|
|
32
|
-
target_class (str | None, optional): The class of the target entities. Defaults to None.
|
|
33
|
-
target_property (str | None, optional): The property of the target entities to match on. Defaults to None.
|
|
34
|
-
relationship_name (str, optional): The name of the relationship to add between the matched entities.
|
|
35
|
-
link_direction (str, optional): The direction of the relationship. Defaults to "target_to_source".
|
|
36
|
-
matching_method (str, optional): The matching method to use. Defaults to "regexp".
|
|
37
|
-
Returns:
|
|
38
|
-
int: The number of new links added to the graph
|
|
39
|
-
"""
|
|
40
|
-
if source_value_type == "multi_value_str":
|
|
41
|
-
# Split the values and add them as separate triples
|
|
42
|
-
query = f"""
|
|
43
|
-
SELECT DISTINCT ?source ?source_value
|
|
44
|
-
WHERE {{
|
|
45
|
-
?source rdf:type neat:{source_class} .
|
|
46
|
-
?source neat:{source_property} ?source_value .
|
|
47
|
-
}}
|
|
48
|
-
"""
|
|
49
|
-
triples_to_remove = []
|
|
50
|
-
r1 = cast(tuple, graph_store.query(query))
|
|
51
|
-
result = list(r1)
|
|
52
|
-
for row in result:
|
|
53
|
-
val_split = row[1].split(",")
|
|
54
|
-
if len(val_split) > 1:
|
|
55
|
-
triples_to_remove.append((row[0], URIRef(link_namespace + source_property), row[1]))
|
|
56
|
-
for val in val_split:
|
|
57
|
-
graph_store.graph.add((row[0], URIRef(link_namespace + source_property), Literal(val)))
|
|
58
|
-
|
|
59
|
-
for triple in triples_to_remove:
|
|
60
|
-
graph_store.graph.remove(triple)
|
|
61
|
-
|
|
62
|
-
graph_store.graph.commit()
|
|
63
|
-
query = ""
|
|
64
|
-
if matching_method == "exact_match":
|
|
65
|
-
query = f"""
|
|
66
|
-
SELECT DISTINCT ?source ?target
|
|
67
|
-
WHERE {{
|
|
68
|
-
?source rdf:type neat:{source_class} .
|
|
69
|
-
?source neat:{source_property} ?source_value .
|
|
70
|
-
?target rdf:type neat:{target_class} .
|
|
71
|
-
?target neat:{target_property} ?target_value .
|
|
72
|
-
FILTER (?source_value = ?target_value)
|
|
73
|
-
}}
|
|
74
|
-
"""
|
|
75
|
-
elif matching_method == "regexp":
|
|
76
|
-
query = f"""
|
|
77
|
-
SELECT DISTINCT ?source ?target
|
|
78
|
-
WHERE {{
|
|
79
|
-
?source rdf:type neat:{source_class} .
|
|
80
|
-
?source neat:{source_property} ?source_value .
|
|
81
|
-
?target rdf:type neat:{target_class} .
|
|
82
|
-
?target neat:{target_property} ?target_value .
|
|
83
|
-
FILTER regex(?target_value,?source_value, "i")
|
|
84
|
-
}}
|
|
85
|
-
"""
|
|
86
|
-
else:
|
|
87
|
-
logging.error(f"Unknown matching method {matching_method}")
|
|
88
|
-
return 0
|
|
89
|
-
logging.debug(f"Running matcher query {query}")
|
|
90
|
-
r1 = cast(tuple, graph_store.query(query))
|
|
91
|
-
result = list(r1)
|
|
92
|
-
logging.debug(f"Identified {len(result)} matches from the graph")
|
|
93
|
-
new_links_counter = 0
|
|
94
|
-
for row in result:
|
|
95
|
-
new_links_counter += 1
|
|
96
|
-
if link_direction == "target_to_source":
|
|
97
|
-
graph_store.graph.add((row[1], URIRef(link_namespace + relationship_name), row[0]))
|
|
98
|
-
else:
|
|
99
|
-
graph_store.graph.add((row[0], URIRef(link_namespace + relationship_name), row[1]))
|
|
100
|
-
|
|
101
|
-
return new_links_counter
|