-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.py
146 lines (105 loc) · 3.52 KB
/
validator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import rdflib
from rdflib.plugins import sparql
def prepare_query(validation_rule):
return (rdflib.plugins.sparql.prepareQuery(validation_rule[0]),
validation_rule[1])
# Expect this to be supplied externally.
VALIDATION_QUERIES = map(prepare_query, [
('SELECT ?s WHERE { ?s <http://schema.org/operation> ?o}', 'No operation present'),
])
def validate(g):
errors = set()
for q in VALIDATION_QUERIES:
if len(g.query(q[0])) < 1:
errors.add(q[1])
return errors
import rdflib
from rdflib import Graph, URIRef, RDF, plugin
from rdflib.parser import Parser
plugin.register('json-ld', Parser, 'rdflib_jsonld.parser', 'JsonLDParser')
rdf_data = '''{
"@context": {
"@vocab": "http://schema.org/"
},
"person":{
"@id": "person",
"@type": "http://schema.org/Person",
"http://schema.org/address": {
"@id": "address",
"http://schema.org/streetAddress": "123 Main Street"
}
}
}'''
graph = rdflib.Graph()
graph.parse(data=rdf_data, format='json-ld')
from rdflib import Graph, URIRef, Namespace
from rdflib.plugins.sparql import prepareQuery
class PathQuery:
def __init__(self, path_query, base='http://schema.org/'):
path = path_query.split('/')
sparql_target = self.build_sparql_path(path)
self.fetch_query = prepareQuery('SELECT ?result WHERE {{ ?root {0}}}'.format(sparql_target), initNs={'ns': Namespace(base)})
self.exists_query = prepareQuery('ASK WHERE {{ ?root {0}}}'.format(sparql_target), initNs={'ns': Namespace(base)})
def build_sparql_path(self, property_path):
prop = property_path[0]
if not (':' in prop):
prop = 'ns:' + prop
if len(property_path) == 1:
return '{0} ?result '.format(prop)
else:
return '{0} [ {1}]'.format(prop, self.build_sparql_path(property_path[1:]))
def get(self, graph, root):
return graph.query(self.fetch_query,
initBindings={'root': root})
def exists(self, graph, root):
resultset = graph.query(self.exists_query,
initBindings={'root': root})
first_result = iter(resultset).next()
return first_result
class PathQueryConstraint:
def __init__(self, path_query, base='http://schema.org/'):
path = path_query.split('/')
self.target = PathQuery('/'.join(path[:-1]), base)
self.constraint = PathQuery(path[-1], base)
def validate(self, graph, root):
for row in self.target.get(graph, root):
target = row[0]
if self.constraint.exists(graph, target):
return True
return False
rdf_data = '''{
"@context": {
"@vocab": "http://schema.org/"
},
"person":{
"@id": "restaurant",
"@type": "http://schema.org/Restaurant",
"operation": {
"@id": "aaa",
"@type": "SearchAction",
"actionStatus": "proposed",
"actionHandler": [
{
"@type": "HttpHandler",
"name": "Review this restaurant",
"httpMethod": "get",
"url": "http://googleknowledge.github.io/ActionsSamples/restaurant.html"
}
]
}
}
}'''
graph = rdflib.Graph()
graph.parse(data=rdf_data, format='json-ld')
SUPPORTED_TYPES = {
'http://schema.org/SearchAction': [
('actionHandler/url', 'Missing actionHandler'),
]
}
def runquery(g):
for schema_type in SUPPORTED_TYPES:
for rule in SUPPORTED_TYPES[schema_type]:
constraint = PathQueryConstraint(rule[0])
for root in graph.subjects(predicate=RDF.type, object=URIRef(schema_type)):
print root
print 'OK' if constraint.validate(graph, root) else rule[1]