panproto-protocols 0.30.0

Built-in protocol definitions for panproto
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//! ELAN/Praat time-aligned annotation protocol definition.
//!
//! Covers both the ELAN Annotation Format (EAF XML) and the Praat TextGrid
//! format. ELAN uses `ANNOTATION_DOCUMENT` as the root, with `TIER` children
//! containing either `ALIGNABLE_ANNOTATION` (references two `TIME_SLOT`s via
//! `TIME_SLOT_REF1`/`TIME_SLOT_REF2`) or `REF_ANNOTATION` (references a
//! parent annotation). Praat TextGrid uses a root `text-grid` with
//! `interval-tier` or `point-tier` children holding `interval` or `point`
//! leaves respectively.
//!
//! Uses Group E theory: constrained multigraph + W-type + metadata.
//! Schema `colimit(ThGraph, ThConstraint, ThMulti)`,
//! instance `colimit(ThWType, ThMeta)`.

use std::collections::HashMap;
use std::hash::BuildHasher;

use panproto_gat::Theory;
use panproto_schema::{EdgeRule, Protocol, Schema, SchemaBuilder};

use crate::emit::{children_by_edge, find_roots, vertex_constraints};
use crate::error::ProtocolError;
use crate::theories;

/// Returns the ELAN/Praat protocol definition.
#[must_use]
pub fn protocol() -> Protocol {
    Protocol {
        name: "elan".into(),
        schema_theory: "ThElanSchema".into(),
        instance_theory: "ThElanInstance".into(),
        edge_rules: edge_rules(),
        obj_kinds: vec![
            // ELAN (EAF) kinds
            "annotation-document".into(),
            "tier".into(),
            "alignable-annotation".into(),
            "ref-annotation".into(),
            "time-slot".into(),
            "linguistic-type".into(),
            "controlled-vocabulary".into(),
            "cv-entry".into(),
            "locale".into(),
            // Praat TextGrid kinds
            "text-grid".into(),
            "interval-tier".into(),
            "point-tier".into(),
            "interval".into(),
            "point".into(),
            // Scalar kinds shared by both
            "string".into(),
            "integer".into(),
            "float".into(),
        ],
        constraint_sorts: vec![
            // ELAN constraint sorts
            "time-value".into(),
            "annotation-value".into(),
            "tier-id".into(),
            "participant".into(),
            "annotator".into(),
            "linguistic-type-id".into(),
            "stereotype".into(),
            // Praat constraint sorts
            "xmin".into(),
            "xmax".into(),
            "text".into(),
            "mark".into(),
        ],
        has_order: true,
        ..Protocol::default()
    }
}

/// Register the component GATs for ELAN.
pub fn register_theories<S: BuildHasher>(registry: &mut HashMap<String, Theory, S>) {
    theories::register_multigraph_wtype_meta(registry, "ThElanSchema", "ThElanInstance");
}

