def filter_instances(query_params): if not class_exists(query_params): error_message = u"Class {0} in graph {1} does not exist".format( query_params["class_uri"], query_params["graph_uri"]) raise HTTPError(404, log_message=error_message) keymap = { "label": "title", "subject": "@id", "sort_object": shorten_uri(query_params["sort_by"]), } for p, o, index in extract_po_tuples(query_params): keymap[o[1:]] = shorten_uri(p) result_dict = query_filter_instances(query_params) if not result_dict or not result_dict['results']['bindings']: return None items_list = compress_keys_and_values( result_dict, keymap=keymap, ignore_keys=["total"], do_expand_uri=query_params['expand_uri'] == u"1") items_list = merge_by_id(items_list) for item in items_list: uri = item["@id"] item["instance_prefix"] = u"{0}/".format(uri.rsplit('/', 1)[0]) item["class_prefix"] = expand_uri(query_params['class_prefix']) decorate_with_resource_id(items_list) return build_json(items_list, query_params)
def should_add_predicate_and_object(self, predicate, object_): predicate = shorten_uri(predicate) if not predicate.startswith("?") else predicate generic_po = predicate.startswith("?") and object_.startswith("?") rdfs_repetition = (predicate in RDFS_LABEL) and object_.startswith("?") return not generic_po and not rdfs_repetition
def filter_instances(query_params): if not class_exists(query_params): error_message = u"Class {0} in graph {1} does not exist".format( query_params["class_uri"], query_params["graph_uri"]) raise HTTPError(404, log_message=error_message) keymap = { "label": "title", "subject": "@id", "sort_object": shorten_uri(query_params["sort_by"]), } for p, o, index in extract_po_tuples(query_params): keymap[o[1:]] = shorten_uri(p) result_dict = query_filter_instances(query_params) if not result_dict or not result_dict['results']['bindings']: return None items_list = compress_keys_and_values(result_dict, keymap=keymap, ignore_keys=["total"]) items_list = merge_by_id(items_list) add_prefix(items_list, query_params['class_prefix']) decorate_with_resource_id(items_list) return build_json(items_list, query_params)
def get_sort_variable(self): sort_predicate = self.params["sort_by"] sort_label = "" if sort_predicate: sort_predicate = shorten_uri(sort_predicate) if not sort_predicate.startswith("?") else sort_predicate if (sort_predicate in RDFS_LABEL): sort_label = "?label" else: for predicate, object_, index in self.po_tuples: if (sort_predicate == predicate) and object_.startswith("?"): sort_label = object_ break if not sort_label: sort_label = "?sort_object" return sort_label
def get_instance_fields(query_params, item, class_schema): """ Assemble an instance's properties data (instance_fields) provided: item: ElasticSearch response item (available inside "hits") class_schema: Brainiak schema definition for the item's class """ instance_fields = [] class_properties = class_schema["properties"] for property_uri, property_object in item["fields"].items(): property_title = class_properties[property_uri]["title"] required = class_properties[property_uri].get("required", False) if isinstance(property_object, list): object_list = property_object else: object_list = [property_object] for object_ in object_list: object_title = object_.get("title") if isinstance( object_, dict) else object_ field = { 'object_title': object_title, 'predicate_id': property_uri if query_params['expand_uri'] == u"1" else shorten_uri(property_uri), 'predicate_title': property_title, 'required': required } if isinstance(object_, dict): field['object_id'] = object_.get("@id") instance_fields.append(field) return instance_fields
def schema(query_params): context_name = query_params['context_name'] class_name = query_params['class_name'] class_prefix = query_params.get('class_prefix', None) if (class_prefix is not None) and query_params.get('expand_uri') == '0': class_prefix = shorten_uri(class_prefix) args = (context_name, class_name, class_prefix) class_schema = get_cached_schema(query_params) if class_prefix is not None: schema_ref = u"/{0}/{1}/_schema?class_prefix={2}".format(*args) href = u"/{0}/{1}?class_prefix={2}".format(*args) else: schema_ref = u'/{0}/{1}/_schema'.format(*args) href = u'/{0}/{1}'.format(*args) if 'expand_uri' in query_params: expand_uri_param = 'expand_uri={0}'.format(query_params['expand_uri']) schema_ref = append_param(schema_ref, expand_uri_param) href = append_param(href, expand_uri_param) link = build_link(query_params) base = { "$schema": "http://json-schema.org/draft-04/schema#", "title": class_schema.get('title', ''), "type": "object", "required": ["items", "_class_prefix", "@id"], "properties": { "_class_prefix": {"type": "string"}, "pattern": {"type": "string"}, # used in _search service responses "do_item_count": {"type": "integer"}, "item_count": {"type": "integer"}, "@id": {"type": "string", "format": "uri"}, "items": { "type": "array", "items": { "type": "object", "title": class_schema.get('title', ''), "required": ["title", "@id", "resource_id", "instance_prefix"], "properties": { "title": {"type": "string"}, "@id": {"type": "string"}, "resource_id": {"type": "string"}, "instance_prefix": {"type": "string", "format": "uri"}, }, "links": [ { "href": link, "method": "GET", "rel": "item" }, { "href": link, "method": "GET", "rel": "instance" } ] } }, }, "links": [ { "href": "{+_base_url}", "method": "GET", "rel": "self" }, { "href": "{+_schema_url}", "method": "GET", "rel": "class" }, { "href": u"/{0}".format(context_name), "method": "GET", "rel": "context" }, { "href": href, "method": "POST", "rel": "add", "schema": {"$ref": schema_ref} }, { "href": "/{0}/{1}/_search?graph_uri={2}&class_uri={3}&pattern={{pattern}}".format( context_name, class_name, query_params['graph_uri'], query_params['class_uri']), "method": "GET", "rel": "search", "schema": SEARCH_PARAM_SCHEMA } ] } base_pagination_url = u'/{0}/{1}'.format(context_name, class_name) extra_url_params = '&class_prefix={_class_prefix}' pagination_dict = pagination_schema(base_pagination_url, extra_url_params) merge_schemas(base, pagination_dict) return base
def normalize_class_prefix(class_prefix, should_expand_uri): return expand_uri(class_prefix) if should_expand_uri else shorten_uri( class_prefix)
def test_shorten_with_slash_in_item(self, mocked_extract_prefix): self.assertEqual(shorten_uri("http://some/invalid/uri"), "http://some/invalid/uri")
def test_shorten_uri_with_prefix(self): self.assertEqual( shorten_uri("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), "rdf")
def test_shorten_uri_fails(self): self.assertEqual(shorten_uri("http://some/invalid/uri"), "http://some/invalid/uri")
def test_shorten_uri_success(self): self.assertEqual( shorten_uri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), "rdf:type")
def get_instance_fields(query_params, item, class_schema): """ Assemble an instance's properties data (instance_fields) provided: item: ElasticSearch response item (available inside "hits") class_schema: Brainiak schema definition for the item's class """ instance_fields = [] class_properties = class_schema["properties"] for property_uri, property_object in item["fields"].items(): property_title = class_properties[property_uri]["title"] required = class_properties[property_uri].get("required", False) if isinstance(property_object, list): object_list = property_object else: object_list = [property_object] for object_ in object_list: object_title = object_.get("title") if isinstance(object_, dict) else object_ field = { 'object_title': object_title, 'predicate_id': property_uri if query_params['expand_uri'] == u"1" else shorten_uri(property_uri), 'predicate_title': property_title, 'required': required } if isinstance(object_, dict): field['object_id'] = object_.get("@id") instance_fields.append(field) return instance_fields