23
|
1 import networkx as nx
|
|
2 from neo4jrestclient.client import GraphDatabase, Node
|
|
3 import sys
|
|
4
|
|
5 ## configure behaviour
|
|
6
|
|
7 # metworkx graph file
|
|
8 input_fn = 'ismi_graph.gpickle'
|
|
9
|
|
10 # label added to all nodes
|
|
11 project_label = '_ismi4'
|
|
12
|
|
13 # neo4j base URL
|
|
14 neo4jBaseURL = "http://localhost:7474/db/data/"
|
|
15
|
|
16
|
|
17 ## setup
|
|
18
|
|
19 n4j_nodes = {}
|
|
20 n4j_relations = {}
|
|
21
|
|
22
|
|
23 def fixName(name, is_src_rel=False, is_tar_rel=False, att_from_rel=False):
|
|
24 # these are too embarrassing...
|
|
25 if 'FLORUIT' in name:
|
|
26 name = name.replace('FLORUIT', 'FLOURISH')
|
|
27
|
|
28 elif 'floruit' in name:
|
|
29 name = name.replace('floruit', 'flourish')
|
|
30
|
|
31 if is_src_rel:
|
|
32 #name = name + '>'
|
|
33 pass
|
|
34
|
|
35 if is_tar_rel:
|
|
36 name = '<' + name
|
|
37
|
|
38 if att_from_rel:
|
|
39 # clean up relations as attribute names
|
|
40 name = name.replace('is_', '')
|
|
41 name = name.replace('has_', '')
|
|
42 name = name.replace('was_', '')
|
|
43 name = name.replace('_of', '')
|
|
44
|
|
45 return name
|
|
46
|
|
47
|
|
48 def copyNodes(nx_graph, n4j_graph):
|
|
49 """copy all nodes from nx_graph to n4j_graph"""
|
|
50
|
|
51 print("Copying nodes to Neo4J")
|
|
52 cnt = 0
|
|
53 for node_id in nx.nodes_iter(nx_graph):
|
|
54 attrs = nx_graph.node[node_id]
|
|
55 type = attrs['type']
|
|
56 ismi_id = attrs['ismi_id']
|
|
57 # create node with attributes
|
|
58 n4j_node = n4j_graph.nodes.create(**attrs)
|
|
59 # add labels
|
|
60 n4j_node.labels.add([project_label, type])
|
|
61 # save reference
|
|
62 n4j_nodes[ismi_id] = n4j_node
|
|
63
|
|
64 cnt += 1
|
|
65 if cnt % 100 == 0:
|
|
66 print(" %s"%cnt)
|
|
67
|
|
68
|
|
69 def copyRelations(nx_graph, n4j_graph):
|
|
70 """copy all relations from nx_graph to n4j_graph"""
|
|
71
|
|
72 print("Copying relations to Neo4J")
|
|
73 cnt = 0
|
|
74 for nx_edge in nx.edges_iter(nx_graph):
|
|
75 (nx_src, nx_tar) = nx_edge
|
|
76 # get attributes of edge
|
|
77 attrs = nx_graph.edge[nx_src][nx_tar][0]
|
|
78 type = attrs['type']
|
|
79 # get ismi_id of source and target nodes
|
|
80 src_id = nx_graph.node[nx_src]['ismi_id']
|
|
81 tar_id = nx_graph.node[nx_tar]['ismi_id']
|
|
82 # get Neo4J nodes
|
|
83 src = n4j_nodes.get(src_id, None)
|
|
84 if src is None:
|
|
85 print("ERROR: src node %s missing!"%src_id)
|
|
86 break
|
|
87
|
|
88 tar = n4j_nodes.get(tar_id, None)
|
|
89 if tar is None:
|
|
90 print("ERROR: tar node %s missing!"%tar_id)
|
|
91 break
|
|
92
|
|
93 # create Neo4J relation
|
|
94 n4j_rel = n4j_graph.relationships.create(src, type, tar)
|
|
95 # add attributes
|
|
96 n4j_rel.properties = attrs
|
|
97
|
|
98 cnt += 1
|
|
99 if cnt % 100 == 0:
|
|
100 print(" %s"%cnt)
|
|
101
|
|
102
|
|
103 ## main
|
|
104
|
|
105 print("Copy graph from networkx to Neo4J")
|
|
106
|
|
107 # read commandline parameters
|
|
108 if len(sys.argv) > 1:
|
|
109 input_fn = sys.argv[1]
|
|
110
|
|
111 # read networkx graph from pickle
|
|
112 print("Reading graph from %s"%input_fn)
|
|
113 nx_graph = nx.read_gpickle(input_fn)
|
|
114 print("Graph info: %s"%nx.info(nx_graph))
|
|
115
|
|
116 # open neo4j graph db
|
|
117 print("Opening Neo4J db at %s"%neo4jBaseURL)
|
|
118 n4j_graph = GraphDatabase(neo4jBaseURL, username="neo4j", password="neo5j")
|
|
119
|
|
120 copyNodes(nx_graph, n4j_graph)
|
|
121
|
|
122 copyRelations(nx_graph, n4j_graph)
|
|
123
|
|
124 print("Done.")
|