workflow eventthread-sequence-summarization
  template: define-unit (filter) -> characterise (align) ->
            define-unit (segment stages) -> characterise (encode tensor) ->
            build-model -> define-unit (derive threads) ->
            contextualise (layout) ->
            loop(define-unit (cluster-by-similarity) ->
                 visualise -> abstract -> assess) ->
            generate-knowledge
  description: "Visual analytics workflow for summarizing large collections
    of heterogeneous event sequences into latent threads via tensor
    decomposition, laying out threads to reveal their stage-wise evolution,
    and interactively clustering threads into latent stage categories to
    support interpretation of progression patterns"

# ============================================================================
# INPUT
# ============================================================================

artifact D_sequences : entities
  origin: given
  internal structure: sequence
  embedment: set
  features:
    - id: f_event_types
      value structure: list
      value type: categorical
      description: "Ordered event types within each sequence"
    - id: f_event_times
      value structure: list
      value type: temporal
      description: "Occurrence times of events within each sequence"
  description: "Heterogeneous timestamped event sequences, one per
    analytic entity, containing variable event types and lengths"

# ============================================================================
# STEP 1: FILTER NOISY EVENTS
# ============================================================================

transform T_filter :
  intent: define-unit
  manner: "filter low-importance events using TF-IDF-based scoring"
  input: D_sequences
  output: D_sequences_filtered
  actor: machine
  description: "Remove events with low TF-IDF importance, retaining
    characteristic events that differentiate each sequence from the
    collection"

artifact D_sequences_filtered : entities
  internal structure: sequence
  embedment: set
  description: "Denoised event sequences retaining characteristic events
    after removing low-importance events for each entity"

# ============================================================================
# STEP 2: ALIGN SEQUENCES TO A COMMON TIME ORIGIN
# ============================================================================

transform T_align :
  intent: characterise
  manner: "shift events to a common relative time origin"
  input: D_sequences_filtered
  output: F_relative_time
  actor: machine
  description: "Compute each event's temporal position relative to a
    shared reference point, replacing variable absolute timestamps with
    comparable relative times"

artifact F_relative_time : feature(D_sequences_filtered)
  value structure: vector
  value type: temporal
  description: "Relative time position of each retained event with respect
    to a common origin for its sequence"

# ============================================================================
# SPECIFICATION FOR STAGE SEGMENTATION AND DECOMPOSITION
# ============================================================================

artifact S_stage_spec : specification
  origin: given
  representation form: "parameter settings"
  description: "Analyst-defined stage segmentation granularity (fixed
    duration or event-count boundaries) and number of latent threads for
    tensor decomposition"

# ============================================================================
# STEP 3: SEGMENT INTO SHARED STAGES
# ============================================================================

transform T_segment :
  intent: define-unit
  manner: "time-partitioning into discrete stages"
  input: D_sequences_filtered, F_relative_time, S_stage_spec
  output: D_stages
  actor: machine
  description: "Define a shared ordered sequence of discrete stages used
    to fold events across all entity sequences"

artifact D_stages : entities
  internal structure: elementary
  embedment: sequence
  description: "Ordered discrete stages representing shared temporal units
    of progression across all sequences (e.g., calendar years, maintenance
    rounds)"

# ============================================================================
# STEP 4: ENCODE AS EVENT-STAGE-ENTITY TENSOR
# ============================================================================

transform T_encode :
  intent: characterise
  manner: "fold events into stages and encode as event-stage-entity
    tensor"
  input: D_sequences_filtered, F_relative_time, D_stages
  output: F_tensor
  actor: machine
  description: "Aggregate events within each stage per entity and
    represent the result as a multi-way tensor of event, stage, and
    entity co-occurrence"

artifact F_tensor : feature(D_sequences_filtered)
  value structure: relational configuration
  value type: numeric
  representation form: "three-way event-stage-entity incidence tensor
    with non-negative occurrence counts"
  description: "Multi-way representation of event occurrences across
    event types, shared stages, and entities"

