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 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
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)