ReGraph tutorial (Neo4j backend)

Part 2: Rewriting hierarchies of graph

ReGraph allows to create a hierarchies of graphs related by means of homomorphisms (or typing). In the context of a hierarchy, if there exists a homomorphism $G \rightarrow T$, we say that graph $G$ is typed by a graph $T$. Graph hierarchy is a DAG, where nodes are graphs and edges are homomorphisms. A homomorphism maps every node of $G$ to some node in $T$ (a type) in such a way that:

  • edges are preserved
  • attributes of both nodes and edges are preserved

ReGraph provides the data structure Neo4jHierarchy that encodes a graph hierarchy as a property graph stored in a Neo4j database. This encoding represents each graph in a hierarchy with nodes of the database labeled by a unique label corresponding to the ID of the corresponding graph in the hierarchy. For example, the following Cypher query mathces all the nodes belonging to the graph graphID in the hierarchy:

MATCH (n:graphID) RETURN n

The edges in graphs are labeled as edge and the typing edges are labeled as typing. We can then easily find the typing of nodes in graph1 by graph2 with the query:

MATCH (n:graph1)-[:typing]->(m:graph2) RETURN n, m

The hierarchy skeleton is represented by nodes labeled as graph be default. So, matching the nodes of the hierarchy skeleton can be done with the following query:

MATCH (n:graph) return n


In [1]:
from regraph import NXGraph, Neo4jHierarchy, Rule
from regraph import plot_graph, plot_instance, plot_rule

In [2]:
%matplotlib inline

1. Creating and modifying a hierarchy object

Consider the following example of a simple graph hierarchy. The two graphs $G$ and $T$ are being created and added to the heirarchy. Afterwards a typing homomorphism between $G$ and $T$ is added, so that every node of $G$ is typed by some node in $T$.


In [3]:
# Define graph G
g = NXGraph()
g.add_nodes_from(["protein", "binding", "region", "compound"])
g.add_edges_from([("region", "protein"), ("protein", "binding"), ("region", "binding"), ("compound", "binding")])

# Define graph T
t = NXGraph()
t.add_nodes_from(["action", "agent"])
t.add_edges_from([("agent", "agent"), ("agent", "action")])

In [4]:
# Create a hierarchy
simple_hierarchy = Neo4jHierarchy(uri="bolt://localhost:7687", user="neo4j", password="admin")

# If you run this notebooks multiple times, you need to clear the graph in the db
simple_hierarchy._clear()

simple_hierarchy.add_graph("G", g, {"name": "Simple protein interaction"})
simple_hierarchy.add_graph("T", t, {"name": "Agent interaction"})
simple_hierarchy.add_typing(
    "G", "T",
    {"protein": "agent",
     "region": "agent",
     "compound": "agent",
     "binding": "action",
    }
)
print(simple_hierarchy)


Graphs:

G {'name': {'Simple protein interaction'}}

T {'name': {'Agent interaction'}}

Typing homomorphisms: 
G -> T: {}

Relations:

The method get_graph returns the graph object corresponding to the provided graph id.


In [5]:
type(simple_hierarchy.get_graph("T"))


Out[5]:
regraph.backends.neo4j.graphs.Neo4jGraph

The method get_typing returns the dictionary object corresponding to the provided hierarchy edge and representing the associated graph homomorphism.


In [6]:
simple_hierarchy.get_typing("G", "T")


Out[6]:
{'binding': 'action',
 'protein': 'agent',
 'region': 'agent',
 'compound': 'agent'}

2. Rewriting of objects in a hierarchy

ReGraph implements the rewriting technique called sesqui-pushout rewriting that allows to transform graphs by applying rules through their instances (matchings). Rewriting an individual graphs in a hierarchy may require an update of other graphs and typings in this hierarchy, such updates are called propagation and are distinguished into two types: backward and forward propagation.

Backward propagation briefly:

  • If some graph elements (nodes/edges or attributes) are removed from a graph in the hierarchy, then all the respective elements that are typed by them in the ancestor graphs should be removed.
  • If a graph node is cloned, then for every instance of this node (every node that is typed by the clonned node) in the ancestor graphs we either: (a) specify to which clone it corresponds or (b) clone it.

