Provides an interface for services on a Neo4j graphs.

Introduced classes

abstract class GraphStore

neo4j :: GraphStore

A mean to save and load a Neo4j graph.
class Neo4jGraphStore

neo4j :: Neo4jGraphStore

Save or load a graph using an actual Neo4j database.
class NeoGraph

neo4j :: NeoGraph

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

neo4j :: NeoNodeCollection

All the nodes in a NeoGraph.

All class definitions

abstract class GraphStore

neo4j $ GraphStore

A mean to save and load a Neo4j graph.
class Neo4jGraphStore

neo4j $ Neo4jGraphStore

Save or load a graph using an actual Neo4j database.
class NeoGraph

neo4j $ NeoGraph

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

neo4j $ NeoNodeCollection

All the nodes in a NeoGraph.
package_diagram neo4j::graph graph neo4j neo4j neo4j::graph->neo4j progression progression neo4j::graph->progression neo4j->progression json json neo4j->json curl curl neo4j->curl pipeline pipeline neo4j->pipeline ...json ... ...json->json ...curl ... ...curl->curl ...progression ... ...progression->progression ...pipeline ... ...pipeline->pipeline neo4j::json_graph_store json_graph_store neo4j::json_graph_store->neo4j::graph neo4j::sequential_id sequential_id neo4j::sequential_id->neo4j::graph a_star-m a_star-m a_star-m->neo4j::json_graph_store a_star-m->neo4j::sequential_id a_star-m... ... a_star-m...->a_star-m

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module caching

serialization :: caching

Services for caching serialization engines
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module curl

curl :: curl

Data transfer powered by the native curl library
module curl_json

neo4j :: curl_json

cURL requests compatible with the JSON REST APIs.
module engine_tools

serialization :: engine_tools

Advanced services for serialization engines
module environ

core :: environ

Access to the environment variables of the process
module error

neo4j :: error

Errors thrown by the neo4j library.
module error

json :: error

Intro JsonParseError which is exposed by all JSON reading APIs
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module inspect

serialization :: inspect

Refine Serializable::inspect to show more useful information
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module json

json :: json

Read and write JSON formatted text using the standard serialization services
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module meta

meta :: meta

Simple user-defined meta-level to manipulate types of instances as object.
module native

core :: native

Native structures for text and bytes
module native_curl

curl :: native_curl

Binding of C libCurl which allow us to interact with network.
module numeric

core :: numeric

Advanced services for Numeric types
module parser_base

parser_base :: parser_base

Simple base for hand-made parsers of all kinds
module poset

poset :: poset

Pre order sets and partial order set (ie hierarchies)
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module safe

serialization :: safe

Services for safer deserialization engines
module serialization

serialization :: serialization

General serialization services
module serialization_core

serialization :: serialization_core

Abstract services to serialize Nit objects to different formats
module serialization_read

json :: serialization_read

Services to read JSON: deserialize_json and JsonDeserializer
module serialization_write

json :: serialization_write

Services to write Nit objects to JSON strings: serialize_to_json and JsonSerializer
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module static

json :: static

Static interface to read Nit objects from JSON strings
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module neo4j

neo4j :: neo4j

Neo4j connector through its JSON REST API using curl.
module progression

progression :: progression

Event-based interface to track the progression of an operation.

Children

module json_graph_store

neo4j :: json_graph_store

Provides JSON as a mean to store graphs.
module sequential_id

neo4j :: sequential_id

Provides a sequential identification scheme for Neo4j nodes.

Descendants

module a_star-m

a_star-m

# Provides an interface for services on a Neo4j graphs.
module neo4j::graph::graph

import neo4j
import progression

# 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

