rete-cli 0.4.1

CLI to build and inspect Rete graph files.
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
//! Build inputs: gzip, Turtle/TriG streaming, and the external (memory-bounded)
//! build's acceptance of them.
//!
//! The public RDF dumps that actually need `--memory-budget-mb` do not ship as
//! plain `.nt` — they ship as `dump.ttl.gz` / `dump.trig.gz`. Expanding one to
//! N-Triples first costs an order of magnitude more disk than the source (146.8
//! bytes/triple measured on SemOpenAlex), which is exactly the resource the
//! external build exists to avoid spending. These tests pin the contract that
//! makes that unnecessary: every accepted syntax streams, compressed or not, and
//! the bytes it produces do not depend on which spelling of the same graph the
//! input used.

mod common;

use std::io::Write as _;

use common::{fixture, rete};
use predicates::prelude::PredicateBooleanExt as _;

const TTL: &str = concat!(
    "@prefix ex: <http://example.test/> .\n",
    "ex:alice ex:knows ex:bob ;\n",
    "         ex:name \"Alice\"@en .\n",
    "ex:bob ex:name \"Bob\"@en .\n",
);

/// The same three statements as `TTL`, but inside a named graph.
const TRIG: &str = concat!(
    "@prefix ex: <http://example.test/> .\n",
    "ex:people {\n",
    "  ex:alice ex:knows ex:bob ;\n",
    "           ex:name \"Alice\"@en .\n",
    "  ex:bob ex:name \"Bob\"@en .\n",
    "}\n",
);

fn gzip(bytes: &[u8]) -> Vec<u8> {
    let mut enc = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
    enc.write_all(bytes).unwrap();
    enc.finish().unwrap()
}

/// `.ttl.gz` builds, and to the same bytes as the uncompressed `.ttl`.
/// Compression is a transport detail; it must not reach the output.
#[test]
fn gzipped_turtle_builds_identically() {
    let f = fixture();
    let plain = f.write("g.ttl", TTL);
    let gz = f.write("g.ttl.gz", gzip(TTL.as_bytes()));
    let out_plain = f.path("plain.rete");
    let out_gz = f.path("gz.rete");

    for (src, out) in [(&plain, &out_plain), (&gz, &out_gz)] {
        rete()
            .args(["build"])
            .arg(src)
            .arg("-o")
            .arg(out)
            .assert()
            .success();
    }
    assert_eq!(
        std::fs::read(&out_plain).unwrap(),
        std::fs::read(&out_gz).unwrap(),
        "gzip must not change the built file"
    );
}

/// A gzip file may be a CONCATENATION of members — what `cat a.gz b.gz` writes,
/// and how several public dumps are assembled. `GzDecoder` stops silently after
/// the first member and would drop the rest of the graph on the floor; the
/// decoder must be the multi-member one.
#[test]
fn concatenated_gzip_members_all_read() {
    let f = fixture();
    let first = "<http://example.test/a> <http://example.test/p> \"1\" .\n";
    let second = "<http://example.test/b> <http://example.test/p> \"2\" .\n";
    let mut both = gzip(first.as_bytes());
    both.extend_from_slice(&gzip(second.as_bytes()));
    let src = f.write("both.nt.gz", both);
    let out = f.path("both.rete");

    rete()
        .args(["build"])
        .arg(&src)
        .arg("-o")
        .arg(&out)
        .assert()
        .success()
        .stdout(predicates::str::contains("2 triples"));
}

/// The external build accepts Turtle — the point of the exercise. Its output is
/// documented to be byte-identical to a standard `--no-pyramid` build, and that
/// promise must survive the new input path.
#[test]
fn external_build_from_gzipped_turtle_matches_in_ram() {
    let f = fixture();
    let gz = f.write("g.ttl.gz", gzip(TTL.as_bytes()));
    let plain = f.write("g.ttl", TTL);
    let out_ext = f.path("ext.rete");
    let out_ram = f.path("ram.rete");

    rete()
        .args(["build"])
        .arg(&gz)
        .args(["--memory-budget-mb", "64", "-o"])
        .arg(&out_ext)
        .assert()
        .success();
    rete()
        .args(["build"])
        .arg(&plain)
        .args(["--no-pyramid", "-o"])
        .arg(&out_ram)
        .assert()
        .success();

    assert_eq!(
        std::fs::read(&out_ext).unwrap(),
        std::fs::read(&out_ram).unwrap(),
        "external build of .ttl.gz must equal the in-RAM --no-pyramid build"
    );
}

/// TriG parses, and its named graph survives into the file.
#[test]
fn trig_keeps_its_named_graph() {
    let f = fixture();
    let src = f.write("g.trig", TRIG);
    let out = f.path("g.rete");

    rete()
        .args(["build"])
        .arg(&src)
        .arg("-o")
        .arg(&out)
        .assert()
        .success()
        .stdout(predicates::str::contains("named graph"));
    rete()
        .args(["graphs"])
        .arg(&out)
        .assert()
        .success()
        .stdout(predicates::str::contains("http://example.test/people"));
}

/// `--collapse-graphs` folds those statements into the default graph — which is
/// what makes a named-graph dump answer `?s ?p ?o` at all. The result must equal
/// the same data written as plain Turtle.
#[test]
fn collapse_graphs_matches_the_default_graph_build() {
    let f = fixture();
    let trig = f.write("g.trig", TRIG);
    let ttl = f.write("g.ttl", TTL);
    let out_collapsed = f.path("collapsed.rete");
    let out_ttl = f.path("ttl.rete");

    rete()
        .args(["build"])
        .arg(&trig)
        .args(["--collapse-graphs", "-o"])
        .arg(&out_collapsed)
        .assert()
        .success();
    rete()
        .args(["build"])
        .arg(&ttl)
        .arg("-o")
        .arg(&out_ttl)
        .assert()
        .success();

    assert_eq!(
        std::fs::read(&out_collapsed).unwrap(),
        std::fs::read(&out_ttl).unwrap(),
        "a collapsed TriG must build to the same file as the equivalent Turtle"
    );
}

/// The external build carries named graphs through the same spill the default
/// graph uses, so a TriG input must produce **byte-identical** output to the
/// in-RAM `--no-pyramid` build — the guarantee `--memory-budget-mb` already made
/// for the default graph, extended to quads (#139).
#[test]
fn external_build_keeps_named_graphs_byte_identically() {
    let f = fixture();
    let src = f.write("g.trig", TRIG);
    let out_ext = f.path("ext.rete");
    let out_ram = f.path("ram.rete");

    rete()
        .args(["build"])
        .arg(&src)
        .args(["--memory-budget-mb", "64", "-o"])
        .arg(&out_ext)
        .assert()
        .success()
        .stdout(predicates::str::contains("named graph"));
    rete()
        .args(["build"])
        .arg(&src)
        .args(["--no-pyramid", "-o"])
        .arg(&out_ram)
        .assert()
        .success();

    assert_eq!(
        std::fs::read(&out_ext).unwrap(),
        std::fs::read(&out_ram).unwrap(),
        "external build of a TriG must equal the in-RAM --no-pyramid build"
    );
    rete()
        .args(["graphs"])
        .arg(&out_ext)
        .assert()
        .success()
        .stdout(predicates::str::contains("http://example.test/people"));

    // …and --collapse-graphs still folds them into the default graph.
    let out_flat = f.path("flat.rete");
    rete()
        .args(["build"])
        .arg(&src)
        .args(["--collapse-graphs", "--memory-budget-mb", "64", "-o"])
        .arg(&out_flat)
        .assert()
        .success();
}

