0
|
1 # Dieses Tool konvertiert graphMl Files erzeugt mit yED in einen graphen, entweder im dot, Format oder als RDF
|
|
2 # TODO: lots of ... Zuordnung graphenTyp zu Ontologie sollte konfiurierbar sein.
|
|
3 from lxml import etree
|
|
4 import os.path
|
|
5 import os
|
|
6
|
|
7
|
|
8 namespaces={'graphML':'http://graphml.graphdrawing.org/xmlns',
|
|
9 'y':'http://www.yworks.com/xml/graphml'
|
|
10 }
|
|
11
|
|
12 # Alle wesentlichen Informstionrn fuer einen Knoten
|
|
13 class Node:
|
|
14
|
|
15 label="" #Label in yED, wird auch gebraucht um gleiche Knoten zu identifizieren, wenn mehr als ein Graph zusammengefuehrt werden soll.
|
|
16 #Es muss also beim Erstellen auf Konsistenz der Label geachtet werden.
|
|
17 internalID="" #InternalID eine Knotens innerhalb eines Graphen, dieses sind nur pro Graph eindeutig-
|
|
18 externalRef="" # Referenzen auf externe Weseiten (key="d4")
|
|
19 internalRef="" # Referenzen auf andere Graphen (key="d4"), unterscheidung zwischen externer und interner, ob Pfad mit http beginnt.
|
|
20 numId=0 #Id des Knotens sollte eindeutig fuer alle Knoten sein.
|
|
21 nodeType=-1
|
|
22
|
|
23 def __init__(self,numId):
|
|
24 self.numId=numId
|
|
25
|
|
26 #Kante
|
|
27 class Edge:
|
|
28 src=None
|
|
29 target=None
|
|
30 edgeType=-1
|
|
31
|
|
32 def __init__(self,src,target):
|
|
33 self.src=src
|
|
34 self.target=target
|
|
35
|
|
36
|
|
37 # Erzeugt Ids fuer die Knoten
|
|
38 class IDDispensor:
|
|
39 currentID=0
|
|
40 def getID(self):
|
|
41 self.currentID+=1
|
|
42 return self.currentID
|
|
43
|
|
44 # Der eigentliche Graph
|
|
45 class Graph:
|
|
46 label2Ids={} # Zuweisung labels zu den Ids des Knoten
|
|
47 #id2nodes={} #Zuweisung id zu den Knoten
|
|
48 internalId2nodesID={} #Zuweisung interneID zur allgemeinen ID
|
|
49 edges=set() # Menger der Kanten
|
|
50 id2label={} # Zuweisung id zu den Labeln
|
|
51 edgeStyles={}
|
|
52 edgeDescription={}
|
|
53 graphURI=""
|
|
54 nodeStyles={}
|
|
55 nodeDescription={}
|
|
56
|
|
57 def __init__(self,dispensor):
|
|
58 self.dispensor=dispensor;
|
|
59 self.id2nodes={};
|
|
60 self.edges=set();
|
|
61
|
|
62
|
|
63 def convertGrahml(self,filename):
|
|
64 """Konvertiert ein Grahphml-File in ein Netzwerk mit Knoten und Kanten.
|
|
65 Die Abbildung von Layout auf Knoten- und Kantentypen erfolgt durch den Vergleich des Layout mit einer Palette
|
|
66 """
|
|
67 tree = etree.parse(filename)
|
|
68
|
|
69 nodes=tree.xpath("//graphML:node",namespaces=namespaces)
|
|
70
|
|
71 for node in nodes:
|
|
72 nodeIDs=node.xpath("@id",namespaces=namespaces)
|
|
73 labels=node.xpath(".//y:NodeLabel",namespaces=namespaces)
|
|
74 for nodeID in nodeIDs:
|
|
75 nodeIDString=unicode(nodeID)
|
|
76
|
|
77 labelString=None
|
|
78 for label in labels:
|
|
79 labelString=unicode(label.text).lstrip().rstrip()
|
|
80
|
|
81
|
|
82 newNode = Node(self.dispensor.getID())
|
|
83 newNode.label=labelString
|
|
84 newNode.internalID=nodeIDString
|
|
85
|
|
86 nodeRefs=node.xpath('./graphML:data[@key="d4"]',namespaces=namespaces)
|
|
87 #nodeRefs=node.xpath("./graphML:data",namespaces=namespaces)
|
|
88 for nodeRef in nodeRefs:
|
|
89 nodeRefString=nodeRef.text
|
|
90 if nodeRefString is None:
|
|
91 continue
|
|
92
|
|
93 #hack dw, scheinbar falsche urs drin
|
|
94 if nodeRefString.find("http")>0:
|
|
95 continue
|
|
96 if nodeRefString.lstrip().startswith("http:") or nodeRefString.startswith("https:") or nodeRefString.startswith("ftp:"):
|
|
97 newNode.externalRef=nodeRefString
|
|
98 else:
|
|
99 newNode.internalRef=nodeRefString
|
|
100
|
|
101 #--
|
|
102
|
|
103 #read styles
|
|
104 typeID=None
|
|
105 #fall 1 Generic Node
|
|
106 gns = node.xpath('./graphML:data[@key="d6"]/y:GenericNode',namespaces=namespaces)
|
|
107 for gn in gns:
|
|
108 style="g"
|
|
109 fills=gn.xpath('./y:Fill',namespaces=namespaces)
|
|
110 fs=None
|
|
111 for fill in fills:
|
|
112 fs=fill.attrib
|
|
113
|
|
114 borderstyles=gn.xpath('./y:BorderStyle',namespaces=namespaces)
|
|
115 bs=None
|
|
116 for borderstyle in borderstyles:
|
|
117 bs=borderstyle.attrib
|
|
118
|
|
119
|
|
120 config=gn.attrib.get("configuration")
|
|
121
|
|
122
|
|
123 typeID=self.getNodeTypeFromPalette(style,fs,bs,config)
|
|
124
|
|
125
|
|
126 #fall 2 shape Node
|
|
127 gns = node.xpath('./graphML:data[@key="d6"]/y:ShapeNode',namespaces=namespaces)
|
|
128 for gn in gns:
|
|
129 style="s"
|
|
130 fills=gn.xpath('./y:Fill',namespaces=namespaces)
|
|
131 fs=None
|
|
132 for fill in fills:
|
|
133 fs=fill.attrib
|
|
134
|
|
135 borderstyles=gn.xpath('./y:BorderStyle',namespaces=namespaces)
|
|
136 bs=None
|
|
137 for borderstyle in borderstyles:
|
|
138 bs=borderstyle.attrib
|
|
139
|
|
140
|
|
141 shapes=gn.xpath('./y:Shape',namespaces=namespaces)
|
|
142 shapeType=None
|
|
143 for shape in shapes:
|
|
144 shapeType=shape.attrib.get("type")
|
|
145
|
|
146
|
|
147 typeID=self.getNodeTypeFromPalette(style,fs,bs,shapeType)
|
|
148
|
|
149
|
|
150 #---
|
|
151 if typeID is None:
|
|
152 typeID=-1
|
|
153 newNode.nodeType=typeID
|
|
154 self.id2nodes[newNode.numId]=newNode
|
|
155 if labelString!=None:
|
|
156 self.label2Ids[labelString]=newNode.numId
|
|
157
|
|
158
|
|
159 self.internalId2nodesID[newNode.internalID]=newNode.numId
|
|
160
|
|
161 edges=tree.xpath("//graphML:edge",namespaces=namespaces)
|
|
162
|
|
163 for edge in edges:
|
|
164 srcIDs=edge.xpath("@source",namespaces=namespaces)
|
|
165 tarIDs=edge.xpath("@target",namespaces=namespaces)
|
|
166 for srcID in srcIDs:
|
|
167 source=unicode(srcID)
|
|
168
|
|
169 for tarID in tarIDs:
|
|
170 target=unicode(tarID)
|
|
171
|
|
172 #read styles
|
|
173
|
|
174 #lineStyles=edge.xpath('./graphML:data[@key="d10"]/y:GenericEdge/y:LineStyle',namespaces=namespaces)
|
|
175 lineStyles=edge.xpath('./graphML:data[@key="d10"]/.//y:LineStyle',namespaces=namespaces)
|
|
176 ls=None
|
|
177 for lineStyle in lineStyles:
|
|
178 ls=lineStyle.attrib
|
|
179
|
|
180 #arrows=edge.xpath('./graphML:data[@key="d10"]/y:GenericEdge/y:Arrows',namespaces=namespaces)
|
|
181 arrows=edge.xpath('./graphML:data[@key="d10"]/.//y:Arrows',namespaces=namespaces)
|
|
182 ars=None
|
|
183 for arrow in arrows:
|
|
184 ars=arrow.attrib
|
|
185
|
|
186 typeID=self.getTypeFromPalette(ls,ars)
|
|
187
|
|
188 newEdge=Edge(self.internalId2nodesID.get(source),self.internalId2nodesID.get(target))
|
|
189 newEdge.edgeType=typeID
|
|
190
|
|
191
|
|
192 self.edges.add(newEdge)
|
|
193
|
|
194
|
|
195 def getNodeTypeFromPalette(self,style,fs,bs,config):
|
|
196 for key,value in self.nodeStyles.items():
|
|
197 styleVorlage,fsVorlage,bsVorlage,configVorlage=value
|
|
198 if style!=styleVorlage:
|
|
199 continue
|
|
200
|
|
201 if config!=configVorlage:
|
|
202 continue
|
|
203
|
|
204
|
|
205 if self.cmpDict(fs,fsVorlage) and self.cmpDict(bs,bsVorlage):
|
|
206 print key
|
|
207 return key
|
|
208
|
|
209 return -1
|
|
210
|
|
211
|
|
212
|
|
213 def getTypeFromPalette(self,ls,ars):
|
|
214
|
|
215 for key,value in self.edgeStyles.items():
|
|
216 lsVorlage,arsVorlage=value
|
|
217 if self.cmpDict(ls,lsVorlage) and self.cmpDict(ars,arsVorlage):
|
|
218
|
|
219 return key
|
|
220
|
|
221 return -1
|
|
222
|
|
223 def cmpDict(self,x,y):
|
|
224 """Teste zwei dicts auf Gleichheit"""
|
|
225
|
|
226 if (x is None) or (y is None):
|
|
227 return False
|
|
228
|
|
229 for key in x.keys():
|
|
230 yVal=y.get(key,None)
|
|
231 xVal=x.get(key)
|
|
232 if yVal!=xVal:
|
|
233 return False
|
|
234
|
|
235 return True
|
|
236
|
|
237 def exportAsDot(self,filename,graphName,onlyMs=False,partOfGraph=None,linksToGraph=None):
|
|
238 out = file(filename,"w")
|
|
239
|
|
240 type2NodeShape={0:'style="solid" color="blue"',
|
|
241 1:'style="solid" color="lightblue"',
|
|
242 2:'style="solid" color="blue"',
|
|
243 3:'style="tapered" color="orange"',
|
|
244 4:'style="solid" color="green"',
|
|
245 5:'style="solid" color="sienna"',
|
|
246 6:'style="solid" color="magenta"',
|
|
247 -1:'style="dotted" color="red"'
|
|
248 }
|
|
249
|
|
250 type2EdgeShape={0:'style="dotted" color="blue"',
|
|
251 1:'style="solid"',
|
|
252 2:'style="bold"',
|
|
253 3:'style="tapered"',
|
|
254 4:'style="solid" color="green"',
|
|
255 5:'style="solid" color="sienna"',
|
|
256 6:'style="solid" color="magenta"',
|
|
257 -1:'style="dotted" color="red"'
|
|
258 }
|
|
259
|
|
260 out.write("""digraph %s {"""%graphName)
|
|
261 g=self
|
|
262
|
|
263 for key,value in g.id2nodes.items():
|
|
264 #name=value.label.replace("\n","").replace(" ","_").replace(".","_").replace("(","_").replace(")","_").replace("?","_").replace("'","_").replace(",","_")
|
|
265 #name=value.numId
|
|
266 name=key
|
|
267 label=value.label.replace("\n","")
|
|
268 url=value.externalRef
|
|
269
|
|
270 if onlyMs:
|
|
271 if not label.lstrip().startswith("Add"):
|
|
272 continue
|
|
273 try:
|
|
274 s="""%s [label="%s" URL="%s" %s];\n"""%(name,label.decode("utf-8"),url,type2NodeShape.get(value.nodeType))
|
|
275 out.write(s)
|
|
276 except:
|
|
277 s="""%s [label="%s" URL="%s" %s];\n"""%(name,repr(label),url,type2NodeShape.get(value.nodeType))
|
|
278 out.write(s)
|
|
279
|
|
280
|
|
281 for edge in g.edges:
|
|
282 try:
|
|
283 #sr=g.id2label.get(edge.src).replace("\n","").replace(" ","_").replace(".","_").replace("(","_").replace(")","_").replace("?","_").replace("'","_").replace(",","_").replace("-","_")
|
|
284 sr=edge.src
|
|
285
|
|
286 tg=edge.target
|
|
287 #tg=g.id2label.get(edge.target).replace("\n","").replace(" ","_").replace(".","_").replace("(","_").replace(")","_").replace("?","_").replace("'","_").replace(",","_").replace("-","_")
|
|
288
|
|
289 s = """%s -> %s [%s];\n"""%(sr,tg,type2EdgeShape.get(edge.edgeType))
|
|
290 out.write(s)
|
|
291 except:
|
|
292 pass
|
|
293
|
|
294
|
|
295 if not partOfGraph is None:
|
|
296 for nodeID,graphList in partOfGraph.items():
|
|
297 #fromNode=g.id2label.get(nodeID).replace("\n","").replace(" ","_").replace(".","_").replace("(","_").replace(")","_").replace("?","_").replace("'","_").replace(",","_").replace("-","_")
|
|
298 fromNode=nodeID
|
|
299 for graph in graphList:
|
|
300 try:
|
|
301 s = """G_%s -> %s [color="yellow"];\n"""%(graph.replace(".","_"),fromNode)
|
|
302 out.write(s)
|
|
303 s = """G_%s [label="%s" color="green" fillcolor="green" style="filled"];\n"""%(graph.replace(".","_"),graph)
|
|
304 out.write(s)
|
|
305 except:
|
|
306 pass
|
|
307
|
|
308
|
|
309 if not linksToGraph is None:
|
|
310 for nodeID,graph in linksToGraph:
|
|
311 #fromNode=g.id2label.get(nodeID).replace("\n","").replace(" ","_").replace(".","_").replace("(","_").replace(")","_").replace("?","_").replace("'","_").replace(",","_").replace("-","_")
|
|
312 fromNode=nodeID
|
|
313 splitted=graph.split("/")
|
|
314 print graph
|
|
315 gr = splitted[-1]
|
|
316 print gr
|
|
317 try:
|
|
318 s = """%s -> G_%s [color="green"];\n"""%(fromNode,gr.replace(".","_"))
|
|
319 out.write(s)
|
|
320 s = """G_%s [label="%s" color="green" fillcolor="green" style="filled"];\n"""%(gr.replace(".","_"),gr)
|
|
321 out.write(s)
|
|
322 except:
|
|
323 pass
|
|
324 out.write("}")
|
|
325
|
|
326
|
|
327 out.close()
|
|
328
|
|
329
|
|
330 def exportAsRDF(self,filename,graphName,onlyMs=False,partOfGraph=None,linksToGraph=None):
|
|
331 out = file(filename,"w")
|
|
332
|
5
|
333 base="http://ontologies.mpiwg-berlin.mpg.de/reasearch/harriot.owl/1.0/"
|
|
334 ressourceBase="http://entities.mpiwg-berlin.mpg.de/reasearch/harriot.owl/1.0/"
|
0
|
335 type2NodeShape={0: base+"Topic",
|
|
336 1: base+"Topic",
|
|
337 2: base+"Topic",
|
|
338 3: base+"FolioPage",
|
|
339 4: base+"4",
|
|
340 5: base+"RelatedFolioPage",
|
|
341 -1: base+"UNKNOWN"}
|
|
342
|
|
343 type2EdgeShape={0: base+"has_prev_by_pagination",
|
|
344 1: base+"has_conjectural_relation",
|
|
345 2: base+"has_prev_by_conjection",
|
|
346 3: base+"has_prev_by_conjection",
|
|
347 4: base+"result_used_from",
|
|
348 5: base+"result_used_from",
|
|
349 -1: base+"is_related_to"}
|
|
350
|
|
351 #out.write("""digraph %s {"""%graphName)
|
|
352 g=self
|
|
353
|
|
354 for key,value in g.id2nodes.items():
|
|
355 #name=value.label.replace("\n","").replace(" ","_").replace(".","_").replace("(","_").replace(")","_").replace("?","_").replace("'","_").replace(",","_")
|
|
356 #name=value.numId
|
|
357 name=key
|
|
358 label=value.label.replace("\n","")
|
|
359 url=value.externalRef
|
|
360
|
|
361 if onlyMs:
|
|
362 if not label.lstrip().startswith("Add"):
|
|
363 continue
|
|
364
|
5
|
365 ressourceURI=ressourceBase+str(name)
|
0
|
366 try:
|
|
367 s=""
|
|
368 if label!="":
|
|
369 s+="""<%s> <http://www.w3.org/2000/01/rdf-schema#label> "%s".\n"""%(ressourceURI,label.decode("utf-8").replace('"','\"'))
|
|
370 if url!="":
|
|
371 s+="""<%s> <%s> <%s>.\n"""%(ressourceURI,base+"describes",url)
|
|
372 print value.nodeType
|
|
373 s+="""<%s> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type><%s>.\n"""%(ressourceURI,type2NodeShape.get(value.nodeType))
|
|
374 #s="""%s [label="%s" URL="%s" %s];\n"""%(name,label.decode("utf-8"),url,type2NodeShape.get(value.nodeType))
|
|
375 out.write(s)
|
|
376 except:
|
|
377 if label!="":
|
|
378 s="""<%s> <http://www.w3.org/2000/01/rdf-schema#label> "%s".\n"""%(ressourceURI,'CHECK_THIS')
|
|
379 #s="""<%s> <http://www.w3.org/2000/01/rdf-schema#label> "%s"."""%(ressourceURI,repr(label).replace('"','\"'))
|
|
380 if url!="":
|
|
381 s+="""<%s> <%s> <%s>."""%(ressourceURI,base+"describes",url)
|
|
382 s+="""<%s> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type><%s>.\n"""%(ressourceURI,type2NodeShape.get(value.nodeType))
|
|
383 #s="""%s [label="%s" URL="%s" %s];\n"""%(name,repr(label),url,type2NodeShape.get(value.nodeType))
|
|
384 out.write(s)
|
|
385
|
|
386
|
|
387 for edge in g.edges:
|
|
388 try:
|
|
389 #sr=g.id2label.get(edge.src).replace("\n","").replace(" ","_").replace(".","_").replace("(","_").replace(")","_").replace("?","_").replace("'","_").replace(",","_").replace("-","_")
|
|
390 sr=edge.src
|
|
391
|
|
392 tg=edge.target
|
|
393 #tg=g.id2label.get(edge.target).replace("\n","").replace(" ","_").replace(".","_").replace("(","_").replace(")","_").replace("?","_").replace("'","_").replace(",","_").replace("-","_")
|
|
394
|
5
|
395 s ="""<%s><%s><%s>.\n"""%(ressourceBase+str(sr),type2EdgeShape.get(edge.edgeType),ressourceBase+str(tg))
|
0
|
396 #s = """%s -> %s [%s];\n"""%(sr,tg,type2EdgeShape.get(edge.edgeType))
|
|
397 out.write(s)
|
|
398 except:
|
|
399 pass
|
|
400
|
|
401
|
|
402 if not partOfGraph is None:
|
|
403 for nodeID,graphList in partOfGraph.items():
|
|
404 #fromNode=g.id2label.get(nodeID).replace("\n","").replace(" ","_").replace(".","_").replace("(","_").replace(")","_").replace("?","_").replace("'","_").replace(",","_").replace("-","_")
|
|
405 fromNode=nodeID
|
|
406 for graph in graphList:
|
|
407 try:
|
5
|
408 ressourceURI=ressourceBase+graph
|
|
409 s ="""<%s><%s><%s>.\n"""%(ressourceBase+str(fromNode),base+"is_part_of",ressourceURI)
|
0
|
410 #s = """G_%s -> %s [color="yellow"];\n"""%(graph.replace(".","_"),fromNode)
|
|
411 out.write(s)
|
|
412
|
|
413 s="""<%s> <http://www.w3.org/2000/01/rdf-schema#label> "%s".\n"""%(ressourceURI,graph)
|
|
414 s+="""<%s> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type><%s>.\n"""%(ressourceURI,base+"HarriotGraph")
|
|
415
|
|
416 #s = """G_%s [label="%s" color="green" fillcolor="green" style="filled"];\n"""%(graph.replace(".","_"),graph)
|
|
417 out.write(s)
|
|
418 except:
|
|
419 pass
|
|
420
|
|
421
|
|
422 if not linksToGraph is None:
|
|
423 for nodeID,graph in linksToGraph:
|
|
424 #fromNode=g.id2label.get(nodeID).replace("\n","").replace(" ","_").replace(".","_").replace("(","_").replace(")","_").replace("?","_").replace("'","_").replace(",","_").replace("-","_")
|
|
425 fromNode=nodeID
|
|
426 splitted=graph.split("/")
|
|
427 print graph
|
|
428 gr = splitted[-1]
|
|
429 print gr
|
5
|
430 ressourceURI=ressourceBase+gr
|
0
|
431
|
|
432
|
|
433
|
|
434 typeSrc=type2NodeShape.get(nodeID)
|
|
435 if typeSrc==base+"Topic":
|
|
436 relation="is_specified_in"
|
|
437 else:
|
|
438 relation="see_also"
|
|
439
|
|
440 try:
|
5
|
441 s ="""<%s><%s><%s>.\n"""%(ressourceBase+str(fromNode),relation,ressourceURI)
|
0
|
442 #s = """%s -> G_%s [color="green"];\n"""%(fromNode,gr.replace(".","_"))
|
|
443 out.write(s)
|
|
444
|
|
445 s="""<%s> <http://www.w3.org/2000/01/rdf-schema#label> "%s".\n"""%(ressourceURI,gr)
|
|
446 s+="""<%s> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type><%s>.\n"""%(ressourceURI,base+"HarriotGraph")
|
|
447
|
|
448 #s = """G_%s [label="%s" color="green" fillcolor="green" style="filled"];\n"""%(gr.replace(".","_"),gr)
|
|
449 out.write(s)
|
|
450 except:
|
|
451 pass
|
|
452 #out.write("}")
|
|
453
|
|
454
|
|
455 out.close()
|
|
456 def readPalette(self,palettePath):
|
|
457 typeNr=0
|
|
458 palette = etree.parse(palettePath)
|
|
459 edges=palette.xpath("//graphML:edge",namespaces=namespaces)
|
|
460 # lage alle kanten in der palette
|
|
461 for edge in edges:
|
|
462 #relevant fuer die einordnugn ist data key=12 linestyle und arrows
|
|
463 lineStyles=edge.xpath('./graphML:data[@key="d12"]/y:GenericEdge/y:LineStyle',namespaces=namespaces)
|
|
464 ls=None
|
|
465 for lineStyle in lineStyles:
|
|
466 ls=lineStyle
|
|
467
|
|
468 arrows=edge.xpath('./graphML:data[@key="d12"]/y:GenericEdge/y:Arrows',namespaces=namespaces)
|
|
469 ars=None
|
|
470 for arrow in arrows:
|
|
471 ars=arrow
|
|
472
|
|
473 #get description
|
|
474 ds=""
|
|
475 descriptions=edge.xpath('./graphML:data[@key="d9"]',namespaces=namespaces)
|
|
476 for description in descriptions:
|
|
477 ds=description
|
|
478
|
|
479 self.edgeDescription[typeNr]=ds.text
|
|
480 self.edgeStyles[typeNr]=(ls.attrib,ars.attrib)
|
|
481 typeNr+=1
|
|
482
|
|
483 typeNr=0
|
|
484 nodes=palette.xpath("//graphML:node",namespaces=namespaces)
|
|
485 for node in nodes:
|
|
486 style=""
|
|
487 #fall 1 Generic Node
|
|
488 gns = node.xpath('./graphML:data[@key="d7"]/y:GenericNode',namespaces=namespaces)
|
|
489 for gn in gns:
|
|
490 style="g"
|
|
491 fills=gn.xpath('./y:Fill',namespaces=namespaces)
|
|
492 fs=None
|
|
493 for fill in fills:
|
|
494 fs=fill.attrib
|
|
495
|
|
496 borderstyles=gn.xpath('./y:BorderStyle',namespaces=namespaces)
|
|
497 bs=None
|
|
498 for borderstyle in borderstyles:
|
|
499 bs=borderstyle.attrib
|
|
500
|
|
501
|
|
502 config=gn.attrib.get("configuration")
|
|
503
|
|
504 #get description
|
|
505 ds=""
|
|
506 descriptions=node.xpath('./graphML:data[@key="d4"]',namespaces=namespaces)
|
|
507 for description in descriptions:
|
|
508 ds=description.text
|
|
509
|
|
510 self.nodeDescription[typeNr]=ds
|
|
511
|
|
512 self.nodeStyles[typeNr]=(style,fs,bs,config)
|
|
513 typeNr+=1
|
|
514
|
|
515 #fall 2 shape Node
|
|
516 gns = node.xpath('./graphML:data[@key="d7"]/y:ShapeNode',namespaces=namespaces)
|
|
517 for gn in gns:
|
|
518 style="s"
|
|
519 fills=gn.xpath('./y:Fill',namespaces=namespaces)
|
|
520 fs=None
|
|
521 for fill in fills:
|
|
522 fs=fill.attrib
|
|
523
|
|
524 borderstyles=gn.xpath('./y:BorderStyle',namespaces=namespaces)
|
|
525 bs=None
|
|
526 for borderstyle in borderstyles:
|
|
527 bs=borderstyle.attrib
|
|
528
|
|
529
|
|
530 shapes=gn.xpath('./y:Shape',namespaces=namespaces)
|
|
531 shapeType=None
|
|
532 for shape in shapes:
|
|
533 shapeType=shape.attrib.get("type")
|
|
534
|
|
535
|
|
536
|
|
537 #get description
|
|
538 ds=""
|
|
539 descriptions=node.xpath('./graphML:data[@key="d4"]',namespaces=namespaces)
|
|
540 for description in descriptions:
|
|
541 ds=description.text
|
|
542
|
|
543 self.nodeDescription[typeNr]=ds
|
|
544 self.nodeStyles[typeNr]=(style,fs,bs,shapeType)
|
|
545 typeNr+=1
|
|
546
|
|
547 def merge(graphs,dispensor):
|
|
548 filter=['supermap.graphml']
|
|
549 partOfGraph={}
|
|
550 linksToGraph=set()
|
|
551
|
|
552 edges=set()
|
|
553 mg =Graph(dispensor)
|
|
554 for g in graphs:
|
|
555 if g.graphURI in filter:
|
|
556 continue
|
|
557 idalt2neu={}
|
|
558 for nodeid in g.id2nodes.keys():
|
|
559 node=g.id2nodes.get(nodeid)
|
|
560 label=node.label
|
|
561 currentID =mg.label2Ids.get(label,dispensor.getID()) #hole id wenn existent sonst neue
|
|
562
|
|
563 mg.label2Ids[label]=currentID
|
|
564 mg.id2label[currentID]=label
|
|
565 idalt2neu[node.numId]=currentID
|
|
566 mg.id2nodes[currentID]=node
|
|
567
|
|
568
|
|
569 if node.internalRef!="":
|
|
570 linksToGraph.add((currentID,node.internalRef))
|
|
571
|
|
572 containedIn = partOfGraph.get(currentID,set())
|
|
573
|
|
574
|
|
575 containedIn.add(g.graphURI)
|
|
576 partOfGraph[currentID]=containedIn
|
|
577
|
|
578
|
|
579 for edge in g.edges:
|
|
580 src=edge.src
|
|
581 target=edge.target
|
|
582
|
|
583 edge.src=idalt2neu.get(src)
|
|
584 edge.target=idalt2neu.get(target)
|
|
585 edges.add(edge)
|
|
586
|
|
587 mg.edges=edges
|
|
588 return mg,partOfGraph,linksToGraph
|
|
589
|
|
590 if __name__ == '__main__':
|
|
591 dispensor = IDDispensor()
|
|
592 #g1=Graph(dispensor)
|
|
593 #g1.readPalette("/Users/dwinter/Documents/Projekte/Diss - data-mining/eclipseWorkspace/graphML2RDF/examples/Manuscripts_3.graphml")
|
|
594 #g1.convertGrahml("/Users/dwinter/Documents/Projekte/Diss - data-mining/eclipseWorkspace/graphML2RDF/examples/5.7.3_cubics_other.graphml")
|
|
595
|
|
596
|
|
597
|
|
598 path="/Users/dwinter/Documents/Projekte/Europeana/harriot-graphml/Maps_20120523/"
|
|
599 ls = os.listdir(path)
|
|
600 graphs=set()
|
|
601
|
|
602 for l in ls:
|
|
603
|
|
604 g1=Graph(dispensor)
|
|
605 g1.readPalette("/Users/dwinter/Documents/Projekte/Diss - data-mining/eclipseWorkspace/graphML2RDF/examples/Manuscripts_3.graphml")
|
|
606
|
|
607 g1.convertGrahml(path+l)
|
|
608 g1.graphURI=l
|
|
609 graphs.add(g1)
|
|
610
|
|
611 g,po,lg = merge(graphs,dispensor)
|
|
612
|
|
613
|
|
614 #print g.label2Ids.keys()
|
|
615 #print len(g.label2Ids.keys())
|
|
616
|
|
617 #g.readPalette("/Users/dwinter/Documents/Projekte/Diss - data-mining/eclipseWorkspace/graphML2RDF/examples/Manuscripts_3.graphml")
|
|
618 #g.exportAsDot("/tmp/out.dot", "harriot",onlyMs=False,partOfGraph=po,linksToGraph=lg)
|
|
619 g.exportAsDot("/tmp/out.dot", "harriot",onlyMs=False,partOfGraph=po,linksToGraph=lg)
|
|
620 g.exportAsRDF("/tmp/out.rdf", "harriot",onlyMs=False,partOfGraph=po,linksToGraph=lg)
|
|
621
|
|
622 out2 = file("/tmp/out.txt","w")
|
|
623 for key in g.label2Ids.keys():
|
|
624 try:
|
|
625 out2.write('"'+key+'"'+"\n")
|
|
626 except:
|
|
627 pass
|
|
628 out2.close()
|
|
629 |