# All the nodes in a `NeoGraph`.
#
# An identification scheme can be defined throught the `register` and `add`
# methods. The `id_property` attribute defines where the local ID (that is the
# ID managed by the collection) is stored in each node.
abstract class NeoNodeCollection
	super SimpleCollection[NeoNode]

	# The type of the local IDs.
	type ID_TYPE: Serializable

	# The property of the nodes that hold the local ID.
	var id_property: String

	# Retrieve the node that has the specified local id.
	#
	# Note: The default implementation uses `get_or_null`.
	fun [](id: ID_TYPE): NeoNode do
		var n = get_or_null(id)
		assert n isa NeoNode
		return n
	end

	# Retrieve the node that has the specified local id, or return `null`.
	#
	# Note: The default implementation uses `iterator`.
	fun get_or_null(id: ID_TYPE): nullable NeoNode do
		for n in self do
			if id_of(n) == id then return n
		end
		return null
	end

	# There is a node that has the specified local id?
	#
	# Note: The default implementation uses `get_or_null`.
	fun has_id(id: ID_TYPE): Bool do return get_or_null(id) isa NeoNode

	# Return the local ID of the node.
	fun id_of(node: NeoNode): ID_TYPE do return node[id_property].as(ID_TYPE)

	# Set the local ID of the specified node.
	#
	# Just update the property at `property_id`. Do not check anything.
	protected fun id_of=(node: NeoNode, id: ID_TYPE) do
		node[id_property] = id
	end

	# Enlarge the collection to have at least the specified capacity.
	#
	# The capacity is specified in number of nodes. Used to minimize the
	# number of times the collection need to be resized when adding nodes
	# in batches.
	#
	# Do nothing by default.
	fun enlarge(cap: Int) do end

	# Add the specified node to the graph and set its local ID.
	#
	# SEE: `add`
	# SEE: `create_node`
	fun register(node: NeoNode) is abstract

	# Add the specified node to the graph assuming that its local ID is already set.
	#
	# SEE: `create_node`
	# SEE: `register`
	redef fun add(node) is abstract

	# Add a new node to the graph and return it.
	#
	# Set the local ID of the node before returning it.
	#
	# SEE: `add`
	# SEE: `register`
	fun create_node: NeoNode do
		var node = new NeoNode
		register(node)
		return node
	end

	# Remove the node with the specified local ID.
	fun remove_at(id: ID_TYPE) is abstract

	# Remove the specified node.
	#
	# The local ID is used instead of `==` to seek the node.
	fun remove_node(node: NeoNode) do
		remove_at(id_of(node))
	end

	redef fun clear do
		for node in self do remove_node(node)
	end

	redef fun remove(node) do
		for n in self do
			if node == n then
				remove_node(n)
				return
			end
		end
	end

	redef fun remove_all(node) do
		for n in self do
			if node == n then remove_node(n)
		end
	end

	# Optimize the collection, possibly by rewritting it.
	#
	# The local ID of the elements may be changed by this method.
	fun compact do end
end

# A mean to save and load a Neo4j graph.
abstract class GraphStore
	super Trackable

	# The graph to save or load.
	var graph: NeoGraph

	# Can we save the graph without conflict?
	fun isolated_save: Bool is abstract

	# Load the graph (or a part of it).
	#
	# Do not reset the graph.
	fun load is abstract

	# Save the graph.
	fun save do save_part(graph.nodes, graph.edges)

	# Save the specified part of the graph.
	#
	# Assume that for each relationship specified, both ends are already saved
	# or are specified in the same call to this method.
	fun save_part(nodes: Collection[NeoNode],
			edges: Collection[NeoEdge]) is abstract
end

# Save or load a graph using an actual Neo4j database.
class Neo4jGraphStore
	super GraphStore

	# The maximum number of entities saved in one request.
	#
	# Also defines the granulity of the reported progression.
	#
	# TODO Also honor this limit in `load`.
	var batch_max_size = 512 is writable

	# The Neo4j client to use.
	var client: Neo4jClient

	# The label to use to retrieve the nodes.
	var node_label: String

	private var done_part = 0
	private var total = 0

	# Is the database already contains at least one node with the specified label?
	fun has_node_label(name: String): Bool do
		var query = new CypherQuery.from_string(
				"match n where \{name\} in labels(n) return count(n)")
		query.params["name"] = name
		var data = client.cypher(query).as(JsonObject)["data"]
		var result = data.as(JsonArray).first.as(JsonArray).first.as(Int)
		return result > 0
	end

	redef fun isolated_save do return not has_node_label(node_label)

	redef fun load do
		assert batch_max_size > 0
		fire_started
		var db_nodes = client.nodes_with_label(node_label)
		var nodes = graph.nodes
		var edges = graph.edges
		var i = 0

		total = nodes.length * 2
		done_part = nodes.length
		fire_progressed(done_part, total)
		for node in db_nodes do
			nodes.add(node)
			edges.add_all(node.out_edges)
			i += 1
			if i >= batch_max_size then
				done_part += batch_max_size
				fire_progressed(done_part, total)
			end
		end
		fire_done
	end

	redef fun save_part(nodes, edges) do
		assert batch_max_size > 0
		fire_started
		total = nodes.length + edges.length
		done_part = 0

		save_entities(nodes)
		save_entities(edges)
		fire_done
	end

	# Save the specified entities.
	private fun save_entities(neo_entities: Collection[NeoEntity]) do
		var batch = new NeoBatch(client)
		var batch_length = 0

		for nentity in neo_entities do
			batch.save_entity(nentity)
			batch_length += 1
			if batch_length >= batch_max_size then
				do_batch(batch)
				done_part += batch_max_size
				fire_progressed(done_part, total)
				batch = new NeoBatch(client)
				batch_length = 0
			end
		end
		do_batch(batch)
		done_part += batch_length
	end

	# Execute `batch` and check for errors.
	#
	# Abort if `batch.execute` returns errors.
	private fun do_batch(batch: NeoBatch) do
		var errors = batch.execute
		assert errors.is_empty else
			for e in errors do sys.stderr.write("{e}\n")
		end
	end
end
lib/neo4j/graph/graph.nit:11,1--278,3