/// `rete merge` fed the external builder, so it refused named-graph shards up
/// front. With #139 it folds them: two shards carrying *different* named graphs
/// merge into one file that has both, and the union of their statements.
#[test]
fn merge_folds_named_graph_shards() {
    let f = fixture();
    let a = f.write(
        "a.nq",
        concat!(
            "<http://ex/s1> <http://ex/p> <http://ex/o1> <http://ex/gA> .\n",
            "<http://ex/d> <http://ex/p> <http://ex/o> .\n",
        ),
    );
    let b = f.write(
        "b.nq",
        concat!(
            "<http://ex/s2> <http://ex/p> <http://ex/o2> <http://ex/gB> .\n",
            "<http://ex/s1> <http://ex/p> <http://ex/o1> <http://ex/gA> .\n",
        ),
    );
    let (ra, rb, out) = (f.path("a.rete"), f.path("b.rete"), f.path("all.rete"));
    for (src, dst) in [(&a, &ra), (&b, &rb)] {
        rete()
            .args(["build"])
            .arg(src)
            .arg("-o")
            .arg(dst)
            .assert()
            .success();
    }
    rete()
        .args(["merge"])
        .arg(&ra)
        .arg(&rb)
        .arg("-o")
        .arg(&out)
        .assert()
        .success();

    let graphs = rete().args(["graphs"]).arg(&out).assert().success();
    let listed = String::from_utf8(graphs.get_output().stdout.clone()).unwrap();
    assert!(listed.contains("http://ex/gA"), "gA missing: {listed}");
    assert!(listed.contains("http://ex/gB"), "gB missing: {listed}");
    // the duplicate quad in gA is written once: 2 named + 1 default
    rete()
        .args(["info"])
        .arg(&out)
        .assert()
        .success()
        .stdout(predicates::str::contains("quad_count: 3"));
}

/// `--text-index` on the memory-bounded path: it builds, the file is the same
/// bytes as the in-RAM `--no-pyramid --text-index` build (cardless, so the
/// whole file, not just the section), and `rete search --contains` answers —
/// for the default scope and for a scoped one.
#[test]
fn external_build_writes_the_in_ram_text_index() {
    let f = fixture();
    let src = f.write(
        "g.nq",
        "<http://ex/s> <http://ex/p> \"hello world\" .\n\
         <http://ex/s> <http://ex/desc> \"has been modified\" .\n\
         <http://ex/t> <http://ex/p> \"hello études\"@fr .\n\
         <http://ex/s/prov> <http://ex/desc> \"has been created\" <http://ex/s/g> .\n",
    );
    let scoped = [
        "--text-index-graphs",
        "all",
        "--text-index-exclude",
        "http://ex/desc",
    ];
    for extra in [&[][..], &scoped[..]] {
        let ram = f.path("ram.rete");
        let ext = f.path("ext.rete");
        rete()
            .args(["build"])
            .arg(&src)
            .args(["--text-index", "--no-pyramid"])
            .args(extra)
            .arg("-o")
            .arg(&ram)
            .assert()
            .success();
        rete()
            .args(["build"])
            .arg(&src)
            .args(["--text-index", "--memory-budget-mb", "64"])
            .args(extra)
            .arg("-o")
            .arg(&ext)
            .assert()
            .success()
            .stderr(predicates::str::contains("extbuild: text index"));
        assert_eq!(
            std::fs::read(&ext).unwrap(),
            std::fs::read(&ram).unwrap(),
            "{extra:?}: memory-bounded and in-RAM builds differ"
        );
        rete()
            .args(["search"])
            .arg(&ext)
            .args(["--contains", "hello"])
            .assert()
            .success()
            .stdout(predicates::str::contains("<http://ex/s>"))
            .stdout(predicates::str::contains("<http://ex/t>"));
    }
    // The scoped file leaves `ex:desc` out, so `modified` finds nothing.
    rete()
        .args(["search"])
        .arg(f.path("ext.rete"))
        .args(["--contains", "modified"])
        .assert()
        .success()
        .stdout(predicates::str::contains("<http://ex/s>").not());
}

/// The scope flags need `--text-index`, take an absolute IRI, and do not mix
/// an include list with an exclude list.
#[test]
fn text_index_scope_flags_are_validated() {
    let f = fixture();
    let src = f.write("g.nt", "<http://ex/s> <http://ex/p> \"hello world\" .\n");
    let out = f.path("out.rete");
    for (args, why) in [
        (
            &["--text-index-exclude", "http://ex/p"][..],
            "required arguments were not provided",
        ),
        (
            &["--text-index-graphs", "all"][..],
            "required arguments were not provided",
        ),
        (
            &["--text-index", "--text-index-exclude", "description"][..],
            "absolute",
        ),
        (
            &[
                "--text-index",
                "--text-index-include",
                "http://ex/a",
                "--text-index-exclude",
                "http://ex/b",
            ][..],
            "cannot be used with",
        ),
    ] {
        rete()
            .args(["build"])
            .arg(&src)
            .args(args)
            .arg("-o")
            .arg(&out)
            .assert()
            .failure()
            .stderr(predicates::str::contains(why));
    }
}

