diff --git a/.gitignore b/.gitignore index b07d3f3..39beda2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +arebac-neo4j/db + # Created by https://www.toptal.com/developers/gitignore/api/eclipse,java,maven # Edit at https://www.toptal.com/developers/gitignore?templates=eclipse,java,maven diff --git a/arebac-neo4j/pom.xml b/arebac-neo4j/pom.xml new file mode 100644 index 0000000..07f56e4 --- /dev/null +++ b/arebac-neo4j/pom.xml @@ -0,0 +1,25 @@ + + 4.0.0 + + io.github.danthe1st + AReBAC + 0.0.1-SNAPSHOT + + arebac-neo4j + AReBAC-Neo4J + + + + io.github.danthe1st + arebac-core + ${project.version} + + + org.neo4j + neo4j + 5.22.0 + + + \ No newline at end of file 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 new file mode 100644 index 0000000..6c16039 --- /dev/null +++ b/arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jDB.java @@ -0,0 +1,47 @@ +package io.github.danthe1st.arebac.neo4j.graph; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import io.github.danthe1st.arebac.data.commongraph.attributed.AttributedGraph; +import org.neo4j.graphdb.Direction; +import org.neo4j.graphdb.Node; +import org.neo4j.graphdb.Relationship; +import org.neo4j.graphdb.ResourceIterable; +import org.neo4j.graphdb.Transaction; + +public class Neo4jDB implements AttributedGraph { + private final Transaction tx; + + public Neo4jDB(Transaction tx) { + this.tx = tx; + } + + @Override + public Neo4jNode findNodeById(String id) { + return new Neo4jNode(tx.getNodeByElementId(id)); + } + + @Override + public Collection findOutgoingEdges(Neo4jNode node) { + return findEdges(node, Direction.OUTGOING); + } + + @Override + public Collection findIncomingEdges(Neo4jNode node) { + return findEdges(node, Direction.INCOMING); + } + + private Collection findEdges(Neo4jNode node, Direction direction) { + Node internalNode = node.getDBNode(); + ResourceIterable relationships = internalNode.getRelationships(direction); + List edges = new ArrayList<>(); + for(Relationship relationship : relationships){ + edges.add(new Neo4jEdge(relationship)); + } + return Collections.unmodifiableList(edges); + } + +} diff --git a/arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jEdge.java b/arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jEdge.java new file mode 100644 index 0000000..a94110c --- /dev/null +++ b/arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jEdge.java @@ -0,0 +1,51 @@ +package io.github.danthe1st.arebac.neo4j.graph; + +import io.github.danthe1st.arebac.data.commongraph.attributed.AttributeValue; +import io.github.danthe1st.arebac.data.commongraph.attributed.AttributedGraphEdge; +import org.neo4j.graphdb.Relationship; + +public class Neo4jEdge implements AttributedGraphEdge { + private final Relationship relationship; + + public Neo4jEdge(Relationship relationship) { + this.relationship = relationship; + } + + @Override + public Neo4jNode source() { + return new Neo4jNode(relationship.getStartNode()); + } + + @Override + public Neo4jNode target() { + return new Neo4jNode(relationship.getEndNode()); + } + + @Override + public String id() { + return relationship.getElementId(); + } + + @Override + public String edgeType() { + return relationship.getType().name(); + } + + @Override + public AttributeValue getAttribute(String key) { + if(!relationship.hasProperty(key)){ + return null; + } + Object property = relationship.getProperty(key); + return switch(property) { + case String s -> AttributeValue.attribute(s); + case Boolean b -> AttributeValue.attribute(b); + case Integer l -> AttributeValue.attribute(l); + default -> throw new UnsupportedOperationException("unknown property type"); + }; + } + + public Relationship getDBEdge() { + return relationship; + } +} diff --git a/arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jNode.java b/arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jNode.java new file mode 100644 index 0000000..3684e85 --- /dev/null +++ b/arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jNode.java @@ -0,0 +1,50 @@ +package io.github.danthe1st.arebac.neo4j.graph; + +import java.util.Iterator; + +import io.github.danthe1st.arebac.data.commongraph.attributed.AttributeValue; +import io.github.danthe1st.arebac.data.commongraph.attributed.AttributedNode; +import org.neo4j.graphdb.Label; +import org.neo4j.graphdb.Node; + +public class Neo4jNode implements AttributedNode { + private static final String NODE_TYPE = "_NO_LABEL"; + + private final Node node; + + public Neo4jNode(Node node) { + this.node = node; + } + + @Override + public String id() { + return node.getElementId(); + } + + @Override + public String nodeType() { + Iterator