-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c9a986
commit 4365a49
Showing
11 changed files
with
460 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...rg/neo4j/gds/applications/algorithms/pathfinding/PrizeCollectingSteinerTreeWriteStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.gds.applications.algorithms.pathfinding; | ||
|
||
import org.neo4j.gds.api.Graph; | ||
import org.neo4j.gds.api.GraphStore; | ||
import org.neo4j.gds.api.ResultStore; | ||
import org.neo4j.gds.applications.algorithms.machinery.RequestScopedDependencies; | ||
import org.neo4j.gds.applications.algorithms.machinery.WriteContext; | ||
import org.neo4j.gds.applications.algorithms.machinery.WriteStep; | ||
import org.neo4j.gds.applications.algorithms.metadata.RelationshipsWritten; | ||
import org.neo4j.gds.core.utils.progress.JobId; | ||
import org.neo4j.gds.core.utils.progress.tasks.ProgressTracker; | ||
import org.neo4j.gds.pcst.PCSTWriteConfig; | ||
import org.neo4j.gds.pricesteiner.PrizeSteinerTreeResult; | ||
import org.neo4j.gds.spanningtree.SpanningGraph; | ||
import org.neo4j.gds.spanningtree.SpanningTree; | ||
|
||
class PrizeCollectingSteinerTreeWriteStep implements WriteStep<PrizeSteinerTreeResult, RelationshipsWritten> { | ||
private final RequestScopedDependencies requestScopedDependencies; | ||
private final PCSTWriteConfig configuration; | ||
private final WriteContext writeContext; | ||
|
||
PrizeCollectingSteinerTreeWriteStep( | ||
RequestScopedDependencies requestScopedDependencies, | ||
WriteContext writeContext, | ||
PCSTWriteConfig configuration | ||
) { | ||
this.requestScopedDependencies = requestScopedDependencies; | ||
this.configuration = configuration; | ||
this.writeContext = writeContext; | ||
} | ||
|
||
@Override | ||
public RelationshipsWritten execute( | ||
Graph graph, | ||
GraphStore graphStore, | ||
ResultStore resultStore, | ||
PrizeSteinerTreeResult steinerTreeResult, | ||
JobId jobId | ||
) { | ||
|
||
var spanningTree = new SpanningTree( | ||
-1, | ||
graph.nodeCount(), | ||
steinerTreeResult.effectiveNodeCount(), | ||
steinerTreeResult.parentArray(), | ||
nodeId -> steinerTreeResult.relationshipToParentCost().get(nodeId), | ||
steinerTreeResult.totalWeight() | ||
); | ||
var spanningGraph = new SpanningGraph(graph, spanningTree); | ||
|
||
var relationshipExporter = writeContext.relationshipExporterBuilder() | ||
.withGraph(spanningGraph) | ||
.withIdMappingOperator(spanningGraph::toOriginalNodeId) | ||
.withTerminationFlag(requestScopedDependencies.terminationFlag()) | ||
.withProgressTracker(ProgressTracker.NULL_TRACKER) | ||
.withResultStore(configuration.resolveResultStore(resultStore)) | ||
.withJobId(configuration.jobId()) | ||
.build(); | ||
|
||
relationshipExporter.write(configuration.writeRelationshipType(), configuration.writeProperty()); | ||
|
||
return new RelationshipsWritten(steinerTreeResult.effectiveNodeCount() - 1); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...g/src/main/java/org/neo4j/gds/paths/prizesteiner/PrizeCollectingSteinerTreeWriteProc.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.gds.paths.prizesteiner; | ||
|
||
import org.neo4j.gds.applications.algorithms.machinery.MemoryEstimateResult; | ||
import org.neo4j.gds.procedures.GraphDataScienceProcedures; | ||
import org.neo4j.gds.procedures.algorithms.pathfinding.PrizeCollectingSteinerTreeWriteResult; | ||
import org.neo4j.procedure.Context; | ||
import org.neo4j.procedure.Description; | ||
import org.neo4j.procedure.Name; | ||
import org.neo4j.procedure.Procedure; | ||
|
||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
import static org.neo4j.gds.procedures.ProcedureConstants.MEMORY_ESTIMATION_DESCRIPTION; | ||
import static org.neo4j.procedure.Mode.READ; | ||
import static org.neo4j.procedure.Mode.WRITE; | ||
|
||
public class PrizeCollectingSteinerTreeWriteProc { | ||
@Context | ||
public GraphDataScienceProcedures facade; | ||
|
||
@Procedure(value = "gds.prizeSteinerTree.write", mode = WRITE) | ||
@Description(Constants.PRIZE_STEINER_DESCRIPTION) | ||
public Stream<PrizeCollectingSteinerTreeWriteResult> steinerTree( | ||
@Name(value = "graphName") String graphName, | ||
@Name(value = "configuration", defaultValue = "{}") Map<String, Object> configuration | ||
) { | ||
return facade.algorithms().pathFinding().prizeCollectingSteinerTreeWrite(graphName, configuration); | ||
} | ||
|
||
@Procedure(value = "gds.prizeSteinerTree.write.estimate", mode = READ) | ||
@Description(MEMORY_ESTIMATION_DESCRIPTION) | ||
public Stream<MemoryEstimateResult> estimate( | ||
@Name(value = "graphNameOrConfiguration") Object graphNameOrConfiguration, | ||
@Name(value = "algoConfiguration") Map<String, Object> algoConfiguration | ||
) { | ||
return facade.algorithms().pathFinding().prizeCollectingSteinerTreeWriteEstimate(graphNameOrConfiguration, algoConfiguration); | ||
} | ||
|
||
} |
114 changes: 114 additions & 0 deletions
114
...c/test/java/org/neo4j/gds/paths/prizesteiner/PrizeCollectingSteinerTreeWriteProcTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.gds.paths.prizesteiner; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.neo4j.gds.BaseProcTest; | ||
import org.neo4j.gds.GdsCypher; | ||
import org.neo4j.gds.Orientation; | ||
import org.neo4j.gds.catalog.GraphProjectProc; | ||
import org.neo4j.gds.extension.IdFunction; | ||
import org.neo4j.gds.extension.Inject; | ||
import org.neo4j.gds.extension.Neo4jGraph; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class PrizeCollectingSteinerTreeWriteProcTest extends BaseProcTest { | ||
|
||
@Neo4jGraph | ||
static final String DB_CYPHER = | ||
"CREATE(a:Node{p:100.0}) " + | ||
"CREATE(b:Node{p:100.0}) " + | ||
"CREATE(c:Node{p:169.0}) " + | ||
"CREATE (a)-[:TYPE {cost:60.0}]->(b) "; | ||
|
||
@Inject | ||
private IdFunction idFunction; | ||
|
||
@BeforeEach | ||
void setup() throws Exception { | ||
|
||
registerProcedures(PrizeCollectingSteinerTreeWriteProc.class, GraphProjectProc.class); | ||
var createQuery = GdsCypher.call(DEFAULT_GRAPH_NAME) | ||
.graphProject() | ||
.withAnyLabel() | ||
.withNodeProperty("p") | ||
.withRelationshipType("TYPE", Orientation.UNDIRECTED) | ||
.withRelationshipProperty("cost") | ||
.yields(); | ||
runQuery(createQuery); | ||
} | ||
|
||
@Test | ||
void testWrite() { | ||
String query = GdsCypher.call(DEFAULT_GRAPH_NAME) | ||
.algo("gds.prizeSteinerTree") | ||
.writeMode() | ||
.addParameter("prizeProperty","p") | ||
.addParameter("relationshipWeightProperty", "cost") | ||
.addParameter("writeRelationshipType", "PCST") | ||
.addParameter("writeProperty", "cost") | ||
.yields( | ||
"effectiveNodeCount", "totalWeight", "sumOfPrizes", "relationshipsWritten" | ||
); | ||
|
||
var rowCount = runQueryWithRowConsumer(query, | ||
resultRow -> { | ||
|
||
assertThat(resultRow.get("effectiveNodeCount")).isInstanceOf(Long.class); | ||
assertThat(resultRow.get("totalWeight")).isInstanceOf(Double.class); | ||
assertThat(resultRow.get("sumOfPrizes")).isInstanceOf(Double.class); | ||
assertThat(resultRow.get("relationshipsWritten")).isInstanceOf(Long.class); | ||
|
||
assertThat((long) resultRow.get("effectiveNodeCount")).isEqualTo(2L); | ||
assertThat((double) resultRow.get("totalWeight")).isEqualTo(60); | ||
assertThat((double) resultRow.get("sumOfPrizes")).isEqualTo(200.0); | ||
assertThat((long) resultRow.get("relationshipsWritten")).isEqualTo(1L); | ||
|
||
}); | ||
assertThat(rowCount).isEqualTo(1L); | ||
|
||
var sourceNode = idFunction.of("a"); | ||
var terminalNode = idFunction.of("b"); | ||
|
||
var rowCountCheck = runQueryWithRowConsumer( | ||
"MATCH (a)-[r:PCST]->(b) RETURN id(a) AS a, id(b) AS b, r.cost AS cost", | ||
row -> { | ||
var a = row.getNumber("a").longValue(); | ||
var b = row.getNumber("b").longValue(); | ||
|
||
assertThat(a).isNotEqualTo(b); | ||
|
||
assertThat(a).isIn(List.of(sourceNode,terminalNode)); | ||
assertThat(b).isIn(List.of(sourceNode,terminalNode)); | ||
|
||
var writtenCost = row.getNumber("cost").doubleValue(); | ||
assertThat(writtenCost) | ||
.isEqualTo(60.0); | ||
} | ||
); | ||
|
||
assertThat(rowCountCheck).isEqualTo(1L); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.