Skip to content

Commit 63e2608

Browse files
committed
update import
1 parent 1c69fa2 commit 63e2608

File tree

1 file changed

+56
-28
lines changed

1 file changed

+56
-28
lines changed

examples/ontology_import.py

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,57 @@
11
import json
2-
import os
2+
3+
# import os
34
import re
45
from uuid import UUID
56

7+
import mwclient
68
from pyld import jsonld
79
from rdflib import Graph
810

911
import osw.model.entity as model
12+
13+
# from osw.auth import CredentialManager
1014
from osw.core import OSW
1115
from osw.wtsite import WtSite
1216

1317
# create/update the password file under examples/accounts.pwd.yaml
14-
pwd_file_path = os.path.join(
15-
os.path.dirname(os.path.abspath(__file__)), "accounts.pwd.yaml"
16-
)
17-
wtsite = WtSite.from_domain("onto-wiki.eu", pwd_file_path)
18+
# pwd_file_path = os.path.join(
19+
# os.path.dirname(os.path.abspath(__file__)), "accounts.pwd.yaml"
20+
# )
21+
# cm = CredentialManager(cred_filepath=pwd_file_path)
22+
# wtsite = WtSite(WtSite.WtSiteConfig(
23+
# iri="http://localhost:18081",
24+
# cred_mngr=cm
25+
# ))
26+
27+
# or use a hardocded login
28+
site = mwclient.Site(scheme="http", host="localhost:18081", path="/w/")
29+
site.login("Admin", "change_me123123")
30+
wtsite = WtSite(WtSite.WtSiteLegacyConfig(site=site))
31+
1832
osw = OSW(site=wtsite)
1933

20-
# load the EmmoTerm schema => run this code only once
21-
# osw.fetch_schema(
22-
# osw.FetchSchemaParam(
23-
# schema_title="Category:OSW57beed5e1294434ba77bb6516e461456", mode="replace" # EmmoTerm
24-
# )
25-
# )
34+
35+
# load the EmmoTerm schema => run the script a second time after the schema was loaded
36+
try:
37+
model.EmmoTerm
38+
except AttributeError:
39+
# name 'EmmoTerm' is not defined
40+
osw.fetch_schema(
41+
osw.FetchSchemaParam(
42+
schema_title="Category:OSW57beed5e1294434ba77bb6516e461456",
43+
mode="replace", # EmmoTerm
44+
)
45+
)
2646

2747
# load the ontology
2848
g = Graph()
29-
# g.parse("http://www.w3.org/People/Berners-Lee/card")
30-
# g.parse("https://raw.githubusercontent.com/emmo-repo/domain-battery/master/battery.ttl", format="n3")
31-
g.parse(r"BVCO_inferred.ttl")
49+
g.parse(
50+
"https://raw.githubusercontent.com/emmo-repo/domain-battery/master/battery.ttl",
51+
format="n3",
52+
)
53+
# g.parse("https://github.com/Battery-Value-Chain-Ontology/ontology/releases/download/v0.3.0/BVCO_inferred.ttl", format="n3")
54+
# g.parse(r"BVCO_inferred.ttl")
3255

3356
# convert to json-ld dict
3457
g = json.loads(g.serialize(format="json-ld", auto_compact=True))
@@ -41,15 +64,15 @@
4164
"xsd": "http://www.w3.org/2001/XMLSchema#",
4265
"skos": "http://www.w3.org/2004/02/skos/core#",
4366
"dc": "http://purl.org/dc/terms/",
44-
# "emmo": "http://emmo.info/emmo#", #keep values with full uri
67+
"emmo": "http://emmo.info/emmo#", # keep values with full uri
4568
"uri": {"@id": "@id"},
4669
"rdf_type": {"@id": "@type"},
4770
# "label": "rdfs:label",
4871
"label": {"@id": "skos:prefLabel"},
4972
"altLabel": {"@id": "skos:altLabel"},
5073
"text": {"@id": "@value"},
5174
"lang": {"@id": "@language"},
52-
"subClassOf": {"@id": "rdfs:subClassOf", "@type": "@id"},
75+
"subclass_of": {"@id": "rdfs:subClassOf", "@type": "@id"},
5376
"source": "dc:source",
5477
"disjointUnionOf": "owl:disjointUnionOf",
5578
"disjointWith": "owl:disjointWith",
@@ -88,10 +111,10 @@
88111
"altLabel",
89112
"comment",
90113
"description",
91-
"subClassOf",
114+
"subclass_of",
92115
]
93-
map_uuid_uri = []
94-
remove_unnamed = ["subClassOf"] # , 'equivalentClass']
116+
map_uuid_uri = ["subclass_of"]
117+
remove_unnamed = ["subclass_of"] # , 'equivalentClass']
95118

96119
# postprocess json-ld
97120
for node in compacted["@graph"]:
@@ -110,25 +133,29 @@
110133
for key in ensure_array:
111134
if key in node and not isinstance(node[key], list):
112135
node[key] = [node[key]]
136+
for key in remove_unnamed:
137+
if key in node:
138+
if isinstance(node[key], list):
139+
node[key] = [value for value in node[key] if not value.startswith("_:")]
140+
elif isinstance(node[key], str) and node[key].startswith("_:"):
141+
del node[key]
113142
for key in map_uuid_uri:
114143
if key in node:
115144
if isinstance(node[key], list):
116145
for i, val in enumerate(node[key]):
117146
node[key][i] = "Category:OSW" + str(
118147
UUID(re.sub(r"[^A-Fa-f0-9]", "", node[key][i])[-32:])
119-
)
148+
).replace("-", "")
120149
if isinstance(node[key], str):
121150
node[key][i] = "Category:OSW" + str(
122151
UUID(re.sub(r"[^A-Fa-f0-9]", "", node[key][i])[-32:])
123-
)
124-
for key in remove_unnamed:
125-
if key in node:
126-
if isinstance(node[key], list):
127-
node[key] = [value for value in node[key] if not value.startswith("_:")]
128-
elif isinstance(node[key], str) and node[key].startswith("_:"):
129-
del node[key]
152+
).replace("-", "")
130153

131-
if "rdf_type" in node and node["rdf_type"] == "owl:Class":
154+
if (
155+
"rdf_type" in node
156+
and node["rdf_type"] == "owl:Class"
157+
and not node["uri"].startswith("_:")
158+
):
132159
node["uuid"] = str(UUID(re.sub(r"[^A-Fa-f0-9]", "", node["uri"])[-32:]))
133160

134161
if "prefLabel" in node:
@@ -205,6 +232,7 @@
205232
)
206233

207234
ontologies = [emmo, battinfo, electrochemistry, periodictable, gpo, bvco]
235+
# ontologies = [battinfo]
208236

209237
# import ontologies
210238
osw.import_ontology(OSW.ImportOntologyParam(ontologies=ontologies, entities=entities))

0 commit comments

Comments
 (0)