Skip to content

Oracle of Bacon - DENOUAL Audrey & Venceslas ROULLIER #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions oracle-of-bacon-backend/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Audrey DENOUAL & Venceslas ROULLIER

# Oracle of Bacon
This application is an Oracle of Bacon implementation based on NoSQL data stores :
* ElasticSearch (http) - localhost:9200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.serli.oracle.of.bacon.repository.ElasticSearchRepository;
import com.serli.oracle.of.bacon.repository.Neo4JRepository;
import com.serli.oracle.of.bacon.repository.Neo4JRepository.GraphItem;
import com.serli.oracle.of.bacon.repository.RedisRepository;
import net.codestory.http.annotations.Get;

Expand All @@ -22,47 +23,14 @@ public APIEndPoint() {
}

@Get("bacon-to?actor=:actorName")
// TODO change return type
public String getConnectionsToKevinBacon(String actorName) {
return "[\n" +
"{\n" +
"\"data\": {\n" +
"\"id\": 85449,\n" +
"\"type\": \"Actor\",\n" +
"\"value\": \"Bacon, Kevin (I)\"\n" +
"}\n" +
"},\n" +
"{\n" +
"\"data\": {\n" +
"\"id\": 2278636,\n" +
"\"type\": \"Movie\",\n" +
"\"value\": \"Mystic River (2003)\"\n" +
"}\n" +
"},\n" +
"{\n" +
"\"data\": {\n" +
"\"id\": 1394181,\n" +
"\"type\": \"Actor\",\n" +
"\"value\": \"Robbins, Tim (I)\"\n" +
"}\n" +
"},\n" +
"{\n" +
"\"data\": {\n" +
"\"id\": 579848,\n" +
"\"source\": 85449,\n" +
"\"target\": 2278636,\n" +
"\"value\": \"PLAYED_IN\"\n" +
"}\n" +
"},\n" +
"{\n" +
"\"data\": {\n" +
"\"id\": 9985692,\n" +
"\"source\": 1394181,\n" +
"\"target\": 2278636,\n" +
"\"value\": \"PLAYED_IN\"\n" +
"}\n" +
"}\n" +
"]";
List<Map<String, GraphItem>> graph = neo4JRepository.getConnectionsToKevinBacon(actorName);
String result = Arrays.toString(
graph.stream()
.map(map -> String.format("{\"data\": %s}", map.entrySet().iterator().next()
.getValue().toString()))
.toArray());
return result;
}

@Get("suggest?q=:searchQuery")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static void main(String[] args) throws IOException, InterruptedException
bufferedReader
.lines()
.forEach(line -> {
// TODO
// TODO
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,72 @@
package com.serli.oracle.of.bacon.repository;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Session;
import org.neo4j.driver.types.Node;
import org.neo4j.driver.types.Relationship;
import org.neo4j.driver.types.Path;
import org.neo4j.driver.Result;
import org.neo4j.driver.Value;
import org.neo4j.driver.Transaction;
import org.neo4j.driver.TransactionWork;

import static org.neo4j.driver.Values.parameters;

public class Neo4JRepository {
private final Driver driver;

public Neo4JRepository() {
this.driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "password"));
this.driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "pandora-agent-twist-ariel-critic-7077"));
}

public List<Map<String, GraphItem>> getConnectionsToKevinBacon(String actorName) {
Session session = driver.session();

// TODO
return null;
List<Map<String, GraphItem>> result = session.writeTransaction(new TransactionWork<List<Map<String, GraphItem>>>() {
@Override
public List<Map<String, GraphItem>> execute(Transaction tx) {
Result result = tx.run("MATCH p=shortestPath(" +
"(bacon:Actor {name:'Bacon, Kevin (I)'})-[*]-" +
"(other:Actor {name:$actorName})" +
")" +
"RETURN p",
parameters("actorName", actorName));
Path path = result.single().get("p").asPath();

//Final result
List<Map<String, GraphItem>> list = new ArrayList<Map<String, GraphItem>>();

//Get movies and actors
for (Node node : path.nodes()) {
Map<String, GraphItem> map = new HashMap<String, GraphItem>();
map.put(String.valueOf(node.id()), mapNodeToGraphNode(node));
list.add(map);
}

//Get relationships
for (Relationship relation : path.relationships()) {
Map<String, GraphItem> map = new HashMap<String, GraphItem>();
map.put(String.valueOf(relation.id()), mapRelationShipToGraphEdge(relation));
list.add(map);
}

return list;
}
});
return result;
}

private GraphEdge mapRelationShipToNodeEdge(Relationship relationship) {
private GraphEdge mapRelationShipToGraphEdge(Relationship relationship) {
return new GraphEdge(relationship.id(), relationship.startNodeId(), relationship.endNodeId(), relationship.type());
}

private GraphNode mapNodeToGrapNode(Node node) {
private GraphNode mapNodeToGraphNode(Node node) {
String type = node.labels().iterator().next();
String value = null;
if (!node.get("name").isNull()) {
Expand Down Expand Up @@ -72,6 +110,11 @@ public GraphNode(long id, String value, String type) {
this.value = value;
this.type = type;
}

@Override
public String toString() {
return String.format("{ \"id\": \"%s\", \"value\": \"%s\", \"type\": \"%s\" }", String.valueOf(this.id), this.value, this.type);
}
}

private static class GraphEdge extends GraphItem {
Expand All @@ -85,5 +128,10 @@ public GraphEdge(long id, long source, long target, String value) {
this.target = target;
this.value = value;
}

@Override
public String toString() {
return String.format("{ \"id\": \"%s\", \"source\": \"%s\", \"target\": \"%s\", \"value\": \"%s\" }", String.valueOf(this.id), this.source, this.target, this.value);
}
}
}
5 changes: 1 addition & 4 deletions oracle-of-bacon-frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.