@@ -26,6 +26,7 @@ def __init__(self,*args,**kwargs):
26
26
def clear_typesystem (self ):
27
27
self .shapes = {}
28
28
self .properties = {}
29
+ self .linktypes = {}
29
30
self .enums = {}
30
31
self .values = {}
31
32
self .typesystem_loaded = False
@@ -47,9 +48,11 @@ def addtoreport(s, end='\n'):
47
48
report += s + end
48
49
49
50
reportedproperties = []
51
+ reportedlinktypes = []
50
52
# print a nicely sorted report with shapes at left, then properties (with type, if defined) in that shape, then enumerations in that property
51
53
for shapeuri in sorted (self .shapes .keys (),key = lambda k : self .shapes [k ]['name' ].lower ()):
52
54
rows .append ( [f"{ quote (self .shapes [shapeuri ]['name' ]):25} " ] )
55
+ # properties
53
56
for propertyuri in sorted (self .shapes [shapeuri ]['properties' ], key = lambda k : self .properties [k ]['name' ].lower ()):
54
57
reportedproperties .append (propertyuri )
55
58
rows .append ( [ "" ,f"{ quote (self .properties [propertyuri ]['name' ])} " ] )
@@ -68,6 +71,21 @@ def addtoreport(s, end='\n'):
68
71
eid = self .enums [enum_uri ].get ('id' ) or enum_uri
69
72
rows .append ( ["" ]* newrowlen + [f"{ quote (self .enums [enum_uri ]['name' ])} " ,eid ,enum_uri ] )
70
73
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
+
71
89
if len (rows )> 0 :
72
90
addtoreport ( "<h2>Shapes<h2>\n " )
73
91
report += utils .print_in_html ( rows ,['Shape' ,'Property Name' ,'Property label' ,'URI' ] )
@@ -93,10 +111,26 @@ def addtoreport(s, end='\n'):
93
111
eid = self .enums [enum_uri ].get ('id' ) or enum_uri
94
112
rows .append ( ["" ]* newrowlen + [f"{ quote (self .enums [enum_uri ]['name' ])} " ,eid ,enum_uri ] )
95
113
logger .info ( f"appended for enum { rows [- 1 ]} " )
96
-
97
114
if len (rows )> 0 :
98
115
addtoreport ( "<h2>Properties with no shape</h2>\n " )
99
116
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
+
100
134
101
135
return report
102
136
@@ -127,7 +161,7 @@ def register_shape( self, shape_name, shape_uri ):
127
161
if shape_uri in self .shapes :
128
162
raise Exception ( f"Shape { shape_uri } already defined!" )
129
163
# 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' :[] }
131
165
self .loaded = True
132
166
133
167
def get_shape_uri ( self , shape_name ):
@@ -173,6 +207,17 @@ def register_property( self, property_name, property_uri, *, property_value_type
173
207
self .shapes [shape_uri ]['properties' ].append (property_uri )
174
208
self .loaded = True
175
209
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
+
176
221
def get_property_uri ( self , property_name , * , shape_uri = None ):
177
222
logger .info ( f"get_property_uri { property_name = } { shape_uri = } " )
178
223
shape_uri = self .normalise_uri ( shape_uri )
@@ -269,5 +314,15 @@ def get_uri_name( self, uri ):
269
314
return result
270
315
271
316
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 )
273
318
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