Skip to content

Commit f116442

Browse files
committed
style: normalize indentation and clean up empty interfaces
1 parent 0eb38df commit f116442

3,604 files changed

Lines changed: 206119 additions & 200496 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

geaflow-ai/src/main/java/org/apache/geaflow/ai/GeaFlowMemoryServer.java

Lines changed: 190 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.HashMap;
2424
import java.util.List;
2525
import java.util.Map;
26+
2627
import org.apache.geaflow.ai.common.util.SeDeUtil;
2728
import org.apache.geaflow.ai.graph.*;
2829
import org.apache.geaflow.ai.graph.io.*;
@@ -40,193 +41,199 @@
4041
@Controller
4142
public class GeaFlowMemoryServer {
4243

43-
private static final Logger LOGGER = LoggerFactory.getLogger(GeaFlowMemoryServer.class);
44-
45-
private static final String SERVER_NAME = "geaflow-memory-server";
46-
private static final int DEFAULT_PORT = 8080;
47-
48-
private static final ServerMemoryCache CACHE = new ServerMemoryCache();
49-
50-
public static void main(String[] args) {
51-
System.setProperty("solon.app.name", SERVER_NAME);
52-
Solon.start(GeaFlowMemoryServer.class, args, app -> {
53-
app.cfg().loadAdd("application.yml");
54-
int port = app.cfg().getInt("server.port", DEFAULT_PORT);
55-
LOGGER.info("Starting {} on port {}", SERVER_NAME, port);
56-
app.get("/", ctx -> {
44+
private static final Logger LOGGER = LoggerFactory.getLogger(GeaFlowMemoryServer.class);
45+
46+
private static final String SERVER_NAME = "geaflow-memory-server";
47+
private static final int DEFAULT_PORT = 8080;
48+
49+
private static final ServerMemoryCache CACHE = new ServerMemoryCache();
50+
51+
public static void main(String[] args) {
52+
System.setProperty("solon.app.name", SERVER_NAME);
53+
Solon.start(
54+
GeaFlowMemoryServer.class,
55+
args,
56+
app -> {
57+
app.cfg().loadAdd("application.yml");
58+
int port = app.cfg().getInt("server.port", DEFAULT_PORT);
59+
LOGGER.info("Starting {} on port {}", SERVER_NAME, port);
60+
app.get(
61+
"/",
62+
ctx -> {
5763
ctx.output("GeaFlow AI Server is running...");
58-
});
59-
app.get("/health", ctx -> {
64+
});
65+
app.get(
66+
"/health",
67+
ctx -> {
6068
ctx.output("{\"status\":\"UP\",\"service\":\"" + SERVER_NAME + "\"}");
61-
});
69+
});
6270
});
71+
}
72+
73+
@Get
74+
@Mapping("/api/test")
75+
public String test() {
76+
return "GeaFlow Memory Server is working!";
77+
}
78+
79+
@Post
80+
@Mapping("/graph/create")
81+
public String createGraph(@Body String input) {
82+
GraphSchema graphSchema = SeDeUtil.deserializeGraphSchema(input);
83+
String graphName = graphSchema.getName();
84+
if (graphName == null || CACHE.getGraphByName(graphName) != null) {
85+
throw new RuntimeException("Cannot create graph name: " + graphName);
6386
}
64-
65-
@Get
66-
@Mapping("/api/test")
67-
public String test() {
68-
return "GeaFlow Memory Server is working!";
69-
}
70-
71-
@Post
72-
@Mapping("/graph/create")
73-
public String createGraph(@Body String input) {
74-
GraphSchema graphSchema = SeDeUtil.deserializeGraphSchema(input);
75-
String graphName = graphSchema.getName();
76-
if (graphName == null || CACHE.getGraphByName(graphName) != null) {
77-
throw new RuntimeException("Cannot create graph name: " + graphName);
78-
}
79-
Map<String, EntityGroup> entities = new HashMap<>();
80-
for (VertexSchema vertexSchema : graphSchema.getVertexSchemaList()) {
81-
entities.put(vertexSchema.getName(), new VertexGroup(vertexSchema, new ArrayList<>()));
82-
}
83-
for (EdgeSchema edgeSchema : graphSchema.getEdgeSchemaList()) {
84-
entities.put(edgeSchema.getName(), new EdgeGroup(edgeSchema, new ArrayList<>()));
85-
}
86-
MemoryGraph graph = new MemoryGraph(graphSchema, entities);
87-
CACHE.putGraph(graph);
88-
LocalMemoryGraphAccessor graphAccessor = new LocalMemoryGraphAccessor(graph);
89-
LOGGER.info("Success to init empty graph.");
90-
91-
EntityAttributeIndexStore indexStore = new EntityAttributeIndexStore();
92-
indexStore.initStore(new SubgraphSemanticPromptFunction(graphAccessor));
93-
LOGGER.info("Success to init EntityAttributeIndexStore.");
94-
95-
GraphMemoryServer server = new GraphMemoryServer();
96-
server.addGraphAccessor(graphAccessor);
97-
server.addIndexStore(indexStore);
98-
LOGGER.info("Success to init GraphMemoryServer.");
99-
CACHE.putServer(server);
100-
101-
LOGGER.info("Success to init graph. SCHEMA: {}", graphSchema);
102-
return "createGraph has been called, graphName: " + graphName;
103-
}
104-
105-
@Post
106-
@Mapping("/graph/addEntitySchema")
107-
public String addSchema(@Param("graphName") String graphName,
108-
@Body String input) {
109-
Graph graph = CACHE.getGraphByName(graphName);
110-
if (graph == null) {
111-
throw new RuntimeException("Graph not exist.");
112-
}
113-
if (!(graph instanceof MemoryGraph)) {
114-
throw new RuntimeException("Graph cannot modify.");
115-
}
116-
MemoryMutableGraph memoryMutableGraph = new MemoryMutableGraph((MemoryGraph) graph);
117-
Schema schema = SeDeUtil.deserializeEntitySchema(input);
118-
String schemaName = schema.getName();
119-
if (schema instanceof VertexSchema) {
120-
memoryMutableGraph.addVertexSchema((VertexSchema) schema);
121-
} else if (schema instanceof EdgeSchema) {
122-
memoryMutableGraph.addEdgeSchema((EdgeSchema) schema);
123-
} else {
124-
throw new RuntimeException("Cannt add schema: " + input);
125-
}
126-
return "addSchema has been called, schemaName: " + schemaName;
127-
}
128-
129-
@Post
130-
@Mapping("/graph/getGraphSchema")
131-
public String getSchema(@Param("graphName") String graphName) {
132-
Graph graph = CACHE.getGraphByName(graphName);
133-
if (graph == null) {
134-
throw new RuntimeException("Graph not exist.");
135-
}
136-
if (!(graph instanceof MemoryGraph)) {
137-
throw new RuntimeException("Graph cannot modify.");
138-
}
139-
return SeDeUtil.serializeGraphSchema(graph.getGraphSchema());
140-
}
141-
142-
@Post
143-
@Mapping("/graph/insertEntity")
144-
public String addEntity(@Param("graphName") String graphName,
145-
@Body String input) {
146-
Graph graph = CACHE.getGraphByName(graphName);
147-
if (graph == null) {
148-
throw new RuntimeException("Graph not exist.");
149-
}
150-
if (!(graph instanceof MemoryGraph)) {
151-
throw new RuntimeException("Graph cannot modify.");
152-
}
153-
MemoryMutableGraph memoryMutableGraph = new MemoryMutableGraph((MemoryGraph) graph);
154-
List<GraphEntity> graphEntities = SeDeUtil.deserializeEntities(input);
155-
156-
for (GraphEntity entity : graphEntities) {
157-
if (entity instanceof GraphVertex) {
158-
memoryMutableGraph.addVertex(((GraphVertex) entity).getVertex());
159-
} else {
160-
memoryMutableGraph.addEdge(((GraphEdge) entity).getEdge());
161-
}
162-
}
163-
CACHE.getConsolidateServer().executeConsolidateTask(
87+
Map<String, EntityGroup> entities = new HashMap<>();
88+
for (VertexSchema vertexSchema : graphSchema.getVertexSchemaList()) {
89+
entities.put(vertexSchema.getName(), new VertexGroup(vertexSchema, new ArrayList<>()));
90+
}
91+
for (EdgeSchema edgeSchema : graphSchema.getEdgeSchemaList()) {
92+
entities.put(edgeSchema.getName(), new EdgeGroup(edgeSchema, new ArrayList<>()));
93+
}
94+
MemoryGraph graph = new MemoryGraph(graphSchema, entities);
95+
CACHE.putGraph(graph);
96+
LocalMemoryGraphAccessor graphAccessor = new LocalMemoryGraphAccessor(graph);
97+
LOGGER.info("Success to init empty graph.");
98+
99+
EntityAttributeIndexStore indexStore = new EntityAttributeIndexStore();
100+
indexStore.initStore(new SubgraphSemanticPromptFunction(graphAccessor));
101+
LOGGER.info("Success to init EntityAttributeIndexStore.");
102+
103+
GraphMemoryServer server = new GraphMemoryServer();
104+
server.addGraphAccessor(graphAccessor);
105+
server.addIndexStore(indexStore);
106+
LOGGER.info("Success to init GraphMemoryServer.");
107+
CACHE.putServer(server);
108+
109+
LOGGER.info("Success to init graph. SCHEMA: {}", graphSchema);
110+
return "createGraph has been called, graphName: " + graphName;
111+
}
112+
113+
@Post
114+
@Mapping("/graph/addEntitySchema")
115+
public String addSchema(@Param("graphName") String graphName, @Body String input) {
116+
Graph graph = CACHE.getGraphByName(graphName);
117+
if (graph == null) {
118+
throw new RuntimeException("Graph not exist.");
119+
}
120+
if (!(graph instanceof MemoryGraph)) {
121+
throw new RuntimeException("Graph cannot modify.");
122+
}
123+
MemoryMutableGraph memoryMutableGraph = new MemoryMutableGraph((MemoryGraph) graph);
124+
Schema schema = SeDeUtil.deserializeEntitySchema(input);
125+
String schemaName = schema.getName();
126+
if (schema instanceof VertexSchema) {
127+
memoryMutableGraph.addVertexSchema((VertexSchema) schema);
128+
} else if (schema instanceof EdgeSchema) {
129+
memoryMutableGraph.addEdgeSchema((EdgeSchema) schema);
130+
} else {
131+
throw new RuntimeException("Cannt add schema: " + input);
132+
}
133+
return "addSchema has been called, schemaName: " + schemaName;
134+
}
135+
136+
@Post
137+
@Mapping("/graph/getGraphSchema")
138+
public String getSchema(@Param("graphName") String graphName) {
139+
Graph graph = CACHE.getGraphByName(graphName);
140+
if (graph == null) {
141+
throw new RuntimeException("Graph not exist.");
142+
}
143+
if (!(graph instanceof MemoryGraph)) {
144+
throw new RuntimeException("Graph cannot modify.");
145+
}
146+
return SeDeUtil.serializeGraphSchema(graph.getGraphSchema());
147+
}
148+
149+
@Post
150+
@Mapping("/graph/insertEntity")
151+
public String addEntity(@Param("graphName") String graphName, @Body String input) {
152+
Graph graph = CACHE.getGraphByName(graphName);
153+
if (graph == null) {
154+
throw new RuntimeException("Graph not exist.");
155+
}
156+
if (!(graph instanceof MemoryGraph)) {
157+
throw new RuntimeException("Graph cannot modify.");
158+
}
159+
MemoryMutableGraph memoryMutableGraph = new MemoryMutableGraph((MemoryGraph) graph);
160+
List<GraphEntity> graphEntities = SeDeUtil.deserializeEntities(input);
161+
162+
for (GraphEntity entity : graphEntities) {
163+
if (entity instanceof GraphVertex) {
164+
memoryMutableGraph.addVertex(((GraphVertex) entity).getVertex());
165+
} else {
166+
memoryMutableGraph.addEdge(((GraphEdge) entity).getEdge());
167+
}
168+
}
169+
CACHE
170+
.getConsolidateServer()
171+
.executeConsolidateTask(
164172
CACHE.getServerByName(graphName).getGraphAccessors().get(0), memoryMutableGraph);
165-
return "Success to add entities, num: " + graphEntities.size();
166-
}
167-
168-
@Post
169-
@Mapping("/graph/delEntity")
170-
public String deleteEntity(@Param("graphName") String graphName,
171-
@Body String input) {
172-
Graph graph = CACHE.getGraphByName(graphName);
173-
if (graph == null) {
174-
throw new RuntimeException("Graph not exist.");
175-
}
176-
if (!(graph instanceof MemoryGraph)) {
177-
throw new RuntimeException("Graph cannot modify.");
178-
}
179-
MemoryMutableGraph memoryMutableGraph = new MemoryMutableGraph((MemoryGraph) graph);
180-
List<GraphEntity> graphEntities = SeDeUtil.deserializeEntities(input);
181-
for (GraphEntity entity : graphEntities) {
182-
if (entity instanceof GraphVertex) {
183-
memoryMutableGraph.removeVertex(entity.getLabel(),
184-
((GraphVertex) entity).getVertex().getId());
185-
} else {
186-
memoryMutableGraph.removeEdge(((GraphEdge) entity).getEdge());
187-
}
188-
}
189-
return "Success to remove entities, num: " + graphEntities.size();
190-
}
191-
192-
@Post
193-
@Mapping("/query/context")
194-
public String createContext(@Param("graphName") String graphName) {
195-
GraphMemoryServer server = CACHE.getServerByName(graphName);
196-
if (server == null) {
197-
throw new RuntimeException("Server not exist.");
198-
}
199-
String sessionId = server.createSession();
200-
CACHE.putSession(server, sessionId);
201-
return sessionId;
202-
}
203-
204-
@Post
205-
@Mapping("/query/exec")
206-
public String execQuery(@Param("sessionId") String sessionId,
207-
@Body String query) {
208-
String graphName = CACHE.getGraphNameBySession(sessionId);
209-
if (graphName == null) {
210-
throw new RuntimeException("Graph not exist.");
211-
}
212-
GraphMemoryServer server = CACHE.getServerByName(graphName);
213-
VectorSearch search = new VectorSearch(null, sessionId);
214-
search.addVector(new KeywordVector(query));
215-
server.search(search);
216-
Context context = server.verbalize(sessionId,
217-
new SubgraphSemanticPromptFunction(server.getGraphAccessors().get(0)));
218-
return context.toString();
219-
}
220-
221-
@Post
222-
@Mapping("/query/result")
223-
public String getResult(@Param("sessionId") String sessionId) {
224-
String graphName = CACHE.getGraphNameBySession(sessionId);
225-
if (graphName == null) {
226-
throw new RuntimeException("Graph not exist.");
227-
}
228-
GraphMemoryServer server = CACHE.getServerByName(graphName);
229-
List<GraphEntity> result = server.getSessionEntities(sessionId);
230-
return result.toString();
173+
return "Success to add entities, num: " + graphEntities.size();
174+
}
175+
176+
@Post
177+
@Mapping("/graph/delEntity")
178+
public String deleteEntity(@Param("graphName") String graphName, @Body String input) {
179+
Graph graph = CACHE.getGraphByName(graphName);
180+
if (graph == null) {
181+
throw new RuntimeException("Graph not exist.");
182+
}
183+
if (!(graph instanceof MemoryGraph)) {
184+
throw new RuntimeException("Graph cannot modify.");
185+
}
186+
MemoryMutableGraph memoryMutableGraph = new MemoryMutableGraph((MemoryGraph) graph);
187+
List<GraphEntity> graphEntities = SeDeUtil.deserializeEntities(input);
188+
for (GraphEntity entity : graphEntities) {
189+
if (entity instanceof GraphVertex) {
190+
memoryMutableGraph.removeVertex(
191+
entity.getLabel(), ((GraphVertex) entity).getVertex().getId());
192+
} else {
193+
memoryMutableGraph.removeEdge(((GraphEdge) entity).getEdge());
194+
}
195+
}
196+
return "Success to remove entities, num: " + graphEntities.size();
197+
}
198+
199+
@Post
200+
@Mapping("/query/context")
201+
public String createContext(@Param("graphName") String graphName) {
202+
GraphMemoryServer server = CACHE.getServerByName(graphName);
203+
if (server == null) {
204+
throw new RuntimeException("Server not exist.");
205+
}
206+
String sessionId = server.createSession();
207+
CACHE.putSession(server, sessionId);
208+
return sessionId;
209+
}
210+
211+
@Post
212+
@Mapping("/query/exec")
213+
public String execQuery(@Param("sessionId") String sessionId, @Body String query) {
214+
String graphName = CACHE.getGraphNameBySession(sessionId);
215+
if (graphName == null) {
216+
throw new RuntimeException("Graph not exist.");
217+
}
218+
GraphMemoryServer server = CACHE.getServerByName(graphName);
219+
VectorSearch search = new VectorSearch(null, sessionId);
220+
search.addVector(new KeywordVector(query));
221+
server.search(search);
222+
Context context =
223+
server.verbalize(
224+
sessionId, new SubgraphSemanticPromptFunction(server.getGraphAccessors().get(0)));
225+
return context.toString();
226+
}
227+
228+
@Post
229+
@Mapping("/query/result")
230+
public String getResult(@Param("sessionId") String sessionId) {
231+
String graphName = CACHE.getGraphNameBySession(sessionId);
232+
if (graphName == null) {
233+
throw new RuntimeException("Graph not exist.");
231234
}
235+
GraphMemoryServer server = CACHE.getServerByName(graphName);
236+
List<GraphEntity> result = server.getSessionEntities(sessionId);
237+
return result.toString();
238+
}
232239
}

0 commit comments

Comments
 (0)