# knowledge_graph.py import json from typing import Dict, List, Optional from graphviz import Digraph class QuantumKnowledgeGraph: """ Represents a dynamic, multi-modal knowledge graph. """ def __init__(self): self.nodes: Dict[int, Dict[str, any]] = {} self.relations: List[Dict[str, any]] = [] self.node_counter = 0 def create_node(self, content: Dict, node_type: str) -> int: self.node_counter += 1 self.nodes[self.node_counter] = { "id": self.node_counter, "content": content, "type": node_type, "connections": [] } return self.node_counter def create_relation(self, source: int, target: int, rel_type: str, strength: float = 1.0) -> None: self.relations.append({ "source": source, "target": target, "type": rel_type, "strength": strength }) self.nodes[source]["connections"].append(target) def visualize_graph(self, focus_node: Optional[int] = None) -> str: dot = Digraph(engine="neato") for nid, node in self.nodes.items(): label = f"{node['type']}\n{self._truncate_content(node['content'])}" dot.node(str(nid), label) for rel in self.relations: dot.edge(str(rel["source"]), str(rel["target"]), label=rel["type"]) if focus_node: dot.node(str(focus_node), color="red", style="filled") return dot.source def _truncate_content(self, content: Dict) -> str: return json.dumps(content)[:50] + "..."