/// RDF/XML is the one syntax the external build still refuses; the error has to
/// say so rather than fall over inside the parser.
#[test]
fn external_build_rejects_rdfxml_with_a_clear_error() {
    let f = fixture();
    let src = f.write("o.rdf", "<rdf:RDF/>");
    let out = f.path("ext.rete");

    rete()
        .args(["build"])
        .arg(&src)
        .args(["--memory-budget-mb", "64", "-o"])
        .arg(&out)
        .assert()
        .failure()
        .stderr(predicates::str::contains("rdfxml"));
}

/// Turtle with **anonymous blank nodes** — `[ … ]` and collections — must build.
///
/// This is the trap the streaming work has to stay clear of: oxttl labels an
/// anonymous blank node with a fresh random id on every parse, so any path that
/// parses the input more than once presents the encoder terms the observing pass
/// never saw. The two-pass assembler does exactly that, which is why `.ttl` files
/// keep taking the whole-text path; the external build reads once and is safe.
/// Both must produce the graph, and the same number of statements.
#[test]
fn turtle_with_anonymous_blank_nodes_builds_on_both_paths() {
    let f = fixture();
    let ttl = concat!(
        "@prefix ex: <http://ex/> .\n",
        "ex:a ex:p [ ex:q \"1\" ] .\n",
        "ex:b ex:list ( \"x\" \"y\" ) .\n",
    );
    let src = f.write("bn.ttl", ttl);
    let out_plain = f.path("bn.rete");
    let out_ext = f.path("bn-ext.rete");

    rete()
        .args(["build"])
        .arg(&src)
        .args(["--no-pyramid", "-o"])
        .arg(&out_plain)
        .assert()
        .success()
        .stdout(predicates::str::contains("7 triples"));
    rete()
        .args(["build"])
        .arg(&src)
        .args(["--memory-budget-mb", "64", "-o"])
        .arg(&out_ext)
        .assert()
        .success()
        .stdout(predicates::str::contains("7 triples"));
}

/// `estimate` reads the same inputs the build does — otherwise the "will this
/// fit?" question cannot be asked about the dumps that need asking.
#[test]
fn estimate_reads_gzipped_turtle() {
    let f = fixture();
    let gz = f.write("g.ttl.gz", gzip(TTL.as_bytes()));

    rete()
        .args(["estimate"])
        .arg(&gz)
        .assert()
        .success()
        .stdout(predicates::str::contains("statements"));
}

/// `--label-index` and the default SCHEMA section are part of the external
/// build's byte-identity promise: the memory-bounded build computes both with
/// external sorts and must write exactly the in-RAM build's bytes.
#[test]
fn external_build_with_label_index_matches_in_ram() {
    let f = fixture();
    let nt = "<http://ex/w1> <http://purl.org/dc/terms/title> \"Acta  Universitatis\" .\n\
              <http://ex/w1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://ex/Journal> .\n\
              <http://ex/w1> <http://ex/editor> <http://ex/p1> .\n\
              <http://ex/p1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://ex/Person> .\n\
              <http://ex/p1> <http://xmlns.com/foaf/0.1/familyName> \"Svobodová\" .\n\
              <http://ex/p1> <http://xmlns.com/foaf/0.1/givenName> \"Jana\" .\n\
              <http://ex/Journal> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://ex/Venue> .\n";
    let src = f.write("l.nt", nt);
    let (out_ext, out_ram) = (f.path("ext.rete"), f.path("ram.rete"));
    rete()
        .args(["build"])
        .arg(&src)
        .args(["--memory-budget-mb", "64", "--label-index", "-o"])
        .arg(&out_ext)
        .assert()
        .success();
    rete()
        .args(["build"])
        .arg(&src)
        .args(["--no-pyramid", "--label-index", "-o"])
        .arg(&out_ram)
        .assert()
        .success();
    let ext = std::fs::read(&out_ext).unwrap();
    assert_eq!(ext, std::fs::read(&out_ram).unwrap());
    let h = rete_core::Header::from_bytes(&ext).unwrap();
    assert!(h.label_index_section().is_some() && h.schema_section().is_some());
    rete()
        .args(["search"])
        .arg(&out_ext)
        .arg("svob")
        .assert()
        .success()
        .stdout(predicates::str::contains("Svobodová, Jana"));
}