def list_all_contexts(query_params): sparql_response = triplestore.query_sparql(QUERY_LIST_CONTEXT, query_params.triplestore_config) all_contexts_uris = sparql.filter_values(sparql_response, "graph") filtered_contexts = filter_and_build_contexts(all_contexts_uris) if not filtered_contexts: json = { "items": [], "warning": "No contexts were found." } return json page_index = int(query_params["page"]) per_page = int(query_params["per_page"]) contexts_pages = split_into_chunks(filtered_contexts, per_page) try: contexts = contexts_pages[page_index] except IndexError: json = { "items": [], "warning": u"No contexts were found in page {0:d}.".format(int(query_params["page"]) + 1) } return json json = { '_base_url': remove_last_slash(query_params.base_url), 'items': contexts } def calculate_total_items(): return len(filtered_contexts) decorate_dict_with_pagination(json, query_params, calculate_total_items) return json
def _get_class_fields_value(query_params, classes, meta_field): query = _build_class_fields_query(classes, meta_field) class_field_query_response = triplestore.query_sparql( query, query_params.triplestore_config) class_field_values = filter_values(class_field_query_response, "field_value") return class_field_values
def list_all_contexts(query_params): sparql_response = triplestore.query_sparql(QUERY_LIST_CONTEXT, query_params.triplestore_config) all_contexts_uris = sparql.filter_values(sparql_response, "graph") filtered_contexts = filter_and_build_contexts(all_contexts_uris) if not filtered_contexts: json = {"items": [], "warning": "No contexts were found."} return json page_index = int(query_params["page"]) per_page = int(query_params["per_page"]) contexts_pages = split_into_chunks(filtered_contexts, per_page) try: contexts = contexts_pages[page_index] except IndexError: json = { "items": [], "warning": u"No contexts were found in page {0:d}.".format( int(query_params["page"]) + 1) } return json json = { '_base_url': remove_last_slash(query_params.base_url), 'items': contexts } def calculate_total_items(): return len(filtered_contexts) decorate_dict_with_pagination(json, query_params, calculate_total_items) return json
def test_query_meta_fields(self): expected = ["http://example.onto/nickname, http://example.onto/description"] classes = ["http://example.onto/City"] meta_field = "http://example.onto/suggestMetaField" query = _build_class_fields_query(classes, meta_field) query_response = self.query(query) meta_field_values = filter_values(query_response, "field_value") self.assertEqual(expected, meta_field_values)
def test_query_predicate_superclass_range(self): expected_classes = ["http://example.onto/Place", "http://example.onto/City"] expected_labels = ["Place", "City"] expected_graphs = ["http://example.onto/", "http://example.onto/"] query_params = { "target": "http://example.onto/birthPlace", "lang_filter_range_label": "" } query_response = self.query(QUERY_PREDICATE_RANGES % query_params) response_classes = filter_values(query_response, "range") response_labels = filter_values(query_response, "range_label") response_graphs = filter_values(query_response, "range_graph") self.assertEqual(expected_classes, response_classes) self.assertEqual(expected_labels, response_labels) self.assertEqual(expected_graphs, response_graphs)
def test_query_subproperties(self): expected = ["http://example.onto/birthCity"] params = { "ruleset": "http://example.onto/ruleset", "property": "http://example.onto/birthPlace" } query_response = self.query(QUERY_SUBPROPERTIES % params) response = filter_values(query_response, "property") self.assertEqual(expected, response)
def _validate_class_restriction(search_params, range_result): classes = set(filter_values(range_result, "range")) if "classes" in search_params: classes_not_in_range = list(set(search_params["classes"]).difference(classes)) if classes_not_in_range: raise HTTPError(400, _(u"Classes {0} are not in the range of predicate '{1}'".format(classes_not_in_range, search_params["target"]))) classes = search_params["classes"] return list(classes)
def _validate_class_restriction(search_params, range_result): classes = set(filter_values(range_result, "range")) if "classes" in search_params: classes_not_in_range = list( set(search_params["classes"]).difference(classes)) if classes_not_in_range: raise HTTPError( 400, _(u"Classes {0} are not in the range of predicate '{1}'". format(classes_not_in_range, search_params["target"]))) classes = search_params["classes"] return list(classes)
def _validate_graph_restriction(search_params, range_result): graphs = set(filter_values(range_result, "range_graph")) if "graphs" in search_params: graphs_set = set(search_params["graphs"]) graphs_not_in_range = list(graphs_set.difference(graphs)) if graphs_not_in_range: raise HTTPError(400, _(u"Classes in the range of predicate '{0}' are not in graphs {1}".format(search_params["target"], graphs_not_in_range))) graphs = graphs_set graphs = graphs.difference(set(settings.GRAPHS_WITHOUT_INSTANCES)) if not graphs: raise HTTPError(400, _(u"Classes in the range of predicate '{0}' are in graphs without instances, such as: {1}".format( search_params["target"], settings.GRAPHS_WITHOUT_INSTANCES))) return list(graphs)
def _validate_graph_restriction(search_params, range_result): graphs = set(filter_values(range_result, "range_graph")) if "graphs" in search_params: graphs_set = set(search_params["graphs"]) graphs_not_in_range = list(graphs_set.difference(graphs)) if graphs_not_in_range: raise HTTPError( 400, _(u"Classes in the range of predicate '{0}' are not in graphs {1}" .format(search_params["target"], graphs_not_in_range))) graphs = graphs_set graphs = graphs.difference(set(settings.GRAPHS_WITHOUT_INSTANCES)) if not graphs: raise HTTPError( 400, _(u"Classes in the range of predicate '{0}' are in graphs without instances, such as: {1}" .format(search_params["target"], settings.GRAPHS_WITHOUT_INSTANCES))) return list(graphs)
def query_superclasses(query_params): uniqueness_property = settings.ANNOTATION_PROPERTY_HAS_UNIQUE_VALUE query_params.set_aux_param('uniqueness_property', uniqueness_property) result_dict = _query_superclasses(query_params) superclasses = filter_values(result_dict, "class") return superclasses
def test_query_new_graph(self): query = QUERY_LIST_CONTEXT response = self.query(query) registered_graphs = sparql.filter_values(response, "graph") self.assertIn('http://whatever.com', registered_graphs)
def test_query_pre_defined_graphs(self): query = QUERY_LIST_CONTEXT response = self.query(query) registered_graphs = sparql.filter_values(response, "graph") self.assertIn('http://semantica.globo.com/upper/', registered_graphs)
def _get_class_fields_value(query_params, classes, meta_field): query = _build_class_fields_query(classes, meta_field) class_field_query_response = triplestore.query_sparql(query, query_params.triplestore_config) class_field_values = filter_values(class_field_query_response, "field_value") return class_field_values