A Neo4j graph with a local identification scheme for its nodes.

An identification scheme can be defined by subclassing NeoNodeCollection.

GraphStore can be subclassed to add ways to save or load a graph. The storing mechanisms may use nodes.id_of to identify the nodes in the graph while encoding the relationships.

Introduced properties

fun create_node: NeoNode

neo4j :: NeoGraph :: create_node

Add a new node to the graph and return it.
fun edges: SimpleCollection[NeoEdge]

neo4j :: NeoGraph :: edges

All the relationships in the graph.
protected fun edges=(edges: SimpleCollection[NeoEdge])

neo4j :: NeoGraph :: edges=

All the relationships in the graph.
init from_json(nodes: NeoNodeCollection, t: Text)

neo4j :: NeoGraph :: from_json

Retrieve the graph from the specified JSON document.
init from_json_object(nodes: NeoNodeCollection, o: JsonObject)

neo4j :: NeoGraph :: from_json_object

Retrieve the graph from the specified JSON object.
fun load_json(t: Text)

neo4j :: NeoGraph :: load_json

Retrieve a part of the graph from the specified JSON document.
fun load_json_object(o: JsonObject)

neo4j :: NeoGraph :: load_json_object

Retrieve a part of the graph from the specified JSON object.
fun nodes: NeoNodeCollection

neo4j :: NeoGraph :: nodes

All the nodes in the graph.
protected fun nodes=(nodes: NeoNodeCollection)

neo4j :: NeoGraph :: nodes=

All the nodes in the graph.

Redefined properties

redef type SELF: NeoGraph

neo4j $ NeoGraph :: SELF

Type of this instance, automatically specialized in every class
redef fun accept_json_serializer(v: JsonSerializer)

neo4j :: json_graph_store $ NeoGraph :: accept_json_serializer

Refinable service to customize the serialization of this class to JSON

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
protected fun accept_json_serializer(v: JsonSerializer)

serialization :: Serializable :: accept_json_serializer

Refinable service to customize the serialization of this class to JSON
protected fun accept_msgpack_attribute_counter(v: AttributeCounter)

serialization :: Serializable :: accept_msgpack_attribute_counter

Hook to customize the behavior of the AttributeCounter
protected fun accept_msgpack_serializer(v: MsgPackSerializer)

serialization :: Serializable :: accept_msgpack_serializer

Hook to customize the serialization of this class to MessagePack
protected fun add_to_bundle(bundle: NativeBundle, key: JavaString)

serialization :: Serializable :: add_to_bundle

Called by []= to dynamically choose the appropriate method according
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun core_serialize_to(serializer: Serializer)

serialization :: Serializable :: core_serialize_to

Actual serialization of self to serializer
fun create_node: NeoNode

neo4j :: NeoGraph :: create_node

Add a new node to the graph and return it.
fun edges: SimpleCollection[NeoEdge]

neo4j :: NeoGraph :: edges

All the relationships in the graph.
protected fun edges=(edges: SimpleCollection[NeoEdge])

neo4j :: NeoGraph :: edges=

All the relationships in the graph.
init from_deserializer(deserializer: Deserializer)

serialization :: Serializable :: from_deserializer

Create an instance of this class from the deserializer
init from_json(nodes: NeoNodeCollection, t: Text)

neo4j :: NeoGraph :: from_json

Retrieve the graph from the specified JSON document.
init from_json_object(nodes: NeoNodeCollection, o: JsonObject)

neo4j :: NeoGraph :: from_json_object

Retrieve the graph from the specified JSON object.
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun load_json(t: Text)

neo4j :: NeoGraph :: load_json

Retrieve a part of the graph from the specified JSON document.
fun load_json_object(o: JsonObject)

neo4j :: NeoGraph :: load_json_object

Retrieve a part of the graph from the specified JSON object.
protected fun msgpack_extra_array_items: Int

serialization :: Serializable :: msgpack_extra_array_items

Hook to request a larger than usual metadata array
fun nodes: NeoNodeCollection

neo4j :: NeoGraph :: nodes

All the nodes in the graph.
protected fun nodes=(nodes: NeoNodeCollection)

neo4j :: NeoGraph :: nodes=

All the nodes in the graph.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun serialize_msgpack(plain: nullable Bool): Bytes

serialization :: Serializable :: serialize_msgpack

Serialize self to MessagePack bytes
fun serialize_to(serializer: Serializer)

serialization :: Serializable :: serialize_to

Serialize self to serializer
fun serialize_to_json(plain: nullable Bool, pretty: nullable Bool): String

serialization :: Serializable :: serialize_to_json

Serialize self to JSON
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
fun to_json: String

serialization :: Serializable :: to_json

Serialize self to plain JSON
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_pretty_json: String

serialization :: Serializable :: to_pretty_json

Serialize self to plain pretty JSON
fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram neo4j::NeoGraph NeoGraph serialization::Serializable Serializable neo4j::NeoGraph->serialization::Serializable core::Object Object serialization::Serializable->core::Object ...core::Object ... ...core::Object->core::Object

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

