Exemplo n.º 1
0
class IcsSymbolToTree:
    def __init__(self):
        self.wTInfer = WholeFieldTypeInfer()

    def icsSymToTree(self, gFormat, cFormats, h=10):
        gNodeFirst, gNodeLast = self.transLineToNodes(gFormat[0:len(gFormat)-1])
        for cFormat in cFormats:
            tFuncNode = node()
            tType = 'F' + ',' + str(len(cFormat)) + ',' + str(cFormat)
            tFuncNode.value.append(tType)
            tCformat,_ = self.transLineToNodes(cFormats[cFormat], h=3)
            tFuncNode.children.append(tCformat)
            gNodeLast.children.append(tFuncNode)
        return gNodeFirst

    def transLineToNodes(self, words, h=10):
        nodes = []
        for word in words:
            wType = self.wTInfer.cVertNumToName(word[1])
            sNode = ''
            if wType == 'Payload':
                sNode = sNode + wType + ',' + '-1'
            else:
                sNode = sNode + wType + ',' + str(word[0][1] - word[0][0])
            tNode = node()
            tNode.value.append(sNode)
            nodes.append(tNode)
        t_len = min(len(words), h)
        i = t_len - 2
        while(i >= 0):
            nodes[i].children.append(nodes[i+1])
            i = i - 1
        return nodes[0], nodes[t_len-1]
Exemplo n.º 2
0
class FormatGeneLogic:
    def __init__(self, messages=None):
        self.messages = messages
        self.wordTypeInfer = WholeFieldTypeInfer(self.messages)
        self.cvter = Converter()
        self.wcvter = word_convert()
        self.msgSplt = MsgSpliter()
        self.dataTuning = DataTuning()
        self.icsSymTree = IcsSymbolToTree()

    def getRanges(self, messages):
        L_len = 65536
        for message in messages:
            if len(message) < L_len:
                L_len = len(message)
        return min(23, L_len + 2)

    def getMesFormat(self):
        pass

    def sortWordsType(self, words):
        words = sorted(words.items(), key=lambda x: x[0][0])
        return words

    def getGFormat(self, congigParas, gVeparas):
        gVoterLogic = GvoterLogic()
        boundaries = gVoterLogic.getSplitMessages(congigParas,
                                                  gVeparas,
                                                  self.messages,
                                                  FType='G')
        boundaries = self.cvter.border2item(boundaries)
        fRange = self.getRanges(self.messages)
        LoRdj = ReAjustLogic(boundaries, self.messages)
        LoRdj.reSplit()
        LoRdj.reCluster()
        boundaries = LoRdj.words
        wordsType = self.wordTypeInfer.extractWords(boundaries, fRange)
        wordsType = self.sortWordsType(wordsType)
        boundaries = self.wcvter.itemtoborder(boundaries)
        return boundaries, wordsType

    def getCFormat(self, configParas, gVeparas, msgs):
        if len(msgs) < 10:
            return [((0, -1), 7)]
        gVoterLogic = GvoterLogic()
        boundaries = gVoterLogic.getSplitMessages(configParas,
                                                  gVeparas,
                                                  msgs,
                                                  FType='C')
        boundaries = self.cvter.border2item(boundaries)
        #print('ss')
        #print(len(msgs))
        #print(msgs[0])
        #print(boundaries)
        #print('ee')
        fRange = self.getRanges(msgs)
        boundaries = self.cvter.filterB(boundaries, fRange)
        LoRdj = ReAjustLogic(boundaries, msgs)
        LoRdj.reSplit()
        LoRdj.reCluster()
        boundaries = LoRdj.words
        cWordTypeInfer = WholeFieldTypeInfer(msgs)
        wordsType = cWordTypeInfer.extractCWords(boundaries)
        wordsType = self.sortWordsType(wordsType)
        return wordsType

    def clsByFunc(self, los):
        tCls = {}
        for msg in self.messages:
            tFunc = msg[los[0]:los[1]]
            if tFunc not in tCls:
                tCls[tFunc] = []
            tCls[tFunc].append(msg[los[1]:])
        return tCls

    def GTreeGenerate(self, configParas, gVeparas):
        _, wordsInfer = self.getGFormat(configParas, gVeparas)
        fcCode = None
        for word in wordsInfer:
            if word[1] == 0:
                fcCode = word[0]
                break
        tFunMsgs = self.clsByFunc(fcCode)
        for fcKey in tFunMsgs:
            tFunMsgs[fcKey] = self.getCFormat(configParas, gVeparas,
                                              tFunMsgs[fcKey])
        return wordsInfer, tFunMsgs
        #print(tFunMsgs[fcKey])

    def GTJsonTree(self, configParas, gVeparas):
        gFormat, cFormats = self.GTreeGenerate(configParas, gVeparas)
        print(gFormat)
        groot = self.icsSymTree.icsSymToTree(gFormat, cFormats)
        return groot.transToIcsDictTree()

    def changeFormat(self, boundaries, wordsType):
        boundaries = [boundaries for i in range(len(self.messages))]
        gForMsg = self.msgSplt.splitMsgByTypes(boundaries, self.messages)
        wordTHeaders = []
        for wordType in wordsType:
            wordTHeaders.append(self.wordTypeInfer.cVertNumToName(wordType[1]))
        return wordTHeaders, gForMsg

    def getGJson(self, congigParas, gVeparas):
        boundaries, wType = self.getGFormat(congigParas, gVeparas)
        return self.changeFormat(boundaries, wType)
        #print(boundaries)

    def getGF(self, uId=' '):
        # future
        uConfig = UserConfig('/home/wxw/data/ToolDatas/15895903730.10.222',
                             '15895903730')
        gVeParas = GveConf.geneGveParas()
        return self.getGJson(uConfig, gVeParas)

    def combineFormats(self):
        pass

    def clsMessages(self):
        pass