Property definitions

neo4j $ NeoNode :: defaultinit
# Nodes are used to represent entities stored in base.
# Apart from properties and relationships (edges),
# nodes can also be labeled with zero or more labels.
#
# A label is a `String` that is used to group nodes into sets.
# All nodes labeled with the same label belongs to the same set.
# A node may be labeled with any number of labels, including none,
# making labels an optional addition to the graph.
#
# Creating new nodes:
#
#     var client = new Neo4jClient("http://neo4j:7474")
#
#     var andres = new NeoNode
#     andres.labels.add "Person"
#     andres["name"] = "Andres"
#     andres["age"] = 22
#     client.save_node(andres)
#     assert andres.is_linked
#
# Get nodes from Neo4j:
#
#     var url = andres.url.to_s
#     var node = client.load_node(url)
#     assert node["name"] == "Andres"
#     assert node["age"].to_s.to_i	== 22
class NeoNode
	super NeoEntity

	private var internal_labels: nullable Array[String] = null
	private var internal_in_edges: nullable List[NeoEdge] = null
	private var internal_out_edges: nullable List[NeoEdge] = null

	init do
		super
		self.internal_labels = new Array[String]
		self.internal_in_edges = new List[NeoEdge]
		self.internal_out_edges = new List[NeoEdge]
	end

	redef fun to_s do
		var tpl = new FlatBuffer
		tpl.append "\{"
		tpl.append "labels: [{labels.join(", ")}],"
		tpl.append "data: {properties.to_json}"
		tpl.append "\}"
		return tpl.write_to_string
	end

	# A label is a `String` that is used to group nodes into sets.
	# A node may be labeled with any number of labels, including none.
	# All nodes labeled with the same label belongs to the same set.
	#
	# Many database queries can work with these sets instead of the whole graph,
	# making queries easier to write and more efficient.
	#
	# Labels are loaded lazily
	fun labels: Array[String] do return internal_labels or else load_labels

	private fun load_labels: Array[String] do
		var labels = new Array[String]
		var res = neo.get(url.to_s / "labels")
		if res isa JsonArray then
			for val in res do labels.add val.to_s
		end
		internal_labels = labels
		return labels
	end

	# Get the list of `NeoEdge` pointing to `self`
	#
	# Edges are loaded lazily
	fun in_edges: List[NeoEdge] do return internal_in_edges or else load_in_edges

	private fun load_in_edges: List[NeoEdge] do
		var edges = new List[NeoEdge]
		var res = neo.get(url.to_s / "relationships/in").as(JsonArray)
		for obj in res do
			edges.add(new NeoEdge.from_json(neo, obj.as(JsonObject)))
		end
		internal_in_edges = edges
		return edges
	end

	# Get the list of `NeoEdge` pointing from `self`
	#
	# Edges are loaded lazily
	fun out_edges: List[NeoEdge] do return internal_out_edges or else load_out_edges

	private fun load_out_edges: List[NeoEdge] do
		var edges = new List[NeoEdge]
		var res = neo.get(url.to_s / "relationships/out")
		for obj in res.as(JsonArray) do
			edges.add(new NeoEdge.from_json(neo, obj.as(JsonObject)))
		end
		internal_out_edges = edges
		return edges
	end

	# Get nodes pointed by `self` following a `rel_type` edge
	fun out_nodes(rel_type: String): Array[NeoNode] do
		var res = new Array[NeoNode]
		for edge in out_edges do
			if edge.rel_type == rel_type then res.add edge.to
		end
		return res
	end

	# Get nodes pointing to `self` following a `rel_type` edge
	fun in_nodes(rel_type: String): Array[NeoNode] do
		var res = new Array[NeoNode]
		for edge in in_edges do
			if edge.rel_type == rel_type then res.add edge.from
		end
		return res
	end
end
lib/neo4j/neo4j.nit:554,1--670,3