Automatic Code Template Extraction via Source Similarity

Resolve Bottlenecks,
Find Innovative Solutions
Generate Solutions

Solution Overview

Problem

Existing code generation software requires manually created, pre-defined templates that are not adaptable or reusable across multiple projects, making it difficult to maintain and evolve templates effectively as programming languages and technologies evolve.

Innovation Solution

A system and method for automatic template extraction based on source code similarity, which classifies, clusters, and selects features from class files using naming rules and inheritance hierarchies, generates a template with a domain model, and creates a variable definition file to produce adaptable and reusable code templates for entire projects or multiple projects.

Engineering Contradictions & Design Principles

VSEngineering Contradiction Analysis

1Ease of manufacture

If templates are manually created by experienced developers, then code generation capability is achieved, but template maintenance becomes difficult and time-consuming as technology evolves

Engineering Contradiction:
Improvetemplate creationVSAvoidtemplate maintenance
Core Design Contradiction:
Ease of manufactureVSLoss of time

Solution Approach 1:

The system automatically extracts templates from existing source code files without requiring manual creation by developers. The template extraction process is self-service, analyzing code patterns, identifying reusable structures, and generating templates autonomously from the codebase.

Inventive Principle:
Principle #25Self-service

Solution Approach 2:

The system performs preliminary template extraction from existing source code before code generation is needed. By pre-analyzing and extracting templates from the codebase, the system prepares reusable patterns in advance, eliminating the need for manual template creation and maintenance as technology evolves.

Inventive Principle:
Principle #10Preliminary action

2Productivity

If pre-defined templates are used for code generation, then code generation speed is improved, but template adaptability across multiple projects is reduced

Engineering Contradiction:
Improvecode generation speedVSAvoidtemplate reusability
Core Design Contradiction:
ProductivityVSAdaptability or versatility

Solution Approach 1:

The extracted templates are designed to be universal and adaptable across multiple projects and programming languages. The system identifies generalizable code patterns that can be applied to different contexts, making templates multi-functional rather than project-specific.

Inventive Principle:
Principle #6Universality (Multi-functionality)

Solution Approach 2:

The template extraction system is dynamic and can adapt to different programming languages and codebases. The templates are not static pre-defined structures but are dynamically extracted and can be updated based on the specific project context, maintaining both speed and adaptability.

Inventive Principle:
Principle #15Dynamics

3Manufacturing precision

If code generation software uses manually written templates, then template precision is achieved, but the complexity of template management increases

Engineering Contradiction:
Improvetemplate accuracyVSAvoidtemplate management system
Core Design Contradiction:
Manufacturing precisionVSDevice complexity

Solution Approach 1:

The system extracts templates directly from existing source code files, taking out reusable patterns from the actual codebase rather than relying on manually written templates. This extraction process automatically captures accurate code structures while eliminating the complexity of manual template management.

Inventive Principle:
Principle #2Taking out (Extraction)

Solution Approach 2:

The system creates copies of existing code patterns from source files to generate templates. By copying and generalizing real code structures, the system maintains accuracy while automating the template creation process, reducing management complexity.

Inventive Principle:
Principle #26Copying

4Reliability

If templates are extracted manually from earlier projects, then best practices are captured, but the ability to evolve templates with technology changes is hindered

Engineering Contradiction:
Improvebest practices captureVSAvoidtemplate evolution
Core Design Contradiction:
ReliabilityVSAdaptability or versatility

Solution Approach 1:

The template extraction system incorporates feedback loops that continuously analyze new source code and update templates accordingly. As new projects and code patterns are introduced, the system learns from them and evolves the templates, ensuring they remain current with technology changes while maintaining best practices.

Inventive Principle:
Principle #23Feedback

Solution Approach 2:

The template extraction process is dynamic and can be re-run on updated codebases to generate evolved templates. This allows templates to adapt and evolve with technology changes, capturing new best practices from emerging projects while maintaining the reliability of established patterns.

Inventive Principle:
Principle #15Dynamics

Data Source

PatentUS10296307B2Method and system for template extraction based on source code similarity
Publication Date: 2019.05.21 INTERNATIONAL BUSINESS MACHINE CORPORATION
  • US10296307B2 patent drawing
  • US10296307B2 patent drawing
  • US10296307B2 patent drawing

AI summary

The present invention is a system and method for template extraction based on source code similarity. The system receives source code and groups the class files into classes based on naming rules and inheritance hierarchy. Features are parsed for each class and encoded a float value. The classes are clustered based on similarities of the features. A similarity value is calculated for the classes in a cluster and potential candidate classes are selected based on the similarity value or inheritance hierarchy. A feature subset is selected across all candidate classes and differences in the features in the subset are determined. The features are then decoded and the differences are parameterized to generate a template. A variable definition file is created to cross-reference features and variables. Source code can then be generated using the template and the variable definition file.