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 resources and service classes #883

Merged
merged 1 commit into from
Aug 21, 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
64 changes: 64 additions & 0 deletions jcommon/ai/neo4j/src/main/java/run/mone/neo4j/BotCall.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package run.mone.neo4j;

import com.google.gson.JsonObject;
import lombok.Setter;
import okhttp3.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
* @author [email protected]
* @date 2024/8/21 13:50
*/
public class BotCall {

//调用bot的地址
@Setter
private static String url = "";


public static String call(String desc, String input) {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.build();

// 使用 Gson 的 JsonObject 构建请求体
JsonObject mainObject = new JsonObject();
mainObject.addProperty("userName", "");
mainObject.addProperty("botId", "");
mainObject.addProperty("input", "");

JsonObject paramsObject = new JsonObject();
paramsObject.addProperty("desc", desc);
paramsObject.addProperty("input", input);
mainObject.add("params", paramsObject);

// 将 JsonObject 转换为字符串
String jsonBody = mainObject.toString();

RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonBody);

Request request = new Request.Builder()
.url(url)
.post(body)
.addHeader("Accept", "application/json, text/plain, */*")
.addHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
.addHeader("Cache-Control", "no-cache")
.addHeader("Connection", "keep-alive")
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "")
.build();

try {
Response response = client.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}

}
84 changes: 70 additions & 14 deletions jcommon/ai/neo4j/src/main/java/run/mone/neo4j/MoneCodeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.nodeTypes.NodeWithName;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
Expand All @@ -21,9 +22,10 @@
import org.neo4j.driver.*;
import org.neo4j.driver.types.Node;

