def nin_processor(self, nin, root_sub): # root_sub {'component': root_query_component, 'variable': root_element['variable'], 'type': root_type} # 1. get the root sub within nin , name it np_content whole_query_component = '' # ================================= Stage 1 ==================================== result = PatternProcessor.general_get_root_sub(nin) nin_root_np = result['np'] nin_root_np['uri'] = PatternProcessor.get_uri_for_root_np(nin_root_np) nin_root_np['component'] = PatternProcessor.construct_components(nin_root_np['uri'], nin_root_np['term']) nin_root_np['variable'] = SPARQLEngine.construct_variable_name(nin_root_np['term']) connection_query_component = PatternProcessor.construct_connect_to_sub(root_sub, nin_root_np) # print('\n\t===============Query Component nin 1 ==================') # print('\t', connection_query_component) # print('\t=================================================') # ================================= Stage 2 ==================================== # use the rest of nps to move on next_np_tree = result['new_tree'].pop() result = PatternProcessor.general_get_root_sub(next_np_tree) next_np = result['np'] next_np['tree'] = next_np_tree next_np['funcs'] = PatternProcessor.get_funcs(next_np['tree']) next_np['variable'] = SPARQLEngine.construct_variable_name(next_np['term']) next_np.update(PatternProcessor.get_uri_for_next_np(next_np, nin_root_np)) next_np_query_component = PatternProcessor.construct_simple_spo(nin_root_np['variable'], next_np['uri'], next_np['variable'], next_np['funcs']) contains_nv_of = ('nvOf' in next_np_query_component) if contains_nv_of: next_np['variable'] = 'nvOf' + next_np['variable'] # print('\n\t===============Query Component nin 2==================') # contains_nv_of = ('nvOf' in next_np_query_component) # if contains_nv_of: # next_np['variable'] = 'nvOf' + next_np['variable'] # print('\t || ', next_np['funcs']) # print('\t || ', next_np['variable']) # print('\t', next_np_query_component) # print('\t=================================================') return {'component': connection_query_component + '\n' + next_np_query_component, 'variable': next_np['variable'], 'funcs': next_np['funcs'], 'from': next_np['from']}
def singlenp_processor(self, np, root_sub): predicate_np_result = PatternProcessor.general_get_root_sub(np) predicate_np = predicate_np_result['np'] funcs = [word[1] for word in np.leaves() if word[1].startswith('FUNC')] # ================================================ predicate_np['uri'] = LookUpService.get_predicate_uri(predicate_np['term'], root_sub['uri'], root_sub['type']) predicate_np['type'] = predicate_np['uri']['type'] predicate_np['variable'] = SPARQLEngine.construct_variable_name(predicate_np['term']) predicate_np['funcs'] = funcs predicate_np['component'] = PatternProcessor.construct_components(predicate_np['uri'], predicate_np['term']) predicate_np['from'] = predicate_np['uri']['from'] if type(predicate_np['uri']) == type({}): predicate_np['uri'] = predicate_np['uri']['uri'] if predicate_np['type'] == 'class': # construct ?plant rdf:type <http://www.theworldavatar.com/OntoEIP/OntoEN/power_plant.owl#PowerGenerator> . # connection_query_component = PatternProcessor.construct_connect_to_sub(root_sub, predicate_np) elif predicate_np['type'] == 'property': # constrcut <http://dbpedia.org/resource/Argentina> <http://dbpedia.org/Ontology/gdp_ppp> ?gdp . if predicate_np['from'] == 'ols': connection_query_component = PatternProcessor.construct_simple_spo_ols(root_sub, predicate_np['uri'], predicate_np['variable']) elif predicate_np['from'] == 'dbpedia': connection_query_component = PatternProcessor.construct_simple_spo_service(root_sub, predicate_np['uri'], predicate_np['variable']) return {'component': connection_query_component, 'variable': predicate_np['variable'], 'funcs': predicate_np['funcs'], 'from': predicate_np['from']}
def construct_components(uri, term): variable_name = SPARQLEngine.construct_variable_name(term) type = uri['type'] uri = uri['uri'] if type == 'class': return '''?%s rdf:type <%s> .''' % (variable_name, uri) elif type == 'instance': return '''<%s>''' % uri
def construct_simple_spo(sub_variable, p_uri, obj_variable, next_np_funcs): if type(p_uri) == type({}): p_uri = p_uri['uri'] component_template = ''' ?%s <%s> ?%s . %s''' nv_component_result = SPARQLEngine.construct_get_numerical_value_component(obj_variable) nv_variable = nv_component_result['variable'] nv_component = nv_component_result['component'] spo_component = component_template % (sub_variable, p_uri, obj_variable, nv_component) return spo_component
def NINVSNIN(self, tree): print('Entering NINVSNIN') tree = toolbox.remove_qi(tree) root_tree = PatternProcessor.get_root_sub_and_search_for_uri(tree) root_np = {} root_np.update(root_tree) root_np['component'] = PatternProcessor.construct_components(root_np, root_np['variable']) NPs = [np for np in root_np['tree'] if toolbox.is_tree(np)] components = [] for np in NPs: np_label = np.label() if np_label == 'NIN': print(' === Entering NIN processor ===') components.append(self.nin_processor(np, root_np)) else: print(' === Entering SINGLENP processor ===') components.append(self.singlenp_processor(np, root_np)) sparqlEngine = SPARQLEngine() query_result = sparqlEngine.fire_mix_query( QueryConstructor.construct_standard_ninvsnin_query(root_np, components[0], components[1])) return query_result
def NIN(self, tree): print('Entering NIN') # ========================= Stage 1 ============================= tree = toolbox.remove_qi(tree) root_np_tree = PatternProcessor.get_root_sub(tree) root_np = toolbox.np_processor(root_np_tree) if 'FUNC_ALL' in root_np['funcs']: root_np.update(LookUpService.get_sub_class_uri(root_np['term'])) else: root_np.update(LookUpService.get_sub_instance_uri(root_np['term'])) root_np['variable'] = SPARQLEngine.construct_variable_name(root_np['term']) root_np['component'] = PatternProcessor.construct_components(root_np, root_np['term']) print(root_np['component']) # ========================= Stage 2 ============================= tree = toolbox.get_the_only_sub_tree(tree) tree.remove(root_np_tree) next_np = toolbox.get_the_only_sub_tree(tree) next_np = toolbox.np_processor(next_np) print('next_np ', next_np) print('next np funcs', next_np['funcs']) if 'FUNC_ALL' in next_np['funcs']: uri = LookUpService.get_sub_class_uri(next_np['term']) next_np['type'] = 'class' next_np.update(uri) else: uri = LookUpService.get_predicate_uri(next_np['term'], root_np['uri'], root_np['type']) next_np.update(uri) print('next np', next_np) next_np['variable'] = SPARQLEngine.construct_variable_name(next_np['term']) query_result = QueryConstructor.construct_standard_nin_query(root_np, next_np) query = query_result['query'] domain = query_result['from'] sparqlEngine = SPARQLEngine() print('========= query =========') print(query) print('=========================') if domain == 'ols': print('=================== Searching ols ===================') result = sparqlEngine.fire_mix_query(query) return result else: print('=================== Searching dbpedia ===================') result = sparqlEngine.fire_query(query) return result
def get_root_sub_and_search_for_uri(tree): root_np = PatternProcessor.get_root_sub(tree) tree = toolbox.get_the_only_sub_tree(tree) tree.remove(root_np) tree = NLP_Engine.nin_pattern_recognizer(tree) np_result = toolbox.np_processor(root_np) term = np_result['term'] funcs = np_result['funcs'] variable_name = SPARQLEngine.construct_variable_name(term) print('funcs', funcs) if 'FUNC_ALL' in funcs: uri = LookUpService.get_sub_class_uri(term) type = 'class' else: uri = LookUpService.get_sub_instance_uri(term) type = 'instance' uri['tree'] = tree uri['variable'] = variable_name return uri
def construct_standard_nin_query(root_np, next_np): # print('=== root_np ===', root_np) # print('=== next_np ===', next_np) if type(root_np['uri']) == type({}): root_np['uri'] = root_np['uri']['uri'] root_np['from'] = root_np['uri']['from'] root_np['type'] = root_np['uri']['type'] if type(next_np['uri']) == type({}): next_np['uri'] = next_np['uri']['uri'] next_np['from'] = next_np['uri']['from'] next_np['type'] = next_np['uri']['type'] print('\n\t======================================================') print('\troot np type: ', root_np['type'], root_np['from'], root_np['uri']) print('\tnext np type: ', next_np['type'], next_np['from'], next_np['uri']) print('\t======================================================\n') if root_np['type'] == 'instance': if next_np['type'] == 'class': head = QueryConstructor.construct_func_header( next_np['variable'], next_np['funcs']) if head['header']: header = head['header'] else: header = '?' + next_np['variable'] query = ''' PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT DISTINCT %s WHERE { ?%s rdf:type <%s> . <%s> %s ?%s } ''' % (header, next_np['variable'], next_np['uri'], root_np['uri'], SPARQLEngine.construct_random_predicate(), next_np['variable']) elif next_np['type'] == 'property': head = QueryConstructor.construct_func_header( next_np['variable'], next_np['funcs']) if head['header']: header = head['header'] else: header = '?' + next_np['variable'] query = ''' PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT DISTINCT %s WHERE { <%s> <%s> ?%s }''' % (header, root_np['uri'], next_np['uri'], next_np['variable']) elif root_np['type'] == 'class': if next_np['type'] == 'class': query = ''' PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT DISTINCT ?%s ?%s WHERE { ?%s rdf:type <%s> . ?%s rdf:type <%s> . } ''' % (root_np['variable'], next_np['variable'], root_np['variable'], root_np['uri'], next_np['variable'], next_np['uri']) elif next_np['type'] == 'property': query = ''' PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT DISTINCT ?%s ?%s WHERE { ?%s rdf:type <%s> . ?%s <%s> ?%s . } ''' % (root_np['variable'], next_np['variable'], root_np['variable'], root_np['uri'], root_np['variable'], next_np['uri'], next_np['variable']) return {'query': query, 'from': next_np['from']}
query2 = ''' PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT DISTINCT ?country ?thegdpppp ?sumofnvOfdesignedcapacity WHERE{ SERVICE <http://dbpedia.org/sparql> { ?country <http://dbpedia.org/property/gdpPpp> ?thegdpppp . ?country rdf:type <http://dbpedia.org/ontology/Country> } { SELECT DISTINCT ?country (SUM(?nvOfdesignedcapacity) as ?sumofnvOfdesignedcapacity) WHERE { ?powerplants rdf:type <http://www.theworldavatar.com/OntoEIP/OntoEN/power_plant.owl#PowerGenerator> . ?powerplants <http://dbpedia.org/ontology/country> ?country . ?powerplants <http://www.theworldavatar.com/OntoEIP/system_aspects/system_realization.owl#designCapacity> ?designedcapacity . ?designedcapacity <http://www.theworldavatar.com/OntoEIP/OntoCAPE/OntoCAPE/upper_level/system.owl#hasValue> ?value24 . ?value24 <http://www.theworldavatar.com/OntoEIP/OntoCAPE/OntoCAPE/upper_level/system.owl#numericalValue> ?nvOfdesignedcapacity }GROUP BY ?country } } ''' s = SPARQLEngine() r = s.fire_mix_query(query2) pprint.pprint(r)
def construct_connect_to_sub(root_element, predicate_element): connetion = SPARQLEngine.check_use_as_sub_or_obj(root_element, predicate_element) return connetion
def get_predicate_in_dbpedia(term, root_sub, root_sub_type): # in order to get the predicate, you need the term and the sub to query with if root_sub_type == 'class': if type(root_sub) == type({}): query = '''PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT DISTINCT ?p WHERE { ?s rdf:type <%s> . ?s ?p ?o} ''' % root_sub[ 'uri'] sparqlEngine = SPARQLEngine() result = sparqlEngine.fire_query(query) predicates = sparqlEngine.get_predicates_by_query(result) return { 'uri': LookUpService.rank_uri_basing_on_label(term, predicates), 'type': 'property', 'from': 'dbpedia' } else: query = '''PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT DISTINCT ?p WHERE { ?s rdf:type <%s> . ?s ?p ?o} ''' % root_sub sparqlEngine = SPARQLEngine() result = sparqlEngine.fire_query(query) predicates = sparqlEngine.get_predicates_by_query(result) return { 'uri': LookUpService.rank_uri_basing_on_label(term, predicates), 'type': 'property', 'from': 'dbpedia' } elif root_sub_type == 'instance': query = '''SELECT DISTINCT ?p WHERE { <%s> ?p ?o} ''' % root_sub sparqlEngine = SPARQLEngine() result = sparqlEngine.fire_query(query) predicates = sparqlEngine.get_predicates_by_query(result) return { 'uri': LookUpService.rank_uri_basing_on_label(term, predicates), 'type': 'property', 'from': 'dbpedia' }
def __init__(self): self.ols_base_url = 'http://localhost:8080/api/search?q=' self.SPARQLEngine = SPARQLEngine()