Automatic reconstruction of code of embedded software
By using machine learning models in software development to automatically refactor based on code and prompt words and evaluate them, the problems of code complexity and reconstruction difficulty of embedded software are solved, and the code quality is improved and the function is maintained.
Patent Information
- Application Number
- CN202411381955.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Priority Date
- 2023-09-28
- Filing Date
- 2024-09-30
- Publication Date
- 2025-03-28
AI Technical Summary
In software development, the high complexity of embedded software makes it challenging to automatically but still reliably refactor code, especially in maintaining consistency of code functionality and improving code quality.
Automatic refactoring of software code using machine learning models based at least on code and prompt words, and the refactored code is evaluated to ensure quality and functional equivalence.
It realizes automatic code reconstruction without affecting the software functions, improves the quality of the code and software, ensures the reliability and security of the refactored code, and reduces maintenance costs.
Smart Images

Figure CN119718414A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to a computer-implemented method for automatically refactoring the code of software. The present disclosure also relates to a computer-implemented method for further training a machine learning model and / or other machine learning models. Background Art
[0002] Embedded software is used to control, regulate and / or monitor engineering systems, in particular cyber-physical systems such as computing units of vehicles and / or robots, and is often of high complexity. Due to this, it is a challenge for individual software engineers and even entire software development departments to maintain an overview of the software and its changes, in particular throughout its entire life cycle (development, testing, production and maintenance).
[0003] In software development, code reconstruction (English: refactoring) refers to structural improvements to the code while maintaining observable program behavior (i.e., functionality). Here, for example, readability, comprehensibility, maintainability, and / or scalability should be improved, with the goal of reducing the corresponding costs of error analysis and / or functional expansion. Typical reconstructions are, for example, renaming variables to more descriptive names and / or extracting code parts into their own methods. The quality of the code and software is improved by reconstruction. However, the reconstruction possibilities must first be identified, and then the associated reconstruction is performed without error. Due to the high complexity of software that also changes frequently, and especially when different software engineers handle the code, it is expected that the code can be reconstructed at the lowest possible cost and whenever necessary.
[0004] Since the functionality and safety of engineering systems must always be guaranteed, the software code must be validated and / or verified (V&V) accordingly. In particular, the reconstruction of the code must always be reliable.
[0005] The present disclosure is therefore based on the problem of refactoring the code of software in an automated but still reliable manner, in particular in order to improve the software. Summary of the invention
[0006] A first general aspect of the present disclosure relates to a computer-implemented method for automatically refactoring software code. The method includes: refactoring the software code via a machine learning model based at least on the code and a prompt word, wherein the code is refactored. The method also includes evaluating the refactored code, wherein an evaluation result is obtained.
[0007] The software may be designed for: controlling, regulating and / or monitoring an engineering system, in particular a cyber-physical system, in particular at least one computing unit of a vehicle. In particular, the software may be embedded software. The method may comprise executing (possibly compiled) reconstructed code, and thus the software. The method may be performed in an electronic programming environment.
[0008] A second general aspect of the present disclosure relates to a computer-implemented method for further training a machine learning model and / or other machine learning models, wherein the machine learning model is designed to: reconstruct the code of the software based at least on the code and prompt words, and the other machine learning model is designed to: generate at least one refactoring suggestion for reconstructing the code based at least on the code and other prompt words, wherein the method includes: adapting the machine learning model and / or other machine learning model based at least on at least one reconstructed code and at least one evaluation result, wherein the at least one evaluation result is obtained by evaluating at least one reconstructed code. The method according to the second general aspect (or embodiment) can be, but not necessarily, performed after the method according to the first general aspect (or its embodiment).
[0009] A third general aspect of the present disclosure relates to a computer system designed to: execute a computer-implemented method for automatically reconstructing software code according to the first general aspect (or its embodiments) and / or execute a computer-implemented method for further training a machine learning model and / or other machine learning models according to the second general aspect (or its embodiments).
[0010] A fourth general aspect of the present disclosure relates to a computer program designed to: execute a computer-implemented method for automatically reconstructing the code of software according to the first general aspect (or its embodiments) and / or execute a computer-implemented method for further training a machine learning model and / or other machine learning models according to the second general aspect (or its embodiments).
[0011] A fifth general aspect of the present disclosure relates to a computer-readable medium or signal storing and / or containing a computer program according to the fourth general aspect (or an embodiment thereof).
[0012] The method according to the first aspect (or its embodiment) proposed in the present disclosure is directed to automatically reconstructing software code. The quality of the code and thus the software can be improved by the method proposed here. This is achieved on the one hand by a machine learning model that has a sufficient understanding of the machine language, and on the other hand by automatically evaluating the reconstructed code according to the established V&V method. In this way, the machine creativity of the machine learning model can be used, but at the same time the quality of the reconstructed code is ensured. In other words: errors that may occasionally be formed by the machine learning model, especially reconstructed code with errors, are reliably or at least with a sufficiently high probability identified by the V&V method before the reconstructed code is used to control, regulate and / or monitor the engineering system. In this way, the function and safety of the software and, for example, the engineering system controlled, regulated and / or monitored by the software, such as a vehicle or a robot, can be improved. In addition, due to the high degree of automation, it is also feasible to reconstruct as frequently as necessary during the life cycle of the software, that is, whenever necessary, and without great expenditure. In this way, the software and thus the engineering system can also be improved.
[0013] During refactoring, further target requirements for the reconstructed code can be preset, for example, by prompt words and / or by other inputs of the machine learning model. For example, in addition to the functional equivalence of the code and the reconstructed code, the quality of the code, especially the maintainability, can also be improved in a targeted manner. Alternatively or additionally, it can also be preset that the reconstructed code meets one or more specifications. In addition to the specifications of the software itself, it can also be required to meet higher-level specifications such as ISO 26262. For example, in this case, the recursion in the code must be replaced by other loops because recursion is not allowed according to ISO26262. Alternatively or additionally, further target requirements can also include one or more requirements that are still functionally important although they do not affect the functional equivalence. Therefore, for example, in addition to functional equivalence, it can also be required to reduce memory consumption and / or comply with stricter runtime requirements. In addition, a target requirement can also be provided: that is, not only the code should be reconstructed, but also translated into another programming language (for example, from C to Rust).
[0014] Due to the better maintainability, comprehensibility and / or modifiability of the code, the software can be further developed more efficiently and less prone to errors. This also reduces the time and costs of software development, at least without compromising the quality of the software. Automated quality improvement is also advantageous in particular in terms of the maintainability of the code, which software engineers unfortunately often lack the time to take care of. Still, poor maintainability should be avoided - as the method proposed here does - because in the long run, it leads to a significant increase in maintenance costs and is more prone to errors when making subsequent changes.
[0015] Another advantage is that the resulting refactoring suggestions and / or the generated plurality of refactored codes can be used to train a domain-specific refactoring assistant. This can be done, for example, as in the method 200 according to the second general aspect (or its embodiments). This allows supervised fine-tuning and / or unsupervised (reinforcement) learning to be performed based on the evaluation results, thereby improving the method according to the first general aspect (or its embodiments). Thus, this allows (future) codes to be refactored even better. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] Figure 1a An exemplary embodiment of a computer-implemented method for automatically refactoring software code is schematically illustrated.
[0017] Figure 1b An exemplary embodiment of a computer-implemented method for automatically refactoring software code is schematically illustrated, wherein at least one refactoring suggestion is implemented.
[0018] Figure 2a An exemplary machine learning model for mapping at least code and prompt words to reconstructed code is schematically shown.
[0019] Figure 2b An exemplary linking of two machine learning models is schematically shown, wherein at least code and hint words are first mapped to at least one refactoring suggestion and then mapped to the refactored code.
[0020] Figure 3 Schematically illustrates the evaluation of refactored code. DETAILED DESCRIPTION
[0021] The method 100 proposed in the present disclosure is directed to automatically refactoring software code. The software may be designed to: control, regulate and / or monitor an engineering system, in particular a cyber-physical system, in particular at least one computing unit of a vehicle. In particular, the software may be embedded software, which is designed to: be executed on an embedded (i.e., for example, task-specific) system.
[0022] To this end, a computer-implemented method 100 for automatically refactoring software code is first disclosed. Figure 1a -b. The method 100 comprises reconstructing 140 the code 10 of the software via the machine learning model 30 based at least on the code 10 and the prompt word 20, wherein the code 10 is reconstructed. The method further comprises evaluating 150 the reconstructed code 40, wherein an evaluation result is obtained.
[0023] The code 10 of the software can be the source code of the software. The code 10 can be written in one or more programming languages. For example, the code 10 can be written in the C programming language. Alternatively or additionally, for example, the code 10 can be written in the Rust programming language. Since the machine learning model has enough language understanding, it is not necessary to specify the programming language. In other words, the method 100 can be applied to any code regardless of the programming language.
[0024] Refactoring 140 the code 10 may include changing the code 10 or may be the changing code 10. The refactored code 40 may also be the code of the software, in particular the source code of the software.
[0025] The machine learning model 30 may include or be a base model (e.g., Basismodell in German). The base model may be a large machine learning model that has been trained with a large amount of data (usually through self-supervised learning or semi-supervised learning) so that the machine learning model is adaptable to a wide range of downstream tasks. In particular, the machine learning model may include or be a large language model (LLM). The large language model may be a language model characterized by its size. In particular, the large language model may be a chatbot and / or have chatbot functionality.
[0026] For example, MetaAI LLaMA can be used as a large language model. Such a large language model can be advantageous because it and in particular its weights and / or distortions can be adapted, for example, by method 200. Alternatively or additionally, for example, Google BERT can be used. Alternatively or additionally, OpenAI ChatGPT (e.g., version as of May 24, 2023) can be used as a large language model. Alternatively or additionally, for example, Hugging Face Bloom can be used as a large language model.
[0027] Alternatively or additionally, the machine learning model may include or be a multi-domain model. For example, OpenAI GPT-4 (e.g., the version as of March 14, 2023) may be used herein.
[0028] Figure 2a -b schematically shows an exemplary implementation of the machine learning model 30.
[0029] The prompt word 20 may be a natural language text. The prompt word 20 may include or be a natural language instruction to the machine learning model 30. For example, for Figure 2a In the machine learning model 30, the prompt words 20 may include language instructions for a large language model (LLM) intended to reconstruct the code 10 of the software.
[0030] Alternatively or additionally, the prompt word 20, for example Figure 2b The machine learning model 30 in may include language instructions to a large language model (LLM), wherein the language instructions are intended to implement at least one refactoring suggestion 50 so that the software code 10 is refactored.
[0031] Alternatively or additionally, the prompt words 20 may include linguistic instructions to a large language model (LLM) intended to expand, improve and / or correct the predetermined reformulation suggestions.
[0032] The evaluation result may include a natural language text or such a natural language text. Alternatively or additionally, the evaluation result may include a data structure written in a predetermined grammar (e.g., in a programming language) or such a data structure. The data structure may include, for example, a natural language text. The evaluation result may include one or more numerical values, in particular one or more confidence values. Thus, the quality of the reconstructed code 40 and then the quality of at least one reconstructing suggestion 50 may be encoded, and the quality may be considered when confirming and verifying the reconstructed code and then the software. The code 40 reconstructed by evaluation 150 may be regarded as confirmation and / or verification (V&V).
[0033] If it is not possible to generate a refactored code, the evaluation result may include information about the failure to generate a refactored code. This may happen, for example, if the code is already contradictory and / or damaged. Said information is also valuable for the development of engineering systems. In this case, the code must then be adapted and in particular improved. On the other hand, if the code has already been (satisfactorily) refactored, refactoring may no longer be necessary. In this case, there is no need for any operation.
[0034] For example, in Figure 3 As schematically shown in , the generated reconstructed code can be evaluated, wherein for example the evaluation result includes a result of OK or nOK (short for: not OK). In the case of an nOK result, the reconstructed code can be repaired ("repaired" or "repaired"), wherein the reconstructed code generated in an earlier iteration of the method 100 can, for example, be adapted, improved and / or corrected in a further iteration of the method 100.
[0035] The method 100 may include executing 180 the (possibly compiled) reconfigured code 10 and thus the software. The engineering system may thus be controlled, regulated and / or monitored by executing the (compiled) reconfigured code.
[0036] Method 100 can be executed in an electronic programming environment. The electronic programming environment can be, but does not have to be, an execution environment. The electronic programming environment can be arranged above the execution environment. For example, the execution environment can be initialized and then controlled by the electronic programming environment. In addition, for example, one or more execution results can be returned from the execution environment to the electronic programming environment. The electronic programming environment enables programming and / or interaction of multiple iterations of method 100. In particular, the electronic programming environment proves to be advantageous when initializing an iteration and / or debugging during an iteration. For example, prompt words 20 and / or other prompt words 21 can be adapted in the electronic programming environment.
[0037] For example Figure 1b As schematically shown in FIG. 1 , refactoring 140 the code 10 of the software may include implementing 141 at least one refactoring suggestion 50 via the machine learning model 30. By implementing 141 at least one refactoring suggestion 50, the code 10 may be refactored, ie in particular changed. On the other hand, as for example Figure 1a As schematically shown in FIG. , if, for example, Figure 2a If the refactored code schematically shown in is already the output of the machine learning model 30, then implementing 141 at least one refactoring suggestion 50 may be unnecessary.
[0038] For example Figure 1b and Figure 2b As schematically shown in , the method 100 may include generating 130 at least one refactoring suggestion 50 for refactoring the code 10 based on at least the code 10 and other prompt words 21 via other machine learning models 31 .
[0039] The step of generating 130 at least one reformulation suggestion 50 may be performed before implementing 141 at least one reformulation suggestion 50. Alternatively or additionally, at least one predetermined reformulation suggestion may be preset as another input to another machine learning model 31 or via another prompt word 21 .
[0040] One or more predetermined reconstruction suggestions may also be fixedly preset, or received from a user, for example, via user input of an electronic programming environment. In other words, the one or more predetermined reconstruction suggestions do not have to be generated 130 via other machine learning models 31, but may also be determined in advance by other techniques (e.g., via bad smell detection).
[0041] Other machine learning models 31 may include or be base models. In particular, other machine learning models may include or be large language models (LLM). Alternatively or additionally, other machine learning models may include or be multi-domain models. Other machine learning models 31 (used to generate 130 at least one refactoring suggestion 50 for refactoring code 10) may, but need not be, machine learning model 30 (used to refactor 140 code and possibly to implement 141 at least one refactoring suggestion 50). Other prompt words 21 may, but need not include prompt words 20.
[0042] Other prompt words 21 may be natural language text. Other prompt words 21 may include or be natural language instructions to the machine learning model 31. Other prompt words may include language instructions to a large language model (LLM) to generate at least one refactoring suggestion 50 based on at least the code 10.
[0043] The method 100 may include receiving 110 a code 10. Alternatively or additionally, the method 100 may include receiving 120 a prompt word 20 and / or further prompt words 21. Alternatively or additionally, the method 100 may include receiving at least one refactoring suggestion 50. Alternatively or additionally, the method 100 may include receiving a machine learning model 30 and / or further machine learning model 31.
[0044] At least one refactoring suggestion 50 may include one or more instructions on how the code 10 of the software may be changed so that the functionality of the software remains the same but satisfies at least one target requirement of the code 10. The at least one target requirement may require at least improving the quality of the code 10.
[0045] Improving the quality of the code 10 may be intended to: reduce the corresponding costs of error analysis and / or functional expansion. For example, improving the quality of the code 10 may include improving the readability of the code 10. Alternatively or additionally, improving the quality of the code 10 may include, for example, improving the comprehensibility of the code 10. Alternatively or additionally, improving the quality of the code 10 may include, for example, improving the maintainability of the code 10. Alternatively or additionally, improving the quality of the code 10 may include, for example, improving the scalability of the code 10 (or software). The prompt word 20 and / or other prompt words 21 may include such a preset. Alternatively, the prompt word and / or other prompt words may be considered as other inputs to the machine learning model 30 and / or other machine learning models 31, respectively.
[0046] The instructions may be in a natural language. Alternatively or additionally, the instructions may include a data structure including the proposed changes to the code 10 , in particular encoding the proposed changes to the code 10 .
[0047] At least one target requirement or another target requirement regarding the change of the code 10 can include, for example, a requirement for the memory requirement when executing the software. For example, such a requirement can be based on an (absolute) memory capacity. Here, for example, it can be provided that the memory requirement should not exceed the maximum memory capacity when executing the software. Alternatively or additionally, the requirement can be based, for example, on a (relative) memory requirement change expectation. For example, it can be provided that the memory requirement should change to a fraction (e.g. 90%) of the current memory requirement.
[0048] Alternatively or additionally, at least one target requirement or another target requirement regarding the change of the code 10 may include, for example, a requirement for the runtime when executing the software. For example, such a requirement may be based on an (absolute) runtime. For example, it may be provided here that the runtime when executing the software should not exceed a maximum runtime. Alternatively or additionally, the requirement may be based, for example, on a (relative) runtime change expectation. For example, it may be provided here that the runtime change is a fraction (e.g. 80%) of the current runtime.
[0049] Alternatively or additionally, at least one target requirement or a further target requirement regarding the change of the code 10 may, for example, comprise a requirement that the refactored code should be written in a different programming language.
[0050] Some specific factors are disclosed below, to which the refactoring and / or generating at least one refactoring suggestion may be related.
[0051] Reconstructing 140 the code of the software via the machine learning model 30 may be based at least on the natural language and / or formal specification of the software. For example, the natural language and / or formal specification of the software may be considered as an additional input to the machine learning model 30 when reconstructing 140. Alternatively, the natural language and / or formal specification of the software may be included in the reconstruction 140 via the prompt word 20.
[0052] Alternatively or additionally, generating 130 the at least one refactoring suggestion 50 via the other machine learning model 31 may be based at least on a natural language and / or formal specification of the software. The natural language and / or formal specification of the software may, for example, be considered as a further input to the other machine learning model 31 when generating 130 the at least one refactoring suggestion 50. Alternatively, the natural language and / or formal specification of the software may be included in generating 130 the at least one refactoring suggestion 50 via the other prompt words 21.
[0053] For example, the natural language and / or formal specification of the software may include one or more valid ranges of parameters of the code 10 (or software). Alternatively or additionally, the natural language and / or formal specification of the software may include, for example, a description of the (desired) functionality of the software. Alternatively or additionally, the natural language and / or formal specification of the software may, for example, include the purpose to be achieved by the software.
[0054] Alternatively or additionally, refactoring 140 the software code via the machine learning model 30 may be based at least on at least one predetermined refactoring suggestion 51. For example, Figure 2a As schematically shown, at least one predetermined refactoring suggestion 51 may be considered as a further input to the machine learning model 30 when refactoring 140. Alternatively, at least one predetermined refactoring suggestion 51 may be included in the refactoring 140 via the prompt word 20.
[0055] Alternatively or additionally, generating 130 at least one refactoring suggestion 50 via another machine learning model 31 may be based at least on a predetermined refactoring suggestion 51. Figure 2b As schematically shown in , the predetermined refactoring suggestion 51 may be considered as a further input to the other machine learning model 31 when generating 130 at least one refactoring suggestion 50. Alternatively, the predetermined refactoring suggestion 51 may be included into the generation 130 of at least one refactoring suggestion 50 via other prompt words 21.
[0056] The predetermined refactoring suggestion 51 may include general suggestions on how the code 10 should be refactored. For example, the general advice may presuppose that recursions in the code 10 should be replaced by other loops (see ISO 26262). Alternatively or additionally, the general advice may, for example, presuppose that dynamic memory allocation should not be performed in the code 10. Alternatively or additionally, the predetermined refactoring suggestion 51 may include suggestions on how an exemplary portion of the code 10 should be changed. In another example, the predetermined refactoring suggestion 51 may be generated in an earlier iteration of the method 100, which should now be expanded, improved and / or corrected in the current iteration of the method 100. To this end, the prompt word 20 may also be adapted for the current iteration if necessary.
[0057] Refactoring 140 the code of the software or implementing 141 at least one refactoring suggestion 50 via the machine learning model 30 may include implementing 142 a plurality of refactoring suggestions via the machine learning model 30. The method 100 may include implementing a plurality of refactoring suggestions (i.e., at least two refactoring suggestions) via the machine learning model 30 based at least on the cue word 20 and / or the other cue word 21. One or more of the plurality of refactoring suggestions may, for example, be implemented based at least on the cue word 20 and / or the other cue word 21, i.e., in a (single) execution of the method 100. Alternatively or additionally, one or more of the plurality of refactoring suggestions may be implemented by multiple executions of the method 100. Advantageously, the cue word 20 and / or the other cue word 21 may be changed in multiple executions of the method 100. On the other hand, the cue word 20 and / or the other cue word 21 does not have to be changed in multiple executions of the method 100.
[0058] Implementing 141, 142 the at least one refactoring suggestion 50 may be performed automatically, ie without input from a user of the electronic programming environment. Alternatively, implementing 141, 142 the at least one refactoring suggestion 50 may be at least partially related to input from a user of the electronic programming environment.
[0059] Generate 130 at least one reconstruction suggestion 50 via other machine learning models 31 and / or Figure 1b The method 100 schematically shown in FIG. 1 may include generating 131 a plurality of refactoring suggestions for refactoring the code 10 based on the code 10 and other prompt words 21 via other machine learning models 31 .
[0060] The method 100 may include generating a plurality of refactoring suggestions (i.e., at least two refactoring suggestions). For example, one or more of the plurality of refactoring suggestions may be generated based at least on the code 10 and the other prompt words 21, i.e., in a (single) execution of the method 100. Alternatively or additionally, one or more of the plurality of refactoring suggestions may be generated by multiple executions of the method 100. Advantageously, the other prompt words 21 may be changed in multiple executions of the method 100. On the other hand, the other prompt words 21 do not have to be changed in multiple executions of the method 100. Alternatively or additionally, one or more of the plurality of refactoring suggestions may be predetermined. With respect to the high complexity of the code, a large number of refactoring suggestions may be required to refactor 150 the code.
[0061] For example, through Figure 3 Schematically shown by the “OK” in FIG. 1 , the method 100 may include retaining 160 the reconstructed code 40 according to a predetermined criterion based on the evaluation result. The method 100 may (but need not) be as follows, for example Figure 1aOtherwise (i.e., if not retained 160) as schematically illustrated in -b, includes discarding 161 the reconstructed code 40. For example, the reconstructed code can be repaired instead of discarded, see e.g. Figure 3 If the reconstructed code 40 is not retained 160 , it and / or knowledge derived therefrom may be considered in a new iteration of the method 100 .
[0062] Evaluating 150 the reconstructed code 40 may include checking whether the code 10 and the reconstructed code 40 are consistent in terms of their execution in at least one predetermined test case. In particular, evaluating 150 the reconstructed code 40 may include checking whether the code 10 and the reconstructed code 40 are consistent in terms of their execution in multiple predetermined test cases. To this end, for example, an execution environment (i.e., a test case) may be automatically constructed by the method 100 in particular so as to execute the code 10 and the reconstructed code 40 in terms of at least one predetermined test case or multiple predetermined test cases. At least one predetermined test case or multiple predetermined test cases may be automatically generated. Alternatively or additionally, at least one predetermined test case or multiple predetermined test cases may be based on fuzz testing. Alternatively or additionally, at least one predetermined test case or multiple predetermined test cases may be based on derivation according to at least one contract. For example, a contract may include one or more annotations in the code, the annotations describing the characteristics that the code should satisfy.
[0063] Alternatively or additionally, at least one predetermined test case or a plurality of predetermined test cases may have been generated by a third machine learning model. The third machine learning model may be a machine learning model 30 (for refactoring 140 the code and possibly for implementing 141 at least one refactoring suggestion). Alternatively or additionally, the third machine learning model may be another machine learning model 31 (for generating 130 at least one refactoring suggestion for refactoring the code 10). On the other hand, the third machine learning model does not necessarily have to be a machine learning model 30 (for refactoring 140 the code and possibly for implementing 141 at least one refactoring suggestion) or another machine learning model 31 (for generating 130 at least one refactoring suggestion for refactoring the code 10). At least a portion of at least one predetermined test case or a plurality of predetermined test cases may have been verified as being different in an earlier iteration of the method 100. The test cases may be different in an earlier iteration of the method 100 if the code that was deterministic in an earlier iteration of the method 100 and the refactored code that was deterministic in an earlier iteration of the method 100 are inconsistent in their execution in the test cases. Differentiated tests and their passing are particularly convincing.
[0064] If the code 10 and the refactored code 40 do not agree in their execution in at least one test case, there is no functional equivalence. The refactored code 40 can (and must) be discarded.
[0065] If the inspection results in that the code 10 and the refactored code 40 are inconsistent in their execution in at least one predetermined test case, the method 100 may include marking the at least one predetermined test case as being different. As already discussed, for example, the at least one predetermined test case marked as being different may be used together with the possibly adapted prompt word 20 and / or the adapted other prompt words 21 to generate one or more other refactoring suggestions in a further iteration of the method 100.
[0066] Alternatively or additionally, evaluating 150 the refactored code 40 may include checking whether the refactored code 40 satisfies at least one predetermined contract. This may be performed in static testing. For example, this may be performed via bounded model checking. Alternatively or additionally, this may be performed, for example, via abstract interpretation. Evaluating 150 the refactored code 40 may include checking whether the refactored code 40 satisfies a plurality of predetermined contracts.
[0067] Alternatively or additionally, evaluating 150 the refactored code 40 may include checking whether the code 10 and the refactored code 40 are functionally identical. This may also be done via bounded model checking, for example.
[0068] Alternatively or additionally, evaluating 150 the reconstructed code 40 may include checking whether the reconstructed code 40 meets at least one target requirement. Checking whether the reconstructed code 40 meets at least one target requirement may include applying code metrics and / or test metrics to the reconstructed code 40. Here, in particular, a maintainability index may be calculated. For example, test coverage may be measured. Alternatively or additionally, other characteristics of the (reconstructed) code may be checked in static or dynamic analysis. Alternatively or additionally, the quality of the code may be based on a weighting of multiple code metrics and / or test metrics.
[0069] Refactored code may be generated and evaluated 150 for at least one or each of the plurality of refactoring suggestions. The refactored code may be retained or discarded, respectively, depending on the corresponding evaluation results, wherein in the case of discarding, the last refactored code retained is retained. In this case, for example, one or more refactoring suggestions may be generated in each step according to the method 100, on the basis of which the code is continuously refactored. In this way, code may be efficiently generated in which all refactoring suggestions have been implemented.
[0070] The method 100 may include outputting 170 the evaluation results. This may be done, for example, via an electronic programming environment. This is advantageous because the flow of the method 100 may be adapted according to the evaluation results. For example, refactoring suggestions and / or refactored code may thus be manually selected or picked out according to the evaluation results.
[0071] Alternatively or additionally, the method 100 may include outputting 171 the reconstructed code 40. This may also be done via an electronic programming environment.
[0072] Alternatively or additionally, the method 100 may comprise outputting 172 at least one refactoring suggestion 50. This may also be done via an electronic programming environment, for example.
[0073] The method 100 may be based on at least one input of a user of an interface of the electronic programming environment. For example, the method 100 and / or multiple executions thereof may be (interactively) controlled via the electronic programming environment. For example, this may be helpful when setting up a programming environment and / or an execution environment before automatically traversing the method 100. Alternatively or additionally, interactive control may be helpful when debugging an automatic traversal of the method 100.
[0074] Alternatively or additionally, before continuing with refactoring the code, interactive selection of a desired refactoring suggestion may be performed via input, for example, after a plurality of refactoring suggestions have been generated.
[0075] Method 100 can be repeated. Here, the machine learning model and / or the prompt word and other inputs as necessary can be changed. Alternatively or additionally, other machine learning models and / or other prompt words and possibly other inputs can also be changed as necessary.
[0076] Different refactoring suggestions may be generated by single and multiple executions of method 100. In a single execution of method 100, differences already exist due to the inherent variability of the machine learning model (temperature). If the method should generate, for example, 101 different refactoring suggestions based on (other) prompt words, then 101 different refactoring suggestions may actually be generated due to the inherent variability. Furthermore, due to the possible diversity of (other) prompt words, there are differences in multiple executions of method 100. Alternatively or additionally, there are differences due to different combinations of different input artifacts (e.g., code only, with specification requirements, etc.). If statically identifiable multiple occurrences of refactoring suggestions are generated (e.g., by independent multiple executions), then said refactoring suggestion may be selected.
[0077] Also disclosed is a computer-implemented method 200 for further training a machine learning model 30 and / or other machine learning models 31, wherein the machine learning model 30 is designed to reconstruct 140 a code 10 of a software based at least on the code 10 and a prompt word 20 and, if necessary, the other machine learning model 31 is designed to generate 130 at least one refactoring suggestion 50 for refactoring the code 10 based at least on the code 10 and other prompt words 21. The method 200 includes adapting the machine learning model 30 and / or other machine learning model 31 based at least on at least one refactored code 40 and at least one evaluation result, wherein the at least one evaluation result is obtained by evaluating 150 at least one refactored code 40.
[0078] In particular, a computer-implemented method 200 for further training a machine learning model 30 is disclosed, wherein the machine learning model 30 is configured to reconstruct 140 a code 10 of a software based at least on the code 10 and a prompt word 20. The method 200 includes adapting the machine learning model 30 based at least on at least one reconstructed code 40 and at least one evaluation result, wherein the at least one evaluation result is obtained by evaluating 150 the at least one reconstructed code 40.
[0079] Alternatively or additionally, a computer-implemented method 200 for further training a further machine learning model 31 is disclosed, wherein the further machine learning model 31 is configured to: generate 130 at least one refactoring suggestion 50 for refactoring the code 10 based at least on the code 10 and other prompts 21. The method 200 includes adapting the further machine learning model 31 based at least on at least one refactored code 40 and at least one evaluation result, wherein the at least one evaluation result is obtained by evaluating 150 the at least one refactored code 40.
[0080] Alternatively or additionally, the computer-implemented method 200 is used to further train the machine learning model 30 and the other machine learning model 31, wherein the machine learning model 30 is designed to: reconstruct 140 the code 10 of the software based at least on the code 10 and the prompt word 20, and the other machine learning model 31 is designed to: generate 130 at least one refactoring suggestion 50 for refactoring the code 10 based at least on the code 10 and the other prompt word 21. The method 200 includes adapting the machine learning model 30 and / or the other machine learning model 31 based at least on at least one refactored code 40 and at least one evaluation result, wherein the at least one evaluation result is obtained by evaluating 150 at least one refactored code 40.
[0081] The evaluation 150 may be, but need not be, part of the method 200. The at least one refactored code 40 may be generated 140 and evaluated 150 according to the method 100 for automatically refactoring a code 10 of software. The method 200 may be, but need not be, a continuation of the method 100.
[0082] Alternatively or additionally, method 100 may be re-executed via the adapted machine learning model and / or adapted other machine learning models. In particular, machine learning model 30 and / or other machine learning models 31 may be adapted between multiple executions of method 100 according to method 200.
[0083] The adaptation of the machine learning model 30 and / or other machine learning models 31 may be based at least on at least one code 10 to be reconstructed and at least on the prompt word 20 and / or other prompt words 21 .
[0084] The machine learning model 30 and / or the further machine learning model 31 can be adapted by supervised learning. Such an adaptation of the machine learning model 30 and / or the further machine learning model 31 can be regarded as supervised fine-tuning. Due to fine-tuning, a general machine learning model, for example, which has been trained for general machine language understanding, can be adapted in an application-specific manner, i.e., in this case in terms of generating refactoring suggestions and / or refactored code.
[0085] Fine-tuning of the machine learning model 30 and / or other machine learning models 31 may, but need not, precede further adaptation of the machine learning model 30 and / or other machine learning models 31 by unsupervised (reinforcement) learning.
[0086] Alternatively or additionally, adapting the machine learning model 30 and / or other machine learning model 31 based at least on at least one refactored code 40 and at least one evaluation result may include: calculating at least one reward based at least on at least one evaluation result, and adapting the machine learning model 30 and / or other machine learning model 31 based at least on at least one refactored code 40 and at least one reward. Such adaptation of the machine learning model 30 and / or other machine learning model 31 can be regarded as unsupervised (reinforcement) learning. Thus, for example, a general machine learning model trained for general machine language understanding and / or such a model after fine-tuning can be (further) adapted in an application-specific manner, i.e., here with respect to generating refactoring suggestions and / or refactored code. Thus, the machine learning model 30 and / or other machine learning model 31 can be better adapted to the application situation of the refactored code.
[0087] A reward may be a parameter, in particular a numerical parameter, which is similar to other parameters which are also rewards. For example, a reward may be greater than, equal to, or less than another reward.
[0088] If the at least one evaluation result is good, the at least one reward may be higher, and if the at least one evaluation result is poor, the at least one reward may be lower.
[0089] If the check on which the evaluation result is based has appeared negative, ie, for example, has not passed, the evaluation result will be poor, in particular, poor. Alternatively or additionally, if all checks appear to be positive, ie, passed, the evaluation result will be good, in particular, good.
[0090] The machine learning model 30 and / or other machine learning models 31 can be adapted based on at least multiple reconstructed codes and multiple corresponding rewards. The method here may include calculating rewards based on at least multiple evaluation results of each reconstructed code. In other words, the reward for the reconstructed code needs to be related not only to its evaluation results, but also to one or more evaluation results of other reconstructed codes. For example, one or more rewards (i.e., how many) may be related to multiple evaluation results. Alternatively or additionally, one or more rewards may be related to multiple better evaluation results and multiple worse evaluation results, in particular to the imbalance between better and worse evaluation results. Alternatively or additionally, the reward for each evaluation result may be calculated first, and then adapted and / or offset based on other evaluation results.
[0091] The adapted machine learning model 30 and / or other machine learning models 31 may be based on a reinforcement learning algorithm such as proximal policy optimization (PPO).
[0092] A part of the machine learning model 30 and / or a part of the other machine learning model 31 cannot be (intentionally) adapted, i.e. fixed. Thus, here, only another part of the machine learning model 30 and / or a part of the other machine learning model 31 is adapted.
[0093] For example, certain parameters such as weights and / or distortions, in particular weights and / or distortions on earlier layers of the machine learning model 30 and / or the other machine learning model 31, cannot be adapted, i.e. are fixed. For example, the adaptation may be limited to later layers of the machine learning model 30 and / or the other machine learning model 31, where refactoring suggestions are made and / or refactoring is performed. It may thereby be ensured that the machine learning model 30 and / or the other machine learning model 31 does not degrade in terms of machine language understanding with respect to costs. On the other hand, degradation in machine language understanding may be addressed by adaptation to the extent that it has no effect on refactoring the code. For example, machine language understanding of Shakespeare's English is not (usually) required for code refactoring.
[0094] The method 200 may be controlled via an electronic programming environment. In particular, the machine learning model 30 and / or other machine learning models 31 may be further adapted via the electronic programming environment.
[0095] A computer system is also disclosed, the computer system being designed to: execute a computer-implemented method 100 for automatically refactoring a code 10 of software. Alternatively or additionally, the computer system may be designed to: execute a computer-implemented method 200 for further training a machine learning model 30 and / or other machine learning models 31. In particular, the computer system may be designed to: execute the computer-implemented method 100 for automatically refactoring a code 10 of software and (e.g. subsequently) execute a computer-implemented method 200 for further training a machine learning model 30 and / or other machine learning models 31. The computer system may include a processor and / or a working memory.
[0096] A computer program is also disclosed, the computer program being designed to: perform a computer-implemented method 100 for automatically refactoring a code 10 of software. Alternatively or additionally, the computer program may be designed to: perform a computer-implemented method 200 for further training a machine learning model 30 and / or other machine learning models 31. In particular, the computer program may be designed to: perform a computer-implemented method 100 for automatically refactoring a code 10 of software and (e.g. subsequently) perform a computer-implemented method 200 for further training a machine learning model 30 and / or other machine learning models 31. For example, the computer program may be in an interpretable or compiled form. The computer program may be (even partially) loaded into the RAM of a computer for execution, for example as a sequence of bits or bytes.
[0097] A computer-readable medium or signal storing and / or containing a computer program is also disclosed. The medium may include, for example, one of a RAM, ROM, EPROM, HDD, SSD, ... on which / in which a signal is stored.
Claims
1. A computer-implemented method (100) for automatically refactoring software code, the method comprising: - reconstructing (140) the code (10) of the software via a machine learning model (30) based at least on the code (10) and the prompt word (20), wherein the code (10) is reconstructed; -Evaluate (150) the reconstructed code (40), wherein an evaluation result is obtained.
2. The method (100) of claim 1, wherein refactoring (140) the code (10) of the software comprises implementing (141) at least one refactoring suggestion (50) via the machine learning model (30), wherein the method comprises: - generating (130) at least one refactoring suggestion (50) for refactoring the code (10) via another machine learning model (31) based at least on the code (10) and other prompt words (21).
3. The method (100) according to claim 1 or 2, wherein the software is designed to: control, regulate and / or monitor an engineering system, in particular a cyber-physical system, in particular at least one computing unit of a vehicle.
4. The method (100) according to any one of the preceding claims, comprising: - retaining (160) the reconstructed code (40) according to a predetermined criterion based on the evaluation result; - Otherwise, optionally, discarding (161) the reconstructed code (40).
5. The method (100) according to any one of the preceding claims, wherein evaluating (150) the reconstructed code (40) comprises: - Checking whether the code (10) and the reconstructed code (40) are identical with respect to their execution in at least one predetermined test case.
6. The method (100) of claim 5, wherein the at least one predetermined test case has been demonstrated to be discriminating in an earlier iteration of the method (100).
7. The method (100) according to any one of the preceding claims, wherein evaluating (150) the reconstructed code (40) comprises: - optionally checking via bounded model checking and / or abstract interpretation whether the refactored code (40) satisfies at least one predetermined contract.
8. The method (100) according to any one of the preceding claims, wherein evaluating (150) the reconstructed code (40) comprises: - checking whether the code (10) and the refactored code (40) are functionally identical, optionally via bounded model checking.
9. The method (100) according to any one of the preceding claims, wherein evaluating (150) the reconstructed code (40) comprises: - Checking whether the reconstructed code (40) meets at least one target requirement.
10. The method (100) according to claim 9, wherein checking whether the refactored code (40) meets at least one target requirement comprises: - Application of code metrics and / or test metrics based on said refactored code (40), in particular based on calculation of a maintainability index.
11. A computer-implemented method (200) for further training a machine learning model (30) and / or other machine learning models (31), The machine learning model (30) is designed to: reconstructing (140) the code (10) of the software based at least on the code (10) and the prompt word (20), and; The other machine learning model (31) is designed to: generate (130) at least one refactoring suggestion (50) for refactoring the code (10) based at least on the code (10) and other prompt words (21); the method (200) comprises: - adapting the machine learning model (30) and / or other machine learning models (31) at least based on at least one reconstructed code (40) and at least one evaluation result, wherein the at least one evaluation result is obtained by evaluating (150) the at least one reconstructed code (40); The at least one refactored code (40) has been generated (140) and evaluated (150) according to the method (100) for automatically refactoring software code (10) according to any one of the preceding claims.
12. The method (200) according to claim 11, wherein adapting the machine learning model (30) and / or other machine learning models (31) based at least on at least one reconstructed code (40) and at least one evaluation result comprises: - calculating at least one reward based at least on the at least one evaluation result; - Adapting the machine learning model (30) and / or other machine learning models (31) based at least on the at least one refactored code (40) and the at least one reward.
13. A computer system designed for A computer-implemented method (100) for executing a code (10) for automatically refactoring software according to any one of claims 1 to 10, and / or A computer-implemented method (200) for further training a machine learning model (30) and / or further machine learning models (31) according to claim 11 or 12 is performed.
14. A computer program designed to A computer-implemented method (100) for executing a code (10) for automatically refactoring software according to any one of claims 1 to 10 and / or A computer-implemented method (200) for further training a machine learning model (30) and / or further machine learning models (31) according to claim 11 or 12 is performed.
15. A computer readable medium or signal storing and / or containing a computer program according to claim 14.