Skip to content

Commit cf728bf

Browse files
sapienza88Selim Soufargiyangxk1
authored
feat(java,info): add remove vertex edge method (#773)
* add remove vertex edge, TODO: tests * reformat only GraphInfo class, TODO: tests * added basic tests for add and remove vertex * format graphInfoTests * format graphInfoTests * refactoring 1 * added basic test for edges * add test * update filter * update * chores --------- Co-authored-by: Selim Soufargi <ssoufargi.idealab.unical@gmail.com~> Co-authored-by: yxk485490 <1320342065@qq.com>
1 parent 14d034f commit cf728bf

2 files changed

Lines changed: 115 additions & 0 deletions

File tree

maven-projects/info/src/main/java/org/apache/graphar/info/GraphInfo.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,34 @@ public Optional<GraphInfo> addVertexAsNew(VertexInfo vertexInfo) {
179179
edgeConcat2EdgeInfo));
180180
}
181181

182+
public Optional<GraphInfo> removeVertex(VertexInfo vertexInfo) {
183+
184+
if (vertexInfo == null || !hasVertexInfo(vertexInfo.getType())) {
185+
return Optional.empty();
186+
}
187+
188+
final List<VertexInfo> newVertexInfoList =
189+
vertexInfos.stream()
190+
.filter(v -> !v.getType().equals(vertexInfo.getType()))
191+
.collect(Collectors.toList());
192+
193+
final Map<String, VertexInfo> newVertexInfoMap =
194+
vertexType2VertexInfo.entrySet().stream()
195+
.filter(v -> !v.getKey().equals(vertexInfo.getType()))
196+
.collect(
197+
Collectors.toUnmodifiableMap(
198+
Map.Entry::getKey, Map.Entry::getValue));
199+
return Optional.of(
200+
new GraphInfo(
201+
name,
202+
newVertexInfoList,
203+
edgeInfos,
204+
baseUri,
205+
version,
206+
newVertexInfoMap,
207+
edgeConcat2EdgeInfo));
208+
}
209+
182210
public Optional<GraphInfo> addEdgeAsNew(EdgeInfo edgeInfo) {
183211
if (edgeInfo == null
184212
|| hasEdgeInfo(
@@ -205,6 +233,38 @@ public Optional<GraphInfo> addEdgeAsNew(EdgeInfo edgeInfo) {
205233
newEdgeConcat2EdgeInfo));
206234
}
207235

236+
public Optional<GraphInfo> removeEdge(EdgeInfo edgeInfo) {
237+
if (edgeInfo == null
238+
|| !hasEdgeInfo(
239+
edgeInfo.getSrcType(), edgeInfo.getEdgeType(), edgeInfo.getDstType())) {
240+
return Optional.empty();
241+
}
242+
final List<EdgeInfo> newEdgeInfos =
243+
edgeInfos.stream()
244+
.filter(
245+
e ->
246+
!(e.getSrcType().equals(edgeInfo.getSrcType())
247+
&& e.getDstType().equals(edgeInfo.getDstType())
248+
&& e.getEdgeType().equals(edgeInfo.getEdgeType())))
249+
.collect(Collectors.toList());
250+
251+
final Map<String, EdgeInfo> newEdgeConcat2EdgeInfo =
252+
edgeConcat2EdgeInfo.entrySet().stream()
253+
.filter(e -> !e.getKey().equals(edgeInfo.getConcat()))
254+
.collect(
255+
Collectors.toUnmodifiableMap(
256+
Map.Entry::getKey, Map.Entry::getValue));
257+
return Optional.of(
258+
new GraphInfo(
259+
name,
260+
vertexInfos,
261+
newEdgeInfos,
262+
baseUri,
263+
version,
264+
vertexType2VertexInfo,
265+
newEdgeConcat2EdgeInfo));
266+
}
267+
208268
public boolean hasVertexInfo(String type) {
209269
return vertexType2VertexInfo.containsKey(type);
210270
}

maven-projects/info/src/test/java/org/apache/graphar/info/GraphInfoTest.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.ArrayList;
2525
import java.util.Arrays;
2626
import java.util.List;
27+
import java.util.Optional;
2728
import org.apache.graphar.info.loader.GraphInfoLoader;
2829
import org.apache.graphar.info.loader.impl.LocalFileSystemStreamGraphInfoLoader;
2930
import org.apache.graphar.info.type.AdjListType;
@@ -110,6 +111,60 @@ public void testGraphInfoBasics() {
110111
illegalArgumentException.getMessage());
111112
// test version gar/v1
112113
Assert.assertEquals(1, graphInfo.getVersion().getVersion());
114+
// basic tests for addVertex and removeVertex
115+
VertexInfo testingVertexInfo =
116+
new VertexInfo(
117+
"test_vertex",
118+
100,
119+
Arrays.asList(TestUtil.pg1),
120+
"vertex/person/",
121+
"gar/v1");
122+
GraphInfo testingVertexGraphInfo =
123+
new GraphInfo("graphVertexTest", new ArrayList<>(), new ArrayList<>(), "", "");
124+
// remove non-existent vertex from an empty graph
125+
Assert.assertTrue(testingVertexGraphInfo.removeVertex(testingVertexInfo).isEmpty());
126+
// add the created vertex on an empty graph
127+
Optional<GraphInfo> addVertexAsNewGraph =
128+
testingVertexGraphInfo.addVertexAsNew(testingVertexInfo);
129+
Assert.assertTrue(addVertexAsNewGraph.isPresent());
130+
testingVertexGraphInfo = addVertexAsNewGraph.get();
131+
Assert.assertEquals(1, addVertexAsNewGraph.get().getVertexInfos().size());
132+
// try to add the same vertex again
133+
Assert.assertTrue(testingVertexGraphInfo.addVertexAsNew(testingVertexInfo).isEmpty());
134+
// test remove vertex
135+
addVertexAsNewGraph = testingVertexGraphInfo.removeVertex(testingVertexInfo);
136+
Assert.assertTrue(addVertexAsNewGraph.isPresent());
137+
Assert.assertEquals(0, addVertexAsNewGraph.get().getVertexInfos().size());
138+
139+
// same tests as vertices for edges
140+
GraphInfo testingEdgeGraphInfo =
141+
new GraphInfo("graphEdgeTest", new ArrayList<>(), new ArrayList<>(), "", "");
142+
EdgeInfo testingEdgeInfo =
143+
new EdgeInfo(
144+
"person",
145+
"knows",
146+
"person",
147+
1024,
148+
100,
149+
100,
150+
false,
151+
URI.create("edge/person_knows_person/"),
152+
"gar/v1",
153+
List.of(TestUtil.orderedBySource),
154+
List.of(TestUtil.pg3));
155+
// remove non-existent edge from an empty graph
156+
Assert.assertTrue(testingEdgeGraphInfo.removeEdge(testingEdgeInfo).isEmpty());
157+
// add the created edge on an empty graph
158+
Optional<GraphInfo> addEdgeAsNewGraph = testingEdgeGraphInfo.addEdgeAsNew(testingEdgeInfo);
159+
Assert.assertTrue(addEdgeAsNewGraph.isPresent());
160+
testingEdgeGraphInfo = addEdgeAsNewGraph.get();
161+
Assert.assertEquals(1, addEdgeAsNewGraph.get().getEdgeInfos().size());
162+
// try to add the same edge again
163+
Assert.assertTrue(testingEdgeGraphInfo.addEdgeAsNew(testingEdgeInfo).isEmpty());
164+
// test remove edge
165+
addEdgeAsNewGraph = testingEdgeGraphInfo.removeEdge(testingEdgeInfo);
166+
Assert.assertTrue(addEdgeAsNewGraph.isPresent());
167+
Assert.assertEquals(0, addEdgeAsNewGraph.get().getEdgeInfos().size());
113168
}
114169

115170
@Test

0 commit comments

Comments
 (0)