2023-01-16 21:43:52 +00:00
|
|
|
#!/bin/python3
|
|
|
|
|
2023-01-25 20:51:59 +00:00
|
|
|
import os
|
2023-04-13 21:28:40 +00:00
|
|
|
import bisect
|
2023-01-25 20:51:59 +00:00
|
|
|
from xml.dom import minidom
|
|
|
|
from dicttoxml import dicttoxml
|
2023-01-16 21:43:52 +00:00
|
|
|
|
|
|
|
class Result:
|
2023-02-15 14:06:42 +00:00
|
|
|
"""This class stores and process/store the results of a simulation."""
|
2023-01-16 21:43:52 +00:00
|
|
|
|
2023-03-27 13:29:39 +00:00
|
|
|
def __init__(self, shape, execID):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It initializes the instance with a specific shape."""
|
2023-01-25 20:51:59 +00:00
|
|
|
self.shape = shape
|
2023-03-27 13:29:39 +00:00
|
|
|
self.execID = execID
|
2023-01-16 21:43:52 +00:00
|
|
|
self.blockAvailable = -1
|
2023-01-25 20:51:59 +00:00
|
|
|
self.tta = -1
|
2023-01-16 21:43:52 +00:00
|
|
|
self.missingVector = []
|
2023-03-21 07:34:28 +00:00
|
|
|
self.metrics = {}
|
2024-01-20 19:57:17 +00:00
|
|
|
self.amImalicious = [0] * shape.numberNodes
|
|
|
|
self.msgSentCount = [0] * shape.numberNodes
|
|
|
|
self.msgRecvCount = [0] * shape.numberNodes
|
|
|
|
self.sampleRecvCount = [0] * shape.numberNodes
|
2024-01-29 14:20:01 +00:00
|
|
|
self.restoreRowCount = [0] * shape.numberNodes
|
|
|
|
self.restoreColumnCount = [0] * shape.numberNodes
|
2024-02-19 22:20:53 +00:00
|
|
|
self.repairedSampleCount = [0] * shape.numberNodes
|
2024-02-21 11:26:38 +00:00
|
|
|
self.numberNodes = shape.numberNodes
|
|
|
|
self.class1ratio = shape.class1ratio
|
2024-01-20 19:57:17 +00:00
|
|
|
|
|
|
|
def copyValidators(self, validators):
|
|
|
|
"""Copy information from simulator.validators to result."""
|
|
|
|
for i in range(0,self.shape.numberNodes):
|
|
|
|
self.amImalicious[i] = validators[i].amImalicious
|
|
|
|
self.msgSentCount[i] = validators[i].msgSentCount
|
|
|
|
self.msgRecvCount[i] = validators[i].msgRecvCount
|
|
|
|
self.sampleRecvCount[i] = validators[i].sampleRecvCount
|
2024-01-29 14:20:01 +00:00
|
|
|
self.restoreRowCount[i] = validators[i].restoreRowCount
|
|
|
|
self.restoreColumnCount[i] = validators[i].restoreColumnCount
|
2024-02-19 22:20:53 +00:00
|
|
|
self.repairedSampleCount[i] = validators[i].repairedSampleCount
|
2023-01-16 21:43:52 +00:00
|
|
|
|
2023-03-29 15:01:28 +00:00
|
|
|
def populate(self, shape, config, missingVector):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It populates part of the result data inside a vector."""
|
2023-01-25 20:51:59 +00:00
|
|
|
self.shape = shape
|
2023-01-16 21:43:52 +00:00
|
|
|
self.missingVector = missingVector
|
2023-04-13 21:28:40 +00:00
|
|
|
v = self.metrics["progress"]["validators ready"]
|
|
|
|
tta = bisect.bisect(v, config.successCondition)
|
2023-04-17 08:56:10 +00:00
|
|
|
if v[-1] >= config.successCondition:
|
2023-01-25 20:51:59 +00:00
|
|
|
self.blockAvailable = 1
|
2023-04-13 21:28:40 +00:00
|
|
|
self.tta = tta * (config.stepDuration)
|
2023-01-25 20:51:59 +00:00
|
|
|
else:
|
|
|
|
self.blockAvailable = 0
|
|
|
|
self.tta = -1
|
|
|
|
|
2023-03-21 07:34:28 +00:00
|
|
|
def addMetric(self, name, metric):
|
2023-03-29 22:01:06 +00:00
|
|
|
"""Generic function to add a metric to the results."""
|
2023-03-21 07:34:28 +00:00
|
|
|
self.metrics[name] = metric
|
|
|
|
|
2023-03-27 13:29:39 +00:00
|
|
|
def dump(self):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It dumps the results of the simulation in an XML file."""
|
2023-01-25 20:51:59 +00:00
|
|
|
if not os.path.exists("results"):
|
|
|
|
os.makedirs("results")
|
2023-03-27 13:29:39 +00:00
|
|
|
if not os.path.exists("results/"+self.execID):
|
|
|
|
os.makedirs("results/"+self.execID)
|
2023-01-25 20:51:59 +00:00
|
|
|
resd1 = self.shape.__dict__
|
|
|
|
resd2 = self.__dict__.copy()
|
|
|
|
resd2.pop("shape")
|
|
|
|
resd1.update(resd2)
|
|
|
|
resXml = dicttoxml(resd1)
|
|
|
|
xmlstr = minidom.parseString(resXml)
|
|
|
|
xmlPretty = xmlstr.toprettyxml()
|
2023-03-27 13:29:39 +00:00
|
|
|
filePath = "results/"+self.execID+"/"+str(self.shape)+".xml"
|
2023-01-25 20:51:59 +00:00
|
|
|
with open(filePath, "w") as f:
|
|
|
|
f.write(xmlPretty)
|