/
create_ontology.py
244 lines (209 loc) · 9.39 KB
/
create_ontology.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import os
from typing import List, Set, Dict, Tuple, Optional
from pprint import pprint
import argparse
import json
from jsonschema import validate
from knora import KnoraError, Knora
import sys
def program(args):
# parse the arguments of the command line
parser = argparse.ArgumentParser()
parser.add_argument("ontofile", help="path to ontology file")
parser.add_argument("-s", "--server", type=str, default="http://0.0.0.0:3333", help="URL of the Knora server")
parser.add_argument("-u", "--user", default="root@example.com", help="Username for Knora")
parser.add_argument("-p", "--password", default="test", help="The password for login")
parser.add_argument("-v", "--validate", action='store_true', help="Do only validation of JSON, no upload of the ontology")
parser.add_argument("-l", "--lists", action='store_true', help="Only create the lists")
args = parser.parse_args(args)
current_dir = os.path.dirname(os.path.realpath(__file__))
print(current_dir)
# let's read the schema for the ontology definition
if args.lists:
with open(os.path.join(current_dir, 'knora-schema-lists.json')) as s:
schema = json.load(s)
else:
with open(os.path.join(current_dir, 'knora-schema.json')) as s:
schema = json.load(s)
# read the ontology definition
with open(args.ontofile) as f:
ontology = json.load(f)
# validate the ontology definition in order to be sure that it is correct
validate(ontology, schema)
print("Ontology is syntactically correct and passed validation!")
if args.validate:
exit(0)
# create the knora connection object
con = Knora(args.server, ontology.get("prefixes"))
con.login(args.user, args.password)
# bulk_templ = con.create_schema(ontology["project"]["shortcode"], ontology["project"]["ontology"]["name"])
if not args.lists:
# create or update the project
try:
project = con.get_project(ontology["project"]["shortcode"])
except KnoraError as err:
proj_iri = con.create_project(
shortcode=ontology["project"]["shortcode"],
shortname=ontology["project"]["shortname"],
longname=ontology["project"]["longname"],
descriptions=ontology["project"].get("descriptions"),
keywords=ontology["project"].get("keywords")
)
print("New project created: IRI: " + proj_iri)
else:
print("Updating existing project!")
print("Old project data:")
pprint(project)
proj_iri = con.update_project(
shortcode=ontology["project"]["shortcode"],
shortname=ontology["project"]["shortname"],
longname=ontology["project"]["longname"],
descriptions=ontology["project"].get("descriptions"),
keywords=ontology["project"].get("keywords")
)
project = con.get_project(ontology["project"]["shortcode"])
print("New project data:")
pprint(project)
else:
project = con.get_project(ontology["project"]["shortcode"])
proj_iri = project["id"]
# now let's create the lists
lists = ontology["project"].get('lists')
listrootnodes = {}
if lists is not None:
for rootnode in lists:
rootnode_iri = con.create_list_node(
project_iri=proj_iri,
name=rootnode['name'],
labels=rootnode['labels'],
comments=rootnode.get('comments')
)
listnodes = list_creator(con, proj_iri, rootnode_iri, rootnode_iri, rootnode['nodes'])
listrootnodes[rootnode['name']] = {
"id": rootnode_iri,
"nodes": listnodes
}
with open('lists.json', 'w', encoding="utf-8") as fp:
json.dump(listrootnodes, fp, indent=3, sort_keys=True)
if args.lists:
print("The definitions of the node-id's can be found in \"lists.json\"!")
exit(0)
# now we add the users if existing
users = ontology["project"].get('users')
if users is not None:
for user in users:
user_iri = con.create_user(username=user["username"],
email=user["email"],
givenName=user["givenName"],
familyName=user["familyName"],
password="password",
lang=user["lang"] if user.get("lang") is not None else "en")
con.add_user_to_project(user_iri, proj_iri)
# now we start creating the ontology
# first we assemble the ontology IRI
onto_iri = args.server + "/ontology/" + ontology["project"]["shortcode"]\
+ "/" + ontology["project"]["ontology"]["name"] + "/v2"
# test, if the ontolgy already exists. if so, let's delete it!
ontos = con.get_project_ontologies(ontology["project"]["shortcode"])
if ontos is not None:
for onto in ontos:
if onto['iri'] == onto_iri:
con.delete_ontology(onto_iri, onto['moddate'])
onto_data = con.create_ontology(
onto_name=ontology["project"]["ontology"]["name"],
project_iri=proj_iri,
label=ontology["project"]["ontology"]["label"]
)
onto_iri = onto_data['onto_iri']
last_onto_date = onto_data['last_onto_date']
# let's create the resources
resource_ids = {}
for resource in ontology["project"]["ontology"]["resources"]:
result = con.create_res_class(
onto_iri=onto_iri,
onto_name=ontology["project"]["ontology"]["name"],
last_onto_date=last_onto_date,
class_name=resource["name"],
super_class=resource["super"] if ':' in resource["super"] else "knora-api:" + resource["super"],
labels=resource["labels"]
)
last_onto_date = result["last_onto_date"]
resource_ids[resource["name"]] = result["class_iri"]
pprint(resource_ids)
# let's create the properties
property_ids = {}
for resource in ontology["project"]["ontology"]["resources"]:
for prop in resource["properties"]:
guiattrs = prop.get("gui_attributes")
if guiattrs is not None:
new_guiattrs = []
for guiattr in guiattrs:
parts = guiattr.split("=")
if parts[0] == "hlist":
new_guiattrs.append("hlist=<" + listrootnodes[parts[1]]["id"] + ">")
else:
new_guiattrs.append(guiattr)
guiattrs = new_guiattrs
if prop.get("super") is not None:
super_props = list(map(lambda a: a if ':' in a else "knora-api:" + a, prop["super"]))
else:
super_props = ["knora-api:hasValue"]
if prop.get("object") is not None:
object = prop["object"] if ':' in prop["object"] else "knora-api:" + prop["object"]
else:
object = None
if prop.get("subject") is not None:
psubject = prop["subject"]
else:
psubject = ontology["project"]["ontology"]["name"] + ':' + resource["name"]
result = con.create_property(
onto_iri=onto_iri,
onto_name=ontology["project"]["ontology"]["name"],
last_onto_date=last_onto_date,
prop_name=prop["name"],
super_props=super_props,
labels=prop["labels"],
gui_element="salsah-gui:" + prop["gui_element"],
gui_attributes=guiattrs,
subject=psubject,
object=object,
comments=prop.get("comments")
)
last_onto_date = result["last_onto_date"]
property_ids[prop["name"]] = result['prop_iri']
# add the cardinalities
for resource in ontology["project"]["ontology"]["resources"]:
for prop in resource["properties"]:
print("=======>" + prop["name"] + "...")
result = con.create_cardinality(
onto_iri=onto_iri,
onto_name=ontology["project"]["ontology"]["name"],
last_onto_date=last_onto_date,
class_iri=ontology["project"]["ontology"]["name"] + ':' + resource["name"],
prop_iri=ontology["project"]["ontology"]["name"] + ':' + prop["name"],
occurrence=prop["cardinality"]
)
last_onto_date = result["last_onto_date"]
con = None # force logout by deleting the connection object.
def list_creator(con: Knora, proj_iri: str, list_iri: str, parent_iri: str, nodes: List[dict]):
nodelist = []
for node in nodes:
node_id = con.create_list_node(
name=node["name"],
project_iri=proj_iri,
labels=node["labels"],
comments=node.get("comments"),
parent_iri=parent_iri
)
if node.get('nodes') is not None:
subnodelist = list_creator(con, proj_iri, list_iri, node_id, node['nodes'])
nodelist.append({node["name"]: {"id": node_id, 'nodes': subnodelist}})
else:
nodelist.append({node["name"]: {"id": node_id}})
return nodelist
def main():
program(sys.argv[1:])
if __name__ == '__main__':
print(sys.argv)
print(sys.argv[1:])
program(sys.argv[1:])