FractalFathom CLI is a Kotlin-based command-line tool designed to generate functional component diagrams for software using static code analysis and deep learning models. By integrating GraphCodeBERT and GPT-4 API, FractalFathom CLI automates the visualization of Java projects, showing complex relationships between classes, methods, fields, and components.
- Static Code Analysis: Parses Java source code using the Spoon framework to build intermediate representations (IR).
- Feature Enhancement: Leverages GraphCodeBERT to generate embedding vectors, enhancing code representation.
- Automated PlantUML Diagram Generation: Uses GPT-4 API to generate PlantUML component diagrams from enhanced IR data for visualizing project structures.
- Server Interaction: Communicates with a Python server to interact with GPT-4 and GraphCodeBERT models, handling embedding generation and diagram creation.
- Programming Language: Kotlin (JVM 21+)
- Build Tool: Gradle
- Main Libraries:
-
Clone the Project:
git clone https://github.com/your-repository/fractalfathom-cli.git cd fractalfathom-cli
-
Set Environment Variables: Create the OpenAI API key in environment variables.
OPENAI_API_KEY=your_openai_api_key_here
-
Install Dependencies: Use Gradle to download and configure all dependencies.
./gradlew build
-
Python Environment Setup: The project requires a Python server for GraphCodeBERT and GPT-4 interactions. Install Flask and other dependencies:
pip install -r requirements.txt
The custom annotation system in FractalFathom enables precise tagging of Java classes, methods, and fields to define functional roles and their relationships to higher-level concepts. This annotation system drives the identification of functional components and their mappings in the analysis process.
- Feature Annotations (
@FractalFathomFeature
): Used to define high-level functionalities of classes or interfaces, specifying their purpose and categorizing them into feature types (e.g.,FUNCTIONAL
orNON_FUNCTIONAL
). - Mapping Annotations (
@FractalFathomMapping
): Establish links between code elements and architectural concepts, aiding in the contextual understanding of software systems. - Annotation Rules:
- Higher-level elements (e.g., classes, interfaces) can use both
@FractalFathomFeature
and@FractalFathomMapping
. - Lower-level elements (e.g., methods, fields) are limited to
@FractalFathomMapping
.
- Higher-level elements (e.g., classes, interfaces) can use both
Below is an example demonstrating the usage of these annotations in a real-world Java application.
package org.protogalaxy.fractalfathom.cli.resources;
import org.protogalaxy.fractalfathom.FeatureType;
import org.protogalaxy.fractalfathom.MappingType;
import org.protogalaxy.fractalfathom.FractalFathomFeature;
import org.protogalaxy.fractalfathom.FractalFathomMapping;
@FractalFathomFeature(name = "UserService", description = "Handles user-related operations", type = FeatureType.FUNCTIONAL)
@FractalFathomMapping(toConcept = "User Management", type = MappingType.MODULE)
public class UserService {
@FractalFathomMapping(toConcept = "User Repository", type = MappingType.COMPONENT)
private UserRepository userRepository;
@FractalFathomMapping(toConcept = "Role Service Interaction", type = MappingType.COMPONENT)
private RoleService roleService;
public UserService(UserRepository userRepository, RoleService roleService) {
this.userRepository = userRepository;
this.roleService = roleService;
}
@FractalFathomMapping(toConcept = "User Creation", type = MappingType.COMPONENT)
public void createUser(String username, String email, String role) {
userRepository.save(new User(username, email));
roleService.assignRole(username, role);
}
@FractalFathomMapping(toConcept = "User Deletion", type = MappingType.COMPONENT)
public void deleteUser(String username) {
userRepository.delete(username);
roleService.revokeAllRoles(username);
}
}
- Class-Level Feature Annotation:
@FractalFathomFeature
onUserService
defines it as a high-level functionality handling "User Management." - Class-Level Mapping Annotation:
@FractalFathomMapping
maps theUserService
class to the architectural concept "User Management." - Field-Level Mapping Annotations: Fields
userRepository
androleService
are mapped to respective components in the system architecture. - Method-Level Mapping Annotations: Methods like
createUser
anddeleteUser
are mapped to specific functional components, providing detailed insight into their roles within the larger context.
These annotations help FractalFathom perform structured analysis, producing accurate and visually comprehensible diagrams for understanding complex software architectures.
-
Run the Flask server:
python3 src/main/kotlin/org/protogalaxy/fractalfathom/cli/modelInference/server.py
Once started, the server will run on
http://localhost:5000
to handle embedding generation and PlantUML code generation requests.
-
Run the FractalFathom CLI tool:
./gradlew run --args='path/to/java/project path/to/output/directory'
The tool will automatically call server endpoints to generate enhanced IR data and PlantUML component diagram code through GraphCodeBERT and GPT-4 APIs.
- src/main/kotlin: Kotlin source code, including CLI, analysis modules, and utility classes.
FractalFathomCLI
: Entry point for CLI.modelInference
: Includes utilities for deep learning interactions, such asGraphCodeBERTUtils
andLLMUtils
.analysis
: Static code analysis module based on Spoon.
- src/main/kotlin/org/protogalaxy/fractalfathom/cli/modelInference/server.py: Python server code.
server.py
: Contains APIs for embedding generation and PlantUML code generation.
- resources: Dependency files and configuration.
Run tests with:
./gradlew test
The GraphCodeBERTUtilsTest
and the LLMUtilsTest
class starts the Python server and tests methods in LLMUtils
and GraphCodeBERTUtils
, verifying that the generated embeddings and PlantUML code meets expectations.
- GPT-4 API Token Limit: To avoid exceeding token limits, IR data is chunked and processed in batches.
- Socket Timeout: Ensure the Python server is running and listening if experiencing connection timeouts.
- Flask Debug Mode Warning: Disable debug mode for production environments.
We welcome contributions! Feel free to submit Pull Requests or open Issues with suggestions. Help us improve FractalFathom CLI for better code comprehension and visualization.
The GNU General Public License v3.0. See LICENSE for details.
We hope FractalFathom CLI enhances your code comprehension and visualization experience!