@@ -26,6 +26,7 @@ def __init__(self,*args,**kwargs):
2626 def clear_typesystem (self ):
2727 self .shapes = {}
2828 self .properties = {}
29+ self .linktypes = {}
2930 self .enums = {}
3031 self .values = {}
3132 self .typesystem_loaded = False
@@ -47,9 +48,11 @@ def addtoreport(s, end='\n'):
4748 report += s + end
4849
4950 reportedproperties = []
51+ reportedlinktypes = []
5052 # print a nicely sorted report with shapes at left, then properties (with type, if defined) in that shape, then enumerations in that property
5153 for shapeuri in sorted (self .shapes .keys (),key = lambda k : self .shapes [k ]['name' ].lower ()):
5254 rows .append ( [f"{ quote (self .shapes [shapeuri ]['name' ]):25} " ] )
55+ # properties
5356 for propertyuri in sorted (self .shapes [shapeuri ]['properties' ], key = lambda k : self .properties [k ]['name' ].lower ()):
5457 reportedproperties .append (propertyuri )
5558 rows .append ( [ "" ,f"{ quote (self .properties [propertyuri ]['name' ])} " ] )
@@ -68,6 +71,21 @@ def addtoreport(s, end='\n'):
6871 eid = self .enums [enum_uri ].get ('id' ) or enum_uri
6972 rows .append ( ["" ]* newrowlen + [f"{ quote (self .enums [enum_uri ]['name' ])} " ,eid ,enum_uri ] )
7073 logger .info ( f"appended for enum { rows [- 1 ]} " )
74+ # linktypes not reported on the shape as they're common to all shapes
75+ # # link types
76+ # for linktypeuri in sorted(self.shapes[shapeuri].get('linktypes',[]), key=lambda k: self.linktypes[k]['name'].lower()):
77+ # reportedlinktypes.append(linktypeuri)
78+ # rows.append( [ "",f"{quote(self.linktypes[linktypeuri]['name'])}"] )
79+ # rows[-1].append( f"{rdfxml.uri_to_default_prefixed_tag(linktypeuri)}" )
80+ # if self.linktypes[linktypeuri]['rdfuri'] is not None:
81+ # rows[-1].append( f"{rdfxml.uri_to_default_prefixed_tag(self.linktypes[linktypeuri]['rdfuri'])}" )
82+ # else:
83+ # rows[-1].append("")
84+ # rows[-1].append( f"{self.linktypes[linktypeuri]['label']}" )
85+ # rows[-1].append( f"{self.linktypes[linktypeuri]['inverselabel']}" )
86+
87+ newrowlen = len (rows [- 1 ])- 3
88+
7189 if len (rows )> 0 :
7290 addtoreport ( "<h2>Shapes<h2>\n " )
7391 report += utils .print_in_html ( rows ,['Shape' ,'Property Name' ,'Property label' ,'URI' ] )
@@ -93,10 +111,26 @@ def addtoreport(s, end='\n'):
93111 eid = self .enums [enum_uri ].get ('id' ) or enum_uri
94112 rows .append ( ["" ]* newrowlen + [f"{ quote (self .enums [enum_uri ]['name' ])} " ,eid ,enum_uri ] )
95113 logger .info ( f"appended for enum { rows [- 1 ]} " )
96-
97114 if len (rows )> 0 :
98115 addtoreport ( "<h2>Properties with no shape</h2>\n " )
99116 report += utils .print_in_html ( rows ,['Shape' ,'Property Name' ,'Property label' ,'URI' ] )
117+
118+ # now report link types
119+ rows = []
120+ for linktypeuri in sorted (self .linktypes , key = lambda k : self .linktypes [k ]['name' ].lower ()):
121+ rows .append ( [ f"{ quote (self .linktypes [linktypeuri ]['name' ])} " ] )
122+ rows [- 1 ].append ( f"{ rdfxml .uri_to_default_prefixed_tag (linktypeuri )} " )
123+ if self .linktypes [linktypeuri ]['rdfuri' ] is not None :
124+ rows [- 1 ].append ( f"{ rdfxml .uri_to_default_prefixed_tag (self .linktypes [linktypeuri ]['rdfuri' ])} " )
125+ else :
126+ rows [- 1 ].append ("" )
127+ rows [- 1 ].append ( f"{ self .linktypes [linktypeuri ]['label' ]} " )
128+ rows [- 1 ].append ( f"{ self .linktypes [linktypeuri ]['inverselabel' ]} " )
129+
130+ if len (rows )> 0 :
131+ addtoreport ( "<h2>Link Types</h2>\n " )
132+ report += utils .print_in_html ( rows ,['Name' , 'URI' , 'RDF URI' ,'Label' ,'Inverse Label' ] )
133+
100134
101135 return report
102136
@@ -127,7 +161,7 @@ def register_shape( self, shape_name, shape_uri ):
127161 if shape_uri in self .shapes :
128162 raise Exception ( f"Shape { shape_uri } already defined!" )
129163 # add the URI as the main registration for the shape
130- self .shapes [shape_uri ] = {'name' :shape_name ,'shape' :shape_uri ,'properties' :[]}
164+ self .shapes [shape_uri ] = {'name' :shape_name ,'shape' :shape_uri ,'properties' :[], 'linktypes' :[] }
131165 self .loaded = True
132166
133167 def get_shape_uri ( self , shape_name ):
@@ -173,6 +207,17 @@ def register_property( self, property_name, property_uri, *, property_value_type
173207 self .shapes [shape_uri ]['properties' ].append (property_uri )
174208 self .loaded = True
175209
210+ def register_linktype ( self , linktype_name , linktype_uri , label , * , inverselabel = None , rdfuri = None , shape_uri = None ):
211+ logger .info ( f"register_linktype { linktype_name = } { linktype_uri = } { label = } { inverselabel = } { rdfuri = } " )
212+ linktype_uri = self .normalise_uri ( linktype_uri )
213+ shape_uri = self .normalise_uri ( shape_uri )
214+ if linktype_uri not in self .linktypes :
215+ # self.linktypes[linktype_uri] = {'name': label, 'inverselabel': inverselabel, 'shape': shape_uri, 'rdfuri': rdfuri }
216+ self .linktypes [linktype_uri ] = {'name' : linktype_name , 'label' : label , 'inverselabel' : inverselabel , 'rdfuri' : rdfuri }
217+ if shape_uri is not None :
218+ self .shapes [shape_uri ]['linktypes' ].append (linktype_uri )
219+ self .loaded = True
220+
176221 def get_property_uri ( self , property_name , * , shape_uri = None ):
177222 logger .info ( f"get_property_uri { property_name = } { shape_uri = } " )
178223 shape_uri = self .normalise_uri ( shape_uri )
@@ -269,5 +314,15 @@ def get_uri_name( self, uri ):
269314 return result
270315
271316 def get_name_uri ( self , name ):
272- result = self .get_shape_uri (name ) or self .get_property_uri (name ) or self .get_enum_uri (name ) or self .get_value_uri (name )
317+ result = self .get_shape_uri (name ) or self .get_property_uri (name ) or self .get_enum_uri (name ) or self .get_value_uri (name ) or self . get_linktype_uri ( name )
273318 return result
319+
320+ def get_linktype_uri ( self , name ):
321+ linktypes = [k for k ,v in self .linktypes .items () if v ['name' ]== name ]
322+ if len (linktypes ) > 1 :
323+ raise Exception ( f"Multiple link types with same name '{ name } '" )
324+ if len (linktypes ) == 0 :
325+ return None
326+
327+ return linktypes [0 ]
328+
0 commit comments