Go to the documentation of this file.00001 from LogReader import LogReader
00002 import math
00003 import json
00004 import pickle
00005
00006
00007 class DataCondenser:
00008 def __init__(self):
00009 self.rdrLog = LogReader()
00010
00011 def condenseData(self, strPath):
00012 dataOwl = None
00013
00014 log = self.rdrLog.loadLog(strPath)
00015 dataOwl = log.getOwlData()
00016
00017 self.tti = dataOwl["task-tree-individuals"]
00018 owlMeta = dataOwl["metadata"]
00019 owlAnnot = dataOwl["annotation"]
00020
00021 if owlMeta:
00022 result = {"Toplevel" : self.condenseNodes("", owlMeta.subActions())};
00023
00024 with open("out.json", "wb") as f:
00025 json.dump(result, f)
00026
00027 with open("generalized_model.pkl", "wb") as f:
00028 pickle.dump({"model" : result,
00029 "parameters" : owlAnnot.annotatedParameterTypes()},
00030 f, pickle.HIGHEST_PROTOCOL)
00031 else:
00032 print "No meta data in file!"
00033
00034 def condenseNodes(self, strParentNode, arrNodes, nLevel = 0):
00035 arrTypes = {}
00036 arrIndividuals = {}
00037
00038 for strNode in arrNodes:
00039 owlNode = self.tti[strNode]
00040 ident = owlNode.taskContext()
00041
00042 failures = owlNode.failures()
00043 failure = ""
00044 if len(failures) > 0:
00045 failure = self.tti[failures[0]].type()
00046
00047 result = self.condenseNodes(strNode, owlNode.subActions(), nLevel + 1)
00048 if not ident in arrTypes:
00049 arrTypes[ident] = result
00050 else:
00051 arrTypes[ident] = self.unifyResults(arrTypes[ident], result)
00052
00053 arrTypes[ident]["individuals"][strNode] = {"parameters" : owlNode.annotatedParameters(True),
00054 "parent" : strParentNode,
00055 "failure" : failure}
00056
00057 return {"subTypes" : arrTypes,
00058 "individuals" : {}}
00059
00060 def unifyResults(self, res1, res2):
00061 resparams = {}
00062 if len(res1["individuals"]) > 0:
00063 resparams = res1["individuals"]
00064
00065 if len(res2["individuals"]) > 0:
00066 resparams = dict(resparams.items() + res2["individuals"].items())
00067
00068 unified = {"subTypes" : {},
00069 "individuals" : resparams}
00070
00071 for ressub1 in res1["subTypes"]:
00072 if ressub1 in res2["subTypes"]:
00073 unified["subTypes"][ressub1] = self.unifyResults(res1["subTypes"][ressub1],
00074 res2["subTypes"][ressub1])
00075 else:
00076 unified["subTypes"][ressub1] = res1["subTypes"][ressub1]
00077
00078 for ressub2 in res2["subTypes"]:
00079 if not ressub2 in res1["subTypes"]:
00080 unified["subTypes"][ressub2] = res2["subTypes"][ressub2]
00081
00082 return unified