Forward propagation briefly:

  • If some graph nodes are merged and these nodes are typed by different nodes in a descendant graph, the corresponding nodes in the descendant graph should be merged.
  • If a new graph element (node/edge or attribute) is added, then for all the descendent graphs in the hierarchy we either (a) select an existing element to type the added element or (b) add a new element to type the added element.

For more details, please see here.

ReGraph allows to rewrite individual graphs situated in the hierarchy using the method rewrite of Neo4jHierarchy. The rewriting can be done in two modes:

  1. Strict rewriting rewriting that does not allow propagation.

  2. Not strict rewriting that allows propagation.

The rewrite takes as the input the following parameters:

  • graph_id, ID of the graph in the hierarchy to rewrite,
  • rule, a rule object to apply,
  • instance, a dictionary containing an instance of the lhs of the rule in the graph subject to rewriting, by default, tries to construct identity morphism of the nodes of the pattern,
  • p_typing, a dictionary containing typing of graphs in the hierarchy by the interface of the rule, keys are IDs of hierarchy graphs, values are dictionaries containing the mapping of nodes from the hierarchy graphs to the inteface nodes (note that a node from a graph can be typed by a set of nodes in the interface of the rule, e.g. if we want to perform cloning of some types, etc).
  • rhs_typing, a dictionary containing typing of the rhs by graphs of the hierarchy, keys are ids of hierarchy graphs, values are dictionaries containing the mapping of nodes from the lhs to the nodes of the typing graph given by the respective key of the value (note that a node from the rhs can be typed by a set of nodes of some graph, e.g. if we want to perform merging of some types, etc),
  • strict, flag indicating if rewriting is strict, then any propagation is not allowed.

2.1. Strict rewriting

Let us create a Rule object containing a rule we would like to apply.


In [7]:
lhs = NXGraph()
lhs.add_nodes_from(["a", "b"])
lhs.add_edges_from([("a", "b")])

p = NXGraph()
p.add_nodes_from(["a", "b"])
p.add_edges_from([])

rhs = NXGraph()
rhs.add_nodes_from(["a", "b", "c"])
rhs.add_edges_from([("c", "a"), ("c", "b")])

# By default if `p_lhs` and `p_rhs` are not provided
# to a rule, it tries to construct this homomorphisms
# automatically by matching the names. In this case we
# have defined lhs, p and rhs in such a way that that
# the names of the matching nodes correspond
rule = Rule(p, lhs, rhs)
plot_rule(rule)


/home/raimon/anaconda3/lib/python3.7/site-packages/networkx/drawing/nx_pylab.py:579: MatplotlibDeprecationWarning: 
The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead.
  if not cb.iterable(width):
/home/raimon/anaconda3/lib/python3.7/site-packages/networkx/drawing/nx_pylab.py:676: MatplotlibDeprecationWarning: 
The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead.
  if cb.iterable(node_size):  # many node sizes

The created rule removes the edge 1->2, adds the new node 3 and two edges 3->1 and 3->2.

Let us find instances of the created rule in the graph G.


In [8]:
instances = simple_hierarchy.find_matching("G", rule.lhs)
print("Instances: ", instances)


Instances:  [{'a': 'protein', 'b': 'binding'}, {'a': 'region', 'b': 'binding'}, {'a': 'region', 'b': 'protein'}, {'a': 'compound', 'b': 'binding'}]

Let us fixed the desired instance: we would like to remove the edge from protein to binding and add some new node connecting them.


In [9]:
instance = {
    "a": "protein",
    "b": "binding"
}

We try to apply the rule to the selected instance as is in the strict rewriting mode.


In [10]:
try:
    rhs_instance = simple_hierarchy.rewrite("G", rule, instance, strict=True)
except Exception as e:
    print("Error message: ", e)
    print("Type: ", type(e))


Error message:  Rewriting is strict (no propagation of types is allowed), typing of the added nodes '{'c'}' by 'T' is required
Type:  <class 'regraph.exceptions.RewritingError'>

We have failed to rewrite G, because we have not specified typing for the newly added node 3. Let us try again, but this time we will prove such typing.


In [11]:
rhs_typing = {
    "T": {"c": "agent"}
}

In [12]:
rhs_instance = simple_hierarchy.rewrite(
    "G", rule, instance, rhs_typing=rhs_typing, strict=True)

