Exemple #1
0
def generateGraph(jsonObject, strFatherLabel, arrCodes, idChange, isBlueColor,
                  graph):
    startLine = jsonObject['startLine']
    startOffset = jsonObject['startOffset']
    endLine = jsonObject['endLine']
    endOffset = jsonObject['endOffset']
    strPosition = '{}-{}-{}-{}'.format(startLine, startOffset, endLine,
                                       endOffset)
    strId = str(jsonObject['id'])
    strLabel = '{}\n{}\n{}'.format(strId, strPosition, jsonObject['type'])
    # strLabel = '{}\n{}'.format(strId, jsonObject['type'])

    if isBlueColor and strId != idChange:
        graph.add_node(strLabel, color='blue')
    else:
        graph.add_node(strLabel, color='red')

    if 'children' in jsonObject.keys():
        lstChildren = jsonObject['children']
        for i in range(0, len(lstChildren)):
            if strId == idChange:
                # print('id and idchange {} {}'.format(strId, idChange))
                isBlueColor = False
            strChildLabel = generateGraph(lstChildren[i], strLabel, arrCodes,
                                          idChange, isBlueColor, graph)
            if strLabel != strChildLabel:
                graph.add_edge(strLabel, strChildLabel, color='black')
    else:
        strTerminalLabel = '{}\n{}'.format(
            strPosition,
            getTerminalValue(startLine, startOffset, endLine, endOffset,
                             arrCodes))
        graph.add_node(strTerminalLabel, color='yellow')
        graph.add_edge(strLabel, strTerminalLabel, color='black')

    return strLabel
Exemple #2
0
def generateGraphForMixCode(jsonObject, arrCodes, strRootProgramId,
                            isInNLNodes, dictGraphIndexContext,
                            dictAcceptableIdsForVersions):
    strValue = 'EMPTY'
    isAbstract = False
    strType = 'ASTNode'
    strLabelInClassification = ''
    strId = '-1'

    if 'isNLRootNode' in jsonObject.keys():
        isInNLNodes = True

    if not isInNLNodes:
        startLine = jsonObject['startLine']
        startOffset = jsonObject['startOffset']
        endLine = jsonObject['endLine']
        endOffset = jsonObject['endOffset']
        strId = str(jsonObject['id'])
        strPosition = '{}-{}-{}-{}'.format(startLine, startOffset, endLine,
                                           endOffset)

        if strId == '1':
            strType = 'ProgramRoot'
            isAbstract = True
            strValue = 'ProgramRoot_' + strRootProgramId
        else:
            strType = 'ASTNode'

        if not 'children' in jsonObject.keys():
            strValue = jsonObject['type'] + '\n' + getTerminalValue(
                startLine, startOffset, endLine, endOffset, arrCodes)
        else:
            strValue = jsonObject['type']
        strLabel = '{}\n{}\n{}'.format(strType, strPosition, strValue)
        # if strValue!='':
        #     strLabel = '{}\n{}\n{}\n{}'.format(strType, strPosition,strValue)
    else:
        strRealLabel = revertBackWordFromPOS(jsonObject['label'])
        if 'isNLRootNode' in jsonObject.keys():
            strType = 'NLRoot'
            startLine = jsonObject['startLine']
            startOffset = jsonObject['startOffset']
            endLine = jsonObject['endLine']
            endOffset = jsonObject['endOffset']
            strPosition = '{}-{}-{}-{}'.format(startLine, startOffset, endLine,
                                               endOffset)
            strLabel = '{}\n{}\n{}'.format(strType, strPosition, strRealLabel)
        else:
            strType = 'NLNode'
            strLabel = '{}\n{}'.format(strType, strRealLabel)
        # if 'children' in jsonObject.keys():
        #     strValue = jsonObject['tag']
        #     if strValue == 'S1' or strValue == 'Paragraph':
        #         strValue = 'NLRoot_' + strRootProgramId
        #         # print('tagg {}'.format(strValue))
        # else:
        #     strValue = jsonObject['value']
    # strLabel = '{}\n{}'.format(strId, jsonObject['type'])

    # strLabel = '{}\n{}\n{}'.format(strId,strPosition,strType)
    # print('current label value {}'.format(strValue))
    if isInNLNodes:
        for numContext in dictGraphIndexContext.keys():
            graphId = dictGraphIndexContext[numContext]
            graphId.add_node(strLabel, color='red')
    else:
        for numContext in dictGraphIndexContext.keys():
            # print('accept lbl here {}\n{}'.format(strId, dictAcceptableIdsForVersions[numContext]))
            if int(strId) in dictAcceptableIdsForVersions[numContext]:
                # print('accept lbl here {} {}'.format(strId,strLabel))
                graphId = dictGraphIndexContext[numContext]
                graphId.add_node(strLabel, color='blue')

    if 'children' in jsonObject.keys():
        lstChildren = jsonObject['children']
        for i in range(0, len(lstChildren)):
            strChildLabel = generateGraphForMixCode(
                lstChildren[i], arrCodes, strRootProgramId, isInNLNodes,
                dictGraphIndexContext, dictAcceptableIdsForVersions)

            if not isInNLNodes:
                strChildId = lstChildren[i]['id']
                for numContext in dictGraphIndexContext.keys():
                    if int(strId) in dictAcceptableIdsForVersions[
                            numContext] and int(
                                strChildId
                            ) in dictAcceptableIdsForVersions[numContext]:
                        graphId = dictGraphIndexContext[numContext]
                        graphId.add_edge(strLabel,
                                         strChildLabel,
                                         color='black')
            else:
                for numContext in dictGraphIndexContext.keys():
                    graphId = dictGraphIndexContext[numContext]
                    graphId.add_edge(strLabel, strChildLabel, color='black')

    # else:
    #     strTerminalLabel = '\n{}\n{}'.format(strPosition,
    #                                            getTerminalValue(startLine, startOffset, endLine, endOffset,
    #                                                             arrCodes))
    #     graph.add_node(strTerminalLabel, color='yellow')
    #     graph.add_edge(strLabel, strTerminalLabel, color='black')

    return strLabel
