1
1
2
2
import numpy as np
3
+ import rdflib
3
4
4
5
import bald
5
6
@@ -40,8 +41,9 @@ def exceptions(self):
40
41
41
42
class SubjectValidation (Validation ):
42
43
43
- def __init__ (self , subject , httpcache = None ):
44
+ def __init__ (self , subject , fhandle , httpcache = None ):
44
45
self .subject = subject
46
+ self .fhandle = fhandle
45
47
if isinstance (httpcache , bald .HttpStatusCache ):
46
48
self .cache = httpcache
47
49
else :
@@ -55,6 +57,10 @@ def exceptions(self):
55
57
exceptions = []
56
58
exceptions = self .check_attr_uris (exceptions )
57
59
exceptions = self .check_attr_domain_range (exceptions )
60
+ exceptions = self ._extra_exceptions (exceptions )
61
+ return exceptions
62
+
63
+ def _extra_exceptions (self , exceptions ):
58
64
return exceptions
59
65
60
66
def check_attr_uris (self , exceptions ):
@@ -77,6 +83,39 @@ def _check_uri(uri, exceptions):
77
83
return exceptions
78
84
79
85
def check_attr_domain_range (self , exceptions ):
86
+ for attr , value in self .subject .attrs .iteritems ():
87
+ uri = self .subject .unpack_uri (attr )
88
+ if self .cache .check_uri (uri ):
89
+ #thus we have a payload
90
+ # go rdf
91
+ g = rdflib .Graph ()
92
+ g .parse (data = self .cache [uri ].text , format = "n3" )
93
+ query = ('SELECT ?s \n '
94
+ '(GROUP_CONCAT(?domain; SEPARATOR=" | ") AS ?domains) \n '
95
+ '(GROUP_CONCAT(?type; SEPARATOR=" | ") AS ?types) \n '
96
+ 'WHERE {{ \n '
97
+ '?s a ?type . \n '
98
+ 'OPTIONAL{{ ?s rdfs:domain ?domain . }} \n '
99
+ 'FILTER(?s = <{uria}> || ?s = <{urib}>) \n '
100
+ '}} \n '
101
+ 'GROUP BY ?s \n ' .format (uria = uri , urib = uri .rstrip ('/' )))
102
+ qres = list (g .query (query ))
103
+ if len (qres ) != 1 :
104
+ raise ValueError ('{} does not define one and only one \n '
105
+ 'rdfs:domain' .format (uri ))
106
+ qres , = qres
107
+ # implement recursive inheritance check
108
+ # we need to check if the value that the attr points to
109
+ # has an rdf:type which is the same as the one required by
110
+ # the object property constraint
111
+ # The value may be a URI or it may be a reference to another
112
+ # subject within the file.
113
+ # therefore subjects in the file have to be typed?!?
114
+
115
+ # import pdb; pdb.set_trace()
116
+ # if qres.domains != rdflib.term.URIRef(value):
117
+ # msg = ('The attribute {} references ')
118
+ # exceptions.appen(ValueError(msg))
80
119
return exceptions
81
120
82
121
@@ -85,24 +124,19 @@ class ContainerValidation(SubjectValidation):
85
124
def __init__ (self , ** kwargs ):
86
125
super (ContainerValidation , self ).__init__ (** kwargs )
87
126
88
- def exceptions (self ):
89
- exceptions = []
90
- exceptions = self .check_attr_uris (exceptions )
91
- return exceptions
127
+
128
+
92
129
93
130
class DatasetValidation (SubjectValidation ):
94
131
95
132
96
- def __init__ (self , name , dataset , fhandle , ** kwargs ):
133
+ def __init__ (self , name , dataset , ** kwargs ):
97
134
98
135
self .dataset = dataset
99
136
self .name = name
100
- self .fhandle = fhandle
101
137
super (DatasetValidation , self ).__init__ (** kwargs )
102
138
103
- def exceptions (self ):
104
- exceptions = []
105
- exceptions = self .check_attr_uris (exceptions )
139
+ def _extra_exceptions (self , exceptions ):
106
140
exceptions = self .check_array_references (exceptions )
107
141
return exceptions
108
142
0 commit comments