From 11644b6e8cf9fe3620fdebc858580de505aa0998 Mon Sep 17 00:00:00 2001 From: feger <marc.feger@hhu.de> Date: Tue, 11 Jun 2019 21:10:25 +0200 Subject: [PATCH] Add graph structure --- src/app.js | 17 +++++++++++------ src/js/graph/edge.js | 10 ++++++++++ src/js/graph/graph.js | 42 ++++++++++++++++++++++++++++++++++++++++++ src/js/graph/node.js | 8 ++++++++ src/js/neo4j/neo.js | 42 ++++++++++++++++++++++++++++++++++++++++++ src/neo.js | 35 ----------------------------------- 6 files changed, 113 insertions(+), 41 deletions(-) create mode 100644 src/js/graph/edge.js create mode 100644 src/js/graph/graph.js create mode 100644 src/js/graph/node.js create mode 100644 src/js/neo4j/neo.js delete mode 100644 src/neo.js diff --git a/src/app.js b/src/app.js index 2ea43e8..cddfe22 100644 --- a/src/app.js +++ b/src/app.js @@ -1,7 +1,12 @@ -import './neo' +import {Graph} from "./js/graph/graph"; +import {Edge} from "./js/graph/edge"; +import {Node} from "./js/graph/node"; +import {Neo4JAdapter} from "./js/neo4j/neo" -// TODO: Dockerize and add a Neo4j-Server -// TODO: Why are there so many results? Improve the loops -// TODO: Lift the nodes and relations into a good representation -// TODO: Add a class for the data -// TODO: Lift the data in a graph and visualize them \ No newline at end of file +var graph = new Graph(); + +var neo = new Neo4JAdapter('neo4j', 'W7uFSy$ywR3M3ck'); + +graph = neo.getGraph(); + +console.log(graph); \ No newline at end of file diff --git a/src/js/graph/edge.js b/src/js/graph/edge.js new file mode 100644 index 0000000..c6911f0 --- /dev/null +++ b/src/js/graph/edge.js @@ -0,0 +1,10 @@ +export class Edge { + + constructor(id, properties, type, start, end) { + this.id = id; + this.properties = properties; + this.type = type; + this.start = start; + this.end = end; + } +} \ No newline at end of file diff --git a/src/js/graph/graph.js b/src/js/graph/graph.js new file mode 100644 index 0000000..af8dde9 --- /dev/null +++ b/src/js/graph/graph.js @@ -0,0 +1,42 @@ +import {Edge} from './edge' + +export class Graph { + + constructor() { + this.edges = {}; + this.nodes = {}; + } + + addEdge(edge) { + if (!(edge.id in this.edges)) { + this.edges[edge.id] = edge; + console.log("Added: " + JSON.stringify(edge) + " to edges."); + } + }; + + addNode(node) { + if (!(node.id in this.nodes)) { + this.nodes[node.id] = node; + console.log("Added: " + JSON.stringify(node) + " to nodes."); + } + }; + + _asList(dict) { + var list = []; + for (var key in dict) { + if (dict.hasOwnProperty(key)) { + list.push(dict[key]); + } + } + return list; + } + + get edgesAsList() { + return this._asList(this.edges); + } + + get nodesAsList() { + return this._asList(this.nodes); + } + +} \ No newline at end of file diff --git a/src/js/graph/node.js b/src/js/graph/node.js new file mode 100644 index 0000000..30f0ed5 --- /dev/null +++ b/src/js/graph/node.js @@ -0,0 +1,8 @@ +export class Node { + + constructor(id, properties, labels) { + this.id = id; + this.properties = properties; + this.labels = labels + } +} \ No newline at end of file diff --git a/src/js/neo4j/neo.js b/src/js/neo4j/neo.js new file mode 100644 index 0000000..995c7d9 --- /dev/null +++ b/src/js/neo4j/neo.js @@ -0,0 +1,42 @@ +import {v1 as neo4j} from 'neo4j-driver' +import {Edge} from '../graph/edge' +import {Node} from '../graph/node' +import {Graph} from '../graph/graph' + +export class Neo4JAdapter { + constructor(username, password) { + this.username = username; + this.password = password; + this.authToken = neo4j.auth.basic(this.username, this.password); + } + + getGraph(graph) { + + const driver = neo4j.driver('bolt://localhost:7687', this.authToken, { + encrypted: false + }); + + const session = driver.session(); + + + const resultPromise = session.run( + 'match (a:User)<-[r]-() return *' + ); + + var graph = new Graph(); + resultPromise.then(result => { + session.close(); + result.records.forEach(function (element) { + element.forEach(function (subelement) { + if (subelement instanceof neo4j.types.Node) { + graph.addNode(new Node(subelement.identity.toInt(), subelement.properties, subelement.labels)); + } else if (subelement instanceof neo4j.types.Relationship) { + graph.addEdge(new Edge(subelement.identity.toInt(), subelement.properties, subelement.type, subelement.start.toInt(), subelement.end.toInt())); + } + }); + }); + driver.close(); + }); + return graph + } +} diff --git a/src/neo.js b/src/neo.js deleted file mode 100644 index 5902d48..0000000 --- a/src/neo.js +++ /dev/null @@ -1,35 +0,0 @@ -import {v1 as neo4j} from 'neo4j-driver' - -const authToken = neo4j.auth.basic('neo4j', 'W7uFSy$ywR3M3ck'); -console.log(authToken); - -const driver = neo4j.driver('bolt://localhost:7687', authToken, { - encrypted: false -}); -const session = driver.session(); - -const personName = 'Marc'; -const resultPromise = session.run( - 'match (a:Person)-[r:IS_FRIEND]->(b:Person) RETURN a,b,r', - {name: personName} -); - -resultPromise.then(result => { - session.close(); - result.records.forEach(function (element) { - element.forEach(function (subelement) { - console.log("++++++ " + subelement.constructor.name + " ++++++"); - if (subelement instanceof neo4j.types.Node) { - console.log("Properties: " + JSON.stringify(subelement.properties)); - console.log("ID: " + subelement.identity.toInt()); - console.log("Labels: " + subelement.labels); - } else if (subelement instanceof neo4j.types.Relationship) { - console.log("Type: " + subelement.type); - console.log("Properties: " + JSON.stringify(subelement.properties)); - console.log("Start: " + subelement.start.toInt()); - console.log("End: " + subelement.end.toInt()); - } - }); - }); - driver.close(); -}); -- GitLab