You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.9 KiB
92 lines
2.9 KiB
5 months ago
|
# K2_Neo4jExecutor.py
|
||
|
from py2neo import Graph
|
||
|
import re
|
||
5 months ago
|
from Util import *
|
||
5 months ago
|
from Config import *
|
||
5 months ago
|
|
||
5 months ago
|
|
||
5 months ago
|
class K2_Neo4jExecutor:
|
||
5 months ago
|
def __init__(self, uri, auth):
|
||
|
self.graph = Graph(uri, auth=auth)
|
||
|
|
||
5 months ago
|
# 新增文本执行方法
|
||
|
def execute_cypher_text(self, cypher_text: str) -> dict:
|
||
|
"""直接执行Cypher文本"""
|
||
|
stats = {'total': 0, 'success': 0, 'failed': 0}
|
||
|
try:
|
||
|
statements = re.split(r';\s*\n', cypher_text)
|
||
|
statements = [s.strip() for s in statements if s.strip()]
|
||
|
|
||
|
stats['total'] = len(statements)
|
||
|
|
||
|
for stmt in statements:
|
||
|
try:
|
||
|
self.graph.run(stmt)
|
||
|
stats['success'] += 1
|
||
|
except Exception as e:
|
||
|
stats['failed'] += 1
|
||
|
print(f"执行失败: {stmt[:50]}... \n错误: {str(e)[:100]}")
|
||
|
|
||
|
return stats
|
||
|
except Exception as e:
|
||
5 months ago
|
print(f"执行失败: {stmt[:100]}... \n完整错误: {str(e)}") # 原为str(e)[:100]
|
||
5 months ago
|
return stats
|
||
|
|
||
5 months ago
|
def execute_cypher_file(self, file_path: str) -> dict: # 确保方法名称正确
|
||
|
"""执行Cypher文件"""
|
||
|
stats = {'total': 0, 'success': 0, 'failed': 0}
|
||
5 months ago
|
try:
|
||
5 months ago
|
with open(file_path, 'r', encoding='utf-8') as f:
|
||
|
cypher_script = f.read()
|
||
|
statements = re.split(r';\s*\n', cypher_script)
|
||
|
statements = [s.strip() for s in statements if s.strip()]
|
||
5 months ago
|
|
||
5 months ago
|
stats['total'] = len(statements)
|
||
5 months ago
|
|
||
5 months ago
|
for stmt in statements:
|
||
|
try:
|
||
|
self.graph.run(stmt)
|
||
|
stats['success'] += 1
|
||
|
except Exception as e:
|
||
|
stats['failed'] += 1
|
||
|
print(f"执行失败: {stmt[:50]}... \n错误: {str(e)[:100]}")
|
||
5 months ago
|
|
||
5 months ago
|
return stats
|
||
5 months ago
|
|
||
|
except Exception as e:
|
||
5 months ago
|
print(f"文件错误: {str(e)}")
|
||
|
return stats
|
||
5 months ago
|
|
||
|
|
||
5 months ago
|
def init():
|
||
5 months ago
|
executor = K2_Neo4jExecutor(
|
||
5 months ago
|
uri=NEO4J_URI,
|
||
|
auth=NEO4J_AUTH
|
||
5 months ago
|
)
|
||
5 months ago
|
# 清库
|
||
|
clear(executor.graph)
|
||
5 months ago
|
|
||
5 months ago
|
# 确保调用名称与方法定义一致
|
||
5 months ago
|
# f="knowledge_graph.cypher"
|
||
|
f = "小学数学一至三年级.cypher"
|
||
|
result = executor.execute_cypher_file(f)
|
||
|
print(f"\n执行结果:")
|
||
|
print(f"- 总语句数: {result.get('total', 0)}")
|
||
|
print(f"- 成功: {result.get('success', 0)}")
|
||
|
print(f"- 失败: {result.get('failed', 0)}")
|
||
5 months ago
|
|
||
5 months ago
|
f = "小学数学四至六年级.cypher"
|
||
|
result = executor.execute_cypher_file(f)
|
||
5 months ago
|
print(f"\n执行结果:")
|
||
5 months ago
|
print(f"- 总语句数: {result.get('total', 0)}")
|
||
|
print(f"- 成功: {result.get('success', 0)}")
|
||
5 months ago
|
print(f"- 失败: {result.get('failed', 0)}")
|
||
|
|
||
|
# 执行优化
|
||
|
f = "优化.cypher"
|
||
|
executor.execute_cypher_file(f)
|
||
|
|
||
5 months ago
|
|
||
|
if __name__ == '__main__':
|
||
|
init()
|