import java.io.File;
import java.io.IOException;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Data
Expand All @@ -34,13 +36,18 @@ public class MoneCodeParser {

private String NEO4J_USER = "neo4j";

private String NEO4J_PASSWORD = "";
private String password = "";

private String embeddingUrl = "";

public MoneCodeParser setPassword(String password) {
this.password = password;
return this;
}


public void queryEntityClasses() {
try (Driver driver = GraphDatabase.driver(NEO4J_URI, AuthTokens.basic(NEO4J_USER, NEO4J_PASSWORD));
try (Driver driver = GraphDatabase.driver(NEO4J_URI, AuthTokens.basic(NEO4J_USER, password));
Session session = driver.session()) {
// 查询 type 为 'entity' 的所有 Class 节点
String query = "MATCH (c:Class {type: 'entity'}) RETURN c";
Expand All @@ -61,10 +68,15 @@ public void queryEntityClasses() {
}
}

//获取session(class)
public Session getSession() {
return GraphDatabase.driver(NEO4J_URI, AuthTokens.basic(NEO4J_USER, password)).session();
}


//查询所有Comment的信息(使用neo4j),返回是个List(class)
public List<Map<String, Object>> getAllComments() {
try (Driver driver = GraphDatabase.driver(NEO4J_URI, AuthTokens.basic(NEO4J_USER, NEO4J_PASSWORD));
try (Driver driver = GraphDatabase.driver(NEO4J_URI, AuthTokens.basic(NEO4J_USER, password));
Session session = driver.session()) {
List<Map<String, Object>> comments = new ArrayList<>();
Result result = session.run("MATCH (comment:Comment) RETURN comment, id(comment) as commentId");
Expand Down Expand Up @@ -102,7 +114,7 @@ public List<Map<String, Object>> queryCommentsByTextVector(String text) {
// 替换为你的查询向量
double[] queryVector = getTextVectorFromHttp(text);

try (Driver driver = GraphDatabase.driver(NEO4J_URI, AuthTokens.basic(NEO4J_USER, NEO4J_PASSWORD));
try (Driver driver = GraphDatabase.driver(NEO4J_URI, AuthTokens.basic(NEO4J_USER, password));
Session session = driver.session()) {

// 执行查询
Expand Down Expand Up @@ -186,7 +198,7 @@ public void fillCommentTextVectors() {

//把Comment的修改,刷新回neo4j(class)
public void updateCommentsInNeo4j(List<Map<String, Object>> comments) {
try (Driver driver = GraphDatabase.driver(NEO4J_URI, AuthTokens.basic(NEO4J_USER, NEO4J_PASSWORD));
try (Driver driver = GraphDatabase.driver(NEO4J_URI, AuthTokens.basic(NEO4J_USER, password));
Session session = driver.session()) {
for (Map<String, Object> comment : comments) {
Long commentId = (Long) comment.get("commentId");
Expand Down Expand Up @@ -271,7 +283,7 @@ public static List<String> getJavaFilesInDirectory(String directoryPath) {

//删除所有节点(class)
public void deleteAllNodes() {
try (Driver driver = GraphDatabase.driver(NEO4J_URI, AuthTokens.basic(NEO4J_USER, NEO4J_PASSWORD));
try (Driver driver = GraphDatabase.driver(NEO4J_URI, AuthTokens.basic(NEO4J_USER, password));
Session session = driver.session()) {
session.run("MATCH (n) DETACH DELETE n");
}
Expand All @@ -288,7 +300,7 @@ private void writeToNeo4j(String filePath) {
// 替换成你的 Java 文件路径
String projectName = "MyProject";

try (Driver driver = GraphDatabase.driver(NEO4J_URI, AuthTokens.basic(NEO4J_USER, NEO4J_PASSWORD));
try (Driver driver = GraphDatabase.driver(NEO4J_URI, AuthTokens.basic(NEO4J_USER, password));
Session session = driver.session()) {

// 解析 Java 文件
Expand All @@ -311,6 +323,31 @@ private static void createProjectNode(Session session, String projectName) {
session.run("MERGE (p:Project {name: $name})", projectParams);
}

/**
* 查找具有指定注解的类
*
* @param session 数据库会话
* @param annotationToFind 要查找的注解
* @return 具有指定注解的类的列表,每个类以Map形式表示
*/
public List<Map<String, Object>> findClassesWithAnnotation(Session session, String annotationToFind) {
Map<String, Object> params = new HashMap<>();
params.put("annotation", annotationToFind);
Result result = session.run(
"MATCH (c:Class) " +
"WHERE ANY(anno IN c.anno WHERE anno = $annotation) " +
"RETURN c",
params
);
List<Map<String,Object>> list = new ArrayList<>();
while (result.hasNext()) {
Record record = result.next();
System.out.println(record.get("c").asMap());
list.add(record.get("c").asMap());
}
return list;
}


private static void createFileNode(Session session, String projectName, String filePath) {
Map<String, Object> fileParams = new HashMap<>();
Expand Down Expand Up @@ -341,6 +378,7 @@ public Visitor(Session session, String projectName, String filePath) {
this.filePath = filePath;
}


@Override
public void visit(ClassOrInterfaceDeclaration n, Void arg) {
// 创建 Class/Interface 节点
Expand All @@ -360,13 +398,20 @@ public void visit(ClassOrInterfaceDeclaration n, Void arg) {

classParams.put("code", code);

System.out.println(classParams);
//获取ClassOrInterfaceDeclaration中的注解
List<String> annoList = n.getAnnotations().stream().peek(annotation -> {
System.out.println("Annotation: " + annotation.getNameAsString());
}).map(NodeWithName::getNameAsString).toList();

//注解
classParams.put("annotations", annoList);

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",
"ON CREATE SET c.full_name = $fullName, c.type = $type, c.code = $code, c.anno = $annotations " +
"ON MATCH SET c.full_name = $fullName, c.type = $type, c.code = $code, c.anno = $annotations",
classParams
);

Expand Down Expand Up @@ -394,7 +439,7 @@ public void visit(ClassOrInterfaceDeclaration n, Void arg) {
dependsOnParams.put("fieldName", fieldName);


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

session.run("MATCH (c:Class {name: $className}) " +
"MATCH (s:Class {name: $serviceName}) " +
Expand All @@ -405,7 +450,6 @@ public void visit(ClassOrInterfaceDeclaration n, Void arg) {
});



super.visit(n, arg);

}
Expand Down Expand Up @@ -489,5 +533,17 @@ private static String getControllerType(ClassOrInterfaceDeclaration n) {
return type;
}

//读取resource下某个文件的文本内容(class)
public String readResourceFileContent(String fileName) {
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(fileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
return reader.lines().collect(Collectors.joining(System.lineSeparator()));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}



}
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
package run.mone.neo4j.test;

import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import org.junit.Test;
import org.neo4j.driver.Record;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.neo4j.driver.internal.InternalRecord;
import org.neo4j.driver.internal.InternalResult;
import org.neo4j.driver.internal.InternalSession;
import run.mone.neo4j.BotCall;
import run.mone.neo4j.MoneCodeParser;
import run.mone.neo4j.test.MoneCodeParserTest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;


/**
* @author [email protected]
Expand All @@ -10,17 +35,68 @@
public class MoneCodeParserTest {



@Test
public void testWriteCatServiceToNeo4j() {
// 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");
new MoneCodeParser().setPassword(System.getenv("password")).writeJavaFilesToNeo4j("/Users/zhangzhiyong/IdeaProjects/goodjava/mone/jcommon/ai/neo4j/src/test/java/run/mone/neo4j/test/m");
}

@Test
public void test1() {
new MoneCodeParser().queryEntityClasses();
}




@Test
public void testFindClassesWithAnnotation() {
MoneCodeParser moneCodeParser = new MoneCodeParser().setPassword(System.getenv("password"));
List<Map<String, Object>> actual = moneCodeParser.findClassesWithAnnotation(moneCodeParser.getSession(), "Table");
System.out.println(actual);

// String xuqiu = "获取狗的主人";
String xuqiu = "管理宠物鸟";
String res = BotCall.call(moneCodeParser.readResourceFileContent("entity.md") + xuqiu, new Gson().toJson(actual));
System.out.println(res);
}



@Test
public void testFindClassesWithAnnotation2() {
MoneCodeParser moneCodeParser = new MoneCodeParser().setPassword(System.getenv("password"));
List<Map<String, Object>> actual = moneCodeParser.findClassesWithAnnotation(moneCodeParser.getSession(), "RestController");
System.out.println(actual);

String xuqiu = "获取狗的主人";
// String xuqiu = "管理宠物鸟";
String res = BotCall.call(moneCodeParser.readResourceFileContent("service.md") + xuqiu, new Gson().toJson(actual));
System.out.println(res);
}


@Test
public void testJson() {
Map<String, String> m = ImmutableMap.of("input", "a+b=?");
System.out.println(new Gson().toJson(m));
}


@Test
public void testReadResourceFileContent() {
String fileName = "entity.md";
// Assuming the test resource file is already placed in the resources directory
String actualContent = new MoneCodeParser().readResourceFileContent(fileName);

assertNotNull(actualContent);
}




}
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/21 16:06
*/
public @interface Service {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package run.mone.neo4j.test.m;

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

import java.util.HashMap;
import java.util.Map;
Expand All @@ -9,7 +10,7 @@
* @author [email protected]
* @date 2024/8/19 18:21
*/
@RestController
@Service
public class CatService {

private Map<String,String> data = new HashMap<>();
Expand Down
3 changes: 3 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
Expand Up @@ -8,4 +8,7 @@
*/
@Table
public class Dog {

private int id;

}
Loading
Loading