-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathx3ml.py
318 lines (242 loc) · 9.81 KB
/
x3ml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!.venv/bin/python
from lxml import etree
import sys
import hashlib
import json
lidoSchemaURI = 'http://www.lido-schema.org'
lidoSchemaXSD = 'http://www.lido-schema.org/schema/v1.0/lido-v1.0.xsd'
lidoSchemaXSI = 'http://www.w3.org/2001/XMLSchema-instance'
gmlSchemaURI = 'http://www.opengis.net/gml'
skosSchemaURL = 'http://www.w3.org/2004/02/skos/core'
oaiSchemaURL = 'http://www.openarchives.org/OAI/2.0/'
xmlnsURI = 'http://www.w3.org/XML/1998/namespace'
lidoNS = {'lido': lidoSchemaURI, 'gml': gmlSchemaURI, 'skos': skosSchemaURL}
def lidoXPath(elem, path):
return elem.xpath(path, namespaces=lidoNS)
def lidoExpandNS(s):
return s.replace('lido:', f'{{{lidoSchemaURI}}}')
def lidoCompressNS(s):
return s.replace(f'{{{lidoSchemaURI}}}', 'lido:')
def md5Hash(s):
return hashlib.md5(s.encode()).hexdigest()
def MAPPING_FILE():
return 'lido2rdf.x3ml'
DOMAIN_PATH = './domain/source_node'
DOMAIN_TYPE = './domain/target_node/entity/type'
DOMAIN_COND = './domain/target_node/if/or/if/equals'
PATH_SRR = './path/source_relation/relation'
PATH_TRR = './path/target_relation/relationship'
PATH_TRE = './path/target_relation/if/or/if/equals'
RANGE_SN = './range/source_node'
RANGE_ENTT = './range/target_node/entity'
RANGE_TYPE = './range/target_node/entity/type'
COLLECTION_ID_TAG = 'cID'
COLLECTION_TAG = 'collection'
PATH_ID_TAG = 'lidoPath'
class lxpath():
'''Wrapper for lidoXPath'''
def __init__(self, tag):
self.tag = tag
def firstId(self, elem):
'''Returns the first id'''
if not self.tag:
return elem.text
if iDs := lidoXPath(elem, f"./{self.tag}/text()"):
return iDs[0]
def children(self, elem):
'''Retruns all child elements'''
if not self.tag:
return [elem]
return lidoXPath(elem, f"./{self.tag}")
'''Mapping lido tags to its ID tags (lxpath)'''
LIDO_ID_MAP = {
'lido:lido': lxpath('lido:lidoRecID'),
'lido:event': lxpath('lido:eventID'),
'lido:eventType': lxpath('lido:eventID'),
'lido:actor': lxpath('lido:actorID'),
'lido:category': lxpath('lido:conceptID'),
'lido:repositorySet': lxpath('lido:workID'),
'lido:place': lxpath('lido:placeID'),
'lido:namePlaceSet': lxpath('lido:appellationValue'),
'lido:subjectConcept': lxpath('lido:conceptID'),
'lido:recordWrap': lxpath('lido:recordID'),
'lido:object': lxpath('lido:objectID'),
'lido:recordType': lxpath('lido:conceptID'),
'lido:rightsHolder': lxpath('lido:legalBodyID'),
'lido:repositoryName': lxpath('lido:legalBodyID'),
'lido:measurementType': lxpath(''),
'lido:appellationValue': lxpath(''),
'lido:resourceSet': lxpath('lido:resourceID')}
'''Valid Lido ID type URIs'''
LIDO_ID_TYPE_URIS = ('http://terminology.lido-schema.org/lido00099',
'http://terminology.lido-schema.org/identifier_type/uri', 'uri')
'''Valid Lido type attributes'''
LIDO_TYPE_ATTR = lidoExpandNS('lido:type')
XML_LANG_ATTR = '{{http://www.w3.org/XML/1998/namespace}}lang'
def notNone(*args) -> bool:
'''Tests all args to not None'''
return not any(x is None for x in args)
def executeS(fun, x, default=None):
'''Applies a function on a valid argument'''
return fun(x) if notNone(x) else default
def isValidType(elem: etree.Element) -> bool:
'''Tests for valid lido type attribute'''
return True
def getIDs(elem):
'''Returns all texts from valid Id elements'''
validItems = filter(isValidType, getIdElements(elem))
return list(map(lambda x: (x.text), validItems))
def getIdElements(elem):
'''Returns all ID child elements'''
tag = lidoCompressNS(elem.tag)
return executeS(lambda x: x.children(elem), LIDO_ID_MAP.get(tag), [])
def findVar(elem: etree.Element) -> str | None:
return executeS(lambda x: x.get('variable', ''), elem.find(RANGE_ENTT+"[@variable]"), '')
def str2bool(bstr) -> bool:
return bstr.lower() in ("yes", "true", "t", "1")
def skipped(elem: etree.Element) -> bool:
return str2bool(elem.get('skip', 'false'))
def fullLidoPath(elem):
'''Return the full lido path of an element'''
tags = [elem.tag.replace(f'{{{lidoSchemaURI}}}', '')]
if tags[0] != 'lido':
parent = elem.getparent()
if notNone(parent):
tags = fullLidoPath(parent) + tags
return tags
def getLidoInfo(elem, i):
if ids := getIDs(elem):
mode = 'lidoID'
id = ids[0]
else:
mode = 'path'
id = '/'.join(fullLidoPath(elem) + [str(i)])
return {'id': id, 'text': elem.text, 'mode': mode}
class ExP:
'''Linking a XML path to a entity label'''
def __init__(self, pathStr: str, typeStr: str, varStr: str = ''):
self.path: str = pathStr
self.entity = typeStr
self.var = varStr
def isRoot(self):
return self.path.startswith('//')
def toDict(self):
return {'path': self.path.strip('/'), 'entity': self.entity,
'var': self.var, 'isRoot': self.isRoot(), 'isLiteral': self.isLiteral()}
def __str__(self):
return f"{self.path}|{self.entity}"
def isLiteral(self):
return self.entity.startswith('http')
def classLabel(self):
return self.entity.split(':')[-1]
def sPath(self):
return self.path.replace('lido:', '')
def elements(self, root):
xpath = "." if self.isRoot() else f".//{self.path}"
return lidoXPath(root, xpath)
def stripPath(link: ExP, txt: str) -> str:
return txt.removeprefix(link.path)
class Condition():
def __init__(self):
self.access = ''
self.values = set()
def toDict(self):
if self.values:
return {'access': self.access, 'values': [str(x) for x in self.values]}
return {}
def add(self, path, value):
self.access = path
self.values.add(value)
def isValid(self, elem) -> bool:
if self.values:
if self.access.endswith('/text()'):
pathValue = lidoXPath(elem, f"./{self.access}")
if self.values.intersection(pathValue):
return True
else:
# assume path as an attribute label
attrName = lidoExpandNS(self.access)
attrValue = elem.get(attrName, '')
if attrValue in self.values:
return True
return False
return True
class PO():
def __init__(self, p: ExP, o: ExP):
self.P = p
self.O = o
self.intermediates = []
self.condition = Condition()
def toDict(self):
return {'P': self.P.toDict(), 'O': self.O.toDict(), 'condition': self.condition.toDict()}
def __str__(self):
return f"{self.P} -> {self.O}"
def isValid(self, elem):
return self.condition.isValid(elem)
def getData(self, root):
data = [getLidoInfo(elem, i)
for i, elem in enumerate(self.O.elements(root))]
return {'P': self.P.toDict(), 'O': self.O.toDict(), 'data': data, 'isValid': self.isValid(root)}
class Mapping:
def __init__(self, s: ExP, n=0):
self.S = s
self.n = n
self.condition = Condition()
self.POs = []
def isValid(self, elem):
return self.condition.isValid(elem)
def toDict(self):
return {'S': self.S.toDict(), 'POs': [po.toDict() for po in self.POs],
'condition': self.condition.toDict(), 'n': self.n}
def getSData(self, elem, i):
info = getLidoInfo(elem, i)
poData = [po.getData(elem) for po in self.POs]
return {'S': self.S.toDict(), 'info': info, 'PO': poData, 'valid': self.isValid(elem)}
def getData(self, root):
return [self.getSData(elemS, i) for i, elemS in enumerate(self.S.elements(root))]
def __str__(self):
poStr = '\n'.join([f"\t{x}" for x in self.POs])
return f"{self.S}:\n{poStr}"
def addPO(self, po: PO):
self.POs.append(po)
def addIntermediate(self, intermediate):
if intermediate:
self.intermediates.append(intermediate)
Mappings = list[Mapping]
def makeExP(pathElem: etree.Element, typeElem: etree.Element, varStr: str = '') -> ExP | None:
if notNone(pathElem, typeElem):
pathText = pathElem.text.strip()
typeText = typeElem.text.strip()
if typeText and pathText:
return ExP(pathText, typeText, varStr)
def mappingsFromNode(mappingElem) -> Mappings:
'''Reads single mappings from an x3ml mapping node'''
mappings = []
if sExP := makeExP(mappingElem.find(DOMAIN_PATH), mappingElem.find(DOMAIN_TYPE)):
mapping = Mapping(sExP)
# Find domain conditions
for cndElem in mappingElem.findall(DOMAIN_COND):
mapping.condition.add(stripPath(sExP, cndElem.text), cndElem.get('value'))
for linkElem in mappingElem.findall('./link'):
if not skipped(linkElem):
if pExP := makeExP(linkElem.find(PATH_SRR), linkElem.find(PATH_TRR)):
varStr = findVar(linkElem)
if oExP := makeExP(linkElem.find(RANGE_SN), linkElem.find(RANGE_TYPE), varStr):
po = PO(pExP, oExP)
for cndElem in linkElem.findall(PATH_TRE):
po.condition.add(stripPath(oExP, cndElem.text), cndElem.get('value'))
mapping.addPO(po)
mappings.append(mapping)
return mappings
def getMapping(fname: str) -> Mappings | None:
'''Returns all mappings from a file'''
mappings = []
for _, elem in etree.iterparse(fname, events=("end",),tag=('mapping'),encoding='UTF-8',remove_blank_text=True):
if not str2bool(elem.get('skip','false')):
mappings += mappingsFromNode(elem)
return mappings
if __name__ == "__main__":
args = sys.argv[1:]
mappings = args[0] if len(args) == 1 else "lido2rdf.x3ml"
for t in getMapping(mappings):
print(json.dumps(t.toDict(), indent=3))