def generateGraphForMixCodeRemoveNLNode(jsonObject, arrCodes, strRootProgramId,
                                        strRootLabel, isInNLNodes, graph,
                                        dictGraphIndexContext,
                                        dictAcceptableIdsForVersions):
    strValue = 'EMPTY'
    isAbstract = False
    strType = 'ASTNode'
    strLabelInClassification = ''
    strId = '-1'

    if 'isNLRootNode' in jsonObject.keys():
        isInNLNodes = True

    if not isInNLNodes:
        startLine = jsonObject['startLine']
        startOffset = jsonObject['startOffset']
        endLine = jsonObject['endLine']
        endOffset = jsonObject['endOffset']
        strId = str(jsonObject['id'])
        strPosition = '{}-{}-{}-{}'.format(startLine, startOffset, endLine,
                                           endOffset)

        if strId == '1':
            strType = 'ProgramRoot'
            strLabelInClassification = strRootLabel
            isAbstract = True
            strValue = 'ProgramRoot_' + strRootProgramId
        else:
            strType = 'ASTNode'

        if not 'children' in jsonObject.keys():
            strValue = getTerminalValue(startLine, startOffset, endLine,
                                        endOffset, arrCodes)
        else:
            strValue = jsonObject['type']
    else:
        strPosition = jsonObject['position']
        if 'isNLRootNode' in jsonObject.keys():
            strLabelInClassification = strRootLabel
            strType = 'NLRoot'
        else:
            strType = 'NLNode'
        if 'children' in jsonObject.keys():
            strValue = jsonObject['tag']
            if strValue == 'S1':
                strValue = 'NLRoot_' + strRootProgramId
                # print('tagg {}'.format(strValue))

        else:
            strValue = jsonObject['value']

    # strLabel = '{}\n{}'.format(strId, jsonObject['type'])

    strLabel = '{}\n{}\n{}\n{}\n{}'.format(isAbstract, strType, strValue,
                                           strLabelInClassification,
                                           strPosition)
    # strLabel = '{}\n{}\n{}'.format(strId,strPosition,strType)
    # print('current label value {}'.format(strValue))
    if isInNLNodes:
        graph.add_node(strLabel, color='red')
        for numContext in dictGraphIndexContext.keys():
            graphId = dictGraphIndexContext[numContext]
            graphId.add_node(strLabel, color='red')
    else:
        graph.add_node(strLabel, color='blue')
        for numContext in dictGraphIndexContext.keys():
            # print('accept lbl here {}\n{}'.format(strId, dictAcceptableIdsForVersions[numContext]))
            if int(strId) in dictAcceptableIdsForVersions[numContext]:
                # print('accept lbl here {} {}'.format(strId,strLabel))
                graphId = dictGraphIndexContext[numContext]
                graphId.add_node(strLabel, color='blue')

    if 'children' in jsonObject.keys():
        lstChildren = jsonObject['children']
        for i in range(0, len(lstChildren)):
            if isInNLNodes:
                continue
            strChildLabel = generateGraphForMixCodeRemoveNLNode(
                lstChildren[i], arrCodes, strRootProgramId, strRootLabel,
                isInNLNodes, graph, dictGraphIndexContext,
                dictAcceptableIdsForVersions)
            if strLabel != strChildLabel:
                graph.add_edge(strLabel, strChildLabel, color='black')
            if not isInNLNodes:
                strChildId = lstChildren[i]['id']
                for numContext in dictGraphIndexContext.keys():
                    if int(strId) in dictAcceptableIdsForVersions[
                            numContext] and int(
                                strChildId
                            ) in dictAcceptableIdsForVersions[numContext]:
                        graphId = dictGraphIndexContext[numContext]
                        graphId.add_edge(strLabel,
                                         strChildLabel,
                                         color='black')
    return strLabel