/// Parse a JSON-based ELAN schema into a [`Schema`].
///
/// # Errors
///
/// Returns [`ProtocolError`] if parsing fails.
pub fn parse_elan(json: &serde_json::Value) -> Result<Schema, ProtocolError> {
    let proto = protocol();
    let mut builder = SchemaBuilder::new(&proto);

    let types = json
        .get("types")
        .and_then(serde_json::Value::as_object)
        .ok_or_else(|| ProtocolError::MissingField("types".into()))?;

    for (name, def) in types {
        let kind = def
            .get("kind")
            .and_then(serde_json::Value::as_str)
            .unwrap_or("annotation-document");
        builder = builder.vertex(name, kind, None)?;

        // Constraints from top-level attributes.
        if let Some(constraints) = def
            .get("constraints")
            .and_then(serde_json::Value::as_object)
        {
            for (sort, val) in constraints {
                if let Some(v) = val.as_str() {
                    builder = builder.constraint(name, sort, v);
                }
            }
        }

        // Nested fields as child vertices with prop edges.
        if let Some(fields) = def.get("fields").and_then(serde_json::Value::as_object) {
            for (field_name, field_def) in fields {
                let field_id = format!("{name}.{field_name}");
                let field_kind = field_def
                    .get("type")
                    .and_then(serde_json::Value::as_str)
                    .unwrap_or("string");
                builder = builder.vertex(&field_id, field_kind, None)?;
                builder = builder.edge(name, &field_id, "contains", Some(field_name))?;

                if let Some(fc) = field_def
                    .get("constraints")
                    .and_then(serde_json::Value::as_object)
                {
                    for (sort, val) in fc {
                        if let Some(v) = val.as_str() {
                            builder = builder.constraint(&field_id, sort, v);
                        }
                    }
                }
            }
        }

        // Typed references (time-ref, type-ref, cv-ref, parent-ref).
        if let Some(refs) = def.get("refs").and_then(serde_json::Value::as_array) {
            for (i, r) in refs.iter().enumerate() {
                if let (Some(edge_kind), Some(target)) = (
                    r.get("edge").and_then(|v| v.as_str()),
                    r.get("target").and_then(|v| v.as_str()),
                ) {
                    let ref_id = format!("{name}:ref{i}");
                    let ref_kind = r
                        .get("kind")
                        .and_then(serde_json::Value::as_str)
                        .unwrap_or("string");
                    builder = builder.vertex(&ref_id, ref_kind, None)?;
                    builder = builder.edge(name, &ref_id, edge_kind, Some(target))?;
                }
            }
        }

        // Items (contained children by kind).
        if let Some(items) = def.get("items").and_then(serde_json::Value::as_array) {
            for (i, item) in items.iter().enumerate() {
                if let Some(item_kind) = item.as_str() {
                    let item_id = format!("{name}:item{i}");
                    builder = builder.vertex(&item_id, item_kind, None)?;
                    builder = builder.edge(name, &item_id, "contains", Some(item_kind))?;
                }
            }
        }
    }

    let schema = builder.build()?;
    Ok(schema)
}

/// Emit a [`Schema`] as a JSON ELAN schema.
///
/// # Errors
///
/// Returns [`ProtocolError`] if emission fails.
pub fn emit_elan(schema: &Schema) -> Result<serde_json::Value, ProtocolError> {
    let structural = &["contains", "time-ref", "type-ref", "cv-ref", "parent-ref"];
    let roots = find_roots(schema, structural);

    let mut types = serde_json::Map::new();
    for root in &roots {
        let mut obj = serde_json::Map::new();
        obj.insert("kind".into(), serde_json::json!(root.kind));

        // Emit constraints.
        let cs = vertex_constraints(schema, &root.id);
        if !cs.is_empty() {
            let mut constraints = serde_json::Map::new();
            for c in &cs {
                constraints.insert(c.sort.to_string(), serde_json::json!(c.value));
            }
            obj.insert("constraints".into(), serde_json::Value::Object(constraints));
        }

        // Emit contains-edge children as fields.
        let children = children_by_edge(schema, &root.id, "contains");
        if !children.is_empty() {
            let mut fields = serde_json::Map::new();
            for (edge, child) in &children {
                let name = edge.name.as_deref().unwrap_or(&child.id);
                let mut field = serde_json::Map::new();
                field.insert("type".into(), serde_json::json!(child.kind));
                let fc = vertex_constraints(schema, &child.id);
                if !fc.is_empty() {
                    let mut fcs = serde_json::Map::new();
                    for c in &fc {
                        fcs.insert(c.sort.to_string(), serde_json::json!(c.value));
                    }
                    field.insert("constraints".into(), serde_json::Value::Object(fcs));
                }
                fields.insert(name.to_string(), serde_json::Value::Object(field));
            }
            obj.insert("fields".into(), serde_json::Value::Object(fields));
        }

        // Emit ref edges.
        let ref_kinds = ["time-ref", "type-ref", "cv-ref", "parent-ref"];
        let mut refs = Vec::new();
        for rk in &ref_kinds {
            let ref_children = children_by_edge(schema, &root.id, rk);
            for (edge, child) in &ref_children {
                let target = edge.name.as_deref().unwrap_or(&child.id);
                refs.push(serde_json::json!({
                    "edge": rk,
                    "target": target,
                    "kind": child.kind,
                }));
            }
        }
        if !refs.is_empty() {
            obj.insert("refs".into(), serde_json::Value::Array(refs));
        }

        types.insert(root.id.to_string(), serde_json::Value::Object(obj));
    }

    Ok(serde_json::json!({ "types": types }))
}

