forked from GRIDAPPSD/Powergrid-Models
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddUsagePointsandEndDevices.py
More file actions
374 lines (325 loc) · 13 KB
/
Copy pathaddUsagePointsandEndDevices.py
File metadata and controls
374 lines (325 loc) · 13 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import uuid
from Meas import constants
from SPARQLWrapper import SPARQLWrapper2
import xml.etree.ElementTree as ET
from lxml import etree, objectify
from xml.etree.ElementTree import Comment
from xml.dom import minidom
import sys
from os import path
'''
since I query from outside the docker container
'''
blazegraph_url = "http://localhost:8889/bigdata/sparql"
usagePoints = {}
endDevices = {}
equipments = {}
usagePendDDict = {}
usedUsagePoints = {}
usedEndDevices = {}
usedEquipments = {}
usedUsagePendDDict = {}
class UsagePoint(object):
'''
class represent a usage point
'''
def __init__(self, name=None, mrid=None):
self.name = name
if mrid:
self.mrid = mrid
else:
self.mrid = uuid.uuid4()
class EndDevice(object):
'''
class represent an end device
'''
def __init__(self, name=None, mrid=None, isSmartInverter=None, usagePoint=None, upID=None):
self.name = name
if mrid:
self.mrid = mrid
else:
self.mrid = uuid.uuid4()
self.isSmartInverter = isSmartInverter
self.usagePoint = usagePoint
self.upID = upID
class Equipment(object):
'''
class represent a power electronics connection
'''
def __init__(self, mrid=None, usagePoint=None, upID=None, tp=None):
if mrid:
self.mrid = mrid
else:
self.mrid = uuid.uuid4()
self.usagePoint = usagePoint
self.upID = upID
self.type = tp
def getPowerElectronicsConnection(sparql):
'''
retrieve all power electronics connection from the blazegraph
:param sparql:
:return:
'''
query = \
(constants.prefix +
"SELECT ?pec ?name ?mrid WHERE {"
"?pec r:type c:PowerElectronicsConnection ."
"?pec c:IdentifiedObject.name ?name ."
"?pec c:IdentifiedObject.mRID ?mrid ."
"}"
)
# Set and execute the query.
sparql.setQuery(query)
return sparql.query()
def getSynchronousMachine(sparql):
'''
retrieve all synchronous machine from the blazegraph
:param sparql:
:return:
'''
query = \
(constants.prefix +
"SELECT ?syncMachine ?name ?mrid WHERE {"
"?syncMachine r:type c:SynchronousMachine ."
"?syncMachine c:IdentifiedObject.name ?name ."
"?syncMachine c:IdentifiedObject.mRID ?mrid ."
"}"
)
# Set and execute the query.
sparql.setQuery(query)
return sparql.query()
def insertUPsAndEDs(pecs, machines):
'''
loop through each power electronics connection and synchronous machine to generate RDF triples for the blazegraph
:param pecs:
:param machines:
:return:
'''
for pec in pecs.bindings:
# print(pec['pec'].value)
# a = pec['name'].value
# print(a)
eqid = pec['mrid'].value
# print(b)
if eqid in equipments:
usage = equipments[eqid].usagePoint
usedUsagePoints[usage.mrid] = usage
usedEquipments[eqid] = equipments[eqid]
if usage in usagePendDDict:
enddevice = usagePendDDict[usage]
usedEndDevices[enddevice.mrid] = enddevice
usedUsagePendDDict[usage] = enddevice
else:
usage = UsagePoint(name='usagePoint_' + pec['name'].value)
usedUsagePoints[usage.mrid] = usage
usedEquipments[eqid] = Equipment(mrid=eqid, usagePoint=usage, upID=usage.mrid, tp="pec")
enddevice = EndDevice(name='endDevice_' + pec['name'].value)
enddevice.isSmartInverter = True
usedEndDevices[enddevice.mrid] = enddevice
usedUsagePendDDict[usage] = enddevice
insertUPandED(pec['pec'].value, usage, enddevice)
for machine in machines.bindings:
eqid = machine['mrid'].value
# print(machine['syncMachine'].value)
if eqid in equipments:
usage = equipments[eqid].usagePoint
usedUsagePoints[usage.mrid] = usage
usedEquipments[eqid] = equipments[eqid]
if usage in usagePendDDict:
enddevice = usagePendDDict[usage]
usedEndDevices[enddevice.mrid] = enddevice
usedUsagePendDDict[usage] = enddevice
else:
usage = UsagePoint(name='usagePoint_' + machine['name'].value)
usedUsagePoints[usage.mrid] = usage
usedEquipments[eqid] = Equipment(mrid=eqid, usagePoint=usage, upID=usage.mrid, tp="syn")
enddevice = EndDevice(name='endDevice_' + machine['name'].value)
enddevice.isSmartInverter = False
usedEndDevices[enddevice.mrid] = enddevice
usedUsagePendDDict[usage] = enddevice
insertUPandED(machine['syncMachine'].value, usage, enddevice)
def insertUPandED(equipment, usagePoint, enddevice):
'''
insert RDF triples for each equipment
:param equipment: the url of the equipment in the blazegraph
:param usagePoint: UsagePoint object related to this equipment
:param enddevice: EndDevice object related to this equipment
:return:
'''
q = (constants.prefix + 'INSERT DATA { ')
upmRIDStr = str(usagePoint.mrid)
if upmRIDStr[0] != '_':
upmRIDStr = '_' + upmRIDStr
edmRIDStr = str(enddevice.mrid)
if edmRIDStr[0] != '_':
edmRIDStr = '_' + edmRIDStr
equipment = '<' + equipment + '>'
uPoint = '<' + blazegraph_url + '#' + upmRIDStr + '>'
eDevice = '<' + blazegraph_url + '#' + edmRIDStr + '>'
# usage point property triples
q += \
uPoint + ' a c:UsagePoint . ' \
+ uPoint + ' c:IdentifiedObject.mRID \"' + upmRIDStr + '\" . ' \
+ uPoint + ' c:IdentifiedObject.name \"' + usagePoint.name + '\" . '
# end device property triples
q += \
eDevice + ' a c:EndDevice . ' \
+ eDevice + ' c:IdentifiedObject.mRID \"' + edmRIDStr + '\" . ' \
+ eDevice + ' c:IdentifiedObject.name \"' + enddevice.name + '\" . ' \
+ eDevice + ' c:EndDevice.isSmartInverter \"' + str(enddevice.isSmartInverter) + '\" . ' \
+ eDevice + ' c:EndDevice.UsagePoint ' + uPoint + ' . ' \
# equipment property triples
q += equipment + ' c:Equipment.UsagePoint ' + uPoint + ' .'
# Update query
q += '}'
# Make update in triplestore.
sparql.setQuery(q)
sparql.method = 'POST'
ret = sparql.query()
print(ret)
def exportCIMXML(pecs, machines):
'''generate xml files that can be uploaded to the blazegraph via blazegraph workbench
loop through each equipment: i.e., power electronics connection and synchronous machine
create EndDevice and UsagePoint object for each equipment, call addAEquipmentToCIMXML to generate the actual xml element
command in terminal is:
curl -s -D- -H 'Content-Type: application/xml' --upload-file "path/to/endDevicesAndUsagePoints.xml" -X POST "http://localhost:8889/bigdata/sparql"
:param pecs:
:param machines:
:return:
'''
root = ET.Element('rdf:RDF', attrib={'xmlns:cim': 'http://iec.ch/TC57/CIM100#', 'xmlns:rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'})
# # comment = Comment('un-comment this line to enable validation\n')
# # root.append(comment)
#
# for pec in pecs.bindings:
# print(pec['pec'].value)
# usage = UsagePoint(name='usagePoint_' + pec['name'].value)
# enddevice = EndDevice(name='endDevice_' + pec['name'].value)
# enddevice.isSmartInverter = True
# addAEquipmentToCIMXML("cim:PowerElectronicsConnection", pec['mrid'].value, usage, enddevice, root)
# for machine in machines.bindings:
# print(machine['syncMachine'].value)
# usage = UsagePoint(name='usagePoint_' + machine['name'].value)
# enddevice = EndDevice(name='endDevice_' + machine['name'].value)
# enddevice.isSmartInverter = False
# addAEquipmentToCIMXML("cim:SynchronousMachine", machine['mrid'].value, usage, enddevice, root)
for eqID, equipment in usedEquipments.items():
usage = equipment.usagePoint
enddevice = usedUsagePendDDict[usage]
if equipment.type == "pec":
addAEquipmentToCIMXML("cim:PowerElectronicsConnection", eqID, usage, enddevice, root)
if equipment.type == "syn":
addAEquipmentToCIMXML("cim:SynchronousMachine", eqID, usage, enddevice, root)
rough_string = ET.tostring(root, 'utf-8')
reparsed = minidom.parseString(rough_string)
with open("endDevicesAndUsagePoints.xml", "w") as f:
f.write(reparsed.toprettyxml(indent=" "))
def addAEquipmentToCIMXML(elementName, equipmentID, usagePoint, enddevice, root):
'''
generate xml elements for each equipment
:param elementName: the name of the xml element that represent the equipment object in xml,
either cim:PowerElectronicsConnection for power electronics connection
or cim:SynchronousMachine for synchronous machine,
:param equipmentID: the mRID of the equipment,
:param usagePoint: the UsagePoint object related to this equipment
:param enddevice: the EndDevice object related to this equipment
:param root: the root of the xml element
:return:
'''
upmRIDStr = str(usagePoint.mrid)
if upmRIDStr[0] != '_':
upmRIDStr = '_' + upmRIDStr
edmRIDStr = str(enddevice.mrid)
if edmRIDStr[0] != '_':
edmRIDStr = '_' + edmRIDStr
thisUsagePoint = ET.SubElement(root, 'cim:UsagePoint', attrib={'rdf:ID': upmRIDStr})
ET.SubElement(thisUsagePoint, "cim:IdentifiedObject.mRID").text = upmRIDStr
ET.SubElement(thisUsagePoint, "cim:IdentifiedObject.name").text = usagePoint.name
thisEndDevice = ET.SubElement(root, 'cim:EndDevice', attrib={'rdf:ID': edmRIDStr})
ET.SubElement(thisEndDevice, "cim:IdentifiedObject.mRID").text = edmRIDStr
ET.SubElement(thisEndDevice, "cim:IdentifiedObject.name").text = enddevice.name
ET.SubElement(thisEndDevice, "cim:EndDevice.isSmartInverter").text = str(enddevice.isSmartInverter)
ET.SubElement(thisEndDevice, "cim:EndDevice.UsagePoint", attrib={'rdf:resource': "#" + upmRIDStr})
thisEquipment = ET.SubElement(root, elementName, attrib={'rdf:ID': equipmentID})
ET.SubElement(thisEquipment, "cim:Equipment.UsagePoint", attrib={'rdf:resource': "#" + upmRIDStr})
def usage():
print("usage: python addUsagePointsandEndDevices.py <input xml file full path>")
print("input xml file should have usage point with end device.")
def parseUsagePoint(e):
for se in e:
st = str(se.tag).split('}')
if len(st) == 2 and st[1] == "IdentifiedObject.name":
name = se.text
if len(st) == 2 and st[1] == "IdentifiedObject.mRID":
mrid = se.text
return UsagePoint(name=name, mrid=mrid)
def parseEndDevice(e):
for se in e:
st = str(se.tag).split('}')
if len(st) == 2 and st[1] == "IdentifiedObject.name":
name = se.text
if len(st) == 2 and st[1] == "IdentifiedObject.mRID":
mrid = se.text
if len(st) == 2 and st[1] == "EndDevice.isSmartInverter":
isSmart = se.text
if len(st) == 2 and st[1] == "EndDevice.UsagePoint":
up = get_id(se.attrib)
return EndDevice(name=name, mrid=mrid, isSmartInverter=isSmart, upID=up)
def parseEquipment(e):
for se in e:
st = str(se.tag).split('}')
if len(st) == 2 and st[1] == "Equipment.UsagePoint":
return get_id(se.attrib)
return None
def readxmlFile(f):
tree = ET.parse(f)
root = tree.getroot()
for e in root:
t = str(e.tag).split('}')
if len(t) == 2:
if t[1] == "UsagePoint":
node = parseUsagePoint(e)
usagePoints[node.mrid] = node
elif t[1] == "EndDevice":
node = parseEndDevice(e)
endDevices[node.mrid] = node
elif t[1] == "SynchronousMachine":
id = get_id(e.attrib)
eq = Equipment(mrid=id, tp="syn")
eq.upID = parseEquipment(e)
equipments[eq.mrid] = eq
elif t[1] == "PowerElectronicsConnection":
id = get_id(e.attrib)
eq = Equipment(mrid=id, tp="pec")
eq.upID = parseEquipment(e)
equipments[eq.mrid] = eq
else:
pass
for k, v in endDevices.items():
if v.upID in usagePoints:
v.usagePoint = usagePoints[v.upID]
usagePendDDict[v.usagePoint] = v
for k, v in equipments.items():
if v.upID in usagePoints:
v.usagePoint = usagePoints[v.upID]
def get_id(attrib):
for k, v in attrib.items():
if v.startswith("#"):
return v[1:]
elif v.startswith("_"):
return v
return None
if __name__ == "__main__":
if len(sys.argv) != 2:
usage()
sys.exit()
inputFile = sys.argv[1]
if path.isfile(inputFile):
readxmlFile(inputFile)
sparql = SPARQLWrapper2(blazegraph_url)
pecs = getPowerElectronicsConnection(sparql)
machines = getSynchronousMachine(sparql)
print(pecs)
print(machines)
insertUPsAndEDs(pecs, machines)
exportCIMXML(pecs, machines)