In [13]:
print("Instance of the RHS in G", rhs_instance)


Instance of the RHS in G {'a': 'protein', 'b': 'binding', 'c': 'c'}

We will now create a rule that applied to T and that clones the node agent into two nodes.


In [14]:
lhs = NXGraph()
lhs.add_nodes_from(["agent"])

rule = Rule.from_transform(lhs)
_, rhs_clone = rule.inject_clone_node("agent")
plot_rule(rule)



In [15]:
instance = {
    "agent": "agent"
}

We try to apply the created rule to the graph T in the strict mode.


In [16]:
try:
    rhs_instance = simple_hierarchy.rewrite("T", rule, instance, strict=True)
except Exception as e:
    print("Error message: ", e)
    print("Type: ", type(e))


Error message:  Rewriting is strict (no propagation of clones is allowed), the cloned node 'agent' in 'T' has instances '['protein', 'region', 'compound', 'c']' in 'G' and their typing by P is not specified
Type:  <class 'regraph.exceptions.RewritingError'>

We have failed to rewrite T, because we have not specified typing for instances of agent in $p$. Let us try again, but this time we will prove such typing.


In [17]:
p_typing = {
    "G": {
        'protein': 'agent',
        'region': 'agent',
        'compound': rhs_clone, 
        'c': 'agent'
    }
}

In [18]:
rhs_instance = simple_hierarchy.rewrite("T", rule, instance, p_typing=p_typing, strict=True)

In [19]:
print("Instance of the RHS in G", rhs_instance)


Instance of the RHS in G {'agent': 'agent1', 'agent1': 'agent'}

Let us relabel nodes in T.


In [20]:
simple_hierarchy.relabel_graph_node('T', rhs_instance['agent'], 'organic_agent')
simple_hierarchy.relabel_graph_node('T', rhs_instance[rhs_clone], 'non_organic_agent')

In [21]:
print(simple_hierarchy.get_typing("G", "T"))


{'binding': 'action', 'compound': 'non_organic_agent', 'protein': 'organic_agent', 'region': 'organic_agent', 'c': 'organic_agent'}

2.2. Rewriting and propagation

To illustrate rewriting with propagation, let us consider a slighlty more sophisticated hierarchy.


In [22]:
hierarchy = Neo4jHierarchy(uri="bolt://localhost:7687", user="neo4j", password="admin")
hierarchy._clear()

colors = NXGraph()
colors.add_nodes_from([
        "green", "red"
    ])
colors.add_edges_from([
        ("red", "green"),
        ("red", "red"),
        ("green", "green")
    ])
hierarchy.add_graph("colors", colors)

shapes = NXGraph()
shapes.add_nodes_from(["circle", "square"])
shapes.add_edges_from([
        ("circle", "square"),
        ("square", "circle"),
        ("circle", "circle")
    ])
hierarchy.add_graph("shapes", shapes)

quality = NXGraph()
quality.add_nodes_from(["good", "bad"])
quality.add_edges_from([
        ("bad", "bad"),
        ("bad", "good"),
        ("good", "good")
    ])
hierarchy.add_graph("quality", quality)

g1 = NXGraph()
g1.add_nodes_from([
    "red_circle",
    "red_square",
])
g1.add_edges_from([
    ("red_circle", "red_square"),
    ("red_circle", "red_circle"),
    ("red_square", "red_circle")
])
g1_colors = {
    "red_circle": "red",
    "red_square": "red",
}
g1_shapes = {
    "red_circle": "circle",
    "red_square": "square",
}

hierarchy.add_graph("g1", g1)
hierarchy.add_typing("g1", "colors", g1_colors)
hierarchy.add_typing("g1", "shapes", g1_shapes)

g2 = NXGraph()
g2.add_nodes_from([
    "good_circle",
    "good_square",
    "bad_circle",
])
g2.add_edges_from([
    ("good_circle", "good_square"),
    ("good_square", "good_circle"),
    ("bad_circle", "good_circle"),
    ("bad_circle", "bad_circle"),
])
g2_shapes = {
    "good_circle": "circle",
    "good_square": "square",
    "bad_circle": "circle"
}
g2_quality = {
    "good_circle": "good",
    "good_square": "good",
    "bad_circle": "bad",
}

