-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.github.danthe1st</groupId> | ||
<artifactId>AReBAC</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>arebac-neo4j</artifactId> | ||
<name>AReBAC-Neo4J</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.github.danthe1st</groupId> | ||
<artifactId>arebac-core</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.neo4j</groupId> | ||
<artifactId>neo4j</artifactId> | ||
<version>5.22.0</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
47 changes: 47 additions & 0 deletions
47
arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jDB.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Neo4jNode, Neo4jEdge> { | ||
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<Neo4jEdge> findOutgoingEdges(Neo4jNode node) { | ||
return findEdges(node, Direction.OUTGOING); | ||
} | ||
|
||
@Override | ||
public Collection<Neo4jEdge> findIncomingEdges(Neo4jNode node) { | ||
return findEdges(node, Direction.INCOMING); | ||
} | ||
|
||
private Collection<Neo4jEdge> findEdges(Neo4jNode node, Direction direction) { | ||
Node internalNode = node.getDBNode(); | ||
ResourceIterable<Relationship> relationships = internalNode.getRelationships(direction); | ||
List<Neo4jEdge> edges = new ArrayList<>(); | ||
for(Relationship relationship : relationships){ | ||
edges.add(new Neo4jEdge(relationship)); | ||
} | ||
return Collections.unmodifiableList(edges); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jEdge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Neo4jNode> { | ||
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; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Label> labelIt = node.getLabels().iterator(); | ||
if(labelIt.hasNext()){ | ||
return labelIt.next().name(); | ||
} | ||
return NODE_TYPE; | ||
} | ||
|
||
@Override | ||
public AttributeValue<?> getAttribute(String key) { | ||
if(!node.hasProperty(key)){ | ||
return null; | ||
} | ||
Object property = node.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 Node getDBNode() { | ||
return node; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters