Skip to content

Commit

Permalink
filter edge types
Browse files Browse the repository at this point in the history
  • Loading branch information
danthe1st committed Sep 27, 2024
1 parent d14cb9a commit 2d5018d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
*/
public interface AttributedGraph<N extends AttributedNode, E extends AttributedGraphEdge<N>> {
N findNodeById(String id);

Collection<E> findOutgoingEdges(N node);
Collection<E> findIncomingEdges(N node);

Collection<E> findOutgoingEdges(N node, String edgeType);
Collection<E> findIncomingEdges(N node, String edgeType);

/**
* Checks whether an attribute is unique for a specified node type.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.danthe1st.arebac.data.memory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -36,10 +37,10 @@ public InMemoryGraph(List<InMemoryGraphNode> nodes, List<InMemoryGraphEdge> edge
nodes = Map.copyOf(nodes);
outgoingEdges = copy(outgoingEdges);
incomingEdges = copy(incomingEdges);

CommonInMemoryGraph.validate(nodes, outgoingEdges, incomingEdges);
}

private static Map<InMemoryGraphNode, List<InMemoryGraphEdge>> copy(Map<InMemoryGraphNode, List<InMemoryGraphEdge>> data) {
Map<InMemoryGraphNode, List<InMemoryGraphEdge>> copy = data
.entrySet()
Expand All @@ -48,19 +49,31 @@ private static Map<InMemoryGraphNode, List<InMemoryGraphEdge>> copy(Map<InMemory
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
return Map.copyOf(copy);
}

@Override
public InMemoryGraphNode findNodeById(String id) {
return nodes().get(id);
}

@Override
public Collection<InMemoryGraphEdge> findOutgoingEdges(InMemoryGraphNode node) {
return Objects.requireNonNullElse(outgoingEdges().get(node), Set.of());
public Collection<InMemoryGraphEdge> findOutgoingEdges(InMemoryGraphNode node, String edgeType) {
Collection<InMemoryGraphEdge> outgoingEdges = Objects.requireNonNullElse(outgoingEdges().get(node), Set.of());
return filterEdgesByType(edgeType, outgoingEdges);
}

private List<InMemoryGraphEdge> filterEdgesByType(String edgeType, Collection<InMemoryGraphEdge> outgoingEdges) {
List<InMemoryGraphEdge> edgesWithType = new ArrayList<>();
for(InMemoryGraphEdge edge : outgoingEdges){
if(edge.hasEdgeType(edgeType)){
edgesWithType.add(edge);
}
}
return edgesWithType;
}

@Override
public Collection<InMemoryGraphEdge> findIncomingEdges(InMemoryGraphNode node) {
return Objects.requireNonNullElse(incomingEdges().get(node), Set.of());
public Collection<InMemoryGraphEdge> findIncomingEdges(InMemoryGraphNode node, String edgeType) {
Collection<InMemoryGraphEdge> incomingEdges = Objects.requireNonNullElse(incomingEdges().get(node), Set.of());
return filterEdgesByType(edgeType, incomingEdges);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,10 @@ private Collection<N> getNeighborsSatisfyingEdgeAndAttributeRequirements(GPNode
Function<E, N> neighborFinder;
if(relevantEdge.isOutgoing()){
// TODO make this more efficient by finding the exact relevant edges
graphEdges = graph.findOutgoingEdges(currentNodeInDB);
graphEdges = graph.findOutgoingEdges(currentNodeInDB, relevantEdge.edgeType());
neighborFinder = AttributedGraphEdge::target;
}else{
graphEdges = graph.findIncomingEdges(currentNodeInDB);
graphEdges = graph.findIncomingEdges(currentNodeInDB, relevantEdge.edgeType());
neighborFinder = AttributedGraphEdge::source;
}
graphEdges = Objects.requireNonNullElse(graphEdges, List.of());
Expand Down Expand Up @@ -467,15 +467,15 @@ private List<RelevantEdge> getRelevantEdges(GPNode currentNode) {

List<RelevantEdge> relevantEdges = new ArrayList<>();
for(GPEdge edge : incomingEdges){
relevantEdges.add(new RelevantEdge(edge, edge.source(), false));
relevantEdges.add(new RelevantEdge(edge, edge.source(), edge.edgeType(), false));
}
for(GPEdge edge : outgoingEdges){
relevantEdges.add(new RelevantEdge(edge, edge.target(), true));
relevantEdges.add(new RelevantEdge(edge, edge.target(), edge.edgeType(), true));
}
return relevantEdges;
}

private record RelevantEdge(GPEdge edge, GPNode otherNode, boolean isOutgoing) {
private record RelevantEdge(GPEdge edge, GPNode otherNode, String edgeType, boolean isOutgoing) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ public JFRRecordedGraphNode<N> findNodeById(String id) {
}

@Override
public Collection<JFRRecordedGraphEdge<N, E>> findOutgoingEdges(JFRRecordedGraphNode<N> node) {
public Collection<JFRRecordedGraphEdge<N, E>> findOutgoingEdges(JFRRecordedGraphNode<N> node, String edgeType) {
FindEdgesEvent event = new FindEdgesEvent(node.id(), Direction.OUTGOING);
event.begin();
Collection<E> outgoingEdges = graph.findOutgoingEdges(node.getInternalNode());
Collection<E> outgoingEdges = graph.findOutgoingEdges(node.getInternalNode(), edgeType);
event.setFoundEdgesCount(outgoingEdges.size());
event.commit();
return outgoingEdges.stream().map(JFRRecordedGraphEdge::new).toList();
}

@Override
public Collection<JFRRecordedGraphEdge<N, E>> findIncomingEdges(JFRRecordedGraphNode<N> node) {
public Collection<JFRRecordedGraphEdge<N, E>> findIncomingEdges(JFRRecordedGraphNode<N> node, String edgeType) {
FindEdgesEvent event = new FindEdgesEvent(node.id(), Direction.INCOMING);
event.begin();
Collection<E> incomingEdges = graph.findIncomingEdges(node.getInternalNode());
Collection<E> incomingEdges = graph.findIncomingEdges(node.getInternalNode(), edgeType);
event.setFoundEdgesCount(incomingEdges.size());
event.commit();
return incomingEdges.stream().map(JFRRecordedGraphEdge::new).toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public static void main(String[] args) {
JFRRecordedGraphWrapper<InMemoryGraphNode, InMemoryGraphEdge> jfrGraph = new JFRRecordedGraphWrapper<>(internalGraph);
JFRRecordedGraphNode<InMemoryGraphNode> foundNode = jfrGraph.findNodeById("someId");
System.out.println(foundNode);
System.out.println(jfrGraph.findIncomingEdges(foundNode));
Collection<JFRRecordedGraphEdge<InMemoryGraphNode, InMemoryGraphEdge>> outgoingEdges = jfrGraph.findOutgoingEdges(foundNode);
System.out.println(jfrGraph.findIncomingEdges(foundNode, "eType"));
Collection<JFRRecordedGraphEdge<InMemoryGraphNode, InMemoryGraphEdge>> outgoingEdges = jfrGraph.findOutgoingEdges(foundNode, "eType");
System.out.println(outgoingEdges);
for(JFRRecordedGraphEdge<InMemoryGraphNode, InMemoryGraphEdge> jfrRecordedGraphEdge : outgoingEdges){
System.err.println(jfrRecordedGraphEdge.getAttribute("attr"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.ResourceIterable;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.schema.ConstraintDefinition;
Expand All @@ -28,18 +29,18 @@ public Neo4jNode findNodeById(String id) {
}

@Override
public Collection<Neo4jEdge> findOutgoingEdges(Neo4jNode node) {
return findEdges(node, Direction.OUTGOING);
public Collection<Neo4jEdge> findOutgoingEdges(Neo4jNode node, String edgeType) {
return findEdges(node, edgeType, Direction.OUTGOING);
}

@Override
public Collection<Neo4jEdge> findIncomingEdges(Neo4jNode node) {
return findEdges(node, Direction.INCOMING);
public Collection<Neo4jEdge> findIncomingEdges(Neo4jNode node, String edgeType) {
return findEdges(node, edgeType, Direction.INCOMING);
}

private Collection<Neo4jEdge> findEdges(Neo4jNode node, Direction direction) {
private Collection<Neo4jEdge> findEdges(Neo4jNode node, String edgeType, Direction direction) {
List<Neo4jEdge> edges = new ArrayList<>();
try(ResourceIterable<Relationship> relationships = node.getDBNode().getRelationships(direction)){
try(ResourceIterable<Relationship> relationships = node.getDBNode().getRelationships(direction, RelationshipType.withName(edgeType))){
for(Relationship relationship : relationships){
edges.add(new Neo4jEdge(relationship));
}
Expand Down

0 comments on commit 2d5018d

Please sign in to comment.