hierarchy.add_graph("g2", g2)
hierarchy.add_typing("g2", "shapes", g2_shapes)
hierarchy.add_typing("g2", "quality", g2_quality)

g3 = NXGraph()
g3.add_nodes_from([
    "good_red_circle",
    "bad_red_circle",
    "good_red_square",
])
g3.add_edges_from([
    ("bad_red_circle", "good_red_circle"),
    ("good_red_square", "good_red_circle"),
    ("good_red_circle", "good_red_square")
])

g3_g1 = {
    "good_red_circle": "red_circle",
    "bad_red_circle": "red_circle",
    "good_red_square": "red_square"
}

g3_g2 = {
    "good_red_circle": "good_circle",
    "bad_red_circle": "bad_circle",
    "good_red_square": "good_square",
}

hierarchy.add_graph("g3", g3)
hierarchy.add_typing("g3", "g1", g3_g1)
hierarchy.add_typing("g3", "g2", g3_g2)

In [23]:
print(hierarchy)


Graphs:

quality {}

g1 {}

g2 {}

g3 {}

colors {}

shapes {}

Typing homomorphisms: 
g1 -> shapes: {}
g1 -> colors: {}
g2 -> quality: {}
g2 -> shapes: {}
g3 -> g2: {}
g3 -> g1: {}

Relations:


In [24]:
for graph in hierarchy.graphs():
    print("Graph '{}', nodes: {}".format(graph, hierarchy.get_graph(graph).nodes()))


Graph 'quality', nodes: ['good', 'bad']
Graph 'g1', nodes: ['red_circle', 'red_square']
Graph 'g2', nodes: ['good_circle', 'good_square', 'bad_circle']
Graph 'g3', nodes: ['good_red_circle', 'bad_red_circle', 'good_red_square']
Graph 'colors', nodes: ['green', 'red']
Graph 'shapes', nodes: ['circle', 'square']

Some of the graphs in the hierarchy are now typed by multiple graphs, which is reflected in the types of nodes, as in the example below:


In [25]:
print("Node types in G3:\n")
for node in hierarchy.get_graph("g3").nodes():
    print(node, hierarchy.node_type("g3", node))


Node types in G3:

good_red_circle {'g2': 'good_circle', 'g1': 'red_circle'}
bad_red_circle {'g2': 'bad_circle', 'g1': 'red_circle'}
good_red_square {'g2': 'good_square', 'g1': 'red_square'}

2.3. Rewriting and propagation

We now show how graph rewriting can be performed in such an hierarchy. In the previous example we perfromed strict rewriting in a hierarchy, where no propagation was performed.

The following example illustrates how the ReGraph propagates the changes made by rewriting on any level to all the graphs (as well as the rules) typed by the one target of rewriting.


In [26]:
lhs = NXGraph()
lhs.add_nodes_from(["a", "b"])
lhs.add_edges_from([
        ("a", "b"),
        ("b", "a")
    ])
p = NXGraph()
p.add_nodes_from(["a", "a1", "b"])
p.add_edges_from([
        ("a", "b"),
        ("a1", "b")
    ])
rhs = NXGraph.copy(p)

rule = Rule(
    p, lhs, rhs,
    {"a": "a", "a1": "a", "b": "b"},
    {"a": "a", "a1": "a1", "b": "b"},
)
plot_rule(rule)


We have created a rule that clones the node a and reconnects the edges between a and b.


In [27]:
instances = hierarchy.find_matching("shapes", lhs)
print("Instances:")
for instance in instances:
    print(instance)


Instances:
{'a': 'circle', 'b': 'square'}
{'a': 'square', 'b': 'circle'}

We rewrite the graph shapes with the fixed instances (so, the node circle is cloned).


In [28]:
rhs_instances = hierarchy.rewrite("shapes", rule, {"a": "circle", "b": "square"})

Observe the following plots, the cloning of circle was propagated to all the ancestors of shapes, because we didn't specify how to retype intances of circle for these ancestors using the p_typing parameter. This is an example of previously mentioned backward propagation.


In [29]:
for graph in hierarchy.graphs():
    print("Graph '{}', nodes: {}".format(graph, hierarchy.get_graph(graph).nodes()))


