src.dackar.RCA.cmms_integration.cmms_context_builder¶
cmms_context_builder — CMMSContextBuilder.
- Orchestrates live CMMS data retrieval for a single RCA run:
Derives the lookback window from kg_context.past_events[] (last PM) or falls back to event_time − fallback_lookback_days.
Identifies sister component IDs from kg_context.components[].
Calls the CMMSContextAdapter.fetch() method.
Enriches raw records: days_before_event, FLOC→KG component match.
Builds the recurrence_summary aggregate.
Returns a dict conforming to schemas/cmms_context.json.
Chroma injection (narrative text → run-scoped embeddings) is handled separately by the orchestrator, which has access to the evidence store.
Attributes¶
Classes¶
Configuration for CMMSContextBuilder. |
|
Builds a |
Functions¶
|
|
|
Module Contents¶
- src.dackar.RCA.cmms_integration.cmms_context_builder._parse_iso(ts)[source]¶
- Parameters:
ts (Optional[str])
- Return type:
Optional[datetime.datetime]
- src.dackar.RCA.cmms_integration.cmms_context_builder._days_between(earlier, later)[source]¶
- Parameters:
earlier (Optional[datetime.datetime])
later (Optional[datetime.datetime])
- Return type:
Optional[int]
- class src.dackar.RCA.cmms_integration.cmms_context_builder.CMMSContextBuilderConfig[source]¶
Configuration for CMMSContextBuilder.
- Parameters:
fallback_lookback_days – Days to look back when no PM date is found in kg_context.past_events[]. Default: 90.
sister_relation_types – KG
relation_to_assetvalues that qualify a component as sister equipment. Default:["same_train", "adjacent"].include_sister_equipment – Whether to include sister equipment in the CMMS query scope. Default: True.
max_cr_records – Cap on how many CR records to retain in the artifact (adapter may return more; the most recent are kept). 0 = no cap.
max_wo_records – Cap on WO records. 0 = no cap.
similarity_resolver – Optional
EquipmentSimilarityResolverinstance. When provided, Tier 2 (failure mode overlap) and Tier 3 (spec embedding) sisters are added to the topology-based sisters already derived from KG topology. Set toNone(default) to use topology-only sister resolution.
- class src.dackar.RCA.cmms_integration.cmms_context_builder.CMMSContextBuilder(adapter, config=None)[source]¶
Builds a
cmms_contextartifact from live CMMS data.- Parameters:
adapter (Any) – Any object implementing the
CMMSContextAdapterProtocol.config (Optional[CMMSContextBuilderConfig]) –
CMMSContextBuilderConfig— defaults to 90-day fallback, same_train + adjacent sister scope.
- build(event, kg_context, run_id)[source]¶
Build and return a
cmms_contextdict.- Parameters:
- Returns:
Conforms to
schemas/cmms_context.json.- Return type:
dict
- get_chroma_documents(cmms_context)[source]¶
Extract narrative documents suitable for Chroma injection.
Returns a list of dicts, each with: -
text: the narrative to embed (long_textfield) -metadata: source, run_id, record ID, is_sister_equipmentMetadata is emitted Chroma-clean (
Noneand empty values dropped, list/dict values JSON-encoded via_chroma_clean_metadata) so the orchestrator can inject it without a separate sanitizer. Path-A structured extras (condition_assessmentetc.) are still read off the record when present, butbuild()projects them out of the artifact records, so the default artifact-driven flow carries none — routing those extras to Chroma is left to the injection MR (pass un-projected records here).The orchestrator passes these to
evidence_store.add_documents()(or equivalent) after callingbuild().
- static _chroma_clean_metadata(meta)[source]¶
Coerce a metadata dict to Chroma-native scalar values.
Native Chroma metadata values must be non-null
str/int/float/bool. This dropsNone-valued keys and empty containers, and JSON-encodes any remaining list/dict values, soget_chroma_documentsemits Chroma-clean metadata itself rather than relying on the orchestrator’s storage sanitizer.
- classmethod _extract_structured_fields(record)[source]¶
Extract and flatten Path-A structured CMMS fields for retrieval metadata.
- static _build_component_lookups(kg_context)[source]¶
Build
functional_location→component_idandequipment_id→component_idmaps fromkg_context.components[].Uses the
maximo_floc/sap_equipment_idKG properties (the same properties the adapters map sister components through). First writer wins on duplicate keys.- Parameters:
kg_context (JsonDict)
- Return type:
tuple
- _resolve_lookback(kg_context, event_ts, primary_asset_id='')[source]¶
Returns (lookback_from: datetime, anchor_label: str).
Searches kg_context.past_events[] for the most recent PM (“PM” / “preventive_maintenance”) on the primary asset that precedes the event, and anchors the window there. Falls back to event_ts − fallback_lookback_days.
past_events[] can span sister components, so entries are filtered to the primary
asset_id; and a PM must precedeevent_tsto bound a valid (non-inverted) window.- Parameters:
kg_context (JsonDict)
event_ts (Optional[datetime.datetime])
primary_asset_id (str)
- Return type:
tuple
- _resolve_sisters(kg_context)[source]¶
Build the sister component list from topology + optional similarity tiers.
Returns a list of dicts with keys:
component_id,component_label,match_type,shared_fm_count,embedding_score.Tier 1 (topology) is always included when
include_sister_equipmentis True — these are components withrelation_to_assetin the configuredsister_relation_types.Tier 2/3 (FM overlap + spec embedding) are added when
config.similarity_resolveris set. Results from both sources are merged; a component found in both topology and similarity tiers gets a combinedmatch_type(e.g."topology+failure_mode_overlap").
- classmethod _project_sister(result)[source]¶
Project one similarity-resolver result onto the sister_components[] schema whitelist.
config.similarity_resolveris typedAny, so a result may carry extra keys, a missingmatch_type, or wrongly-typed numerics that would fail strictsister_components[]validation. Drops non-schema keys, coercesshared_fm_count/embedding_scoreto the schema’s numeric types, and returnsNone(logging a warning) when a required field (component_id/match_type) is absent.- Parameters:
result (Any)
- Return type:
Optional[JsonDict]
- _enrich_all(raw_records, event_dt, *, is_cr, floc_to_cid, equip_to_cid, sister_ids)[source]¶
Enrich a list of raw CMMS records, skipping any that are still malformed after normalization. Returns
(valid_records, dropped_count).- Parameters:
raw_records (List[Any])
event_dt (Optional[datetime.datetime])
is_cr (bool)
floc_to_cid (Dict[str, str])
equip_to_cid (Dict[str, str])
sister_ids (set)
- Return type:
tuple
- _enrich_record(record, event_dt, is_cr, *, floc_to_cid=None, equip_to_cid=None, sister_ids=None)[source]¶
Add derived fields to a raw CMMS record, project it onto the cmms_context schema whitelist, and validate the schema-required fields. Returns the enriched record, or
Nonewhen it is still malformed after normalization (missing/invalid required field) so the caller can skip it and record the drop in provenance.days_before_event: int or Nonecomponent_id: resolved fromfunctional_location/equipment_idvia the KG lookups when the adapter did not supply onestatus: normalized to open/closed/cancelled/unknown (every documented Maximo/SAP code; unrecognized → unknown)is_sister_equipment: coerced to a real bool from the adapter value (a truthy string like"false"no longer survives), else derived from whether the resolvedcomponent_idis a KG sister
Adapter-supplied fields outside the schema whitelist (e.g. Path-A
condition_assessment/failure_mode_refs) are dropped from the returned record so the artifact validates againstcmms_context.json(additionalProperties: false).
- static _coerce_bool(value)[source]¶
Coerce a raw
is_sister_equipmentvalue to a real bool. ReturnsNonefor an absent or uninterpretable value so the caller can derive it from KG topology instead (a truthy string like"false"must not read as True).- Parameters:
value (Any)
- Return type:
Optional[bool]
- static _has_required_fields(record, id_key)[source]¶
True if
recordcarries the cmms_context-required fields with valid types: a non-empty string id, a stringshort_description, and a parseablecreated_date(is_sister_equipmentis always set to a bool upstream;statusis always a valid enum value).- Parameters:
record (JsonDict)
id_key (str)
- Return type:
bool