def __loadProgramExecutionFromJsonMap(self, progExecMap): ''' Load a new ProgramExecution class from a "Json-compliant" dict. ''' ## # @param aProgramExecution Program execution that holds the LanguageObject searched. # @id Id for the LanguageObject being serched (or 0 for a "None" object). # @return The LanguageObject searching for id, or None if id is 0. def getLanguageObjectFromId(aProgramExecution, id ): ''' Return the LanguageObject searching for id, or None if id is 0. ''' return None if id is 0 else aProgramExecution.getLanguageObjects()[id] JSON = CallGraphSerializer.JSON language = progExecMap[JSON.LANGUAGE] myProgramExecution = ProgramExecution(progExecMap[JSON.LANGUAGE]) #LanguageType map is already loaded, verify it has the same values langTypesMap = progExecMap[JSON.LANGUAGE_TYPES] langTypes = [] for lt in langTypesMap: ltId = lt[JSON.ID] if LanguageType.asString(ltId) != lt[JSON.NAME]: raise CallGraphLoadException("Invalid LanguageType pair. Id: " + str(ltId) + ", name:'" + lt[JSON.NAME] + "'") langTypes.append(ltId) if sorted(myProgramExecution.getLanguageTypes()) != sorted(langTypes): raise CallGraphLoadException("Invalid LanguageType's: " + str(langTypes)) langObjectsArray = progExecMap[JSON.LANGUAGE_OBJECTS] for lo in langObjectsArray: parent = getLanguageObjectFromId(myProgramExecution, lo[JSON.PARENT_ID]) myLo = LanguageObject (lo[JSON.ID], lo[JSON.LANGUAGE_TYPE_ID], lo[JSON.DECLARATION_TYPE], asJsonString(lo[JSON.DECLARATION_CODE]), parent) myProgramExecution.addLanguageObject(myLo) myCalls = progExecMap[JSON.CALL_GRAPH] for callMap in myCalls: callId = callMap[JSON.ID] callee = getLanguageObjectFromId(myProgramExecution, callMap[JSON.CALLEE_ID]) funcName = callMap[JSON.FUNC_NAME] methodType = callMap[JSON.METHOD_TYPE] level = callMap[JSON.LEVEL] returnedObject = getLanguageObjectFromId(myProgramExecution, callMap[JSON.RETURNED_OBJECT] if callMap.has_key(JSON.RETURNED_OBJECT) else 0) threwException = callMap[JSON.THREW_EXCEPTION] if callMap.has_key(JSON.THREW_EXCEPTION) else False totalTime = callMap[JSON.TOTAL_TIME] if callMap.has_key(JSON.TOTAL_TIME) else None args = callMap[JSON.ARGUMENTS][JSON.ARGS] argsList = [] #TODO GERVA: UNIFICAR if language == ProgramExecution.Languages.PYTHON: for arg in args: argObj = Argument(getLanguageObjectFromId(myProgramExecution, arg)) argsList.append(argObj) kargsMap = callMap[JSON.ARGUMENTS][JSON.KARGS] for argName, objId in kargsMap.items(): argObj = Argument(getLanguageObjectFromId(myProgramExecution, objId), argName) argsList.append(argObj) else: assert language == ProgramExecution.Languages.C_PLUS_PLUS for arg in args: argLoId = arg[JSON.ID] argValueType = arg[JSON.ARG_TYPE] argIsConst = arg[JSON.IS_CONST] argObj = Argument(getLanguageObjectFromId(myProgramExecution, argLoId), None, argValueType, argIsConst) argsList.append(argObj) func = FunctionCall(callId, callee, funcName, methodType, argsList, level, returnedObject, threwException, totalTime) myProgramExecution.addFunctionCall(func) return myProgramExecution
def __loadProgramExecutionFromJsonMap(self, progExecMap): ''' Load a new ProgramExecution class from a "Json-compliant" dict. ''' ## # @param aProgramExecution Program execution that holds the LanguageObject searched. # @id Id for the LanguageObject being serched (or 0 for a "None" object). # @return The LanguageObject searching for id, or None if id is 0. def getLanguageObjectFromId(aProgramExecution, id): ''' Return the LanguageObject searching for id, or None if id is 0. ''' return None if id is 0 else aProgramExecution.getLanguageObjects( )[id] JSON = CallGraphSerializer.JSON language = progExecMap[JSON.LANGUAGE] myProgramExecution = ProgramExecution(progExecMap[JSON.LANGUAGE]) #LanguageType map is already loaded, verify it has the same values langTypesMap = progExecMap[JSON.LANGUAGE_TYPES] langTypes = [] for lt in langTypesMap: ltId = lt[JSON.ID] if LanguageType.asString(ltId) != lt[JSON.NAME]: raise CallGraphLoadException( "Invalid LanguageType pair. Id: " + str(ltId) + ", name:'" + lt[JSON.NAME] + "'") langTypes.append(ltId) if sorted(myProgramExecution.getLanguageTypes()) != sorted(langTypes): raise CallGraphLoadException("Invalid LanguageType's: " + str(langTypes)) langObjectsArray = progExecMap[JSON.LANGUAGE_OBJECTS] for lo in langObjectsArray: parent = getLanguageObjectFromId(myProgramExecution, lo[JSON.PARENT_ID]) myLo = LanguageObject(lo[JSON.ID], lo[JSON.LANGUAGE_TYPE_ID], lo[JSON.DECLARATION_TYPE], asJsonString(lo[JSON.DECLARATION_CODE]), parent) myProgramExecution.addLanguageObject(myLo) myCalls = progExecMap[JSON.CALL_GRAPH] for callMap in myCalls: callId = callMap[JSON.ID] callee = getLanguageObjectFromId(myProgramExecution, callMap[JSON.CALLEE_ID]) funcName = callMap[JSON.FUNC_NAME] methodType = callMap[JSON.METHOD_TYPE] level = callMap[JSON.LEVEL] returnedObject = getLanguageObjectFromId( myProgramExecution, callMap[JSON.RETURNED_OBJECT] if callMap.has_key(JSON.RETURNED_OBJECT) else 0) threwException = callMap[JSON.THREW_EXCEPTION] if callMap.has_key( JSON.THREW_EXCEPTION) else False totalTime = callMap[JSON.TOTAL_TIME] if callMap.has_key( JSON.TOTAL_TIME) else None args = callMap[JSON.ARGUMENTS][JSON.ARGS] argsList = [] #TODO GERVA: UNIFICAR if language == ProgramExecution.Languages.PYTHON: for arg in args: argObj = Argument( getLanguageObjectFromId(myProgramExecution, arg)) argsList.append(argObj) kargsMap = callMap[JSON.ARGUMENTS][JSON.KARGS] for argName, objId in kargsMap.items(): argObj = Argument( getLanguageObjectFromId(myProgramExecution, objId), argName) argsList.append(argObj) else: assert language == ProgramExecution.Languages.C_PLUS_PLUS for arg in args: argLoId = arg[JSON.ID] argValueType = arg[JSON.ARG_TYPE] argIsConst = arg[JSON.IS_CONST] argObj = Argument( getLanguageObjectFromId(myProgramExecution, argLoId), None, argValueType, argIsConst) argsList.append(argObj) func = FunctionCall(callId, callee, funcName, methodType, argsList, level, returnedObject, threwException, totalTime) myProgramExecution.addFunctionCall(func) return myProgramExecution
def __dumpProgramExecutionAsJsonMap(self, aProgramExecution): ''' Translate a ProgramExecution class to a "Json-compliant" format, i.e.: a Python dict (simplejson forces the object to dump to be a native type, not a custom class) with the same information. ''' ## # @param obj LanguageObject, or None # @return Obj's LanguageObject id, or 0 if obj is None. def getOptionalObjectId( obj ): ''' Return LanguageObject id, or 0 if obj is None. ''' return 0 if obj is None else obj.getId() JSON = CallGraphSerializer.JSON progExecMap = {} progExecMap[JSON.LANGUAGE] = aProgramExecution.getLanguage() progExecMap[JSON.LANGUAGE_TYPES] = [] ltArray = progExecMap[JSON.LANGUAGE_TYPES] myLanguageTypes = aProgramExecution.getLanguageTypes() for lt in myLanguageTypes: ltArray.append( { JSON.ID: lt, JSON.NAME: LanguageType.asString( lt) } ) langObjects = aProgramExecution.getLanguageObjects() progExecMap[JSON.LANGUAGE_OBJECTS] = [] loArray = progExecMap[JSON.LANGUAGE_OBJECTS] for oId, o in langObjects.items(): loMap = {} assert oId == o.getId() loMap[JSON.ID] = oId loMap[JSON.LANGUAGE_TYPE_ID] = o.getLanguageType() loMap[JSON.DECLARATION_TYPE] = o.getDeclarationType() loMap[JSON.DECLARATION_CODE] = fromJsonString(o.getDeclarationCode()) parent = o.getParent() loMap[JSON.PARENT_ID] = getOptionalObjectId(parent) loArray.append(loMap) theCalls = aProgramExecution.getFunctionCalls() progExecMap[JSON.CALL_GRAPH] = [] callGraph = progExecMap[JSON.CALL_GRAPH] for aCall in theCalls: callMap = {} callMap[JSON.ID] = aCall.getId() callMap[JSON.CALLEE_ID] = aCall.getCallee().getId() callMap[JSON.FUNC_NAME] = aCall.getFunctionName() callMap[JSON.METHOD_TYPE] = aCall.getMethodType() callMap[JSON.LEVEL] = aCall.getLevel() callMap[JSON.RETURNED_OBJECT] = getOptionalObjectId(aCall.getReturnedObject()) callMap[JSON.THREW_EXCEPTION] = aCall.threwException() totalTime = aCall.getTotalTime() if totalTime: callMap[JSON.TOTAL_TIME] = totalTime #Arguments callMap[JSON.ARGUMENTS] = {} argsList = [] kargsMap = {} for anArgument in aCall.getArgsList(): theId = anArgument.getLanguageObject().getId() theName = anArgument.getName() if anArgument.getName() is None: argsList.append(theId) else: kargsMap[theName] = theId callMap[JSON.ARGUMENTS][JSON.ARGS] = argsList callMap[JSON.ARGUMENTS][JSON.KARGS] = kargsMap callGraph.append(callMap) return progExecMap
def __dumpProgramExecutionAsJsonMap(self, aProgramExecution): ''' Translate a ProgramExecution class to a "Json-compliant" format, i.e.: a Python dict (simplejson forces the object to dump to be a native type, not a custom class) with the same information. ''' ## # @param obj LanguageObject, or None # @return Obj's LanguageObject id, or 0 if obj is None. def getOptionalObjectId(obj): ''' Return LanguageObject id, or 0 if obj is None. ''' return 0 if obj is None else obj.getId() JSON = CallGraphSerializer.JSON progExecMap = {} progExecMap[JSON.LANGUAGE] = aProgramExecution.getLanguage() progExecMap[JSON.LANGUAGE_TYPES] = [] ltArray = progExecMap[JSON.LANGUAGE_TYPES] myLanguageTypes = aProgramExecution.getLanguageTypes() for lt in myLanguageTypes: ltArray.append({JSON.ID: lt, JSON.NAME: LanguageType.asString(lt)}) langObjects = aProgramExecution.getLanguageObjects() progExecMap[JSON.LANGUAGE_OBJECTS] = [] loArray = progExecMap[JSON.LANGUAGE_OBJECTS] for oId, o in langObjects.items(): loMap = {} assert oId == o.getId() loMap[JSON.ID] = oId loMap[JSON.LANGUAGE_TYPE_ID] = o.getLanguageType() loMap[JSON.DECLARATION_TYPE] = o.getDeclarationType() loMap[JSON.DECLARATION_CODE] = fromJsonString( o.getDeclarationCode()) parent = o.getParent() loMap[JSON.PARENT_ID] = getOptionalObjectId(parent) loArray.append(loMap) theCalls = aProgramExecution.getFunctionCalls() progExecMap[JSON.CALL_GRAPH] = [] callGraph = progExecMap[JSON.CALL_GRAPH] for aCall in theCalls: callMap = {} callMap[JSON.ID] = aCall.getId() callMap[JSON.CALLEE_ID] = aCall.getCallee().getId() callMap[JSON.FUNC_NAME] = aCall.getFunctionName() callMap[JSON.METHOD_TYPE] = aCall.getMethodType() callMap[JSON.LEVEL] = aCall.getLevel() callMap[JSON.RETURNED_OBJECT] = getOptionalObjectId( aCall.getReturnedObject()) callMap[JSON.THREW_EXCEPTION] = aCall.threwException() totalTime = aCall.getTotalTime() if totalTime: callMap[JSON.TOTAL_TIME] = totalTime #Arguments callMap[JSON.ARGUMENTS] = {} argsList = [] kargsMap = {} for anArgument in aCall.getArgsList(): theId = anArgument.getLanguageObject().getId() theName = anArgument.getName() if anArgument.getName() is None: argsList.append(theId) else: kargsMap[theName] = theId callMap[JSON.ARGUMENTS][JSON.ARGS] = argsList callMap[JSON.ARGUMENTS][JSON.KARGS] = kargsMap callGraph.append(callMap) return progExecMap