Skip to content
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

chore: update test files and MoneCodeParser.java with no specific cha… #882

Merged
merged 1 commit into from
Aug 20, 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
112 changes: 107 additions & 5 deletions jcommon/ai/neo4j/src/main/java/run/mone/neo4j/MoneCodeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
Expand All @@ -16,6 +19,7 @@
import okhttp3.*;
import org.neo4j.driver.Record;
import org.neo4j.driver.*;
import org.neo4j.driver.types.Node;

import java.io.File;
import java.io.IOException;
Expand All @@ -35,6 +39,29 @@ public class MoneCodeParser {
private String embeddingUrl = "";


public void queryEntityClasses() {
try (Driver driver = GraphDatabase.driver(NEO4J_URI, AuthTokens.basic(NEO4J_USER, NEO4J_PASSWORD));
Session session = driver.session()) {
// 查询 type 为 'entity' 的所有 Class 节点
String query = "MATCH (c:Class {type: 'entity'}) RETURN c";
Result result = session.run(query);
while (result.hasNext()) {
Record record = result.next();
Node classNode = record.get("c").asNode();
String name = classNode.get("name").asString();
String fullName = classNode.get("full_name").asString();
String type = classNode.get("type").asString();

// 输出或处理查询结果
System.out.println("Class Name: " + name);
System.out.println("Full Name: " + fullName);
System.out.println("Type: " + type);
System.out.println(classNode.get("code").asString());
}
}
}


//查询所有Comment的信息(使用neo4j),返回是个List(class)
public List<Map<String, Object>> getAllComments() {
try (Driver driver = GraphDatabase.driver(NEO4J_URI, AuthTokens.basic(NEO4J_USER, NEO4J_PASSWORD));
Expand All @@ -52,13 +79,25 @@ public List<Map<String, Object>> getAllComments() {
}


//给你ClassOrInterfaceDeclaration,帮我过滤掉所有method中的body,返回这个class的String内容(class)
public static String filterMethodBodies(ClassOrInterfaceDeclaration classOrInterfaceDeclaration) {
classOrInterfaceDeclaration.getMethods().forEach(method -> method.setBody(null));
return classOrInterfaceDeclaration.toString();
}

//给你ClassOrInterfaceDeclaration,帮我删除掉所有method,返回这个class的String内容(class)
public static String removeAllMethods(ClassOrInterfaceDeclaration classOrInterfaceDeclaration) {
classOrInterfaceDeclaration.getMethods().forEach(MethodDeclaration::remove);
return classOrInterfaceDeclaration.toString();
}

/**
* 根据文本向量查询评论
*
* @param text 输入的文本,用于生成查询向量
* @return 查询结果的列表,包含评论节点和相似度分数
*/
@SneakyThrows
@SneakyThrows
public List<Map<String, Object>> queryCommentsByTextVector(String text) {
// 替换为你的查询向量
double[] queryVector = getTextVectorFromHttp(text);
Expand Down Expand Up @@ -118,7 +157,7 @@ public double[] convertListToFloatArray(List<Double> floatList) {
* @param vectorB 第二个向量
* @return 两个向量的余弦相似度
*/
private double calculateCosineSimilarity(double[] vectorA, double[] vectorB) {
private double calculateCosineSimilarity(double[] vectorA, double[] vectorB) {
double dotProduct = 0.0;
double normA = 0.0;
double normB = 0.0;
Expand Down Expand Up @@ -166,7 +205,7 @@ public void updateCommentsInNeo4j(List<Map<String, Object>> comments) {
private static Gson gson = new Gson();


private double[] getTextVectorFromHttp(String text) throws IOException {
private double[] getTextVectorFromHttp(String text) throws IOException {
JsonObject jsonRequest = new JsonObject();
jsonRequest.addProperty("text", text);

Expand Down Expand Up @@ -243,7 +282,7 @@ public void deleteAllNodes() {
*
* @param filePath Java文件的路径
*/
@SneakyThrows
@SneakyThrows
private void writeToNeo4j(String filePath) {
//写入到neo4j中
// 替换成你的 Java 文件路径
Expand Down Expand Up @@ -272,6 +311,7 @@ private static void createProjectNode(Session session, String projectName) {
session.run("MERGE (p:Project {name: $name})", projectParams);
}


private static void createFileNode(Session session, String projectName, String filePath) {
Map<String, Object> fileParams = new HashMap<>();
fileParams.put("name", filePath);
Expand Down Expand Up @@ -308,7 +348,27 @@ public void visit(ClassOrInterfaceDeclaration n, Void arg) {
classParams.put("name", n.getNameAsString());
classParams.put("fullName", n.getFullyQualifiedName().orElse(""));

session.run("MERGE (c:Class {name: $name, full_name: $fullName})", classParams);
//class 的类型
String type = getControllerType(n);
classParams.put("type", type);

String code = "";

if (type.equals("entity")) {
code = removeAllMethods(n);
}

classParams.put("code", code);

System.out.println(classParams);


session.run(
"MERGE (c:Class {name: $name}) " +
"ON CREATE SET c.full_name = $fullName, c.type = $type, c.code = $code " +
"ON MATCH SET c.full_name = $fullName, c.type = $type, c.code = $code",
classParams
);

// 创建 CONTAINS 关系 (File -[:CONTAINS]-> Class)
Map<String, Object> containsParams = new HashMap<>();
Expand All @@ -319,6 +379,33 @@ public void visit(ClassOrInterfaceDeclaration n, Void arg) {
"MERGE (f)-[:CONTAINS]->(c)",
containsParams);


// 处理字段声明,查找 @Resource 注解
n.findAll(FieldDeclaration.class).forEach(field -> {
field.getAnnotations().forEach(annotation -> {
if (annotation.getNameAsString().equals("Resource")) {
String fieldName = field.getVariables().get(0).getNameAsString();
String fieldType = field.getElementType().asString();

// 创建 DEPENDS_ON 关系 (Class -[:DEPENDS_ON]-> Service)
Map<String, Object> dependsOnParams = new HashMap<>();
dependsOnParams.put("className", n.getNameAsString());
dependsOnParams.put("serviceName", fieldType);
dependsOnParams.put("fieldName", fieldName);


session.run("MERGE (c:Class {name: $name})", ImmutableMap.of("name",fieldType));

session.run("MATCH (c:Class {name: $className}) " +
"MATCH (s:Class {name: $serviceName}) " +
"MERGE (c)-[:DEPENDS_ON {field: $fieldName}]->(s)",
dependsOnParams);
}
});
});



super.visit(n, arg);

}
Expand Down Expand Up @@ -387,5 +474,20 @@ private void createCommentNode(Comment comment, MethodDeclaration n) {

}

private static String getControllerType(ClassOrInterfaceDeclaration n) {
String type = "";
Optional<AnnotationExpr> optional = n.getAnnotationByName("RestController");
if (optional.isPresent()) {
type = "controller";
}

optional = n.getAnnotationByName("Table");
if (optional.isPresent()) {
type = "entity";
}

return type;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ public class MoneCodeParserTest {

@Test
public void testWriteCatServiceToNeo4j() {
new MoneCodeParser().writeJavaFilesToNeo4j("/Users/zhangzhiyong/IdeaProjects/ai/m78/m78-service/src/main/java/run/mone/m78/service");
// new MoneCodeParser().writeJavaFilesToNeo4j("/Users/zhangzhiyong/IdeaProjects/ai/m78/m78-service/src/main/java/run/mone/m78/service");
// MoneCodeParser.writeJavaFilesToNeo4j("/Users/zhangzhiyong/IdeaProjects/ai/m78/m78-service/src/main/java/run/mone/m78/service/database");
// MoneCodeParser.writeJavaFilesToNeo4j("/Users/zhangzhiyong/IdeaProjects/ai/m78/m78-service/src/main/java/run/mone/m78/service/database/SqlParseUtil.java");
// new MoneCodeParser().writeJavaFilesToNeo4j("/Users/zhangzhiyong/IdeaProjects/goodjava/mone/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/A.java");
new MoneCodeParser().writeJavaFilesToNeo4j("/Users/zhangzhiyong/IdeaProjects/goodjava/mone/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m");
}

@Test
public void test1() {
new MoneCodeParser().queryEntityClasses();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package run.mone.neo4j.test.anno;

/**
* @author [email protected]
* @date 2024/8/19 18:20
*/
public @interface Resource {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package run.mone.neo4j.test.anno;

/**
* @author [email protected]
* @date 2024/8/19 16:51
*/
public @interface RestController {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package run.mone.neo4j.test.anno;

/**
* @author [email protected]
* @date 2024/8/19 16:50
*/
public @interface Table {
}
21 changes: 21 additions & 0 deletions jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package run.mone.neo4j.test.m;

import run.mone.neo4j.test.anno.Table;

/**
* @author [email protected]
* @date 2024/8/19 17:03
*/
@Table
public class Cat {

private int id;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package run.mone.neo4j.test.m;

import run.mone.neo4j.test.anno.RestController;

import java.util.HashMap;
import java.util.Map;

/**
* @author [email protected]
* @date 2024/8/19 18:21
*/
@RestController
public class CatService {

private Map<String,String> data = new HashMap<>();


//获取小猫的数量
//获取小猫的数量
public int getCatCount() {
return data.size();
}


}
11 changes: 11 additions & 0 deletions jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package run.mone.neo4j.test.m;

import run.mone.neo4j.test.anno.Table;

/**
* @author [email protected]
* @date 2024/8/19 17:03
*/
@Table
public class Dog {
}
11 changes: 11 additions & 0 deletions jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package run.mone.neo4j.test.m;

import run.mone.neo4j.test.anno.Table;

/**
* @author [email protected]
* @date 2024/8/19 16:51
*/
@Table
public class Person {
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package run.mone.neo4j.test;
package run.mone.neo4j.test.m;

import run.mone.neo4j.test.anno.Resource;
import run.mone.neo4j.test.anno.RestController;

/**
* @author [email protected]
* @date 2024/8/16 10:16
* AAA
*/
public class A {
@RestController
public class PersonService {


@Resource
private CatService catService;


/**
Expand Down
Loading