Code generation method and device, electronic equipment, storage medium and program product
By detecting and merging view components in the interface design images and establishing a view tree mapping relationship, the reusability and usability issues when converting interface design images into code are solved, generating reusable code.
Patent Information
- Application Number
- CN202510645242.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-19
- Publication Date
- 2026-11-20
AI Technical Summary
In existing technologies, when interface design images are converted into code, the generated code has low reusability and usability, and fails to effectively and reasonably reuse similar user interface parts.
By detecting view components in the interface design images, the view tree is determined, and the view tree groups are merged based on similarity to establish a mapping relationship between the template view tree and the view tree, generating executable code.
It improves code reusability and usability, ensuring that each element in a view component corresponds one-to-one with the template view tree, generating reusable code.
Smart Images

Figure CN123137648A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a code generation method, apparatus, electronic device, storage medium, and program product. Background Technology
[0002] Interface design images (also known as visual drafts) are used in the development of an application's interface, which involves the process of converting interface design images into executable code.
[0003] In related technologies, when using Design to Code (D2C) to generate code, elements in the visual artwork are typically mapped one by one to code. However, this generation method results in code that is merely a simple list, and similar user interface (UI) parts are not reused properly, leading to low reusability and usability of the generated code. Summary of the Invention
[0004] This application provides a code generation method, apparatus, electronic device, storage medium, and program product that can generate reusable code and improve code usability.
[0005] The technical solution of this application embodiment is implemented as follows:
[0006] This application provides a code generation method, the method comprising:
[0007] The interface design image of the application interface is subjected to view component detection to obtain multiple view components in the interface design image, and the view tree of each view component is determined.
[0008] The multiple view trees are grouped to obtain multiple view tree groups;
[0009] Based on the similarity between the view trees in every two view tree groups, the multiple view tree groups are merged to obtain a template view tree;
[0010] Determine the mapping relationship between the first node in the template view tree and each second node in the view tree, wherein the first node represents an element in the view component corresponding to the template view tree, and the second node represents an element in the view component corresponding to the view tree;
[0011] Based on the template view tree and the mapping relationship, the executable code for the application interface is generated.
[0012] This application provides a code generation apparatus, comprising: a determining module, configured to perform view component detection on an interface design image of an application interface to obtain multiple view components in the interface design image, and determine a view tree for each view component; a grouping module, configured to group the multiple view trees to obtain multiple view tree groups; a merging module, configured to merge the multiple view tree groups based on the similarity between view trees in every two view tree groups to obtain a template view tree; a mapping module, configured to determine the mapping relationship between a first node in the template view tree and a second node in each view tree, wherein the first node represents an element in the view component corresponding to the template view tree, and the second node represents an element in the view component corresponding to the view tree; and a generation module, configured to generate executable code for the application interface based on the template view tree and the mapping relationship.
[0013] This application provides an electronic device, the electronic device comprising:
[0014] Memory is used to store executable instructions or computer programs.
[0015] The processor, when executing computer-executable instructions or computer programs stored in the memory, implements the code generation method provided in the embodiments of this application.
[0016] This application provides a computer-readable storage medium storing a computer program or computer-executable instructions, which, when executed by a processor, implements the code generation method provided in this application.
[0017] This application provides a computer program product, including a computer program or computer executable instructions, which, when executed by a processor, implements the code generation method provided in this application.
[0018] The embodiments of this application have the following beneficial effects:
[0019] After detecting multiple view components in the interface design image and determining the view tree for each view component, the view tree groups are grouped and merged based on similarity. This effectively identifies view components with similar structures in the interface design image and abstracts these similar view components into reusable template view trees. By establishing a mapping relationship between the first node in the template view tree and the second node in the view tree, it is ensured that each element in the view component corresponds one-to-one with the code generated using the template view tree, thereby generating reusable code and improving code usability. Attached Figure Description
[0020] Figure 1This is a schematic diagram of the architecture of the code generation system provided in the embodiments of this application;
[0021] Figure 2 This is a schematic diagram of the structure of the electronic device provided in the embodiments of this application;
[0022] Figure 3 This is a flowchart illustrating the code generation method provided in the embodiments of this application. Figure 1 ;
[0023] Figure 4 This is a schematic diagram of the interface design of the communication application provided in the embodiments of this application;
[0024] Figure 5 These are schematic diagrams of the interface design of the business application provided in the embodiments of this application;
[0025] Figure 6 This is a schematic diagram of multiple view trees of the interface design images provided in the embodiments of this application;
[0026] Figure 7 This is a flowchart illustrating the code generation method provided in the embodiments of this application. Figure 2 ;
[0027] Figure 8 This is a flowchart illustrating the code generation method provided in the embodiments of this application. Figure 3 ;
[0028] Figure 9 This is a flowchart illustrating the code generation method provided in the embodiments of this application. Figure 4 ;
[0029] Figure 10 This is a flowchart illustrating the code generation method provided in the embodiments of this application. Figure 5 ;
[0030] Figure 11 This is a flowchart illustrating the code generation method provided in the embodiments of this application. Figure 6 ;
[0031] Figure 12 This is a flowchart illustrating the code generation method provided in the embodiments of this application. Figure 7 ;
[0032] Figure 13 This is another flowchart illustrating the code generation method provided in the embodiments of this application;
[0033] Figure 14 This is a schematic diagram of the code file generated without merging the list items, provided in an embodiment of this application.
[0034] Figure 15 This is a schematic diagram of the interface of the D2C plugin provided in the embodiments of this application;
[0035] Figure 16 These are schematic diagrams of the original visual draft and the revised visual draft provided in the embodiments of this application;
[0036] Figure 17 This is a schematic diagram of the preview interface of the list code and application interface provided in the embodiments of this application;
[0037] Figure 18 This is a schematic diagram of the overall process of the code generation method provided in the embodiments of this application;
[0038] Figure 19 This is a schematic diagram of list item grouping provided in an embodiment of this application;
[0039] Figure 20 This is a schematic diagram of list item merging provided in an embodiment of this application;
[0040] Figure 21 This is a schematic diagram of the UI view of the list items merged to generate a large template provided in the embodiments of this application;
[0041] Figure 22 This is a schematic diagram of structural inspection and repair provided in an embodiment of this application;
[0042] Figure 23 This is a schematic diagram of the mind map provided in the embodiments of this application;
[0043] Figure 24 This is a flowchart of the plug-in side provided in the embodiments of this application;
[0044] Figure 25 This is a schematic diagram of the element merging process provided in the embodiments of this application;
[0045] Figure 26 This is a schematic diagram illustrating the determination of whether nodes are similar, provided in an embodiment of this application.
[0046] Figure 27 This is a flowchart illustrating the code generation process provided in an embodiment of this application;
[0047] Figure 28 This is a schematic diagram of attribute mapping and merging in the list items provided in the embodiments of this application;
[0048] Figure 29 This is a schematic diagram of a code file provided in an embodiment of this application that does not involve merging similar list items;
[0049] Figure 30 This is a schematic diagram of the code file for merging similar list items provided in an embodiment of this application;
[0050] Figure 31 This is a schematic diagram of the merged template view tree provided in the embodiments of this application;
[0051] Figure 32 This is a schematic diagram of the merged code file provided in the embodiments of this application. Detailed Implementation
[0052] To make the objectives, technical solutions, and advantages of this application clearer, the application will be further described in detail below with reference to the accompanying drawings. The described embodiments should not be regarded as limitations on this application. All other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0053] In the following description, references are made to “some embodiments,” which describe a subset of all possible embodiments. However, it is understood that “some embodiments” may be the same subset or different subsets of all possible embodiments and may be combined with each other without conflict.
[0054] In the following description, the terms "first, second, third" are used merely to distinguish similar objects and do not represent a specific ordering of objects. It is understood that "first, second, third" may be interchanged in a specific order or sequence where permitted, so that the embodiments of this application described herein can be implemented in an order other than that illustrated or described herein.
[0055] In this application embodiment, the terms "module" or "unit" refer to a computer program or part of a computer program that has a predetermined function and works with other related parts to achieve a predetermined goal, and can be implemented wholly or partially using software, hardware (such as processing circuitry or memory), or a combination thereof. Similarly, a processor (or multiple processors or memory) can be used to implement one or more modules or units. Furthermore, each module or unit can be part of an overall module or unit that includes the functionality of that module or unit.
[0056] Unless otherwise defined, all technical and scientific terms used in the embodiments of this application have the same meaning as commonly understood by one of ordinary skill in the art. The terminology used in the embodiments of this application is for the purpose of describing the embodiments of this application only and is not intended to limit this application.
[0057] In the implementation of this application, the collection and processing of relevant data should strictly comply with the requirements of relevant laws and regulations, obtain the informed consent or separate consent of the personal information subject, and carry out subsequent data use and processing within the scope of laws and regulations and the authorization of the personal information subject.
[0058] Before providing a further detailed description of the embodiments of this application, the nouns and terms involved in the embodiments of this application will be explained, and the nouns and terms involved in the embodiments of this application shall be interpreted as follows.
[0059] 1) Application Interface: This is the graphical user interface that users directly interact with in an application, such as a music player interface or a communication window.
[0060] 2) Interface Design Images: Also known as visual drafts, these are files created by designers during application or website development to showcase the appearance and interactive details of the user interface. Interface design images include all visual elements of the application interface (such as colors, fonts, icons, button styles, etc.), as well as element layout and element types. Interface design images may also include specific parameters such as size, spacing, and hierarchy, and can describe interactive logic (such as click events, animation effects, etc.) through annotations or explanations.
[0061] 3) View components: These are container-type UI units in the application interface. In this application, they may specifically refer to UI units with repeating layout features, such as list items (single items in a vertical or horizontal scrolling list, such as product list items or communication channels), grids (containers arranged in a grid, such as each cell in a nine-square grid), etc.
[0062] 4) View Tree: This is the hierarchical structure of view components, consisting of multiple nodes. Each node represents an element within the view component corresponding to the view tree. For example, the root node of the view tree is usually the frame of the view component, which is the basic container of the view component. The structure of the view tree reflects the stacking order and logical grouping of elements within the view components.
[0063] 5) Tree edit distance: This is a metric used to measure the similarity between two view trees. Specifically, it is defined as the minimum number of edit operations required to transform one view tree into another.
[0064] 6) Elements: The constituent units within a view component, such as images, text, borders, components, etc.
[0065] 7) Executable code: Programming language files that can be translated into machine instructions by a compiler.
[0066] 8) YOLO model (You Only Look Once): A computer vision algorithm used for object detection tasks.
[0067] 9) Design to Code (D2C): Converting visual designs provided by designers into executable code for development.
[0068] 10) Figma: A vector graphics editor and prototyping tool. Figma elements are various graphics, text, layouts, and other visual building blocks created and used in the Figma design tool. These elements together constitute the components that designers use to express interface designs, including shapes, text, images, frames, components, etc.
[0069] 11) Model-View-ViewModel (MVVM): A software architecture design pattern used to decompose an application interface into a view layer (View), a model layer (Model), and a viewmodel layer (ViewModel). The View is the user interface, responsible for displaying data and interacting with the user; the Model represents the application's data and business logic, handling data retrieval, storage, and updates, such as retrieving data from a database or requesting data via the network; the ViewModel is the intermediary layer connecting the Model and the View, obtaining data from the Model and converting it into a format that the View can understand and use.
[0070] 12) UI file: The interface template code file corresponding to the view layer.
[0071] 13) Model file: The code file corresponding to the model layer, used to define the business data structure.
[0072] 14) ViewModel file: The code file corresponding to the view model layer. In the MVVM architecture, it is the intermediate layer code file used to implement two-way binding between data and the user interface.
[0073] This application provides code generation methods, apparatus, electronic devices, storage media, and program products, capable of generating reusable code and improving code usability. The following describes exemplary applications of the electronic devices provided in this application. These electronic devices can be implemented as various types of terminals such as laptops, tablets, desktop computers, set-top boxes, smartphones, smart speakers, smartwatches, smart TVs, and in-vehicle terminals, or as servers. Exemplary applications of the electronic devices as terminals or servers will be described below.
[0074] See Figure 1 , Figure 1 This is a schematic diagram of the architecture of the code generation system provided in this application embodiment. To support a code generation application, the code generation system 100 includes at least a terminal 400, a network 300, and a server 200. The terminal 400 is connected to the server 200 through the network 300, which can be a wide area network (WAN), a local area network (LAN), or a combination of both.
[0075] In some embodiments, the embodiments of this application can be implemented by the terminal 400 alone. For example, after the user completes the interface design image of the application interface through the terminal 400, the terminal 400 performs view component detection on the interface design image of the application interface to obtain multiple view components in the interface design image, and determines the view tree of each view component; the terminal 400 groups the multiple view trees to obtain multiple view tree groups; the terminal 400 merges the multiple view tree groups based on the similarity between the view trees in every two view tree groups to obtain a template view tree; the terminal 400 determines the mapping relationship between the first node in the template view tree and the second node in each view tree, wherein the first node represents an element in the view component corresponding to the template view tree, and the second node represents an element in the view component corresponding to the view tree; the terminal 400 generates executable code for the application interface based on the template view tree and the mapping relationship.
[0076] In other embodiments, the embodiments of this application can be implemented collaboratively by server 200 and terminal 400. For example, after the user completes the interface design image of the application interface through terminal 400, terminal 400 performs view component detection on the interface design image of the application interface to obtain multiple view components in the interface design image and determines the view tree of each view component; terminal 400 groups the multiple view trees to obtain multiple view tree groups; terminal 400 merges the multiple view tree groups based on the similarity between the view trees in each pair of view tree groups to obtain a template view tree; terminal 400 determines the mapping relationship between the first node in the template view tree and the second node in each view tree, wherein the first node represents an element in the view component corresponding to the template view tree, and the second node represents an element in the view component corresponding to the view tree; terminal 400 encapsulates the template view tree and the mapping relationship into a code generation request and sends the code generation request to server 200. In response to the received code generation request, server 200 generates executable code for the application interface based on the template view tree and the mapping relationship. Server 200 sends the executable code file to terminal 400.
[0077] The code generation method provided in this application can be applied to any scenario where executable code for an application interface is generated, generating reusable code and improving code usability. Specific application scenarios include:
[0078] 1) Dynamic generation of product detail pages in mobile application development. Users upload the visual design of the product detail page for an e-commerce app (including product image grids and specification selection list items). The terminal automatically generates a view tree of the grids and list items. The view trees of similar specification selection list items (such as color and size) are grouped and merged to generate a reusable template view tree. By establishing a mapping relationship between the nodes in the template view tree and the nodes in the specification selection list item view tree, the executable code of the product detail page containing the mapping relationship is finally output.
[0079] 2) News List Page Generation. Users input a Figma design draft for the news list page (including text and image list items) via a terminal. The terminal uploads the Figma design draft to the server. The server (e.g., a cloud compilation platform) parses the Figma design draft to obtain view trees for multiple list items. It identifies the layout differences of list items for different news types (text or video), combines view trees with a similarity >85%, and generates a template view tree. By establishing a mapping relationship between nodes in the template view tree and nodes in the list item view trees, it finally outputs executable code for the news list page containing the mapping relationships.
[0080] 3) Dynamic interface generation in the game UI editor. Users drag and drop to build the inventory grid interface (including equipment icons and durability progress bars) in the game editor. The system detects the nesting level of the view tree within the grid in real time, abstracts the relative layout of equipment icons and durability progress bars into a template view tree, and automatically generates an executable script for the inventory grid interface according to the mapping rules.
[0081] 4) Channel page design for the communication application. The user provides a visual draft of the channel page for the communication application. The terminal detects multiple communication channel list items in the visual draft and determines the view tree for each communication channel list item. The view trees of each communication channel list item are compared for similarity and merged to generate a template view tree. By establishing a mapping relationship between the nodes in the template view tree and the nodes in the view trees of each communication channel list item, the executable code of the news list page containing the mapping relationship is finally output.
[0082] In some embodiments, the electronic device implementing the code generation method provided in this application may be Figure 1 Terminal 400 in the middle. See also Figure 2 , Figure 2 This is a schematic diagram of the structure of the electronic device provided in the embodiments of this application. Figure 2The illustrated electronic device includes at least one processor 410, a memory 450, at least one network interface 420, and a user interface 430. The various components of the electronic device are coupled together via a bus system 440. It is understood that the bus system 440 is used to implement communication between these components. In addition to a data bus, the bus system 440 also includes a power bus, a control bus, and a status signal bus. However, for clarity, in… Figure 2 The general labeled all buses as Bus System 440.
[0083] The processor 410 can be an integrated circuit chip with signal processing capabilities, such as a general-purpose processor, a digital signal processor (DSP), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor can be a microprocessor or any conventional processor, etc.
[0084] User interface 430 includes one or more output devices 431 that enable the presentation of media content, including one or more speakers and / or one or more visual displays. User interface 430 also includes one or more input devices 432, including user interface components that facilitate user input, such as a keyboard, mouse, microphone, touch screen display, camera, other input buttons and controls.
[0085] The memory 450 may be removable, non-removable, or a combination thereof. Exemplary hardware devices include solid-state storage, hard disk drives, optical disk drives, etc. The memory 450 may optionally include one or more storage devices physically located away from the processor 410.
[0086] The memory 450 may include volatile memory or non-volatile memory, or both. The non-volatile memory may be read-only memory (ROM), and the volatile memory may be random access memory (RAM). The memory 450 described in this application embodiment is intended to include any suitable type of memory.
[0087] In some embodiments, memory 450 is capable of storing data to support various operations, examples of which include programs, modules, and data structures or subsets or supersets thereof, as illustrated below.
[0088] Operating system 451 includes system programs for handling various basic system services and performing hardware-related tasks, such as the framework layer, core library layer, driver layer, etc., for implementing various basic business functions and handling hardware-based tasks;
[0089] The network communication module 452 is used to reach other electronic devices via one or more (wired or wireless) network interfaces 420, exemplary network interfaces 420 including: Bluetooth, WiFi, and Universal Serial Bus (USB), etc.
[0090] Presentation module 453 is configured to enable the presentation of information (e.g., a user interface for operating peripheral devices and displaying content and information) via one or more output devices 431 (e.g., a display screen, a speaker, etc.) associated with user interface 430;
[0091] The input processing module 454 is used to detect and translate one or more user inputs or interactions from one or more input devices 432.
[0092] In some embodiments, the apparatus provided in this application can be implemented in software. Figure 2 A code generation apparatus 455 stored in memory 450 is shown. This apparatus can be software in the form of programs and plug-ins, and includes the following software modules: a determination module 4551, a grouping module 4552, a merging module 4553, a mapping module 4554, and a generation module 4555. These modules are logically connected and can therefore be arbitrarily combined or further divided according to their implemented functions. The functions of each module will be described below.
[0093] In other embodiments, the apparatus provided in this application can be implemented in hardware. As an example, the apparatus provided in this application can be a processor in the form of a hardware decoding processor, which is programmed to execute the code generation method provided in this application. For example, the processor in the form of a hardware decoding processor can be one or more application-specific integrated circuits (ASICs), digital signal processors (DSPs), programmable logic devices (PLDs), complex programmable logic devices (CPLDs), field-programmable gate arrays (FPGAs), or other electronic components.
[0094] In some embodiments, the terminal or server can implement the code generation method provided in this application by running various computer-executable instructions or computer programs. For example, computer-executable instructions can be microprogram-level commands, machine instructions, or software instructions. Computer programs can be native programs or software modules in an operating system; they can be native applications (APPs), i.e., programs that need to be installed in the operating system to run, which can provide offline services, can be used without relying on the network, and usually have richer functions and a smoother user experience, such as visual draft to code conversion APPs; or they can be mini-programs that can be embedded in any APP, i.e., programs that only need to be downloaded to a browser environment to run, such as code generation software embedded in a visual draft repair APP, or online code generation websites. In summary, the above-mentioned computer-executable instructions can be any form of instruction, and the above-mentioned computer programs can be any form of application, module, or plugin.
[0095] The code generation method provided in the embodiments of this application will be described below. As mentioned above, the electronic device implementing the code generation method of the embodiments of this application can be a terminal, a server, or a combination of both. Therefore, the executing entity of each step will not be described again below.
[0096] The code generation method provided in this application will be described in conjunction with exemplary applications and implementations of the terminals provided in the embodiments of this application.
[0097] See Figure 3 , Figure 3 This is a flowchart illustrating the code generation method provided in the embodiments of this application. Figure 1 , will combine Figure 3 The steps shown are explained as follows: Figure 3 As shown, taking the terminal as the execution subject of the code generation method as an example, the method includes the following steps 101 to 105:
[0098] In step 101, view component detection is performed on the interface design image of the application interface to obtain multiple view components in the interface design image, and the view tree of each view component is determined.
[0099] It should be noted that the code generation method provided in this application embodiment is used to: generate executable code corresponding to the application interface based on the interface design image of the application interface; when the executable code is executed, the application interface is generated. Therefore, in step 101, firstly, the interface design image of the application interface to be generated is obtained, and this interface design image corresponds to an application; the application can be any application, such as a mobile application (e.g., a smartphone application), a web application, a computer application, etc. The interface design image includes the design information of the view components in the application interface.
[0100] Interface design images (also known as visual drafts) are graphical representations used to showcase and convey design intent during software development. Interface design images often serve as a bridge between the design and development teams, enabling developers without a design background to understand and implement the designer's ideas. The following are some characteristics and components of interface design images: 1) Characteristics: a) Graphical: Interface design images are presented in graphic form, typically including visual elements such as layout, color, fonts, and icons; b) Interactivity: Contains interactive elements such as buttons and links to demonstrate possible user interactions with the interface; c) Static or Dynamic: Can be static or dynamic images, the latter showcasing transitions and animations between interfaces. 2) Components: a) Layout: Shows the arrangement and partitioning of elements in the design, including the page header, footer, sidebar, and content areas. b) Color: Define the color scheme in the design, including primary color, secondary color, background color, etc.; c) Font and Typography: Specify the font type, size, line spacing, and paragraph spacing used in the design; d) Icons and Graphics: Include all icons, images, and graphic elements used in the design; e) View Components: User-interactive elements such as list items, grids, buttons, input boxes, and drop-down menus; f) States and Variations: Show the appearance of elements in different states, such as normal, hover, disabled, etc.; etc.
[0101] In this embodiment, the view component can be a component with repeating layout features in the interface design image, such as list items or grids. A list item is a single entry in a vertically or horizontally scrolling list, such as a product list item or a communication channel. A grid is a container arranged in a grid format.
[0102] For example, Figure 4 This is a schematic diagram of the interface design of a communication application provided in an embodiment of this application. The application is a communication application, and the application interface is the communication channel interface of the communication application. The interface design image is a visual draft of the communication channel interface designed by the user using the Figma tool. See also Figure 4The interface design image displays "My Channels," which includes six communication channels: Channel 1, Channel 2, Channel 3, Channel 4, Channel 5, and Channel 6. The six view components included in the interface design image are the list items of these six communication channels.
[0103] Figure 5 This is a schematic diagram of the interface design of a business application provided in this application embodiment. The application is a business application, and the application interface is the business display interface of the business application. The interface design image is a visual draft of the business display interface designed by the user using the Figma tool. See also Figure 5 The interface design image displays "Business," which includes six business functions displayed in a grid: Business 1, Business 2, Business 3, Business 4, Business 5, and Business 6. The six view components included in the interface design image represent the grid of these six business functions.
[0104] After acquiring the interface design image, it can be divided into regions using an image segmentation algorithm to obtain view component regions. An object detection model (e.g., the YOLO model) is then used to detect view components within these regions, yielding the object detection results. These results can include the type and position of each view component in the interface design image. The position indicates the coordinate information of the view component's border. For example, the position of a view component can be (x, y, width, height), where (x, y) are the x and y coordinates of a point within the border (e.g., the center point or the top-left corner), width is the width of the border, and height is the height of the border.
[0105] After obtaining multiple view components, the view tree for each view component is determined. A view tree is a tree-like data structure that describes the structure of a view component. It includes multiple node levels, with each level containing at least one node. Each node represents an element within the view component (e.g., text, image, etc.). The node levels in the view tree represent the hierarchical relationship (e.g., nesting) between elements within the view component.
[0106] In some embodiments, after obtaining the user-provided application interface design image, a pre-processing method can be used based on a D2C plugin to obtain a repaired interface design image. Pre-processing includes steps such as removing invalid nodes, intelligent image merging, simplification and hierarchical re-division, implementing adaptive element layout, and semantic naming. Object detection can be performed on the repaired interface design image to obtain multiple view components in the view component area. The interface design image is then converted into an interface view tree, which includes multiple subtrees, each containing a view tree for a view component. Each generated view tree for a view component can be labeled; only after labeling is the resulting view tree used as the view tree for that view component.
[0107] For example, Figure 6 This is a schematic diagram of multiple view trees of the interface design images provided in the embodiments of this application. See also Figure 6 The interface design image 601 corresponds to the interface view tree 602. The view component area 604 in the interface design image 601 includes six list items (view components): Channel 1, Channel 2, Channel 3, Channel 4, Channel 5, and Channel 6. The subtree 605 in the interface view tree 602 includes view tree 1 corresponding to Channel 1, view tree 2 corresponding to Channel 2, view tree 3 corresponding to Channel 3, view tree 4 corresponding to Channel 4, view tree 5 corresponding to Channel 5, and view tree 6 corresponding to Channel 6. The view tree for each list item is labeled [c:cell].
[0108] In step 102, multiple view trees are grouped to obtain multiple view tree groups.
[0109] Here, the similarity between any two view trees can be calculated, and multiple view trees can be grouped based on the similarity to obtain multiple view tree groups. Alternatively, containment relationship matching can be performed on multiple view trees to obtain matching results, and the multiple view trees can be grouped based on the matching results to obtain multiple view tree groups. Each view tree group contains at least one view tree.
[0110] In some embodiments, see Figure 7 , Figure 7 The step 102, which involves grouping multiple view trees to obtain multiple view tree groups, can be achieved through the following steps 1021A to 1022A:
[0111] In step 1021A, a preset first similarity threshold is obtained.
[0112] Here, the first similarity threshold is a preset value used to measure whether two view trees are sufficiently similar. This application does not limit the first similarity threshold; it can be set according to actual needs. For example, multiple similarity thresholds for historical groups can be obtained, and the average of these multiple similarity thresholds can be used as the first similarity threshold.
[0113] In step 1022A, when the similarity between any two view trees is greater than or equal to the first similarity threshold, the two view trees are assigned to the same view tree group.
[0114] Here, the similarity between any two view trees can be determined. Each view tree is treated as an independent view tree group. The similarity between any two view trees is compared one by one to see if it is greater than or equal to a first similarity threshold. If the similarity between a pair of view trees is greater than or equal to the first similarity threshold, the view tree group containing that pair is merged. This process is repeated for the remaining view trees until all view trees are assigned to appropriate groups.
[0115] For example, if the similarity between view tree T1 and view tree T2 is 0.95 and the first similarity threshold is 0.9, then the similarity between view tree T1 and view tree T2 is greater than the first similarity threshold, and view tree T1 and view tree T2 are assigned to view tree group A. If the similarity between view tree T1 and view tree T3 is 0.96, then the similarity between view tree T1 and view tree T3 is greater than the first similarity threshold, and view tree T3 is assigned to view tree group A. If the similarity between view tree T4 and every other view tree is less than the first similarity threshold, then view tree T4 is assigned to a view tree group B.
[0116] This application embodiment clarifies the quantitative basis for view tree grouping by setting a first similarity threshold, avoiding the uncertainty of subjective judgment. Different application scenarios may require different similarity requirements. For example, in interface design, a higher similarity threshold can ensure the accuracy of grouping; while in performance optimization, a lower similarity threshold can expand the scope of reuse. By grouping similar view trees together, the management of a large number of view trees can be simplified, reducing complexity. Grouped view trees are easier to operate on in batches (such as uniformly updating styles, states, or interactive behaviors), improving code maintainability.
[0117] In some embodiments, see Figure 8 Before step 102, the code generation method provided in this application embodiment can also determine the similarity between any two view trees through the following steps 201 to 204:
[0118] In step 201, two view trees are arbitrarily selected from the multiple view trees.
[0119] Here, two view trees are randomly or sequentially selected from all the view trees to be processed to calculate similarity.
[0120] For example, the interface design image includes six view components: list items for Channel 1, Channel 2, Channel 3, Channel 4, Channel 5, and Channel 6, which correspond to view trees T1, T2, T3, T4, T5, and T6, respectively. Two view trees, T1 and T2, are randomly selected from these six view trees.
[0121] In step 202, the tree edit distance between the two view trees is determined.
[0122] Here, tree edit distance represents the minimum number of operations required to transform one view tree into another. A smaller tree edit distance indicates that the structures of the two view trees are more similar. Editing operations that transform one view tree into another can include insertion, deletion, and replacement operations. It's important to note that each editing operation operates on a node within the view tree; for example, insertion adds a node to the view tree, and deletion removes a node. Therefore, both insertion and deletion operations require 1 operation. Replacement operations include modifying the content or attributes of nodes (e.g., label type, text content, text style).
[0123] The tree edit distance between two view trees can be determined using a dynamic programming algorithm. First, represent the nodes of the two view trees as two node sequences, and then construct a two-dimensional matrix. Each element in the two-dimensional matrix represents the minimum number of operations required to transform a partial structure of one view tree into a partial structure of the other. Starting from the top left corner of the matrix, calculate the value of each matrix element step by step. When the dynamic programming matrix is fully filled, the value in the bottom right corner is the tree edit distance between the two view trees.
[0124] In some embodiments, see Figure 9 , Figure 9 The method for determining the tree edit distance between two view trees in step 202 is shown, which can be achieved through steps 2021 to 2023:
[0125] In step 2021, a sequence of editing operations is determined.
[0126] The editing operation sequence includes multiple editing operations, which are used to transform one of the two view trees into the other.
[0127] Here, an edit operation sequence is a set of sequentially executed edit operations used to progressively transform one view tree into another. Each edit operation sequence represents a possible transformation method. Since there can be multiple different implementations for transforming from one view tree to another, multiple edit operation sequences can be generated.
[0128] In step 2022, for each editing operation sequence, the weight of the editing operation is determined based on the operation type of the editing operation, and the weights of multiple editing operations included in the editing operation sequence are summed to obtain the total weight of the editing operation sequence.
[0129] Here, the types of editing operations can include insertion, deletion, and replacement operations. The weight of an editing operation is used to characterize its cost or expense. For each sequence of editing operations, multiple editing operations are obtained from that sequence. The weight of each editing operation is determined based on its operation type. The weights of the multiple editing operations are summed to obtain the total weight of the editing operation sequence.
[0130] For example, suppose that the edit operation sequence 1 includes an insert operation, a delete operation, and a deletion operation performed in sequence, and the weight of the insert operation and the deletion operation is 1. Then the total weight of the edit operation sequence 1 is 1 + 1 + 1 = 3.
[0131] In some embodiments, the weight of an editing operation is determined based on its operation type, which can be achieved as follows: when the operation type is an insertion or deletion operation, a preset parameter value is determined as the weight of the editing operation; when the operation type is a replacement operation, the node type of the node pair corresponding to the editing operation is determined, wherein the node pair includes a fifth node in one view tree and a sixth node in another first view tree, the fifth node representing an element in the view component corresponding to one view tree, and the sixth node representing an element in the view component corresponding to the other first view tree; when the node type is a text node, the similarity of the node pair is determined based on the text attributes corresponding to the fifth node and the sixth node; when the node type is an image node, the similarity of the node pair is determined based on the position information corresponding to the fifth node and the position information corresponding to the sixth node; the weight of the editing operation is determined based on the similarity of the node pairs.
[0132] Here, the preset parameter values corresponding to the insertion operation and the deletion operation can be the same or different. For example, the preset parameter value "1" is used as the weight for both the insertion and deletion operations. The node pair corresponding to the editing operation is the node pair to be inserted, deleted, or replaced, including the fifth node in one view tree and the sixth node in another first view tree. For example, if the purpose of the editing operation is to replace the fifth node a in view tree T1 with the sixth node b in view tree T2, then the operation type of the editing operation is a replacement operation, and the corresponding node pair includes the fifth node a and the sixth node b.
[0133] After identifying the node pair corresponding to the editing operation, the node type of the node pair can be determined. In the replacement operation, the fifth and sixth nodes in the node pair have the same node type. Node types can include text nodes, image nodes, component nodes, and other types of nodes. When the node type is a text node, the text node stores text attributes, which can include text content, font style (e.g., size, color, bold), and formatting information (e.g., line spacing, paragraph indentation). The text attributes corresponding to the fifth node can be encoded as a first encoding vector, and the text attributes corresponding to the sixth node can be encoded as a second encoding vector. The similarity of the node pair is determined by calculating the cosine similarity between the first and second encoding vectors.
[0134] When the node type is an image node, the image node stores the node's location information, which may include the image's coordinates, size, etc. This application does not limit the method for calculating the similarity of node pairs based on location information. For example, coordinate similarity can be calculated using Euclidean distance based on the coordinates of the image in the fifth node and the image in the sixth node, and size similarity can be calculated by the ratio of the size of the image in the fifth node to the size of the image in the sixth node. The weighted sum of the coordinate similarity and size similarity yields the similarity of the node pair.
[0135] In some embodiments, when the editing operation is a replacement operation, after determining the similarity of the node pairs corresponding to the editing operation, a third similarity threshold (which can be set according to actual needs) can be obtained. If the similarity of the node pairs is greater than or equal to the third similarity threshold, it indicates that the fifth and sixth nodes are relatively similar, and the cost of replacement is low; in this case, the parameter value "0" can be used as the weight of the editing operation. If the similarity of the node pairs is less than the third similarity threshold, it indicates that the fifth and sixth nodes are not similar, and the cost of replacement is high; in this case, the parameter value "1" can be used as the weight of the editing operation.
[0136] In some embodiments, when the operation type of the editing operation is a replacement operation, after determining the similarity of the node pairs corresponding to the editing operation, the difference between the preset parameter value "1" and the similarity can be used as the weight of the editing operation.
[0137] This application sets weights for insertion, deletion, and replacement operations, and calculates similarity by combining node type (such as text nodes, image nodes, etc.) and their attribute differences, making the cost assessment of editing operations more accurate. By introducing a third similarity threshold or dynamically adjusting weights based on similarity, the cost of replacement operations can be flexibly controlled according to actual needs, improving the applicability of the algorithm. By refining the weights of replacement operations, errors caused by simple assignment are reduced, thereby improving the accuracy of tree edit distance calculation and providing a more reliable basis for subsequent view tree grouping and similarity analysis.
[0138] In some embodiments, when the edit operation type is a replacement operation, it can also be determined whether the two nodes in the node pair corresponding to the boundary operation are the same. If the two nodes are the same, the weight of the edit operation is 0; if the two nodes are different, the weight of the edit operation is 1. Here, "two nodes are the same" means that the node type, attributes, content, position, etc. are all the same.
[0139] In step 2023, the minimum value of the total weights corresponding to the multiple edit operation sequences is taken as the tree edit distance.
[0140] Here, the total weight of each edit operation sequence is obtained, and the minimum value is determined from multiple total weights. This minimum value is used as the tree edit distance. The tree edit distance is calculated according to the following formula (1).
[0141]
[0142] Where tree_distance(T1,T2) is the tree edit distance between view trees T1 and T2, S(T1,T2) is the set of all possible edit operation sequences for transforming view tree T1 into view tree T2, σ is an edit operation sequence, and cost(x,y) represents the weight of the edit operation performed on the node pair (x,y). ∑ (x,y)∈σ cost(x,y) represents the total weight corresponding to the edit operation sequence σ. min indicates the operation of taking the minimum value.
[0143] For example, there are three possible editing operation sequences for transforming view tree T1 into view tree T2: editing operation sequence 1, editing operation sequence 2, and editing operation sequence 3. The total weight of editing operation sequence 1 is 1, the total weight of editing operation sequence 2 is 4, and the total weight of editing operation sequence 3 is 6. Therefore, the minimum value is 1, and the tree edit distance between view tree T1 and view tree T2 is 1.
[0144] In this embodiment, the calculation of tree edit distance comprehensively considers the differences in the number of nodes, hierarchical structure, and node content of the view trees, thus fully reflecting the similarity or difference between two view trees. By assigning different weights to different types of editing operations, the calculation result of tree edit distance can be adjusted according to specific needs. For example, in some scenarios, more attention may be paid to the insertion or deletion of nodes, while in other scenarios, more attention may be paid to the replacement of nodes. By selecting the minimum total weight as the tree edit distance, the optimality of the calculation result is ensured. This method can not only accurately measure the differences between view trees but also provide a reliable basis for subsequent similarity calculations.
[0145] In step 203, the maximum number of nodes is determined from the number of nodes in the two view trees.
[0146] Here, we obtain the number of nodes contained in each of the two view trees, and take the maximum value of the number of nodes contained in each of the two view trees as the maximum number of nodes.
[0147] For example, if view tree T1 contains 10 nodes and view tree T2 contains 5 nodes, then the maximum number of nodes is 10.
[0148] In step 204, the ratio of tree edit distance to maximum number of nodes is determined, and the similarity between the two view trees is determined based on the ratio.
[0149] Here, the tree edit distance is processed by the maximum number of nodes to obtain a ratio. The difference between the preset parameter value and this ratio is used as the similarity between the two view trees. For example, the preset parameter value is 1. The similarity satisfies the following formula (2).
[0150]
[0151] Where sim(T1,T2) is the similarity between view tree T1 and view tree T2, tree_distance(T1,T2) is the tree edit distance between view tree T1 and view tree T2, count_node1 is the number of nodes in view tree T1, count_node2 is the number of nodes in view tree T2, and max(count_node1,count_node2) is the maximum number of nodes.
[0152] For example, if view tree T1 contains 10 nodes and view tree T2 contains 5 nodes, then count_node1 is 10, count_node2 is 5, and the maximum number of nodes is 10. The tree edit distance between view tree T1 and view tree T2 is 1, so the similarity between view tree T1 and view tree T2 is sim(T1,T2) = 1 - 1 / 10 = 0.9.
[0153] This application's embodiments convert the similarity between view trees into a specific numerical value by using the ratio of tree edit distance to the maximum number of nodes. The calculation of tree edit distance can comprehensively consider the differences in the structure, node content, and attributes of the view trees, enabling comparisons to be made even if the view trees are large or complex in structure through a standardized ratio, thereby improving the accuracy of similarity calculation.
[0154] In some embodiments, see Figure 10 , Figure 10 The process of grouping multiple view trees in step 102 to obtain multiple view tree groups can be achieved through the following steps 1021B to 1023B:
[0155] In step 1021B, the first view tree and the second view tree from the multiple view trees are obtained.
[0156] Here, two view trees are randomly or sequentially selected from all the view trees to be processed for inclusion relationship matching, and they are called the first view tree and the second view tree, respectively.
[0157] For example, the interface design image includes six view components: list items for Channel 1, Channel 2, Channel 3, Channel 4, Channel 5, and Channel 6, which correspond to view trees T1, T2, T3, T4, T5, and T6, respectively. The first view tree T1 and the second view tree T2 are arbitrarily selected from these six view trees.
[0158] In step 1022B, when a node in the first view tree includes a node in the second view tree, it is determined that there is an inclusion relationship between the first view tree and the second view tree.
[0159] Here, we determine whether all nodes in the first view tree include all nodes in the second view tree. If all nodes in the first view tree include all nodes in the second view tree, then the inclusion relationship matching result is that the first view tree and the second view tree have an inclusion relationship. We can traverse all nodes in the second view tree and check whether these nodes have corresponding nodes in the first view tree. If all nodes in the second view tree can be found in the first view tree, then the inclusion relationship between the two is confirmed. Nodes being the same means that the node's hierarchical structure, label type, text content, style, and other characteristics are the same. If at least one node in the second view tree does not exist in the first view tree, and at least one node in the first view tree does not exist in the second view tree, then the inclusion relationship matching result is that the first view tree and the second view tree do not have an inclusion relationship.
[0160] For example, if the first view tree T1 includes nodes 1, 2, 3, 4, and 5, and the second view tree T2 includes nodes 1, 2, 3, and 4, then the first view tree T1 and the second view tree T2 have an inclusion relationship.
[0161] In step 1023B, the first view tree and the second view tree, which have an inclusion relationship, are grouped into the same view tree group.
[0162] Here, once it is determined that there is an inclusion relationship between the first view tree and the second view tree, these two view trees are divided into the same view tree group.
[0163] This application's embodiments effectively identify hierarchical dependencies between view trees by determining their containment relationships. This grouping method is particularly suitable for interface designs with complex nested structures, such as the relationship between parent and child components. The determination of containment relationships considers not only the number of nodes but also their structure, content, and attributes, ensuring more accurate grouping results. If the structure or content of the view tree changes, the containment relationships can be recalculated and the grouping adjusted, maintaining the system's flexibility. This dynamic adjustment capability allows the method to adapt to constantly changing application scenarios.
[0164] In some embodiments, multiple view trees can be first grouped based on the containment relationship between them to obtain multiple initial view tree groups. Then, the multiple first view tree groups are corrected based on the similarity between the view trees to obtain multiple view tree groups.
[0165] In step 103, multiple view tree groups are merged based on the similarity between view trees in every two view tree groups to obtain a template view tree.
[0166] Here, the process of determining the similarity between view trees can refer to steps 201 to 204 in the above embodiments, and will not be described again here. A template view tree refers to a standardized view tree extracted from a group of view trees with similar structures and features, which can represent the common characteristics of the group of view trees. The template view tree includes the common nodes of the view trees in the merged view tree group, as well as the difference nodes between the view trees.
[0167] If the grouping results in two view tree groups, then if the similarity between the view trees in these two view tree groups is greater than or equal to the second similarity threshold, these two view tree groups can be merged to obtain a template view tree. If the number of view tree groups obtained from the grouping result is greater than two, then for these multiple view tree groups, two view tree groups can be randomly or sequentially selected. If the similarity between the view trees in these two view tree groups is greater than or equal to the second similarity threshold, these two view tree groups are merged first, and then the third view tree group is merged to obtain the template view tree. It should be noted that if there exists a third view tree group whose similarity to the two existing view tree groups is also greater than or equal to the second similarity threshold, then the two existing view tree groups are merged first, and then the third view tree group is merged to obtain the template view tree.
[0168] In some embodiments, see Figure 11 , Figure 11 The step 103, which merges multiple view tree groups based on the similarity between view trees in every two view tree groups to obtain a template view tree, can be achieved through the following steps 1031 to 1034:
[0169] In step 1031, two view tree groups are selected from multiple view tree groups, and the two view tree groups are used as the first view tree group and the second view tree group.
[0170] Here, two view tree groups are randomly selected from multiple pre-grouped view tree groups, referred to as the first view tree group and the second view tree group, respectively. The selection method can be random, sequential, or based on a specific rule (such as prioritizing the larger view tree group).
[0171] In step 1032, the view tree with the most nodes in the first view tree group is designated as the third view tree, and the view tree with the most nodes in the second view tree group is designated as the fourth view tree.
[0172] Here, the number of nodes in each view tree in the first view tree group is obtained, and the view tree with the largest number of nodes is selected as the third view tree. The number of nodes in each view tree in the second view tree group is obtained, and the view tree with the largest number of nodes is selected as the fourth view tree.
[0173] In step 1033, the first size of the view component corresponding to the third view tree and the second size of the view component corresponding to the fourth view tree are determined.
[0174] Here, dimensions can include the width or height of the view component. Obtain the first dimension of the view component corresponding to the third view tree and the second dimension of the view component corresponding to the fourth view tree from the interface design image. Both height and width can be represented using pixel dimensions from the interface design image.
[0175] For example, the view component is a list item, and the size is the height of the list item. The view component corresponding to the third view tree is list item 1 in the interface design image, and the view component corresponding to the fourth view tree is list item 2 in the interface design image. The first size is the height of list item 1: 10 pixels (px), and the second size is the height of list item 2: 9 pixels (px).
[0176] In step 1034, based on the first size, the second size, and the similarity between the third view tree and the fourth view tree, the first view tree group and the second view tree group are merged to obtain the template view tree.
[0177] Here, we can first make a preliminary judgment on whether the third view tree and the fourth view tree can be merged based on the first and second dimensions, and obtain the judgment result. If the judgment result is that the third view tree and the fourth view tree may be merged, we determine the similarity between the third view tree and the fourth view tree, and make a second judgment on whether the third view tree and the fourth view tree can be merged based on the similarity.
[0178] This application's embodiments, by comprehensively considering size and similarity, can more accurately determine whether two view tree groups have sufficient similarity. Size comparison ensures the consistency of the view tree groups in terms of scale, while similarity calculation further verifies the degree of matching in structure and content. By merging to obtain a template view tree, the amount of generated executable code can be reduced, thereby improving code reusability.
[0179] In some embodiments, the first view tree group and the second view tree group are merged based on the first size, the second size, and the similarity between the third view tree and the fourth view tree to obtain a template view tree. This can be achieved by: obtaining a preset second similarity threshold when the difference between the first size and the second size is less than or equal to a size threshold; and merging the third view tree and the fourth view tree when the similarity between the third view tree and the fourth view tree is greater than or equal to the second similarity threshold to obtain a template view tree.
[0180] Here, this embodiment does not limit the size threshold and can be set according to actual needs. Alternatively, the average size threshold during the view tree merging process over a historical period can be calculated as the size threshold for this judgment. When the difference between the first size and the second size is less than or equal to the size threshold, a judgment result indicating whether the third and fourth view trees can be merged can be obtained, and a second judgment on whether the third and fourth view trees can be merged is made based on similarity. When the difference between the first size and the second size is greater than the size threshold, the third and fourth view trees cannot be merged, the merging process of the first and second view tree groups is terminated, and the judgment on whether the other two view tree groups can be merged continues. This embodiment does not limit the second similarity threshold, as long as the second similarity threshold is less than the first similarity threshold. For example, the second similarity threshold is 0.7. When the similarity between the third and fourth view trees is greater than or equal to the second similarity threshold, the third and fourth view trees are merged to obtain a template view tree. When the similarity between the third view tree and the fourth view tree is less than the second similarity threshold, the merging process of the first view tree group and the second view tree group is terminated, and the process continues to determine whether the other two view tree groups can be merged.
[0181] This application's embodiments introduce a size threshold (a limitation on the difference between a first size and a second size) to ensure that only view tree groups of similar size are considered for merging. This mechanism avoids erroneous merging caused by excessive size differences, thereby improving the accuracy of merging. By setting a second similarity threshold, it ensures that only view trees with sufficiently similar structures and content are merged. This dual verification mechanism (size + similarity) further enhances the reliability of the merging results. The template view tree extracts the common structure and attributes from the view tree group, enabling the generation of executable code based on the template view tree. This reduces the writing of repetitive code and improves code readability and maintainability.
[0182] In this embodiment, merging the third view tree and the fourth view tree to obtain a template view tree can be achieved in the following way: First, the view tree with the most nodes among the third and fourth view trees is taken as the target view tree, and the other view tree among the third and fourth view trees excluding the target view tree is taken as the fifth view tree; then, based on the node characteristics of each third node in the target view tree and the node characteristics of each fourth node in the fifth view tree, at least one difference node is selected from multiple fourth nodes, wherein the third node represents an element in the view component corresponding to the target view tree, and the fourth node represents an element in the view component corresponding to the fifth view tree; finally, at least one difference node is inserted into the target view tree to obtain the template view tree.
[0183] Here, since the view tree with more nodes more comprehensively reflects the common structural features of the two view trees, the view tree with the most nodes is selected as the target view tree from the third and fourth view trees. For example, the third view tree is view tree T1, containing 10 nodes, and the fourth view tree is view tree T2, containing 5 nodes. View tree T1 is then used as the target view tree, and view tree T2 is used as the fifth view tree. The target view tree is the base view tree for which difference nodes are inserted to generate the template view tree. Nodes in the target view tree are used as third nodes, and nodes in the fifth view tree are used as fourth nodes. Node characteristics can include node type and node attributes, but not node content. If the node type is a text node, the node attributes can include the font size, color, and position information (e.g., the size, width, height, and coordinates of the text box). If the node type is another type of node (e.g., image node, icon node, etc.), the node attributes can include position information.
[0184] Traverse the fourth nodes in the fifth view tree. For each fourth node, obtain multiple candidate nodes with the same node type from multiple third nodes. If the node type is a text node, since the text length and text box width vary, the width of the fourth node and candidate nodes can be automatically widened to make their widths consistent. Then, compare the font size, color, and other element attributes of the fourth node with those of each candidate node. If all element attributes of each candidate node are different from those of the fourth node, then the fourth node is designated as a difference node. If there are candidate nodes with the same element attributes, continue comparing the size, height, coordinates, and other positional information of the candidate node with those of the fourth node. If the positional information of each candidate node is different from that of the fourth node, then the fourth node is designated as a difference node. Otherwise, continue to the next fourth node.
[0185] If the node type is another type, such as an image node, then compare the size, width, height, coordinates, and other positional information of each candidate node with that of the fourth node. If the positional information of each candidate node is different from that of the fourth node, then the fourth node is considered a difference node. Otherwise, continue to the next fourth node. Insert at least one difference node into the target view tree to obtain the template view tree.
[0186] This embodiment uses the target view tree as a base and supplements it with difference nodes from the fifth view tree, resulting in a template view tree that more comprehensively reflects the common features of the third and fourth view trees. During the process of filtering difference nodes, only the truly different parts between the target view tree and the fifth view tree are retained, while duplicate or unnecessary nodes are removed. This optimization makes the template view tree more concise and efficient, reducing the overhead of subsequent rendering or processing. Through node-by-node comparison and difference filtering, this method can effectively handle view trees with complex nested structures. Even if the structures of two view trees differ significantly, merging can be achieved by gradually inserting difference nodes.
[0187] In some embodiments, the target view tree has multiple node levels. Inserting at least one difference node into the target view tree to obtain a template view tree can be achieved as follows: First, for each difference node, determine the target node level in the target view tree where the difference node is to be inserted from the multiple node levels; then, determine the node constraint relationship between the third node in the target node level and the difference node; finally, based on the target node level and the node constraint relationship, insert the difference node into the target view tree, and bind view variables to the difference nodes inserted into the target view tree. The target view tree with bound view variables is determined as the template view tree, wherein the view variables are used to control the display state of the difference node.
[0188] Here, the target view tree has multiple node levels, which represent the nesting relationships between elements in the view components corresponding to the target view tree. For example, the root node in the target view tree is the first node level, representing the frame of the list item. The root node includes two child nodes, a and b, which belong to the second node level. Child node a represents the text box a within the frame of the list item, and child node b represents the icon b within the frame of the list item. The first and second node levels represent the nesting relationship between the frame of the list item, the text box a, and the icon b.
[0189] For each differing node, based on its original node position (e.g., coordinates) in the fifth view tree, the target node level in the target view tree can be determined from multiple node levels. Multiple candidate node levels matching the original node position can be selected from the target view tree, and the node level capable of accommodating the differing node is chosen as the target node level. For example, if the second and third node levels in the target view tree both match the original node position of the differing node, but if the differing node is inserted into the third node level, the element corresponding to the differing node will overlap with the element corresponding to a node in the third node level in the view display, and the third node level cannot accommodate the differing node, then the second node level is chosen as the target node level.
[0190] Node constraints refer to the constraints between the third node and the elements in the view components corresponding to the difference node. The constraints between elements refer to the positional and dimensional constraints defined in the layout structure, which determine how elements are positioned and resized relative to other elements or relative to the edges of the layout content. For example, if the third node is the root node in the target view tree, the node constraints between the difference node and the third node include the distance between the element corresponding to the difference node and the border of the corresponding list item in the target view tree. If the third node is image node 'a' in the target view tree, the node constraints between the difference node and the third node include the relative positional relationship between the element corresponding to the difference node and the image corresponding to image node 'a' within the list item, such as horizontal and vertical distances.
[0191] Insert difference nodes into the target node hierarchy of the target view tree, ensuring that the insertion position of the difference nodes satisfies node constraints. Bind view variables to the difference nodes inserted into the target view tree, and define the target view tree with the bound view variables as the template view tree. For example, if the fifth node tree is view tree T2 and the view variable is style-T2, then when the value of view variable style-T2 is 1, the difference nodes bound to this view variable style-T2 in the template view tree will be displayed. When the value of view variable style-T2 is 0, the difference nodes bound to this view variable style-T2 in the template view tree will not be displayed.
[0192] This application's embodiments ensure that differing nodes can be correctly inserted into the target view tree by clearly defining the target node hierarchy and node constraints, thereby maintaining the overall structural logic of the view tree and avoiding structural chaos caused by arbitrary insertion, thus improving the rationality of the template view tree. By binding view variables to differing nodes, the template view tree can support dynamic expansion and adapt to the needs of different scenarios, simplifying the management of differing nodes and enhancing the flexibility of the template view tree. Through node-by-node analysis and insertion, this method can effectively handle view trees with complex nested structures.
[0193] In step 104, the mapping relationship between the first node in the template view tree and the second node in each view tree is determined.
[0194] The first node represents an element in the view component corresponding to the template view tree, and the second node represents an element in the view component corresponding to the view tree.
[0195] Here, after generating the template view tree, for each view component in the interface design image, multiple second nodes in the view tree corresponding to that view component are obtained. For each second node, from the multiple first nodes in the template view tree, the first node that matches that second node is determined, and a mapping relationship between the second node and the first node is constructed. Here, matching means that the node content is different but the node characteristics are the same. For example, the node characteristics of the first node are a text node at the first node level, with a height of 50px, coordinates (5,5), and the node content is the text "Hello". The node characteristics of the second node are also a text node at the first node level, with a height of 50px, coordinates (5,5), and the node content is the text "Expand". Then, a mapping relationship between the first node and the second node is established. The mapping relationship can be stored in the second nodes of each view tree, or a separate mapping table can be generated for storage.
[0196] For example, the multiple first nodes in the template view tree include: A, B, C, D, E, the multiple second nodes in the view tree T1 include A1, B1, and the constructed mapping relationship includes A1-A, B1–B.
[0197] In step 105, executable code for the application interface is generated based on the template view tree and mapping relationships.
[0198] Here, executable code refers to code that can be run directly and render the application's interface.
[0199] In some embodiments, see Figure 12 , Figure 12 The executable code for generating the application interface in step 105, based on the template view tree and mapping relationships, can be achieved through steps 1051 to 1054:
[0200] In step 1051, a code framework is generated based on the template view tree.
[0201] Here, the code framework is the basic structure of the application interface, defining the overall layout, style, and interaction logic. Based on the structure of the template view tree, nodes can be converted into corresponding code snippets layer by layer to obtain the code framework, such as a template UI file.
[0202] In step 1052, the component code for each view tree is generated.
[0203] Here, component code refers to the independent code snippets generated for each specific view tree. For example, each view component can generate its own ViewModel file. Based on the structure and content of each view tree, corresponding code snippets can be generated to obtain the ViewModel file for the view component.
[0204] In step 1053, the code of multiple components is merged based on the mapping relationship to obtain the target component code.
[0205] Here, the component code includes the code for each second node. Multiple mappings with the same first node can be obtained. The code for the second node included in each of these mappings can be merged to obtain the target component code. For example, if second node a in component code 1 and second node b in component code 2 map to the same first node, then the code for second node a and the code for second node b will be merged.
[0206] In step 1054, the target component code is added to the code framework to obtain the executable code for the application interface.
[0207] Here, the target component code is added to the location of the first node in the code framework to obtain the executable code for the application interface.
[0208] This application's embodiments reduce the writing of repetitive code by extracting common parts and generating target component code. This reduces unnecessary code redundancy and lowers rendering and computational overhead.
[0209] This application's embodiments, after detecting multiple view components in the interface design image and determining the view tree for each view component, effectively identify view components with similar structures in the interface design image by grouping view trees and merging view tree groups based on similarity. These view components with similar structures are then abstracted into reusable template view trees. By establishing a mapping relationship between the first node in the template view tree and the second node in the view tree, it ensures that each element in the view component corresponds one-to-one with the code generated using the template view tree, thereby generating reusable code and improving code usability.
[0210] Figure 13 This is another flowchart illustrating the code generation method provided in the embodiments of this application, such as... Figure 13 As shown, the method includes the following steps 301 to 309:
[0211] Step 301: The terminal receives the user's interactive operation.
[0212] Here, interactive operations can include input interface design images, for example, the terminal can be a D2C Figma plugin, and users can design images through the input interface of the D2C Figma plugin.
[0213] Step 302: The terminal performs view component detection on the interface design image of the application interface to obtain multiple view components in the interface design image and determines the view tree of each view component.
[0214] Here, the specific process of performing view component detection on the interface design image of the application interface to obtain multiple view components in the interface design image and determining the view tree of each view component can refer to step 101 in the above embodiment, and will not be repeated.
[0215] Step 303: The terminal groups the multiple view trees to obtain multiple view tree groups.
[0216] Here, the specific process of grouping multiple view trees to obtain multiple view tree groups can be referred to step 102 in the above embodiment, and will not be repeated here.
[0217] Step 304: The terminal merges multiple view tree groups based on the similarity between view trees in every two view tree groups to obtain a template view tree.
[0218] Here, the specific process of merging multiple view tree groups based on the similarity between view trees in every two view tree groups to obtain the template view tree can be referred to step 103 in the above embodiment, and will not be repeated here.
[0219] Step 305: The terminal determines the mapping relationship between the first node in the template view tree and the second node in each view tree.
[0220] Here, the specific process of determining the mapping relationship between the first node in the template view tree and the second node in each view tree can be referred to step 104 in the above embodiment, and will not be repeated.
[0221] Step 306: The terminal generates a code generation request.
[0222] Here, the code encapsulates the template view tree and mapping relationships into the code generation request.
[0223] Step 307: The terminal sends a code generation request to the server.
[0224] Step 308: In response to the code generation request sent by the terminal, the server generates executable code for the application interface based on the template view tree and mapping relationship.
[0225] Here, the server can be the code generation terminal. The specific process of generating executable code for the application interface based on the template view tree and mapping relationships can be referred to step 105 in the above embodiment, and will not be repeated here.
[0226] Step 309: The server sends the executable code file to the terminal.
[0227] The following will describe an exemplary application of the embodiments of this application in a real-world application scenario.
[0228] In related technologies, when using Design to Code (D2C) for code generation, Figma elements are typically mapped one-to-one to code. However, this generation method results in list code that is merely a simple listing, failing to properly reuse similar user interface (UI) components. In list page scenarios, each list item often exhibits high similarity. If code is generated directly through mapping, similar list items cannot be effectively merged, leading to overly verbose and repetitive code that lacks efficiency and maintainability.
[0229] Figure 14 This is a schematic diagram of the code file generated without merging list items, as provided in an embodiment of this application. See also... Figure 14 Visual draft 1401 (corresponding to the interface design image in the above embodiments) is a visual draft of a communication application interface. Visual draft 1401 includes the title "My Channels" and 6 list items (corresponding to the view components in the above embodiments), each list item representing a channel. Code file 1402 is generated for the "Channel 1" list item, code file 1403 for the "Channel 2" list item, code file 1404 for the "Channel 3" list item, code file 1405 for the "Channel 4" list item, code file 1406 for the "Channel 5" list item, and code file 1407 for the "Channel 6" list item.
[0230] The methods described above for generating list view code simply map to Figma list items. Similar list items are not processed for code reusability, and differing list items generate two similar sets of code. Alternatively, related technologies may abstract reusable code locally to create components, but this only extracts identical code after generation, resulting in low accuracy and fault tolerance. These technologies suffer from the following technical problems: Poor usability of generated code: The generated list code does not merge similar parts or the merging is incomplete, only merging local code, leading to excessive redundancy and very poor maintainability. Poor readability of generated code: The code generated through mapping to the Figma view tree contains multiple similar sets of code, which does not conform to the development habits of actual developers and results in poor readability.
[0231] Based on this, the code generation method provided in this application is an automated code generation method for merging similar components in a visual design. It merges components at the Figma view tree level, addressing the list item reuse problem at its root. After identifying list nodes in the Figma visual design using a YOLO-trained model, algorithm-based similarity recognition is performed on the list node subtrees to complete the first round of similar subtree identification. Subsequently, the similar subtrees are grouped, and the similarity of different groups of subtrees is compared. Subtrees that meet the similarity criteria undergo a second round of merging and mapping, enabling the reuse of similar subtrees. Only one template code is generated for each reused subtree during code generation, thereby reducing the generation of redundant similar code and improving the usability of code generation.
[0232] The code generated by this application's embodiments is highly readable, allowing for reasonable reuse of code for similar list items, and conforms to actual development habits. The list merging has a high fault tolerance rate; list items with element differences and misalignments can also be merged to generate the same UI code. It is fully automated: the list item merging process and code generation process are fully automated, reducing manual intervention by developers.
[0233] The following describes the product-side application scenarios of this application embodiment.
[0234] In a typical development workflow, after receiving Figma visual drafts from designers, developers can use the D2C plugin for automatic optimization and repair. The D2C plugin's repair process includes pre-repair and post-processing. Pre-repair involves key steps such as: removing invalid nodes, intelligent image merging, streamlining and re-dividing hierarchy, achieving adaptive element layout, and semantic naming. Post-processing includes key steps such as: list node detection, similar list item identification, and merging of similar list items. These operations aim to improve the structure and usability of the visual drafts, ensuring that the generated code is more efficient and standardized.
[0235] The process includes: Removing invalid nodes: Figma visuals may contain numerous redundant elements (such as hidden layers, unused graphics, and overlapping groups). These elements are visually invisible but pollute the code structure. The system scans the tree structure of the visual to identify nodes with no actual rendering value (e.g., elements with 0 opacity or elements exceeding the artboard) and removes them. Intelligent image merging: It automatically identifies duplicate graphic resources (such as icons). For example, if an icon is composed of multiple nodes, it merges these nodes into a single icon node. Simplifying and re-hierarchizing: The visual's layer groups may be excessively nested (e.g., multiple meaningless groups or frames). It automatically creates new hierarchical levels. Adaptive element layout: For example, a text box width of 500 pixels might only need to be 100 pixels for actual text length. Semantic naming: Figma visual layer names are often meaningless, such as "Rectangle 1" or "Group 23," which are difficult to map directly to class or component names in the code. Semantic names are generated based on element type and context.
[0236] Figure 15 This is a schematic diagram of the interface of the D2C plugin provided in an embodiment of this application. See also... Figure 15 Select a visual draft page and click the "High-Quality Repair" button in the "Smart Repair Visual Draft" section. The D2C plugin will then execute a fully automated repair process. This process sequentially performs preprocessing, intelligent grouping, intelligent layout, and post-processing. Post-processing includes list detection, similar list item identification, and merging of differing list items. After the repair is complete, to avoid damaging the original visual draft, the D2C plugin will generate a repaired visual draft alongside the original.
[0237] Figure 16 These are schematic diagrams of the original and revised visual drafts provided in embodiments of this application. In the revised visual draft, the list structure can be selected using a dashed box (or by color selection, such as blue, not shown in the figure). See also... Figure 6 The view tree corresponding to the identified list structure 604 will carry the [c:cell] tag. For example, the view tree 1 corresponding to the list item "Channel 1" carries the [c:cell] tag. The [c:cell] tag can carry the information of the first group. For example, view tree 1 [c:cell](g1) means that view tree 1 belongs to view tree group one, and (g2) means that view tree group two.
[0238] See Figure 15After receiving the revised visual design, users can click the "Build Code" button on the D2C plugin interface to generate reusable list code (corresponding to the executable code in the above embodiment) suitable for various platforms. This greatly simplifies the transition from design to development, improving development efficiency and code quality. The generated list code can be previewed in the D2C plugin client. See [example example] for details. Figure 17 , Figure 17 This is a schematic diagram of the preview interface for the list code and application interface provided in this application embodiment. Here, the left side of the preview interface is used to preview the code, displaying the list code corresponding to the visual draft, and the right side of the preview interface is the application interface corresponding to the visual draft generated in the preview area. The preview interface also includes a "Fold Preview" button, used to expand or collapse the list code on the left.
[0239] The following describes the specific implementation process of the code generation method provided in the embodiments of this application.
[0240] Figure 18 This is a schematic diagram of the overall process of the code generation method provided in the embodiments of this application.
[0241] See Figure 18 The code generation method provided in this application embodiment is applied to both the D2C plugin end and the backend code generation end.
[0242] Step 1801: Find similar list items.
[0243] In this process, the Figma visual design is input into the D2C plugin. After the D2C plugin reads the selected Figma visual design, it uses a YOLO model to predict the region where the list view is located in the Figma visual design and checks the rationality of the list structure in that region. If the list structure is reasonable, the similarity of the view trees of the list items is identified. After the similarity algorithm is completed, similar view trees are grouped for the first time. List items with a similarity greater than 0.9 (corresponding to the first similarity threshold in the above embodiment) are grouped into the same group. As an example, Figure 19 This is a schematic diagram of list item grouping provided in an embodiment of this application. See also... Figure 19 The list items for Channel 1, Channel 2, Channel 3, and Channel 6 were grouped into Group 1, while the list items for Channel 4 and Channel 5 were grouped into Group 2.
[0244] Here, the similarity between the view trees satisfies the above formula (2). The similarity between two view trees is between [0,1], and when the similarity is 1, the two view trees are considered to be completely similar.
[0245] Where Sim(T1, T2) represents the similarity between view trees T1 and T2. T1 and T2 represent the view trees of two list items (corresponding to the view components in the above embodiments). The tree_distance function represents the minimum edit distance (corresponding to the tree edit distance in the above embodiments) for transforming view tree T1 into view tree T2. count_node1 represents the number of valid nodes in view tree T1 (corresponding to the number of nodes in the above embodiments), and count_node2 represents the number of valid nodes in view tree T2. Valid nodes refer to nodes other than the root node that can be displayed in the view, such as image, text, icon, component, etc.
[0246] The minimum edit distance for transforming view tree T1 into view tree T2 satisfies the above formula (1).
[0247] Where σ represents a sequence of editing operations from view tree T1 to view tree T2 (including multiple editing operations: insertion, deletion, replacement, etc.), and S(T1, T2) represents the set of all possible editing operation sequences. (x, y) are the node pairs involved in the editing operation, where x is a node in view tree T1 and y is a node in view tree T2 (or a special empty node, representing insertion or deletion). Cost(x, y) is the cost of editing node x into node y (corresponding to the weight in the above embodiment). Formula (1) calculates the editing operation sequence with the minimum cost among all possible editing operation sequences, thus obtaining the minimum edit distance.
[0248] Step 1802: Merge the difference list items.
[0249] After grouping, for each view tree group, the view tree with the most nodes is selected. The similarity of this view tree across different view tree groups is checked (similarity is calculated based on tree edit distance). View trees that meet a certain similarity threshold (corresponding to the second similarity threshold in the above embodiment, which is less than the grouping threshold of 0.9) will undergo subtree merging (i.e., merging differing elements). As an example, Figure 20 This is a schematic diagram illustrating the merging of list items provided in an embodiment of this application. See also... Figure 20 Combine Group 1 and Group 2 to obtain the template.
[0250] Step 1803: Establish the template element mapping relationship.
[0251] Once the view trees are merged, a template view tree will be generated, and a mapping relationship between each element (valid node) of the view tree and each element of the template view tree will be generated (the mapping table can be built separately or stored in the template view tree).
[0252] Step 1804: Establish adaptive constraint information.
[0253] This allows for the adaptive establishment of constraint relationships between the difference nodes corresponding to newly inserted difference elements in the template view tree and other nodes in the template view tree.
[0254] Step 1805, guessing the list type.
[0255] The code generation end can build a backend template view tree based on the template view tree generated by the D2C plugin end, and perform type inference on the template view tree (inferring the type of each node in the template view tree, such as button component, switch component, etc. In this embodiment, the inferred node type is a list view).
[0256] Step 1806: Generate template UI layer code.
[0257] The code generation end generates UI files based on the template view tree merged from the D2C plugin end, reads the mapping relationship established by the D2C plugin end for each list item and the template view, and generates reusable UI components.
[0258] Step 1807: Template data-driven attribute generation.
[0259] The code generation process generates Model and ViewModel files. The ViewModel file is a collection of UI component properties; for example, the UI file might include a button UI component and a text UI component. The ViewModel file includes the properties of the button UI component (buttonText) and the text UI component (labelText).
[0260] Step 1808, MVVM data assembly.
[0261] This involves assembling the UI file, Model file, and ViewModel file to obtain the code file.
[0262] The following is a detailed explanation of the processing flow on the D2C plugin side.
[0263] The D2C plugin is used to merge the difference list items (cells, list items with a similarity greater than a certain threshold) to generate a large template UI view, and to generate a mapping relationship between each list item's elements and the elements of the large template. As an example, Figure 21 This is a schematic diagram of the UI view for merging list items to generate a large template, as provided in an embodiment of this application. See also... Figure 21The UI view of the larger template is obtained by merging list item 1 and list item 2. A mapping relationship is established between each element in the UI view and the list item using variables. When an element is bound to variable 1, that element is an element of list item 1. All elements bound to variable 1 constitute the UI view (variable 1). When an element is bound to variable 2, that element is an element of list item 2, and all elements bound to variable 2 constitute the UI view (variable 2).
[0264] After the initial repair is completed, the list control will be inspected and marked. Marking refers to labeling the view tree of the list item [c:cell]. Only after marking is completed can the view tree be considered as the list item's view tree. After marking, the elements in the list item will undergo structural inspection and repair. Specifically, the width and height of the list (or grid) will be inspected, and the margins of the list elements will be supplemented to ensure that the dimensions of similar elements are basically consistent. As an example, Figure 22 This is a schematic diagram illustrating the structural inspection and repair provided in an embodiment of this application. See also... Figure 22 The system detects the width and height of the list items in Channel 1 and Channel 2. If the detection result shows that the width of Channel 2 is greater than the width of Channel 1, then the list items in Channel 1 are margined (i.e., the list items are widened) to make the widths of Channel 1 and Channel 2 the same.
[0265] After the structural repair is completed, an algorithmic list similarity test is performed, and the resulting list structure will be displayed in the mind map. If the user is not satisfied with the grouping results of the list items, manual marking can be used for secondary merging. For example, Figure 23 This is a schematic diagram of a mind map provided in an embodiment of this application. See also... Figure 23 Mind map 2301 displays the visual draft 2302 and the checked list structure 2303. The checked list structure 2303 includes group 2 (channels 2, 3, 4, 5, 6) and group 3 (channel 1) obtained by similarity grouping. The user may want to merge all groups into one group, in which case all list items (channels 1, 2, 3, 4, 5, 6) are manually marked. After clicking the confirm merge button, the D2C plugin begins to verify whether the elements in the list items of multiple groups can be merged. If the verification is successful, the element merging and variable design process begins.
[0266] Figure 24 This is a flowchart of the plug-in side provided in the embodiments of this application.
[0267] Step 2401, list detection and labeling.
[0268] In this process, after intelligent pre-processing repair, the repaired visual draft is analyzed using a model to identify the list areas and detect the view tree for each list item, then the view tree is labeled. It should be noted that a grid is a special type of list structure.
[0269] Step 2402: List structure retrieval and repair.
[0270] The list structure retrieval and repair process includes steps such as list width detection, list height detection, grid width detection, and grid height detection.
[0271] Step 2403, list similarity detection.
[0272] The similarity of list items can be detected using tree similarity matching and inclusion relationship matching. In addition to grouping list items with a similarity greater than 0.9 together, list items with inclusion relationships also need to be grouped together. For example, if the view tree of list item A includes nodes 1, 2, 3, and 4, and the view tree of list item B includes nodes 1, 2, 3, 4, and 5, then list item B includes list item A.
[0273] Step 2404: Manually mark the mind map.
[0274] The mind map can be generated using a generative pre-trained transformer (GPT). Manual annotations can be added to the mind map to merge different groups.
[0275] Step 2405, similarity detection.
[0276] The similarity detection of list items can be based on list item height and tree edit distance. If the height difference between two list items is greater than a threshold (2 pixels), the view trees of the two list items are directly considered dissimilar, and tree edit distance is not calculated. If the height difference is less than the threshold, that is, for view tree groups that may need to be merged, the similarity is calculated again using tree edit distance for the view trees selected in the first view tree group and the view trees selected in the second view tree group. Element complexity is not involved in the similarity calculation; it only indicates that the calculation of tree edit distance is related to the number of nodes in the view tree. The more complex the element and the more nodes in the tree, the lower the similarity may be.
[0277] Step 2406: Does the similarity meet the standard?
[0278] If the similarity is greater than a certain threshold (e.g., 0.7), the similarity meets the standard, and the process proceeds to step 2408. If the similarity is less than a certain threshold (e.g., 0.7), the similarity does not meet the standard, and the process proceeds to step 2407.
[0279] Step 2407, Exit.
[0280] Here, "exit" means exiting the merging process on the plugin side and executing the code generation process on the code generation side.
[0281] Step 2408, Element merging and variable design.
[0282] Step 2409: Similar element search and tagging.
[0283] Here, we find the mapping relationship between each element in the non-template list items (6 channels) and the elements in the template view tree. Elements with mapping relationships are marked. The marking can be in the large template or in the mapping table. For example, if the template view tree has elements [A, B, C, D, E] and list item 1 has elements [A1, B1], then the mapping relationship of the marking is A1-A, B1-B.
[0284] Step 2410: Upload the marking results to the code generation terminal.
[0285] The element merging process in step 2408 involves steps such as detecting differing elements, merging differing elements, representing differing variables, generating the template view tree, adaptive repair, and uploading variable designs. Figure 25 This is a schematic diagram of the element merging process provided in the embodiments of this application.
[0286] Step 2501: Mark the target view tree.
[0287] Specifically, the view tree with more nodes is selected from the view tree of the first view tree group and the view tree of the second view tree group as the target view tree, and the other is selected as the non-template view tree (corresponding to the fifth view tree in the above embodiment).
[0288] Step 2502: Compare the differences between each element.
[0289] The process involves comparing the differences between elements in the non-template view tree and the target view tree, where each element refers to a valid node. For each element in the non-template view tree (corresponding to the fourth node in the above embodiment), the type of each element in the target view tree (corresponding to the third node in the above embodiment) is sequentially checked against the type of the element (corresponding to the fifth node in the above embodiment). If they match, element attributes (font size, color, etc.) are compared. If the element attributes do not match, the element is considered a difference element (corresponding to the difference node in the above embodiment). If the element attributes match, the element's size, width, height, and position are compared. If they do not match, the element is considered a difference element. Type compatibility comparison is performed on text elements, such as automatically widening the text box (Text). Since text length is variable and width varies, the text box is automatically widened. Size (Rect) comparison refers to comparing size, width, height, and position.
[0290] Step 2503: Are there any differences in the elements?
[0291] Specifically, for each element in the non-template view tree, if the element is not a difference element, proceed to step 2504; if the element is a difference element, proceed to step 2505.
[0292] Step 2504: No element merging is performed.
[0293] Step 2505: Query the reasonable insertion position of the difference element.
[0294] Specifically, based on the coordinates of the difference element in the non-template view tree, containers that match the coordinates are selected from the target view tree, and the container that can accommodate the difference element is used as the insertion position (corresponding to the target node level in the above embodiment).
[0295] Step 2506: Insert the difference elements into the target view tree.
[0296] Step 2507, derive the difference style variable.
[0297] Specifically, a difference style variable style-a (corresponding to the view variable in the above embodiment) is generated for each inserted difference element.
[0298] Step 2508: Bind the difference style variable to the difference element.
[0299] Specifically, for each newly inserted difference element in the target view tree, a difference style variable style-a is stored to establish a mapping relationship (the difference element is only displayed when it is under the style-a style).
[0300] Step 2509, Adaptive Repair.
[0301] This involves establishing node constraints for newly inserted elements, such as establishing constraints with surrounding nodes (e.g., how many pixels away from the left node, which can be queried from the original list view).
[0302] In the automatic merging of difference views, it is necessary to detect the similarity of elements, which involves the verification of element height, element complexity, and tree edit distance. The tree edit distance algorithm compares whether each pair of nodes is similar, and makes the judgment based on the node type and the attribute information of the elements in the Figma view. Figure 26 This is a schematic diagram illustrating the determination of node similarity provided in an embodiment of this application. See also... Figure 26If the node is a component node, relative coordinate detection is performed first, followed by component attribute detection, to determine if the node is a similar node. If the node is a text node, relative coordinate text width detection is performed first, followed by text key attribute detection, to determine if the node is a similar node. If the node is an image node, relative coordinate detection is performed first, followed by view size detection, to determine if the node is a similar node. If the node is any other type of node, relative coordinate and view type detection are performed first, followed by view size detection, to determine if the node is a similar node.
[0303] The definition of tree edit distance: Given two view trees A and B, the tree edit distance d(A, B) is defined as the minimum operation cost required to transform view tree A into view tree B. Operations include: 1. Inserting a node: Adding a new node to the tree. 2. Deleting a node: Removing a node from the tree. 3. Replacing a node: Replacing one node with another.
[0304] First, initialization is performed. The input view trees A and B are converted into AnnotatedTree objects. Each node in the view tree is traversed using a post-traversal method. The number of nodes in view trees A and B, sizea and sizeb, are calculated. A two-dimensional array `treedists` is created to store the edit distances between subtrees. Boundary conditions: If view tree A is empty, then the edit distance d(A, B) = |B| (all nodes need to be inserted), where |B| is the total number of nodes in view tree B, sizeb. If view tree B is empty, then the edit distance d(A, B) = |A| (all nodes need to be deleted).
[0305] Then, dynamic programming is performed. An auxiliary function `treedist(i, j)` is defined to calculate the edit distance between the i-th node of view tree A and the j-th node of view tree B. A dynamic programming table `fd` is used to store the solutions to the subproblems. `fd[x][y]` represents the edit distance for converting the first x nodes of view tree A to the first y nodes of view tree B. State transition: For each pair of nodes (i, j), `fd[x][y]` is updated according to the following formula, and `fd[i][i]` becomes `treedist(i, j)`.
[0306]
[0307] The process involves several steps: Deletion: Delete a node from view tree A, increasing the edit distance by 1. Delete the x-th node from view tree A, then process the remaining x-1 nodes. Insertion: Insert a node into view tree B, increasing the edit distance by 1. Insert the y-th node from view tree B into view tree A, then process the remaining y-1 nodes. Replacement: If node A[i] and node B[j] are different, the cost depends on whether the nodes are the same. If they are different, the cost of replacement is 1; if they are the same, the cost is 0. Replace the x-th node of view tree A with the y-th node of view tree B, then process the remaining x-1 and y-1 nodes.
[0308] Traverse all key root nodes (i.e., the valid nodes mentioned above) and calculate all possible edit distances. The final tree edit distance is the edit distance from the root node of view tree A to the root node of view tree B. The edit distance of the root node is stored in the last position of the array treedists, which is counted starting from 0. Therefore, the formula below is tree edit distance d(A, B) = treedists[sizea-1][sizeb-1].
[0309] The code generation end generates code based on the template view tree merged from the plugin end, reads the mapping relationship established by the plugin end between each list item and the template view tree, and generates reusable UI components. Figure 27 This is a flowchart illustrating the code generation process provided in an embodiment of this application.
[0310] Step 2701: Generate the list control.
[0311] List control generation refers to generating a list code framework, that is, generating the list code for the corresponding platform. For example, the iOS platform uses the UITableView control to generate code, which involves a series of code template assembly and code concatenation operations.
[0312] Step 2702: Read the similarity tags.
[0313] The similarity marker is the mapping relationship between list items and template view tree.
[0314] Step 2703: Read the mapping tag.
[0315] The mapping tags include the Mode tag and the variable tag. The Mode tag represents the mapping relationship between each list item and the template view tree, while the variable tag represents the mapping relationship between each element in the list item and the element in the template view tree.
[0316] Step 2704, View Model Layer Design.
[0317] This involves using mapping tags to design the ViewModel layer and merging the ViewModels of the differing list items. For example, if a visual design includes six list items, an initial ViewModel is generated for each list item. Then, the six ViewModels are merged based on the mapping tags to obtain a single ViewModel.
[0318] Step 2705: Template UI generation.
[0319] Here, the variable tag can be used to control the hiding and showing of controls in the template UI, as well as the merging of similar elements. Code is generated from the template view tree to obtain the template UI (corresponding to the code framework in the above embodiment).
[0320] Step 2706, View Model Layer Construction.
[0321] This process involves generating the ViewModel layer for the current card, which is the list item. The mapping relationship between the current card and the larger template is read, and differentiated ViewModel layers are constructed using tags and mappings.
[0322] Step 2707: Code merging and deletion.
[0323] The code for the six generated list items is merged, and then unnecessary code is deleted to obtain the code file.
[0324] Step 2708: Code optimization.
[0325] The generated code is further optimized by calling large artificial intelligence (AI) models (such as the gpt4 model), mainly to improve the code's structure and readability.
[0326] The overall logic of the code generation end is as follows: After detecting a list control, read the similarity markers, determine if there is a list merging marker, and if so, proceed with the merged generation process to generate the list control code. Read the mapping relationship between list items and template list items recorded by the plugin end to design the ViewModel's properties. Combine the variable markers generated by the plugin end and the nodes in the template view tree to design the template UI generation. Combine the model and variable to construct differentiated ViewModels for each element and assemble the list MVVM code. Code concatenation, merging, and deletion are performed, and AI code optimization is implemented. Figure 28 This is a schematic diagram illustrating the attribute mapping and merging in the list items provided in an embodiment of this application. See also... Figure 28The current list item attributes are extracted, resulting in text node 1: text1, text node 2: text2, image node 1: link1 (url1), and image node 2: link2 (url2) for list item 1. Then, mapping relationships are read to obtain the mapping relationships between template node 1 and text node 1, template node 2 and text node 2, template node 3 and image node 1, and template node 4 and image node 2. Finally, mapping and merging are performed to obtain the ViewModel attributes, including: template node 1: text1, template node 2: text2, template node 3: link1, and template node 4: link2.
[0327] In iOS code generation, if the MVVM list pattern is selected and list item merging is not performed, six list UI files, six list ViewModel files, and six Model files will be generated. After merging, only one list UI file, one viewModel file, and one model file are needed, reducing the number of files by 83% and significantly improving code reusability and usability. Figure 29 This is a schematic diagram of the code file provided in the embodiment of this application without merging similar list items. Figure 29 There are 6 list UI files and 6 list ViewModel files in the code files on the right side. Figure 30 This is a schematic diagram of the code file for merging similar list items provided in an embodiment of this application. Figure 30 There is one list UI file and one list ViewModel file in the code file on the right.
[0328] Figure 31 This is a schematic diagram of the merged template view tree provided in an embodiment of this application. See also... Figure 31 Even list items with differences can be merged; the differing elements will initially exist as hidden elements in the template UI. After merging list items 1-4, the generated template view tree is displayed on the left. List item 1 contains the differing element "6000," and the element corresponding to this differing element "6000" in list items 2-4 is the button ">". Therefore, the node "Expand_down-R[v.merge]" corresponding to the differing element "6000" is merged into the template view tree. Figure 32 This is a schematic diagram of the merged code file provided in an embodiment of this application. See also... Figure 32 In iOS MVVM code generation, similar list items will use the same ViewModel file, and different UI styles will be displayed through data. List items 1-4 share the same ViewModel file. Figure 32The code file on the right includes code blocks 1-5, where code block 2 corresponds to list item 1, code block 3 corresponds to list item 2, code block 4 corresponds to list item 3, and code block 5 corresponds to list item 4. Code blocks 2-5 all include the same ViewModel file. See also... Figure 30 The merged board UI also needs to merge similar controls from the template UI to avoid duplicate and redundant UI elements in a large template.
[0329] This application embodiment generates reusable UI component code by detecting and recognizing the visual draft list area and merging and marking the subtrees of the difference list items. This significantly improves the usability and readability of the generated code and avoids the generation of redundant and useless code.
[0330] The following description continues to illustrate the exemplary structure of the code generation apparatus 455 provided in the embodiments of this application as a software module. In some embodiments, such as Figure 2 As shown, the software modules stored in the code generation apparatus 455 of the memory 450 may include:
[0331] The determination module 4551 is used to detect view components in the interface design image of the application interface, obtain multiple view components in the interface design image, and determine the view tree of each view component.
[0332] Grouping module 4552 is used to group multiple view trees to obtain multiple view tree groups;
[0333] The merging module 4553 is used to merge multiple view tree groups based on the similarity between view trees in every two view tree groups to obtain a template view tree;
[0334] The mapping module 4554 is used to determine the mapping relationship between the first node in the template view tree and the second node in each view tree, wherein the first node represents an element in the view component corresponding to the template view tree, and the second node represents an element in the view component corresponding to the view tree.
[0335] Module 4555 is used to generate executable code for the application interface based on the template view tree and mapping relationships.
[0336] In some embodiments, the grouping module 4552 is further configured to obtain a preset first similarity threshold; when the similarity between any two view trees is greater than or equal to the first similarity threshold, the two view trees are grouped into the same view tree group.
[0337] In some embodiments, the grouping module 4552 is further configured to obtain a first view tree and a second view tree among multiple view trees; when a node in the first view tree includes a node in the second view tree, determine that there is an inclusion relationship between the first view tree and the second view tree; and divide the first view tree and the second view tree with the inclusion relationship into the same view tree group.
[0338] In some embodiments, the merging module 4553 is further configured to select two view tree groups from multiple view tree groups, and use the two view tree groups as a first view tree group and a second view tree group; use the view tree with the most nodes in the first view tree group as a third view tree, and use the view tree with the most nodes in the second view tree group as a fourth view tree; determine the first size of the view component corresponding to the third view tree, and the second size of the view component corresponding to the fourth view tree; and merge the first view tree group and the second view tree group based on the first size, the second size, and the similarity between the third view tree and the fourth view tree to obtain a template view tree.
[0339] In some embodiments, the merging module 4553 is further configured to obtain a preset second similarity threshold when the difference between the first size and the second size is less than or equal to the size threshold; and to merge the third view tree and the fourth view tree to obtain a template view tree when the similarity between the third view tree and the fourth view tree is greater than or equal to the second similarity threshold.
[0340] In some embodiments, the merging module 4553 is further configured to: use the view tree with the most nodes among the third view tree and the fourth view tree as the target view tree; use the other view tree among the third view tree and the fourth view tree excluding the target view tree as the fifth view tree; based on the node characteristics of each third node in the target view tree and the node characteristics of each fourth node in the fifth view tree, select at least one difference node from the plurality of fourth nodes, wherein the third node represents an element in the view component corresponding to the target view tree and the fourth node represents an element in the view component corresponding to the fifth view tree; and insert at least one difference node into the target view tree to obtain a template view tree.
[0341] In some embodiments, the merging module 4553 is further configured to, for each difference node, determine the target node level in the target view tree where the difference node is to be inserted from multiple node levels; determine the node constraint relationship between the third node in the target node level and the difference node; insert the difference node in the target view tree based on the target node level and the node constraint relationship; bind view variables to the difference node inserted into the target view tree; and determine the target view tree with bound view variables as a template view tree, wherein the view variables are used to control the display state of the difference node.
[0342] In some embodiments, the generation module 4555 is further configured to generate a code framework based on a template view tree; generate component code for each view tree; merge multiple component codes based on mapping relationships to obtain target component code; and add the target component code to the code framework to obtain executable code for the application interface.
[0343] In some embodiments, the code generation apparatus 455 further includes a similarity calculation module, configured to arbitrarily select two view trees from a plurality of view trees; determine the tree edit distance between the two view trees; determine the maximum number of nodes from the number of nodes in the two first view trees; determine the ratio of the tree edit distance to the maximum number of nodes; and determine the similarity between the two view trees based on the ratio.
[0344] In some embodiments, the similarity calculation module is further configured to determine multiple editing operation sequences, wherein the editing operation sequence includes multiple editing operations, which are used to transform one of the two view trees into another view tree; for each editing operation sequence, the weight of the editing operation is determined based on the operation type of the editing operation, and the weights of the multiple editing operations included in the editing operation sequence are summed to obtain the total weight of the editing operation sequence; the minimum value among the total weights corresponding to the multiple editing operation sequences is taken as the tree edit distance.
[0345] In some embodiments, the similarity calculation module is further configured to: determine a preset parameter value as the weight of the editing operation when the operation type of the editing operation is an insertion or deletion operation; determine the node type of the node pair corresponding to the editing operation when the operation type of the editing operation is a replacement operation, wherein the node pair includes a fifth node in one view tree and a sixth node in another first view tree, the fifth node representing an element in the view component corresponding to one view tree and the sixth node representing an element in the view component corresponding to another first view tree; determine the similarity of the node pair based on the text attributes corresponding to the fifth node and the text attributes corresponding to the sixth node when the node type is a text node; determine the similarity of the node pair based on the position information corresponding to the fifth node and the position information corresponding to the sixth node when the node type is an image node; and determine the weight of the editing operation based on the similarity of the node pair.
[0346] This application provides a computer program product, which includes a computer program or computer-executable instructions stored in a computer-readable storage medium. The processor of an electronic device reads the computer-executable instructions from the computer-readable storage medium and executes the computer-executable instructions, causing the electronic device to perform the code generation method described in this application.
[0347] This application provides a computer-readable storage medium storing computer-executable instructions or a computer program. When the computer-executable instructions or the computer program are executed by a processor, the processor will execute the code generation method provided in this application. For example, ... Figure 3 The code generation method is shown.
[0348] In some embodiments, the computer-readable storage medium may be a memory such as RAM, ROM, flash memory, magnetic surface memory, optical disk, or CD-ROM; or it may be a variety of devices including one or any combination of the above-mentioned memories.
[0349] In some embodiments, computer-executable instructions may take the form of programs, software, software modules, scripts, or code, written in any form of programming language (including compiled or interpreted languages, or declarative or procedural languages), and may be deployed in any form, including as stand-alone programs or as modules, components, subroutines, or other units suitable for use in a computing environment.
[0350] As an example, computer-executable instructions may, but do not necessarily, correspond to files in a file system. They may be stored as part of a file that holds other programs or data, for example, in one or more scripts in a Hyper Text Markup Language (HTML) document, in a single file dedicated to the program in question, or in multiple co-located files (e.g., files that store one or more modules, subroutines, or code sections).
[0351] As an example, computer-executable instructions can be deployed to execute on a single electronic device, or on multiple electronic devices located at one location, or on multiple electronic devices distributed across multiple locations and interconnected via a communication network.
[0352] In summary, the generated code in this application demonstrates high reusability: compared to mainstream code generation solutions, the list code generated by this invention can be reused to the greatest extent possible, with similar list item views sharing only one set of code, significantly reducing the amount of code and the number of code files. It also improves the readability of the generated code: the reusability of the list code aligns with the thinking of developers, making it easier to read and increasing its usability.
[0353] The above description is merely an embodiment of this application and is not intended to limit the scope of protection of this application. Any modifications, equivalent substitutions, and improvements made within the spirit and scope of this application are included within the scope of protection of this application.
Claims
1. A code generation method, characterized in that, The method includes: The interface design image of the application interface is subjected to view component detection to obtain multiple view components in the interface design image, and the view tree of each view component is determined. The multiple view trees are grouped to obtain multiple view tree groups; Based on the similarity between the view trees in every two view tree groups, the multiple view tree groups are merged to obtain a template view tree; Determine the mapping relationship between the first node in the template view tree and each second node in the view tree, wherein the first node represents an element in the view component corresponding to the template view tree, and the second node represents an element in the view component corresponding to the view tree; Based on the template view tree and the mapping relationship, the executable code for the application interface is generated.
2. The method according to claim 1, characterized in that, The process of grouping multiple view trees to obtain multiple view tree groups includes: Obtain the preset first similarity threshold; When the similarity between any two view trees is greater than or equal to the first similarity threshold, the two view trees are grouped into the same view tree group.
3. The method according to claim 1, characterized in that, The process of grouping multiple view trees to obtain multiple view tree groups includes: Obtain the first view tree and the second view tree from among the multiple view trees; When a node in the first view tree includes a node in the second view tree, it is determined that there is an inclusion relationship between the first view tree and the second view tree. The first view tree and the second view tree, which have an inclusion relationship, are grouped into the same view tree group.
4. The method according to claim 1, characterized in that, The step of merging the multiple view tree groups based on the similarity between view trees in every two view tree groups to obtain a template view tree includes: Two view tree groups are selected from the plurality of view tree groups, and the two view tree groups are used as the first view tree group and the second view tree group; The view tree with the most nodes in the first view tree group is designated as the third view tree, and the view tree with the most nodes in the second view tree group is designated as the fourth view tree. Determine the first size of the view component corresponding to the third view tree, and the second size of the view component corresponding to the fourth view tree; Based on the first size, the second size, and the similarity between the third view tree and the fourth view tree, the first view tree group and the second view tree group are merged to obtain a template view tree.
5. The method according to claim 4, characterized in that, The step of merging the first view tree group and the second view tree group based on the first size, the second size, and the similarity between the third view tree and the fourth view tree to obtain a template view tree includes: When the difference between the first size and the second size is less than or equal to the size threshold, a preset second similarity threshold is obtained; When the similarity between the third view tree and the fourth view tree is greater than or equal to the second similarity threshold, the third view tree and the fourth view tree are merged to obtain a template view tree.
6. The method according to claim 5, characterized in that, The process of merging the third view tree and the fourth view tree to obtain a template view tree includes: The view tree with the most nodes among the third view tree and the fourth view tree is taken as the target view tree, and the other view tree among the third view tree and the fourth view tree other than the target view tree is taken as the fifth view tree; Based on the node features of each third node in the target view tree and the node features of each fourth node in the fifth view tree, at least one difference node is selected from the plurality of fourth nodes, wherein the third node represents an element in the view component corresponding to the target view tree, and the fourth node represents an element in the view component corresponding to the fifth view tree; Insert the at least one difference node into the target view tree to obtain the template view tree.
7. The method according to claim 6, characterized in that, The target view tree has multiple node levels, and inserting the at least one difference node into the target view tree to obtain the template view tree includes: For each of the difference nodes, the target node level in the target view tree to which the difference node is to be inserted is determined from the plurality of node levels; Determine the node constraint relationship between the third node and the difference node in the target node hierarchy; Based on the target node hierarchy and the node constraint relationship, the difference node is inserted into the target view tree, and a view variable is bound to the difference node inserted into the target view tree. The target view tree bound with the view variable is determined as a template view tree, wherein the view variable is used to control the display state of the difference node.
8. The method according to any one of claims 1 to 7, characterized in that, The step of generating executable code for the application interface based on the template view tree and the mapping relationship includes: Based on the template view tree, a code framework is generated; Generate the component code for each of the aforementioned view trees; Based on the mapping relationship, the code of multiple components is merged to obtain the target component code; The target component code is added to the code framework to obtain the executable code of the application interface.
9. The method according to any one of claims 1 to 7, characterized in that, The method further includes: Select any two view trees from the plurality of view trees; Determine the tree edit distance between the two view trees; Determine the maximum number of nodes from the number of nodes in the two view trees; Determine the ratio of the tree edit distance to the maximum number of nodes, and determine the similarity between the two view trees based on the ratio.
10. The method according to claim 9, characterized in that, Determining the tree edit distance between the two view trees includes: A sequence of multiple editing operations is determined, wherein the sequence of editing operations includes multiple editing operations, which are used to transform one of the two view trees into the other view tree; For each editing operation sequence, the weight of the editing operation is determined based on the operation type of the editing operation, and the weights of the multiple editing operations included in the editing operation sequence are summed to obtain the total weight of the editing operation sequence; The minimum value among the total weights corresponding to the multiple edit operation sequences is taken as the tree edit distance.
11. The method according to claim 10, characterized in that, Determining the weight of an editing operation based on its operation type includes: When the editing operation is an insertion or deletion operation, a preset parameter value is determined as the weight of the editing operation; When the operation type of the editing operation is a replacement operation, the node type of the node pair corresponding to the editing operation is determined, wherein the node pair includes the fifth node in the first view tree and the sixth node in the other first view tree, the fifth node represents an element in the view component corresponding to the first view tree, and the sixth node represents an element in the view component corresponding to the other first view tree; When the node type is a text node, the similarity of the node pair is determined based on the text attributes corresponding to the fifth node and the text attributes corresponding to the sixth node; When the node type is an image node, the similarity of the node pair is determined based on the location information corresponding to the fifth node and the location information corresponding to the sixth node; The weight of the editing operation is determined based on the similarity of the node pairs.
12. A code generation device, characterized in that, The device includes: The determination module is used to detect view components in the interface design image of the application interface, obtain multiple view components in the interface design image, and determine the view tree of each view component. The grouping module is used to group the multiple view trees to obtain multiple view tree groups; The merging module is used to merge the multiple view tree groups based on the similarity between the view trees in every two view tree groups to obtain a template view tree; A mapping module is used to determine the mapping relationship between a first node in the template view tree and a second node in each view tree, wherein the first node represents an element in the view component corresponding to the template view tree, and the second node represents an element in the view component corresponding to the view tree; The generation module is used to generate executable code for the application interface based on the template view tree and the mapping relationship.
13. An electronic device, characterized in that, The electronic device includes: Memory is used to store executable instructions or computer programs. A processor, when executing computer-executable instructions or computer programs stored in the memory, implements the code generation method according to any one of claims 1 to 11.
14. A computer-readable storage medium storing computer-executable instructions or a computer program, characterized in that, When the computer-executable instructions or computer program are executed by a processor, they implement the code generation method according to any one of claims 1 to 11.
15. A computer program product comprising computer-executable instructions or a computer program, characterized in that, When the computer-executable instructions or computer program are executed by a processor, they implement the code generation method according to any one of claims 1 to 11.