Graph 'quality', nodes: ['good', 'bad']
Graph 'g1', nodes: ['red_circle1', 'red_circle', 'red_square']
Graph 'g2', nodes: ['bad_circle1', 'good_circle1', 'good_circle', 'good_square', 'bad_circle']
Graph 'g3', nodes: ['good_red_circle11', 'good_red_circle1', 'bad_red_circle1', 'bad_red_circle11', 'good_red_circle', 'bad_red_circle', 'good_red_square', 'good_red_circle2', 'bad_red_circle2']
Graph 'colors', nodes: ['green', 'red']
Graph 'shapes', nodes: ['circle', 'square', 'circle1']

Let us now consider a small example of forward propagation. We will create a rule that performs some additions and merges of nodes.


In [30]:
pattern = NXGraph()
pattern.add_nodes_from(["a", "b"])

rule = Rule.from_transform(pattern)
rhs_node = rule.inject_merge_nodes(["a", "b"])
rule.inject_add_node("c")
rule.inject_add_edge("c", rhs_node)

In [31]:
instance = {
    "a": "good_circle",
    "b": "bad_circle",
}

Application of this rule will merge nodes bad_circle and good_circle in the graph g2. It with then add a new node and connect it with an edge to the merged node. Let us specify some typings of the new node in the RHS: we will set the new node to be typed as circle in the graph shapes.


In [32]:
rhs_typing = {
    "shapes": {
        "c": "circle"
    }
}

In [33]:
rhs_instance = hierarchy.rewrite("g2", rule, instance, rhs_typing=rhs_typing)
print(rhs_instance)


{'a_b': 'bad_circle_good_circle', 'c': 'c'}

Observe the following graphs, as the resul of forward propagation nodes good and bad were merged in the graph qualities. In addition, a new node typing the node c in the rule was added to the graph qualities.


In [34]:
for graph in hierarchy.graphs():
    print("Graph '{}', nodes: {}".format(graph, hierarchy.get_graph(graph).nodes()))


Graph 'quality', nodes: ['bad_good', 'c']
Graph 'g1', nodes: ['red_circle1', 'red_circle', 'red_square']
Graph 'g2', nodes: ['bad_circle1', 'good_circle1', 'good_square', 'bad_circle_good_circle', 'c']
Graph 'g3', nodes: ['good_red_circle11', 'good_red_circle1', 'bad_red_circle1', 'bad_red_circle11', 'good_red_circle', 'bad_red_circle', 'good_red_square', 'good_red_circle2', 'bad_red_circle2']
Graph 'colors', nodes: ['green', 'red']
Graph 'shapes', nodes: ['circle', 'square', 'circle1']

3. Dumping hierarchies with JSON

ReGraph provides some utils for serialization of Neo4jHierarchy objects and implements the following methods for loading and exporting your hierarchy in JSON-format:

  • Neo4jHierarchy.to_json creates a json representations of the hierarchy;

  • Neo4jHierarchy.from_json loads an hierarchy from json representation (returns new Hierarchy object);

  • Neo4jHierarchy.export exports the hierarchy to a file (json format);
  • Neo4jHierarchy.load loads an hierarchy from a .json file (returns new object as well).

In [35]:
hierarchy_json = hierarchy.to_json()

In [36]:
import json
print(json.dumps(hierarchy_json, indent="  "))


