CN116363256A - Method and device for realizing visual display of code pictures - Google Patents
Method and device for realizing visual display of code pictures Download PDFInfo
- Publication number
- CN116363256A CN116363256A CN202310282197.1A CN202310282197A CN116363256A CN 116363256 A CN116363256 A CN 116363256A CN 202310282197 A CN202310282197 A CN 202310282197A CN 116363256 A CN116363256 A CN 116363256A
- Authority
- CN
- China
- Prior art keywords
- code
- picture
- statement
- test case
- visual display
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Images
Classifications
-
- G—PHYSICS
- G09—EDUCATION; CRYPTOGRAPHY; DISPLAY; ADVERTISING; SEALS
- G09B—EDUCATIONAL OR DEMONSTRATION APPLIANCES; APPLIANCES FOR TEACHING, OR COMMUNICATING WITH, THE BLIND, DEAF OR MUTE; MODELS; PLANETARIA; GLOBES; MAPS; DIAGRAMS
- G09B5/00—Electrically-operated educational appliances
- G09B5/02—Electrically-operated educational appliances with visual presentation of the material to be studied, e.g. using film strip
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F11/00—Error detection; Error correction; Monitoring
- G06F11/36—Prevention of errors by analysis, debugging or testing of software
- G06F11/3668—Testing of software
- G06F11/3672—Test management
- G06F11/3684—Test management for test design, e.g. generating new test cases
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06T—IMAGE DATA PROCESSING OR GENERATION, IN GENERAL
- G06T11/00—Two-dimensional [2D] image generation
- G06T11/20—Drawing from basic elements
- G06T11/26—Drawing of charts or graphs
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06V—IMAGE OR VIDEO RECOGNITION OR UNDERSTANDING
- G06V30/00—Character recognition; Recognising digital ink; Document-oriented image-based pattern recognition
- G06V30/10—Character recognition
-
- Y—GENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
- Y02—TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
- Y02D—CLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
- Y02D10/00—Energy efficient computing, e.g. low power processors, power management or thermal management
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Educational Technology (AREA)
- Educational Administration (AREA)
- Business, Economics & Management (AREA)
- Computer Hardware Design (AREA)
- Quality & Reliability (AREA)
- General Engineering & Computer Science (AREA)
- Computer Vision & Pattern Recognition (AREA)
- Multimedia (AREA)
- Debugging And Monitoring (AREA)
Abstract
本发明公开了一种代码图片的可视化展示实现方法及装置,涉及软件工程技术领域,该方法包括基于光学字符识别方式识别得到代码图片中的代码,并对识别得到的代码进行纠错处理;基于代码格式化工具对代码进行格式化处理,并采用绘图工具将格式化后的代码制作成代码图片;对格式化后的代码进行插桩处理,并采用测试用例产生工具对插桩后的代码产生测试用例;运行测试用例,并采用特定显示方式对代码运行顺序进行展示,同时展示代码运行阶段的数值信息。本发明能够有效帮助用户增进对于代码图片中代码片段的理解,简化了用户实现代码的流程。
The invention discloses a method and device for visually displaying a code picture, and relates to the technical field of software engineering. The method includes recognizing the code in the code picture based on an optical character recognition method, and performing error correction processing on the recognized code; based on The code formatting tool formats the code, and uses the drawing tool to make the formatted code into a code picture; performs instrumentation on the formatted code, and uses the test case generation tool to generate the instrumented code Test case; run the test case, and use a specific display method to display the code running sequence, and at the same time display the numerical information of the code running stage. The invention can effectively help the user to improve the understanding of the code segment in the code picture, and simplifies the process for the user to realize the code.
Description
技术领域technical field
本发明涉及软件工程技术领域,具体涉及一种代码图片的可视化展示实现方法及装置。The invention relates to the technical field of software engineering, in particular to a method and device for implementing a visual display of code pictures.
背景技术Background technique
在以数据结构与算法分析为首的教科书中,经常出现不易理解的代码。以往,学生在阅读此类书籍时,由于教科书中的代码为静态的文字,学生们对于教科书中的代码具体是如何运行会产生疑问,只能采用手动方式将书中代码在电脑上实际敲打出来以运行。例如,学生不理解教科书上一个排序的算法,只能手动将代码实现并设计测试用例带入该算法函数,并查看运行结果。In textbooks such as data structure and algorithm analysis, codes that are difficult to understand often appear. In the past, when students read such books, because the codes in the textbooks were static text, students would have doubts about how the codes in the textbooks actually run. They had to manually type out the codes in the books on the computer. to run. For example, students do not understand a sorting algorithm in textbooks, so they can only manually implement the code and design test cases into the algorithm function, and view the running results.
然而,上述实现方式对于学生理解代码还是效果不佳,主要原因是:(1)对于运行时代码的执行顺序不了解;(2)对于代码运行时(例如进行排序)各项参数的变动没办法掌握;(3)手动敲打代码方式极为耗时。However, the above implementation method is not effective for students to understand the code. The main reasons are: (1) they do not understand the execution order of the code at runtime; (2) there is no way to change the parameters of the code at runtime (such as sorting) (3) The way of manually typing codes is extremely time-consuming.
发明内容Contents of the invention
针对现有技术中存在的缺陷,本发明的目的在于提供一种代码图片的可视化展示实现方法及装置,能够有效帮助用户增进对于代码图片中代码片段的理解,简化了用户实现代码的流程。In view of the deficiencies in the prior art, the purpose of the present invention is to provide a method and device for visually displaying code pictures, which can effectively help users improve their understanding of code fragments in code pictures, and simplify the process for users to implement codes.
为达到以上目的,本发明提供的一种代码图片的可视化展示实现方法,具体包括以下步骤:In order to achieve the above purpose, the present invention provides a method for realizing the visual display of code pictures, which specifically includes the following steps:
基于光学字符识别方式识别得到代码图片中的代码,并对识别得到的代码进行纠错处理;Recognize the code in the code picture based on the optical character recognition method, and perform error correction processing on the recognized code;
基于代码格式化工具对代码进行格式化处理,并采用绘图工具将格式化后的代码制作成代码图片;Format the code based on the code formatting tool, and use the drawing tool to make the formatted code into a code picture;
对格式化后的代码进行插桩处理,并采用测试用例产生工具对插桩后的代码产生测试用例;Perform instrumentation on the formatted code, and use the test case generation tool to generate test cases for the instrumented code;
运行测试用例,并采用特定显示方式对代码运行顺序进行展示,同时展示代码运行阶段的数值信息。Run the test case, and use a specific display method to display the code running sequence, and at the same time display the numerical information of the code running stage.
在上述技术方案的基础上,所述对识别得到的代码进行纠错处理,具体为:On the basis of the above technical solution, the error correction processing of the identified code is specifically:
基于启发式规则对代码进行纠错处理,以将光学字符识别时识别的错误字符替换为正确字符。Codes are corrected based on heuristic rules to replace incorrect characters recognized during optical character recognition with correct characters.
在上述技术方案的基础上,所述代码图片中,代码间采用等宽字体。On the basis of the above technical solution, in the code picture, a font of equal width is used between the codes.
在上述技术方案的基础上,所述对格式化后的代码进行插桩处理,具体步骤包括:On the basis of the above technical solution, the specific steps of inserting the formatted code include:
采用源代码分析和转换工具对代码进行遍历,在每个语句前后插入打印该代码位置编号以及其参数值的代码。Use source code analysis and conversion tools to traverse the code, and insert the code that prints the code position number and its parameter value before and after each statement.
在上述技术方案的基础上,所述在每个语句前后插入打印该代码位置编号以及其参数值的代码,具体为:On the basis of the above technical solution, the code for printing the code position number and its parameter value is inserted before and after each statement, specifically:
对于非循环的所有语句,在语句前插入打印该语句行编号的代码;For all statements that are not loops, insert the code that prints the line number of the statement before the statement;
对于循环语句,则在语句前插入打印该循环开始与结束位置的代码;For a loop statement, insert the code that prints the start and end positions of the loop before the statement;
对于赋值语句、初始值语句,以及输入特定参数名的调用函数语句,则另外在语句后插入打印该值的代码。For an assignment statement, an initial value statement, and a call function statement that inputs a specific parameter name, additionally insert a code that prints the value after the statement.
在上述技术方案的基础上,On the basis of the above technical solutions,
在采用源代码分析和转换工具对代码进行遍历的同时,获取当前代码区块的位置信息;Obtain the location information of the current code block while traversing the code with source code analysis and conversion tools;
所述位置信息包括代码起始行与结束字元编号,以及代码结束行与结束字元编号。The position information includes code start line and end character number, and code end line and end character number.
在上述技术方案的基础上,在采用测试用例产生工具对插桩后的代码产生测试用例时,在产生的测试用例中限制代入源代码的参数。On the basis of the above technical solution, when using the test case generation tool to generate test cases for the inserted code, limit the parameters substituted into the source code in the generated test cases.
在上述技术方案的基础上,当采用测试用例产生工具对插桩后的代码产生测试用例之后,还包括:On the basis of the above technical solution, when the test case generation tool is used to generate test cases for the inserted code, it also includes:
采用源代码分析和转换工具遍历测试用例,判断测试用例中被测函数的参数是否完整,以挑选得到具备预设可视化理解效果的测试用例。Use source code analysis and conversion tools to traverse test cases, judge whether the parameters of the function under test in test cases are complete, and select test cases with preset visual understanding effects.
在上述技术方案的基础上,所述数值信息包括代码行编号、循环语句位置编号和代码中各项参数的数值。On the basis of the above technical solution, the numerical information includes the code line number, the position number of the loop statement and the numerical value of each parameter in the code.
本发明提供的一种代码图片的可视化展示实现装置,包括:A device for implementing a visual display of a code image provided by the present invention includes:
识别模块,其用于基于光学字符识别方式识别得到代码图片中的代码,并对识别得到的代码进行纠错处理;A recognition module, which is used to recognize the code in the code picture based on the optical character recognition method, and perform error correction processing on the recognized code;
第一处理模块,其用于基于代码格式化工具对代码进行格式化处理,并采用绘图工具将格式化后的代码制作成代码图片;A first processing module, which is used to format the code based on a code formatting tool, and use a drawing tool to make the formatted code into a code picture;
第二处理模块,其用于对格式化后的代码进行插桩处理,并采用测试用例产生工具对插桩后的代码产生测试用例;The second processing module is used to insert the formatted code, and use a test case generation tool to generate a test case for the inserted code;
执行模块,其用于运行测试用例,并采用特定显示方式对代码运行顺序进行展示,同时展示代码运行阶段的数值信息。Execution module, which is used to run test cases, and use a specific display method to display the code running sequence, and at the same time display the numerical information of the code running stage.
与现有技术相比,本发明的优点在于:能够有效帮助用户增进对于代码图片中代码片段的理解,简化了用户实现代码的流程,使得用户不必手动输入并实现代码,简化了用户理解代码的流程,自动产生测试用例带入代码图片中代码片段,自动产生可视化展示代码的运行语句顺序与相对应参数值的变化,具有自动化、低成本、易于理解等特点。Compared with the prior art, the present invention has the advantages of being able to effectively help users improve their understanding of code fragments in code pictures, simplifying the process for users to implement codes, eliminating the need for users to manually input and implement codes, and simplifying the process for users to understand codes. The process automatically generates test cases and brings them into code fragments in the code picture, and automatically generates a visual display of the code's running statement sequence and changes in corresponding parameter values. It has the characteristics of automation, low cost, and easy understanding.
附图说明Description of drawings
为了更清楚地说明本申请实施例中的技术方案,下面将对实施例描述中所需要使用的附图作简单地介绍,显而易见地,下面描述中的附图仅仅是本申请的一些实施例,对于本领域普通技术人员来讲,在不付出创造性劳动的前提下,还可以根据这些附图获得其他的附图。In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings that need to be used in the description of the embodiments will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments of the present application. For those skilled in the art, other drawings can also be obtained based on these drawings without creative effort.
图1为本发明实施例中一种代码图片的可视化展示实现方法的流程图。FIG. 1 is a flow chart of a method for implementing a visual display of a code image in an embodiment of the present invention.
具体实施方式Detailed ways
为使本申请实施例的目的、技术方案和优点更加清楚,下面将结合本申请实施例中的附图,对本申请实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例是本申请的一部分实施例,而不是全部的实施例。In order to make the purposes, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments It is a part of the embodiments of this application, but not all of them.
本发明提供一种代码图片的可视化展示实现方法,该方法可基于智能设备实现,结合智能设备帮助用户(如学生、程序员等)快速理解文本中的代码示例,使用者只需使用智能设备对着目标代码进行截取图片,就能对此代码片段以高亮代码行的方式对代码运行过程进行可视化展示,并展示各项参数值实时的变化,具有自动化、低成本、易于理解等特点;以可视化展示程序运行过程的方式增进用户(如学生、程序员等)对于代码图片(如教科书上的代码照片、屏幕代码截图等)中代码片段的理解。The present invention provides a method for implementing visual display of code pictures, which can be implemented based on smart devices, combined with smart devices to help users (such as students, programmers, etc.) quickly understand the code examples in the text, users only need to use smart devices to By intercepting the image of the target code, you can visually display the code running process by highlighting the code line, and display the real-time changes of various parameter values. It has the characteristics of automation, low cost, and easy understanding; The way of visually displaying the running process of the program improves users' (such as students, programmers, etc.) understanding of code fragments in code pictures (such as code photos on textbooks, screenshots of screen codes, etc.).
参见图1所示,本发明实施例提供的一种代码图片的可视化展示实现方法,具体包括以下步骤:Referring to Figure 1, a method for implementing a visual display of a code image provided by an embodiment of the present invention specifically includes the following steps:
S1:基于光学字符识别方式识别得到代码图片中的代码,并对识别得到的代码进行纠错处理;S1: Recognize the code in the code picture based on the optical character recognition method, and perform error correction processing on the recognized code;
即对于代码图片进行光学字符识别,得到代码的字符串,并通过程序分析技术对识别得到的代码字符串进行纠错,使其可编译。That is, optical character recognition is performed on code pictures to obtain code strings, and error correction is performed on the recognized code strings through program analysis technology to make them compilable.
本发明中,对识别得到的代码进行纠错处理,具体为:基于启发式规则对代码进行纠错处理,以将光学字符识别时识别的错误字符替换为正确字符。即基于启发式规则来替换光学字符识别所产生的常见错误,其技术形式是基于静态程序分析的代码纠错,以达到可编译的效果。In the present invention, performing error correction processing on the recognized code is specifically: performing error correction processing on the code based on heuristic rules, so as to replace wrong characters recognized during optical character recognition with correct characters. That is, based on heuristic rules to replace common errors generated by optical character recognition, its technical form is code error correction based on static program analysis to achieve compilable results.
S2:基于代码格式化工具对代码进行格式化处理,并采用绘图工具将格式化后的代码制作成代码图片;代码图片中,代码间采用等宽字体,以便掌握各行或各语句在图片上的位置方便后续进行高亮可视化展示。S2: Format the code based on the code formatting tool, and use the drawing tool to make the formatted code into a code picture; in the code picture, use a font of equal width between the codes, so as to grasp the position of each line or statement on the picture The location is convenient for subsequent highlighting and visual display.
S3:对格式化后的代码进行插桩处理,并采用测试用例产生工具对插桩后的代码产生测试用例;对代码进行插桩处理,可使得代码在运行时能以字符串的形式打印出相关信息。S3: Instrument the formatted code, and use the test case generation tool to generate test cases for the instrumented code; instrument the code so that the code can be printed in the form of a string at runtime Related Information.
本发明中,对格式化后的代码进行插桩处理,具体步骤包括:采用源代码分析和转换工具对代码进行遍历,在每个语句前后插入打印该代码位置编号以及其参数值的代码。In the present invention, the formatted code is instrumented, and the specific steps include: using a source code analysis and conversion tool to traverse the code, inserting and printing the code position number and its parameter value before and after each statement.
本发明中,在每个语句前后插入打印该代码位置编号以及其参数值的代码,具体为:In the present invention, the code that prints this code position number and its parameter value is inserted before and after each statement, specifically:
对于非循环的所有语句,在语句前插入打印该语句行编号的代码;For all statements that are not loops, insert the code that prints the line number of the statement before the statement;
对于循环语句,则在语句前插入打印该循环开始与结束位置的代码;For a loop statement, insert the code that prints the start and end positions of the loop before the statement;
对于赋值语句、初始值语句,以及输入特定参数名的调用函数语句,则另外在语句后插入打印该值的代码。For an assignment statement, an initial value statement, and a call function statement that inputs a specific parameter name, additionally insert a code that prints the value after the statement.
进一步的,在采用源代码分析和转换工具对代码进行遍历的同时,获取当前代码区块的位置信息;位置信息包括代码起始行与结束字元编号,以及代码结束行与结束字元编号。Further, while traversing the code with a source code analysis and conversion tool, the location information of the current code block is obtained; the location information includes code start line and end character number, and code end line and end character number.
本发明中,在采用测试用例产生工具对插桩后的代码产生测试用例时,在产生的测试用例中代入源代码的参数。即在使用测试用例产生工具时,为了使可视化理解代码的效果更好,对产生的测试用例带入源代码的参数进行限制。In the present invention, when the test case generation tool is used to generate test cases for the inserted code, the parameters of the source code are substituted into the generated test cases. That is, when using the test case generation tool, in order to make the visual understanding of the code better, the parameters brought into the source code by the generated test case are restricted.
本发明中,当采用测试用例产生工具对插桩后的代码产生测试用例之后,还包括:采用源代码分析和转换工具遍历测试用例,判断测试用例中被测函数的参数是否完整,以挑选得到具备预设可视化理解效果的测试用例。In the present invention, after using the test case generation tool to generate the test case for the code after the post insertion, it also includes: using the source code analysis and conversion tool to traverse the test case, judging whether the parameters of the function under test in the test case are complete, and selecting to obtain Test cases with preset visual comprehension effects.
S4:运行测试用例,并采用特定显示方式对代码运行顺序进行展示,同时展示代码运行阶段的数值信息。S4: Run the test case, and use a specific display method to display the code running sequence, and display the numerical information of the code running stage.
即在运行测试用例时,通过遍历产生的与之相关信息的字符串,以高亮显示代码方式对代码运行顺序进行展示,同时展示代码运行阶段的数值信息,数值信息包括代码行编号、循环语句位置编号和代码中各项参数的数值。That is, when running a test case, by traversing the generated strings of related information, the code running sequence is displayed by highlighting the code, and the numerical information of the code running stage is displayed at the same time. The numerical information includes the code line number, loop statement The position number and the value of each parameter in the code.
具体的,本发明所述可视化展示实现方法可应用于智能设备(例如智能手机),以智能设备的应用的形式呈现。使用者在阅读文本材料时,若遇到需要理解的代码片段,即可打开智能设备应用,该智能设备应用会使用智能设备自带的摄像头权限,使用者只需要将画面对准代码片段并按截屏按钮就可以看到弹出的代码可视化展现。Specifically, the implementation method for visual display in the present invention can be applied to smart devices (such as smart phones), and presented in the form of applications of smart devices. When reading text materials, if the user encounters a code segment that needs to be understood, the smart device application can be opened. The smart device application will use the camera permission of the smart device. The user only needs to point the screen at the code segment and press Click the screenshot button to see the visual display of the pop-up code.
本发明结合了光学字符识别工具与测试用例生成工具,以代码插桩的方式收集代码信息,最后以可视化的形式展现。整体可以分成四个阶段:光学字符识别与代码补全纠错阶段、遍历代码进行插桩阶段、测试用例生成与选择阶段、输出结果收集与可视化展示阶段。The present invention combines an optical character recognition tool and a test case generation tool, collects code information by means of code insertion, and finally presents it in a visualized form. The whole can be divided into four stages: optical character recognition and code completion error correction stage, code traversal for instrumentation stage, test case generation and selection stage, output result collection and visual display stage.
对于光学字符识别与代码补全纠错阶段,首先使用光学字符识别技术将图片中的代码文字转换为字符串,由于光学字符识别技术可能存在精度不足的问题,在代码出现错误的情况下将无法编译,故进行代码纠错,可以使用启发式规则来将显而易见以及重复率高的错误进行修正。For the optical character recognition and code completion error correction stage, first use the optical character recognition technology to convert the code text in the picture into a string, because the optical character recognition technology may have insufficient precision, it will not be able to Compile, so for code error correction, heuristic rules can be used to correct obvious and high-repetition errors.
对于遍历代码进行插桩阶段,为了在运行代码时得出所需的信息,使用源代码分析和转换工具遍历代码,并在每一行代码之前插入打印该行代码号的语句;在每一个赋值语句、初始化语句与输入特定参数名的调用函数语句之后插入打印该参数的语句;在循环的前后插入标明循环种类与编号及其运行位置的语句。For the instrumentation stage of traversing the code, in order to obtain the required information when running the code, use source code analysis and conversion tools to traverse the code, and insert a statement that prints the code number of the line before each line of code; in each assignment statement 1. After the initialization statement and the call function statement for inputting a specific parameter name, insert a statement that prints the parameter; before and after the loop, insert a statement that indicates the type and number of the loop and its running position.
为了作可视化展示,利用代码格式化工具将原代码进行代码格式化,使代码风格统一。将格式化过后的代码使用绘图功能存取成代码图片,以此图片作为可视化展示的基底,并且为了能高亮出代码片段在图片中的位置,在此使用等宽字体。For visual display, use the code formatting tool to format the original code to make the code style uniform. Use the drawing function to access the formatted code as a code picture, and use the picture as the base of the visual display. In order to highlight the position of the code fragment in the picture, a monospaced font is used here.
在遍历代码的同时,由于已经遍历了代码,故可以得出当前代码行或者当前代码片段在整个代码中的位置,例如此行在第3行的第5个字元到第25个字元,并根据此位置信息与等宽字体的高度或宽度相乘,以之前所制作的可视化基底图片为基础,制作在基底图片上高亮目标代码片段的图片,并以对应目标代码片段命名,例如高亮第三行代码的图片命名为“3.jpg”。While traversing the code, since the code has been traversed, the position of the current code line or the current code fragment in the entire code can be obtained, for example, the line is in the 5th character to the 25th character of the 3rd line, And according to this position information multiplied by the height or width of the monospaced font, based on the previously created visual base image, make a picture that highlights the target code segment on the base image, and name it after the corresponding target code segment, such as height The image on the third line of code is named "3.jpg".
对于测试用例生成与选择阶段,使用测试用例生成工具对插桩后的代码生成测试用例。测试用例生成工具会根据设定的参数生成多个测试用例。由于工具生成的测试用例不一定有助于可视化理解代码,选择更好的测试用例就显得至关重要,在此使用源代码分析和转换工具检查测试用例代码,检查是否真的有待入非空的值,限制数组最小长度等等,若符合条件,则存取当前测试用例作为可视化展示使用,最后,运行通过筛选的测试用例。For the test case generation and selection phase, use the test case generation tool to generate test cases for the instrumented code. The test case generation tool will generate multiple test cases according to the set parameters. Since the test cases generated by the tool are not necessarily helpful for visual understanding of the code, it is very important to choose a better test case. Here, use source code analysis and conversion tools to check the test case code and check whether there are really non-empty value, limit the minimum length of the array, etc., if the conditions are met, access the current test case for use as a visual display, and finally, run the test cases that pass the filter.
对于输出结果收集与可视化展示阶段,由于已运行了测试用例,此时所插入的代码会将所需的信息顺序(包含代码行数、参数数值与循环状态)打印出。将此字符流读入并分解相关信息,即可将之前制作的高亮代码片段图片按照运行顺序高亮显示,进行可视化展示,并且显示运行到该代码片段时所有参数值的状态。For the output result collection and visual display stage, since the test case has been run, the code inserted at this time will print out the required information sequence (including the number of code lines, parameter values and loop status). By reading this character stream and decomposing relevant information, the previously created highlighted code snippet images can be highlighted in the running order for visual display, and the status of all parameter values when running to the code snippet is displayed.
以下对本发明代码图片的可视化展示实现方法的具体实施流程进行说明。The following describes the specific implementation process of the implementation method of the visual display of the code picture in the present invention.
(1)使用光学字符识别工具(例如ABBYY、Tesseract)取得代码图片中的代码;(1) Use optical character recognition tools (such as ABBYY, Tesseract) to obtain the code in the code picture;
(2)对于识别得到的代码进行纠错与代码补全,并把代码片段存成可运行的类,使用启发式规则将常见的光学字符识别错误替换为正确的字词,例如常见的I、l、1的错误问题,将1ength、Ioop等词替换为正确的length、loop;(2) Perform error correction and code completion on the recognized code, and save the code fragment as a runnable class, and use heuristic rules to replace common optical character recognition errors with correct words, such as common I, l. For the wrong problem of 1, replace words such as 1length and Ioop with the correct length and loop;
(3)对代码使用代码格式化工具(例如源代码分析和转换工具Spoon的PrettyPrint功能),并将格式化后的代码使用绘图工具(如java.awt.Graphics.drawString())将代码片段制作成图片,以此图片作为可视化的基础;(3) Use code formatting tools (such as the PrettyPrint function of the source code analysis and conversion tool Spoon) for the code, and use the formatted code to use the drawing tool (such as java.awt.Graphics.drawString()) to make code fragments into a picture, and use the picture as the basis for visualization;
(4)使用源代码分析和转换工具(例如spoon)将步骤(3)所格式化的代码遍历并进行插桩,加入将打印信息输出至文字档(在此存为sequence.txt)的代码。对于插桩部份,重点针对下面三种类型:(4) Use a source code analysis and conversion tool (such as spoon) to traverse and insert the code formatted in step (3), and add the code to output the printed information to a text file (here saved as sequence.txt). For the instrumentation part, the focus is on the following three types:
A.在每一行之前插入探针,打印代码行编号,例如out.write("!LINE!"+6+"#,#");A. Insert the probe before each line, print the code line number, for example out.write("!LINE!"+6+"#,#");
B.在每一个赋值语句、初始化语句与已被纪录的参数名被输入至其他函数的语句之后插入探针(包括for语句的initial等等),打印该参数值。例如out.write("p="+p+"#,#"),其中若是数组,则将数组的所有值输出,例如out.write("a="+java.util.Arrays.toString(a)+"#,#");B. Insert a probe (including the initial of the for statement, etc.) after each assignment statement, initialization statement, and recorded parameter name is input to other functions, and print the parameter value. For example out.write("p="+p+"#,#"), if it is an array, output all the values of the array, for example out.write("a="+java.util.Arrays.toString(a) +"#,#");
C.针对每一个循环种类,在其开始与结束插入探针,以显示目前状态与该循环编号。例如for循环外插入探针使其打印”ForA1”,例如out.write("ForA1#,#")、for循环开始前插入探针使其打印”ForB1”,例如out.write("ForB1#,#")、结束时插入探针,使其打印”ForC1”,例如out.write("ForC1#,#")。C. For each cycle type, insert probes at the beginning and end to display the current status and the cycle number. For example, insert a probe outside the for loop to print "ForA1", such as out.write("ForA1#,#"), insert a probe before the start of the for loop to print "ForB1", such as out.write("ForB1#, #"), insert the probe at the end so that it prints "ForC1", for example out.write("ForC1#,#").
(5)在遍历代码插桩的同时,取得该代码区块的位置信息,包括该代码起始行与结束字元编号、结束行与结束字元编号。每一次遍历时基于步骤(3)所制作的代码图片,根据等宽字体的宽度与高度相乘,即可以得出该代码块的坐标。利用该坐标信息以绘图方法(例如Java graphics.fillRect()),在代码图片上高亮出该代码块。最后使用Java I/O将此图另存为一图片,其档案名需对应步骤(4)探针所打印的字符。(5) Obtain the location information of the code block while traversing the code stub, including the code start line and end character number, end line and end character number. Based on the code picture made in step (3) during each traversal, the coordinates of the code block can be obtained by multiplying the width and height of the monospaced font. Use the coordinate information to highlight the code block on the code picture with a drawing method (such as Java graphics.fillRect()). Finally, use Java I/O to save this picture as a picture, and its file name must correspond to the characters printed by the probe in step (4).
(6)对于插桩后的代码,利用测试用例生成工具(例如evosuite)对其生成测试用例。为了提高可视化理解的效果,对于测试用例生成加入条件,例如evosuite指令中增加限制最大integer值不超过10,以及不要产生assertion,并且尽可能多产生一些测试用例。(6) For the inserted code, use a test case generation tool (such as evosuite) to generate a test case for it. In order to improve the effect of visual understanding, add conditions for test case generation, such as increasing the maximum integer value in the evosuite command to no more than 10, and not generating assertions, and generating as many test cases as possible.
(7)对于步骤(6)所产生的测试类,其中一个测试类会包含许多测试用例,使用源代码分析和转换工具对其中的每个测试用例进行遍历,对于每个测试用例,记录赋值语句所赋予的值和参数名,并且对应当前测试用例调用被测函数的语句,若带入的值不为空值、且若带入的值为数组,其长度大于3,则认为此测试用例对可视化理解是有帮助的,即可存取此测试用例单独至一个新的类,依序存为test0.java、test1.java等等。(7) For the test classes generated in step (6), one of the test classes will contain many test cases, use source code analysis and conversion tools to traverse each test case, and record assignment statements for each test case The given value and parameter name, and corresponding to the statement that the current test case calls the function under test, if the value brought in is not empty, and if the value brought in is an array with a length greater than 3, then the test case is considered to be correct Visual understanding is helpful, you can access this test case separately to a new class, and save it as test0.java, test1.java, etc. in sequence.
(8)将运行步骤(7)所存取的测试用例类的指令储存成批次档,运行该批次档即可得出打印出信息的文字档(sequence.txt)。使用Java I/O读取该文字流并遍历,并根据步骤(4)插入的探针所打印的值分别作处理。若是代码行或者是循环中的位置,则转换成步骤(5)所生成的对应图片路径,利用排程方法(例如timer())设置每一秒进行图片播放,若是打印的参数值,则将各参数值存储或者更新至一个数据结构(在此使用Map<String,ArrayList<String>>),并且将该数据结构在画面中展示出来。(8) Store the test case class instructions accessed in the running step (7) into a batch file, and run the batch file to obtain a text file (sequence.txt) for printing out information. Use Java I/O to read and traverse the text stream, and process it according to the values printed by the probes inserted in step (4). If it is a line of code or a position in a loop, convert it to the corresponding image path generated in step (5), use the scheduling method (such as timer()) to set the image to play every second, and if it is a printed parameter value, set Each parameter value is stored or updated to a data structure (Map<String, ArrayList<String>> is used here), and the data structure is displayed on the screen.
本发明的核心点在于:1、将光学字符识别技术、源代码分析和转换工具与测试用例自动生成工具相结合,以可视化的形式展现帮助用户理解代码图片中的代码片段;2、基于光学字符识别技术和代码纠错的可编译性恢复,基于启发式规则,通过静态程序分析技术纠正光学字符识别技术识别后的代码的识别错误,使得原本不完整或者残缺的代码片段可编译运行;3、基于测试用例生成技术和选取技术的程序执行的可视化理解,构造自动的测试用例生成,并依据限制输入被测代码的参数范围的策略来选取更适合可视化理解的测试用例;4、结合智能设备的应用程序,以动态的高亮执行位置展示显示测试用例所经过的代码区块的方式,结合智能设备自带的摄像头拍摄代码图片,并以高亮代码区块的方式进行展示代码运行顺序,简化了自行运行代码的繁琐步骤。The core points of the present invention are: 1. Combining optical character recognition technology, source code analysis and conversion tools with test case automatic generation tools, and visually displaying code fragments in code pictures to help users understand; 2. Based on optical characters Compilability restoration of recognition technology and code error correction, based on heuristic rules, corrects the recognition errors of codes recognized by optical character recognition technology through static program analysis technology, so that originally incomplete or incomplete code fragments can be compiled and run; 3. Based on the visual understanding of program execution based on test case generation technology and selection technology, automatic test case generation is constructed, and test cases that are more suitable for visual understanding are selected according to the strategy of limiting the parameter range of the input code under test; 4. Combined with smart devices The application program displays the code blocks passed by the test case in a dynamic way of highlighting the execution position, combines the smart device's built-in camera to take pictures of the code, and displays the code running sequence in the way of highlighting the code blocks, simplifying Eliminates the tedious steps of running the code yourself.
本发明实施例的代码图片的可视化展示实现方法,能够有效帮助用户增进对于代码图片中代码片段的理解,简化了用户实现代码的流程,使得用户不必手动输入并实现代码,简化了用户理解代码的流程,自动产生测试用例带入代码图片中代码片段,自动产生可视化展示代码的运行语句顺序与相对应参数值的变化,具有自动化、低成本、易于理解等特点。The implementation method of the visual display of the code picture in the embodiment of the present invention can effectively help the user to improve the understanding of the code fragments in the code picture, simplify the process of the user implementing the code, so that the user does not need to manually input and implement the code, and simplifies the process for the user to understand the code The process automatically generates test cases and brings them into code fragments in the code picture, and automatically generates a visual display of the code's running statement sequence and changes in corresponding parameter values. It has the characteristics of automation, low cost, and easy understanding.
在一种可能的实施方式中,本发明实施例还提供一种可读存储介质,可读存储介质位于PLC(Programmable Logic Controller,可编程逻辑控制器)控制器中,可读存储介质上存储有计算机程序,该程序被处理器执行时实现以下所述一种代码图片的可视化展示实现方法的步骤:In a possible implementation, the embodiment of the present invention also provides a readable storage medium, the readable storage medium is located in the PLC (Programmable Logic Controller, Programmable Logic Controller) controller, and the readable storage medium stores the A computer program, when the program is executed by a processor, implements the following steps of a visual display method for a code picture:
基于光学字符识别方式识别得到代码图片中的代码,并对识别得到的代码进行纠错处理;Recognize the code in the code picture based on the optical character recognition method, and perform error correction processing on the recognized code;
基于代码格式化工具对代码进行格式化处理,并采用绘图工具将格式化后的代码制作成代码图片;Format the code based on the code formatting tool, and use the drawing tool to make the formatted code into a code picture;
对格式化后的代码进行插桩处理,并采用测试用例产生工具对插桩后的代码产生测试用例;Perform instrumentation on the formatted code, and use the test case generation tool to generate test cases for the instrumented code;
运行测试用例,并采用特定显示方式对代码运行顺序进行展示,同时展示代码运行阶段的数值信息。Run the test case, and use a specific display method to display the code running sequence, and at the same time display the numerical information of the code running stage.
存储介质可以采用一个或多个计算机可读的介质的任意组合。计算机可读介质可以是计算机可读信号介质或者计算机可读存储介质。计算机可读存储介质例如可以是但不限于:电、磁、光、电磁、红外线、或半导体的系统、装置或器件,或者任意以上的组合。计算机可读存储介质的更具体的例子(非穷举的列表)包括:具有一个或多个导线的电连接、便携式计算机磁盘、硬盘、随机存取存储器(RAM)、只读存储器(ROM)、可擦式可编程只读存储器(EPROM或闪存)、光纤、便携式紧凑磁盘只读存储器(CD-ROM)、光存储器件、磁存储器件、或者上述的任意合适的组合。在本文件中,计算机可读存储介质可以是任何包含或存储程序的有形介质,该程序可以被指令执行系统、装置或者器件使用或者与其结合使用。The storage medium may employ any combination of one or more computer-readable media. The computer readable medium may be a computer readable signal medium or a computer readable storage medium. A computer-readable storage medium may be, for example but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or device, or any combination thereof. More specific examples (non-exhaustive list) of computer readable storage media include: electrical connections with one or more leads, portable computer disks, hard disks, random access memory (RAM), read only memory (ROM), Erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination of the above. In this document, a computer-readable storage medium may be any tangible medium that contains or stores a program that can be used by or in conjunction with an instruction execution system, apparatus, or device.
计算机可读的信号介质可以包括在基带中或者作为载波一部分传播的数据信号,其中承载了计算机可读的程序代码。这种传播的数据信号可以采用多种形式,包括但不限于电磁信号、光信号或上述的任意合适的组合。计算机可读的信号介质还可以是计算机可读存储介质以外的任何计算机可读介质,该计算机可读介质可以发送、传播或者传输用于由指令执行系统、装置或者器件使用或者与其结合使用的程序。计算机可读介质上包含的程序代码可以用任何适当的介质传输,包括但不限于:无线、电线、光缆、RF等等,或者上述的任意合适的组合。A computer readable signal medium may include a data signal carrying computer readable program code in baseband or as part of a carrier wave. Such propagated data signals may take many forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the foregoing. A computer-readable signal medium may also be any computer-readable medium other than a computer-readable storage medium, which can send, propagate, or transmit a program for use by or in conjunction with an instruction execution system, apparatus, or device. . Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., or any suitable combination of the foregoing.
可以以一种或多种程序设计语言或其组合来编写用于执行本发明操作的计算机程序代码,所述程序设计语言包括面向对象的程序设计语言,诸如Java、Smalltalk、C++,还包括常规的过程式程序设计语言—诸如“C”语言或类似的程序设计语言。程序代码可以完全地在用户计算机上执行、部分地在用户计算机上执行、作为一个独立的软件包执行、部分在用户计算机上部分在远程计算机上执行、或者完全在远程计算机或服务器上执行。在涉及远程计算机的情形中,远程计算机可以通过任意种类的网络,包括局域网(LAN)或广域网(WAN),连接到用户计算机,或者,可以连接到外部计算机(例如利用因特网服务提供商来通过因特网连接)。Computer program code for carrying out the operations of the present invention may be written in one or more programming languages, or combinations thereof, including object-oriented programming languages, such as Java, Smalltalk, C++, and conventional Procedural Programming Language - such as "C" or a similar programming language. The program code may execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer or entirely on the remote computer or server. In cases involving a remote computer, the remote computer can be connected to the user computer through any kind of network, including a local area network (LAN) or a wide area network (WAN), or it can be connected to an external computer (such as through the Internet using an Internet service provider). connect).
本发明实施例提供的一种代码图片的可视化展示实现装置,包括识别模块、第一处理模块、第二处理模块和执行模块。An embodiment of the present invention provides an apparatus for realizing a visual display of a code picture, including an identification module, a first processing module, a second processing module, and an execution module.
识别模块用于基于光学字符识别方式识别得到代码图片中的代码,并对识别得到的代码进行纠错处理;第一处理模块用于基于代码格式化工具对代码进行格式化处理,并采用绘图工具将格式化后的代码制作成代码图片;第二处理模块用于对格式化后的代码进行插桩处理,并采用测试用例产生工具对插桩后的代码产生测试用例;执行模块用于运行测试用例,并采用特定显示方式对代码运行顺序进行展示,同时展示代码运行阶段的数值信息。The recognition module is used to recognize the code in the code picture based on the optical character recognition method, and perform error correction processing on the recognized code; the first processing module is used to format the code based on the code formatting tool, and use the drawing tool Make the formatted code into a code picture; the second processing module is used to insert the formatted code, and use the test case generation tool to generate test cases for the inserted code; the execution module is used to run the test Use cases, and use a specific display method to display the code running sequence, and at the same time display the numerical information of the code running phase.
以上所述仅是本申请的具体实施方式,使本领域技术人员能够理解或实现本申请。对这些实施例的多种修改对本领域的技术人员来说将是显而易见的,本文中所定义的一般原理可以在不脱离本申请的精神或范围的情况下,在其它实施例中实现。因此,本申请将不会被限制于本文所示的这些实施例,而是要符合与本文所申请的原理和新颖特点相一致的最宽的范围。The above descriptions are only specific implementation manners of the present application, so that those skilled in the art can understand or implement the present application. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the application. Therefore, the present application will not be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features claimed herein.
本发明是参照根据本发明实施例的方法、设备(系统)和计算机程序产品的流程图和/或方框图来描述的。应理解可由计算机程序指令实现流程图和/或方框图中的每一流程和/或方框、以及流程图和/或方框图中的流程和/或方框的结合。可提供这些计算机程序指令到通用计算机、专用计算机、嵌入式处理机或其他可编程数据处理设备的处理器以产生一个机器,使得通过计算机或其他可编程数据处理设备的处理器执行的指令产生用于实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能的装置。The present invention is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems) and computer program products according to embodiments of the invention. It should be understood that each procedure and/or block in the flowchart and/or block diagram, and a combination of procedures and/or blocks in the flowchart and/or block diagram can be realized by computer program instructions. These computer program instructions may be provided to a general purpose computer, special purpose computer, embedded processor, or processor of other programmable data processing equipment to produce a machine such that the instructions executed by the processor of the computer or other programmable data processing equipment produce a An apparatus for realizing the functions specified in one or more procedures of the flowchart and/or one or more blocks of the block diagram.
Claims (10)
Priority Applications (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202310282197.1A CN116363256A (en) | 2023-03-17 | 2023-03-17 | Method and device for realizing visual display of code pictures |
Applications Claiming Priority (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202310282197.1A CN116363256A (en) | 2023-03-17 | 2023-03-17 | Method and device for realizing visual display of code pictures |
Publications (1)
| Publication Number | Publication Date |
|---|---|
| CN116363256A true CN116363256A (en) | 2023-06-30 |
Family
ID=86941310
Family Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| CN202310282197.1A Pending CN116363256A (en) | 2023-03-17 | 2023-03-17 | Method and device for realizing visual display of code pictures |
Country Status (1)
| Country | Link |
|---|---|
| CN (1) | CN116363256A (en) |
-
2023
- 2023-03-17 CN CN202310282197.1A patent/CN116363256A/en active Pending
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| US10929449B2 (en) | Generating a structured document guiding view | |
| US8434062B2 (en) | Enhancing source code debugging and readability using visual symbols | |
| US7266809B2 (en) | Software debugger and software development support system for microcomputer operable to execute conditional execution instruction | |
| CN106502896B (en) | Method and device for generating function test code | |
| KR101076867B1 (en) | Automatic Image Capture to Generate Content | |
| CN111142988B (en) | Page testing method, device, storage medium and electronic equipment | |
| US10635434B2 (en) | Annotating generator output stream with source code information of the generator | |
| CN108170611A (en) | Automated testing method and device, storage medium, electronic equipment | |
| US20060277523A1 (en) | Annotations for tracking provenance | |
| JP6440895B2 (en) | Software analysis apparatus and software analysis method | |
| CN115857926A (en) | A method and device for constructing front-end code engineering based on low-code applications | |
| KR20140116438A (en) | Graphical representation of an order of operations | |
| CN102214142A (en) | Instrumentation method for traceless manageable source code manually-defined mark | |
| CN116643755A (en) | Code generation method, code generation device, visualization platform and electronic equipment | |
| Kumar et al. | Code-Viz: data structure specific visualization and animation tool for user-provided code | |
| CN116108781A (en) | A co-lay part inspection method, device, equipment and medium | |
| CN115617353A (en) | Method for generating description file based on ksp and rapid instrumentation gradle compiling system | |
| CN116363256A (en) | Method and device for realizing visual display of code pictures | |
| CN118760613A (en) | Test case automatic generation method, device, electronic device and storage medium | |
| CN106469050B (en) | A use case generation method, page generation method and device | |
| CN115629828A (en) | Display method, device and medium of a state machine | |
| CN111078529A (en) | Client write-in module testing method and device and electronic equipment | |
| CN114429806A (en) | A medical data testing method, device, storage medium and equipment | |
| CN113220596B (en) | Application testing method, device, equipment, storage medium and program product | |
| CN111708572A (en) | An automatic generation method of control flow chart based on Clang program structure |
Legal Events
| Date | Code | Title | Description |
|---|---|---|---|
| PB01 | Publication | ||
| PB01 | Publication | ||
| SE01 | Entry into force of request for substantive examination | ||
| SE01 | Entry into force of request for substantive examination |
