1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
11 # Provides an interface for services on a Neo4j graphs.
12 module neo4j
::graph
::graph
17 # A Neo4j graph with a local identification scheme for its nodes.
19 # An identification scheme can be defined by subclassing `NeoNodeCollection`.
21 # `GraphStore` can be subclassed to add ways to save or load a graph. The
22 # storing mechanisms may use `nodes.id_of` to identify the nodes in the graph
23 # while encoding the relationships.
25 # All the nodes in the graph.
26 var nodes
: NeoNodeCollection
28 # All the relationships in the graph.
29 var edges
: SimpleCollection[NeoEdge] = new Array[NeoEdge]
31 # Add a new node to the graph and return it.
33 # Set the local ID of the node before returning it.
35 # SEE: `NeoNodeCollection.add`
36 # SEE: `NeoNodeCollection.create_node`
37 # SEE: `NeoNodeCollection.register`
38 fun create_node
: NeoNode do return nodes
.create_node
41 # All the nodes in a `NeoGraph`.
43 # An identification scheme can be defined throught the `register` and `add`
44 # methods. The `id_property` attribute defines where the local ID (that is the
45 # ID managed by the collection) is stored in each node.
46 abstract class NeoNodeCollection
47 super SimpleCollection[NeoNode]
49 # The type of the local IDs.
50 type ID_TYPE: Jsonable
52 # The property of the nodes that hold the local ID.
53 var id_property
: String
55 # Retrieve the node that has the specified local id.
57 # Note: The default implementation uses `get_or_null`.
58 fun [](id
: ID_TYPE): NeoNode do
59 var n
= get_or_null
(id
)
64 # Retrieve the node that has the specified local id, or return `null`.
66 # Note: The default implementation uses `iterator`.
67 fun get_or_null
(id
: ID_TYPE): nullable NeoNode do
69 if id_of
(n
) == id
then return n
74 # There is a node that has the specified local id?
76 # Note: The default implementation uses `get_or_null`.
77 fun has_id
(id
: ID_TYPE): Bool do return get_or_null
(id
) isa NeoNode
79 # Return the local ID of the node.
80 fun id_of
(node
: NeoNode): ID_TYPE do return node
[id_property
].as(ID_TYPE)
82 # Set the local ID of the specified node.
84 # Just update the property at `property_id`. Do not check anything.
85 protected fun id_of
=(node
: NeoNode, id
: ID_TYPE) do
86 node
[id_property
] = id
89 # Add the specified node to the graph and set its local ID.
93 fun register
(node
: NeoNode) is abstract
95 # Add the specified node to the graph assuming that its local ID is already set.
99 redef fun add
(node
: NeoNode) is abstract
101 # Add a new node to the graph and return it.
103 # Set the local ID of the node before returning it.
107 fun create_node
: NeoNode do
108 var node
= new NeoNode
114 # A mean to save and load a Neo4j graph.
115 abstract class GraphStore
118 # The graph to save or load.
121 # Can we save the graph without conflict?
122 fun isolated_save
: Bool is abstract
124 # Load the graph (or a part of it).
126 # Do not reset the graph.
130 fun save
do save_part
(graph
.nodes
, graph
.edges
)
132 # Save the specified part of the graph.
134 # Assume that for each relationship specified, both ends are already saved
135 # or are specified in the same call to this method.
136 fun save_part
(nodes
: Collection[NeoNode],
137 edges
: Collection[NeoEdge]) is abstract