|
| 1 | +/* |
| 2 | + * Copyright © 2021-present Arcade Data Ltd ([email protected]) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-FileCopyrightText: 2021-present Arcade Data Ltd ([email protected]) |
| 17 | + * SPDX-License-Identifier: Apache-2.0 |
| 18 | + */ |
| 19 | +package com.arcadedb.query.opencypher.procedures.algo; |
| 20 | + |
| 21 | +import com.arcadedb.database.Database; |
| 22 | +import com.arcadedb.database.DatabaseFactory; |
| 23 | +import com.arcadedb.graph.MutableVertex; |
| 24 | +import com.arcadedb.graph.Vertex; |
| 25 | +import com.arcadedb.query.sql.executor.Result; |
| 26 | +import com.arcadedb.query.sql.executor.ResultSet; |
| 27 | +import org.junit.jupiter.api.AfterEach; |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | + |
| 37 | +/** |
| 38 | + * Tests for the algo.dijkstra Cypher procedure. |
| 39 | + * |
| 40 | + * Regression for issue #4042: yielded weight was always 0 because the path returned by the |
| 41 | + * underlying A* implementation only contains vertex RIDs, not edges. Weight must be reconstructed |
| 42 | + * by traversing the edges between consecutive vertices on the path. |
| 43 | + * |
| 44 | + * @author Luca Garulli ([email protected]) |
| 45 | + */ |
| 46 | +class AlgoDijkstraTest { |
| 47 | + private Database database; |
| 48 | + |
| 49 | + @BeforeEach |
| 50 | + void setup() { |
| 51 | + final DatabaseFactory factory = new DatabaseFactory("./target/databases/test-algo-dijkstra"); |
| 52 | + if (factory.exists()) |
| 53 | + factory.open().drop(); |
| 54 | + database = factory.create(); |
| 55 | + database.getSchema().createVertexType("Node"); |
| 56 | + database.getSchema().createEdgeType("Road"); |
| 57 | + |
| 58 | + // Start --2--> Middle --3--> Finish |
| 59 | + database.transaction(() -> { |
| 60 | + final MutableVertex start = database.newVertex("Node").set("name", "Start").save(); |
| 61 | + final MutableVertex middle = database.newVertex("Node").set("name", "Middle").save(); |
| 62 | + final MutableVertex finish = database.newVertex("Node").set("name", "Finish").save(); |
| 63 | + start.newEdge("Road", middle, true, new Object[] { "distance", 2 }).save(); |
| 64 | + middle.newEdge("Road", finish, true, new Object[] { "distance", 3 }).save(); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + @AfterEach |
| 69 | + void teardown() { |
| 70 | + if (database != null) |
| 71 | + database.drop(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void dijkstraComputesCorrectWeight() { |
| 76 | + final ResultSet rs = database.query("opencypher", |
| 77 | + """ |
| 78 | + MATCH (src:Node {name:'Start'}),(dst:Node {name:'Finish'}) \ |
| 79 | + CALL algo.dijkstra(src, dst, 'Road', 'distance') \ |
| 80 | + YIELD path, weight RETURN weight"""); |
| 81 | + |
| 82 | + assertThat(rs.hasNext()).isTrue(); |
| 83 | + final Result result = rs.next(); |
| 84 | + final Object weight = result.getProperty("weight"); |
| 85 | + assertThat(weight).isInstanceOf(Number.class); |
| 86 | + assertThat(((Number) weight).doubleValue()).isEqualTo(5.0); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void dijkstraPathIncludesEdges() { |
| 91 | + final ResultSet rs = database.query("opencypher", |
| 92 | + """ |
| 93 | + MATCH (src:Node {name:'Start'}),(dst:Node {name:'Finish'}) \ |
| 94 | + CALL algo.dijkstra(src, dst, 'Road', 'distance') \ |
| 95 | + YIELD path, weight RETURN path, weight"""); |
| 96 | + |
| 97 | + assertThat(rs.hasNext()).isTrue(); |
| 98 | + final Result result = rs.next(); |
| 99 | + @SuppressWarnings("unchecked") |
| 100 | + final Map<String, Object> path = (Map<String, Object>) result.getProperty("path"); |
| 101 | + assertThat(path).isNotNull(); |
| 102 | + |
| 103 | + final List<?> nodes = (List<?>) path.get("nodes"); |
| 104 | + final List<?> relationships = (List<?>) path.get("relationships"); |
| 105 | + assertThat(nodes).hasSize(3); |
| 106 | + assertThat(relationships).hasSize(2); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void dijkstraStartEqualsEnd() { |
| 111 | + final ResultSet rs = database.query("opencypher", |
| 112 | + """ |
| 113 | + MATCH (src:Node {name:'Start'}) \ |
| 114 | + CALL algo.dijkstra(src, src, 'Road', 'distance') \ |
| 115 | + YIELD path, weight RETURN weight"""); |
| 116 | + |
| 117 | + assertThat(rs.hasNext()).isTrue(); |
| 118 | + final Result result = rs.next(); |
| 119 | + assertThat(((Number) result.getProperty("weight")).doubleValue()).isEqualTo(0.0); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void dijkstraMultiHopAccumulatesWeight() { |
| 124 | + // Add a fourth hop: Finish --4--> End |
| 125 | + database.transaction(() -> { |
| 126 | + final Vertex finish = database.query("sql", |
| 127 | + "SELECT FROM Node WHERE name='Finish'").next().getElement().get().asVertex(); |
| 128 | + final MutableVertex end = database.newVertex("Node").set("name", "End").save(); |
| 129 | + finish.modify().newEdge("Road", end, true, new Object[] { "distance", 4 }).save(); |
| 130 | + }); |
| 131 | + |
| 132 | + final ResultSet rs = database.query("opencypher", |
| 133 | + """ |
| 134 | + MATCH (src:Node {name:'Start'}),(dst:Node {name:'End'}) \ |
| 135 | + CALL algo.dijkstra(src, dst, 'Road', 'distance') \ |
| 136 | + YIELD path, weight RETURN weight"""); |
| 137 | + |
| 138 | + assertThat(rs.hasNext()).isTrue(); |
| 139 | + final Result result = rs.next(); |
| 140 | + assertThat(((Number) result.getProperty("weight")).doubleValue()).isEqualTo(9.0); |
| 141 | + } |
| 142 | +} |
0 commit comments