-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmapping_parser.py
225 lines (204 loc) · 7.52 KB
/
mapping_parser.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
import sys
import json
from collections import defaultdict
from rdflib import Graph
import os
triples = defaultdict(lambda: defaultdict(list))
class_dict = defaultdict()
constant_dict = defaultdict()
datatype_dict = defaultdict()
iterator_dict = defaultdict()
logicalSource_dict = defaultdict()
objectMap_dict = defaultdict()
parentTriplesMap_dict = defaultdict()
predicate_dict = defaultdict()
predicateObjectMap_dict = defaultdict(list)
reference_dict = defaultdict()
referenceFormulation_dict = defaultdict()
source_dict = defaultdict()
subjectMap_dict = defaultdict()
templateMap_dict = defaultdict()
joinCondition_dict = defaultdict()
child_dict = defaultdict()
parent_dict = defaultdict()
source_server_dict = defaultdict()
server_schema_dict = defaultdict()
server_username_dict = defaultdict()
server_password_dict = defaultdict()
#rr:tableName "review";
table_name_dict = defaultdict()
query_dict = defaultdict()
def read_file(file_name):
content = ''
with open(file_name) as f:
content = f.read()
return content
def remove_prefix(s):
if 'http' in s:
if '#' in s:
return s.split('#')[1]
else:
return s.split('/')[-1]
else:
return s
def parse_mapping(mapping_file='venue-mapper.ttl', mapping_format='n3'):
mapping_graph = Graph()
#file = open('all_triples.txt', 'w')
mapping_graph.parse(mapping_file, format=mapping_format)
for subj, pred, obj in mapping_graph:
subj = remove_prefix(subj.toPython())
pred = remove_prefix(pred.toPython())
# obj = remove_prefix(obj.toPython())
triples[pred][subj].append(obj)
#file.writelines('{} {} {}\n'.format(subj, pred, obj))
if pred == 'class':
obj = remove_prefix(obj.toPython())
class_dict[subj] = obj
if pred == 'constant':
obj = remove_prefix(obj.toPython())
constant_dict[subj] = obj
if pred == 'datatype':
obj = remove_prefix(obj.toPython())
datatype_dict[subj] = obj
if pred == 'iterator':
obj = remove_prefix(obj.toPython())
iterator_dict[subj] = obj
if pred == 'logicalSource':
obj = remove_prefix(obj.toPython())
logicalSource_dict[subj] = obj
if pred == 'tableName':
obj = remove_prefix(obj.toPython())
table_name_dict[subj] = obj
if pred == 'objectMap':
obj = remove_prefix(obj.toPython())
objectMap_dict[subj] = obj
if pred == 'parentTriplesMap':
obj = remove_prefix(obj.toPython())
parentTriplesMap_dict[subj] = obj
if pred == 'predicate':
obj = remove_prefix(obj.toPython())
predicate_dict[subj] = obj
if pred == 'predicateObjectMap':
obj = remove_prefix(obj.toPython())
predicateObjectMap_dict[subj].append(obj)
if pred == 'reference' or pred == 'column':
obj = remove_prefix(obj.toPython())
reference_dict[subj] = obj
if pred == 'referenceFormulation':
obj = remove_prefix(obj.toPython())
referenceFormulation_dict[subj] = obj
if pred == 'source':
source_dict[subj] = obj.toPython()
if pred == 'subjectMap':
obj = remove_prefix(obj.toPython())
subjectMap_dict[subj] = obj
if pred == 'template':
# obj = remove_prefix(obj.toPython())
templateMap_dict[subj] = obj.toPython()
if pred == 'joinCondition':
obj = remove_prefix(obj.toPython())
joinCondition_dict[subj] = obj
if pred == 'child':
obj = remove_prefix(obj.toPython())
child_dict[subj] = obj
if pred == 'parent':
obj = remove_prefix(obj.toPython())
parent_dict[subj] = obj
if pred == 'server':
source_server_dict[subj] = obj.toPython()
if pred == 'username':
server_username_dict[subj] = obj.toPython()
if pred == 'password':
server_password_dict[subj] = obj.toPython()
if pred == 'jdbcDSN':
server_schema_dict[subj] = obj.toPython()
if pred == 'sqlQuery':
query_dict[subj] = obj.toPython()
return mapping_graph
def generateSourceList():
return 0
def getDBSourceList():
db_source_lst = []
for (key, value) in server_schema_dict.items():
source_rec = {'name': key, 'server': value, 'username': server_username_dict[key], 'password': server_password_dict[key]}
db_source_lst.append(source_rec)
return db_source_lst
def generateLogicalSourceList():
logical_source_lst = []
iterator_str = ''
for (key, value) in source_dict.items():
if key in iterator_dict.keys():
iterator_str = iterator_dict[key]
if remove_prefix(value) in server_schema_dict.keys():
table_name = ''
query = ''
if key in table_name_dict.keys():
table_name = table_name_dict[key]
if key in query_dict.keys():
query = query_dict[key]
ls_record = {'name': key, 'source': remove_prefix(value), 'referenceFormulation': 'ql:' + referenceFormulation_dict[key], 'table': table_name, 'query': query, 'iterator': iterator_str}
else:
ls_record = {'name': key, 'source': value, 'referenceFormulation': 'ql:' + referenceFormulation_dict[key], 'iterator': iterator_str}
logical_source_lst.append(ls_record)
return logical_source_lst
def generateMappingList():
mappings_lst = []
for (key, value) in logicalSource_dict.items():
mapping_dict = dict()
# render 'name' and 'logicalSource' fields
mapping_dict['name'] = key
mapping_dict['logicalSource'] = value
# render 'subjectMap' field
subject_map_key = subjectMap_dict[key]
template = templateMap_dict[subject_map_key]
class_of = class_dict[subject_map_key]
poms_lst = []
for pom_anonymous_name in predicateObjectMap_dict[key]:
pom_dict = dict()
print(key, pom_anonymous_name)
pom_dict['predicate'] = predicate_dict[pom_anonymous_name]
om_anonymous_name = objectMap_dict[pom_anonymous_name]
if om_anonymous_name in reference_dict.keys():
reference_field = reference_dict[om_anonymous_name]
data_type = ''
if om_anonymous_name in datatype_dict.keys():
data_type = datatype_dict[om_anonymous_name]
pom_dict['objectMap'] = {'reference': reference_field, 'datatype': data_type}
if om_anonymous_name in constant_dict.keys():
# this if block is not yet tested
constant_value = constant_dict[om_anonymous_name]
#data_type = ''
#if om_anonymous_name in datatype_dict.keys():
data_type = datatype_dict[om_anonymous_name]
pom_dict['objectMap'] = {'constant': constant_value, 'datatype': data_type}
if om_anonymous_name in parentTriplesMap_dict.keys():
parent_mapping_name = parentTriplesMap_dict[om_anonymous_name]
jc_anonymous_name = joinCondition_dict[om_anonymous_name]
child_filed = child_dict[jc_anonymous_name]
parent_field = parent_dict[jc_anonymous_name]
pom_dict['objectMap'] = {'parentTriplesMap': parent_mapping_name, 'joinCondition': {'child': child_filed, 'parent': parent_field}}
poms_lst.append(pom_dict)
mapping_dict['subjectMap'] = {'template': template, 'class': class_of}
mapping_dict['predicateObjectMap'] = poms_lst
mappings_lst.append(mapping_dict)
return mappings_lst
def write_json(mapping, output_file_name = 'mappings.json'):
file_name = './' + output_file_name
with open(file_name, 'w') as fp:
json.dump(mapping, fp)
if __name__ == '__main__':
g = parse_mapping(str(sys.argv[1]))
mapping_file_name = global_ontolopy_name = str(sys.argv[1]).split('/')[-1].split('.')[0]
folder_name = os.path.basename(os.getcwd())
if folder_name == 'mapping_parser':
output_file_name = '{file_name}.json'.format(file_name=mapping_file_name)
else:
output_file_name = './mapping_parser/{file_name}.json'.format(file_name=mapping_file_name)
logicalSource_lst = generateLogicalSourceList()
#print(logicalSource_dict)
print(predicateObjectMap_dict)
print(predicate_dict)
mappings = generateMappingList()
db_source = getDBSourceList()
rml_mapping = {'DBSources': db_source, 'LogicalSources': logicalSource_lst, 'Mappings': mappings}
write_json(rml_mapping, output_file_name)