diff --git a/AI/Neo4j/N1_ReadZsd.py b/AI/Neo4j/N1_ReadZsd.py index a657d6e7..fb1b9ed1 100644 --- a/AI/Neo4j/N1_ReadZsd.py +++ b/AI/Neo4j/N1_ReadZsd.py @@ -1,8 +1,6 @@ import json from Neo4j.Neo4jExecutor import * -from Neo4j.Util import clear - def json_to_cypher(data): """将知识体系JSON转换为Cypher插入脚本""" diff --git a/AI/Neo4j/Neo4jExecutor.py b/AI/Neo4j/Neo4jExecutor.py index a9bb1ff6..65450327 100644 --- a/AI/Neo4j/Neo4jExecutor.py +++ b/AI/Neo4j/Neo4jExecutor.py @@ -1,7 +1,49 @@ import re -from Util import * from Config import * +from py2neo import Graph, Node, Relationship, Subgraph +from typing import List, Tuple +def clear(db): + # 清空数据 + db.run("MATCH (n) DETACH DELETE n") + + # 分步删除约束和索引 + try: + # 删除约束 + constraints = db.run("SHOW CONSTRAINTS YIELD name").data() + for constr in constraints: + db.run(f"DROP CONSTRAINT `{constr['name']}`") + + # 删除索引 + indexes = db.run("SHOW INDEXES YIELD name, type WHERE type <> 'LOOKUP'").data() + for idx in indexes: + db.run(f"DROP INDEX `{idx['name']}`") + except Exception as e: + print(f"删除操作失败: {e}") + + +def create_subgraph(db: Graph, nodes: List[Node], relations: List[Tuple[Node, str, Node]]) -> None: + """统一创建子图""" + subgraph = Subgraph( + nodes=nodes, + relationships=[Relationship(start, rel_type, end) for start, rel_type, end in relations] + ) + db.create(subgraph) + +def tx_create(db: Graph, nodes: List[Node], relations: List[Tuple[Node, str, Node]]) -> None: + """事务方式创建数据""" + try: + tx = db.begin() + subgraph = Subgraph( + nodes=nodes, + relationships=[Relationship(start, rel_type, end) for start, rel_type, end in relations] + ) + tx.create(subgraph) + db.commit(tx) + except Exception as e: + db.rollback(tx) + print(f"事务操作失败: {str(e)}") + raise class Neo4jExecutor: def __init__(self, uri, auth): diff --git a/AI/Neo4j/Util.py b/AI/Neo4j/Util.py deleted file mode 100644 index 9dc1062c..00000000 --- a/AI/Neo4j/Util.py +++ /dev/null @@ -1,48 +0,0 @@ -from py2neo import Graph, Node, Relationship, Subgraph -from typing import List, Tuple - -# 修正版本查询代码 -#version = db.run("CALL dbms.components() YIELD versions UNWIND versions AS version RETURN version").evaluate() -#print(f"Neo4j 版本: {version}") - -def clear(db): - # 清空数据 - db.run("MATCH (n) DETACH DELETE n") - - # 分步删除约束和索引 - try: - # 删除约束 - constraints = db.run("SHOW CONSTRAINTS YIELD name").data() - for constr in constraints: - db.run(f"DROP CONSTRAINT `{constr['name']}`") - - # 删除索引 - indexes = db.run("SHOW INDEXES YIELD name, type WHERE type <> 'LOOKUP'").data() - for idx in indexes: - db.run(f"DROP INDEX `{idx['name']}`") - except Exception as e: - print(f"删除操作失败: {e}") - - -def create_subgraph(db: Graph, nodes: List[Node], relations: List[Tuple[Node, str, Node]]) -> None: - """统一创建子图""" - subgraph = Subgraph( - nodes=nodes, - relationships=[Relationship(start, rel_type, end) for start, rel_type, end in relations] - ) - db.create(subgraph) - -def tx_create(db: Graph, nodes: List[Node], relations: List[Tuple[Node, str, Node]]) -> None: - """事务方式创建数据""" - try: - tx = db.begin() - subgraph = Subgraph( - nodes=nodes, - relationships=[Relationship(start, rel_type, end) for start, rel_type, end in relations] - ) - tx.create(subgraph) - db.commit(tx) - except Exception as e: - db.rollback(tx) - print(f"事务操作失败: {str(e)}") - raise \ No newline at end of file diff --git a/AI/Neo4j/__pycache__/Neo4jExecutor.cpython-310.pyc b/AI/Neo4j/__pycache__/Neo4jExecutor.cpython-310.pyc index e97b71ea..c73869a8 100644 Binary files a/AI/Neo4j/__pycache__/Neo4jExecutor.cpython-310.pyc and b/AI/Neo4j/__pycache__/Neo4jExecutor.cpython-310.pyc differ diff --git a/AI/Neo4j/__pycache__/Util.cpython-310.pyc b/AI/Neo4j/__pycache__/Util.cpython-310.pyc deleted file mode 100644 index fe0bb290..00000000 Binary files a/AI/Neo4j/__pycache__/Util.cpython-310.pyc and /dev/null differ