diff --git a/api/server/src/main/java/org/softwareheritage/graph/SwhId.java b/api/server/src/main/java/org/softwareheritage/graph/SwhId.java index a3f9e14..11176ca 100644 --- a/api/server/src/main/java/org/softwareheritage/graph/SwhId.java +++ b/api/server/src/main/java/org/softwareheritage/graph/SwhId.java @@ -1,38 +1,48 @@ package org.softwareheritage.graph; import com.fasterxml.jackson.annotation.JsonValue; public class SwhId { String swhId; String type; String hash; public SwhId(String swhId) { this.swhId = swhId; String[] parts = swhId.split(":"); if (parts.length != 4) { throw new IllegalArgumentException("Incorrect SWH ID format: " + swhId); } // SWH ID format: 'swh:1:type:hash' this.type = parts[2]; this.hash = parts[3]; } + @Override + public boolean equals(Object otherObj) { + if (otherObj == this) return true; + if (! (otherObj instanceof SwhId)) return false; + + SwhId other = (SwhId) otherObj; + return swhId.equals(other.getSwhId()); + } + + @Override + public String toString() { + return swhId; + } + @JsonValue public String getSwhId() { return swhId; } public String getType() { return type; } public String getHash() { return hash; } - - public String toString() { - return swhId; - } } diff --git a/api/server/src/main/java/org/softwareheritage/graph/SwhPath.java b/api/server/src/main/java/org/softwareheritage/graph/SwhPath.java new file mode 100644 index 0000000..3cd002a --- /dev/null +++ b/api/server/src/main/java/org/softwareheritage/graph/SwhPath.java @@ -0,0 +1,69 @@ +package org.softwareheritage.graph; + +import java.util.ArrayList; + +import org.softwareheritage.graph.SwhId; + +public class SwhPath { + ArrayList path; + + public SwhPath() { + this.path = new ArrayList(); + } + + public SwhPath(String ...swhIds) { + this(); + for (String swhId : swhIds) { + add(new SwhId(swhId)); + } + } + + public SwhPath(SwhId ...swhIds) { + this(); + for (SwhId swhId : swhIds) { + add(swhId); + } + } + + public void add(SwhId swhId) { + path.add(swhId); + } + + public SwhId get(int index) { + return path.get(index); + } + + public int size() { + return path.size(); + } + + @Override + public boolean equals(Object otherObj) { + if (otherObj == this) return true; + if (! (otherObj instanceof SwhPath)) return false; + + SwhPath other = (SwhPath) otherObj; + if (size() != other.size()) { + return false; + } + + for (int i = 0; i < size(); i++) { + SwhId thisId = get(i); + SwhId otherId = other.get(i); + if (! thisId.equals(otherId)) { + return false; + } + } + + return true; + } + + @Override + public String toString() { + String str = new String(); + for (SwhId swhId : path) { + str += swhId + "/"; + } + return str; + } +} diff --git a/api/server/src/main/java/org/softwareheritage/graph/algo/Visit.java b/api/server/src/main/java/org/softwareheritage/graph/algo/Visit.java index 13c58a5..f00d2a2 100644 --- a/api/server/src/main/java/org/softwareheritage/graph/algo/Visit.java +++ b/api/server/src/main/java/org/softwareheritage/graph/algo/Visit.java @@ -1,69 +1,68 @@ package org.softwareheritage.graph.algo; import java.util.ArrayList; import java.util.Stack; import it.unimi.dsi.big.webgraph.LazyLongIterator; import it.unimi.dsi.bits.LongArrayBitVector; import org.softwareheritage.graph.Graph; import org.softwareheritage.graph.SwhId; +import org.softwareheritage.graph.SwhPath; public class Visit { - public class Path extends ArrayList {} - Graph graph; boolean isTransposed; String allowedEdges; Stack currentPath; - ArrayList paths; + ArrayList paths; LongArrayBitVector visited; public Visit(Graph graph, SwhId start, String allowedEdges, String algorithm, String direction) { this.graph = graph; this.isTransposed = (direction == "backward"); this.allowedEdges = allowedEdges; - this.paths = new ArrayList(); + this.paths = new ArrayList(); this.currentPath = new Stack(); this.visited = LongArrayBitVector.ofLength(graph.getNbNodes()); if (algorithm == "dfs") { dfs(graph.getNode(start)); } } // Allow Jackson JSON to only serialize the 'paths' field - public ArrayList getPaths() { + public ArrayList getPaths() { return paths; } private void dfs(long currentNode) { visited.set(currentNode); currentPath.push(currentNode); long degree = graph.degree(currentNode, isTransposed); LazyLongIterator neighbors = graph.neighbors(currentNode, isTransposed); if (degree == 0) { - Path path = new Path(); + SwhPath path = new SwhPath(); for (long node : currentPath) { path.add(graph.getSwhId(node)); } paths.add(path); } while (degree-- > 0) { long nextNode = neighbors.nextLong(); if (isEdgeAllowed(currentNode, nextNode) && !visited.getBoolean(nextNode)) { dfs(nextNode); } } currentPath.pop(); } private boolean isEdgeAllowed(long currentNode, long nextNode) { // TODO return true; } }