Skip to content

Commit ed90832

Browse files
IoannisPanagiotasvnickolov
authored andcommitted
Implement Spanning Tree mutate result transformer
1 parent 3a60dec commit ed90832

File tree

4 files changed

+251
-1
lines changed

4 files changed

+251
-1
lines changed

procedures/pushback-procedures-facade/src/main/java/org/neo4j/gds/procedures/algorithms/pathfinding/mutate/PushbackPathFindingMutateProcedureFacade.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public Stream<SpanningTreeMutateResult> spanningTree(String graphName, Map<Strin
263263
config.toParameters(),
264264
config.jobId(),
265265
config.logProgress(),
266-
(g, gs) -> (r) -> Stream.<SpanningTreeMutateResult>empty()
266+
new SpanningTreeMutateResultTransformerBuilder(mutateRelationshipService,config)
267267
).join();
268268
}
269269

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.procedures.algorithms.pathfinding.mutate;
21+
22+
import org.neo4j.gds.api.Graph;
23+
import org.neo4j.gds.api.GraphStore;
24+
import org.neo4j.gds.applications.algorithms.metadata.RelationshipsWritten;
25+
import org.neo4j.gds.core.utils.ProgressTimer;
26+
import org.neo4j.gds.pathfinding.SpanningTreeMutateStep;
27+
import org.neo4j.gds.procedures.algorithms.pathfinding.SpanningTreeMutateResult;
28+
import org.neo4j.gds.result.TimedAlgorithmResult;
29+
import org.neo4j.gds.results.ResultTransformer;
30+
import org.neo4j.gds.spanningtree.SpanningTree;
31+
32+
import java.util.Map;
33+
import java.util.concurrent.atomic.AtomicLong;
34+
import java.util.stream.Stream;
35+
36+
public class SpanningTreeMutateResultTransformer implements ResultTransformer<TimedAlgorithmResult<SpanningTree>, Stream<SpanningTreeMutateResult>> {
37+
38+
private final SpanningTreeMutateStep mutateStep;
39+
private final Graph graph;
40+
private final GraphStore graphStore;
41+
private final Map<String, Object> configuration;
42+
43+
SpanningTreeMutateResultTransformer(
44+
SpanningTreeMutateStep mutateStep,
45+
Graph graph,
46+
GraphStore graphStore,
47+
Map<String, Object> configuration
48+
) {
49+
this.mutateStep = mutateStep;
50+
this.graph = graph;
51+
this.graphStore = graphStore;
52+
this.configuration = configuration;
53+
}
54+
55+
@Override
56+
public Stream<SpanningTreeMutateResult> apply(TimedAlgorithmResult<SpanningTree> algorithmResult) {
57+
58+
RelationshipsWritten relationshipsWritten;
59+
var mutateMillis = new AtomicLong();
60+
try (var ignored = ProgressTimer.start(mutateMillis::set)) {
61+
relationshipsWritten = mutateStep.execute(graph, graphStore, algorithmResult.result());
62+
}
63+
var treeResult = algorithmResult.result();
64+
return Stream.of(
65+
new SpanningTreeMutateResult(
66+
0,
67+
algorithmResult.computeMillis(),
68+
mutateMillis.get(),
69+
treeResult.effectiveNodeCount(),
70+
relationshipsWritten.value(),
71+
treeResult.totalWeight(),
72+
configuration
73+
)
74+
);
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.procedures.algorithms.pathfinding.mutate;
21+
22+
import org.neo4j.gds.api.Graph;
23+
import org.neo4j.gds.api.GraphStore;
24+
import org.neo4j.gds.applications.algorithms.machinery.MutateRelationshipService;
25+
import org.neo4j.gds.pathfinding.SpanningTreeMutateStep;
26+
import org.neo4j.gds.procedures.algorithms.pathfinding.SpanningTreeMutateResult;
27+
import org.neo4j.gds.result.TimedAlgorithmResult;
28+
import org.neo4j.gds.results.ResultTransformerBuilder;
29+
import org.neo4j.gds.spanningtree.SpanningTree;
30+
import org.neo4j.gds.spanningtree.SpanningTreeMutateConfig;
31+
32+
import java.util.stream.Stream;
33+
34+
class SpanningTreeMutateResultTransformerBuilder implements ResultTransformerBuilder<TimedAlgorithmResult<SpanningTree>, Stream<SpanningTreeMutateResult>> {
35+
36+
private final MutateRelationshipService mutateRelationshipService;
37+
private final SpanningTreeMutateConfig configuration;
38+
39+
SpanningTreeMutateResultTransformerBuilder(
40+
MutateRelationshipService mutateRelationshipService,
41+
SpanningTreeMutateConfig configuration
42+
) {
43+
this.mutateRelationshipService = mutateRelationshipService;
44+
this.configuration = configuration;
45+
}
46+
47+
@Override
48+
public SpanningTreeMutateResultTransformer build(
49+
Graph graph,
50+
GraphStore graphStore
51+
) {
52+
var mutateStep = new SpanningTreeMutateStep(
53+
configuration.mutateRelationshipType(),
54+
configuration.mutateProperty(),
55+
mutateRelationshipService
56+
);
57+
return new SpanningTreeMutateResultTransformer(
58+
mutateStep,
59+
graph,
60+
graphStore,
61+
configuration.toMap()
62+
);
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.procedures.algorithms.pathfinding.mutate;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.neo4j.gds.api.Graph;
24+
import org.neo4j.gds.api.GraphStore;
25+
import org.neo4j.gds.applications.algorithms.machinery.MutateRelationshipService;
26+
import org.neo4j.gds.applications.algorithms.metadata.RelationshipsWritten;
27+
import org.neo4j.gds.logging.Log;
28+
import org.neo4j.gds.pathfinding.SpanningTreeMutateStep;
29+
import org.neo4j.gds.result.TimedAlgorithmResult;
30+
import org.neo4j.gds.spanningtree.SpanningTree;
31+
32+
import java.util.Map;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
import static org.mockito.ArgumentMatchers.any;
36+
import static org.mockito.Mockito.mock;
37+
import static org.mockito.Mockito.times;
38+
import static org.mockito.Mockito.verify;
39+
import static org.mockito.Mockito.verifyNoMoreInteractions;
40+
import static org.mockito.Mockito.when;
41+
42+
class SpanningTreeMutateResultTransformerTest {
43+
44+
@Test
45+
void shouldTransformToMutateResult() {
46+
var config = Map.<String, Object>of("foo", "bar");
47+
var graph = mock(Graph.class);
48+
var graphStore = mock(GraphStore.class);
49+
var mutateStep = mock(SpanningTreeMutateStep.class);
50+
51+
var algoResult = mock(SpanningTree.class);
52+
when(algoResult.effectiveNodeCount()).thenReturn(1L);
53+
when(algoResult.totalWeight()).thenReturn(3d);
54+
55+
56+
var relationshipsWritten = new RelationshipsWritten(5L);
57+
when(mutateStep.execute(any(), any(), any())).thenReturn(relationshipsWritten);
58+
59+
var timedResult = new TimedAlgorithmResult<>(algoResult, 123L);
60+
61+
var transformer = new SpanningTreeMutateResultTransformer(mutateStep, graph, graphStore, config);
62+
63+
var resultStream = transformer.apply(timedResult);
64+
var result = resultStream.findFirst().orElseThrow();
65+
66+
assertThat(result.preProcessingMillis()).isZero();
67+
assertThat(result.computeMillis()).isEqualTo(123L);
68+
assertThat(result.mutateMillis()).isNotNegative();
69+
assertThat(result.configuration()).isEqualTo(config);
70+
assertThat(result.effectiveNodeCount()).isEqualTo(1L);
71+
assertThat(result.totalWeight()).isEqualTo(3d);
72+
73+
assertThat(result.relationshipsWritten()).isEqualTo(5L);
74+
75+
verify(mutateStep, times(1)).execute(graph, graphStore, algoResult);
76+
verifyNoMoreInteractions(mutateStep);
77+
}
78+
79+
@Test
80+
void shouldTransformEmptyResultToMutateResult() {
81+
var config = Map.<String, Object>of("boo", "foo");
82+
var graph = mock(Graph.class);
83+
var graphStore = mock(GraphStore.class);
84+
var mutateRelationshipService = new MutateRelationshipService(Log.noOpLog());
85+
var mutateStep = new SpanningTreeMutateStep(
86+
"r",
87+
"foo",
88+
mutateRelationshipService
89+
);
90+
91+
var algoResult = SpanningTree.EMPTY;
92+
93+
var timedResult = new TimedAlgorithmResult<>(algoResult, 123L);
94+
95+
var transformer = new SpanningTreeMutateResultTransformer(mutateStep, graph, graphStore, config);
96+
97+
var resultStream = transformer.apply(timedResult);
98+
var result = resultStream.findFirst().orElseThrow();
99+
100+
assertThat(result.preProcessingMillis()).isZero();
101+
assertThat(result.computeMillis()).isEqualTo(123L);
102+
assertThat(result.mutateMillis()).isNotNegative();
103+
assertThat(result.configuration()).isEqualTo(config);
104+
assertThat(result.effectiveNodeCount()).isEqualTo(0L);
105+
assertThat(result.totalWeight()).isEqualTo(0d);
106+
assertThat(result.relationshipsWritten()).isEqualTo(0);
107+
108+
}
109+
110+
}

0 commit comments

Comments
 (0)