def get_graph_of_user(self, username): """get all graph of a user Parameters ---------- username : string Username Returns ------- list List of graphs """ query_launcher = SparqlQueryLauncher(self.app, self.session) query_builder = SparqlQuery(self.app, self.session) query = """ SELECT DISTINCT ?graph WHERE {{ ?graph dc:creator <{}> . }} """.format(username) header, data = query_launcher.process_query(query_builder.prefix_query(query)) graphs = [] for result in data: graphs.append(result["graph"]) return graphs
def get_all_graphs(self): """get all graph of a user Returns ------- list List of graphs """ query_launcher = SparqlQueryLauncher(self.app, self.session) query_builder = SparqlQuery(self.app, self.session) query = """ SELECT DISTINCT ?graph WHERE {{ ?graph dc:creator ?user . }} """ header, data = query_launcher.process_query( query_builder.prefix_query(query)) graphs = [] for result in data: graphs.append(result["graph"]) return graphs
def init(): """Get the default sparql query Returns ------- json """ try: # Disk space files_utils = FilesUtils(current_app, session) disk_space = files_utils.get_size_occupied_by_user( ) if "user" in session else None # Get graphs and endpoints query = SparqlQuery(current_app, session) graphs, endpoints = query.get_graphs_and_endpoints(all_selected=True) # Default query default_query = query.prefix_query(query.get_default_query()) console_enabled = can_access(session['user']) except Exception as e: traceback.print_exc(file=sys.stdout) return jsonify({ "error": True, "errorMessage": str(e), "defaultQuery": "", "graphs": {}, "endpoints": {}, "diskSpace": None, "console_enabled": False }), 500 return jsonify({ "error": False, "errorMessage": "", "defaultQuery": default_query, "graphs": graphs, "endpoints": endpoints, "diskSpace": disk_space, "console_enabled": console_enabled })
def update_base_url(self, graph, old_url, new_url): """Update base url for a graph Parameters ---------- graph : string Graph to update old_url : string Old base_url new_url : string New base_url Returns ------- list List of graphs """ query_launcher = SparqlQueryLauncher(self.app, self.session) query_builder = SparqlQuery(self.app, self.session) query = """ WITH <{0}> DELETE{{ ?s ?p ?o }} INSERT{{ ?s2 ?p2 ?o2 }} WHERE {{ ?s ?p ?o FILTER(REGEX(?s, '{1}', 'i') || REGEX(?p, '{1}', 'i') || REGEX(?o, '{1}', 'i')) . BIND(IF (isURI(?o), URI(REPLACE(STR(?o), '{1}', '{2}')), ?o) AS ?o2) . BIND(IF (isURI(?s), URI(REPLACE(STR(?s), '{1}', '{2}')), ?s) AS ?s2) . BIND(IF (isURI(?p), URI(REPLACE(STR(?p), '{1}', '{2}')), ?p) AS ?p2) . }} """.format(graph, old_url, new_url) header, data = query_launcher.process_query( query_builder.prefix_query(query))
def get_startpoints(self): """Get public and user startpoints Returns ------- list Startpoints """ filter_user = "" if self.logged_user(): filter_user = "******".format(self.session["user"]["username"]) query_launcher = SparqlQueryLauncher(self.app, self.session) query_builder = SparqlQuery(self.app, self.session) query = ''' SELECT DISTINCT ?endpoint ?graph ?entity ?entity_label ?creator ?public WHERE {{ ?graph askomics:public ?public . ?graph dc:creator ?creator . GRAPH ?graph {{ ?graph prov:atLocation ?endpoint . ?entity a askomics:entity . ?entity a askomics:startPoint . ?entity rdfs:label ?entity_label . }} FILTER ( ?public = <true>{} ) }} '''.format(filter_user) header, data = query_launcher.process_query(query_builder.prefix_query(query)) startpoints = [] entities = [] for result in data: if result["endpoint"] == self.settings.get("triplestore", "endpoint"): endpoint_name = "local" else: try: endpoint_name = tld.get_fld(result["endpoint"]).split('.')[0] except Exception: endpoint_name = urlparse(result["endpoint"]).netloc if result["entity"] not in entities: # new entity entities.append(result['entity']) startpoint = { "entity": result["entity"], "entity_label": result["entity_label"], "graphs": [{ "uri": result["graph"], "public": result["public"], "creator": result["creator"] }], "endpoints": [{"url": result["endpoint"], "name": endpoint_name}], "public": self.str_to_bool(result["public"]), "private": not self.str_to_bool(result["public"]) } startpoints.append(startpoint) else: # update existing entity index = entities.index(result['entity']) graph = { "uri": result["graph"], "public": result["public"], "creator": result["creator"] } startpoints[index]["graphs"].append(graph) startpoints[index]["endpoints"].append({"url": result["endpoint"], "name": endpoint_name}) if self.str_to_bool(result["public"]): startpoints[index]["public"] = True else: startpoints[index]["private"] = True return startpoints
def get_abstraction_relations(self): """Get user abstraction relations from the triplestore Returns ------- list Relations """ filter_user = "" if self.logged_user(): filter_user = "******".format(self.session["user"]["username"]) query_launcher = SparqlQueryLauncher(self.app, self.session) query_builder = SparqlQuery(self.app, self.session) query = ''' SELECT DISTINCT ?graph ?entity_uri ?entity_faldo ?entity_label ?node_type ?attribute_uri ?attribute_faldo ?attribute_label ?attribute_range ?property_uri ?property_faldo ?property_label ?range_uri ?category_value_uri ?category_value_label WHERE {{ # Graphs ?graph askomics:public ?public . ?graph dc:creator ?creator . GRAPH ?graph {{ # Property (relations and categories) ?property_uri a owl:ObjectProperty . ?property_uri a askomics:AskomicsRelation . ?property_uri rdfs:label ?property_label . ?property_uri rdfs:range ?range_uri . }} # Relation of entity (or motherclass of entity) {{ ?property_uri rdfs:domain ?mother . ?entity_uri rdfs:subClassOf ?mother . }} UNION {{ ?property_uri rdfs:domain ?entity_uri . }} FILTER ( ?public = <true>{} ) }} '''.format(filter_user) header, data = query_launcher.process_query(query_builder.prefix_query(query)) relations_list = [] relations = [] for result in data: # Relation if "property_uri" in result: rel_tpl = (result["property_uri"], result["entity_uri"], result["range_uri"]) if rel_tpl not in relations_list: relations_list.append(rel_tpl) relation = { "uri": result["property_uri"], "label": result["property_label"], "graphs": [result["graph"], ], "source": result["entity_uri"], "target": result["range_uri"] } relations.append(relation) else: # if graph is diff, append it index_relation = relations_list.index(rel_tpl) if result["graph"] not in relations[index_relation]["graphs"]: relations[index_relation]["graphs"].append(result["graph"]) return relations
def get_abstraction_attributes(self): """Get user abstraction attributes from the triplestore Returns ------- list AskOmics attributes """ filter_user = "" if self.logged_user(): filter_user = "******".format(self.session["user"]["username"]) litterals = ( "http://www.w3.org/2001/XMLSchema#string", "http://www.w3.org/2001/XMLSchema#decimal", "http://www.w3.org/2001/XMLSchema#boolean" ) query_launcher = SparqlQueryLauncher(self.app, self.session) query_builder = SparqlQuery(self.app, self.session) query = ''' SELECT DISTINCT ?graph ?entity_uri ?attribute_uri ?attribute_type ?attribute_faldo ?attribute_label ?attribute_range ?category_value_uri ?category_value_label WHERE {{ # Graphs ?graph askomics:public ?public . ?graph dc:creator ?creator . GRAPH ?graph {{ ?attribute_uri a ?attribute_type . VALUES ?attribute_type {{ owl:DatatypeProperty askomics:AskomicsCategory }} ?attribute_uri rdfs:label ?attribute_label . ?attribute_uri rdfs:range ?attribute_range . # Faldo OPTIONAL {{ ?attribute_uri a ?attribute_faldo . VALUES ?attribute_faldo {{ askomics:faldoStart askomics:faldoEnd askomics:faldoStrand askomics:faldoReference }} }} # Categories (DK) OPTIONAL {{ ?attribute_range askomics:category ?category_value_uri . ?category_value_uri rdfs:label ?category_value_label . }} }} # Attribute of entity (or motherclass of entity) {{ ?attribute_uri rdfs:domain ?mother . ?entity_uri rdfs:subClassOf ?mother . }} UNION {{ ?attribute_uri rdfs:domain ?entity_uri . }} FILTER ( ?public = <true>{} ) }} '''.format(filter_user) header, data = query_launcher.process_query(query_builder.prefix_query(query)) attributes_list = [] attributes = [] for result in data: # Attributes if "attribute_uri" in result and "attribute_label" in result and result["attribute_type"] != "{}AskomicsCategory".format(self.settings.get("triplestore", "namespace_internal")) and result["attribute_range"] in litterals: attr_tpl = (result["attribute_uri"], result["entity_uri"]) if attr_tpl not in attributes_list: attributes_list.append(attr_tpl) attribute = { "uri": result["attribute_uri"], "label": result["attribute_label"], "graphs": [result["graph"], ], "entityUri": result["entity_uri"], "type": result["attribute_range"], "faldo": result["attribute_faldo"] if "attribute_faldo" in result else None, "categories": [] } attributes.append(attribute) else: # if graph is different, store it index_attribute = attributes_list.index(attr_tpl) if result["graph"] not in attributes[index_attribute]["graphs"]: attributes[index_attribute]["graphs"].append(result["graph"]) index_attribute = attributes_list.index(attr_tpl) # Categories if "attribute_uri" in result and result["attribute_type"] == "{}AskomicsCategory".format(self.settings.get("triplestore", "namespace_internal")) and "category_value_uri" in result: attr_tpl = (result["attribute_uri"], result["entity_uri"]) if attr_tpl not in attributes_list: attributes_list.append(attr_tpl) attribute = { "uri": result["attribute_uri"], "label": result["attribute_label"], "graphs": [result["graph"], ], "entityUri": result["entity_uri"], "type": result["attribute_type"], "faldo": result["attribute_faldo"] if "attribute_faldo" in result else None, "categories": [{ "uri": result["category_value_uri"], "label": result["category_value_label"] }] } attributes.append(attribute) else: # if graph diff, store it index_attribute = attributes_list.index(attr_tpl) if result["graph"] not in attributes[index_attribute]["graphs"]: attributes[index_attribute]["graphs"].append(result["graph"]) # Store value if new value = { "uri": result["category_value_uri"], "label": result["category_value_label"] } if value not in attributes[index_attribute]["categories"]: attributes[index_attribute]["categories"].append(value) return attributes
def get_abstraction_entities(self): """Get abstraction entities Returns ------- list List of entities available """ filter_user = "" if self.logged_user(): filter_user = "******".format(self.session["user"]["username"]) query_launcher = SparqlQueryLauncher(self.app, self.session) query_builder = SparqlQuery(self.app, self.session) query = ''' SELECT DISTINCT ?endpoint ?graph ?entity_uri ?entity_type ?entity_faldo ?entity_label ?have_no_label WHERE {{ ?graph askomics:public ?public . ?graph dc:creator ?creator . GRAPH ?graph {{ ?graph prov:atLocation ?endpoint . ?entity_uri a ?entity_type . VALUES ?entity_type {{ askomics:entity askomics:bnode }} . # Faldo OPTIONAL {{ ?entity_uri a ?entity_faldo . VALUES ?entity_faldo {{ askomics:faldo }} . }} # Label OPTIONAL {{ ?entity_uri rdfs:label ?entity_label . }} OPTIONAL {{ ?entity_uri askomics:instancesHaveNoLabels ?have_no_label . }} }} FILTER ( ?public = <true>{} ) }} '''.format(filter_user) header, data = query_launcher.process_query(query_builder.prefix_query(query)) entities_list = [] # list of entity uri entities = [] # list of entity dict for result in data: if result["entity_uri"] not in entities_list: # New entity entities_list.append(result["entity_uri"]) # Uri, graph and label label = "" if "entity_label" not in result else result["entity_label"] entity_type = "bnode" if result["entity_type"] == "{}bnode".format(self.settings.get("triplestore", "namespace_internal")) else "node" entity = { "uri": result["entity_uri"], "type": entity_type, "label": label, "instancesHaveLabels": True if "have_no_label" not in result else False if result["have_no_label"] == "1" else True, "faldo": True if "entity_faldo" in result else False, "endpoints": [result["endpoint"]], "graphs": [result["graph"]], } entities.append(entity) else: # if graph is different, store it index_entity = entities_list.index(result['entity_uri']) if result["graph"] not in entities[index_entity]["graphs"]: entities[index_entity]["graphs"].append(result["graph"]) # If endpoint is different, store it if result["endpoint"] not in entities[index_entity]["endpoints"]: entities[index_entity]["endpoints"].append(result["endpoint"]) return entities