fn edge_rules() -> Vec<EdgeRule> {
    vec![
        // ELAN structural containment: document → tiers/time-slots/vocabularies,
        // tier → annotations, vocabulary → entries.
        EdgeRule {
            edge_kind: "contains".into(),
            src_kinds: vec![
                "annotation-document".into(),
                "tier".into(),
                "controlled-vocabulary".into(),
                // Praat TextGrid containment: grid → tiers, tiers → leaves.
                "text-grid".into(),
                "interval-tier".into(),
                "point-tier".into(),
            ],
            tgt_kinds: vec![],
        },
        // Only alignable-annotation has direct time-slot references; ref-annotation
        // inherits timing through its parent chain.
        EdgeRule {
            edge_kind: "time-ref".into(),
            src_kinds: vec!["alignable-annotation".into()],
            tgt_kinds: vec!["time-slot".into()],
        },
        EdgeRule {
            edge_kind: "type-ref".into(),
            src_kinds: vec!["tier".into()],
            tgt_kinds: vec!["linguistic-type".into()],
        },
        EdgeRule {
            edge_kind: "cv-ref".into(),
            src_kinds: vec!["tier".into()],
            tgt_kinds: vec!["controlled-vocabulary".into()],
        },
        // ref-annotation references another annotation as its parent; the parent
        // can be either an alignable-annotation or another ref-annotation.
        EdgeRule {
            edge_kind: "parent-ref".into(),
            src_kinds: vec!["ref-annotation".into()],
            tgt_kinds: vec!["alignable-annotation".into(), "ref-annotation".into()],
        },
    ]
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod tests {
    use super::*;

    #[test]
    fn protocol_def() {
        let p = protocol();
        assert_eq!(p.name, "elan");
    }

    #[test]
    fn register_theories_works() {
        let mut registry = HashMap::new();
        register_theories(&mut registry);
        assert!(registry.contains_key("ThElanSchema"));
        assert!(registry.contains_key("ThElanInstance"));
    }

    /// ELAN: abstract `annotation` kind is not in `obj_kinds` or edge rules.
    #[test]
    fn abstract_annotation_kind_absent() {
        let p = protocol();
        assert!(
            !p.obj_kinds.iter().any(|k| k == "annotation"),
            "abstract 'annotation' kind must not appear in obj_kinds"
        );
        for rule in &p.edge_rules {
            assert!(
                !rule.src_kinds.iter().any(|k| k == "annotation"),
                "abstract 'annotation' kind must not appear in src_kinds of '{}'",
                rule.edge_kind
            );
            assert!(
                !rule.tgt_kinds.iter().any(|k| k == "annotation"),
                "abstract 'annotation' kind must not appear in tgt_kinds of '{}'",
                rule.edge_kind
            );
        }
    }

    /// Only `alignable-annotation` may be the source of a `time-ref` edge.
    #[test]
    fn time_ref_src_only_alignable() {
        let p = protocol();
        let rule = p
            .find_edge_rule("time-ref")
            .expect("time-ref rule must exist");
        assert_eq!(
            rule.src_kinds,
            vec!["alignable-annotation".to_string()],
            "time-ref src_kinds must be exactly [alignable-annotation]"
        );
    }

    /// `parent-ref` target must be concrete annotation kinds only.
    #[test]
    fn parent_ref_tgt_concrete() {
        let p = protocol();
        let rule = p
            .find_edge_rule("parent-ref")
            .expect("parent-ref rule must exist");
        assert!(
            rule.tgt_kinds.contains(&"alignable-annotation".to_string()),
            "parent-ref tgt_kinds must include alignable-annotation"
        );
        assert!(
            rule.tgt_kinds.contains(&"ref-annotation".to_string()),
            "parent-ref tgt_kinds must include ref-annotation"
        );
        assert!(
            !rule.tgt_kinds.iter().any(|k| k == "annotation"),
            "parent-ref tgt_kinds must not include abstract annotation"
        );
    }

    /// Praat vertex kinds must all be present as recognized kinds.
    #[test]
    fn praat_kinds_present() {
        let p = protocol();
        for kind in &[
            "text-grid",
            "interval-tier",
            "point-tier",
            "interval",
            "point",
        ] {
            assert!(
                p.is_known_vertex_kind(kind),
                "Praat kind '{kind}' must be recognized"
            );
        }
    }

    #[test]
    fn parse_and_emit() {
        let json = serde_json::json!({
            "types": {
                "doc": {
                    "kind": "annotation-document",
                    "fields": {
                        "main-tier": {
                            "type": "tier",
                            "constraints": {
                                "tier-id": "default"
                            }
                        }
                    }
                },
                "ts1": {
                    "kind": "time-slot",
                    "constraints": {
                        "time-value": "0"
                    }
                },
                "ts2": {
                    "kind": "time-slot",
                    "constraints": {
                        "time-value": "1000"
                    }
                },
                "ann1": {
                    "kind": "alignable-annotation",
                    "constraints": {
                        "annotation-value": "hello"
                    },
                    "refs": [
                        {"edge": "time-ref", "target": "ts1", "kind": "time-slot"},
                        {"edge": "time-ref", "target": "ts2", "kind": "time-slot"}
                    ]
                }
            }
        });
        let schema = parse_elan(&json).expect("should parse");
        assert!(schema.has_vertex("doc"));
        assert!(schema.has_vertex("ts1"));
        assert!(schema.has_vertex("ann1"));
        let emitted = emit_elan(&schema).expect("emit");
        let s2 = parse_elan(&emitted).expect("re-parse");
        assert_eq!(schema.vertex_count(), s2.vertex_count());
    }

    /// Praat `TextGrid` round-trip: parse a minimal text-grid schema and re-emit.
    #[test]
    fn praat_text_grid_round_trip() {
        let json = serde_json::json!({
            "types": {
                "tg": {
                    "kind": "text-grid",
                    "constraints": {
                        "xmin": "0.0",
                        "xmax": "3.5"
                    }
                },
                "tier1": {
                    "kind": "interval-tier",
                    "constraints": {
                        "xmin": "0.0",
                        "xmax": "3.5"
                    }
                },
                "itvl1": {
                    "kind": "interval",
                    "constraints": {
                        "xmin": "0.0",
                        "xmax": "1.2",
                        "text": "hello"
                    }
                },
                "ptier1": {
                    "kind": "point-tier",
                    "constraints": {
                        "xmin": "0.0",
                        "xmax": "3.5"
                    }
                },
                "pt1": {
                    "kind": "point",
                    "constraints": {
                        "xmin": "2.1",
                        "mark": "boundary"
                    }
                }
            }
        });
        let schema = parse_elan(&json).expect("praat parse");
        assert!(schema.has_vertex("tg"));
        assert!(schema.has_vertex("tier1"));
        assert!(schema.has_vertex("itvl1"));
        assert!(schema.has_vertex("ptier1"));
        assert!(schema.has_vertex("pt1"));
        let emitted = emit_elan(&schema).expect("praat emit");
        let s2 = parse_elan(&emitted).expect("praat re-parse");
        assert_eq!(schema.vertex_count(), s2.vertex_count());
    }

    /// ref-annotation may reference another ref-annotation as parent (chained refs).
    #[test]
    fn ref_annotation_parent_ref_chained() {
        let json = serde_json::json!({
            "types": {
                "ts1": { "kind": "time-slot", "constraints": { "time-value": "0" } },
                "ts2": { "kind": "time-slot", "constraints": { "time-value": "500" } },
                "ann_align": {
                    "kind": "alignable-annotation",
                    "constraints": { "annotation-value": "base" },
                    "refs": [
                        { "edge": "time-ref", "target": "ts1", "kind": "time-slot" },
                        { "edge": "time-ref", "target": "ts2", "kind": "time-slot" }
                    ]
                },
                "ann_ref1": {
                    "kind": "ref-annotation",
                    "constraints": { "annotation-value": "child" },
                    "refs": [
                        { "edge": "parent-ref", "target": "ann_align", "kind": "alignable-annotation" }
                    ]
                },
                "ann_ref2": {
                    "kind": "ref-annotation",
                    "constraints": { "annotation-value": "grandchild" },
                    "refs": [
                        { "edge": "parent-ref", "target": "ann_ref1", "kind": "ref-annotation" }
                    ]
                }
            }
        });
        let schema = parse_elan(&json).expect("chained ref parse");
        assert!(schema.has_vertex("ann_ref2"));
    }
}