static bool is_descendant_of(SordModel* model, const URIs* uris, const SordNode* child, const SordNode* parent, const SordNode* pred) { if (!child) { return false; } else if (sord_node_equals(child, parent) || sord_ask(model, child, uris->owl_equivalentClass, parent, NULL)) { return true; } SordIter* i = sord_search(model, child, pred, NULL, NULL); for (; !sord_iter_end(i); sord_iter_next(i)) { const SordNode* o = sord_iter_get_node(i, SORD_OBJECT); if (sord_node_equals(child, o)) { continue; // Weird class is explicitly a descendent of itself } if (is_descendant_of(model, uris, o, parent, pred)) { sord_iter_free(i); return true; } } sord_iter_free(i); return false; }
/** Create a new LilvNode from `node`, or return NULL if impossible */ LilvNode* lilv_node_new_from_node(LilvWorld* world, const SordNode* node) { if (!node) { return NULL; } LilvNode* result = NULL; SordNode* datatype_uri = NULL; LilvNodeType type = LILV_VALUE_STRING; size_t len = 0; switch (sord_node_get_type(node)) { case SORD_URI: result = (LilvNode*)malloc(sizeof(LilvNode)); result->world = (LilvWorld*)world; result->type = LILV_VALUE_URI; result->node = sord_node_copy(node); break; case SORD_BLANK: result = (LilvNode*)malloc(sizeof(LilvNode)); result->world = (LilvWorld*)world; result->type = LILV_VALUE_BLANK; result->node = sord_node_copy(node); break; case SORD_LITERAL: datatype_uri = sord_node_get_datatype(node); if (datatype_uri) { if (sord_node_equals(datatype_uri, world->uris.xsd_boolean)) type = LILV_VALUE_BOOL; else if (sord_node_equals(datatype_uri, world->uris.xsd_decimal) || sord_node_equals(datatype_uri, world->uris.xsd_double)) type = LILV_VALUE_FLOAT; else if (sord_node_equals(datatype_uri, world->uris.xsd_integer)) type = LILV_VALUE_INT; else if (sord_node_equals(datatype_uri, world->uris.xsd_base64Binary)) type = LILV_VALUE_BLOB; else LILV_ERRORF("Unknown datatype `%s'\n", sord_node_get_string(datatype_uri)); } result = lilv_node_new( world, type, (const char*)sord_node_get_string_counted(node, &len)); lilv_node_set_numerics_from_string(result, len); break; } return result; }
static bool check_type(SordModel* model, const URIs* uris, const SordNode* node, const SordNode* type) { if (sord_node_equals(type, uris->rdfs_Resource) || sord_node_equals(type, uris->owl_Thing)) { return true; } if (sord_node_get_type(node) == SORD_LITERAL) { if (sord_node_equals(type, uris->rdfs_Literal)) { return true; } else if (sord_node_equals(type, uris->rdf_PlainLiteral)) { return !sord_node_get_language(node); } else { return literal_is_valid(model, uris, node, type); } } else if (sord_node_get_type(node) == SORD_URI) { if (sord_node_equals(type, uris->foaf_Document)) { return true; // Questionable... } else if (is_descendant_of( model, uris, type, uris->xsd_anyURI, uris->owl_onDatatype)) { /* Type is any URI and this is a URI, so pass. Restrictions on anyURI subtypes are not currently checked (very uncommon). */ return true; // Type is anyURI, and this is a URI } else { SordIter* t = sord_search(model, node, uris->rdf_type, NULL, NULL); for (; !sord_iter_end(t); sord_iter_next(t)) { if (is_descendant_of(model, uris, sord_iter_get_node(t, SORD_OBJECT), type, uris->rdfs_subClassOf)) { sord_iter_free(t); return true; } } sord_iter_free(t); return false; } } else { return true; // Blanks often lack explicit types, ignore } return false; }
LILV_API bool lilv_node_equals(const LilvNode* value, const LilvNode* other) { if (value == NULL && other == NULL) return true; else if (value == NULL || other == NULL) return false; else if (value->type != other->type) return false; switch (value->type) { case LILV_VALUE_URI: case LILV_VALUE_BLANK: case LILV_VALUE_STRING: case LILV_VALUE_BLOB: return sord_node_equals(value->node, other->node); case LILV_VALUE_INT: return (value->val.int_val == other->val.int_val); case LILV_VALUE_FLOAT: return (value->val.float_val == other->val.float_val); case LILV_VALUE_BOOL: return (value->val.bool_val == other->val.bool_val); } return false; /* shouldn't get here */ }
static int check_properties(SordModel* model, URIs* uris) { int st = 0; SordIter* i = sord_begin(model); for (; !sord_iter_end(i); sord_iter_next(i)) { SordQuad quad; sord_iter_get(i, quad); const SordNode* subj = quad[SORD_SUBJECT]; const SordNode* pred = quad[SORD_PREDICATE]; const SordNode* obj = quad[SORD_OBJECT]; bool is_any_property = false; SordIter* t = sord_search(model, pred, uris->rdf_type, NULL, NULL); for (; !sord_iter_end(t); sord_iter_next(t)) { if (is_descendant_of(model, uris, sord_iter_get_node(t, SORD_OBJECT), uris->rdf_Property, uris->rdfs_subClassOf)) { is_any_property = true; break; } } sord_iter_free(t); const bool is_ObjectProperty = sord_ask( model, pred, uris->rdf_type, uris->owl_ObjectProperty, 0); const bool is_FunctionalProperty = sord_ask( model, pred, uris->rdf_type, uris->owl_FunctionalProperty, 0); const bool is_InverseFunctionalProperty = sord_ask( model, pred, uris->rdf_type, uris->owl_InverseFunctionalProperty, 0); const bool is_DatatypeProperty = sord_ask( model, pred, uris->rdf_type, uris->owl_DatatypeProperty, 0); if (!is_any_property) { st = error("Use of undefined property", quad); } if (!sord_ask(model, pred, uris->rdfs_label, NULL, NULL)) { st = errorf("Property <%s> has no label\n", sord_node_get_string(pred)); } if (is_DatatypeProperty && sord_node_get_type(obj) != SORD_LITERAL) { st = error("Datatype property with non-literal value", quad); } if (is_ObjectProperty && sord_node_get_type(obj) == SORD_LITERAL) { st = error("Object property with literal value", quad); } if (is_FunctionalProperty && sord_count(model, subj, pred, NULL, NULL) > 1) { st = error("Functional property with several objects", quad); } if (is_InverseFunctionalProperty && sord_count(model, NULL, pred, obj, NULL) > 1) { st = error("Inverse functional property with several subjects", quad); } if (sord_node_equals(pred, uris->rdf_type) && !sord_ask(model, obj, uris->rdf_type, uris->rdfs_Class, NULL) && !sord_ask(model, obj, uris->rdf_type, uris->owl_Class, NULL)) { st = error("Type is not a rdfs:Class or owl:Class", quad); } if (sord_node_get_type(obj) == SORD_LITERAL && !literal_is_valid(model, uris, obj, sord_node_get_datatype(obj))) { st = error("Literal does not match datatype", quad); } SordIter* r = sord_search(model, pred, uris->rdfs_range, NULL, NULL); for (; !sord_iter_end(r); sord_iter_next(r)) { const SordNode* range = sord_iter_get_node(r, SORD_OBJECT); if (!check_type(model, uris, obj, range)) { st = error("Object not in property range", quad); fprintf(stderr, "note: Range is <%s>\n", sord_node_get_string(range)); } } sord_iter_free(r); SordIter* d = sord_search(model, pred, uris->rdfs_domain, NULL, NULL); if (d) { const SordNode* domain = sord_iter_get_node(d, SORD_OBJECT); if (!check_type(model, uris, subj, domain)) { st = error("Subject not in property domain", quad); fprintf(stderr, "note: Domain is <%s>\n", sord_node_get_string(domain)); } sord_iter_free(d); } } sord_iter_free(i); return st; }