annotate graphML2RDF.py @ 3:c97d43ac8fa6

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