{
"event": "PreToolUse",
"tool_name": "Write",
"tool_input": {
"file_path": "\/var\/www\/scripts\/pipeline\/build_hierarchies.py",
"content": "#!\/usr\/bin\/env python3\n\"\"\"\nBuild hierarchies for taxonomy_terms and ontology_classes.\nPhase 2 of semantic infrastructure cleanup.\n\"\"\"\n\nimport os\nimport sys\n\nsys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))\n\nfrom db import db\n\n\ndef build_taxonomy_hierarchy():\n \"\"\"Build taxonomy term hierarchy for systemisches Coaching.\"\"\"\n print(\"Building taxonomy hierarchy...\")\n\n # Hierarchy design for systemisches Coaching:\n # Wissen (12) - Root\n # ├── Theorie (2) - depth 1\n # ├── Praxis (3) - depth 1\n # │ ├── Methoden (1) - depth 2\n # │ └── Prozess (9) - depth 2\n # ├── Kontext (10) - depth 1\n # │ ├── Organisation (5) - depth 2\n # │ └── Kommunikation (4) - depth 2\n # └── Anwendungsfelder - new, depth 1\n # ├── Coaching (7) - depth 2\n # ├── Therapie (8) - depth 2\n # ├── Entwicklung (6) - depth 2\n # └── Haltung (11) - depth 2\n\n ROOT_ID = 12 # Wissen\n\n # First, update root\n db.execute(\n \"UPDATE taxonomy_terms SET depth = 0, path = %s WHERE id = %s\",\n (\"\/wissen\", ROOT_ID)\n )\n\n # Create Anwendungsfelder category\n db.execute(\n \"\"\"INSERT INTO taxonomy_terms (name, slug, parent_id, description, depth, path)\n VALUES (%s, %s, %s, %s, %s, %s)\"\"\",\n (\"Anwendungsfelder\", \"anwendungsfelder\", ROOT_ID,\n \"Praktische Anwendungsbereiche\", 1, \"\/wissen\/anwendungsfelder\")\n )\n db.commit()\n\n # Get Anwendungsfelder ID\n cursor = db.execute(\"SELECT id FROM taxonomy_terms WHERE slug = 'anwendungsfelder'\")\n anwendung_id = cursor.fetchone()[\"id\"]\n cursor.close()\n\n # Define hierarchy updates\n updates = [\n # Level 1 - direct children of Wissen\n (2, ROOT_ID, 1, \"\/wissen\/theorie\"), # Theorie\n (3, ROOT_ID, 1, \"\/wissen\/praxis\"), # Praxis\n (10, ROOT_ID, 1, \"\/wissen\/kontext\"), # Kontext\n\n # Level 2 - children of Praxis\n (1, 3, 2, \"\/wissen\/praxis\/methoden\"), # Methoden -> Praxis\n (9, 3, 2, \"\/wissen\/praxis\/prozess\"), # Prozess -> Praxis\n\n # Level 2 - children of Kontext\n (5, 10, 2, \"\/wissen\/kontext\/organisation\"), # Organisation -> Kontext\n (4, 10, 2, \"\/wissen\/kontext\/kommunikation\"), # Kommunikation -> Kontext\n\n # Level 2 - children of Anwendungsfelder\n (7, anwendung_id, 2, \"\/wissen\/anwendungsfelder\/coaching\"), # Coaching\n (8, anwendung_id, 2, \"\/wissen\/anwendungsfelder\/therapie\"), # Therapie\n (6, anwendung_id, 2, \"\/wissen\/anwendungsfelder\/entwicklung\"), # Entwicklung\n (11, anwendung_id, 2, \"\/wissen\/anwendungsfelder\/haltung\"), # Haltung\n ]\n\n for term_id, parent_id, depth, path in updates:\n db.execute(\n \"UPDATE taxonomy_terms SET parent_id = %s, depth = %s, path = %s WHERE id = %s\",\n (parent_id, depth, path, term_id)\n )\n\n db.commit()\n print(f\" Updated {len(updates) + 1} taxonomy terms with hierarchy\")\n\n\ndef build_ontology_hierarchy():\n \"\"\"Build ontology class hierarchy.\"\"\"\n print(\"Building ontology hierarchy...\")\n\n # Add root class\n db.execute(\n \"\"\"INSERT INTO ontology_classes (name, parent_class_id, description, properties)\n VALUES (%s, %s, %s, %s)\"\"\",\n (\"Coaching-Wissen\", None, \"Wurzelklasse für Coaching-Domäne\", '{\"domain\": \"coaching\"}')\n )\n db.commit()\n\n # Get root ID\n cursor = db.execute(\"SELECT id FROM ontology_classes WHERE name = 'Coaching-Wissen'\")\n root_id = cursor.fetchone()[\"id\"]\n cursor.close()\n\n # Add intermediate classes\n db.execute(\n \"\"\"INSERT INTO ontology_classes (name, parent_class_id, description)\n VALUES (%s, %s, %s)\"\"\",\n (\"Coaching-Aktivität\", root_id, \"Handlungen und Interventionen im Coaching\")\n )\n db.commit()\n\n cursor = db.execute(\"SELECT id FROM ontology_classes WHERE name = 'Coaching-Aktivität'\")\n activity_id = cursor.fetchone()[\"id\"]\n cursor.close()\n\n # Update existing classes to have parents\n # Coaching-Methode, Coaching-Prozess, Team-Intervention -> Coaching-Aktivität\n # Coaching-Konzept -> Coaching-Wissen (root)\n updates = [\n (1, activity_id), # Coaching-Methode -> Aktivität\n (3, activity_id), # Coaching-Prozess -> Aktivität\n (4, activity_id), # Team-Intervention -> Aktivität\n (2, root_id), # Coaching-Konzept -> Wissen\n ]\n\n for class_id, parent_id in updates:\n db.execute(\n \"UPDATE ontology_classes SET parent_class_id = %s WHERE id = %s\",\n (parent_id, class_id)\n )\n\n db.commit()\n print(f\" Updated {len(updates)} ontology classes with hierarchy\")\n\n\ndef verify_hierarchies():\n \"\"\"Verify hierarchies are properly set up.\"\"\"\n print(\"\\nVerification:\")\n\n # Check taxonomy\n cursor = db.execute(\"\"\"\n SELECT t1.name, t1.depth, t1.path, t2.name as parent_name\n FROM taxonomy_terms t1\n LEFT JOIN taxonomy_terms t2 ON t1.parent_id = t2.id\n ORDER BY t1.path\n \"\"\")\n terms = cursor.fetchall()\n cursor.close()\n\n print(\"\\n Taxonomy Hierarchy:\")\n for t in terms:\n indent = \" \" * (t[\"depth\"] or 0)\n parent = f\" (← {t['parent_name']})\" if t[\"parent_name\"] else \"\"\n print(f\" {indent}• {t['name']}{parent}\")\n\n # Check ontology\n cursor = db.execute(\"\"\"\n SELECT o1.name, o2.name as parent_name\n FROM ontology_classes o1\n LEFT JOIN ontology_classes o2 ON o1.parent_class_id = o2.id\n ORDER BY o1.parent_class_id NULLS FIRST, o1.id\n \"\"\")\n classes = cursor.fetchall()\n cursor.close()\n\n print(\"\\n Ontology Hierarchy:\")\n for c in classes:\n parent = f\" (← {c['parent_name']})\" if c[\"parent_name\"] else \" [ROOT]\"\n print(f\" • {c['name']}{parent}\")\n\n\ndef main():\n \"\"\"Build hierarchies.\"\"\"\n print(\"=\" * 60)\n print(\"Phase 2: Building Hierarchies\")\n print(\"=\" * 60)\n\n if not db.connect():\n print(\"ERROR: Could not connect to database\")\n return 1\n\n try:\n build_taxonomy_hierarchy()\n build_ontology_hierarchy()\n verify_hierarchies()\n\n print(\"\\n\" + \"=\" * 60)\n print(\"Hierarchies built successfully!\")\n print(\"=\" * 60)\n\n finally:\n db.disconnect()\n\n return 0\n\n\nif __name__ == \"__main__\":\n sys.exit(main())\n"
}
}
{
"tool_response": {
"type": "create",
"filePath": "\/var\/www\/scripts\/pipeline\/build_hierarchies.py",
"content": "#!\/usr\/bin\/env python3\n\"\"\"\nBuild hierarchies for taxonomy_terms and ontology_classes.\nPhase 2 of semantic infrastructure cleanup.\n\"\"\"\n\nimport os\nimport sys\n\nsys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))\n\nfrom db import db\n\n\ndef build_taxonomy_hierarchy():\n \"\"\"Build taxonomy term hierarchy for systemisches Coaching.\"\"\"\n print(\"Building taxonomy hierarchy...\")\n\n # Hierarchy design for systemisches Coaching:\n # Wissen (12) - Root\n # ├── Theorie (2) - depth 1\n # ├── Praxis (3) - depth 1\n # │ ├── Methoden (1) - depth 2\n # │ └── Prozess (9) - depth 2\n # ├── Kontext (10) - depth 1\n # │ ├── Organisation (5) - depth 2\n # │ └── Kommunikation (4) - depth 2\n # └── Anwendungsfelder - new, depth 1\n # ├── Coaching (7) - depth 2\n # ├── Therapie (8) - depth 2\n # ├── Entwicklung (6) - depth 2\n # └── Haltung (11) - depth 2\n\n ROOT_ID = 12 # Wissen\n\n # First, update root\n db.execute(\n \"UPDATE taxonomy_terms SET depth = 0, path = %s WHERE id = %s\",\n (\"\/wissen\", ROOT_ID)\n )\n\n # Create Anwendungsfelder category\n db.execute(\n \"\"\"INSERT INTO taxonomy_terms (name, slug, parent_id, description, depth, path)\n VALUES (%s, %s, %s, %s, %s, %s)\"\"\",\n (\"Anwendungsfelder\", \"anwendungsfelder\", ROOT_ID,\n \"Praktische Anwendungsbereiche\", 1, \"\/wissen\/anwendungsfelder\")\n )\n db.commit()\n\n # Get Anwendungsfelder ID\n cursor = db.execute(\"SELECT id FROM taxonomy_terms WHERE slug = 'anwendungsfelder'\")\n anwendung_id = cursor.fetchone()[\"id\"]\n cursor.close()\n\n # Define hierarchy updates\n updates = [\n # Level 1 - direct children of Wissen\n (2, ROOT_ID, 1, \"\/wissen\/theorie\"), # Theorie\n (3, ROOT_ID, 1, \"\/wissen\/praxis\"), # Praxis\n (10, ROOT_ID, 1, \"\/wissen\/kontext\"), # Kontext\n\n # Level 2 - children of Praxis\n (1, 3, 2, \"\/wissen\/praxis\/methoden\"), # Methoden -> Praxis\n (9, 3, 2, \"\/wissen\/praxis\/prozess\"), # Prozess -> Praxis\n\n # Level 2 - children of Kontext\n (5, 10, 2, \"\/wissen\/kontext\/organisation\"), # Organisation -> Kontext\n (4, 10, 2, \"\/wissen\/kontext\/kommunikation\"), # Kommunikation -> Kontext\n\n # Level 2 - children of Anwendungsfelder\n (7, anwendung_id, 2, \"\/wissen\/anwendungsfelder\/coaching\"), # Coaching\n (8, anwendung_id, 2, \"\/wissen\/anwendungsfelder\/therapie\"), # Therapie\n (6, anwendung_id, 2, \"\/wissen\/anwendungsfelder\/entwicklung\"), # Entwicklung\n (11, anwendung_id, 2, \"\/wissen\/anwendungsfelder\/haltung\"), # Haltung\n ]\n\n for term_id, parent_id, depth, path in updates:\n db.execute(\n \"UPDATE taxonomy_terms SET parent_id = %s, depth = %s, path = %s WHERE id = %s\",\n (parent_id, depth, path, term_id)\n )\n\n db.commit()\n print(f\" Updated {len(updates) + 1} taxonomy terms with hierarchy\")\n\n\ndef build_ontology_hierarchy():\n \"\"\"Build ontology class hierarchy.\"\"\"\n print(\"Building ontology hierarchy...\")\n\n # Add root class\n db.execute(\n \"\"\"INSERT INTO ontology_classes (name, parent_class_id, description, properties)\n VALUES (%s, %s, %s, %s)\"\"\",\n (\"Coaching-Wissen\", None, \"Wurzelklasse für Coaching-Domäne\", '{\"domain\": \"coaching\"}')\n )\n db.commit()\n\n # Get root ID\n cursor = db.execute(\"SELECT id FROM ontology_classes WHERE name = 'Coaching-Wissen'\")\n root_id = cursor.fetchone()[\"id\"]\n cursor.close()\n\n # Add intermediate classes\n db.execute(\n \"\"\"INSERT INTO ontology_classes (name, parent_class_id, description)\n VALUES (%s, %s, %s)\"\"\",\n (\"Coaching-Aktivität\", root_id, \"Handlungen und Interventionen im Coaching\")\n )\n db.commit()\n\n cursor = db.execute(\"SELECT id FROM ontology_classes WHERE name = 'Coaching-Aktivität'\")\n activity_id = cursor.fetchone()[\"id\"]\n cursor.close()\n\n # Update existing classes to have parents\n # Coaching-Methode, Coaching-Prozess, Team-Intervention -> Coaching-Aktivität\n # Coaching-Konzept -> Coaching-Wissen (root)\n updates = [\n (1, activity_id), # Coaching-Methode -> Aktivität\n (3, activity_id), # Coaching-Prozess -> Aktivität\n (4, activity_id), # Team-Intervention -> Aktivität\n (2, root_id), # Coaching-Konzept -> Wissen\n ]\n\n for class_id, parent_id in updates:\n db.execute(\n \"UPDATE ontology_classes SET parent_class_id = %s WHERE id = %s\",\n (parent_id, class_id)\n )\n\n db.commit()\n print(f\" Updated {len(updates)} ontology classes with hierarchy\")\n\n\ndef verify_hierarchies():\n \"\"\"Verify hierarchies are properly set up.\"\"\"\n print(\"\\nVerification:\")\n\n # Check taxonomy\n cursor = db.execute(\"\"\"\n SELECT t1.name, t1.depth, t1.path, t2.name as parent_name\n FROM taxonomy_terms t1\n LEFT JOIN taxonomy_terms t2 ON t1.parent_id = t2.id\n ORDER BY t1.path\n \"\"\")\n terms = cursor.fetchall()\n cursor.close()\n\n print(\"\\n Taxonomy Hierarchy:\")\n for t in terms:\n indent = \" \" * (t[\"depth\"] or 0)\n parent = f\" (← {t['parent_name']})\" if t[\"parent_name\"] else \"\"\n print(f\" {indent}• {t['name']}{parent}\")\n\n # Check ontology\n cursor = db.execute(\"\"\"\n SELECT o1.name, o2.name as parent_name\n FROM ontology_classes o1\n LEFT JOIN ontology_classes o2 ON o1.parent_class_id = o2.id\n ORDER BY o1.parent_class_id NULLS FIRST, o1.id\n \"\"\")\n classes = cursor.fetchall()\n cursor.close()\n\n print(\"\\n Ontology Hierarchy:\")\n for c in classes:\n parent = f\" (← {c['parent_name']})\" if c[\"parent_name\"] else \" [ROOT]\"\n print(f\" • {c['name']}{parent}\")\n\n\ndef main():\n \"\"\"Build hierarchies.\"\"\"\n print(\"=\" * 60)\n print(\"Phase 2: Building Hierarchies\")\n print(\"=\" * 60)\n\n if not db.connect():\n print(\"ERROR: Could not connect to database\")\n return 1\n\n try:\n build_taxonomy_hierarchy()\n build_ontology_hierarchy()\n verify_hierarchies()\n\n print(\"\\n\" + \"=\" * 60)\n print(\"Hierarchies built successfully!\")\n print(\"=\" * 60)\n\n finally:\n db.disconnect()\n\n return 0\n\n\nif __name__ == \"__main__\":\n sys.exit(main())\n",
"structuredPatch": [],
"originalFile": null
}
}