Changeset View
Changeset View
Standalone View
Standalone View
java/src/main/java/org/softwareheritage/graph/AllowedEdges.java
Show All 14 Lines | |||||
public class AllowedEdges { | public class AllowedEdges { | ||||
/** | /** | ||||
* 2D boolean matrix storing access rights for all combination of src/dst node types (first | * 2D boolean matrix storing access rights for all combination of src/dst node types (first | ||||
* dimension is source, second dimension is destination), when edge restriction is not enforced | * dimension is source, second dimension is destination), when edge restriction is not enforced | ||||
* this array is set to null for early bypass. | * this array is set to null for early bypass. | ||||
*/ | */ | ||||
public boolean[][] restrictedTo; | public boolean[][] restrictedTo; | ||||
/** Graph on which edge restriction is performed */ | |||||
Graph graph; | |||||
/** | /** | ||||
* Constructor. | * Constructor. | ||||
* | * | ||||
* @param graph the graph on which to perform edge restriction | |||||
* @param edgesFmt a formatted string describing <a | * @param edgesFmt a formatted string describing <a | ||||
* href="https://docs.softwareheritage.org/devel/swh-graph/api.html#terminology">allowed edges</a> | * href="https://docs.softwareheritage.org/devel/swh-graph/api.html#terminology">allowed edges</a> | ||||
*/ | */ | ||||
public AllowedEdges(Graph graph, String edgesFmt) { | public AllowedEdges(String edgesFmt) { | ||||
this.graph = graph; | |||||
int nbNodeTypes = Node.Type.values().length; | int nbNodeTypes = Node.Type.values().length; | ||||
this.restrictedTo = new boolean[nbNodeTypes][nbNodeTypes]; | this.restrictedTo = new boolean[nbNodeTypes][nbNodeTypes]; | ||||
// Special values (null, empty, "*") | // Special values (null, empty, "*") | ||||
if (edgesFmt == null || edgesFmt.isEmpty()) { | if (edgesFmt == null || edgesFmt.isEmpty()) { | ||||
return; | return; | ||||
} | } | ||||
if (edgesFmt.equals("*")) { | if (edgesFmt.equals("*")) { | ||||
// Allows for quick bypass (with simple null check) when no edge restriction | // Allows for quick bypass (with simple null check) when no edge restriction | ||||
Show All 17 Lines | public AllowedEdges(String edgesFmt) { | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Checks if a given edge can be followed during graph traversal. | * Checks if a given edge can be followed during graph traversal. | ||||
* | * | ||||
* @param srcNodeId edge source node | * @param srcType edge source type | ||||
* @param dstNodeId edge destination node | * @param dstType edge destination type | ||||
* @return true if allowed and false otherwise | * @return true if allowed and false otherwise | ||||
*/ | */ | ||||
public boolean isAllowed(long srcNodeId, long dstNodeId) { | |||||
Node.Type srcType = graph.getNodeType(srcNodeId); | |||||
Node.Type dstType = graph.getNodeType(dstNodeId); | |||||
return restrictedTo[srcType.ordinal()][dstType.ordinal()]; | |||||
} | |||||
/** | |||||
* Works like {@link AllowedEdges#isAllowed(long, long)} but with node types directly instead of | |||||
* node ids. | |||||
* | |||||
* @see AllowedEdges#isAllowed(long, long) | |||||
*/ | |||||
public boolean isAllowed(Node.Type srcType, Node.Type dstType) { | public boolean isAllowed(Node.Type srcType, Node.Type dstType) { | ||||
if (restrictedTo == null) | |||||
return true; | |||||
return restrictedTo[srcType.ordinal()][dstType.ordinal()]; | return restrictedTo[srcType.ordinal()][dstType.ordinal()]; | ||||
} | } | ||||
} | } |