mto2doc first commit

This commit is contained in:
Avhinav Gupta
2026-08-04 15:59:48 +05:30
commit bc02c9e357
28 changed files with 5944 additions and 0 deletions
+524
View File
@@ -0,0 +1,524 @@
"""
Universal Engineering Object Compiler.
Compiles raw parallel extractions (text, symbols, paths, relationships)
into a structured master UniversalEngineeringGraph.
Drawing-type-aware dispatch:
- PID / PFD / ISOMETRIC → compile lines, instruments, valves, PSVs, equipment
- ELECTRICAL_LAYOUT → compile luminaires, panels, cables, equipment, annotations
- EARTHING_LAYOUT → compile earthing components, equipment, annotations
- SLD → compile panels, circuits, equipment, annotations
- HVAC_LAYOUT → compile equipment, annotations
- STRUCTURAL_LAYOUT → compile equipment, annotations
- CABLE_SCHEDULE → compile cables, panels, annotations
- GENERIC → compile generic components and annotations
All hardcoded tag-number lookups and project-specific logic have been removed.
Properties are derived purely from extracted attributes or sensible generic defaults.
"""
import re
import logging
from typing import Dict, Any, List, Optional
from src.agents.base import BaseAgent
from src.models import (
UniversalEngineeringGraph,
EquipmentItem, LineItem, InstrumentItem, ValveItem, SafetyReliefValveItem,
LuminaireItem, PanelItem, CableItem, EarthingItem,
GenericComponentItem, AnnotationItem, Relationship,
)
from src.state import GraphState
logger = logging.getLogger(__name__)
# Classification sets per category
_ELECTRICAL_CLASSIFICATIONS = {'PANEL_TAG', 'LUMINAIRE_TAG', 'CIRCUIT_TAG'}
_EARTHING_CLASSIFICATIONS = {'EARTH_BAR_TAG', 'EARTH_PIT_TAG', 'BOND_CONDUCTOR_TAG'}
_ANNOTATION_CLASSIFICATIONS = {'NOTE', 'ELEVATION_TAG', 'RATING'}
_PID_VALVE_KEYWORDS = ('GB', 'CB', 'HV', 'XV', 'CV', 'FV', 'PCV', 'FCV',
'TCV', 'LCV', 'MOV', 'SDV', 'BDV', 'EV', 'BV')
class CompilerAgent(BaseAgent):
"""
Agent responsible for compiling raw parallel extractions into a structured
master UniversalEngineeringGraph.
"""
def run(self, state: GraphState) -> Dict[str, Any]:
logger.info("Running Universal Engineering Object Compiler...")
entities = state.get("extracted_entities", {})
text_elements = entities.get("text_elements", [])
symbols = entities.get("symbols", [])
relations = entities.get("relations", [])
geometry = entities.get("geometry", {})
metadata = state.get("metadata", {})
drawing_type = metadata.get("drawing_type", "GENERIC").upper()
discipline = metadata.get("discipline", "Unknown")
logger.info(f"Compiling for drawing_type='{drawing_type}', discipline='{discipline}'")
logger.info(
f"Input: {len(text_elements)} text elements, "
f"{len(symbols)} symbols, {len(relations)} relations"
)
# Build the universal graph
graph = UniversalEngineeringGraph(
drawing_type=drawing_type,
discipline=discipline,
)
# Always compile relationships (cross-type)
graph.relationships = self._compile_relationships(relations)
# Always compile annotations (notes, elevations, ratings)
graph.annotations = self._compile_annotations(text_elements)
# Drawing-type dispatch
if drawing_type in ('PID', 'PFD', 'ISOMETRIC'):
graph.equipment = self._compile_equipment(text_elements, symbols)
graph.lines = self._compile_lines(text_elements, geometry, relations)
graph.instruments = self._compile_instruments(text_elements, symbols)
graph.valves = self._compile_valves(text_elements, symbols, relations, graph.lines)
graph.safety_relief_valves = self._compile_safety_relief_valves(text_elements, symbols)
elif drawing_type == 'ELECTRICAL_LAYOUT':
graph.luminaires = self._compile_luminaires(text_elements, symbols)
graph.panels = self._compile_panels(text_elements, symbols)
graph.cables = self._compile_cables(text_elements, symbols, relations)
graph.equipment = self._compile_equipment(text_elements, symbols)
elif drawing_type == 'EARTHING_LAYOUT':
graph.earthing_components = self._compile_earthing(text_elements, symbols)
graph.equipment = self._compile_equipment(text_elements, symbols)
elif drawing_type == 'SLD':
graph.panels = self._compile_panels(text_elements, symbols)
graph.cables = self._compile_cables(text_elements, symbols, relations)
graph.equipment = self._compile_equipment(text_elements, symbols)
elif drawing_type == 'CABLE_SCHEDULE':
graph.cables = self._compile_cables(text_elements, symbols, relations)
graph.panels = self._compile_panels(text_elements, symbols)
else:
# HVAC_LAYOUT, STRUCTURAL_LAYOUT, GENERIC, and anything else
graph.equipment = self._compile_equipment(text_elements, symbols)
graph.generic_components = self._compile_generic(text_elements, symbols)
total = graph.total_items
logger.info(
f"Compiler produced {total} total items across all entity types. "
f"drawing_type={drawing_type}"
)
return {
"engineering_graph": graph,
"revision_history": state.get("revision_history", []) + [{
"action": f"Compiled {total} engineering entities into UniversalEngineeringGraph",
"drawing_type": drawing_type,
"items_count": total,
}],
}
# ── P&ID Compilers ─────────────────────────────────────────────────────────
def _compile_equipment(self, texts: List[Dict], symbols: List[Dict]) -> List[EquipmentItem]:
compiled = []
eq_tags = [t for t in texts if t["classification"] == "EQUIPMENT_TAG"]
for eq in eq_tags:
tag = eq["tag"]
coords = None
eq_type = "Generic Equipment"
for sym in symbols:
if sym.get("inferred_tag") == tag:
coords = [sym["ymin"], sym["xmin"], sym["ymax"], sym["xmax"]]
eq_type = sym["symbol_type"]
break
attrs = eq.get("attributes") or {}
compiled.append(EquipmentItem(
tag=tag,
name=eq["value"],
type=eq_type,
description=eq["value"],
design_pressure=attrs.get("design_pressure"),
design_temperature=attrs.get("design_temperature"),
flow_rate=attrs.get("flow_rate"),
duty=attrs.get("duty") or eq.get("rating"),
material=attrs.get("material"),
coordinates=coords,
))
return compiled
def _compile_lines(self, texts: List[Dict], geom: Dict, relations: List[Dict]) -> List[LineItem]:
compiled = []
line_tags = [t for t in texts if t["classification"] == "LINE_TAG"]
for lt in line_tags:
tag = lt["tag"]
# Parse line tag components (e.g. 8"-PV-26-9035-FC11S-08)
pattern = r'^(\d+(?:\.\d+)?\"?)-([A-Z]+)-(\d+)-(\d+)-([A-Z0-9]+)(?:-([A-Z0-9]+))?'
match = re.match(pattern, tag)
if match:
size, service, system, sequence, spec, insulation = match.groups()
else:
parts = tag.split('-')
size = parts[0] if parts else '1"'
service = parts[1] if len(parts) > 1 else 'UNK'
spec = parts[-2] if len(parts) > 3 else 'UNSPEC'
sequence = parts[-1] if len(parts) > 2 else '0000'
insulation = None
path_coords = None
for trace in geom.get("traces", []):
if trace["tag"] == tag:
path_coords = trace["grid_path"]
break
from_node = None
to_node = None
for rel in relations:
if rel["rel_type"] == "CONNECTS_TO":
if rel["source_tag"] == tag:
to_node = rel["target_tag"]
elif rel["target_tag"] == tag:
from_node = rel["source_tag"]
compiled.append(LineItem(
tag=tag,
size=size,
service=service,
spec=spec,
sequence_number=sequence,
insulation=insulation,
from_node=from_node,
to_node=to_node,
coordinates=path_coords,
))
return compiled
def _compile_instruments(self, texts: List[Dict], symbols: List[Dict]) -> List[InstrumentItem]:
compiled = []
inst_tags = [t for t in texts if t["classification"] == "INSTRUMENT_TAG"]
for inst in inst_tags:
tag = inst["tag"]
type_match = re.search(r'([A-Z]+)', tag)
inst_type = type_match.group(1) if type_match else "INST"
loop_match = re.search(r'(\d+)', tag)
loop_id = loop_match.group(1) if loop_match else "0000"
coords = None
for sym in symbols:
if sym.get("inferred_tag") == tag:
coords = [sym["ymin"], sym["xmin"], sym["ymax"], sym["xmax"]]
break
compiled.append(InstrumentItem(
tag=tag,
type=inst_type,
service=None,
location="Field",
loop_id=loop_id,
coordinates=coords,
))
return compiled
def _compile_valves(
self, texts: List[Dict], symbols: List[Dict],
relations: List[Dict], lines: List[LineItem]
) -> List[ValveItem]:
compiled = []
valve_tags = [t for t in texts if t["classification"] == "VALVE_TAG"]
for v in valve_tags:
tag = v["tag"]
tag_upper = tag.upper()
# Determine valve type from tag prefix (not hardcoded specific numbers)
v_type = "Valve"
if "GB" in tag_upper or "GATE" in tag_upper:
v_type = "Gate Valve"
elif "CB" in tag_upper or "CHECK" in tag_upper:
v_type = "Check Valve"
elif "BALL" in tag_upper or "BV" in tag_upper:
v_type = "Ball Valve"
elif "GLOBE" in tag_upper or "GLV" in tag_upper:
v_type = "Globe Valve"
elif "NEEDLE" in tag_upper or "NV" in tag_upper:
v_type = "Needle Valve"
elif "BUTTERFLY" in tag_upper or "BFV" in tag_upper:
v_type = "Butterfly Valve"
elif tag_upper.startswith(('HV', 'XV', 'CV', 'FCV', 'PCV', 'TCV', 'LCV')):
v_type = "Control Valve"
elif tag_upper.startswith(('MOV', 'SDV', 'BDV', 'EV')):
v_type = "On-Off Valve"
# Find associated line
associated_line = None
for rel in relations:
if rel["source_tag"] == tag and rel["rel_type"] == "INSTALLED_ON":
associated_line = rel["target_tag"]
break
# Derive size from the associated line
derived_size = None
if associated_line:
for line in lines:
if line.tag == associated_line:
derived_size = line.size
break
if not derived_size:
derived_size = None # Unknown — don't fabricate
# Rating from attrs
attrs = v.get("attributes") or {}
rating = v.get("rating") or attrs.get("rating") or attrs.get("pressure_class")
# Normal state from attrs
normal_state = attrs.get("normal_state")
coords = None
for sym in symbols:
if sym.get("inferred_tag") == tag:
coords = [sym["ymin"], sym["xmin"], sym["ymax"], sym["xmax"]]
break
compiled.append(ValveItem(
tag=tag,
type=v_type,
size=derived_size,
line_tag=associated_line,
rating=rating,
normal_state=normal_state,
coordinates=coords,
))
return compiled
def _compile_safety_relief_valves(self, texts: List[Dict], symbols: List[Dict]) -> List[SafetyReliefValveItem]:
compiled = []
psv_tags = [t for t in texts if t["classification"] == "PSV_TAG"]
for psv in psv_tags:
tag = psv["tag"]
attrs = psv.get("attributes") or {}
# Infer unit prefix from tag (e.g. "26" from "26-PSV-9066A")
unit_match = re.match(r'^(\d{2})-', tag)
unit = unit_match.group(1) if unit_match else ""
coords = None
for sym in symbols:
if sym.get("inferred_tag") == tag:
coords = [sym["ymin"], sym["xmin"], sym["ymax"], sym["xmax"]]
break
compiled.append(SafetyReliefValveItem(
tag=tag,
type=attrs.get("valve_type", "PSV"),
service=psv["value"],
unit=unit,
set_pressure=psv.get("rating") or attrs.get("set_pressure", "N/A"),
inlet_size=attrs.get("inlet_size", "N/A"),
outlet_size=attrs.get("outlet_size", "N/A"),
inlet_spec=attrs.get("inlet_spec", "N/A"),
relief_destination=attrs.get("relief_destination", "N/A"),
remarks=attrs.get("remarks"),
coordinates=coords,
))
return compiled
# ── Electrical Layout Compilers ────────────────────────────────────────────
def _compile_luminaires(self, texts: List[Dict], symbols: List[Dict]) -> List[LuminaireItem]:
compiled = []
items = [t for t in texts if t["classification"] == "LUMINAIRE_TAG"]
for item in items:
tag = item["tag"]
attrs = item.get("attributes") or {}
coords = None
fitting_type = "Luminaire"
for sym in symbols:
if sym.get("inferred_tag") == tag:
coords = [sym["ymin"], sym["xmin"], sym["ymax"], sym["xmax"]]
fitting_type = sym.get("symbol_type", "Luminaire")
break
compiled.append(LuminaireItem(
tag=tag,
fitting_type=attrs.get("fitting_type", fitting_type),
wattage=attrs.get("wattage"),
circuit=attrs.get("circuit"),
panel=attrs.get("panel"),
elevation=attrs.get("elevation"),
location=attrs.get("location"),
coordinates=coords,
))
return compiled
def _compile_panels(self, texts: List[Dict], symbols: List[Dict]) -> List[PanelItem]:
compiled = []
items = [t for t in texts if t["classification"] == "PANEL_TAG"]
for item in items:
tag = item["tag"]
attrs = item.get("attributes") or {}
# Infer panel type from tag
tag_upper = tag.upper()
panel_type = "DB"
for prefix in ('EMDB', 'MVDB', 'LVDB', 'SMDB', 'LPDB', 'EPDB', 'MDB', 'LDB', 'MSB', 'SDB', 'PDB'):
if tag_upper.startswith(prefix):
panel_type = prefix
break
coords = None
for sym in symbols:
if sym.get("inferred_tag") == tag:
coords = [sym["ymin"], sym["xmin"], sym["ymax"], sym["xmax"]]
break
compiled.append(PanelItem(
tag=tag,
panel_type=attrs.get("panel_type", panel_type),
voltage=attrs.get("voltage"),
capacity_kva=attrs.get("capacity_kva"),
feeder_from=attrs.get("feeder_from"),
location=attrs.get("location"),
description=item["value"],
coordinates=coords,
))
return compiled
def _compile_cables(
self, texts: List[Dict], symbols: List[Dict], relations: List[Dict]
) -> List[CableItem]:
compiled = []
items = [t for t in texts if t["classification"] == "CIRCUIT_TAG"]
for item in items:
tag = item["tag"]
attrs = item.get("attributes") or {}
# Look up relationships for from/to
from_panel = None
to_equip = None
for rel in relations:
if rel["source_tag"] == tag and rel["rel_type"] in ("FEEDS", "CONNECTS_TO"):
to_equip = rel["target_tag"]
elif rel["target_tag"] == tag and rel["rel_type"] in ("FEEDS", "CONNECTS_TO"):
from_panel = rel["source_tag"]
compiled.append(CableItem(
tag=tag,
cable_type=attrs.get("cable_type"),
size_mm2=attrs.get("size_mm2"),
cores=attrs.get("cores"),
from_panel=from_panel or attrs.get("from_panel"),
to_equipment=to_equip or attrs.get("to_equipment"),
length_m=attrs.get("length_m"),
route=attrs.get("route"),
))
return compiled
# ── Earthing Layout Compilers ──────────────────────────────────────────────
def _compile_earthing(self, texts: List[Dict], symbols: List[Dict]) -> List[EarthingItem]:
compiled = []
earth_items = [
t for t in texts
if t["classification"] in ('EARTH_BAR_TAG', 'EARTH_PIT_TAG', 'BOND_CONDUCTOR_TAG')
]
type_map = {
'EARTH_BAR_TAG': 'EARTH_BAR',
'EARTH_PIT_TAG': 'EARTH_PIT',
'BOND_CONDUCTOR_TAG': 'BOND_CONDUCTOR',
}
for item in earth_items:
tag = item["tag"]
attrs = item.get("attributes") or {}
coords = None
for sym in symbols:
if sym.get("inferred_tag") == tag:
coords = [sym["ymin"], sym["xmin"], sym["ymax"], sym["xmax"]]
break
compiled.append(EarthingItem(
tag=tag,
component_type=type_map.get(item["classification"], "EARTHING_COMPONENT"),
material=attrs.get("material"),
size=attrs.get("size"),
connected_to=attrs.get("connected_to"),
location=attrs.get("location"),
elevation=attrs.get("elevation"),
resistance=attrs.get("resistance"),
coordinates=coords,
))
return compiled
# ── Generic / Annotation Compilers ────────────────────────────────────────
def _compile_generic(self, texts: List[Dict], symbols: List[Dict]) -> List[GenericComponentItem]:
compiled = []
# Anything with a tag that isn't already handled by a specific compiler
generic_items = [
t for t in texts
if t["classification"] in ('EQUIPMENT_TAG', 'GENERIC_TAG')
]
for item in generic_items:
coords = None
for sym in symbols:
if sym.get("inferred_tag") == item["tag"]:
coords = [sym["ymin"], sym["xmin"], sym["ymax"], sym["xmax"]]
break
compiled.append(GenericComponentItem(
tag=item["tag"],
classification=item["classification"],
description=item.get("value"),
attributes=item.get("attributes"),
coordinates=coords,
))
return compiled
def _compile_annotations(self, texts: List[Dict]) -> List[AnnotationItem]:
compiled = []
ann_items = [t for t in texts if t["classification"] in _ANNOTATION_CLASSIFICATIONS]
type_map = {
'NOTE': 'NOTE',
'ELEVATION_TAG': 'ELEVATION',
'RATING': 'RATING',
}
for item in ann_items:
attrs = item.get("attributes") or {}
compiled.append(AnnotationItem(
text=item["value"],
annotation_type=type_map.get(item["classification"], "NOTE"),
position_x=float(attrs.get("pos_x", 0)) if attrs.get("pos_x") else None,
position_y=float(attrs.get("pos_y", 0)) if attrs.get("pos_y") else None,
))
return compiled
def _compile_relationships(self, relations: List[Dict]) -> List[Relationship]:
return [
Relationship(
source=r["source_tag"],
target=r["target_tag"],
type=r["rel_type"].lower(),
attributes={},
)
for r in relations
]