|
|
import json
|
|
|
from Neo4j.Neo4jExecutor import *
|
|
|
|
|
|
# Neo4j连接配置
|
|
|
graph = Graph(NEO4J_URI, auth=NEO4J_AUTH)
|
|
|
|
|
|
|
|
|
class Py2neoLoader:
|
|
|
def __init__(self):
|
|
|
self.node_cache = {}
|
|
|
self.tx = graph.begin() # 初始化事务
|
|
|
|
|
|
def create_literacy_nodes(self, data):
|
|
|
try:
|
|
|
print("Received data structure keys:", data.keys())
|
|
|
|
|
|
# 创建核心素养节点
|
|
|
if "tree" in data and len(data["tree"]) > 0:
|
|
|
self._create_core_node(data["tree"][0])
|
|
|
else:
|
|
|
raise ValueError("Invalid data structure: missing 'tree' array")
|
|
|
|
|
|
# 创建关联体系节点(处理第二个节点)
|
|
|
if len(data["tree"]) > 1:
|
|
|
self._create_relation_system(data["tree"][1])
|
|
|
graph.commit(self.tx) # 替换 self.tx.commit()
|
|
|
print(f"数据导入成功!共处理{len(self.node_cache)}个节点")
|
|
|
except Exception as e:
|
|
|
graph.rollback(self.tx) # 替换 self.tx.rollback()
|
|
|
print("主流程错误:", str(e))
|
|
|
raise
|
|
|
|
|
|
def _create_core_node(self, node):
|
|
|
try:
|
|
|
core_node = Node("CoreLiteracy",
|
|
|
title=node.get("title", "未命名节点"),
|
|
|
value=node.get("value", "CORE_DEFAULT"),
|
|
|
key=node.get("key", "ROOT_DEFAULT"))
|
|
|
self.tx.merge(core_node, "CoreLiteracy", "value")
|
|
|
self.node_cache[node.get("value")] = core_node
|
|
|
print(f"创建核心节点: {core_node}")
|
|
|
|
|
|
if "children" in node:
|
|
|
for child in node["children"]:
|
|
|
self._create_child_node(child, core_node)
|
|
|
except Exception as e:
|
|
|
print(f"创建核心节点出错: {str(e)}")
|
|
|
print("问题节点数据:", node)
|
|
|
raise
|
|
|
|
|
|
def _create_child_node(self, node, parent):
|
|
|
try:
|
|
|
if "value" not in node:
|
|
|
node["value"] = f"AUTO_VALUE_{len(self.node_cache) + 1}"
|
|
|
print(f"为节点生成默认value: {node['value']}")
|
|
|
|
|
|
child_node = Node("LiteracyNode",
|
|
|
title=node.get("title", "未命名子节点"),
|
|
|
value=node["value"],
|
|
|
key=node.get("key", ""),
|
|
|
parentValue=node.get("parentValue", ""),
|
|
|
preSkills=node.get("preSkills", []))
|
|
|
self.tx.merge(child_node, "LiteracyNode", "value")
|
|
|
self.node_cache[node["value"]] = child_node
|
|
|
|
|
|
if parent is not None:
|
|
|
rel = Relationship(parent, "HAS_SUB_NODE", child_node)
|
|
|
self.tx.merge(rel)
|
|
|
print(f"创建关系: {parent['value']} -> {child_node['value']}")
|
|
|
|
|
|
# 处理前置技能关系
|
|
|
for pre_skill in node.get("preSkills", []):
|
|
|
if pre_skill in self.node_cache:
|
|
|
pre_node = self.node_cache[pre_skill]
|
|
|
rel = Relationship(pre_node, "REQUIRES", child_node)
|
|
|
self.tx.merge(rel)
|
|
|
else:
|
|
|
print(f"警告: 前置技能节点 {pre_skill} 不存在")
|
|
|
|
|
|
# 递归处理子节点
|
|
|
if "children" in node:
|
|
|
for sub_child in node.get("children", []):
|
|
|
self._create_child_node(sub_child, child_node)
|
|
|
except Exception as e:
|
|
|
print(f"创建子节点出错: {str(e)}")
|
|
|
print("问题节点数据:", node)
|
|
|
raise
|
|
|
|
|
|
def _create_relation_system(self, relation_system):
|
|
|
try:
|
|
|
# 创建关联体系根节点
|
|
|
sys_node = Node("RelationSystem",
|
|
|
title=relation_system.get("title", "未命名关联体系"),
|
|
|
value=relation_system.get("value", "REL_SYS_DEFAULT"),
|
|
|
path=relation_system.get("path", []))
|
|
|
self.tx.merge(sys_node, "RelationSystem", "value")
|
|
|
print(f"创建关联体系节点: {sys_node}")
|
|
|
|
|
|
# 处理关联链
|
|
|
for chain in relation_system.get("children", []):
|
|
|
chain_node = Node("RelationChain",
|
|
|
title=chain.get("title", "未命名关联链"),
|
|
|
value=chain.get("value", f"CHAIN_{len(self.node_cache) + 1}"),
|
|
|
path=chain.get("path", []))
|
|
|
self.tx.merge(chain_node, "RelationChain", "value")
|
|
|
self.tx.merge(Relationship(sys_node, "HAS_CHAIN", chain_node))
|
|
|
|
|
|
# 处理路径关系
|
|
|
for path_str in chain.get("path", []):
|
|
|
nodes = [n.strip() for n in path_str.replace("+", "→").split("→")]
|
|
|
for i in range(len(nodes) - 1):
|
|
|
source = self.node_cache.get(nodes[i])
|
|
|
target = self.node_cache.get(nodes[i + 1])
|
|
|
if source and target:
|
|
|
rel = Relationship(source, "NEXT_STEP", target)
|
|
|
self.tx.merge(rel)
|
|
|
else:
|
|
|
print(f"路径节点缺失: {nodes[i]}→{nodes[i + 1]}")
|
|
|
except Exception as e:
|
|
|
print(f"创建关联体系出错: {str(e)}")
|
|
|
print("问题数据:", relation_system)
|
|
|
raise
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
try:
|
|
|
with open("小学数学学科素养点体系.json", 'r', encoding='utf-8') as f:
|
|
|
raw_data = json.load(f)
|
|
|
print("JSON文件加载成功,顶层键:", raw_data.keys())
|
|
|
|
|
|
if "data" not in raw_data:
|
|
|
raise ValueError("JSON缺少'data'字段")
|
|
|
|
|
|
loader = Py2neoLoader()
|
|
|
loader.create_literacy_nodes(raw_data["data"])
|
|
|
except Exception as e:
|
|
|
print("主程序错误:", str(e)) |