66
77import pyshacl
88import rdflib
9+ from rdflib import RDF , RDFS , DCTERMS , OWL
910from nanopub import Publication , NanopubClient
1011from rdflib .tools .rdf2dot import rdf2dot
1112
12- from fairworkflows import namespaces
13+ from fairworkflows import namespaces , LinguisticSystem
1314from fairworkflows .config import PACKAGE_DIR
1415
1516PLEX_SHAPES_SHACL_FILEPATH = str (PACKAGE_DIR / 'resources' / 'plex-shapes.ttl' )
1617
17-
1818class RdfWrapper :
19- def __init__ (self , uri , ref_name = 'fairobject' , derived_from : List [str ] = None ):
19+ def __init__ (self , uri , ref_name = 'fairobject' , derived_from : List [str ] = None ,
20+ language : LinguisticSystem = None ):
2021 self ._rdf = rdflib .Graph ()
2122 self ._uri = str (uri )
2223 self .self_ref = rdflib .term .BNode (ref_name )
2324 self ._is_modified = False
2425 self ._is_published = False
2526 self .derived_from = derived_from
27+
2628 self ._bind_namespaces ()
2729
30+ # A blank node to which triples about the linguistic
31+ # system for this FAIR object can be added
32+ self .lingsys_ref = rdflib .BNode ('LinguisticSystem' )
33+ self ._rdf .add ((self .self_ref , DCTERMS .language , self .lingsys_ref ))
34+
35+ if language is not None :
36+ self .language = language
37+
2838 def _bind_namespaces (self ):
2939 """Bind namespaces used often in fair step and fair workflow.
3040
@@ -35,6 +45,9 @@ def _bind_namespaces(self):
3545 self .rdf .bind ("dul" , namespaces .DUL )
3646 self .rdf .bind ("bpmn" , namespaces .BPMN )
3747 self .rdf .bind ("pwo" , namespaces .PWO )
48+ self .rdf .bind ("schema" , namespaces .SCHEMAORG )
49+ self .rdf .bind ("dc" , DCTERMS )
50+ self .rdf .bind ("owl" , OWL )
3851
3952 @property
4053 def rdf (self ) -> rdflib .Graph :
@@ -114,6 +127,57 @@ def remove_attribute(self, predicate, object=None):
114127 """
115128 self ._rdf .remove ((self .self_ref , predicate , object ))
116129
130+ @property
131+ def label (self ):
132+ """Label.
133+
134+ Returns the rdfs:label of this Fair object (or a list, if more than one matching triple is found)
135+ """
136+ return self .get_attribute (RDFS .label )
137+
138+ @label .setter
139+ def label (self , value ):
140+ """
141+ Adds the given text string as an rdfs:label for this Fair object.
142+ """
143+ self .set_attribute (RDFS .label , rdflib .term .Literal (value ))
144+
145+ @property
146+ def description (self ):
147+ """Description.
148+
149+ Returns the dcterms:description of this Fair object (or a list, if more than
150+ one matching triple is found)
151+ """
152+ return self .get_attribute (DCTERMS .description )
153+
154+ @description .setter
155+ def description (self , value ):
156+ """
157+ Adds the given text string as a dcterms:description for this Fair object.
158+ """
159+ self .set_attribute (DCTERMS .description , rdflib .term .Literal (value ))
160+
161+ @property
162+ def language (self ):
163+ """Returns the language for this fair objects's description (could be code).
164+ Returns a LinguisticSystem object.
165+ """
166+ lingsys_rdf = rdflib .Graph ()
167+ for t in self ._rdf .triples ((self .lingsys_ref , None , None )):
168+ lingsys_rdf .add (t )
169+ return LinguisticSystem .from_rdf (lingsys_rdf )
170+
171+ @language .setter
172+ def language (self , value : LinguisticSystem ):
173+ """Sets the language for this fair object's code (takes a LinguisticSystem).
174+ Removes the existing linguistic system triples from the RDF decription
175+ and replaces them with the new linguistic system."""
176+ lingsys_triples = list (self ._rdf .triples ( (self .lingsys_ref , None , None ) ))
177+ if len (lingsys_triples ) > 0 :
178+ self ._rdf .remove (lingsys_triples )
179+ self ._rdf += value .generate_rdf (self .lingsys_ref )
180+
117181 def shacl_validate (self ):
118182 sg = rdflib .Graph ()
119183 sg .parse (PLEX_SHAPES_SHACL_FILEPATH , format = 'ttl' )
0 commit comments