Skip to content

Version 0.2.0—support slim jsons from codeanalyzer #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cldk/analysis/java/treesitter/javasitter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ipdb import set_trace
from itertools import groupby
from typing import List, Set, Dict
from tree_sitter import Language, Node, Parser, Query
Expand Down
30 changes: 21 additions & 9 deletions cldk/models/java/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import re
import json
from ipdb import set_trace
from contextvars import ContextVar
import re
from typing import Dict, List, Optional
from .constants_namespace import ConstantsNamespace
from pydantic import BaseModel, field_validator, model_validator
from contextvars import ContextVar
from .constants_namespace import ConstantsNamespace

constants = ConstantsNamespace()
context_concrete_class = ContextVar("context_concrete_class") # context var to store class concreteness
_CALLABLES_LOOKUP_TABLE = dict()


class JField(BaseModel):
Expand Down Expand Up @@ -340,12 +340,13 @@ class JGraphEdges(BaseModel):
@field_validator("source", "target", mode="before")
@classmethod
def validate_source(cls, value) -> JMethodDetail:
callable_dict = json.loads(value)
j_callable = JCallable(**json.loads(callable_dict["callable"])) # parse the value which is a quoted string
class_name = callable_dict["class_interface_declarations"]
file_path, type_declaration, callable_declaration = value["file_path"], value["type_declaration"], value["callable_declaration"]
j_callable = _CALLABLES_LOOKUP_TABLE.get((file_path, type_declaration, callable_declaration), None)
if j_callable is None:
raise ValueError(f"Callable not found in lookup table: {file_path}, {type_declaration}, {callable_declaration}")
class_name = type_declaration
method_decl = j_callable.declaration
mc = JMethodDetail(method_declaration=method_decl, klass=class_name, method=j_callable)
return mc
return JMethodDetail(method_declaration=method_decl, klass=class_name, method=j_callable)

def __hash__(self):
return hash(tuple(self))
Expand All @@ -365,3 +366,14 @@ class JApplication(BaseModel):

symbol_table: Dict[str, JCompilationUnit]
system_dependency_graph: List[JGraphEdges] = None

@field_validator("symbol_table", mode="after")
@classmethod
def validate_source(cls, symbol_table):
from ipdb import set_trace

# Populate the lookup table for callables
for file_path, j_compulation_unit in symbol_table.items():
for type_declaration, jtype in j_compulation_unit.type_declarations.items():
for callable_declaration, j_callable in jtype.callable_declarations.items():
_CALLABLES_LOOKUP_TABLE[(file_path, type_declaration, callable_declaration)] = j_callable
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cldk"
version = "0.1.1"
version = "0.2.0"
description = "codellm-devkit: A python library for seamless integration with LLMs."
authors = ["Rahul Krishna <i.m.ralk@gmail.com>", "Rangeet Pan <rangeet.pan@ibm.com>", "Saurabh Sinhas <sinhas@us.ibm.com>",
"Raju Pavuluri <pavuluri@us.ibm.com>"]
Expand Down Expand Up @@ -64,5 +64,5 @@ line-length = 180

[tool.cldk.testing]
sample-application = "tests/resources/java/application/"
sample-application-analysis-json = "tests/resources/java/analysis_db"
sample-application-analysis-json = "tests/resources/java/analysis_json/slim"
codeanalyzer-jar-path = "tests/resources/java/codeanalyzer/build/libs/"
80 changes: 40 additions & 40 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,43 @@ def analysis_json_fixture():
return config["tool"]["cldk"]["testing"]["sample-application-analysis-json"]


@pytest.fixture(scope="session", autouse=True)
def test_fixture():
"""
Returns the path to the test data directory.

Yields:
Path : The path to the test data directory.
"""
# ----------------------------------[ SETUP ]----------------------------------
# Path to your pyproject.toml
pyproject_path = Path(__file__).parent.parent / "pyproject.toml"

# Load the configuration
config = toml.load(pyproject_path)

# Access the test data path
test_data_path = config["tool"]["cldk"]["testing"]["sample-application"]

if not Path(test_data_path).exists():
Path(test_data_path).mkdir(parents=True)
url = "https://github.com/OpenLiberty/sample.daytrader8/archive/refs/tags/v1.2.zip"
filename = Path(test_data_path).absolute() / "v1.2.zip"
urlretrieve(url, filename)

# Extract the zip file to the test data path
with zipfile.ZipFile(filename, "r") as zip_ref:
zip_ref.extractall(test_data_path)

# Remove the zip file
filename.unlink()
# --------------------------------------------------------------------------------
# Daytrader8 sample application path
yield Path(test_data_path) / "sample.daytrader8-1.2"

# -----------------------------------[ TEARDOWN ]----------------------------------
# Remove the daytrader8 sample application that was downloaded for testing
for directory in Path(test_data_path).iterdir():
if directory.exists() and directory.is_dir():
shutil.rmtree(directory)
# ---------------------------------------------------------------------------------
# @pytest.fixture(scope="session", autouse=True)
# def test_fixture():
# """
# Returns the path to the test data directory.

# Yields:
# Path : The path to the test data directory.
# """
# # ----------------------------------[ SETUP ]----------------------------------
# # Path to your pyproject.toml
# pyproject_path = Path(__file__).parent.parent / "pyproject.toml"

# # Load the configuration
# config = toml.load(pyproject_path)

# # Access the test data path
# test_data_path = config["tool"]["cldk"]["testing"]["sample-application"]

# if not Path(test_data_path).exists():
# Path(test_data_path).mkdir(parents=True)
# url = "https://github.com/OpenLiberty/sample.daytrader8/archive/refs/tags/v1.2.zip"
# filename = Path(test_data_path).absolute() / "v1.2.zip"
# urlretrieve(url, filename)

# # Extract the zip file to the test data path
# with zipfile.ZipFile(filename, "r") as zip_ref:
# zip_ref.extractall(test_data_path)

# # Remove the zip file
# filename.unlink()
# # --------------------------------------------------------------------------------
# # Daytrader8 sample application path
# yield Path(test_data_path) / "sample.daytrader8-1.2"

# # -----------------------------------[ TEARDOWN ]----------------------------------
# # Remove the daytrader8 sample application that was downloaded for testing
# for directory in Path(test_data_path).iterdir():
# if directory.exists() and directory.is_dir():
# shutil.rmtree(directory)
# # ---------------------------------------------------------------------------------
3 changes: 2 additions & 1 deletion tests/models/java/test_java_models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from ipdb import set_trace
from typing import List, Tuple
from cldk import CLDK


def test_get_class_call_graph(analysis_json_fixture):
# Initialize the CLDK object with the project directory, language, and analysis_backend.
cldk = CLDK(language="java")

analysis = cldk.analysis(
project_path=analysis_json_fixture, analysis_backend="codeanalyzer", analysis_json_path=analysis_json_fixture, eager=False, analysis_level="call-graph"
)
assert analysis.get_call_graph_json() is not None
1 change: 1 addition & 0 deletions tests/resources/java/analysis_json/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!*.json
20,017 changes: 20,017 additions & 0 deletions tests/resources/java/analysis_json/fat/analysis.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/resources/java/analysis_json/slim/analysis.json

Large diffs are not rendered by default.