From fa5254ed6543a3f7488699b894e5b204994471c8 Mon Sep 17 00:00:00 2001 From: zhangzhiyong Date: Tue, 20 Aug 2024 14:29:18 +0800 Subject: [PATCH] chore: update test files and MoneCodeParser.java with no specific changes --- .../java/run/mone/neo4j/MoneCodeParser.java | 112 +++++++++++++++++- .../mone/neo4j/test/MoneCodeParserTest.java | 8 +- .../run/mone/neo4j/test/anno/Resource.java | 8 ++ .../mone/neo4j/test/anno/RestController.java | 8 ++ .../java/run/mone/neo4j/test/anno/Table.java | 8 ++ .../test/java/run/mone/neo4j/test/m/Cat.java | 21 ++++ .../run/mone/neo4j/test/m/CatService.java | 25 ++++ .../test/java/run/mone/neo4j/test/m/Dog.java | 11 ++ .../java/run/mone/neo4j/test/m/Person.java | 11 ++ .../test/{A.java => m/PersonService.java} | 12 +- 10 files changed, 216 insertions(+), 8 deletions(-) create mode 100644 jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/anno/Resource.java create mode 100644 jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/anno/RestController.java create mode 100644 jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/anno/Table.java create mode 100644 jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/Cat.java create mode 100644 jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/CatService.java create mode 100644 jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/Dog.java create mode 100644 jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/Person.java rename jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/{A.java => m/PersonService.java} (72%) diff --git a/jcommon/ai/neo4j/src/main/java/run/mone/neo4j/MoneCodeParser.java b/jcommon/ai/neo4j/src/main/java/run/mone/neo4j/MoneCodeParser.java index 328dc825b..db7acf099 100644 --- a/jcommon/ai/neo4j/src/main/java/run/mone/neo4j/MoneCodeParser.java +++ b/jcommon/ai/neo4j/src/main/java/run/mone/neo4j/MoneCodeParser.java @@ -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; @@ -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; @@ -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> getAllComments() { try (Driver driver = GraphDatabase.driver(NEO4J_URI, AuthTokens.basic(NEO4J_USER, NEO4J_PASSWORD)); @@ -52,13 +79,25 @@ public List> 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> queryCommentsByTextVector(String text) { // 替换为你的查询向量 double[] queryVector = getTextVectorFromHttp(text); @@ -118,7 +157,7 @@ public double[] convertListToFloatArray(List 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; @@ -166,7 +205,7 @@ public void updateCommentsInNeo4j(List> 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); @@ -243,7 +282,7 @@ public void deleteAllNodes() { * * @param filePath Java文件的路径 */ - @SneakyThrows + @SneakyThrows private void writeToNeo4j(String filePath) { //写入到neo4j中 // 替换成你的 Java 文件路径 @@ -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 fileParams = new HashMap<>(); fileParams.put("name", filePath); @@ -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 containsParams = new HashMap<>(); @@ -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 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); } @@ -387,5 +474,20 @@ private void createCommentNode(Comment comment, MethodDeclaration n) { } + private static String getControllerType(ClassOrInterfaceDeclaration n) { + String type = ""; + Optional optional = n.getAnnotationByName("RestController"); + if (optional.isPresent()) { + type = "controller"; + } + + optional = n.getAnnotationByName("Table"); + if (optional.isPresent()) { + type = "entity"; + } + + return type; + } + } diff --git a/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/MoneCodeParserTest.java b/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/MoneCodeParserTest.java index 0c83cb788..057a02e35 100644 --- a/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/MoneCodeParserTest.java +++ b/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/MoneCodeParserTest.java @@ -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(); } } diff --git a/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/anno/Resource.java b/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/anno/Resource.java new file mode 100644 index 000000000..2366ed8d8 --- /dev/null +++ b/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/anno/Resource.java @@ -0,0 +1,8 @@ +package run.mone.neo4j.test.anno; + +/** + * @author goodjava@qq.com + * @date 2024/8/19 18:20 + */ +public @interface Resource { +} diff --git a/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/anno/RestController.java b/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/anno/RestController.java new file mode 100644 index 000000000..770f21261 --- /dev/null +++ b/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/anno/RestController.java @@ -0,0 +1,8 @@ +package run.mone.neo4j.test.anno; + +/** + * @author goodjava@qq.com + * @date 2024/8/19 16:51 + */ +public @interface RestController { +} diff --git a/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/anno/Table.java b/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/anno/Table.java new file mode 100644 index 000000000..e009de88d --- /dev/null +++ b/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/anno/Table.java @@ -0,0 +1,8 @@ +package run.mone.neo4j.test.anno; + +/** + * @author goodjava@qq.com + * @date 2024/8/19 16:50 + */ +public @interface Table { +} diff --git a/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/Cat.java b/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/Cat.java new file mode 100644 index 000000000..5e3c87714 --- /dev/null +++ b/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/Cat.java @@ -0,0 +1,21 @@ +package run.mone.neo4j.test.m; + +import run.mone.neo4j.test.anno.Table; + +/** + * @author goodjava@qq.com + * @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; + } +} diff --git a/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/CatService.java b/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/CatService.java new file mode 100644 index 000000000..92ed980cb --- /dev/null +++ b/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/CatService.java @@ -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 goodjava@qq.com + * @date 2024/8/19 18:21 + */ +@RestController +public class CatService { + + private Map data = new HashMap<>(); + + + //获取小猫的数量 + //获取小猫的数量 + public int getCatCount() { + return data.size(); + } + + +} diff --git a/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/Dog.java b/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/Dog.java new file mode 100644 index 000000000..2811cc06d --- /dev/null +++ b/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/Dog.java @@ -0,0 +1,11 @@ +package run.mone.neo4j.test.m; + +import run.mone.neo4j.test.anno.Table; + +/** + * @author goodjava@qq.com + * @date 2024/8/19 17:03 + */ +@Table +public class Dog { +} diff --git a/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/Person.java b/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/Person.java new file mode 100644 index 000000000..1c67046be --- /dev/null +++ b/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/Person.java @@ -0,0 +1,11 @@ +package run.mone.neo4j.test.m; + +import run.mone.neo4j.test.anno.Table; + +/** + * @author goodjava@qq.com + * @date 2024/8/19 16:51 + */ +@Table +public class Person { +} diff --git a/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/A.java b/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/PersonService.java similarity index 72% rename from jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/A.java rename to jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/PersonService.java index 8281e28ce..65794057e 100644 --- a/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/A.java +++ b/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m/PersonService.java @@ -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 goodjava@qq.com * @date 2024/8/16 10:16 * AAA */ -public class A { +@RestController +public class PersonService { + + + @Resource + private CatService catService; /**