Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
TOAR-II FastAPI
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
esde
toar-data
TOAR-II FastAPI
Commits
61a8c244
Commit
61a8c244
authored
4 years ago
by
Sabine Schröder
Browse files
Options
Downloads
Patches
Plain Diff
enabled saving onthologies in different formats
parent
6a595f00
No related branches found
No related tags found
No related merge requests found
Pipeline
#58450
passed
4 years ago
Stage: deploy
Stage: pages
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
toardb/utils/TOARII_ontology.py
+98
-6
98 additions, 6 deletions
toardb/utils/TOARII_ontology.py
with
98 additions
and
6 deletions
toardb/utils/TOARII_ontology.py
+
98
−
6
View file @
61a8c244
...
...
@@ -33,7 +33,7 @@ class TOAR_Ontology:
get_class(title):
find the onthology class for the given title
get_class_by_id(
self,
id):
get_class_by_id(id):
find the onthology class for the given id
show_class_definition(ont_class):
...
...
@@ -42,7 +42,7 @@ class TOAR_Ontology:
show_entries(ont_class):
show information on given ontology class
add_entry(
self,
ont_class, idcount, label, definition):
add_entry(ont_class, idcount, label, definition):
add entry to given ontology class
check_entry(entry, ont_class):
...
...
@@ -51,14 +51,24 @@ class TOAR_Ontology:
print(format=
""
):
Prints the onthology in the given format
save(
self,
filename):
save the onthology in
json-ld
format to the given file
save(filename
, format
):
save the onthology in
given
format to the given file
"""
def
__init__
(
self
,
filename
):
"""
initialize the onthology
Parameters
----------
filename: str
filename containing the ontology in json-ld format
Returns
-------
TOAR_Ontology
"""
self
.
filename
=
filename
g
=
rdflib
.
Graph
()
self
.
graph
=
g
.
parse
(
self
.
filename
,
format
=
'
json-ld
'
)
...
...
@@ -66,6 +76,15 @@ class TOAR_Ontology:
def
get_class
(
self
,
title
):
"""
find the onthology class for the given title
Parameters
----------
title: str
title of class
Returns
-------
OWL.Class
"""
for
s
,
p
,
o
in
self
.
graph
.
triples
(
(
None
,
RDF
.
type
,
OWL
.
Class
)
):
value
=
self
.
graph
.
value
(
subject
=
s
,
predicate
=
RDFS
.
label
)
...
...
@@ -76,6 +95,15 @@ class TOAR_Ontology:
def
get_class_by_id
(
self
,
id
):
"""
find the onthology class for the given id
Parameters
----------
id: str
id of class
Returns
-------
OWL.Class
"""
for
s
,
p
,
o
in
self
.
graph
.
triples
(
(
None
,
RDF
.
type
,
OWL
.
Class
)
):
if
s
.
strip
()
==
id
.
strip
():
...
...
@@ -85,6 +113,15 @@ class TOAR_Ontology:
def
show_class_definition
(
self
,
ont_class
):
"""
show information on given ontology class
Parameters
----------
ont_class: OWL.class
onthology class
Returns
-------
None
"""
for
s
,
p
,
o
in
self
.
graph
.
triples
(
(
ont_class
,
SKOS
.
definition
,
None
)
):
print
(
f
"
definition of
{
ont_class
}
:
{
o
}
"
)
...
...
@@ -92,6 +129,15 @@ class TOAR_Ontology:
def
show_entries
(
self
,
ont_class
):
"""
show information on given ontology class
Parameters
----------
ont_class: OWL.class
onthology class
Returns
-------
None
"""
for
s
,
p
,
o
in
self
.
graph
.
triples
(
(
None
,
RDF
.
type
,
ont_class
)
):
label
=
self
.
graph
.
value
(
subject
=
s
,
predicate
=
RDFS
.
label
)
...
...
@@ -102,6 +148,21 @@ class TOAR_Ontology:
def
add_entry
(
self
,
ont_class
,
idcount
,
label
,
definition
):
"""
add entry to given ontology class
Parameters
----------
ont_class: OWL.class
onthology class
id_count: int
integer for constructing ID
"
OWLNamedIndividual_nnnnnn
"
label: str
title of entry to be inserted
definition: str
definition of entry to be inserted
Returns
-------
None
"""
# still to do:
# how to get rid of "http://www.fz-juelich.de/ontologies/2020/ToarOntotology"?!
...
...
@@ -134,6 +195,17 @@ class TOAR_Ontology:
def
check_entry
(
self
,
entry
,
ont_class
):
"""
check whether entry is a valid value of the ontology
Parameters
----------
entry: str
title of entry to be checked
ont_class: OWL.class
onthology class
Returns
-------
None
"""
found
=
False
for
s
,
p
,
o
in
self
.
graph
.
triples
(
(
None
,
RDF
.
type
,
ont_class
)
):
...
...
@@ -144,15 +216,35 @@ class TOAR_Ontology:
def
print
(
self
,
format
=
'
json-ld
'
):
"""
print the onthology in the given format
Parameters
----------
format: str, optional
output format (default: json-ld)
Returns
-------
None
"""
print
(
self
.
graph
.
serialize
(
format
=
format
).
decode
(
"
utf-8
"
))
def
save
(
self
,
filename
):
def
save
(
self
,
filename
,
format
=
'
json-ld
'
):
"""
save the onthology in json-ld format to the given file
Parameters
----------
filename: str
output file name
format: str, optional
output format (default: json-ld)
Returns
-------
None
"""
outfile
=
open
(
filename
,
'
w
'
)
outfile
.
write
(
self
.
graph
.
serialize
(
format
=
'
json-ld
'
).
decode
(
"
utf-8
"
))
outfile
.
write
(
self
.
graph
.
serialize
(
format
=
format
).
decode
(
"
utf-8
"
))
# #check_entry would have been nicer, if something like this worked:
# subj2 = rdflib.URIRef("http://www.fz-juelich.de/ontologies/2020/ToarOntotology#OWLNamedIndividual_000141")
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment