annotate importFromOpenMind/importer/ismi2neo4j.py @ 17:4dfd832e9cd9

added automatic creation of inverse relations. added more attribute types.
author casties
date Thu, 03 Sep 2015 18:48:21 +0200
parents de0a06eef13b
children 0827156df210
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
1 import urllib.request
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
2 import json
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
3 from neo4jrestclient.client import GraphDatabase, Node
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
4
17
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
5 ## configure behaviour
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
6
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
7 # add inverse relations as "<relation"
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
8 add_inverse_relations = True
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
9
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
10 # try to find and re-use existing nodes in neo4j (slow!)
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
11 keep_nodes = False
16
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
12
17
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
13 # label added to all nodes
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
14 project_label = '_ismi_inv_rel'
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
15
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
16 # OpenMind base URL
16
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
17 baseURL="http://localhost:18080/ismi-richfaces/jsonInterface?"
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
18
17
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
19 # neo4j base URL
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
20 neo4jBaseURL = "http://localhost:7474/db/data/"
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
21
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
22
16
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
23 entsURL=baseURL+"method=get_ents&oc=%s"
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
24
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
25 entURL=baseURL+"method=get_ent&id=%s&include_content=True"
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
26
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
27
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
28 def readJSON(url):
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
29 wsh=urllib.request.urlopen(url)
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
30 txt = wsh.read()
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
31 return json.loads(txt.decode("utf-8"))
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
32
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
33 defs_json = readJSON(baseURL+"method=get_defs")
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
34
17
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
35 # current list of all definitions
16
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
36 ismi_defs = [atts['ov'] for atts in defs_json['defs']]
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
37
17
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
38 #ismi_types=["PERSON","WITNESS","CODEX","PLACE","COLLECTION","REPOSITORY"]
16
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
39
17
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
40
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
41 gdb = GraphDatabase(neo4jBaseURL, username="neo4j", password="neo5j")
16
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
42
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
43 n4j_nodes = {}
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
44 ismi_relations = {}
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
45 n4j_relations = {}
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
46
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
47 ent_exclude_attrs = [
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
48 'lw',
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
49 'node_type',
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
50 'nov'
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
51 ]
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
52
17
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
53 def fixName(name, is_src_rel=False, is_tar_rel=False):
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
54 # these are too embarrasing...
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
55 if 'FLORUIT' in name:
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
56 name = name.replace('FLORUIT', 'FLOURISH')
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
57
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
58 elif 'floruit' in name:
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
59 name = name.replace('floruit', 'flourish')
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
60
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
61 if is_src_rel:
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
62 name = name + '>'
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
63
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
64 if is_tar_rel:
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
65 name = '<' + name
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
66
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
67 return name
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
68
16
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
69
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
70 def getNode(ismi_id=None):
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
71 if ismi_id is not None:
17
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
72 res = gdb.query("match (n {ismi_id: %s}) return n"%ismi_id, returns=(Node))
16
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
73 if len(res) > 0:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
74 return res[0]
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
75
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
76 return None
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
77
17
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
78
16
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
79 def nodeFromEnt(ent, etype):
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
80 attrs = {}
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
81 # go through all attributes
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
82 for att in ent['atts']:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
83 ct = att.get('content_type', None)
17
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
84 if ct is None or ct.lower() in ['text', 'arabic', 'bool', 'boolean', 'url', 'language']:
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
85 # normal text attribute (assume no content_type is text too...)
16
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
86 key = att['name']
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
87 val = att['ov']
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
88
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
89 if key in ent_exclude_attrs:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
90 # exclude attribute
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
91 continue
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
92
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
93 # keep attribute
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
94 attrs[key] = val
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
95
17
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
96 elif ct == 'num':
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
97 # number attribute
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
98 key = att['name']
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
99 val = att['ov']
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
100
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
101 if key in ent_exclude_attrs:
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
102 # exclude attribute
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
103 continue
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
104
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
105 # keep attribute, assume num is int
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
106 attrs[key] = int(val)
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
107
16
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
108 elif ct == 'date':
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
109 # date attribute
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
110 key = att['name']
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
111 val = att['ov']
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
112 #print("don't know what to do with date: %s=%s"%(key,val))
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
113
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
114 elif ct == 'old':
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
115 # ignore attribute
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
116 continue
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
117
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
118 else:
17
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
119 print("WARN: attribute with unknown content_type: %s"%repr(att))
16
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
120 # ignore other content types
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
121 continue
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
122
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
123 # process base attributes
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
124 oc = ent['oc']
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
125 if oc != etype:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
126 print("ERROR: entity type doesn't match!")
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
127 return null
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
128
17
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
129 attrs['type'] = fixName(oc)
16
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
130
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
131 ismi_id = ent['id']
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
132 # rename id to ismi_id
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
133 attrs['ismi_id'] = ismi_id
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
134
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
135 ov = ent.get('ov', None)
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
136 if ov is not None:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
137 # save ov as label
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
138 attrs['label'] = ov
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
139
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
140 # create node with attributes
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
141 node = gdb.nodes.create(**attrs)
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
142 # add labels
17
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
143 node.labels.add([project_label, fixName(etype)])
16
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
144 return node
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
145
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
146
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
147 # In[77]:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
148
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
149 def relsFromEnt(ent, relations):
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
150 # go through src_rels and tar_rels
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
151 rels = ent.get('src_rels', []) + ent.get('tar_rels', [])
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
152 for rel in rels:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
153 rel_id = rel['id']
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
154 if rel_id in relations:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
155 old_rel = relations[rel_id]
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
156 if rel != old_rel:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
157 print("ERROR: relation is different: %s != %s"%(repr(rel), repr(old_rel)))
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
158 continue
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
159
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
160 relations[rel_id] = rel
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
161
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
162 return relations
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
163
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
164
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
165 # In[110]:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
166
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
167 def n4jrelationsFromRels(rels, nodes):
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
168 # go through all rels
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
169 print("importing %s relations"%len(rels))
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
170 cnt = 0
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
171 for rel in rels.values():
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
172 cnt += 1
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
173 if cnt % 100 == 0:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
174 print(" %s relations"%cnt)
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
175
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
176 rel_id = rel['id']
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
177 rel_name = rel['name']
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
178 src_id = rel['src_id']
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
179 tar_id = rel['tar_id']
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
180 src = nodes.get(src_id, None)
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
181 if src is None:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
182 print("ERROR: relation %s src node %s missing!"%(rel_id,src_id))
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
183 continue
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
184
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
185 tar = nodes.get(tar_id, None)
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
186 if tar is None:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
187 print("ERROR: relation %s tar node %s missing!"%(rel_id,tar_id))
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
188 continue
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
189
17
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
190 if add_inverse_relations:
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
191 n4j_rel = [gdb.relationships.create(src, fixName(rel_name, is_src_rel=True), tar),
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
192 gdb.relationships.create(tar, fixName(rel_name, is_tar_rel=True), src)]
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
193
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
194 else:
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
195 n4j_rel = gdb.relationships.create(src, fixName(rel_name), tar)
4dfd832e9cd9 added automatic creation of inverse relations.
casties
parents: 16
diff changeset
196
16
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
197 n4j_relations[rel_id] = n4j_rel
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
198
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
199 return n4j_relations
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
200
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
201
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
202 # In[114]:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
203
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
204 def importEnts(etype):
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
205 # read json for all entities of given type
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
206 json = readJSON(entsURL%etype)
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
207 ents = json['ents']
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
208 print("importing %s %ss"%(len(ents),etype))
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
209 cnt = 0
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
210 for ent in ents:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
211 cnt += 1
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
212 if cnt % 100 == 0:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
213 print(" %s %ss"%(cnt, etype))
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
214
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
215 # extract ismi id
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
216 ismi_id = ent['id']
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
217
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
218 node = None
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
219
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
220 # fetch full data for entity
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
221 ent_json = readJSON(entURL%ismi_id)
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
222 ent_data = ent_json['ent']
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
223 # create neo4j node
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
224 if keep_nodes:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
225 node = getNode(ismi_id)
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
226
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
227 if node is None:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
228 node = nodeFromEnt(ent_data, etype)
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
229
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
230 if ismi_id in n4j_nodes:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
231 print("ERROR: entity with id=%s exists!"%ismi_id)
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
232 return
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
233
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
234 # save node reference
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
235 n4j_nodes[ismi_id] = node
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
236
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
237 # extract relations
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
238 relsFromEnt(ent_data, ismi_relations)
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
239
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
240 #if cnt >= 100:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
241 # return
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
242
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
243
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
244 # In[119]:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
245
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
246 def importAllEnts(etypes):
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
247
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
248 for etype in etypes:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
249 importEnts(etype)
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
250
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
251 n4jrelationsFromRels(ismi_relations, n4j_nodes)
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
252
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
253
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
254 # In[120]:
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
255
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
256 #importAllEnts(ismi_types)
de0a06eef13b new neo4j importer for network visualisation frontend.
casties
parents:
diff changeset
257 importAllEnts(ismi_defs)