| 1 |
// Copyright 2012-2021 David Robillard <[email protected]> |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include "sord_config.h" |
| 5 |
|
| 6 |
#include <serd/serd.h> |
| 7 |
#include <sord/sord.h> |
| 8 |
#include <zix/allocator.h> |
| 9 |
#include <zix/filesystem.h> |
| 10 |
|
| 11 |
#if USE_PCRE2 |
| 12 |
# ifdef __clang__ |
| 13 |
# pragma clang diagnostic push |
| 14 |
# pragma clang diagnostic ignored "-Wdisabled-macro-expansion" |
| 15 |
# endif |
| 16 |
|
| 17 |
# define PCRE2_CODE_UNIT_WIDTH 8 |
| 18 |
# include <pcre2.h> |
| 19 |
|
| 20 |
# ifdef __clang__ |
| 21 |
# pragma clang diagnostic pop |
| 22 |
# endif |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <inttypes.h> |
| 26 |
#include <stdarg.h> |
| 27 |
#include <stdbool.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
|
| 32 |
#ifdef __GNUC__ |
| 33 |
# define SORD_LOG_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1))) |
| 34 |
#else |
| 35 |
# define SORD_LOG_FUNC(fmt, arg1) |
| 36 |
#endif |
| 37 |
|
| 38 |
#define NS_foaf (const uint8_t*)"http://xmlns.com/foaf/0.1/" |
| 39 |
#define NS_owl (const uint8_t*)"http://www.w3.org/2002/07/owl#" |
| 40 |
#define NS_rdf (const uint8_t*)"http://www.w3.org/1999/02/22-rdf-syntax-ns#" |
| 41 |
#define NS_rdfs (const uint8_t*)"http://www.w3.org/2000/01/rdf-schema#" |
| 42 |
#define NS_xsd (const uint8_t*)"http://www.w3.org/2001/XMLSchema#" |
| 43 |
|
| 44 |
typedef struct { |
| 45 |
SordNode* foaf_Document; |
| 46 |
SordNode* owl_AnnotationProperty; |
| 47 |
SordNode* owl_Class; |
| 48 |
SordNode* owl_DatatypeProperty; |
| 49 |
SordNode* owl_FunctionalProperty; |
| 50 |
SordNode* owl_InverseFunctionalProperty; |
| 51 |
SordNode* owl_ObjectProperty; |
| 52 |
SordNode* owl_OntologyProperty; |
| 53 |
SordNode* owl_Restriction; |
| 54 |
SordNode* owl_Thing; |
| 55 |
SordNode* owl_cardinality; |
| 56 |
SordNode* owl_equivalentClass; |
| 57 |
SordNode* owl_maxCardinality; |
| 58 |
SordNode* owl_minCardinality; |
| 59 |
SordNode* owl_onDatatype; |
| 60 |
SordNode* owl_onProperty; |
| 61 |
SordNode* owl_someValuesFrom; |
| 62 |
SordNode* owl_withRestrictions; |
| 63 |
SordNode* rdf_PlainLiteral; |
| 64 |
SordNode* rdf_Property; |
| 65 |
SordNode* rdf_first; |
| 66 |
SordNode* rdf_rest; |
| 67 |
SordNode* rdf_type; |
| 68 |
SordNode* rdfs_Class; |
| 69 |
SordNode* rdfs_Literal; |
| 70 |
SordNode* rdfs_Resource; |
| 71 |
SordNode* rdfs_domain; |
| 72 |
SordNode* rdfs_label; |
| 73 |
SordNode* rdfs_range; |
| 74 |
SordNode* rdfs_subClassOf; |
| 75 |
SordNode* xsd_anyURI; |
| 76 |
SordNode* xsd_decimal; |
| 77 |
SordNode* xsd_double; |
| 78 |
SordNode* xsd_maxInclusive; |
| 79 |
SordNode* xsd_minInclusive; |
| 80 |
SordNode* xsd_pattern; |
| 81 |
SordNode* xsd_string; |
| 82 |
} URIs; |
| 83 |
|
| 84 |
static int n_errors = 0; |
| 85 |
static int n_restrictions = 0; |
| 86 |
static bool one_line_errors = false; |
| 87 |
|
| 88 |
static int |
| 89 |
print_version(void) |
| 90 |
{ |
| 91 |
printf("sord_validate " SORD_VERSION |
| 92 |
" <http://drobilla.net/software/sord>\n"); |
| 93 |
printf("Copyright 2012-2021 David Robillard <[email protected]>.\n" |
| 94 |
"License: <http://www.opensource.org/licenses/isc>\n" |
| 95 |
"This is free software; you are free to change and redistribute it." |
| 96 |
"\nThere is NO WARRANTY, to the extent permitted by law.\n"); |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
static int |
| 101 |
print_usage(const char* name, bool error) |
| 102 |
{ |
| 103 |
FILE* const os = error ? stderr : stdout; |
| 104 |
fprintf(os, "Usage: %s [OPTION]... INPUT...\n", name); |
| 105 |
fprintf(os, "Validate RDF data.\n\n"); |
| 106 |
fprintf(os, " -h Display this help and exit\n"); |
| 107 |
fprintf(os, " -l Print errors on a single line\n"); |
| 108 |
fprintf(os, " -v Display version information and exit\n"); |
| 109 |
fprintf(os, |
| 110 |
"\n" |
| 111 |
"Validate RDF data. This is a simple validator which checks\n" |
| 112 |
"that all used properties are actually defined. It doesn't do\n" |
| 113 |
"any automatic file retrieval, so all vocabularies must be\n" |
| 114 |
"passed as command-line arguments.\n"); |
| 115 |
return error ? 1 : 0; |
| 116 |
} |
| 117 |
|
| 118 |
SORD_LOG_FUNC(2, 3) static int |
| 119 |
errorf(const SordQuad quad, const char* fmt, ...) |
| 120 |
{ |
| 121 |
va_list args; |
| 122 |
va_start(args, fmt); |
| 123 |
fprintf(stderr, "error: "); |
| 124 |
vfprintf(stderr, fmt, args); |
| 125 |
va_end(args); |
| 126 |
|
| 127 |
const char* sep = one_line_errors ? "\t" : "\n "; |
| 128 |
fprintf(stderr, |
| 129 |
"%s%s%s%s%s%s\n", |
| 130 |
sep, |
| 131 |
(const char*)sord_node_get_string(quad[SORD_SUBJECT]), |
| 132 |
sep, |
| 133 |
(const char*)sord_node_get_string(quad[SORD_PREDICATE]), |
| 134 |
sep, |
| 135 |
(const char*)sord_node_get_string(quad[SORD_OBJECT])); |
| 136 |
|
| 137 |
++n_errors; |
| 138 |
return 1; |
| 139 |
} |
| 140 |
|
| 141 |
static bool |
| 142 |
is_descendant_of(SordModel* model, |
| 143 |
const URIs* uris, |
| 144 |
const SordNode* child, |
| 145 |
const SordNode* parent, |
| 146 |
const SordNode* pred) |
| 147 |
{ |
| 148 |
if (!child) { |
| 149 |
return false; |
| 150 |
} else if (sord_node_equals(child, parent) || |
| 151 |
sord_ask(model, child, uris->owl_equivalentClass, parent, NULL)) { |
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
SordIter* i = sord_search(model, child, pred, NULL, NULL); |
| 156 |
for (; !sord_iter_end(i); sord_iter_next(i)) { |
| 157 |
const SordNode* o = sord_iter_get_node(i, SORD_OBJECT); |
| 158 |
if (sord_node_equals(child, o)) { |
| 159 |
continue; // Weird class is explicitly a descendent of itself |
| 160 |
} |
| 161 |
if (is_descendant_of(model, uris, o, parent, pred)) { |
| 162 |
sord_iter_free(i); |
| 163 |
return true; |
| 164 |
} |
| 165 |
} |
| 166 |
sord_iter_free(i); |
| 167 |
|
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
static bool |
| 172 |
regexp_match(const uint8_t* const pattern, const char* const str) |
| 173 |
{ |
| 174 |
#if USE_PCRE2 |
| 175 |
static const uint32_t options = PCRE2_ANCHORED | PCRE2_ENDANCHORED; |
| 176 |
|
| 177 |
int err = 0; |
| 178 |
size_t erroffset = 0U; |
| 179 |
|
| 180 |
pcre2_code* const re = pcre2_compile( |
| 181 |
pattern, PCRE2_ZERO_TERMINATED, options, &err, &erroffset, NULL); |
| 182 |
|
| 183 |
if (!re) { |
| 184 |
fprintf(stderr, |
| 185 |
"Error in pattern `%s' at offset %zu (%d)\n", |
| 186 |
pattern, |
| 187 |
erroffset, |
| 188 |
err); |
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
pcre2_match_data* const match_data = |
| 193 |
pcre2_match_data_create_from_pattern(re, NULL); |
| 194 |
|
| 195 |
const int rc = pcre2_match(re, |
| 196 |
(const uint8_t*)str, |
| 197 |
PCRE2_ZERO_TERMINATED, |
| 198 |
0, |
| 199 |
options, |
| 200 |
match_data, |
| 201 |
NULL); |
| 202 |
|
| 203 |
pcre2_match_data_free(match_data); |
| 204 |
|
| 205 |
pcre2_code_free(re); |
| 206 |
return rc > 0; |
| 207 |
#else |
| 208 |
(void)pattern; |
| 209 |
(void)str; |
| 210 |
return true; |
| 211 |
#endif // USE_PCRE2 |
| 212 |
} |
| 213 |
|
| 214 |
static int |
| 215 |
bound_cmp(SordModel* model, |
| 216 |
const URIs* uris, |
| 217 |
const SordNode* literal, |
| 218 |
const SordNode* type, |
| 219 |
const SordNode* bound) |
| 220 |
{ |
| 221 |
const char* str = (const char*)sord_node_get_string(literal); |
| 222 |
const char* bound_str = (const char*)sord_node_get_string(bound); |
| 223 |
const SordNode* pred = uris->owl_onDatatype; |
| 224 |
const bool is_numeric = |
| 225 |
is_descendant_of(model, uris, type, uris->xsd_decimal, pred) || |
| 226 |
is_descendant_of(model, uris, type, uris->xsd_double, pred); |
| 227 |
|
| 228 |
if (is_numeric) { |
| 229 |
const double fbound = serd_strtod(bound_str, NULL); |
| 230 |
const double fliteral = serd_strtod(str, NULL); |
| 231 |
return ((fliteral < fbound) ? -1 : (fliteral > fbound) ? 1 : 0); |
| 232 |
} else { |
| 233 |
return strcmp(str, bound_str); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
static bool |
| 238 |
check_restriction(SordModel* model, |
| 239 |
const URIs* uris, |
| 240 |
const SordNode* literal, |
| 241 |
const SordNode* type, |
| 242 |
const SordNode* restriction) |
| 243 |
{ |
| 244 |
size_t len = 0; |
| 245 |
const char* str = (const char*)sord_node_get_string_counted(literal, &len); |
| 246 |
|
| 247 |
// Check xsd:pattern |
| 248 |
SordIter* p = sord_search(model, restriction, uris->xsd_pattern, 0, 0); |
| 249 |
if (p) { |
| 250 |
const SordNode* pat = sord_iter_get_node(p, SORD_OBJECT); |
| 251 |
if (!regexp_match(sord_node_get_string(pat), str)) { |
| 252 |
fprintf(stderr, |
| 253 |
"`%s' does not match <%s> pattern `%s'\n", |
| 254 |
sord_node_get_string(literal), |
| 255 |
sord_node_get_string(type), |
| 256 |
sord_node_get_string(pat)); |
| 257 |
sord_iter_free(p); |
| 258 |
return false; |
| 259 |
} |
| 260 |
sord_iter_free(p); |
| 261 |
++n_restrictions; |
| 262 |
} |
| 263 |
|
| 264 |
// Check xsd:minInclusive |
| 265 |
SordIter* l = sord_search(model, restriction, uris->xsd_minInclusive, 0, 0); |
| 266 |
if (l) { |
| 267 |
const SordNode* lower = sord_iter_get_node(l, SORD_OBJECT); |
| 268 |
if (bound_cmp(model, uris, literal, type, lower) < 0) { |
| 269 |
fprintf(stderr, |
| 270 |
"`%s' is not >= <%s> minimum `%s'\n", |
| 271 |
sord_node_get_string(literal), |
| 272 |
sord_node_get_string(type), |
| 273 |
sord_node_get_string(lower)); |
| 274 |
sord_iter_free(l); |
| 275 |
return false; |
| 276 |
} |
| 277 |
sord_iter_free(l); |
| 278 |
++n_restrictions; |
| 279 |
} |
| 280 |
|
| 281 |
// Check xsd:maxInclusive |
| 282 |
SordIter* u = sord_search(model, restriction, uris->xsd_maxInclusive, 0, 0); |
| 283 |
if (u) { |
| 284 |
const SordNode* upper = sord_iter_get_node(u, SORD_OBJECT); |
| 285 |
if (bound_cmp(model, uris, literal, type, upper) > 0) { |
| 286 |
fprintf(stderr, |
| 287 |
"`%s' is not <= <%s> maximum `%s'\n", |
| 288 |
sord_node_get_string(literal), |
| 289 |
sord_node_get_string(type), |
| 290 |
sord_node_get_string(upper)); |
| 291 |
sord_iter_free(u); |
| 292 |
return false; |
| 293 |
} |
| 294 |
sord_iter_free(u); |
| 295 |
++n_restrictions; |
| 296 |
} |
| 297 |
|
| 298 |
return true; // Unknown restriction, be quietly tolerant |
| 299 |
} |
| 300 |
|
| 301 |
static bool |
| 302 |
literal_is_valid(SordModel* model, |
| 303 |
const URIs* uris, |
| 304 |
const SordQuad quad, |
| 305 |
const SordNode* literal, |
| 306 |
const SordNode* type) |
| 307 |
{ |
| 308 |
if (!type) { |
| 309 |
return true; |
| 310 |
} |
| 311 |
|
| 312 |
/* Check that literal data is related to required type. We don't do a |
| 313 |
strict subtype check here because e.g. an xsd:decimal might be a valid |
| 314 |
xsd:unsignedInt, which the pattern checks will verify, but if the |
| 315 |
literal type is not related to the required type at all |
| 316 |
(e.g. xsd:decimal and xsd:string) there is a problem. */ |
| 317 |
const SordNode* datatype = sord_node_get_datatype(literal); |
| 318 |
if (datatype && datatype != type) { |
| 319 |
if (!is_descendant_of(model, uris, datatype, type, uris->owl_onDatatype) && |
| 320 |
!is_descendant_of(model, uris, type, datatype, uris->owl_onDatatype) && |
| 321 |
!(sord_node_equals(datatype, uris->xsd_decimal) && |
| 322 |
is_descendant_of( |
| 323 |
model, uris, type, uris->xsd_double, uris->owl_onDatatype))) { |
| 324 |
errorf(quad, |
| 325 |
"Literal `%s' datatype <%s> is not compatible with <%s>\n", |
| 326 |
sord_node_get_string(literal), |
| 327 |
sord_node_get_string(datatype), |
| 328 |
sord_node_get_string(type)); |
| 329 |
return false; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
// Find restrictions list |
| 334 |
SordIter* rs = sord_search(model, type, uris->owl_withRestrictions, 0, 0); |
| 335 |
if (!rs) { |
| 336 |
return true; // No restrictions |
| 337 |
} |
| 338 |
|
| 339 |
// Walk list, checking each restriction |
| 340 |
const SordNode* head = sord_iter_get_node(rs, SORD_OBJECT); |
| 341 |
while (head) { |
| 342 |
SordIter* f = sord_search(model, head, uris->rdf_first, 0, 0); |
| 343 |
if (!f) { |
| 344 |
break; // Reached end of restrictions list without failure |
| 345 |
} |
| 346 |
|
| 347 |
// Check this restriction |
| 348 |
const bool good = check_restriction( |
| 349 |
model, uris, literal, type, sord_iter_get_node(f, SORD_OBJECT)); |
| 350 |
sord_iter_free(f); |
| 351 |
|
| 352 |
if (!good) { |
| 353 |
sord_iter_free(rs); |
| 354 |
return false; // Failed, literal is invalid |
| 355 |
} |
| 356 |
|
| 357 |
// Seek to next list node |
| 358 |
SordIter* n = sord_search(model, head, uris->rdf_rest, 0, 0); |
| 359 |
head = n ? sord_iter_get_node(n, SORD_OBJECT) : NULL; |
| 360 |
sord_iter_free(n); |
| 361 |
} |
| 362 |
|
| 363 |
sord_iter_free(rs); |
| 364 |
|
| 365 |
SordIter* s = sord_search(model, type, uris->owl_onDatatype, 0, 0); |
| 366 |
if (s) { |
| 367 |
const SordNode* super = sord_iter_get_node(s, SORD_OBJECT); |
| 368 |
const bool good = literal_is_valid(model, uris, quad, literal, super); |
| 369 |
sord_iter_free(s); |
| 370 |
return good; // Match iff literal also matches supertype |
| 371 |
} |
| 372 |
|
| 373 |
return true; // Matches top level type |
| 374 |
} |
| 375 |
|
| 376 |
static bool |
| 377 |
check_type(SordModel* model, |
| 378 |
const URIs* uris, |
| 379 |
const SordQuad quad, |
| 380 |
const SordNode* node, |
| 381 |
const SordNode* type) |
| 382 |
{ |
| 383 |
if (sord_node_equals(type, uris->rdfs_Resource) || |
| 384 |
sord_node_equals(type, uris->owl_Thing)) { |
| 385 |
return true; |
| 386 |
} |
| 387 |
|
| 388 |
if (sord_node_get_type(node) == SORD_LITERAL) { |
| 389 |
if (sord_node_equals(type, uris->rdfs_Literal)) { |
| 390 |
return true; |
| 391 |
} else if (sord_node_equals(type, uris->rdf_PlainLiteral)) { |
| 392 |
return !sord_node_get_language(node); |
| 393 |
} else { |
| 394 |
return literal_is_valid(model, uris, quad, node, type); |
| 395 |
} |
| 396 |
} else if (sord_node_get_type(node) == SORD_URI) { |
| 397 |
if (sord_node_equals(type, uris->foaf_Document) || // Questionable... |
| 398 |
is_descendant_of( |
| 399 |
model, uris, type, uris->xsd_anyURI, uris->owl_onDatatype)) { |
| 400 |
return true; // This is a URI, and type is Document or anyUri |
| 401 |
} else { |
| 402 |
SordIter* t = sord_search(model, node, uris->rdf_type, NULL, NULL); |
| 403 |
for (; !sord_iter_end(t); sord_iter_next(t)) { |
| 404 |
if (is_descendant_of(model, |
| 405 |
uris, |
| 406 |
sord_iter_get_node(t, SORD_OBJECT), |
| 407 |
type, |
| 408 |
uris->rdfs_subClassOf)) { |
| 409 |
sord_iter_free(t); |
| 410 |
return true; |
| 411 |
} |
| 412 |
} |
| 413 |
sord_iter_free(t); |
| 414 |
return false; |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
return true; // Blanks often lack explicit types, ignore |
| 419 |
} |
| 420 |
|
| 421 |
static uint64_t |
| 422 |
count_non_blanks(SordIter* i, SordQuadIndex field) |
| 423 |
{ |
| 424 |
uint64_t n = 0; |
| 425 |
for (; !sord_iter_end(i); sord_iter_next(i)) { |
| 426 |
const SordNode* node = sord_iter_get_node(i, field); |
| 427 |
if (sord_node_get_type(node) != SORD_BLANK) { |
| 428 |
++n; |
| 429 |
} |
| 430 |
} |
| 431 |
return n; |
| 432 |
} |
| 433 |
|
| 434 |
static int |
| 435 |
check_properties(SordModel* model, URIs* uris) |
| 436 |
{ |
| 437 |
int st = 0; |
| 438 |
SordIter* i = sord_begin(model); |
| 439 |
for (; !sord_iter_end(i); sord_iter_next(i)) { |
| 440 |
SordQuad quad; |
| 441 |
sord_iter_get(i, quad); |
| 442 |
|
| 443 |
const SordNode* subj = quad[SORD_SUBJECT]; |
| 444 |
const SordNode* pred = quad[SORD_PREDICATE]; |
| 445 |
const SordNode* obj = quad[SORD_OBJECT]; |
| 446 |
|
| 447 |
bool is_any_property = false; |
| 448 |
SordIter* t = sord_search(model, pred, uris->rdf_type, NULL, NULL); |
| 449 |
for (; !sord_iter_end(t); sord_iter_next(t)) { |
| 450 |
if (is_descendant_of(model, |
| 451 |
uris, |
| 452 |
sord_iter_get_node(t, SORD_OBJECT), |
| 453 |
uris->rdf_Property, |
| 454 |
uris->rdfs_subClassOf)) { |
| 455 |
is_any_property = true; |
| 456 |
break; |
| 457 |
} |
| 458 |
} |
| 459 |
sord_iter_free(t); |
| 460 |
|
| 461 |
const bool is_ObjectProperty = |
| 462 |
sord_ask(model, pred, uris->rdf_type, uris->owl_ObjectProperty, 0); |
| 463 |
const bool is_FunctionalProperty = |
| 464 |
sord_ask(model, pred, uris->rdf_type, uris->owl_FunctionalProperty, 0); |
| 465 |
const bool is_InverseFunctionalProperty = sord_ask( |
| 466 |
model, pred, uris->rdf_type, uris->owl_InverseFunctionalProperty, 0); |
| 467 |
const bool is_DatatypeProperty = |
| 468 |
sord_ask(model, pred, uris->rdf_type, uris->owl_DatatypeProperty, 0); |
| 469 |
|
| 470 |
if (!is_any_property) { |
| 471 |
st = errorf(quad, "Use of undefined property"); |
| 472 |
} |
| 473 |
|
| 474 |
if (!sord_ask(model, pred, uris->rdfs_label, NULL, NULL)) { |
| 475 |
st = |
| 476 |
errorf(quad, "Property <%s> has no label", sord_node_get_string(pred)); |
| 477 |
} |
| 478 |
|
| 479 |
if (is_DatatypeProperty && sord_node_get_type(obj) != SORD_LITERAL) { |
| 480 |
st = errorf(quad, "Datatype property with non-literal value"); |
| 481 |
} |
| 482 |
|
| 483 |
if (is_ObjectProperty && sord_node_get_type(obj) == SORD_LITERAL) { |
| 484 |
st = errorf(quad, "Object property with literal value"); |
| 485 |
} |
| 486 |
|
| 487 |
if (is_FunctionalProperty) { |
| 488 |
SordIter* o = sord_search(model, subj, pred, NULL, NULL); |
| 489 |
const uint64_t n = count_non_blanks(o, SORD_OBJECT); |
| 490 |
if (n > 1) { |
| 491 |
st = errorf(quad, "Functional property with %" PRIu64 " objects", n); |
| 492 |
} |
| 493 |
sord_iter_free(o); |
| 494 |
} |
| 495 |
|
| 496 |
if (is_InverseFunctionalProperty) { |
| 497 |
SordIter* s = sord_search(model, NULL, pred, obj, NULL); |
| 498 |
const uint64_t n = count_non_blanks(s, SORD_SUBJECT); |
| 499 |
if (n > 1) { |
| 500 |
st = errorf( |
| 501 |
quad, "Inverse functional property with %" PRIu64 " subjects", n); |
| 502 |
} |
| 503 |
sord_iter_free(s); |
| 504 |
} |
| 505 |
|
| 506 |
if (sord_node_equals(pred, uris->rdf_type) && |
| 507 |
!sord_ask(model, obj, uris->rdf_type, uris->rdfs_Class, NULL) && |
| 508 |
!sord_ask(model, obj, uris->rdf_type, uris->owl_Class, NULL)) { |
| 509 |
st = errorf(quad, "Type is not a rdfs:Class or owl:Class"); |
| 510 |
} |
| 511 |
|
| 512 |
if (sord_node_get_type(obj) == SORD_LITERAL && |
| 513 |
!literal_is_valid( |
| 514 |
model, uris, quad, obj, sord_node_get_datatype(obj))) { |
| 515 |
st = errorf(quad, "Literal does not match datatype"); |
| 516 |
} |
| 517 |
|
| 518 |
SordIter* r = sord_search(model, pred, uris->rdfs_range, NULL, NULL); |
| 519 |
for (; !sord_iter_end(r); sord_iter_next(r)) { |
| 520 |
const SordNode* range = sord_iter_get_node(r, SORD_OBJECT); |
| 521 |
if (!check_type(model, uris, quad, obj, range)) { |
| 522 |
st = errorf( |
| 523 |
quad, "Object not in range <%s>\n", sord_node_get_string(range)); |
| 524 |
} |
| 525 |
} |
| 526 |
sord_iter_free(r); |
| 527 |
|
| 528 |
SordIter* d = sord_search(model, pred, uris->rdfs_domain, NULL, NULL); |
| 529 |
if (d) { |
| 530 |
const SordNode* domain = sord_iter_get_node(d, SORD_OBJECT); |
| 531 |
if (!check_type(model, uris, quad, subj, domain)) { |
| 532 |
st = errorf( |
| 533 |
quad, "Subject not in domain <%s>", sord_node_get_string(domain)); |
| 534 |
} |
| 535 |
sord_iter_free(d); |
| 536 |
} |
| 537 |
} |
| 538 |
sord_iter_free(i); |
| 539 |
|
| 540 |
return st; |
| 541 |
} |
| 542 |
|
| 543 |
static int |
| 544 |
check_instance(SordModel* model, |
| 545 |
const URIs* uris, |
| 546 |
const SordNode* restriction, |
| 547 |
const SordQuad quad) |
| 548 |
{ |
| 549 |
const SordNode* instance = quad[SORD_SUBJECT]; |
| 550 |
int st = 0; |
| 551 |
|
| 552 |
const SordNode* prop = |
| 553 |
sord_get(model, restriction, uris->owl_onProperty, NULL, NULL); |
| 554 |
if (!prop) { |
| 555 |
return 0; |
| 556 |
} |
| 557 |
|
| 558 |
const unsigned values = |
| 559 |
(unsigned)sord_count(model, instance, prop, NULL, NULL); |
| 560 |
|
| 561 |
// Check exact cardinality |
| 562 |
const SordNode* card = |
| 563 |
sord_get(model, restriction, uris->owl_cardinality, NULL, NULL); |
| 564 |
if (card) { |
| 565 |
const unsigned c = (unsigned)atoi((const char*)sord_node_get_string(card)); |
| 566 |
if (values != c) { |
| 567 |
st = errorf(quad, |
| 568 |
"Property %s on %s has %u != %u values", |
| 569 |
sord_node_get_string(prop), |
| 570 |
sord_node_get_string(instance), |
| 571 |
values, |
| 572 |
c); |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
// Check minimum cardinality |
| 577 |
const SordNode* minCard = |
| 578 |
sord_get(model, restriction, uris->owl_minCardinality, NULL, NULL); |
| 579 |
if (minCard) { |
| 580 |
const unsigned m = |
| 581 |
(unsigned)atoi((const char*)sord_node_get_string(minCard)); |
| 582 |
if (values < m) { |
| 583 |
st = errorf(quad, |
| 584 |
"Property %s on %s has %u < %u values", |
| 585 |
sord_node_get_string(prop), |
| 586 |
sord_node_get_string(instance), |
| 587 |
values, |
| 588 |
m); |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
// Check maximum cardinality |
| 593 |
const SordNode* maxCard = |
| 594 |
sord_get(model, restriction, uris->owl_maxCardinality, NULL, NULL); |
| 595 |
if (maxCard) { |
| 596 |
const unsigned m = |
| 597 |
(unsigned)atoi((const char*)sord_node_get_string(maxCard)); |
| 598 |
if (values < m) { |
| 599 |
st = errorf(quad, |
| 600 |
"Property %s on %s has %u > %u values", |
| 601 |
sord_node_get_string(prop), |
| 602 |
sord_node_get_string(instance), |
| 603 |
values, |
| 604 |
m); |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
// Check someValuesFrom |
| 609 |
SordIter* sf = |
| 610 |
sord_search(model, restriction, uris->owl_someValuesFrom, NULL, NULL); |
| 611 |
if (sf) { |
| 612 |
const SordNode* type = sord_iter_get_node(sf, SORD_OBJECT); |
| 613 |
|
| 614 |
SordIter* v = sord_search(model, instance, prop, NULL, NULL); |
| 615 |
bool found = false; |
| 616 |
for (; !sord_iter_end(v); sord_iter_next(v)) { |
| 617 |
const SordNode* value = sord_iter_get_node(v, SORD_OBJECT); |
| 618 |
if (check_type(model, uris, quad, value, type)) { |
| 619 |
found = true; |
| 620 |
break; |
| 621 |
} |
| 622 |
} |
| 623 |
if (!found) { |
| 624 |
st = errorf(quad, |
| 625 |
"%s has no <%s> values of type <%s>\n", |
| 626 |
sord_node_get_string(instance), |
| 627 |
sord_node_get_string(prop), |
| 628 |
sord_node_get_string(type)); |
| 629 |
} |
| 630 |
sord_iter_free(v); |
| 631 |
} |
| 632 |
sord_iter_free(sf); |
| 633 |
|
| 634 |
return st; |
| 635 |
} |
| 636 |
|
| 637 |
static int |
| 638 |
check_class_instances(SordModel* model, |
| 639 |
const URIs* uris, |
| 640 |
const SordNode* restriction, |
| 641 |
const SordNode* klass) |
| 642 |
{ |
| 643 |
// Check immediate instances of this class |
| 644 |
SordIter* i = sord_search(model, NULL, uris->rdf_type, klass, NULL); |
| 645 |
for (; !sord_iter_end(i); sord_iter_next(i)) { |
| 646 |
SordQuad quad; |
| 647 |
sord_iter_get(i, quad); |
| 648 |
check_instance(model, uris, restriction, quad); |
| 649 |
} |
| 650 |
sord_iter_free(i); |
| 651 |
|
| 652 |
// Check instances of all subclasses recursively |
| 653 |
SordIter* s = sord_search(model, NULL, uris->rdfs_subClassOf, klass, NULL); |
| 654 |
for (; !sord_iter_end(s); sord_iter_next(s)) { |
| 655 |
const SordNode* subklass = sord_iter_get_node(s, SORD_SUBJECT); |
| 656 |
check_class_instances(model, uris, restriction, subklass); |
| 657 |
} |
| 658 |
sord_iter_free(s); |
| 659 |
|
| 660 |
return 0; |
| 661 |
} |
| 662 |
|
| 663 |
static int |
| 664 |
check_instances(SordModel* model, const URIs* uris) |
| 665 |
{ |
| 666 |
int st = 0; |
| 667 |
SordIter* r = |
| 668 |
sord_search(model, NULL, uris->rdf_type, uris->owl_Restriction, NULL); |
| 669 |
for (; !sord_iter_end(r); sord_iter_next(r)) { |
| 670 |
const SordNode* restriction = sord_iter_get_node(r, SORD_SUBJECT); |
| 671 |
const SordNode* prop = |
| 672 |
sord_get(model, restriction, uris->owl_onProperty, NULL, NULL); |
| 673 |
if (!prop) { |
| 674 |
continue; |
| 675 |
} |
| 676 |
|
| 677 |
SordIter* c = |
| 678 |
sord_search(model, NULL, uris->rdfs_subClassOf, restriction, NULL); |
| 679 |
for (; !sord_iter_end(c); sord_iter_next(c)) { |
| 680 |
const SordNode* klass = sord_iter_get_node(c, SORD_SUBJECT); |
| 681 |
check_class_instances(model, uris, restriction, klass); |
| 682 |
} |
| 683 |
sord_iter_free(c); |
| 684 |
} |
| 685 |
sord_iter_free(r); |
| 686 |
|
| 687 |
return st; |
| 688 |
} |
| 689 |
|
| 690 |
int |
| 691 |
main(int argc, char** argv) |
| 692 |
{ |
| 693 |
if (argc < 2) { |
| 694 |
return print_usage(argv[0], true); |
| 695 |
} |
| 696 |
|
| 697 |
int a = 1; |
| 698 |
for (; a < argc && argv[a][0] == '-'; ++a) { |
| 699 |
if (argv[a][1] == 'h') { |
| 700 |
return print_usage(argv[0], false); |
| 701 |
} else if (argv[a][1] == 'l') { |
| 702 |
one_line_errors = true; |
| 703 |
} else if (argv[a][1] == 'v') { |
| 704 |
return print_version(); |
| 705 |
} else { |
| 706 |
fprintf(stderr, "%s: Unknown option `%s'\n", argv[0], argv[a]); |
| 707 |
return print_usage(argv[0], true); |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
SordWorld* world = sord_world_new(); |
| 712 |
SordModel* model = sord_new(world, SORD_SPO | SORD_OPS, false); |
| 713 |
SerdEnv* env = serd_env_new(&SERD_NODE_NULL); |
| 714 |
SerdReader* reader = sord_new_reader(model, env, SERD_TURTLE, NULL); |
| 715 |
|
| 716 |
for (; a < argc; ++a) { |
| 717 |
const uint8_t* input = (const uint8_t*)argv[a]; |
| 718 |
uint8_t* rel_in_path = serd_file_uri_parse(input, NULL); |
| 719 |
char* in_path = zix_canonical_path(NULL, (char*)rel_in_path); |
| 720 |
|
| 721 |
free(rel_in_path); |
| 722 |
if (!in_path) { |
| 723 |
fprintf(stderr, "Skipping file %s\n", input); |
| 724 |
continue; |
| 725 |
} |
| 726 |
|
| 727 |
SerdURI base_uri; |
| 728 |
SerdNode base_uri_node = |
| 729 |
serd_node_new_file_uri((const uint8_t*)in_path, NULL, &base_uri, true); |
| 730 |
|
| 731 |
serd_env_set_base_uri(env, &base_uri_node); |
| 732 |
const SerdStatus st = |
| 733 |
serd_reader_read_file(reader, (const uint8_t*)in_path); |
| 734 |
if (st) { |
| 735 |
fprintf(stderr, "error reading %s: %s\n", in_path, serd_strerror(st)); |
| 736 |
} |
| 737 |
|
| 738 |
serd_node_free(&base_uri_node); |
| 739 |
zix_free(NULL, in_path); |
| 740 |
} |
| 741 |
serd_reader_free(reader); |
| 742 |
serd_env_free(env); |
| 743 |
|
| 744 |
#define URI(prefix, suffix) \ |
| 745 |
uris.prefix##_##suffix = sord_new_uri(world, NS_##prefix #suffix) |
| 746 |
|
| 747 |
URIs uris; |
| 748 |
URI(foaf, Document); |
| 749 |
URI(owl, AnnotationProperty); |
| 750 |
URI(owl, Class); |
| 751 |
URI(owl, DatatypeProperty); |
| 752 |
URI(owl, FunctionalProperty); |
| 753 |
URI(owl, InverseFunctionalProperty); |
| 754 |
URI(owl, ObjectProperty); |
| 755 |
URI(owl, OntologyProperty); |
| 756 |
URI(owl, Restriction); |
| 757 |
URI(owl, Thing); |
| 758 |
URI(owl, cardinality); |
| 759 |
URI(owl, equivalentClass); |
| 760 |
URI(owl, maxCardinality); |
| 761 |
URI(owl, minCardinality); |
| 762 |
URI(owl, onDatatype); |
| 763 |
URI(owl, onProperty); |
| 764 |
URI(owl, someValuesFrom); |
| 765 |
URI(owl, withRestrictions); |
| 766 |
URI(rdf, PlainLiteral); |
| 767 |
URI(rdf, Property); |
| 768 |
URI(rdf, first); |
| 769 |
URI(rdf, rest); |
| 770 |
URI(rdf, type); |
| 771 |
URI(rdfs, Class); |
| 772 |
URI(rdfs, Literal); |
| 773 |
URI(rdfs, Resource); |
| 774 |
URI(rdfs, domain); |
| 775 |
URI(rdfs, label); |
| 776 |
URI(rdfs, range); |
| 777 |
URI(rdfs, subClassOf); |
| 778 |
URI(xsd, anyURI); |
| 779 |
URI(xsd, decimal); |
| 780 |
URI(xsd, double); |
| 781 |
URI(xsd, maxInclusive); |
| 782 |
URI(xsd, minInclusive); |
| 783 |
URI(xsd, pattern); |
| 784 |
URI(xsd, string); |
| 785 |
|
| 786 |
#if !USE_PCRE2 |
| 787 |
fprintf(stderr, "warning: Built without PCRE2, datatypes not checked.\n"); |
| 788 |
#endif |
| 789 |
|
| 790 |
const int prop_st = check_properties(model, &uris); |
| 791 |
const int inst_st = check_instances(model, &uris); |
| 792 |
|
| 793 |
printf("Found %d errors among %d files (checked %d restrictions)\n", |
| 794 |
n_errors, |
| 795 |
argc - 1, |
| 796 |
n_restrictions); |
| 797 |
|
| 798 |
sord_free(model); |
| 799 |
sord_world_free(world); |
| 800 |
return prop_st || inst_st; |
| 801 |
} |