From 2d5018dd7032b24ba5a4395cbf6131fbe6ecba0d Mon Sep 17 00:00:00 2001 From: danthe1st Date: Fri, 27 Sep 2024 11:36:41 +0200 Subject: [PATCH] filter edge types --- .../attributed/AttributedGraph.java | 6 ++-- .../arebac/data/memory/InMemoryGraph.java | 29 ++++++++++++++----- .../danthe1st/arebac/gpeval/GPEval.java | 10 +++---- .../arebac/jfr/JFRRecordedGraphWrapper.java | 8 ++--- .../github/danthe1st/arebac/jfr/JFRTest.java | 4 +-- .../danthe1st/arebac/neo4j/graph/Neo4jDB.java | 13 +++++---- 6 files changed, 42 insertions(+), 28 deletions(-) diff --git a/arebac-core/src/main/java/io/github/danthe1st/arebac/data/commongraph/attributed/AttributedGraph.java b/arebac-core/src/main/java/io/github/danthe1st/arebac/data/commongraph/attributed/AttributedGraph.java index 3944f9a..b7f07f1 100644 --- a/arebac-core/src/main/java/io/github/danthe1st/arebac/data/commongraph/attributed/AttributedGraph.java +++ b/arebac-core/src/main/java/io/github/danthe1st/arebac/data/commongraph/attributed/AttributedGraph.java @@ -11,10 +11,10 @@ */ public interface AttributedGraph> { N findNodeById(String id); - - Collection findOutgoingEdges(N node); - Collection findIncomingEdges(N node); + Collection findOutgoingEdges(N node, String edgeType); + Collection findIncomingEdges(N node, String edgeType); + /** * Checks whether an attribute is unique for a specified node type. * diff --git a/arebac-core/src/main/java/io/github/danthe1st/arebac/data/memory/InMemoryGraph.java b/arebac-core/src/main/java/io/github/danthe1st/arebac/data/memory/InMemoryGraph.java index c035e6d..d583f8c 100644 --- a/arebac-core/src/main/java/io/github/danthe1st/arebac/data/memory/InMemoryGraph.java +++ b/arebac-core/src/main/java/io/github/danthe1st/arebac/data/memory/InMemoryGraph.java @@ -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; @@ -36,10 +37,10 @@ public InMemoryGraph(List nodes, List edge nodes = Map.copyOf(nodes); outgoingEdges = copy(outgoingEdges); incomingEdges = copy(incomingEdges); - + CommonInMemoryGraph.validate(nodes, outgoingEdges, incomingEdges); } - + private static Map> copy(Map> data) { Map> copy = data .entrySet() @@ -48,19 +49,31 @@ private static Map> copy(Map findOutgoingEdges(InMemoryGraphNode node) { - return Objects.requireNonNullElse(outgoingEdges().get(node), Set.of()); + public Collection findOutgoingEdges(InMemoryGraphNode node, String edgeType) { + Collection outgoingEdges = Objects.requireNonNullElse(outgoingEdges().get(node), Set.of()); + return filterEdgesByType(edgeType, outgoingEdges); + } + + private List filterEdgesByType(String edgeType, Collection outgoingEdges) { + List edgesWithType = new ArrayList<>(); + for(InMemoryGraphEdge edge : outgoingEdges){ + if(edge.hasEdgeType(edgeType)){ + edgesWithType.add(edge); + } + } + return edgesWithType; } @Override - public Collection findIncomingEdges(InMemoryGraphNode node) { - return Objects.requireNonNullElse(incomingEdges().get(node), Set.of()); + public Collection findIncomingEdges(InMemoryGraphNode node, String edgeType) { + Collection incomingEdges = Objects.requireNonNullElse(incomingEdges().get(node), Set.of()); + return filterEdgesByType(edgeType, incomingEdges); } } diff --git a/arebac-core/src/main/java/io/github/danthe1st/arebac/gpeval/GPEval.java b/arebac-core/src/main/java/io/github/danthe1st/arebac/gpeval/GPEval.java index 6062aaa..0d3e98e 100644 --- a/arebac-core/src/main/java/io/github/danthe1st/arebac/gpeval/GPEval.java +++ b/arebac-core/src/main/java/io/github/danthe1st/arebac/gpeval/GPEval.java @@ -410,10 +410,10 @@ private Collection getNeighborsSatisfyingEdgeAndAttributeRequirements(GPNode Function 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()); @@ -467,15 +467,15 @@ private List getRelevantEdges(GPNode currentNode) { List 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) { } } diff --git a/arebac-jfr/src/main/java/io/github/danthe1st/arebac/jfr/JFRRecordedGraphWrapper.java b/arebac-jfr/src/main/java/io/github/danthe1st/arebac/jfr/JFRRecordedGraphWrapper.java index 5eee595..772af55 100644 --- a/arebac-jfr/src/main/java/io/github/danthe1st/arebac/jfr/JFRRecordedGraphWrapper.java +++ b/arebac-jfr/src/main/java/io/github/danthe1st/arebac/jfr/JFRRecordedGraphWrapper.java @@ -29,20 +29,20 @@ public JFRRecordedGraphNode findNodeById(String id) { } @Override - public Collection> findOutgoingEdges(JFRRecordedGraphNode node) { + public Collection> findOutgoingEdges(JFRRecordedGraphNode node, String edgeType) { FindEdgesEvent event = new FindEdgesEvent(node.id(), Direction.OUTGOING); event.begin(); - Collection outgoingEdges = graph.findOutgoingEdges(node.getInternalNode()); + Collection outgoingEdges = graph.findOutgoingEdges(node.getInternalNode(), edgeType); event.setFoundEdgesCount(outgoingEdges.size()); event.commit(); return outgoingEdges.stream().map(JFRRecordedGraphEdge::new).toList(); } @Override - public Collection> findIncomingEdges(JFRRecordedGraphNode node) { + public Collection> findIncomingEdges(JFRRecordedGraphNode node, String edgeType) { FindEdgesEvent event = new FindEdgesEvent(node.id(), Direction.INCOMING); event.begin(); - Collection incomingEdges = graph.findIncomingEdges(node.getInternalNode()); + Collection incomingEdges = graph.findIncomingEdges(node.getInternalNode(), edgeType); event.setFoundEdgesCount(incomingEdges.size()); event.commit(); return incomingEdges.stream().map(JFRRecordedGraphEdge::new).toList(); diff --git a/arebac-jfr/src/test/java/io/github/danthe1st/arebac/jfr/JFRTest.java b/arebac-jfr/src/test/java/io/github/danthe1st/arebac/jfr/JFRTest.java index c50f5eb..93986b8 100644 --- a/arebac-jfr/src/test/java/io/github/danthe1st/arebac/jfr/JFRTest.java +++ b/arebac-jfr/src/test/java/io/github/danthe1st/arebac/jfr/JFRTest.java @@ -20,8 +20,8 @@ public static void main(String[] args) { JFRRecordedGraphWrapper jfrGraph = new JFRRecordedGraphWrapper<>(internalGraph); JFRRecordedGraphNode foundNode = jfrGraph.findNodeById("someId"); System.out.println(foundNode); - System.out.println(jfrGraph.findIncomingEdges(foundNode)); - Collection> outgoingEdges = jfrGraph.findOutgoingEdges(foundNode); + System.out.println(jfrGraph.findIncomingEdges(foundNode, "eType")); + Collection> outgoingEdges = jfrGraph.findOutgoingEdges(foundNode, "eType"); System.out.println(outgoingEdges); for(JFRRecordedGraphEdge jfrRecordedGraphEdge : outgoingEdges){ System.err.println(jfrRecordedGraphEdge.getAttribute("attr")); diff --git a/arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jDB.java b/arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jDB.java index 1c571db..5f34bce 100644 --- a/arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jDB.java +++ b/arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jDB.java @@ -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; @@ -28,18 +29,18 @@ public Neo4jNode findNodeById(String id) { } @Override - public Collection findOutgoingEdges(Neo4jNode node) { - return findEdges(node, Direction.OUTGOING); + public Collection findOutgoingEdges(Neo4jNode node, String edgeType) { + return findEdges(node, edgeType, Direction.OUTGOING); } @Override - public Collection findIncomingEdges(Neo4jNode node) { - return findEdges(node, Direction.INCOMING); + public Collection findIncomingEdges(Neo4jNode node, String edgeType) { + return findEdges(node, edgeType, Direction.INCOMING); } - private Collection findEdges(Neo4jNode node, Direction direction) { + private Collection findEdges(Neo4jNode node, String edgeType, Direction direction) { List edges = new ArrayList<>(); - try(ResourceIterable relationships = node.getDBNode().getRelationships(direction)){ + try(ResourceIterable relationships = node.getDBNode().getRelationships(direction, RelationshipType.withName(edgeType))){ for(Relationship relationship : relationships){ edges.add(new Neo4jEdge(relationship)); }