Mercurial > hg > drupalISMI
comparison importFromOpenMind/importer/model2neo4j_restclient.py @ 48:6625019a0c96
old model2neo4j renamed to model2neo4j_restclient. new model2neo4j_client and model2neo4j_import. fixed ismixml2model and compare_models.
author | casties |
---|---|
date | Tue, 07 Feb 2017 21:06:13 +0100 |
parents | importFromOpenMind/importer/model2neo4j.py@74dfaed3600b |
children |
comparison
equal
deleted
inserted
replaced
47:378dcb66a27f | 48:6625019a0c96 |
---|---|
1 import networkx as nx | |
2 from neo4jrestclient.client import GraphDatabase | |
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 = '_ismi' | |
12 | |
13 # neo4j base URL | |
14 neo4jBaseURL = "http://localhost:7474/db/data/" | |
15 | |
16 # name of type attribute | |
17 node_type_attribute = '_type' | |
18 rel_type_attribute = '_type' | |
19 | |
20 | |
21 ## setup | |
22 | |
23 n4j_nodes = {} | |
24 n4j_relations = {} | |
25 | |
26 | |
27 def copyNodes(nx_graph, n4j_graph): | |
28 """copy all nodes from nx_graph to n4j_graph""" | |
29 | |
30 print("Copying nodes to Neo4J") | |
31 cnt = 0 | |
32 for node_id in nx.nodes_iter(nx_graph): | |
33 attrs = nx_graph.node[node_id] | |
34 ntype = attrs[node_type_attribute] | |
35 ismi_id = attrs['ismi_id'] | |
36 # create node with attributes | |
37 n4j_node = n4j_graph.nodes.create(**attrs) | |
38 # add labels | |
39 n4j_node.labels.add([project_label, ntype]) | |
40 # save reference | |
41 n4j_nodes[ismi_id] = n4j_node | |
42 | |
43 cnt += 1 | |
44 if cnt % 100 == 0: | |
45 print(" %s nodes"%cnt) | |
46 | |
47 | |
48 def copyRelations(nx_graph, n4j_graph): | |
49 """copy all relations from nx_graph to n4j_graph""" | |
50 | |
51 print("Copying relations to Neo4J") | |
52 cnt = 0 | |
53 for nx_edge in nx.edges_iter(nx_graph): | |
54 (nx_src, nx_tar) = nx_edge | |
55 # get attributes of edge | |
56 attrs = nx_graph.edge[nx_src][nx_tar][0] | |
57 etype = attrs[rel_type_attribute] | |
58 # get ismi_id of source and target nodes | |
59 src_id = nx_graph.node[nx_src]['ismi_id'] | |
60 tar_id = nx_graph.node[nx_tar]['ismi_id'] | |
61 # get Neo4J nodes | |
62 src = n4j_nodes.get(src_id, None) | |
63 if src is None: | |
64 print("ERROR: src node %s missing!"%src_id) | |
65 break | |
66 | |
67 tar = n4j_nodes.get(tar_id, None) | |
68 if tar is None: | |
69 print("ERROR: tar node %s missing!"%tar_id) | |
70 break | |
71 | |
72 # create Neo4J relation | |
73 n4j_rel = n4j_graph.relationships.create(src, etype, tar) | |
74 # add attributes | |
75 n4j_rel.properties = attrs | |
76 | |
77 cnt += 1 | |
78 if cnt % 100 == 0: | |
79 print(" %s relations"%cnt) | |
80 | |
81 | |
82 ## main | |
83 | |
84 print("Copy graph from networkx to Neo4J") | |
85 | |
86 # read commandline parameters | |
87 if len(sys.argv) > 1: | |
88 input_fn = sys.argv[1] | |
89 | |
90 # read networkx graph from pickle | |
91 print("Reading graph from %s"%input_fn) | |
92 nx_graph = nx.read_gpickle(input_fn) | |
93 print("Graph info: %s"%nx.info(nx_graph)) | |
94 | |
95 # open neo4j graph db | |
96 print("Opening Neo4J db at %s"%neo4jBaseURL) | |
97 n4j_graph = GraphDatabase(neo4jBaseURL, username="neo4j", password="neo5j") | |
98 | |
99 copyNodes(nx_graph, n4j_graph) | |
100 | |
101 copyRelations(nx_graph, n4j_graph) | |
102 | |
103 print("Done.") |