Skip to content

Commit ff36bc1

Browse files
author
irinaschubert
authored
feat: add support for external ontologies (dev-512) (#170)
* update documentation * add more external ontologies * code cleanup * add test data * clean up code * update author information * update bazel version * rename variables in get method * make group optional for dsp-tools get * allow IRIs to not be reduced * fix merge conflict * code changes after review * Update docstring for createDefinitionFileObj
1 parent 6c44688 commit ff36bc1

12 files changed

Lines changed: 158 additions & 144 deletions

‎docs/dsp-tools-create-ontologies.md‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -891,19 +891,19 @@ Example for a resource definition:
891891

892892
## Referencing Ontologies
893893

894-
For several fields, such as `super` in both `resources` and `properties` or `propname` in `cardinalities`,
895-
it is necessary to reference entities that are defined elsewhere. The following cases are possible.
894+
For several fields, such as `super` in both `resources` and `properties` or `propname` in `cardinalities`
895+
it is necessary to reference entities that are defined elsewhere. The following cases are possible:
896896

897-
- DSP-API internals: These must be written *without* leading colon and should not be a fully qualified IRI.
897+
- DSP API internals: They are referenced as such and do not have a leading colon.
898898
E.g. `Resource`, `DocumentRepresentation` or `hasValue`
899-
- An external ontology: The ontology must be defined in the [prefixes](dsp-tools-create.md#prefixes-object)
900-
This prefix should be used for referencing the ontology.
899+
- An external ontology: The ontology must be defined in the [prefixes](dsp-tools-create.md#prefixes-object) section.
900+
The prefix can then be used for referencing the ontology.
901901
E.g. `foaf:familyName` or `sdo:Organization`
902902
- The current ontology: Within an ontology definition, references can be made by prepending a colon without a prefix.
903-
E.g. `:hasName`
904-
Optionally, an explicit prefix can be used, in this case the ontology must be added to the
905-
[prefixes](dsp-tools-create.md#prefixes-object) and the prefix must be identical to the ontology's `name`.
903+
E.g. `:hasName`
904+
Optionally, an explicit prefix can be used. In this case the ontology must be added to the
905+
[prefixes](dsp-tools-create.md#prefixes-object) section and the prefix must be identical to the ontology's `name`.
906906
- A different ontology defined in the same file: Within one data model file, multiple ontologies can be defined.
907907
These will be created in the exact order they appear in the `ontologies` array. Once an ontology has been created,
908-
it can be referenced by the following ontologies via its name: `first-onto:hasName`. It is not necessary to add
908+
it can be referenced by the following ontologies by its name, e.g. `first-onto:hasName`. It is not necessary to add
909909
`first-onto` to the prefixes.

‎docs/dsp-tools-create.md‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ A complete data model definition for DSP looks like this:
5252

5353
`"prefixes": { "prefix": "<iri>", ...}`
5454

55-
The `prefixes` object contains the prefixes of external ontologies that are used in the current project. All prefixes
56-
are composed of the actual prefix and an IRI. The prefix is used as an abbreviation so one does not have to write the
57-
full qualified IRI each time it is used. So, instead of writing a property called "familyname" as
58-
`http://xmlns.com/foaf/0.1/familyName` one can simply use `foaf:familyName`.
55+
The `prefixes` object contains the prefixes of external ontologies that are used in the current file. All prefixes
56+
are composed of the prefix and a URI. The prefix is used as namespace so one does not have to write the
57+
fully qualified name of the referenced object each time it is used. Instead of writing a property called "familyName"
58+
as `http://xmlns.com/foaf/0.1/familyName` one can simply write `foaf:familyName`.
5959

6060
```json
6161
{
@@ -66,8 +66,9 @@ full qualified IRI each time it is used. So, instead of writing a property calle
6666
}
6767
```
6868

69-
It is not necessary to define prefixes for the ontologies that are defined in this file. Ontologies in the same
70-
file can refer to each other via their name. See also [here](./dsp-tools-create-ontologies.md#referencing-ontologies).
69+
It is not necessary to define prefixes for the ontologies that are defined in the same file. Ontologies in the same
70+
file can be referenced by their name. See [this section](./dsp-tools-create-ontologies.md#referencing-ontologies) for
71+
more information about referencing ontologies.
7172

7273
### "$schema" object
7374

‎knora/dsplib/models/connection.py‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
import re
3-
from typing import Optional, Union
3+
from typing import Optional, Union, Any
44

55
import requests
66
from pystrict import strict
@@ -159,7 +159,7 @@ def post(self, path: str, jsondata: Optional[str] = None):
159159
result = req.json()
160160
return result
161161

162-
def get(self, path: str, headers: Optional[dict[str, str]] = None):
162+
def get(self, path: str, headers: Optional[dict[str, str]] = None) -> dict[str, Any]:
163163
"""
164164
Get data from a server using a HTTP GET request
165165
:param path: Path of RESTful route
@@ -169,22 +169,21 @@ def get(self, path: str, headers: Optional[dict[str, str]] = None):
169169

170170
if path[0] != '/':
171171
path = '/' + path
172-
if self._token is None:
173-
if headers is None:
174-
req = requests.get(self._server + path)
172+
if not self._token:
173+
if not headers:
174+
response = requests.get(self._server + path)
175175
else:
176-
req = requests.get(self._server + path, headers)
176+
response = requests.get(self._server + path, headers)
177177
else:
178-
if headers is None:
179-
req = requests.get(self._server + path,
180-
headers={'Authorization': 'Bearer ' + self._token})
178+
if not headers:
179+
response = requests.get(self._server + path, headers={'Authorization': 'Bearer ' + self._token})
181180
else:
182181
headers['Authorization'] = 'Bearer ' + self._token
183-
req = requests.get(self._server + path, headers)
182+
response = requests.get(self._server + path, headers)
184183

185-
self.on_api_error(req)
186-
result = req.json()
187-
return result
184+
self.on_api_error(response)
185+
json_response = response.json()
186+
return json_response
188187

189188
def put(self, path: str, jsondata: Optional[str] = None, content_type: str = 'application/json'):
190189
"""

‎knora/dsplib/models/group.py‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ def has_changed(self) -> bool:
160160

161161
@classmethod
162162
def fromJsonObj(cls, con: Connection, json_obj: Any):
163-
id = json_obj.get('id')
164-
if id is None:
163+
group_id = json_obj.get('id')
164+
if group_id is None:
165165
raise BaseError('Group "id" is missing')
166166
name = json_obj.get('name')
167167
if name is None:
@@ -181,7 +181,7 @@ def fromJsonObj(cls, con: Connection, json_obj: Any):
181181
raise BaseError("Status is missing")
182182
return cls(con=con,
183183
name=name,
184-
id=id,
184+
id=group_id,
185185
descriptions=descriptions,
186186
project=project,
187187
selfjoin=selfjoin,
@@ -249,16 +249,14 @@ def getAllGroups(con: Connection) -> Optional[list[Group]]:
249249
return None
250250

251251
@staticmethod
252-
def getAllGroupsForProject(con: Connection, proj_shortcode: str) -> list[Group]:
253-
result = con.get(Group.ROUTE)
254-
if 'groups' not in result:
255-
raise BaseError("Request got no groups!")
256-
all_groups = result["groups"]
257-
project_groups = []
258-
for group in all_groups:
259-
if group["project"]["id"] == "http://rdfh.ch/projects/" + proj_shortcode:
260-
project_groups.append(group)
261-
return list(map(lambda a: Group.fromJsonObj(con, a), project_groups))
252+
def getAllGroupsForProject(con: Connection, proj_shortcode: str) -> Optional[list[Group]]:
253+
all_groups: Optional[list[Group]] = Group.getAllGroups(con)
254+
if all_groups:
255+
project_groups = []
256+
for group in all_groups:
257+
if group.project == "http://rdfh.ch/projects/" + proj_shortcode:
258+
project_groups.append(group)
259+
return project_groups
262260

263261
def createDefinitionFileObj(self):
264262
group = {

0 commit comments

Comments
 (0)