def get_subjects(request): """ Get the RDF subjects that match the current filters. Return a dictionary with the subject list. """ data = simplejson.loads(request.body) sparql = SPARQLQuery(data['endpoint_url'], data['search_type']) subjects_raw = sparql.get_subjects( data['predicates'], data['offset'], data['dataset'], ) subjects = [] for subject in subjects_raw['subjects']: subjects.append(subject) return HttpResponse(simplejson.dumps({ 'subjects': subjects, 'runtime': subjects_raw['time'], 'query': subjects_raw['query'], 'endpoint_url': subjects_raw['endpoint_url'], }), mimetype='application/json')
def get_predicates_used_by_subjects(request): """ Get the RDF predicates used by the given subjects. Return a dictionary with the predicates and their respective label. """ data = simplejson.loads(request.body) sparql = SPARQLQuery(data['endpoint_url'], data['search_type']) predicates_raw = sparql.get_predicates(data['subjects'], ) predicates = [] for predicate in predicates_raw['predicates']: predicates.append({ 'uri': predicate['uri'], 'label': predicate['label'], 'inverse': predicate['inverse'], }) return HttpResponse( simplejson.dumps({ 'predicates': predicates, 'runtime': predicates_raw['time'], 'query': predicates_raw['query'], }), mimetype='application/json', )
def get42dataDatasets(request): """ """ data = simplejson.loads(request.body) sparql = SPARQLQuery(data['endpoint_url'], "virtuoso") query = Template(""" SELECT ?dataset ?label WHERE { ?dataset <http://www.w3.org/ns/prov#wasGeneratedBy> ?generated_by . ?dataset <http://www.w3.org/2000/01/rdf-schema#label> ?label . ?generated_by <http://www.w3.org/ns/prov#wasStartedBy> ?started_by . ?started_by <http://www.w3.org/2000/01/rdf-schema#label> ?user FILTER (?user = "******"^^<http://www.w3.org/2001/XMLSchema#string>) . }""").substitute({'userId': data['userId']}) queryResult = sparql.query(query) response = {} # Add info for the selected endpoint selected_endpoint = Endpoint.objects.get(sparql_url=data['endpoint_url']) response['endpoint'] = Endpoint.objects.filter( sparql_url=data['endpoint_url']).values('label', 'website_url')[0] response['datasets'] = [] for result in queryResult["results"]["bindings"]: responseRow = {} responseRow["uri"] = result["dataset"]["value"] responseRow["label"] = result["label"]["value"] responseRow["size"] = 0 response['datasets'].append(responseRow) return HttpResponse(simplejson.dumps(response), mimetype='application/json')
def reload_datasets(request, endpoint_url): """ Reload the cube datasets of a SPARQL endpoint. Return a redirect to the same page. """ endpoint = Endpoint.objects.get(pk=endpoint_url) sparql = SPARQLQuery(endpoint.sparql_url) sparql.refresh_dataset_list() return redirect('/endpoints')
def getVis(self): try: sparqlqueryObjectD3 = "" resultObject = {} '''st = "http://data.lod2.eu/" if st in self.dataset: sparqlqueryObjectD3=SPARQLQuery('http://open-data.europa.eu/en/sparqlep', 'regex') else: sparqlqueryObjectD3=SPARQLQuery('http://zaire.dimis.fim.uni-passau.de:8890/sparql', 'virtuoso')''' if not self.endpoint: self.endpoint = \ 'http://zaire.dimis.fim.uni-passau.de:8890/sparql' search_type = 'regex' sparqlqueryObjectD3 = SPARQLQuery(self.endpoint, search_type) measure = sparqlqueryObjectD3.get_cube_measure_for_auto_mapping( self.dataset, self.deletedMeasure) valueOfMeasure = sparqlqueryObjectD3.get_value_of_cube_measure( self.dataset, self.dimension, measure, self.datasetFilters) generatorFactoryObjectAuto = generatorfactoryforautomapping.GeneratorFactoryForAutoMapping( ) generatorauto = generatorFactoryObjectAuto.createFactoryauto( self.selectedChart, self.dimension, measure, valueOfMeasure, self.dataset, self.chartrowIndex) if generatorauto != None: generatorauto.transform() columns = "" rows = "" if generatorauto.results.has_key("columns"): columns = generatorauto.results['columns'] if generatorauto.results.has_key("rows"): rows = generatorauto.results['rows'] resultObject = { 'name': self.selectedChart, 'start': generatorauto.results['code'], 'rows': rows, 'columns': columns } return resultObject except Exception as ex: print("-OutProcessorauto.getVis: %s" % ex) raise Exception("%s" % ex)
def get_objects_for_predicate(request): """ Get the objects for the given subjects and predicate. Return a dictionary with the predicate and the found objects. """ data = simplejson.loads(request.body) sparql = SPARQLQuery(data['endpoint_url'], data['search_type']) objects_raw = sparql.get_objects( data['subjects'], data['predicate'], ) objects = [] for row in objects_raw['table']: sub = row['s'] obj = row['o']['value'] if row['o']['type'] == 'uri': temp_object = { 's': sub, 'o': { 'type': row['o']['type'], 'uri': obj } } if 'label' in row['o']: temp_object['o']['label'] = row['o']['label']['value'] objects.append(temp_object) else: temp_object = { 's': sub, 'o': { 'type': row['o']['type'], 'label': obj, } } if row['o'].get('datatype'): temp_object['o']['datatype'] = row['o'].get('datatype') objects.append(temp_object) return HttpResponse(simplejson.dumps({ 'predicate': data['predicate'], 'objects': objects, 'runtime': objects_raw['time'], 'query': objects_raw['query'], }), mimetype='application/json')
def get_comprehensive_sparql_query(request): """ Get the SPARQL query that rules them all. Return JSON containing the query. """ data = simplejson.loads(request.body) sparql = SPARQLQuery(data['endpoint_url'], data['search_type']) query = sparql.get_comprehensive_sparql_query( data['predicates'], data['dataset'], ) return HttpResponse(simplejson.dumps({ 'query': query, }), mimetype='application/json')
def get_cube_dimensions_and_measures(request): """ Get the dimensions and measures of the currently displayed cube. Return a list of the cube dimension dictionaries containing uri and label as well as a list of the cube measure dictionaries containing the uri. """ data = simplejson.loads(request.body) sparql = SPARQLQuery(data['endpoint_url'], data['search_type']) output = {} # Get the dimensions dimensions_raw = sparql.get_cube_dimensions(data['dataset']) dimensions = [] for dimension in dimensions_raw: dimensions.append({ 'uri': dimension['dimensionuri'], 'label': dimension['label'], }) output['dimensions'] = dimensions # Get the measures measures_raw = sparql.get_cube_measure(data['dataset']) measures = [] for measure in measures_raw: measures.append({ 'uri': measure['measureuri'], 'label': measure['label'], }) output['measures'] = measures return HttpResponse(simplejson.dumps(output), mimetype='application/json')
def get_dataset_label(request): """Get the label of a dataset from the local DB and return as JSON.""" data = simplejson.loads(request.body) response = {} # Get the label of the given dataset try: dataset = Dataset.objects.get(pk=data['dataset_uri']) except ObjectDoesNotExist: sparql = SPARQLQuery(data['endpoint_url'], data['search_type']) dataset = sparql.get_and_save_dataset_label(data['dataset_uri']) response['dataset'] = { 'label': dataset.label, 'description': dataset.description, } response['endpoint'] = { 'label': dataset.endpoint.label, 'website_url': dataset.endpoint.website_url, } return HttpResponse(simplejson.dumps(response), mimetype='application/json')
def process(self): try: print "OutProcessorForAutomaticallyMapping::start..." resultArray = [] supportedCharts = [] supportedArray = [] mappingProposalObject = mappingproposal.MappingProposal() sparqlqueryObjectD3 = "" print "OutProcessorForAutomaticallyMapping::1...", self.dataset '''if self.dataset == "http://data.lod2.eu/scoreboard/ds/indicator/i_iuolc_IND_TOTAL__ind_iu3": sparqlqueryObjectD3 = SPARQLQuery("http://open-data.europa.eu/en/sparqlep", 'regex')''' st = "http://data.lod2.eu/" if st in self.dataset: sparqlqueryObjectD3 = SPARQLQuery( 'http://open-data.europa.eu/en/sparqlep', 'regex') else: sparqlqueryObjectD3 = SPARQLQuery( 'http://zaire.dimis.fim.uni-passau.de:8890/sparql', 'virtuoso') dimensions = sparqlqueryObjectD3.get_cube_dimensions_for_auto_mapping( self.dataset) measure = sparqlqueryObjectD3.get_cube_measure_for_auto_mapping( self.dataset) #valueOfMeasure = sparqlqueryObjectD3.get_value_of_cube_measure(self.dataset, dimensions, measure, 10) chartComponentsArray = mappingProposalObject.getChartComponents() #print "OLAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", chartComponentsArray, "DIMMMMMMMMMMMMMMMMM", dimensions supportedCharts = mappingProposalObject.getSupportedChart( chartComponentsArray, dimensions, measure) #print "JETZT: ", len(supportedCharts) for element in supportedCharts: ch = element['chart'] chartComponent = { 'chart': '', 'charturi': '', 'visualChannels': [] } chUri = element['charturi'] chartComponent['chart'] = ch chartComponent['charturi'] = chUri supportedArray = mappingProposalObject.getVisChannelsOfSupportedChart( chUri, chartComponentsArray, dimensions, measure) chartComponent['visualChannels'] = supportedArray resultArray.append(chartComponent) #print "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT", resultArray return resultArray except Exception as ex: raise Exception("-OutProcessorauto.process: %s" % ex)
def getVis(self): try: resultArrayForVis = [] sparqlqueryObjectD3 = "" '''if self.dataset == "http://data.lod2.eu/scoreboard/ds/indicator/i_iuolc_IND_TOTAL__ind_iu3": sparqlqueryObjectD3 = SPARQLQuery("http://open-data.europa.eu/en/sparqlep", 'regex')''' st = "http://data.lod2.eu/" if st in self.dataset: sparqlqueryObjectD3 = SPARQLQuery( 'http://open-data.europa.eu/en/sparqlep', 'regex') else: sparqlqueryObjectD3 = SPARQLQuery( 'http://zaire.dimis.fim.uni-passau.de:8890/sparql', 'virtuoso') #dimensions=sparqlqueryObjectD3.get_cube_dimensions_for_auto_mapping(self.dataset) measure = sparqlqueryObjectD3.get_cube_measure_for_auto_mapping( self.dataset) #valueOfMeasure=sparqlqueryObjectD3.get_value_of_cube_measure(self.dataset, dimensions, measure, 10) valueOfMeasure = sparqlqueryObjectD3.get_value_of_cube_measure( self.dataset, self.dimension, measure, 150) generatorFactoryObjectAuto = generatorfactoryforautomapping.GeneratorFactoryForAutoMapping( ) generatorauto = generatorFactoryObjectAuto.createFactoryauto( self.selectedChart, self.dimension, measure, valueOfMeasure, self.dataset) if generatorauto != None: generatorauto.transform() transformedResult = generatorauto.results resultObject = { 'name': self.selectedChart, 'start': transformedResult['code'] } transformedResult = generatorauto.results resultArrayForVis.append(resultObject) #print "ID###########################", resultArrayForVis return resultArrayForVis except Exception as ex: raise Exception("-OutProcessorauto.getVis: %s" % ex)
def process(self): try: chartComponentsArray = [] mappingProposalObject = mappingproposal.MappingProposal() sparqlqueryObjectD3 = "" mappingAlgorithmlObject = mappingalgorithm.MappingAlgorithm() '''st = "http://data.lod2.eu/" if st in self.dataset: sparqlqueryObjectD3 = SPARQLQuery('http://open-data.europa.eu/en/sparqlep', 'regex') else: sparqlqueryObjectD3 = SPARQLQuery('http://zaire.dimis.fim.uni-passau.de:8890/sparql', 'virtuoso')''' if not self.endpoint: self.endpoint = \ 'http://zaire.dimis.fim.uni-passau.de:8890/sparql' search_type = 'regex' time1 = time.time() sparqlqueryObjectD3 = SPARQLQuery(self.endpoint, search_type) time1_end = time.time() - time1 time2 = time.time() dimensions = sparqlqueryObjectD3.get_cube_dimensions_for_auto_mapping( self.dataset) time2_end = time.time() - time2 time3 = time.time() measure = sparqlqueryObjectD3.get_cube_measure_for_auto_mapping( self.dataset, self.deletedMeasure) time3_end = time.time() - time3 time4 = time.time() valueOfMeasure = sparqlqueryObjectD3.get_value_of_cube_measure( self.dataset, dimensions, measure, self.datasetFilters) time4_end = time.time() - time4 time5 = time.time() #chartComponentsArray = mappingProposalObject.getChartComponents() time5_end = time.time() - time5 time6 = time.time() mappingQueries = mappingProposalObject.getMappingQueries( dimensions, measure) time6_end = time.time() - time6 time7 = time.time() possibleVisualizations = mappingProposalObject.getPossibleVisualizationVariants( mappingQueries, dimensions, measure, valueOfMeasure) time7_end = time.time() - time7 '''print "TIMES: \n ------------------------------------" print "T1: ", time1_end print "T2: ", time2_end print "T3: ", time3_end print "T4: ", time4_end print "T5: ", time5_end print "T6: ", time6_end print "T7: ", time7_end''' #print "----------------possible vis------------------------------", possibleVisualizations return possibleVisualizations except Exception as ex: print("-OutProcessorauto.process: %s" % ex) raise Exception("%s" % ex)
def process(self): try: self.resultArray = [] mappingProposalObject = mappingproposal.MappingProposal() limitForGoogle = 10 limitForStreamgraph = 400 limitForD3 = 100 limitForGroupedBarChart = 100 #self.dataset="http://code-research.eu/resource/datasetpan_2009_ext" if not self.dataset: if self.IDofSelectedChart == "google" or self.IDofSelectedChart == "googleFull": sparqlqueryObject = SPARQLQuery( "http://open-data.europa.eu/en/sparqlep", 'regex') dimensions = sparqlqueryObject.get_cube_dimensions( "http://data.lod2.eu/scoreboard/ds/indicator/i_iuolc_IND_TOTAL__ind_iu3" ) measure = sparqlqueryObject.get_cube_measure( "http://data.lod2.eu/scoreboard/ds/indicator/i_iuolc_IND_TOTAL__ind_iu3" ) limit = limitForGoogle if self.IDofSelectedChart == "googleFull": limit = 1000 valueOfMeasure = sparqlqueryObject.get_value_of_cube_measure( "http://data.lod2.eu/scoreboard/ds/indicator/i_iuolc_IND_TOTAL__ind_iu3", dimensions, measure, limit) if self.IDofSelectedChart == "d3": sparqlqueryObjectD3 = SPARQLQuery( 'http://zaire.dimis.fim.uni-passau.de:8890/sparql', 'virtuoso') dimensions = sparqlqueryObjectD3.get_cube_dimensions( "http://code-research.eu/resource/datasetpan_2009_ext") measure = sparqlqueryObjectD3.get_cube_measure( "http://code-research.eu/resource/datasetpan_2009_ext") valueOfMeasure = sparqlqueryObjectD3.get_value_of_cube_measure( "http://code-research.eu/resource/datasetpan_2009_ext", dimensions, measure, limitForD3) if self.IDofSelectedChart == "streamgraph": sparqlqueryObject = SPARQLQuery( "http://open-data.europa.eu/en/sparqlep", 'regex') dimensions = sparqlqueryObject.get_cube_dimensions( "http://data.lod2.eu/scoreboard/ds/indicator/i_iuolc_IND_TOTAL__ind_iu3" ) measure = sparqlqueryObject.get_cube_measure( "http://data.lod2.eu/scoreboard/ds/indicator/i_iuolc_IND_TOTAL__ind_iu3" ) valueOfMeasure = sparqlqueryObject.get_value_of_cube_measure( "http://data.lod2.eu/scoreboard/ds/indicator/i_iuolc_IND_TOTAL__ind_iu3", dimensions, measure, limitForStreamgraph) if self.IDofSelectedChart == "grouped": sparqlqueryObject = SPARQLQuery( "http://open-data.europa.eu/en/sparqlep", 'regex') dimensions = sparqlqueryObject.get_cube_dimensions( "http://data.lod2.eu/scoreboard/ds/indicator/i_iuolc_IND_TOTAL__ind_iu3" ) measure = sparqlqueryObject.get_cube_measure( "http://data.lod2.eu/scoreboard/ds/indicator/i_iuolc_IND_TOTAL__ind_iu3" ) valueOfMeasure = sparqlqueryObject.get_value_of_cube_measure( "http://data.lod2.eu/scoreboard/ds/indicator/i_iuolc_IND_TOTAL__ind_iu3", dimensions, measure, limitForGroupedBarChart) else: if self.IDofSelectedChart == "google" or self.IDofSelectedChart == "googleFull": sparqlqueryObject = SPARQLQuery( "http://open-data.europa.eu/en/sparqlep", 'regex') dimensions = sparqlqueryObject.get_cube_dimensions( self.dataset) measure = sparqlqueryObject.get_cube_measure(self.dataset) limit = limitForGoogle if self.IDofSelectedChart == "googleFull": limit = 1000 valueOfMeasure = sparqlqueryObject.get_value_of_cube_measure( self.dataset, dimensions, measure, limit) if self.IDofSelectedChart == "d3": sparqlqueryObjectD3 = SPARQLQuery( 'http://zaire.dimis.fim.uni-passau.de:8890/sparql', 'virtuoso') dimensions = sparqlqueryObjectD3.get_cube_dimensions( self.dataset) measure = sparqlqueryObjectD3.get_cube_measure( self.dataset) valueOfMeasure = sparqlqueryObjectD3.get_value_of_cube_measure( self.dataset, dimensions, measure, limitForD3) if self.IDofSelectedChart == "streamgraph": sparqlqueryObject = SPARQLQuery( "http://open-data.europa.eu/en/sparqlep", 'regex') dimensions = sparqlqueryObject.get_cube_dimensions( self.dataset) measure = sparqlqueryObject.get_cube_measure(self.dataset) valueOfMeasure = sparqlqueryObject.get_value_of_cube_measure( self.dataset, dimensions, measure, limitForStreamgraph) if self.IDofSelectedChart == "grouped": sparqlqueryObject = SPARQLQuery( "http://open-data.europa.eu/en/sparqlep", 'regex') dimensions = sparqlqueryObject.get_cube_dimensions( self.dataset) measure = sparqlqueryObject.get_cube_measure(self.dataset) valueOfMeasure = sparqlqueryObject.get_value_of_cube_measure( self.dataset, dimensions, measure, limitForGroupedBarChart) mappingProposalObject.getChartComponents() firstPhaseMapping = mappingProposalObject.getFirstPhaseMapping() firstPhaseMappingForMeasure = mappingProposalObject.getFirstPhaseMappingForMeasure( ) generatorFactoryObject = generatorfactory.GeneratorFactory() generator = generatorFactoryObject.createFactory( self.selectedChart, dimensions, measure, valueOfMeasure) print "HERE I AM", dimensions if generator != None: generator.transform() transformedResult = generator.results columns = "" rows = "" if transformedResult.has_key("columns"): columns = transformedResult['columns'] if transformedResult.has_key("rows"): rows = transformedResult['rows'] resultObject = { 'name': self.selectedChart, 'start': transformedResult['code'], 'rows': rows, 'columns': columns } transformedResult = generator.results #print "ID###########################", resultObject self.resultArray.append(resultObject) except Exception as ex: raise Exception("-OutProcessor.process: %s" % ex)
def transform(self): try: self.results = {} lineArraytwo = [] labOfdm = "" code = self.codeObjectTwo['code'] tableForDim = {} xEntries = [] tableForDimArray= [] for entry in self.mappingInfoDimension: dim = entry['dimensionuri'] tableForDim = {'dimension' : ''} tableForDim['dimension'] = dim tableForDimArray.append(tableForDim) #print "DIMMMMMURIIIII", dim indexForXAxis = self.getDimensionIndex( "x-Axis") indexForLineAxis = self.getDimensionIndex( "Line") if len(tableForDimArray) == 2: lineArray = [] for i in range(len(tableForDimArray)-1): parallelcoordinatesGeneratorRows = {} for element in self.mappingInfoValue: xAxis = element['observation']['dimensionlabel%s'% (indexForXAxis)] #print "PC#####################################,x-Axis", xAxis line = element['observation']['dimensionlabel%s'% (indexForLineAxis)] #print "PC#####################################,line", line lineArray.append(line) yAxisArray = [] elementForXAxis = line yAxis = element['observation']['measurevalue'] #print "O:OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOo", yAxis if not yAxis: yAxis = str(0.0) valueObj = { xAxis : yAxis } if elementForXAxis in parallelcoordinatesGeneratorRows: parallelcoordinatesGeneratorRows[elementForXAxis].append(valueObj) else: parallelcoordinatesGeneratorRows[elementForXAxis] = yAxisArray parallelcoordinatesGeneratorRows[elementForXAxis].append(valueObj) xEntries.append(xAxis) xEntries = self.unique(xEntries) strResult = "[ " strResult = "[ " for element in parallelcoordinatesGeneratorRows: #print "parallelcoordinatesGeneratorRows##############################",parallelcoordinatesGeneratorRows values = parallelcoordinatesGeneratorRows[ element ] strValueObject = "" #print "Values##################################################", values strContent = "" valueKeys = [] for value in values: for key in value.keys(): #print "key##############################",key valueKeys.append(key) if value[key]: strContent = strContent + '"'+key+'"'+":" + value[key]+"," else: strContent = strContent + '"'+key+'"'+":0.0," strContent = strContent + '"'+key+'"'+":" + value[key]+"," + '"'+"Labels"+'"'+":" + '"'+element+'"'+"," #strContent = strContent + '"'+key+'"'+":" + value[key]+"," #print "key##############################",key #gib nullen dazu for xValue in xEntries: if xValue in valueKeys: strContent = strContent else: strContent = strContent + '"'+xValue+'"'+":0.0," tempList = list(strContent) tempList[len(tempList)-1]="" strEndContent = "".join(tempList) strValueObject = "{" +strEndContent+ "}, " toDictObject = strValueObject strResult = strResult + toDictObject tempList = list(strResult) tempList[len(tempList)-2]="" strEndResult = "".join(tempList) strResult = strEndResult + "]" lineArraytwo = [] for i in range(len(lineArray)): #print "##########################", lineArray[i] lineObject = {'name': lineArray[i]} lineArraytwo.append(lineObject) lineArraytwo = self.unique(lineArraytwo) code=code.replace("@@@DATA@@@", "".join(strResult)) code=code.replace("@@@LINE@@@", simplejson.dumps(str(lineArraytwo))) print "CODEEEEEEEEEEEEEEEEEEEEEEEEEE", strResult '''self.results['code']=codeTwo print "CODEEEEEEEEEEEEEEEEEEEEEEEEEE", self.results''' if len(tableForDimArray) == 3: xAxisUri = "" code=self.codeObject['code'] #print "lllllllllllllllllllllllllllll", code lineArray = [] indexForLine2Axis = self.getDimensionIndex2("Line", indexForXAxis, indexForLineAxis ) #print "##########################################", indexForXAxis, "####################################", indexForLineAxis, "###############", indexForLine2Axis for i in range(len(tableForDimArray)-2): parallelcoordinatesGeneratorRows = {} for element in self.mappingInfoValue: xAxis = element['observation']['dimensionlabel%s'% (indexForXAxis)] xAxisUri = element['observation']['dimensionuri%s'% (indexForXAxis)] line = element['observation']['dimensionlabel%s'% (indexForLineAxis)] lineArray.append(line) line2 = element['observation']['dimensionlabel%s'% (indexForLine2Axis)] yAxisArray = [] elementForXAxis = line2 yAxis = element['observation']['measurevalue'] valueObject = {line : yAxis} valObj = {xAxis : valueObject} if elementForXAxis in parallelcoordinatesGeneratorRows: parallelcoordinatesGeneratorRows[elementForXAxis].append(valObj) else: parallelcoordinatesGeneratorRows[elementForXAxis] = yAxisArray parallelcoordinatesGeneratorRows[elementForXAxis].append(valObj) st = "http://data.lod2.eu/" sparqlqueryObject = "" if st in self.dataset: sparqlqueryObject = SPARQLQuery('http://open-data.europa.eu/en/sparqlep', 'regex') else: sparqlqueryObject = SPARQLQuery('http://zaire.dimis.fim.uni-passau.de:8890/sparql', 'virtuoso') nullIndexUri = "" if indexForXAxis == 1: nullIndexUri = element['observation']['dimensionuri%s'% (indexForXAxis)] if indexForLineAxis == 1: nullIndexUri = element['observation']['dimensionuri%s'% (indexForLineAxis)] if indexForLine2Axis == 1: nullIndexUri = element['observation']['dimensionuri%s'% (indexForLine2Axis)] #print "GENOMMEN: :: :: ::: : ", nullIndexUri labelOfdim = sparqlqueryObject.get_label_of_dimensions(self.dataset,nullIndexUri ) labOfdm = labelOfdim [0]['label'] #print "LABEL OF DIM: ", labelOfdim lineBundle = [ ] for element in parallelcoordinatesGeneratorRows: #print "ELEMENT##################################### ", element values = parallelcoordinatesGeneratorRows[ element ] strValueObject = "" strContent = "" valueKeys = [] for value in values: for element2 in value: vals = value[ element2 ] for key in vals.keys(): commonElementMap = self.getCommonElementMap( lineBundle, key ) if commonElementMap: mapAttr = self.getCommonElementMap(commonElementMap, element ) if mapAttr: objMap = { 'attr' : element2, 'value': vals[key]} mapAttr.append(objMap) else: objMap = { 'common': element, 'map': [ { 'attr' : element2, 'value': vals[key]} ]} commonElementMap.append(objMap) else: obj = { 'common' : key, 'map' : [] } mapAttr = { 'common': element, 'map': [ { 'attr' : element2, 'value': vals[key]} ]} obj['map'].append(mapAttr) lineBundle.append(obj) self.sortLineBundle(lineBundle) attrArray = [] strResult = "[ " for item in lineBundle: mapArray = item ['map'] #print "ELEMENT ----------------------", mapArray for maps in mapArray: mapArrayTwo = maps ['map'] comm = maps ['common'] comArray = [] if comm.isdigit(): comArray = [{labOfdm: comm}] #print "GEMAPPT: KEY: ", comArray else: #print "false", comm comArray = [{'Labels': comm}] generatorRows = {} for attrs in mapArrayTwo: att = attrs ['attr'] val = attrs ['value'] attrObject = {att : val } #print "HEREEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE", attrObject if comm in generatorRows: generatorRows[comm ].append(attrObject) attrArray. append (generatorRows) else: generatorRows[comm] = comArray generatorRows[comm].append(attrObject) attrArray. append (generatorRows) #print "HEREEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE", comArray attrArrays = self.unique(attrArray) strValueObject = "" strContent = "" valueKeys = [] finalArray = [] for element in attrArrays: #print "element##################################", element for x in element: #print "------------------------------------->", element[key] elementArray = element[x] #print "strcontenntttt ############", elementArray, "xxxxxxxxxxxxxxxxxxxxxxx", x #for entry in elementArray: sContent = "" for i in range(len(elementArray)): #print "lLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLl", elementArray[i] entry = elementArray[i] for key in entry.keys(): sContent = sContent + '"'+key+'"'+":" + '"'+entry[key]+'"'+',' #print "KEYYYYYYYYYYYYYYYYYYYYYYYYYYyy", key, " ", entry[key] tempList2 = list(sContent) tempList2[len(tempList2)-1]="" sContent = "".join(tempList2) sContent = "{ " + sContent + "}, " #print ">>> SCONTENT: ", sContent strContent = strContent + sContent #print "##########################################", strContent tempList = list(strContent) tempList[len(tempList)-1]="" strEndContent = "".join(tempList) #strValueObject = "{" +strEndContent+ "}, " toDictObject = strEndContent strResult = strResult + toDictObject tempList = list(strResult) tempList[len(tempList)-1]="" strEndResult = "".join(tempList) strResult = strEndResult + "]" lineArraytwo = [] for i in range(len(lineArray)): #print "##########################", lineArray[i] lineObject = {'name': lineArray[i]} lineArraytwo.append(lineObject) #print "RESULTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT", strResult lineArraytwo = self.unique(lineArraytwo) code = code.replace("@@@LINE@@@", simplejson.dumps((lineArraytwo))) code = code.replace("@@@DATA@@@", "".join(strResult)) #print "CODE\n", code ''' lineStr = "" for i in range(len(lineArray)): #print "LINESSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS", lineArray lineStr = lineStr + """ var colorText%s = "<font style=\'background-color: "+foreground.strokeStyle+"\'> </FONT>" var line%s = %s; globalLegend.push( {"cluster":line%s, "color": colorText%s} ); """%(i, i, '"' + lineArray[i] + '"', i, i) code=code.replace("@@@LINE@@@", lineStr)''' #self.results['code']=code self.results['code']=code indexForXAxis = None indexForLineAxis = None indexForLineAxis = None #addRows=self.transformRows(parallelcoordinatesGeneratorRowsArray) except Exception as ex: raise Exception("-ParallelGenerator.transform: %s"%ex)
def aggregate(request): """ Create a new RDF Data Cube based on the received parameters. Return a JSON object with the newly created cube. """ data = simplejson.loads(request.body) if not data.get('label'): data['label'] = 'Aggregated dataset' sparql = SPARQLQuery(data['endpoint_url'], data['search_type']) response = sparql.get_aggregated_data(data['dataset_uri'], data['grouped_dimensions'], data['aggregated_measures']) ### Header row header_row = "" # Dimensions for index, dimension in enumerate(data['grouped_dimensions']): header_row += Template(""" <td data-component="dimension" data-range="http://www.code-research.eu/resource#CubeDimensionNominal" data-url="$uri"> <p>$label</p> </td> """).substitute({ 'uri': dimension['uri'], 'label': dimension['label'], }) # Measures unique_measure_counter = 1 used_uris = [] for index, measure in enumerate(data['aggregated_measures']): # Make sure every URI is unique uri = measure['uri'] if uri in used_uris: uri = uri + '_' + str(unique_measure_counter) unique_measure_counter += 1 used_uris.append(uri) header_row += Template(""" <td data-component="observation" data-range="http://www.code-research.eu/resource#CubeObservationNumber" data-url="$uri"> <p>$function of $label</p> </td> """).substitute({ 'uri': uri, 'label': measure['label'], 'function': measure['function'].title() }) ### Data rows data_rows = "" for row in response['results']['results']['bindings']: data_rows += "<tr>" # Dimensions for index, dimension in enumerate(data['grouped_dimensions']): uri = "" if "http" in row['d' + str(index)]['value']: uri = row['d' + str(index)]['value'] if row.get('d' + str(index) + 'label'): label = row['d' + str(index) + 'label']['value'] else: label = shorten_uri(row['d' + str(index)]['value']) if not uri: uri = 'http://code-research.eu/resource/' + \ urllib.quote_plus(label) data_rows += Template(""" <td data-component="dimension" data-range="http://www.code-research.eu/resource#CubeDimensionNominal" data-url="$uri"> <p>$label</p> </td> """).substitute({ 'uri': uri, 'label': cgi.escape(label), }) # Measures for index, measure in enumerate(data['aggregated_measures']): data_rows += Template(""" <td data-component="observation" data-range="http://www.code-research.eu/resource#CubeObservationNumber"> <p>$value</p> </td> """).substitute({ 'function': measure['function'].title(), 'value': row['m' + str(index) + 'agg']['value'], }) data_rows += "</tr>" html_table = Template(""" <table data-source="$source" data-relation="$relation" data-auth="$author" data-description="$description" data-label="$label"> <tbody> <tr> $header_row </tr> $data_rows </tbody> </table> """).substitute({ 'header_row': header_row, 'data_rows': data_rows, 'source': data.get('source'), 'relation': data.get('relation'), 'author': data.get('importer'), 'description': data.get('description'), 'label': data.get('label'), }).replace('\n', '') while True: (html_table, occurrences) = re.subn(r'> ', r'>', html_table) if occurrences == 0: break while True: (html_table, occurrences) = re.subn(r'" ', r'" ', html_table) if occurrences == 0: break html_table = html_table.strip() # ZAIRE Cubificatoin start: #cubification_request = requests.post( # 'http://zaire.dimis.fim.uni-passau.de:8383' + # '/code-server/demo/dataextraction/headless', # data={ # 'htmlTable': html_table, # } #) #aggregated_dataset_uri = urllib.quote_plus( # cubification_request.json()['dataset'] #) #response = { # 'dataset': aggregated_dataset_uri, # 'endpoint': 'http://zaire.dimis.fim.uni-passau.de:8890/sparql', #} # / ZAIRE Cubificatoin end. # Local Cubificatoin start: aggregated_dataset_uri = cubegen.create_and_save_cube(html_table) response = { 'dataset': aggregated_dataset_uri, 'endpoint': settings.LOCAL_SPARQL_ENDPOINT, } # / Local Cubificatoin end. return HttpResponse(simplejson.dumps(response), mimetype='application/json')
def service(request): cmd = "" if request.method == 'GET': cmd = request.GET['cmd'] else: cmd = request.POST['cmd'] mappingProposalObject = mappingproposal.MappingProposal() response = HttpResponse() if (cmd == "getPreview"): try: ds = request.POST['dataset'] chart = request.POST['chart'] chartID = request.POST['chartid'] inProcessorObject = inprocessor.InProcessor(ds, chart, chartID) inProcessorObject.process() resultArray = inProcessorObject.resultArray return HttpResponse(json.dumps(resultArray)) except Exception as inst: msg = "ERROR (code - queryPreview): [%s] %s" % (type(inst), inst) print msg mappingProposalObject.reInit() return HttpResponse(json.dumps({'error': '' + msg + ''})) if (cmd == "getPreviewAuto"): try: ds = request.POST['dataset'] chart = request.POST['chart'] dimension = request.POST['dimension'] resultArray = [] inProcessorObject = inprocessorauto.InProcessorAuto( ds, chart, dimension) resultArray = inProcessorObject.process() '''for i in range(len(resultArray)): supChart = resultArray[i]['chart'] supChartUri = resultArray[i]['charturi'] chartArray.append(supChart)''' #print "KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK", resultArray return HttpResponse(json.dumps(resultArray)) except Exception as inst: msg = "ERROR (code - queryPreviewAuto): [%s] %s" % (type(inst), inst) print msg #mappingProposalObject.reInit() return HttpResponse(json.dumps({'error': '' + msg + ''})) if (cmd == "getVisualization"): try: dimUriArray = [] ds = request.POST['dataset'] chart = request.POST['chart'] dimension = json.loads(request.POST['dimension']) #print "HIERRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRr", dimension dimUri = "" for elements in dimension: dimUri = elements['dimensionuri'] dim = elements['dim'] dimUriObject = {'dimensionuri': dimUri, 'dim': dim} dimUriArray.append(dimUriObject) inProcessorObject = inprocessorauto.InProcessorAuto( ds, chart, dimUriArray) resultArray = inProcessorObject.getVis() #resultArray=inProcessorObject.resultArray return HttpResponse(json.dumps(resultArray)) except Exception as inst: msg = "ERROR (code - queryVisualization): [%s] %s" % (type(inst), inst) print msg mappingProposalObject.reInit() return HttpResponse(json.dumps({'error': '' + msg + ''})) if (cmd == "getDimension"): try: ds = request.POST['dataset'] sparqlqueryObjectD3 = "" if ds == "http://data.lod2.eu/scoreboard/ds/indicator/i_iuolc_IND_TOTAL__ind_iu3": sparqlqueryObjectD3 = SPARQLQuery( "http://open-data.europa.eu/en/sparqlep", 'regex') else: sparqlqueryObjectD3 = SPARQLQuery( 'http://zaire.dimis.fim.uni-passau.de:8890/sparql', 'virtuoso') dimensions = sparqlqueryObjectD3.get_cube_dimensions_for_auto_mapping( ds) return HttpResponse(json.dumps(dimensions)) except Exception as inst: msg = "ERROR (code - getDimension): [%s] %s" % (type(inst), inst) print msg mappingProposalObject.reInit() return HttpResponse(json.dumps({'error': '' + msg + ''})) if (cmd == "getMeasure"): try: ds = request.POST['dataset'] sparqlqueryObjectD3 = "" if ds == "http://data.lod2.eu/scoreboard/ds/indicator/i_iuolc_IND_TOTAL__ind_iu3": sparqlqueryObjectD3 = SPARQLQuery( "http://open-data.europa.eu/en/sparqlep", 'regex') else: sparqlqueryObjectD3 = SPARQLQuery( 'http://zaire.dimis.fim.uni-passau.de:8890/sparql', 'virtuoso') dimensions = sparqlqueryObjectD3.get_cube_measure_for_auto_mapping( ds) #print "HIERRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR", dimensions return HttpResponse(json.dumps(dimensions)) except Exception as inst: msg = "ERROR (code - getMeasure): [%s] %s" % (type(inst), inst) print msg mappingProposalObject.reInit() return HttpResponse(json.dumps({'error': '' + msg + ''})) if (cmd == "getD3Data"): try: #chart=request.GET['chart'] chartID = request.GET['chartid'] ds = request.GET['dataset'] inProcessorObject = inprocessor.InProcessor(ds, "d3data", chartID) inProcessorObject.process() resultArray = inProcessorObject.resultArray return HttpResponse(json.dumps(resultArray)) except Exception as ex: msg = "ERROR (code - getD3Data): [%s] %s" % (type(ex), ex) print msg
def create_and_save_cube(html_table): print(html_table) """Create a RDF Data Cube based on a semantically annotated HTML table.""" if not html_table: html_table = """ <table data-auth="822" data-description="Budget and Gross Income" data-label="Toy Story Movies" data-relation="Query Wizard" data-source= "http://code.know-center.tugraz.at/search#?p0=http%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23label&p0i=false&p0ft=search&p0fv=toy%20story&p1=http%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23type&p1i=false&p2=http%3A%2F%2Fdbpedia.org%2Fontology%2Fbudget&p2i=false&p2ft=not_empty&p2fv=1&p3=http%3A%2F%2Fdbpedia.org%2Fontology%2Fgross&p3i=false&endpoint=http%3A%2F%2Fdbpedia.org%2Fsparql&searchtype=virtuoso" id="results"> <tbody> <tr> <td data-component="dimension" data-url= "http://www.w3.org/2000/01/rdf-schema#label"> <p>Label</p> </td> <td data-component="observation" data-url= "http://dbpedia.org/ontology/budget"> <p>Budget ($)</p> </td> <td data-component="observation" data-url= "http://dbpedia.org/ontology/gross"> <p>Gross ($)</p> </td> </tr> <tr> <td data-component="dimension" data-inverse="false" data-p= "http://www.w3.org/2000/01/rdf-schema#label" data-range= "http://code-research.eu/resource#CubeDimensionNominal" data-s= "http://dbpedia.org/resource/Toy_Story" data-url= "http://dbpedia.org/resource/Toy_Story"> <p>Toy Story</p> </td> <td data-component="observation" data-inverse="false" data-p= "http://dbpedia.org/ontology/budget" data-range= "http://code-research.eu/resource#CubeObservationNumber" data-s="http://dbpedia.org/resource/Toy_Story" data-url= "http://dbpedia.org/resource/Toy_Story"> <p>30000000</p> </td> <td data-component="observation" data-inverse="false" data-p= "http://dbpedia.org/ontology/gross" data-range= "http://code-research.eu/resource#CubeObservationNumber" data-s="http://dbpedia.org/resource/Toy_Story" data-url= "http://dbpedia.org/resource/Toy_Story"> <p>361958736</p> </td> </tr> <tr> <td data-component="dimension" data-inverse="false" data-p= "http://www.w3.org/2000/01/rdf-schema#label" data-range= "http://code-research.eu/resource#CubeDimensionNominal" data-s= "http://dbpedia.org/resource/Toy_Story_2" data-url= "http://dbpedia.org/resource/Toy_Story_2"> <p>Toy Story 2</p> </td> <td data-component="observation" data-inverse="false" data-p= "http://dbpedia.org/ontology/budget" data-range= "http://code-research.eu/resource#CubeObservationNumber" data-s="http://dbpedia.org/resource/Toy_Story_2" data-url= "http://dbpedia.org/resource/Toy_Story_2"> <p>90000000</p> </td> <td data-component="observation" data-inverse="false" data-p= "http://dbpedia.org/ontology/gross" data-range= "http://code-research.eu/resource#CubeObservationNumber" data-s="http://dbpedia.org/resource/Toy_Story_2" data-url= "http://dbpedia.org/resource/Toy_Story_2"> <p>485015179</p> </td> </tr> <tr> <td data-component="dimension" data-inverse="false" data-p= "http://www.w3.org/2000/01/rdf-schema#label" data-range= "http://code-research.eu/resource#CubeDimensionNominal" data-s= "http://dbpedia.org/resource/Toy_Story_3" data-url= "http://dbpedia.org/resource/Toy_Story_3"> <p>Toy Story 3</p> </td> <td data-component="observation" data-inverse="false" data-p= "http://dbpedia.org/ontology/budget" data-range= "http://code-research.eu/resource#CubeObservationNumber" data-s="http://dbpedia.org/resource/Toy_Story_3" data-url= "http://dbpedia.org/resource/Toy_Story_3"> <p>200000000</p> </td> <td data-component="observation" data-inverse="false" data-p= "http://dbpedia.org/ontology/gross" data-range= "http://code-research.eu/resource#CubeObservationNumber" data-s="http://dbpedia.org/resource/Toy_Story_3" data-url= "http://dbpedia.org/resource/Toy_Story_3"> <p>1063171911</p> </td> </tr> </tbody> </table> """ print "******************** html table ************************" print html_table print "******************** end html table ************************" ########################################################################### # Read the table # http://www.crummy.com/software/BeautifulSoup/ soupParser = BeautifulSoup(html_table) # Label will serve to name the dsd and the dataset table = soupParser.find('table') label = table['data-label'] description = table['data-description'] # header contains dimensions and measures header = soupParser.find('tr') headerComponents = [] dCount = 0 mCount = 0 timestamp = get_timestamp() print("************************ BEAUTIFUL SOUP ************************ ") for h in header.find_all('td'): if h['data-component'] == 'dimension': headerComponents.append({ 'value': h.find('p').string, 'type': 'dimension', 'id': 'dimension_' + str(dCount) + '_' + str(timestamp), 'uri': h['data-url'], }) dCount = dCount + 1 print "data-url = ", h['data-url'] if h['data-component'] == 'observation': headerComponents.append({ 'value': h.find('p').string, 'type': 'measure', 'id': 'measure_' + str(mCount) + '_' + str(timestamp), 'uri': h['data-url'], }) mCount = mCount + 1 # Observations # Create the namespaces QB = Namespace("http://purl.org/linked-data/cube#") CODE = Namespace("http://code-research.eu/resource/") VA = Namespace("http://code-research.eu/ontology/visual-analytics#") SDMX_MEASURE = Namespace("http://purl.org/linked-data/sdmx/2009/measure#") # OWL = Namespace("http://www.w3.org/2002/07/owl#") observations = soupParser.find_all('tr') obsComponents = [] firstTableRow = True for o in observations: # The first table row contains the structure and no observations if firstTableRow: firstTableRow = False continue # to the next round of the loop anObs = [] for i, item in enumerate(o.find_all('td')): value = str(item.find('p').string) item_value = value value = urllib.quote_plus(value) item_id = value + '_' + str(timestamp) item_obs_type = headerComponents[i]['type'] item_parent = CODE[headerComponents[i]['id']] if item_obs_type == 'dimension' and headerComponents[i].get('uri'): item_parent = URIRef(headerComponents[i].get('uri')) anObs.append({ 'id': item_id, 'uri': item.get('data-url'), 'value': item_value, 'parent': item_parent, 'obs-type': item_obs_type, }) obsComponents.append(anObs) print obsComponents ########################################################################### # Create the RDF Data Cube # http://rdflib.readthedocs.org/en/latest/intro_to_creating_rdf.html g = Graph() # dataset = URIRef('dataset_' + str(urllib.quote_plus(label)) + '_' + str(timestamp)) dataset = URIRef('dataset_' + str(timestamp)) # dsd = URIRef('dsd_' + str(urllib.quote_plus(label)) + '_' + str(timestamp)) dsd = URIRef('dsd_' + str(timestamp)) ### Dsd definition g.add((CODE[dsd], RDF.type, QB.DataStructureDefinition)) ### Components for c in headerComponents: id = c['id'] component = 'component_' + id g.add((CODE[dsd], QB.component, CODE[component])) if c['type'] == 'dimension': tempSubject = CODE[id] if c.get('uri'): tempSubject = URIRef(c.get('uri')) g.add((CODE[component], QB.dimension, tempSubject)) else: g.add((CODE[component], QB.measure, CODE[id])) g.add((CODE[component], RDFS.label, Literal(str(c['value']), lang='en'))) ### Dimensions & measures for c in headerComponents: id = c['id'] # if c.get('uri'): # g.add((CODE[id], OWL.equivalentProperty, URIRef(c.get('uri')))) if c['type'] == 'dimension': tempSubject = CODE[id] if c.get('uri'): tempSubject = URIRef(c.get('uri')) g.add((tempSubject, RDF.type, RDF.Property)) g.add((tempSubject, RDF.type, QB.DimensionProperty)) g.add((tempSubject, RDFS.label, Literal(str(c['value']), lang='en'))) g.add((tempSubject, RDFS.subPropertyOf, VA.cubeDimensionNominal)) g.add((tempSubject, RDFS.range, XSD.string)) else: g.add((CODE[id], RDF.type, RDF.Property)) g.add((CODE[id], RDF.type, QB.MeasureProperty)) g.add((CODE[id], RDFS.label, Literal(str(c['value']), lang='en'))) g.add((CODE[id], RDFS.subPropertyOf, SDMX_MEASURE.obsValue)) g.add((CODE[id], RDFS.range, XSD.decimal)) ### Dataset definition g.add((CODE[dataset], RDF.type, QB.DataSet)) g.add((CODE[dataset], RDFS.label, Literal(str(label)))) g.add((CODE[dataset], RDFS.comment, Literal(str(description)))) g.add((CODE[dataset], QB.structure, CODE[dsd])) ### Observations obsCount = 0 for o in obsComponents: # Add observation observation = 'obs_' + str(obsCount) + '_' + str(timestamp) g.add((CODE[observation], RDF.type, QB.Observation)) for item in o: parent = item['parent'] if item['obs-type'] == 'dimension': entity = 'entity_' + item['id'] # Add entity g.add((CODE[entity], RDF.type, CODE.Entity)) g.add((CODE[entity], RDFS.label, Literal(str(item['value'])))) # if item.get('uri'): # g.add((CODE[entity], OWL.sameAs, URIRef(item.get('uri')))) # Add entity to observation g.add((CODE[observation], parent, CODE[entity])) else: # Add number measured to observation g.add( (CODE[observation], parent, Literal(float(item['value'])))) g.add((CODE[observation], QB.dataSet, CODE[dataset])) obsCount = obsCount + 1 ### Prefix binding g.bind('qb', URIRef("http://purl.org/linked-data/cube#")) g.bind('code', URIRef("http://code-research.eu/resource/")) g.bind('va', URIRef("http://code-research.eu/ontology/visual-analytics#")) g.bind('sdmx-measure', URIRef("http://purl.org/linked-data/sdmx/2009/dimension#")) # g.bind('owl', URIRef("http://www.w3.org/2002/07/owl#")) serialized_graph = g.serialize(format='nt') print("************************ CUBE ******************************* ") print(g.serialize(format='nt')) # Save the Cube to Virtuoso sparql = SPARQLQuery(settings.LOCAL_SPARQL_ENDPOINT, 'virtuoso') query = "INSERT DATA INTO GRAPH <http://code-research.eu/graph/" + str( timestamp) + "> {\n" query += serialized_graph query += "}" response = sparql.update(query) # Return some status message if response['results']['bindings'][0]['callret-0']['value']: return str(CODE[dataset]) else: return "Looks like it didn't work ..."