Skip to content

Commit d712ee2

Browse files
Do for PCST
1 parent 161e129 commit d712ee2

File tree

3 files changed

+218
-2
lines changed

3 files changed

+218
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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.write;
21+
22+
import org.neo4j.gds.api.Graph;
23+
import org.neo4j.gds.api.GraphStore;
24+
import org.neo4j.gds.api.ResultStore;
25+
import org.neo4j.gds.applications.algorithms.metadata.RelationshipsWritten;
26+
import org.neo4j.gds.core.utils.ProgressTimer;
27+
import org.neo4j.gds.core.utils.progress.JobId;
28+
import org.neo4j.gds.pathfinding.PrizeCollectingSteinerTreeWriteStep;
29+
import org.neo4j.gds.procedures.algorithms.pathfinding.PrizeCollectingSteinerTreeWriteResult;
30+
import org.neo4j.gds.pricesteiner.PrizeSteinerTreeResult;
31+
import org.neo4j.gds.result.TimedAlgorithmResult;
32+
import org.neo4j.gds.results.ResultTransformer;
33+
34+
import java.util.Map;
35+
import java.util.concurrent.atomic.AtomicLong;
36+
import java.util.stream.Stream;
37+
38+
public class PCSTWriteResultTransformer implements ResultTransformer<TimedAlgorithmResult<PrizeSteinerTreeResult>, Stream<PrizeCollectingSteinerTreeWriteResult>> {
39+
40+
private final PrizeCollectingSteinerTreeWriteStep writeStep;
41+
private final Graph graph;
42+
private final GraphStore graphStore;
43+
@Deprecated(forRemoval = true)
44+
private final ResultStore resultStore;
45+
private final JobId jobId;
46+
private final Map<String, Object> configuration;
47+
48+
public PCSTWriteResultTransformer(
49+
PrizeCollectingSteinerTreeWriteStep writeStep,
50+
Graph graph,
51+
GraphStore graphStore,
52+
ResultStore resultStore,
53+
JobId jobId,
54+
Map<String, Object> configuration
55+
) {
56+
this.writeStep = writeStep;
57+
this.graph = graph;
58+
this.graphStore = graphStore;
59+
this.resultStore = resultStore;
60+
this.jobId = jobId;
61+
this.configuration = configuration;
62+
}
63+
64+
@Override
65+
public Stream<PrizeCollectingSteinerTreeWriteResult> apply(TimedAlgorithmResult<PrizeSteinerTreeResult> algorithmResult) {
66+
67+
RelationshipsWritten relationshipsWritten;
68+
var writeMillis = new AtomicLong();
69+
var result = algorithmResult.result();
70+
try (var ignored = ProgressTimer.start(writeMillis::set)) {
71+
relationshipsWritten = writeStep.execute(
72+
graph,
73+
graphStore,
74+
resultStore,
75+
result,
76+
jobId
77+
);
78+
}
79+
80+
return Stream.of(
81+
new PrizeCollectingSteinerTreeWriteResult(
82+
0,
83+
algorithmResult.computeMillis(),
84+
writeMillis.get(),
85+
result.effectiveNodeCount(),
86+
result.totalWeight(),
87+
result.sumOfPrizes(),
88+
relationshipsWritten.value(),
89+
configuration
90+
)
91+
);
92+
}
93+
}

procedures/pushback-procedures-facade/src/main/java/org/neo4j/gds/procedures/algorithms/pathfinding/write/PCSTWriteResultTransformerBuilder.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,25 @@
3131
import java.util.stream.Stream;
3232