# ============================================================================
# STEP 5: DECOMPOSE TENSOR TO EXTRACT LATENT THREADS
# ============================================================================

transform T_decompose :
  intent: build-model
  manner: "CP tensor decomposition with an orthogonality constraint on
    the stage factor"
  input: F_tensor, S_stage_spec
  output: M_tensor_model
  actor: machine
  description: "Factorise the event-stage-entity tensor into component
    weights and event, stage, and entity factor matrices, constraining
    the stage factor to support interpretable thread evolution"

artifact M_tensor_model : model(F_tensor, S_stage_spec)
  model type: "tensor decomposition model"
  representation form: "CP decomposition with component weights and
    event, stage, and entity factor matrices"
  description: "Factorised representation from which latent threads and
    their event, stage, and entity distributions are derived"

# ============================================================================
# STEP 6: DEFINE LATENT THREADS
# ============================================================================

transform T_define_threads :
  intent: define-unit
  manner: "derive latent threads from decomposition factor matrices"
  input: M_tensor_model
  output: D_threads, F_thread_event_relevance, F_thread_stage_likelihood,
    F_thread_entity_relevance
  actor: machine
  description: "Define threads as latent sequential patterns, each
    characterised by its relevance to events, likelihood across stages,
    and relevance to entities"

artifact D_threads : entities
  internal structure: sequence
  embedment: set
  description: "Latent threads representing summarized clusters of
    similar event sequences, each evolving across the shared stage
    sequence"

artifact F_thread_event_relevance : feature(D_threads)
  value structure: vector
  value type: numeric
  description: "Relevance of each event type to each thread"

artifact F_thread_stage_likelihood : feature(D_threads)
  value structure: vector
  value type: numeric
  description: "Occurrence likelihood of each thread at each stage"

artifact F_thread_entity_relevance : feature(D_threads)
  value structure: vector
  value type: numeric
  description: "Relevance of each entity to each thread"

# ============================================================================
# STEP 7: LAYOUT THREADS
# ============================================================================

transform T_layout :
  intent: contextualise
  manner: "optimisation-based layout balancing thread similarity and
    crossing minimisation"
  input: D_threads, F_thread_event_relevance, F_thread_stage_likelihood
  output: D_layout_space, A_thread_layout
  actor: machine
  description: "Compute positions for threads at each stage that group
    similar threads together while minimising crossings across stages"

artifact D_layout_space : entities
  internal structure: elementary
  features:
    - id: f_axes
      value structure: atomic
      value type: numeric
      description: "Number of spatial dimensions (stage axis, vertical
        position)"
  description: "Computed two-dimensional layout space for displaying
    threads across stages"

artifact A_thread_layout : arrangement(D_threads)
  context: D_layout_space
  principle: "stage-wise vertical positioning balancing thread similarity
    and crossing minimisation"
  description: "Positions of threads within each stage reflecting
    event-based similarity and continuity across stages"

# ============================================================================
# SPECIFICATION FOR STAGE-CATEGORY CLUSTERING (initial loop artifact)
# ============================================================================

artifact S_clustering : specification
  origin: given
  representation form: "parameter settings"
  description: "Initial distance threshold controlling grouping of
    threads into latent stage categories at each stage"

# ============================================================================
# STEP 8: INTERACTIVE STAGE-CATEGORY CLUSTERING LOOP
# ============================================================================

