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.
neo4j :: NeoGraph :: create_node
Add a new node to the graph and return it.neo4j :: NeoGraph :: defaultinit
neo4j :: NeoGraph :: from_json_object
Retrieve the graph from the specified JSON object.neo4j :: NeoGraph :: load_json_object
Retrieve a part of the graph from the specified JSON object.neo4j :: json_graph_store $ NeoGraph :: accept_json_serializer
Refinable service to customize the serialization of this class to JSONserialization :: Serializable :: accept_json_serializer
Refinable service to customize the serialization of this class to JSONserialization :: Serializable :: accept_msgpack_attribute_counter
Hook to customize the behavior of theAttributeCounter
serialization :: Serializable :: accept_msgpack_serializer
Hook to customize the serialization of this class to MessagePackserialization :: Serializable :: add_to_bundle
Called by[]=
to dynamically choose the appropriate method according
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
serialization :: Serializable :: core_serialize_to
Actual serialization ofself
to serializer
neo4j :: NeoGraph :: create_node
Add a new node to the graph and return it.core :: Object :: defaultinit
neo4j :: NeoGraph :: defaultinit
serialization :: Serializable :: from_deserializer
Create an instance of this class from thedeserializer
neo4j :: NeoGraph :: from_json_object
Retrieve the graph from the specified JSON object.core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
neo4j :: NeoGraph :: load_json_object
Retrieve a part of the graph from the specified JSON object.serialization :: Serializable :: msgpack_extra_array_items
Hook to request a larger than usual metadata arraycore :: Object :: output_class_name
Display class name on stdout (debug only).serialization :: Serializable :: serialize_msgpack
Serializeself
to MessagePack bytes
serialization :: Serializable :: serialize_to
Serializeself
to serializer
serialization :: Serializable :: serialize_to_json
Serializeself
to JSON
serialization :: Serializable :: to_pretty_json
Serializeself
to plain pretty JSON
Serializer::serialize
# 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
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