3333
class PCSTWriteResultTransformerBuilder implements ResultTransformerBuilder<TimedAlgorithmResult<PrizeSteinerTreeResult>, Stream<PrizeCollectingSteinerTreeWriteResult>> {
34-
PCSTWriteResultTransformerBuilder(PrizeCollectingSteinerTreeWriteStep writeStep, PCSTWriteConfig config) {}
34+
private final PrizeCollectingSteinerTreeWriteStep writeStep;
35+
private final PCSTWriteConfig config;
36+
37+
PCSTWriteResultTransformerBuilder(PrizeCollectingSteinerTreeWriteStep writeStep, PCSTWriteConfig config) {
38+
this.writeStep = writeStep;
39+
this.config = config;
40+
}
3541

3642
@Override
3743
public ResultTransformer<TimedAlgorithmResult<PrizeSteinerTreeResult>, Stream<PrizeCollectingSteinerTreeWriteResult>> build(
3844
GraphResources graphResources
3945
) {
40-
return ar -> Stream.empty();
46+
return new PCSTWriteResultTransformer(
47+
writeStep,
48+
graphResources.graph(),
49+
graphResources.graphStore(),
50+
graphResources.resultStore(),
51+
config.jobId(),
52+
config.toMap()
53+
);
4154
}
4255
}
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.write;
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.api.ResultStore;
26+
import org.neo4j.gds.applications.algorithms.metadata.RelationshipsWritten;
27+
import org.neo4j.gds.core.utils.progress.JobId;
28+
import org.neo4j.gds.pathfinding.PrizeCollectingSteinerTreeWriteStep;
29+
import org.neo4j.gds.pricesteiner.PrizeSteinerTreeResult;
30+
import org.neo4j.gds.result.TimedAlgorithmResult;
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 PCSTWriteResultTransformerTest {
43+
44+
@Test
45+
void shouldTransformToWriteResult() {
46+
var config = Map.<String, Object>of("foo", "bar");
47+
var graph = mock(Graph.class);
48+
var graphStore = mock(GraphStore.class);
49+
var resultStore = mock(ResultStore.class);
50+
var jobId = new JobId();
51+
var writeStep = mock(PrizeCollectingSteinerTreeWriteStep.class);
52+
53+
var algoResult = mock(PrizeSteinerTreeResult.class);
54+
when(algoResult.effectiveNodeCount()).thenReturn(1L);
55+
when(algoResult.totalWeight()).thenReturn(3d);
56+
when(algoResult.sumOfPrizes()).thenReturn(4d);
57+
58+
var relationshipsWritten = new RelationshipsWritten(5L);
59+
when(writeStep.execute(any(), any(), any(), any(), any())).thenReturn(relationshipsWritten);
60+
61+
var timedResult = new TimedAlgorithmResult<>(algoResult, 123L);
62+
63+
var transformer = new PCSTWriteResultTransformer(writeStep, graph, graphStore, resultStore, jobId, config);
64+
65+
var resultStream = transformer.apply(timedResult);
66+
var result = resultStream.findFirst().orElseThrow();
67+
68+
assertThat(result.preProcessingMillis()).isZero();
69+
assertThat(result.computeMillis()).isEqualTo(123L);
70+
assertThat(result.writeMillis()).isNotNegative();
71+
assertThat(result.configuration()).isEqualTo(config);
72+
assertThat(result.effectiveNodeCount()).isEqualTo(1L);
73+
assertThat(result.totalWeight()).isEqualTo(3d);
74+
assertThat(result.sumOfPrizes()).isEqualTo(4d);
75+
76+
assertThat(result.relationshipsWritten()).isEqualTo(5L);
77+
78+
verify(writeStep, times(1)).execute(graph, graphStore, resultStore, algoResult, jobId);
79+
verifyNoMoreInteractions(writeStep);
80+
}
81+
82+
@Test
83+
void shouldTransformEmptyResultToWriteResult() {
84+
var config = Map.<String, Object>of("boo", "foo");
85+
var graph = mock(Graph.class);
86+
var graphStore = mock(GraphStore.class);
87+
var resultStore = mock(ResultStore.class);
88+
var jobId = new JobId();
89+
var writeStep = mock(PrizeCollectingSteinerTreeWriteStep.class);
90+
when(writeStep.execute(any(), any(), any(), any(), any())).thenReturn(new RelationshipsWritten(0L));
91+
92+
var algoResult = PrizeSteinerTreeResult.EMPTY;
93+
94+
var timedResult = new TimedAlgorithmResult<>(algoResult, 123L);
95+
96+
var transformer = new PCSTWriteResultTransformer(writeStep, graph, graphStore, resultStore, jobId, config);
97+
98+
var resultStream = transformer.apply(timedResult);
99+
var result = resultStream.findFirst().orElseThrow();
100+
101+
assertThat(result.preProcessingMillis()).isZero();
102+
assertThat(result.computeMillis()).isEqualTo(123L);
103+
assertThat(result.writeMillis()).isNotNegative();
104+
assertThat(result.configuration()).isEqualTo(config);
105+
assertThat(result.effectiveNodeCount()).isEqualTo(0L);
106+
assertThat(result.totalWeight()).isEqualTo(0d);
107+
assertThat(result.sumOfPrizes()).isEqualTo(0d);
108+
assertThat(result.relationshipsWritten()).isEqualTo(0);
109+
}
110+
}

0 commit comments

Comments
 (0)