Skip to content

Commit

Permalink
use static imports for graph pattern creation
Browse files Browse the repository at this point in the history
  • Loading branch information
danthe1st committed Aug 4, 2024
1 parent 66fe721 commit 28ecb85
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package io.github.danthe1st.arebac.tests.gpeval;

import static io.github.danthe1st.arebac.data.commongraph.attributed.AttributeValue.attribute;
import static io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirement.ID_KEY;
import static io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirementOperator.EQUAL;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import java.util.Map;
import java.util.Set;

import io.github.danthe1st.arebac.data.commongraph.attributed.AttributeValue;
import io.github.danthe1st.arebac.data.graph_pattern.GPEdge;
import io.github.danthe1st.arebac.data.graph_pattern.GPGraph;
import io.github.danthe1st.arebac.data.graph_pattern.GPNode;
import io.github.danthe1st.arebac.data.graph_pattern.GraphPattern;
import io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirement;
import io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirementOperator;
import io.github.danthe1st.arebac.data.memory.InMemoryGraph;
import io.github.danthe1st.arebac.data.memory.InMemoryGraphEdge;
import io.github.danthe1st.arebac.data.memory.InMemoryGraphNode;
Expand All @@ -27,9 +28,9 @@ class DifferentEdgeTypesTest {
void test() {
InMemoryGraphNode requestorNode = new InMemoryGraphNode(REQUESTOR_ID, NODE_TYPE, Map.of());
InMemoryGraphNode fakeNode = new InMemoryGraphNode("fake", NODE_TYPE, Map.of());
InMemoryGraphNode fakeEndNode = new InMemoryGraphNode("fakeEnd", NODE_TYPE, Map.of("end", AttributeValue.attribute(false)));
InMemoryGraphNode fakeEndNode = new InMemoryGraphNode("fakeEnd", NODE_TYPE, Map.of("end", attribute(false)));
InMemoryGraphNode middleNode = new InMemoryGraphNode("mid", NODE_TYPE, Map.of());
InMemoryGraphNode endNode = new InMemoryGraphNode("end", NODE_TYPE, Map.of("end", AttributeValue.attribute(true)));
InMemoryGraphNode endNode = new InMemoryGraphNode("end", NODE_TYPE, Map.of("end", attribute(true)));

InMemoryGraph graph = new InMemoryGraph(
List.of(requestorNode, fakeNode, fakeEndNode, middleNode, endNode),
Expand All @@ -40,11 +41,11 @@ void test() {
new InMemoryGraphEdge(middleNode, endNode, "mid->end", "e2", Map.of())
)
);

Set<List<InMemoryGraphNode>> result = GPEval.evaluate(graph, createPattern());
assertEquals(Set.of(List.of(middleNode, endNode)), result);
}

private GraphPattern createPattern() {
GPNode requestor = new GPNode("requestor", NODE_TYPE);
GPNode someNode = new GPNode("someNode", NODE_TYPE);
Expand All @@ -59,8 +60,8 @@ private GraphPattern createPattern() {
),
List.of(),
Map.of(
requestor, List.of(new AttributeRequirement(AttributeRequirement.ID_KEY, AttributeRequirementOperator.EQUAL, AttributeValue.attribute(REQUESTOR_ID))),
otherNode, List.of(new AttributeRequirement("end", AttributeRequirementOperator.EQUAL, AttributeValue.attribute(true)))
requestor, List.of(new AttributeRequirement(ID_KEY, EQUAL, attribute(REQUESTOR_ID))),
otherNode, List.of(new AttributeRequirement("end", EQUAL, attribute(true)))
),
Map.of(),
List.of(someNode, otherNode), Map.of()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package io.github.danthe1st.arebac.tests.gpeval;

import static io.github.danthe1st.arebac.data.commongraph.attributed.AttributeValue.attribute;
import static io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirement.ID_KEY;
import static io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirementOperator.EQUAL;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import java.util.Map;
import java.util.Set;

import io.github.danthe1st.arebac.data.commongraph.attributed.AttributeValue;
import io.github.danthe1st.arebac.data.graph_pattern.GPEdge;
import io.github.danthe1st.arebac.data.graph_pattern.GPGraph;
import io.github.danthe1st.arebac.data.graph_pattern.GPNode;
import io.github.danthe1st.arebac.data.graph_pattern.GraphPattern;
import io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirement;
import io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirementOperator;
import io.github.danthe1st.arebac.data.memory.InMemoryGraph;
import io.github.danthe1st.arebac.data.memory.InMemoryGraphNode;
import io.github.danthe1st.arebac.gpeval.GPEval;
Expand All @@ -31,7 +32,7 @@ void test() {
List.of(node, otherNode),
List.of(new GPEdge(node, otherNode, "", EDGE_TYPE))
), List.of(),
Map.of(node, List.of(new AttributeRequirement(AttributeRequirement.ID_KEY, AttributeRequirementOperator.EQUAL, AttributeValue.attribute("123")))),
Map.of(node, List.of(new AttributeRequirement(ID_KEY, EQUAL, attribute("123")))),
Map.of(), List.of(otherNode), Map.of()
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package io.github.danthe1st.arebac.tests.gpeval;

import static io.github.danthe1st.arebac.data.commongraph.attributed.AttributeValue.attribute;
import static io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirement.ID_KEY;
import static io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirementOperator.EQUAL;
import static io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirementOperator.LESS_THAN;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import java.util.Map;
import java.util.Set;

import io.github.danthe1st.arebac.data.commongraph.attributed.AttributeValue;
import io.github.danthe1st.arebac.data.graph_pattern.GPEdge;
import io.github.danthe1st.arebac.data.graph_pattern.GPGraph;
import io.github.danthe1st.arebac.data.graph_pattern.GPNode;
import io.github.danthe1st.arebac.data.graph_pattern.GraphPattern;
import io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirement;
import io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirementOperator;
import io.github.danthe1st.arebac.data.memory.InMemoryGraph;
import io.github.danthe1st.arebac.data.memory.InMemoryGraphEdge;
import io.github.danthe1st.arebac.data.memory.InMemoryGraphNode;
Expand All @@ -38,11 +40,11 @@ private GraphPattern createStudentGraphPattern() {
graph,
List.of(),
Map.of(
requestor, List.of(new AttributeRequirement(AttributeRequirement.ID_KEY, AttributeRequirementOperator.EQUAL, AttributeValue.attribute("1337331"))),
student, List.of(new AttributeRequirement(AttributeRequirement.ID_KEY, AttributeRequirementOperator.EQUAL, AttributeValue.attribute("12345678")))
requestor, List.of(new AttributeRequirement(ID_KEY, EQUAL, attribute("1337331"))),
student, List.of(new AttributeRequirement(ID_KEY, EQUAL, attribute("12345678")))
),
Map.of(
attendCourse, List.of(new AttributeRequirement("grade", AttributeRequirementOperator.LESS_THAN, new AttributeValue.IntAttribute(5)))
attendCourse, List.of(new AttributeRequirement("grade", LESS_THAN, attribute(5)))
),
List.of(course),
Map.of("course", course)// not needed for GP-Eval
Expand All @@ -54,8 +56,8 @@ private InMemoryGraph createSampleGraph() {
InMemoryGraphNode student = new InMemoryGraphNode("12345678", "student", Map.of());
InMemoryGraphNode course = new InMemoryGraphNode("somecourse", "course", Map.of());
InMemoryGraphNode otherCourse = new InMemoryGraphNode("othercourse", "course", Map.of());
InMemoryGraphEdge attendCourse = new InMemoryGraphEdge(student, course, "ac", "attend_course", Map.of("grade", AttributeValue.attribute(1)));
InMemoryGraphEdge attendOtherCourse = new InMemoryGraphEdge(student, otherCourse, "ac2", "attend_course", Map.of("grade", AttributeValue.attribute(1)));
InMemoryGraphEdge attendCourse = new InMemoryGraphEdge(student, course, "ac", "attend_course", Map.of("grade", attribute(1)));
InMemoryGraphEdge attendOtherCourse = new InMemoryGraphEdge(student, otherCourse, "ac2", "attend_course", Map.of("grade", attribute(1)));
InMemoryGraphEdge teachCourse = new InMemoryGraphEdge(requestor, course, "tc", "teach_course", Map.of());
return new InMemoryGraph(List.of(requestor, student, course), List.of(attendCourse, attendOtherCourse, teachCourse));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package io.github.danthe1st.arebac.tests.weaving;

import static io.github.danthe1st.arebac.data.commongraph.attributed.AttributeValue.attribute;
import static io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirement.ID_KEY;
import static io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirementOperator.EQUAL;
import static io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirementOperator.LESS_THAN;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import java.util.Map;

import io.github.danthe1st.arebac.data.commongraph.attributed.AttributeValue;
import io.github.danthe1st.arebac.data.graph_pattern.GPEdge;
import io.github.danthe1st.arebac.data.graph_pattern.GPGraph;
import io.github.danthe1st.arebac.data.graph_pattern.GPNode;
import io.github.danthe1st.arebac.data.graph_pattern.GraphPattern;
import io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirement;
import io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirementOperator;
import io.github.danthe1st.arebac.weaving.Weaving;
import org.junit.jupiter.api.Test;

Expand All @@ -37,8 +39,8 @@ private GraphPattern createRequestPattern() {
return new GraphPattern(
requestGraph,
List.of(),
Map.of(student, List.of(new AttributeRequirement(AttributeRequirement.ID_KEY, AttributeRequirementOperator.EQUAL, AttributeValue.attribute("123")))),
Map.of(attendCourse, List.of(new AttributeRequirement("grade", AttributeRequirementOperator.LESS_THAN, AttributeValue.attribute(5)))),
Map.of(student, List.of(new AttributeRequirement(ID_KEY, EQUAL, attribute("123")))),
Map.of(attendCourse, List.of(new AttributeRequirement("grade", LESS_THAN, attribute(5)))),
List.of(course),
Map.of("student", student, "course", course)
);
Expand All @@ -58,7 +60,7 @@ private GraphPattern createPolicyPattern() {
return new GraphPattern(
policyGraph,
List.of(),
Map.of(requestorNode, List.of(new AttributeRequirement(AttributeRequirement.ID_KEY, AttributeRequirementOperator.EQUAL, AttributeValue.attribute("123")))),
Map.of(requestorNode, List.of(new AttributeRequirement(ID_KEY, EQUAL, attribute("123")))),
Map.of(),
List.of(),
Map.of("course", courseNode)
Expand All @@ -79,10 +81,10 @@ private GraphPattern createExpectedCombinedPattern() {
graph,
List.of(),
Map.of(
student, List.of(new AttributeRequirement(AttributeRequirement.ID_KEY, AttributeRequirementOperator.EQUAL, AttributeValue.attribute("123"))),
requestor, List.of(new AttributeRequirement(AttributeRequirement.ID_KEY, AttributeRequirementOperator.EQUAL, AttributeValue.attribute("123")))
student, List.of(new AttributeRequirement(ID_KEY, EQUAL, attribute("123"))),
requestor, List.of(new AttributeRequirement(ID_KEY, EQUAL, attribute("123")))
),
Map.of(attendCourse, List.of(new AttributeRequirement("grade", AttributeRequirementOperator.LESS_THAN, AttributeValue.attribute(5)))),
Map.of(attendCourse, List.of(new AttributeRequirement("grade", LESS_THAN, attribute(5)))),
List.of(course),
Map.of("student", student, "course", course)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.github.danthe1st.arebac.neo4j.tests;

import static io.github.danthe1st.arebac.data.commongraph.attributed.AttributeValue.attribute;
import static io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirement.ID_KEY;
import static io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirementOperator.EQUAL;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
Expand All @@ -14,13 +17,11 @@
import java.util.Map;
import java.util.Set;

import io.github.danthe1st.arebac.data.commongraph.attributed.AttributeValue;
import io.github.danthe1st.arebac.data.graph_pattern.GPEdge;
import io.github.danthe1st.arebac.data.graph_pattern.GPGraph;
import io.github.danthe1st.arebac.data.graph_pattern.GPNode;
import io.github.danthe1st.arebac.data.graph_pattern.GraphPattern;
import io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirement;
import io.github.danthe1st.arebac.data.graph_pattern.constraints.AttributeRequirementOperator;
import io.github.danthe1st.arebac.data.graph_pattern.constraints.MutualExclusionConstraint;
import io.github.danthe1st.arebac.data.memory.InMemoryGraph;
import io.github.danthe1st.arebac.data.memory.InMemoryGraphEdge;
Expand Down Expand Up @@ -78,7 +79,7 @@ private GraphPattern createFindAnswersPattern(String requestorId) {
List.of(new GPEdge(requestor, answer, null, Neo4JSetup.RelType.PROVIDED.name()))
),
List.of(),
Map.of(requestor, List.of(new AttributeRequirement(AttributeRequirement.ID_KEY, AttributeRequirementOperator.EQUAL, AttributeValue.attribute(requestorId)))),
Map.of(requestor, List.of(new AttributeRequirement(ID_KEY, EQUAL, attribute(requestorId)))),
Map.of(),
List.of(answer), Map.of("requestor", requestor, "answer", answer)
);
Expand Down Expand Up @@ -231,7 +232,7 @@ private GraphPattern createCommentsToSameQuestionInTagPattern(String tagId) {
new MutualExclusionConstraint(question1, question2)
),
Map.of(
tag, List.of(new AttributeRequirement(AttributeRequirement.ID_KEY, AttributeRequirementOperator.EQUAL, AttributeValue.attribute(tagId)))
tag, List.of(new AttributeRequirement(ID_KEY, EQUAL, attribute(tagId)))
),
Map.of(),
List.of(user1Comment1, user2Comment1, user1Comment2, user2Comment2),
Expand Down

0 comments on commit 28ecb85

Please sign in to comment.