Skip to content

Commit

Permalink
Create Inital Path API response
Browse files Browse the repository at this point in the history
  • Loading branch information
john681611 committed Aug 10, 2023
1 parent 4fe7ab6 commit fe25407
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 25 deletions.
83 changes: 62 additions & 21 deletions application/database/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,48 @@ def link_CRE_to_Node(self, CRE_id, node_id, link_type):
bID=node_id,
relType=str.upper(link_type).replace(' ', '_'),
database_="neo4j")
@classmethod
def gap_analysis(self, name_1, name_2):
if not self.connected:
return
records, _, _ = self.driver.execute_query(
"MATCH"
"(KevinB:Node {name: $name1}), "
"(Al:Node {name: $name2}), "
"p = shortestPath((KevinB)-[*]-(Al)) "
"WHERE length(p) > 1 "
"RETURN p "
"LIMIT 100",
name1=name_1,
name2=name_2,
database_="neo4j"
)

def format_record(rec):
def format_segment(seg):
return {
"start": {
"name": seg.start_node["name"],
"id": seg.start_node["id"]
},
"end": {
"name": seg.end_node["name"],
"id": seg.end_node["id"]
},
"relationship": seg.type
}
return {
"start": {
"name": rec.start_node["name"],
"id": rec.start_node["id"]
},
"end": {
"name": rec.end_node["name"],
"id": rec.end_node["id"]
},
"path": [format_segment(seg) for seg in rec.relationships]
}
return [format_record(rec['p']) for rec in records]

class CRE_Graph:
graph: nx.Graph = None
Expand All @@ -255,7 +296,7 @@ def instance(cls, session, neo_db: NEO_DB):
if cls.__instance is None:
cls.__instance = cls.__new__(cls)
cls.neo_db = neo_db
cls.graph = cls.load_cre_graph(session)
# cls.graph = cls.load_cre_graph(session)
return cls.__instance

def __init__(sel):
Expand Down Expand Up @@ -1158,30 +1199,30 @@ def find_path_between_nodes(

return res

def gap_analysis(self, node_names: List[str]) -> List[cre_defs.Node]:
def gap_analysis(self, node_names: List[str]):
"""Since the CRE structure is a tree-like graph with
leaves being nodes we can find the paths between nodes
find_path_between_nodes() is a graph-path-finding method
"""
processed_nodes = []
dbnodes: List[Node] = []
for name in node_names:
dbnodes.extend(self.session.query(Node).filter(Node.name == name).all())

for node in dbnodes:
working_node = nodeFromDB(node)
for other_node in dbnodes:
if node.id == other_node.id:
continue
if self.find_path_between_nodes(node.id, other_node.id):
working_node.add_link(
cre_defs.Link(
ltype=cre_defs.LinkTypes.LinkedTo,
document=nodeFromDB(other_node),
)
)
processed_nodes.append(working_node)
return processed_nodes
# processed_nodes = []
# dbnodes: List[Node] = []
# for name in node_names:
# dbnodes.extend(self.session.query(Node).filter(Node.name == name).all())

# for node in dbnodes:
# working_node = nodeFromDB(node)
# for other_node in dbnodes:
# if node.id == other_node.id:
# continue
# if self.find_path_between_nodes(node.id, other_node.id):
# working_node.add_link(
# cre_defs.Link(
# ltype=cre_defs.LinkTypes.LinkedTo,
# document=nodeFromDB(other_node),
# )
# )
# processed_nodes.append(working_node)
return self.neo_db.gap_analysis(node_names[0], node_names[1])

def text_search(self, text: str) -> List[Optional[cre_defs.Document]]:
"""Given a piece of text, tries to find the best match
Expand Down
14 changes: 10 additions & 4 deletions application/web/web_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,16 @@ def find_document_by_tag() -> Any:
def gap_analysis() -> Any: # TODO (spyros): add export result to spreadsheet
database = db.Node_collection()
standards = request.args.getlist("standard")
documents = database.gap_analysis(standards=standards)
if documents:
res = [doc.todict() for doc in documents]
return jsonify(res)
paths = database.gap_analysis(standards)
grouped_paths = {}
for path in paths:
key = path['start']['id']
if key not in grouped_paths:
grouped_paths[key] = {"start": path['start'], "paths": []}
del path['start']
grouped_paths[key]['paths'].append(path)

return jsonify(grouped_paths)


@app.route("/rest/v1/text_search", methods=["GET"])
Expand Down

0 comments on commit fe25407

Please sign in to comment.