A Toolkit for Generating Code Knowledge Graphs

Ibrahim Abdelaziz, Julian Dolby, Jamie P. McCusker, Kavitha Srinivas

TL;DR

This paper introduces GRAPHGEN4CODE, a toolkit that builds comprehensive knowledge graphs of software code by integrating static code analysis (data and control flow) with natural language artifacts like documentation and forum discussions. This allows for richer code representations that go beyond traditional ASTs, enabling new applications in areas like code search and understanding.

What Problem Does This Paper Address?

Understanding and working with software code is becoming increasingly complex. Existing methods for representing code, such as Abstract Syntax Trees (ASTs) or simple text representations, often fail to capture the full picture. They struggle with "non-local" relationships within code and rarely incorporate the wealth of natural language information (like API documentation or Stack Overflow discussions) that developers use to understand code. This gap makes tasks like sophisticated code search, bug detection, and automated refactoring challenging. The authors aim to bridge this gap by creating a unified knowledge graph that represents code's structure, flow, and its associated natural language context.

Methods

The core of GRAPHGEN4CODE is its ability to build code knowledge graphs by:

  • Inter-procedural Static Analysis: It leverages a modified WALA framework to perform data and control flow analysis across entire programs, not just within individual functions. This is crucial for understanding how code components interact.
  • Scalable Abstraction for APIs: To handle millions of API calls efficiently, especially in dynamic languages like Python, the toolkit employs simplifying assumptions. For instance, it assumes imported functions return new objects and that reading an object's field returns the object itself. While this introduces some imprecision, it's a necessary trade-off for scalability.
  • Linking Code to Natural Language:
    • Documentation Extraction: It uses Python's introspection capabilities to extract documentation strings, parameter names, and return types from libraries.
  • Forum Discussion Integration: It processes millions of posts from Stack Overflow and StackExchange, using information retrieval techniques to link code elements (functions, classes) to relevant discussions and answers.
  • Graph Representation: The output is stored in RDF or JSON format, using existing ontologies like Schema.org and SIO where possible, and introducing new properties as needed. Each program is modeled as a separate graph, with nodes representing code elements (calls, data reads) and edges representing relationships like "flowsTo" and "immediatelyPrecedes."

Datasets

  • Code: 1.38 million Python files from GitHub, 2,300 Python modules.
  • Documentation: Extracted from 506 popular Python libraries.
  • Forum Discussions: 45 million posts from Stack Overflow and 2.7 million posts from StackExchange.

The paper does not introduce new benchmarks but rather demonstrates the scalability of their toolkit on existing, large-scale code and discussion datasets.

Key Insights and Ideas

  • Bridging the Code-Text Divide: The most significant insight is the successful integration of static code analysis with natural language artifacts. This creates a richer, more context-aware representation of code than either approach alone.
  • Scalability through Abstraction: The paper highlights the critical need for smart abstractions (even if imperfect) to make static analysis of massive codebases feasible. The chosen abstractions are key to handling the scale of millions of programs.
  • "Flows To" and "Immediately Precedes" as Core Relations: These two edge types are fundamental to capturing both data movement and execution order, providing a more nuanced view than just call graphs.
  • Dynamic Languages are a Challenge (and Opportunity): The focus on Python, a dynamic language, underscores the difficulty and importance of robust code analysis when type information is not explicit. The techniques developed are intended to be extensible to other dynamic languages.

Key Results

  • Massive Knowledge Graph: The toolkit successfully generated a code knowledge graph with over 2 billion triples, integrating data from 1.3 million Python programs and 47 million forum posts.
  • Code Analysis Coverage: Static analysis graphs covered an average of 86% of function calls in ASTs of 441 Python programs, demonstrating good coverage despite the inherent limitations of static analysis.
  • Forum Link Accuracy: Human evaluation of links between code elements and forum posts showed a 79% linking accuracy, with most irrelevant links occurring due to cross-language similarities or generic class names.

Limitations and Future Work

  • Language Specificity: The current toolkit's documentation extraction is language-specific (Python). Extending it to other languages requires custom code.
  • Abstraction Imprecision: The simplifying assumptions made for scalability introduce imprecision into the analysis.
  • Future Work: The authors suggest extending the toolkit to other languages, further enriching the graph with more sophisticated semantic models, and exploring applications like code recommendation and bug detection. They also note that their approach can benefit from combining with other techniques, such as those for learning ontologies from text.

Summary updated

BibTeX
@inproceedings{abdelaziz2021toolkit,
  author = {Ibrahim Abdelaziz and Julian Dolby and Jamie P. McCusker and Kavitha Srinivas},
  title = {{A Toolkit for Generating Code Knowledge Graphs}},
  booktitle = {K-CAP},
  year = {2021},
  doi = {10.1145/3460210.3493578},
}
Figure