Skip to content

Commit

Permalink
use more general collections
Browse files Browse the repository at this point in the history
  • Loading branch information
danthe1st committed Jul 29, 2024
1 parent 3d3b823 commit a63b56c
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.danthe1st.arebac.data.commongraph.attributed;

import java.util.List;
import java.util.Collection;

/**
* Interface for attributed graphs.
Expand All @@ -12,6 +12,6 @@
public interface AttributedGraph<N extends AttributedNode, E extends AttributedGraphEdge<N>> {
N findNodeById(String id);

List<E> findOutgoingEdges(N node);
List<E> findIncomingEdges(N node);
Collection<E> findOutgoingEdges(N node);
Collection<E> findIncomingEdges(N node);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.danthe1st.arebac.data.commongraph.memory;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand All @@ -17,11 +17,12 @@
*/
public interface CommonInMemoryGraph<N extends CommonNode, E extends CommonEdge<N>> {
Map<String, N> nodes();

Map<N, ? extends Collection<E>> outgoingEdges();

Map<N, List<E>> outgoingEdges();
Map<N, List<E>> incomingEdges();

static <N extends CommonNode, E extends CommonEdge<N>> void validate(Map<String, N> nodes, Map<N, List<E>> outgoingEdges, Map<N, List<E>> incomingEdges) {
Map<N, ? extends Collection<E>> incomingEdges();

static <N extends CommonNode, E extends CommonEdge<N>> void validate(Map<String, N> nodes, Map<N, ? extends Collection<E>> outgoingEdges, Map<N, ? extends Collection<E>> incomingEdges) {
nodes.forEach((k, v) -> {
Objects.requireNonNull(k);
Objects.requireNonNull(v);
Expand All @@ -31,7 +32,7 @@ static <N extends CommonNode, E extends CommonEdge<N>> void validate(Map<String,
});
Set<E> allEdges = new HashSet<>();
for(N node : outgoingEdges.keySet()){
List<E> edges = outgoingEdges.get(node);
Collection<E> edges = outgoingEdges.get(node);
allEdges.addAll(edges);
for(E edge : edges){
if(!node.equals(edge.source())){
Expand All @@ -41,7 +42,7 @@ static <N extends CommonNode, E extends CommonEdge<N>> void validate(Map<String,
}
int incomingEdgeCount = 0;
for(N node : incomingEdges.keySet()){
List<E> edges = incomingEdges.get(node);
Collection<E> edges = incomingEdges.get(node);
incomingEdgeCount += edges.size();
for(E edge : edges){
if(!allEdges.contains(edge)){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.danthe1st.arebac.data.graph_pattern;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
Expand All @@ -17,14 +17,14 @@
* @see GraphPattern
*/
public record GPGraph(Map<String, GPNode> nodes,
Map<GPNode, List<GPEdge>> outgoingEdges,
Map<GPNode, List<GPEdge>> incomingEdges) implements CommonInMemoryGraph<GPNode, GPEdge> {
Map<GPNode, Collection<GPEdge>> outgoingEdges,
Map<GPNode, Collection<GPEdge>> incomingEdges) implements CommonInMemoryGraph<GPNode, GPEdge> {

public GPGraph(List<GPNode> nodes, List<GPEdge> edges) {
public GPGraph(Collection<GPNode> nodes, Collection<GPEdge> edges) {
this(
nodes.stream().collect(Collectors.toMap(GPNode::id, Function.identity())),
edges.stream().collect(Collectors.groupingBy(GPEdge::source, LinkedHashMap::new, Collectors.toList())),
edges.stream().collect(Collectors.groupingBy(GPEdge::target, LinkedHashMap::new, Collectors.toList()))
edges.stream().collect(Collectors.groupingBy(GPEdge::source, Collectors.toCollection(HashSet::new))),
edges.stream().collect(Collectors.groupingBy(GPEdge::target, Collectors.toCollection(HashSet::new)))
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.danthe1st.arebac.data.graph_pattern;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -68,7 +69,7 @@ private static void checkGraphHasNode(GPGraph graph, GPNode node) {
}

private static void checkGraphHasEdge(GPGraph graph, GPEdge edge) {
List<GPEdge> edges = graph.outgoingEdges().get(edge.source());
Collection<GPEdge> edges = graph.outgoingEdges().get(edge.source());
if(edges == null){
throw new IllegalArgumentException("edge missing in graph: " + edge);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.github.danthe1st.arebac.data.memory;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -44,12 +46,12 @@ public InMemoryGraphNode findNodeById(String id) {
}

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

@Override
public List<InMemoryGraphEdge> findIncomingEdges(InMemoryGraphNode node) {
return Objects.requireNonNullElse(incomingEdges().get(node), List.of());
public Collection<InMemoryGraphEdge> findIncomingEdges(InMemoryGraphNode node) {
return Objects.requireNonNullElse(incomingEdges().get(node), Set.of());
}
}
9 changes: 5 additions & 4 deletions src/main/java/io/github/danthe1st/arebac/gpeval/GPEval.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.danthe1st.arebac.gpeval;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -279,7 +280,7 @@ private boolean forwardChecking(GPNode currentNode, Map<GPNode, Set<GPNode>> inc

private List<N> getNeighborsSatisfyingEdgeAndAttributeRequirements(GPNode currentNode, RelevantEdge relevantEdge) {
N currentNodeInDB = assignments.get(currentNode);
List<E> graphEdges;
Collection<E> graphEdges;
Function<E, N> neighborFinder;
if(relevantEdge.isOutgoing()){
graphEdges = graph.findOutgoingEdges(currentNodeInDB);
Expand Down Expand Up @@ -322,7 +323,7 @@ private boolean checkHasNecessaryEdges(GPNode node, N graphNode) {
);
}

private boolean checkNecessaryEdgesOneDirection(List<E> neighboringEdges, List<GPEdge> patternEdges, Function<E, N> edgeOtherNodeFinder, Function<GPEdge, GPNode> gpOtherNodeFinder) {
private boolean checkNecessaryEdgesOneDirection(Collection<E> neighboringEdges, Collection<GPEdge> patternEdges, Function<E, N> edgeOtherNodeFinder, Function<GPEdge, GPNode> gpOtherNodeFinder) {
for(GPEdge edge : Objects.requireNonNullElse(patternEdges, List.<GPEdge>of())){
boolean isSatisfied = false;
GPNode otherNode = gpOtherNodeFinder.apply(edge);
Expand Down Expand Up @@ -352,8 +353,8 @@ private boolean checkAttributeRequirements(List<AttributeRequirement> requiremen
}

private List<RelevantEdge> getRelevantEdges(GPNode currentNode) {
List<GPEdge> outgoingEdges = Objects.requireNonNullElse(pattern.graph().outgoingEdges().get(currentNode), List.of());
List<GPEdge> incomingEdges = Objects.requireNonNullElse(pattern.graph().incomingEdges().get(currentNode), List.of());
Collection<GPEdge> outgoingEdges = Objects.requireNonNullElse(pattern.graph().outgoingEdges().get(currentNode), Set.of());
Collection<GPEdge> incomingEdges = Objects.requireNonNullElse(pattern.graph().incomingEdges().get(currentNode), Set.of());

List<RelevantEdge> relevantEdges = new ArrayList<>();
for(GPEdge edge : incomingEdges){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.danthe1st.arebac.weaving;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -77,7 +78,7 @@ private LinkedHashMap<EdgeInGraphPattern, GPEdge> createEdges() {
LinkedHashMap<EdgeInGraphPattern, GPEdge> oldToNewEdges = new LinkedHashMap<>();
for(int patternId = 0; patternId < inputPatterns.size(); patternId++){
GraphPattern graphPattern = inputPatterns.get(patternId);
for(List<GPEdge> edgesOfNode : graphPattern.graph().incomingEdges().values()){
for(Collection<GPEdge> edgesOfNode : graphPattern.graph().incomingEdges().values()){
for(GPEdge oldEdgeInPattern : edgesOfNode){
EdgeInGraphPattern edgeInPattern = new EdgeInGraphPattern(patternId, oldEdgeInPattern);
GPEdge newEdge = convertEdge(edgeInPattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirementOperator.EQUAL;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -104,7 +105,7 @@ void evaluateGoodOutsiderFriends() {
Set<List<InMemoryGraphNode>> result = GPEval.evaluate(graph, pattern);
assertEquals(Set.of(List.of(connectorFriend)), result);
}

@Test
void evaluateConnector() {
GraphPattern pattern = createFriendOfFriendPattern(connector.id());
Expand All @@ -118,7 +119,7 @@ void evaluateGoodConnectorFriends() {
Set<List<InMemoryGraphNode>> result = GPEval.evaluate(graph, pattern);
assertEquals(Set.of(), result);
}

@Test
void evaluateCompletor() {
GraphPattern pattern = createFriendOfFriendPattern(triangleCompletor.id());
Expand All @@ -132,7 +133,7 @@ void evaluateGoodCompletorFriends() {
Set<List<InMemoryGraphNode>> result = GPEval.evaluate(graph, pattern);
assertEquals(Set.of(List.of(connectorFriend)), result);
}

// adaptation from example 17 of https://doi.org/10.1145/3401027
private GraphPattern createFriendOfFriendPattern(String requestorId) {
GPNode requestor = new GPNode("requestor", USER_NODE_TYPE);
Expand All @@ -154,7 +155,7 @@ private GraphPattern createFriendOfFriendPattern(String requestorId) {
Map.of("requestor", requestor, FRIEND_EDGE_TYPE, friend, "friendOfFriend", friendOfFriend)
);
}

private GraphPattern createGoodFriendOfFriendRequirement(String requestorId) {
GraphPattern friendOfFriendPattern = createFriendOfFriendPattern(requestorId);
Map<GPEdge, List<AttributeRequirement>> newEdgeRequirements =
Expand All @@ -163,7 +164,7 @@ private GraphPattern createGoodFriendOfFriendRequirement(String requestorId) {
.outgoingEdges()
.values()
.stream()
.flatMap(List::stream)
.flatMap(Collection::stream)
.collect(Collectors.toMap(e -> e, e -> List.of(new AttributeRequirement(GOOD_FRIEND_EDGE_ATTRIBUTE, EQUAL, attribute(true)))));
return new GraphPattern(friendOfFriendPattern.graph(), friendOfFriendPattern.mutualExclusionConstraints(), friendOfFriendPattern.nodeRequirements(), newEdgeRequirements, friendOfFriendPattern.returnedNodes(), friendOfFriendPattern.actorsToNodes());
}
Expand Down

0 comments on commit a63b56c

Please sign in to comment.