Ejemplo n.º 1
0
	def getResponse(self, inValue, inUnit, outFormat):

		# Process values passed from the UI.
		
		# Value
		orig_value = float(inValue)

		# Format
		out_format = outFormat
		if out_format != "json" and out_format != "json-ld":
			raise ValueError("Invalid return format. Possible values are 'json' and 'json-ld'.")

		# Unit
		orig_unit = inUnit
		quantity = self.decideContext(orig_unit)
		base_unit = units.baseUnit(quantity)
		
		print(RequestHandler.logString + "User query: " + str(orig_value) + " " + str(orig_unit) + ", return " + out_format)
		print(RequestHandler.logString + "Quantity: " + str(quantity))
		print(RequestHandler.logString + "Base unit: " + str(base_unit))

		# Instance of GraphBuilder which builds the RDF graph.
		graphBuilder = GraphBuilder()

		# Start building the final RDF graph. The "request" and part of the "query"
		# section are produced now.
		try:
			requestGraph = self.buildRequestGraph(graphBuilder, orig_unit, orig_value)
		except Exception:
			raise RuntimeError("Building the request graph failed!")

		# For debugging:
		#print(requestGraph.serialize(format='turtle'))

		# Input is normalized to base unit.
		norm_value = conv.convert(orig_value, orig_unit, quantity, RequestHandler.logString)

		# Ask results from the wrappers.
		finalGraph = self.getFinalGraph(quantity, norm_value, base_unit, graphBuilder, out_format, orig_unit, orig_value)

		# Pass to calling method.
		return finalGraph
Ejemplo n.º 2
0
	def getResource(self, inWrapper, inValue, inUnit, inRange):
		
		# Process values passed from the user.
		
		# Wrapper
		wrapper = inWrapper
		if wrapper != "dbpedia" and wrapper != "worldbank" and wrapper != "wikidata":
			raise ValueError("Invalid datasource. Possible values are 'dbpedia', 'worldbank', and 'wikidata'.")

		# Value
		orig_value = float(inValue)

		# Range
		range_decimal = float(inRange)

		# Unit
		orig_unit = inUnit
		quantity = self.decideContext(orig_unit)
		base_unit = units.baseUnit(quantity)

		# Input is normalized to base unit.
		query_value = conv.convert(orig_value, orig_unit, quantity, RequestHandler.logString)

		print(RequestHandler.logString + "User query: " + str(orig_value) + " " + str(orig_unit) + ", range " + str(range_decimal))
		print(RequestHandler.logString + "Quantity: " + str(quantity))

		# Possible range of results:
		range = range_decimal*query_value

		# Instance of GraphBuilder which builds the RDF graph.
		graphBuilder = GraphBuilder()

		# Build request graph
		try:
			requestGraph = self.buildRequestGraph(graphBuilder, base_unit, query_value)
		except Exception:
			raise RuntimeError("Building the request graph failed!")

		# For debugging:
		#requestGraph.serialize(destination='graph_tests/interfaceRequestGraph.txt', format='turtle')

		# Get data from wrapper
		rdfResult = self.getData(wrapper, float(query_value), base_unit, float(range))
	
		# Merge request and result (no factor here).
		if rdfResult is not None:

			# For debugging, uncomment:
			#rdfResult.serialize(destination='graph_tests/interfaceResultGraph.txt', format='turtle')

			# Test whether merging graphs works
			try:
				finalGraph = graphBuilder.mergeWithResultGraph(rdfResult)
			except Exception:
				raise RuntimeError("Merging request and result graphs failed!")

			# Convert output graph to JSON-LD and save as file (for debugging).
			#finalGraph.serialize(destination='graph_tests/interfaceFinalGraph_JSONLD.txt', format='json-ld', indent=4)
			#finalGraph.serialize(destination='graph_tests/interfaceFinalGraph.txt', format='turtle')

			# Return to API user.
			print(finalGraph.serialize(format='json-ld', indent=4))
			return finalGraph.serialize(format='json-ld', indent=4)

		# If no result available:
		else:
			print("No results returned.")
			return None