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
0a6067cf
Commit
0a6067cf
authored
4 years ago
by
Sabine Schröder
Browse files
Options
Downloads
Patches
Plain Diff
working with onthologies
parent
73eecfb4
No related branches found
No related tags found
No related merge requests found
Pipeline
#58205
passed
4 years ago
Stage: deploy
Stage: pages
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
toardb/utils/TOARII_ontology.py
+92
-17
92 additions, 17 deletions
toardb/utils/TOARII_ontology.py
toardb/utils/onto_example.py
+30
-0
30 additions, 0 deletions
toardb/utils/onto_example.py
with
122 additions
and
17 deletions
toardb/utils/TOARII_ontology.py
+
92
−
17
View file @
0a6067cf
...
...
@@ -5,45 +5,120 @@ class TOAR_Ontology
"""
import
rdflib
from
rdflib.namespace
import
RDF
,
RDFS
g
=
rdflib
.
Graph
()
result
=
g
.
parse
(
"
TOAR-II-Ontology_2020-11-24.owl
"
,
format
=
'
json-ld
'
)
#print("graph has %s statements." % len(g))
from
rdflib
import
URIRef
,
Literal
from
rdflib.namespace
import
RDF
,
RDFS
,
SKOS
class
TOAR_Ontology
:
"""
some information
A class to represent the TOAR onthology.
...
Attributes
----------
filename : str
filename of file containing onthology (in json-ld-Format)
graph : Graph
onthology graph
Methods
-------
get_class(title):
find the onthology class for the given title
def show_class_definition(ont_class):
show information on given ontology class
show_entries(ont_class):
show information on given ontology class
def add_entry(self, ont_class, idcount, label, definition):
add entry to given ontology class
def check_entry(entry, ont_class):
check whether entry is a valid value of the ontology
print(format=
""
):
Prints the onthology in the given format
def save(self, filename):
save the onthology in json-ld format to the given file
"""
def
get_class
(
title
):
def
__init__
(
self
,
filename
):
"""
initialize the onthology
"""
self
.
filename
=
filename
g
=
rdflib
.
Graph
()
self
.
graph
=
g
.
parse
(
"
TOAR-II-Ontology_2020-11-24.owl
"
,
format
=
'
json-ld
'
)
def
get_class
(
self
,
title
):
"""
find the
right
class for the given title
find the
onthology
class for the given title
"""
for
s
,
p
,
o
in
g
.
triples
(
(
None
,
RDF
.
type
,
rdflib
.
URIRef
(
"
http://www.w3.org/2002/07/owl#Class
"
)
)
):
value
=
g
.
value
(
subject
=
s
,
predicate
=
RDFS
.
label
)
for
s
,
p
,
o
in
self
.
graph
.
triples
(
(
None
,
RDF
.
type
,
rdflib
.
URIRef
(
"
http://www.w3.org/2002/07/owl#Class
"
)
)
):
value
=
self
.
graph
.
value
(
subject
=
s
,
predicate
=
RDFS
.
label
)
print
(
f
"
value:
{
value
}
, title:
{
title
}
"
)
if
value
and
value
.
strip
()
==
title
.
strip
():
ont_class
=
s
return
ont_class
def
show_
entries
(
ont_class
):
def
show_
class_definition
(
self
,
ont_class
):
"""
show information on given ontology class
"""
print
(
f
"
show information on
'
{
ont_class
}
'"
)
for
s
,
p
,
o
in
g
.
triples
(
(
None
,
RDF
.
type
,
ont_class
)
):
print
(
"
%s is %s
"
%
(
s
,
g
.
value
(
subject
=
s
,
predicate
=
RDFS
.
label
)))
for
s
,
p
,
o
in
self
.
graph
.
triples
(
(
ont_class
,
SKOS
.
definition
,
None
)
):
print
(
f
"
definition of
{
ont_class
}
:
{
o
}
"
)
def
check_entry
(
entry
,
ont_class
):
def
show_entries
(
self
,
ont_class
):
"""
show information on given ontology class
"""
for
s
,
p
,
o
in
self
.
graph
.
triples
(
(
None
,
RDF
.
type
,
ont_class
)
):
label
=
self
.
graph
.
value
(
subject
=
s
,
predicate
=
RDFS
.
label
)
print
(
f
"
{
s
}
is
{
label
}
"
)
for
s
,
p
,
o
in
self
.
graph
.
triples
(
(
s
,
SKOS
.
definition
,
None
)
):
print
(
f
"
definition of
{
label
}
:
{
o
}
"
)
def
add_entry
(
self
,
ont_class
,
idcount
,
label
,
definition
):
"""
add entry to given ontology class
"""
print
(
f
"
add entry to
'
{
ont_class
}
'"
)
s
=
URIRef
(
f
"
http://www.fz-juelich.de/ontologies/2020/ToarOntotology#OWLNamedIndividual_
{
idcount
:
06
d
}
"
)
p
=
URIRef
(
"
http://www.w3.org/1999/02/22-rdf-syntax-ns#type
"
)
o
=
URIRef
(
ont_class
)
self
.
graph
.
add
((
s
,
p
,
o
))
o
=
URIRef
(
"
http://www.w3.org/2002/07/owl#NamedIndividual
"
)
self
.
graph
.
add
((
s
,
p
,
o
))
self
.
graph
.
add
((
s
,
RDFS
.
label
,
Literal
(
label
,
lang
=
"
en
"
)))
self
.
graph
.
add
((
s
,
SKOS
.
definition
,
Literal
(
definition
)))
def
check_entry
(
self
,
entry
,
ont_class
):
"""
check whether entry is a valid value of the ontology
"""
found
=
False
for
s
,
p
,
o
in
g
.
triples
(
(
None
,
RDF
.
type
,
ont_class
)
):
if
g
.
value
(
subject
=
s
,
predicate
=
RDFS
.
label
).
strip
()
==
entry
:
for
s
,
p
,
o
in
self
.
graph
.
triples
(
(
None
,
RDF
.
type
,
ont_class
)
):
if
self
.
graph
.
value
(
subject
=
s
,
predicate
=
RDFS
.
label
).
strip
()
==
entry
:
found
=
True
return
found
def
print
(
self
,
format
=
'
json-ld
'
):
"""
print the onthology in the given format
"""
print
(
self
.
graph
.
serialize
(
format
=
format
).
decode
(
"
utf-8
"
))
def
save
(
self
,
filename
):
"""
save the onthology in json-ld format to the given file
"""
outfile
=
open
(
filename
,
'
w
'
)
outfile
.
write
(
self
.
graph
.
serialize
(
format
=
'
json-ld
'
).
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")
# pred2 = RDFS.label
...
...
This diff is collapsed.
Click to expand it.
toardb/utils/onto_example.py
0 → 100644
+
30
−
0
View file @
0a6067cf
import
TOARII_ontology
help
(
TOARII_ontology
)
onto
=
TOARII_ontology
.
TOAR_Ontology
(
'
TOAR-II-Ontology_2020-11-24.owl
'
)
help
(
onto
)
onto
.
print
()
onto
.
print
(
format
=
"
turtle
"
)
onto
.
print
(
format
=
'
application/rdf+xml
'
)
dlclass
=
onto
.
get_class
(
'
Station Dominant Landcover Type
'
)
onto
.
show_class_definition
(
dlclass
)
onto
.
show_entries
(
dlclass
)
onto
.
check_entry
(
'
Croplands
'
,
dlclass
)
onto
.
check_entry
(
'
Craplands
'
,
dlclass
)
czclass
=
onto
.
get_class
(
'
Climatic Zone
'
)
onto
.
show_class_definition
(
czclass
)
onto
.
show_entries
(
czclass
)
onto
.
check_entry
(
'
PolarMoist
'
,
czclass
)
onto
.
check_entry
(
'
PolarWet
'
,
czclass
)
onto
=
TOARII_ontology
.
TOAR_Ontology
(
'
TOAR-Ontology-TimeZone-CountryCode.owl
'
)
dlclass
=
onto
.
get_class
(
'
Station Dominant Landcover Type
'
)
onto
.
show_class_definition
(
dlclass
)
onto
.
show_entries
(
dlclass
)
ccclass
=
onto
.
get_class
(
'
Country Code
'
)
ccclass
=
onto
.
get_class
(
'
Country Code
'
)
onto
.
show_class_definition
(
ccclass
)
onto
.
show_entries
(
ccclass
)
#highest index of NamedInvidual is 194
#==> start counting from here
onto
.
add_entry
(
ccclass
,
195
,
"
GB
"
,
"
United Kingdom of Great Britain and Northern Ireland
"
)
onto
.
show_entries
(
ccclass
)
onto
.
save
(
'
TOAR-II-Ontology_2021-01-26.owl
'
)
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