Skip to content

Commit 84dc1d2

Browse files
committed
chore: add existing files into new structure
1 parent 9f6f716 commit 84dc1d2

9 files changed

Lines changed: 2210 additions & 1 deletion

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,4 @@ venv.bak/
102102

103103
# mypy
104104
.mypy_cache/
105+
.idea

‎.idea/vcs.xml‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎README.md‎

Lines changed: 511 additions & 1 deletion
Large diffs are not rendered by default.

‎knora/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
name = "knora"

‎knora/create_ontology.py‎

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

0 commit comments

Comments
 (0)