{
  "graphs": [
    {
      "id": "quality",
      "graph": {
        "edges": [
          {
            "from": "bad_good",
            "to": "bad_good",
            "attrs": {}
          },
          {
            "from": "c",
            "to": "bad_good",
            "attrs": {}
          }
        ],
        "nodes": [
          {
            "id": "bad_good",
            "attrs": {}
          },
          {
            "id": "c",
            "attrs": {}
          }
        ]
      },
      "attrs": {}
    },
    {
      "id": "g1",
      "graph": {
        "edges": [
          {
            "from": "red_circle1",
            "to": "red_circle1",
            "attrs": {}
          },
          {
            "from": "red_circle1",
            "to": "red_square",
            "attrs": {}
          },
          {
            "from": "red_circle1",
            "to": "red_circle",
            "attrs": {}
          },
          {
            "from": "red_circle",
            "to": "red_circle1",
            "attrs": {}
          },
          {
            "from": "red_circle",
            "to": "red_circle",
            "attrs": {}
          },
          {
            "from": "red_circle",
            "to": "red_square",
            "attrs": {}
          }
        ],
        "nodes": [
          {
            "id": "red_circle1",
            "attrs": {}
          },
          {
            "id": "red_circle",
            "attrs": {}
          },
          {
            "id": "red_square",
            "attrs": {}
          }
        ]
      },
      "attrs": {}
    },
    {
      "id": "g2",
      "graph": {
        "edges": [
          {
            "from": "bad_circle1",
            "to": "bad_circle_good_circle",
            "attrs": {}
          },
          {
            "from": "bad_circle1",
            "to": "good_circle1",
            "attrs": {}
          },
          {
            "from": "bad_circle1",
            "to": "bad_circle1",
            "attrs": {}
          },
          {
            "from": "good_circle1",
            "to": "good_square",
            "attrs": {}
          },
          {
            "from": "bad_circle_good_circle",
            "to": "good_square",
            "attrs": {}
          },
          {
            "from": "bad_circle_good_circle",
            "to": "bad_circle1",
            "attrs": {}
          },
          {
            "from": "bad_circle_good_circle",
            "to": "good_circle1",
            "attrs": {}
          },
          {
            "from": "bad_circle_good_circle",
            "to": "bad_circle_good_circle",
            "attrs": {}
          },
          {
            "from": "c",
            "to": "bad_circle_good_circle",
            "attrs": {}
          }
        ],
        "nodes": [
          {
            "id": "bad_circle1",
            "attrs": {}
          },
          {
            "id": "good_circle1",
            "attrs": {}
          },
          {
            "id": "good_square",
            "attrs": {}
          },
          {
            "id": "bad_circle_good_circle",
            "attrs": {}
          },
          {
            "id": "c",
            "attrs": {}
          }
        ]
      },
      "attrs": {}
    },
    {
      "id": "g3",
      "graph": {
        "edges": [
          {
            "from": "good_red_circle11",
            "to": "good_red_square",
            "attrs": {}
          },
          {
            "from": "good_red_circle1",
            "to": "good_red_square",
            "attrs": {}
          },
          {
            "from": "bad_red_circle1",
            "to": "good_red_circle2",
            "attrs": {}
          },
          {
            "from": "bad_red_circle1",
            "to": "good_red_circle11",
            "attrs": {}
          },
          {
            "from": "bad_red_circle1",
            "to": "good_red_circle1",
            "attrs": {}
          },
          {
            "from": "bad_red_circle1",
            "to": "good_red_circle",
            "attrs": {}
          },
          {
            "from": "bad_red_circle11",
            "to": "good_red_circle2",
            "attrs": {}
          },
          {
            "from": "bad_red_circle11",
            "to": "good_red_circle",
            "attrs": {}
          },
          {
            "from": "bad_red_circle11",
            "to": "good_red_circle1",
            "attrs": {}
          },
          {
            "from": "bad_red_circle11",
            "to": "good_red_circle11",
            "attrs": {}
          },
          {
            "from": "good_red_circle",
            "to": "good_red_square",
            "attrs": {}
          },
          {
            "from": "bad_red_circle",
            "to": "good_red_circle2",
            "attrs": {}
          },
          {
            "from": "bad_red_circle",
            "to": "good_red_circle11",
            "attrs": {}
          },
          {
            "from": "bad_red_circle",
            "to": "good_red_circle1",
            "attrs": {}
          },
          {
            "from": "bad_red_circle",
            "to": "good_red_circle",
            "attrs": {}
          },
          {
            "from": "good_red_circle2",
            "to": "good_red_square",
            "attrs": {}
          },
          {
            "from": "bad_red_circle2",
            "to": "good_red_circle2",
            "attrs": {}
          },
          {
            "from": "bad_red_circle2",
            "to": "good_red_circle",
            "attrs": {}
          },
          {
            "from": "bad_red_circle2",
            "to": "good_red_circle11",
            "attrs": {}
          },
          {
            "from": "bad_red_circle2",
            "to": "good_red_circle1",
            "attrs": {}
          }
        ],
        "nodes": [
          {
            "id": "good_red_circle11",
            "attrs": {}
          },
          {
            "id": "good_red_circle1",
            "attrs": {}
          },
          {
            "id": "bad_red_circle1",
            "attrs": {}
          },
          {
            "id": "bad_red_circle11",
            "attrs": {}
          },
          {
            "id": "good_red_circle",
            "attrs": {}
          },
          {
            "id": "bad_red_circle",
            "attrs": {}
          },
          {
            "id": "good_red_square",
            "attrs": {}
          },
          {
            "id": "good_red_circle2",
            "attrs": {}
          },
          {
            "id": "bad_red_circle2",
            "attrs": {}
          }
        ]
      },
      "attrs": {}
    },
    {
      "id": "colors",
      "graph": {
        "edges": [
          {
            "from": "green",
            "to": "green",
            "attrs": {}
          },
          {
            "from": "red",
            "to": "red",
            "attrs": {}
          },
          {
            "from": "red",
            "to": "green",
            "attrs": {}
          }
        ],
        "nodes": [
          {
            "id": "green",
            "attrs": {}
          },
          {
            "id": "red",
            "attrs": {}
          }
        ]
      },
      "attrs": {}
    },
    {
      "id": "shapes",
      "graph": {
        "edges": [
          {
            "from": "circle",
            "to": "circle1",
            "attrs": {}
          },
          {
            "from": "circle",
            "to": "circle",
            "attrs": {}
          },
          {
            "from": "circle",
            "to": "square",
            "attrs": {}
          },
          {
            "from": "circle1",
            "to": "circle",
            "attrs": {}
          },
          {
            "from": "circle1",
            "to": "circle1",
            "attrs": {}
          },
          {
            "from": "circle1",
            "to": "square",
            "attrs": {}
          }
        ],
        "nodes": [
          {
            "id": "circle",
            "attrs": {}
          },
          {
            "id": "square",
            "attrs": {}
          },
          {
            "id": "circle1",
            "attrs": {}
          }
        ]
      },
      "attrs": {}
    }
  ],
  "typing": [
    {
      "from": "g1",
      "to": "shapes",
      "mapping": {
        "red_circle1": "circle1",
        "red_circle": "circle",
        "red_square": "square"
      },
      "attrs": {}
    },
    {
      "from": "g1",
      "to": "colors",
      "mapping": {
        "red_square": "red",
        "red_circle": "red",
        "red_circle1": "red"
      },
      "attrs": {}
    },
    {
      "from": "g2",
      "to": "quality",
      "mapping": {
        "good_square": "bad_good",
        "good_circle1": "bad_good",
        "bad_circle1": "bad_good",
        "bad_circle_good_circle": "bad_good",
        "c": "c"
      },
      "attrs": {}
    },
    {
      "from": "g2",
      "to": "shapes",
      "mapping": {
        "bad_circle_good_circle": "circle",
        "c": "circle",
        "good_square": "square",
        "bad_circle1": "circle1",
        "good_circle1": "circle1"
      },
      "attrs": {}
    },
    {
      "from": "g3",
      "to": "g2",
      "mapping": {
        "bad_red_circle11": "bad_circle1",
        "bad_red_circle2": "bad_circle1",
        "good_red_circle11": "good_circle1",
        "good_red_circle2": "good_circle1",
        "good_red_square": "good_square",
        "good_red_circle": "bad_circle_good_circle",
        "good_red_circle1": "bad_circle_good_circle",
        "bad_red_circle": "bad_circle_good_circle",
        "bad_red_circle1": "bad_circle_good_circle"
      },
      "attrs": {}
    },
    {
      "from": "g3",
      "to": "g1",
      "mapping": {
        "good_red_circle11": "red_circle1",
        "bad_red_circle11": "red_circle1",
        "good_red_circle2": "red_circle1",
        "bad_red_circle2": "red_circle1",
        "good_red_circle": "red_circle",
        "bad_red_circle": "red_circle",
        "good_red_circle1": "red_circle",
        "bad_red_circle1": "red_circle",
        "good_red_square": "red_square"
      },
      "attrs": {}
    }
  ],
  "relations": []
}

In [37]:
# Clear the DB for the previous hierarchy
hierarchy._clear()

# Load json-data back to the DB
hierarchy = Neo4jHierarchy.from_json(
    uri="bolt://localhost:7687", user="neo4j", password="admin", json_data=hierarchy_json)