Skip to content

Commit

Permalink
Inital Hack of NEO4j DB creation
Browse files Browse the repository at this point in the history
  • Loading branch information
john681611 committed Aug 2, 2023
1 parent b2f02f6 commit 06aa35d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
47 changes: 47 additions & 0 deletions application/database/db.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from neo4j import GraphDatabase
from sqlalchemy.orm import aliased
import os
import logging
Expand Down Expand Up @@ -179,6 +180,13 @@ def add_node(self, *args, **kwargs):
@classmethod
def add_cre(cls, dbcre: CRE, graph: nx.DiGraph) -> nx.DiGraph:
if dbcre:
Neo4j_driver.execute_query(
"MERGE (n:CRE {id: $nid, name: $name, description: $description, external_id: $external_id})",
nid=dbcre.id,
name=dbcre.name,
description=dbcre.description,
external_id=dbcre.external_id,
database_="neo4j")
graph.add_node(
f"CRE: {dbcre.id}", internal_id=dbcre.id, external_id=dbcre.external_id
)
Expand All @@ -189,6 +197,21 @@ def add_cre(cls, dbcre: CRE, graph: nx.DiGraph) -> nx.DiGraph:
@classmethod
def add_dbnode(cls, dbnode: Node, graph: nx.DiGraph) -> nx.DiGraph:
if dbnode:
Neo4j_driver.execute_query(
"MERGE (n:Node {id: $nid, name: $name, section: $section, section_id: $section_id, subsection: $subsection, tags: $tags, version: $version, description: $description, ntype: $ntype})",
nid=dbnode.id,
name=dbnode.name,
section=dbnode.section,
section_id=dbnode.section_id,
subsection=dbnode.subsection or "",
tags=dbnode.tags,
version=dbnode.version or "",
description=dbnode.description,
ntype=dbnode.ntype,
database_="neo4j")

# coma separated tags

graph.add_node(
"Node: " + str(dbnode.id),
internal_id=dbnode.id,
Expand All @@ -215,6 +238,16 @@ def load_cre_graph(cls, session) -> nx.Graph:
graph = cls.add_cre(dbcre=cre, graph=graph)

graph.add_edge(f"CRE: {il.group}", f"CRE: {il.cre}", ltype=il.type)
Neo4j_driver.execute_query(
"MATCH (a:CRE), (b:CRE) "
"WHERE a.id = $aID AND b.id = $bID "
"CALL apoc.create.relationship(a,$relType, {},b) "
"YIELD rel "
"RETURN rel",
aID=il.group,
bID=il.cre,
relType=str.upper(il.type).replace(' ', '_'),
database_="neo4j")

for lnk in session.query(Links).all():
node = session.query(Node).filter(Node.id == lnk.node).first()
Expand All @@ -226,6 +259,16 @@ def load_cre_graph(cls, session) -> nx.Graph:
graph = cls.add_cre(dbcre=cre, graph=graph)

graph.add_edge(f"CRE: {lnk.cre}", f"Node: {str(lnk.node)}", ltype=lnk.type)
Neo4j_driver.execute_query(
"MATCH (a:CRE), (b:Node) "
"WHERE a.id = $aID AND b.id = $bID "
"CALL apoc.create.relationship(a,$relType, {},b) "
"YIELD rel "
"RETURN rel",
aID=lnk.cre,
bID=lnk.node,
relType=str.upper(lnk.type).replace(' ', '_'),
database_="neo4j")
return graph


Expand Down Expand Up @@ -1475,3 +1518,7 @@ def dbCREfromCRE(cre: cre_defs.CRE) -> CRE:
external_id=cre.id,
tags=",".join(tags),
)

URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "password")
Neo4j_driver = GraphDatabase.driver(URI, auth=AUTH)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ networkx==2.5.1
nltk==3.8.1
nose==1.3.7
numpy==1.23.0
neo4j==5.11.0
oauthlib==3.1.0
openai==0.27.6
openapi-schema-validator==0.3.4
Expand Down

0 comments on commit 06aa35d

Please sign in to comment.