Exemple #1
0
def single_from_id(author_num, unigraph, cutoff):
	"""
	Takes node id, full graph and cutoff and returns graph with all nodes reachable by source node up to a distance of cutoff
	"""
	# make full node id
	author_id = "http://eprints.gla.ac.uk/view/author/" + author_num + ".html"
	# Node not found, return error message
	if author_id not in graph_utils.get_node_set(unigraph):
		errorMessage = json.dumps({"error": "Sorry, the author was not found"})
		return HttpResponse(errorMessage, content_type='application/json')

	# Make and return single author graph
	author_graph = graph_utils.single_author_graph(unigraph, author_id, cutoff)

	graphdata = graph_utils.json_from_graph(author_graph)
	newdata = json.dumps(graphdata)

	return HttpResponse(newdata, content_type='application/json')
Exemple #2
0
def longest_from_id(source_num, unigraph):
	"""
	Takes the id for a node and a graph and returns a graph of the node's path to its furthest reachable node
	"""
	# Make full node id out of the id number
	source_id = "http://eprints.gla.ac.uk/view/author/" + source_num + ".html"

	# Check if node in graph, if not return error message
	if source_id not in graph_utils.get_node_set(unigraph):
		errorMessage = json.dumps({"error": "Sorry, the author was not found"})
		return HttpResponse(errorMessage, content_type='application/json')	

	# Otherwise get longest path and make and return path graph
	longest_path = graph_utils.get_longest_path(unigraph, source_id)
	path_graph = graph_utils.make_path_graph(longest_path, unigraph)

	graphdata = graph_utils.json_from_graph(path_graph)
	newdata = json.dumps(graphdata)
	return HttpResponse(newdata, content_type='application/json')	
Exemple #3
0
def path_graph_from_ids(source_num, target_num, unigraph):
	"""
	Takes the node ids for source and target and the graph and returns a graph for the path between source and target
	"""

	# Use the ids provided to make urls (which are the actual node ids used in the graph)
	source_id = "http://eprints.gla.ac.uk/view/author/" + source_num + ".html"
	target_id = "http://eprints.gla.ac.uk/view/author/" + target_num + ".html"

	# Get the set of nodes in the graph to check for the source and target
	uni_node_set = graph_utils.get_node_set(unigraph)
	# Check if source and target in graph, if not return error message
	if source_id not in uni_node_set and target_id not in uni_node_set:
		errorMessage = json.dumps({"error": "Sorry, neither author was found"})
		return HttpResponse(errorMessage, content_type='application/json')
	elif source_id not in uni_node_set:
		errorMessage = json.dumps({"error": "Sorry, the source author was not found"})
		return HttpResponse(errorMessage, content_type='application/json')
	elif target_id not in uni_node_set:
		errorMessage = json.dumps({"error": "Sorry, the target author was not found"})
		return HttpResponse(errorMessage, content_type='application/json')		
		
	# If both were found, get the path betwen them
	s_path = graph_utils.get_path(unigraph, source_id, target_id)
	# If s_path is false, no path was found between the authors, return error message
	if not s_path:
		errorMessage = json.dumps({"error": "Sorry, no path was found between the authors"})
		return HttpResponse(errorMessage, content_type='application/json')	
	
	# Otherwise make a graph connecting the path between the authors and return
	path_graph = graph_utils.make_path_graph(s_path, unigraph)

	graphdata = graph_utils.json_from_graph(path_graph)
	newdata = json.dumps(graphdata)

	return HttpResponse(newdata, content_type='application/json')