loop L_stage_clustering:
  purpose: "Iteratively adjust the thread clustering threshold to reveal
    meaningful and interpretable latent stage categories and their
    evolution across stages"
  until: "Latent stage categories and their merging/splitting patterns
    are interpretable and reveal meaningful evolution structures"
  body:

    transform T_cluster_threads :
      intent: define-unit
      manner: "cluster-by-similarity (threshold-based grouping within
        each stage)"
      input: D_threads, A_thread_layout, S_clustering
      output: D_stage_categories, F_category_label
      actor: hybrid
      description: "Group neighbouring threads within each stage into
        latent stage categories based on an adjustable distance
        threshold"

    artifact D_stage_categories : entities
      internal structure: group/cluster
      embedment: {set, time}
      description: "Groups of threads with similar event profiles within
        a given stage, representing latent stage categories"

    artifact F_category_label : feature(D_threads)
      value structure: vector
      value type: categorical
      description: "Thread's latent stage category membership at each
        stage"

    transform T_visualise_categories :
      intent: visualise
      manner: "line-map thread view with coordinated contextual panels"
      input: A_thread_layout, F_category_label,
        F_thread_event_relevance, F_thread_stage_likelihood,
        F_thread_entity_relevance, D_threads,
        D_sequences_filtered, D_stages
      output: V_threads
      actor: machine
      description: "Render threads as stage-segmented stripes grouped into
        latent stage categories, with event and entity context in
        coordinated views for inspection"

    artifact V_threads : visualisation(A_thread_layout, F_category_label,
        F_thread_event_relevance, F_thread_stage_likelihood,
        F_thread_entity_relevance, D_sequences_filtered, D_stages)
      layout: "line-map with threads divided into stage segments;
        coordinated contextual panels"
      form: "coloured linear stripes with event nodes, optional
        entity-volume backgrounds, and contextual list and flow views"
      encoding: "stage and vertical position from A_thread_layout;
        category grouping from F_category_label; stripe opacity and
        dashed segments from F_thread_stage_likelihood; event-node size
        from event relevance weighted by stage likelihood; event-node
        colour from event type; background height from entity relevance
        weighted by stage likelihood; contextual views show selected raw
        events and entities"
      description: "Coordinated multi-view visualisation of latent
        threads, latent stage categories, and supporting event-sequence
        context"

    transform T_interpret :
      intent: abstract
      manner: "perception and interpretation of thread/category merging,
        splitting, and event composition"
      input: V_threads, D_stage_categories
      output: P_patterns
      actor: human
      description: "Interpret the semantic meaning of latent stage
        categories and their evolution (merges, splits, branches) across
        stages"

    artifact P_patterns : pattern(D_stage_categories, D_threads)
      representation form: "textual labels describing category
        composition and evolution"
      description: "Interpreted descriptions of latent stage categories,
        their characteristic events and entities, and how they merge,
        split, or diverge across stages"

    transform T_assess_clustering :
      intent: assess
      manner: "evaluate interpretability and coherence of resulting
        stage categories"
      input: V_threads, P_patterns
      output: K_cluster_quality
      actor: human
      description: "Assess whether the current clustering threshold
        yields coherent, interpretable stage categories and meaningful
        evolution patterns"

    artifact K_cluster_quality : knowledge(D_stage_categories)
      representation form: "quality judgment"
      description: "Assessment of stage-category coherence,
        interpretability, and whether further threshold adjustment is
        needed"

    if K_cluster_quality indicates satisfactory clustering:
      then:
        exit loop L_stage_clustering
      else:
        transform T_adjust_threshold :
          intent: generate-knowledge
          manner: "adjust clustering distance threshold based on
            assessment"
          input: K_cluster_quality, V_threads, S_clustering
          output: S_clustering_r
          actor: human
          description: "Adjust the distance threshold to merge or
            separate thread groupings and better reveal meaningful stage
            categories"

        artifact S_clustering_r : specification
          representation form: "parameter settings"
          description: "Updated distance threshold after analyst
            refinement"

        assign:
          S_clustering := S_clustering_r

end loop L_stage_clustering

# ============================================================================
# STEP 9: SYNTHESISE FINDINGS
# ============================================================================

transform T_synthesise :
  intent: generate-knowledge
  manner: "formulate statements about domain progression patterns"
  input: P_patterns, K_cluster_quality
  output: K_findings
  actor: human
  description: "Synthesise insights about typical and divergent
    progression pathways, their distinguishing event characteristics,
    and their evolution across stages"

artifact K_findings : knowledge(P_patterns, K_cluster_quality)
  representation form: "statements and explanations"
  description: "Documented domain insights about typical progression
    pathways, divergent subgroup behaviours, and characteristics
    revealed by thread evolution across stages"