interface Serializable

serialization :: Serializable

Instances of this class can be passed to Serializer::serialize

Class definitions

neo4j $ NeoGraph
# A Neo4j graph with a local identification scheme for its nodes.
#
# An identification scheme can be defined by subclassing `NeoNodeCollection`.
#
# `GraphStore` can be subclassed to add ways to save or load a graph. The
# storing mechanisms may use `nodes.id_of` to identify the nodes in the graph
# while encoding the relationships.
class NeoGraph
	# All the nodes in the graph.
	var nodes: NeoNodeCollection

	# All the relationships in the graph.
	var edges: SimpleCollection[NeoEdge] = new Array[NeoEdge]

	# Add a new node to the graph and return it.
	#
	# Set the local ID of the node before returning it.
	#
	# SEE: `NeoNodeCollection.add`
	# SEE: `NeoNodeCollection.create_node`
	# SEE: `NeoNodeCollection.register`
	fun create_node: NeoNode do return nodes.create_node
end
lib/neo4j/graph/graph.nit:17,1--39,3

neo4j :: json_graph_store $ NeoGraph
redef class NeoGraph
	super Serializable

	# Retrieve the graph from the specified JSON document.
	#
	# For the expected format, see `JsonGraphStore`.
	#
	# ~~~nit
	# import neo4j::graph::sequential_id
	#
	# var graph = new NeoGraph(new SequentialNodeCollection("node_id"))
	# var a = new NeoNode
	# a.labels.add "Foo"
	# a["answer"] = 42
	# a["Ultimate question of"] = new JsonArray.from(["life",
	#		"the Universe", "and Everything."])
	# graph.nodes.register a
	# var b = graph.create_node
	# b.labels.add "Foo"
	# b.labels.add "Bar"
	# graph.edges.add new NeoEdge(a, "BAZ", b)
	#
	# graph = new NeoGraph.from_json(
	#		new SequentialNodeCollection("node_id"), graph.to_json)
	# assert 1 == graph.edges.length
	# for edge in graph.edges do
	#	assert "BAZ" == edge.rel_type
	#	assert a.labels == edge.from.labels
	#	for k, v in a.properties do assert v == edge.from.properties[k]
	#	assert b.labels == edge.to.labels
	#	for k, v in b.properties do assert v == edge.to.properties[k]
	# end
	# assert 2 == graph.nodes.length
	# ~~~
	init from_json(nodes: NeoNodeCollection, t: Text) do
		from_json_object(nodes, t.parse_json.as(JsonObject))
	end

	# Retrieve the graph from the specified JSON object.
	#
	# For the expected format, see `JsonGraphStore`.
	init from_json_object(nodes: NeoNodeCollection, o: JsonObject) do
		init(nodes)
		load_json_object(o)
	end

	# Retrieve a part of the graph from the specified JSON document.
	#
	# For the expected format, see `JsonGraphStore`.
	fun load_json(t: Text) do
		load_json_object(t.parse_json.as(JsonObject))
	end

	# Retrieve a part of the graph from the specified JSON object.
	#
	# For the expected format, see `JsonGraphStore`.
	fun load_json_object(o: JsonObject) do
		var json_nodes = o["nodes"].as(JsonArray)
		var nodes = self.nodes
		nodes.enlarge(nodes.length)
		for json_node in json_nodes do
			assert json_node isa JsonObject
			var node = new NeoNode.from_json_object(json_node)
			nodes.add node
		end

		var json_edges = o["edges"].as(JsonArray)
		var edges = self.edges
		if edges isa AbstractArray[NeoEdge] then edges.enlarge(edges.length)
		for json_edge in json_edges do
			assert json_edge isa JsonObject
			var from = nodes[nodes.id_from_jsonable(json_edge["from"])]
			var to = nodes[nodes.id_from_jsonable(json_edge["to"])]
			var rel_type = json_edge["type"].as(String)
			var json_properties = json_edge["properties"].as(JsonObject)
			var edge = new NeoEdge(from, rel_type, to)
			edge.properties.add_all(json_properties)
			edges.add edge
		end
	end

	redef fun accept_json_serializer(v) do
		v.stream.write "\{\"nodes\":["
		append_entities_json(nodes, v)
		v.stream.write "],\"edges\":["
		append_entities_json(edges, v)
		v.stream.write "]\}"
	end

	# Encode `self` in JSON.
	#
	# For a description of the format, see `JsonGraphStore`.
	private fun append_entities_json(entities: Collection[NeoEntity], v: JsonSerializer) do
		var i = entities.iterator
		if i.is_ok then
			i.item.append_json_for(self, v)
			i.next
			for entity in i do
				v.stream.write ","
				entity.append_json_for(self, v)
			end
		end
	end
end
lib/neo4j/graph/json_graph_store.nit:113,1--216,3