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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
use crate::PartitionDisplay;
use crate::PartitionsDisplay;
use crate::Reason;
use crate::Reasons;
use crate::ValidatorConfig;
use crate::ValidatorErrors;
use crate::atom;
use crate::ref_typing::RefTyping;
use crate::validator_error::*;
use either::Either;
use indexmap::IndexSet;
use itertools::Itertools;
// use prefixmap::PrefixMap;
use rbe::MatchCond;
use rudof_iri::iri;
use rudof_rdf::rdf_core::{
NeighsRDF,
query::QueryRDF,
term::{BlankNode, Iri as _, Object},
};
use shex_ast::Expr;
use shex_ast::Node;
use shex_ast::Pred;
use shex_ast::ShapeLabelIdx;
use shex_ast::ast::cond_kind::CondKind;
use shex_ast::ir::extend_alternative::ExtendAlternative;
use shex_ast::ir::external_resolver::{DispatchOutcome, ExternalResolveCtx};
use shex_ast::ir::preds::Preds;
use shex_ast::ir::schema_ir::SchemaIR;
use shex_ast::ir::sem_act::SemAct;
use shex_ast::ir::semantic_action_context::SemanticActionContext;
use shex_ast::ir::shape::Shape;
use shex_ast::ir::shape_expr::ShapeExpr;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::hash_map::Entry;
use tracing::debug;
use tracing::trace;
type Result<T> = std::result::Result<T, ValidatorError>;
type Atom = atom::Atom<(Node, ShapeLabelIdx)>;
type NegAtom = (Node, ShapeLabelIdx);
type PosAtom = (Node, ShapeLabelIdx);
type Neighs = (Vec<(Pred, Node)>, Vec<Pred>);
type ValidationResult = Either<Vec<ValidatorError>, Vec<Reason>>;
#[derive(Debug, Clone)]
pub struct Engine {
checked: IndexSet<Atom>,
pending: IndexSet<Atom>,
config: ValidatorConfig,
step_counter: usize,
reasons: HashMap<PosAtom, Vec<Reason>>,
errors: HashMap<NegAtom, Vec<ValidatorError>>,
}
impl Engine {
pub fn new(config: &ValidatorConfig) -> Engine {
Engine {
checked: IndexSet::new(),
pending: IndexSet::new(),
config: config.clone(),
step_counter: 0,
reasons: HashMap::new(),
errors: HashMap::new(),
}
}
pub fn reset(&mut self) {
let config = self.config.clone();
*self = Engine::new(&config);
}
pub(crate) fn validate_pending<R>(&mut self, rdf: &R, schema: &SchemaIR) -> Result<()>
where
R: NeighsRDF + QueryRDF,
{
while let Some(atom) = self.pop_pending() {
match atom.clone() {
Atom::Pos((node, idx)) => {
if !check_start_acts(schema.start_acts(), &node, &idx, schema)? {
self.add_checked_neg(
atom.clone(),
vec![ValidatorError::StartActFailed {
node: Box::new(node.clone()),
idx,
}],
);
// We can abort validation if start actions failed
continue;
}
let mut hyp = Vec::new();
match self.prove(&node, &idx, &mut hyp, schema, rdf)? {
Either::Right(reasons) => {
self.add_checked_pos(atom, reasons);
},
Either::Left(errors) => {
self.add_checked_neg(atom, errors);
},
}
},
Atom::Neg((node, idx)) => {
todo!("Handle the case where we have a pending negative atom. Node: {node}, shape idx: {idx}")
},
}
}
Ok(())
}
pub(crate) fn add_checked_pos(&mut self, atom: Atom, reasons: Vec<Reason>) {
let new_atom = atom.clone();
match atom {
Atom::Pos(positive_atom) => {
self.checked.insert(new_atom);
self.add_reasons(positive_atom, reasons)
},
Atom::Neg(negative_atom) => {
todo!("Handle the case where we have a checked negative atom {negative_atom:?}")
},
}
}
pub(crate) fn add_checked_neg(&mut self, atom: Atom, errors: Vec<ValidatorError>) {
match atom.clone() {
Atom::Neg(na) => {
self.checked.insert(atom);
self.add_errors(na, errors)
},
Atom::Pos(na) => {
self.checked.insert(atom.negated());
self.add_errors(na, errors)
},
}
}
pub(crate) fn checked(&self) -> IndexSet<Atom> {
self.checked.clone()
}
fn add_reasons(&mut self, pa: PosAtom, rs: Vec<Reason>) {
match self.reasons.entry(pa) {
Entry::Occupied(mut vs) => vs.get_mut().extend(rs),
Entry::Vacant(vac) => {
vac.insert(rs);
},
}
}
fn add_errors(&mut self, na: NegAtom, es: Vec<ValidatorError>) {
match self.errors.entry(na) {
Entry::Occupied(mut vs) => vs.get_mut().extend(es),
Entry::Vacant(vac) => {
vac.insert(es);
},
}
}
pub(crate) fn pending(&self) -> IndexSet<Atom> {
self.pending.clone()
}
pub fn set_max_steps(&mut self, max_steps: usize) {
self.config.set_max_steps(max_steps);
}
pub fn new_step(&mut self) {
self.step_counter += 1;
}
pub fn add_ok(&mut self, n: Node, s: ShapeLabelIdx) {
let pa = (n, s);
self.checked.insert(Atom::pos(&pa));
}
// TODO: We may remove this method
fn _add_failed(&mut self, n: Node, s: ShapeLabelIdx, err: ValidatorError) {
let atom = (n, s);
self.checked.insert(Atom::neg(&atom));
match self.errors.entry(atom) {
Entry::Occupied(mut es) => es.get_mut().push(err),
Entry::Vacant(vacant) => {
vacant.insert(vec![err]);
},
}
}
pub fn more_pending(&self) -> bool {
!self.pending.is_empty()
}
pub fn add_pending(&mut self, n: Node, s: ShapeLabelIdx) {
let pos_atom = (n, s);
self.pending.insert(Atom::pos(&pos_atom));
}
pub fn pop_pending(&mut self) -> Option<Atom> {
self.pending.pop()
}
pub fn steps(&self) -> usize {
self.step_counter
}
pub fn max_steps(&self) -> usize {
self.config.max_steps()
}
pub(crate) fn find_errors(&self, na: &NegAtom) -> Vec<ValidatorError> {
match self.errors.get(na) {
Some(vs) => vs.to_vec(),
None => Vec::new(),
}
}
pub(crate) fn find_reasons(&self, pa: &PosAtom) -> Vec<Reason> {
match self.reasons.get(pa) {
Some(vs) => vs.to_vec(),
None => Vec::new(),
}
}
/// Returns the set of pairs `(node, shape_idx)` that are dependencies of `node@idx`,
/// i.e. all pairs `(node1, idx1)` such that:
/// - `node1@idx1` is a direct reference in the shape expression of `idx`, or
/// - there is a triple constraint `(pred, ref)` in the shape expression of `idx` and
/// the neighbours of `node` are `(pred, node1)`
pub(crate) fn dep<R>(
&self,
node: &Node,
idx: &ShapeLabelIdx,
schema: &SchemaIR,
rdf: &R,
) -> Result<HashSet<(Node, ShapeLabelIdx)>>
where
R: QueryRDF + NeighsRDF,
{
if let Some(info) = schema.find_shape_idx(idx) {
let se = info.expr();
let mut dep = HashSet::new();
// Search all direct references of the shape expression
for idx in se.direct_references().iter() {
dep.insert((node.clone(), *idx));
}
// Search all pairs `(node1, idx1)` in the shape expr referenced by `idx` such that there is a triple constraint `(pred, ref)`
// and the neighbours of `node` are `(pred, node1)`
let references = se.references(schema);
// trace!("References in shape expr: {:?}", references);
let preds = references.keys().cloned().collect::<Vec<_>>();
let (neighs, _) = self.neighs(node, preds, rdf)?;
for (pred, neigh_node) in neighs {
if let Some(idx_list) = references.get(&pred) {
for idx in idx_list {
dep.insert((neigh_node.clone(), *idx));
}
} else {
/*debug!("No references found for predicate {pred}");*/
}
}
/*trace!(
"Dependencies of {node}@{idx} are: [{}]",
dep.iter().map(|(n, i)| format!("{n}@{i}")).join(", ")
);*/
Ok(dep)
} else {
Err(ValidatorError::ShapeExprNotFound { idx: *idx })
}
}
// Implements algorithm presented in page 14 of this paper:
// https://labra.weso.es/publication/2017_semantics-validation-shapes-schemas/
pub(crate) fn prove<R>(
&self,
node: &Node,
label: &ShapeLabelIdx,
hyp: &mut Vec<(Node, ShapeLabelIdx)>,
schema: &SchemaIR,
rdf: &R,
) -> Result<ValidationResult>
where
R: NeighsRDF + QueryRDF,
{
hyp.push((node.clone(), *label));
let hyp_as_set: HashSet<(Node, ShapeLabelIdx)> =
hyp.iter().map(|(n, l)| (n.clone(), *l)).collect::<HashSet<_>>();
let mut typing = RefTyping::new();
let candidates = self.dep(node, label, schema, rdf)?;
let cleaned_candidates: HashSet<_> = candidates.difference(&hyp_as_set).cloned().collect();
for (n1, l1) in cleaned_candidates {
match self.prove(&n1, &l1, hyp, schema, rdf)? {
Either::Right(_reasons) => {
/*debug!(
"Proved {n1}@{l1} while proving {node}@{label}: {}",
rs.iter().map(|r| format!("{r}")).join(", ")
);*/
typing.insert_passed(n1.clone(), l1);
},
Either::Left(errors) => {
/*debug!(
"Failed to prove {n1}@{l1} while proving {node}@{label}: {}",
errors.iter().map(|e| format!("{e}")).join(", ")
);*/
typing.insert_failed(n1.clone(), l1, errors);
},
}
}
for (n, l) in hyp_as_set.iter() {
typing.insert_passed(n.clone(), *l);
}
let result = self.check_node_idx(node, label, schema, rdf, &mut typing, hyp)?;
hyp.pop();
/*debug!(
"{} {node}@{label} with result: {}, hyp: [{}]",
if result.is_right() { "Proved" } else { "Failed to prove" },
show_result(
&result,
&rdf.prefixmap().unwrap_or_default(),
schema,
self.config.width()
)?,
hyp.iter().map(|(n, l)| format!("{n}@{l}")).join(", ")
);*/
Ok(result)
}
pub(crate) fn check_node_idx<R>(
&self,
node: &Node,
idx: &ShapeLabelIdx,
schema: &SchemaIR,
rdf: &R,
typing: &mut RefTyping,
hyp: &mut Vec<(Node, ShapeLabelIdx)>,
) -> Result<ValidationResult>
where
R: NeighsRDF + QueryRDF,
{
/*trace!(
"Checking {node}@{idx}, typing: [{}]",
typing.iter().map(|(n, l)| format!("{n}@{l}")).join(", ")
);*/
if let Some(info) = schema.find_shape_idx(idx) {
if schema.is_abstract(idx) {
let descendants = schema.descendants(idx);
if descendants.is_empty() {
return Err(ValidatorError::AbstractShapeNoDescendants { idx: *idx });
}
// Each descendant gets its own prove call so it builds a fresh typing
// with all the dependencies it actually needs (RESTRICTS semantics).
let descendants_result = self.check_descendants(node, idx, descendants, schema, rdf, hyp)?;
if descendants_result.is_right() {
Ok(descendants_result)
} else {
// Indicate that the main shape and its descendants failed
Ok(Either::Left(vec![ValidatorError::AbstractShapeError {
idx: *idx,
node: Box::new(node.clone()),
errors: ValidatorErrors::new(descendants_result.left().unwrap().to_vec()),
}]))
}
} else {
let se = info.expr();
let result = self.check_node_shape_expr(idx, node, se, schema, rdf, typing)?;
/*tracing::debug!(
"Result of {node}@{idx} is: {}",
show_result(
&result,
&rdf.prefixmap().unwrap_or_default(),
schema,
self.config.width()
)?,
);*/
if result.is_right() {
Ok(result)
} else {
let descendants = schema.descendants(idx);
if descendants.is_empty() {
return Ok(result);
}
// Each descendant gets its own prove call so it builds a fresh typing
// with all the dependencies it actually needs (RESTRICTS semantics).
let descendants_result = self.check_descendants(node, idx, descendants, schema, rdf, hyp)?;
if descendants_result.is_right() {
Ok(descendants_result)
} else {
// Indicate that the main shape and its descendants failed
let errors_descendants = descendants_result.left().unwrap();
let errors_result = result.left().unwrap();
let all_errors = errors_result
.iter()
.cloned()
.chain(errors_descendants.iter().cloned())
.collect::<Vec<_>>();
Ok(Either::Left(all_errors))
}
}
}
} else {
Err(ValidatorError::ShapeExprNotFound { idx: *idx })
}
}
fn check_descendants<R>(
&self,
node: &Node,
idx: &ShapeLabelIdx,
descendants: Vec<ShapeLabelIdx>,
schema: &SchemaIR,
rdf: &R,
hyp: &mut Vec<(Node, ShapeLabelIdx)>,
) -> Result<ValidationResult>
where
R: NeighsRDF + QueryRDF,
{
let mut errors_collection = Vec::new();
for desc in descendants {
// Use prove (not check_node_idx) so each descendant builds its own complete
// typing from its own dependency set. This implements the ShEx RESTRICTS
// property: if the node conforms to a descendant, it conforms to the ancestor.
match self.prove(node, &desc, hyp, schema, rdf)? {
Either::Left(errors) => {
trace!(
"Descendant {desc} failed for node {node}\nErrors: {}",
errors.iter().map(|err| format!("{err}")).join(", "),
);
errors_collection.push(ValidatorError::DescendantShapeError {
current: *idx,
desc,
node: Box::new(node.clone()),
errors: ValidatorErrors::new(errors),
});
},
Either::Right(reasons) => {
return Ok(Either::Right(vec![Reason::DescendantShape {
node: node.clone(),
shape: *idx,
reasons: Reasons::new(reasons.clone()),
}]));
},
}
}
Ok(Either::Left(vec![ValidatorError::DescendantsShapeError {
idx: *idx,
node: Box::new(node.clone()),
errors: ValidatorErrors::new(errors_collection),
}]))
}
pub(crate) fn check_node_shape_expr<R>(
&self,
idx: &ShapeLabelIdx,
node: &Node,
se: &ShapeExpr,
schema: &SchemaIR,
rdf: &R,
typing: &mut RefTyping,
) -> Result<ValidationResult>
where
R: NeighsRDF + QueryRDF,
{
match se {
ShapeExpr::ShapeAnd { exprs, .. } => {
/*tracing::debug!(
"Checking node {node} with AND, typing: [{}]",
typing.iter().map(|(n, l)| format!("{n}@{l}")).join(", ")
);*/
let mut reasons_collection = Vec::new();
for e in exprs {
match self.check_node_ref(node, e, typing)? {
Either::Left(errors) => {
/*trace!(
"AND failed at component {e} for node {node}\nErrors: {}\nTyping: {}",
errors.iter().map(|err| format!("{err}")).join(", "),
typing.iter().map(|(n, l)| format!("{n}@{l}")).join(", ")
);*/
return Ok(Either::Left(vec![ValidatorError::ShapeAndError {
shape_expr: *e,
node: Box::new(node.clone()),
errors: ValidatorErrors::new(errors),
}]));
},
Either::Right(reasons) => {
reasons_collection.push(reasons);
},
}
}
Ok(Either::Right(vec![Reason::ShapeAnd {
node: node.clone(),
se: Box::new(se.clone()),
reasons: reasons_collection,
}]))
},
ShapeExpr::ShapeOr { exprs, .. } => {
let mut errors_collection = Vec::new();
for e in exprs {
match self.check_node_ref(node, e, typing)? {
Either::Left(errors) => {
/*trace!(
"OR branch {e} failed for node {node}\nErrors: {}\nTyping: {}",
errors.iter().map(|err| format!("{err}")).join(", "),
typing.iter().map(|(n, l)| format!("{n}@{l}")).join(", ")
);*/
errors_collection.push((*e, ValidatorErrors::new(errors)));
},
Either::Right(reasons) => {
return Ok(Either::Right(vec![Reason::ShapeOr {
shape_expr: *e,
node: node.clone(),
reasons: Reasons::new(reasons),
}]));
},
}
}
// If we didn't return inside the loop, all branches failed
Ok(Either::Left(vec![ValidatorError::ShapeOrError {
shape_expr: Box::new(se.clone()),
node: Box::new(node.clone()),
errors: errors_collection.clone(),
}]))
},
ShapeExpr::ShapeNot { expr, .. } => {
let result = self.check_node_ref(node, expr, typing)?;
match result {
Either::Left(errors) => Ok(Either::Right(vec![Reason::ShapeNot {
node: node.clone(),
shape_expr: se.clone(),
errors_evidences: ValidatorErrors::new(errors),
}])),
Either::Right(reasons) => Ok(Either::Left(vec![ValidatorError::ShapeNotError {
node: Box::new(node.clone()),
shape_expr: Box::new(se.clone()),
reasons: Reasons::new(reasons),
}])),
}
},
ShapeExpr::NodeConstraint(nc) => {
// TODO: In the case of a node constraint...is the context only the subject?
let ctx = SemanticActionContext::subject(node).with_registry(schema.semantic_actions_registry_arc());
match nc.cond().matches(node, &ctx) {
Ok(_pending) => {
// We ignore pending nodes here, as node constraints are not expected to generate pending nodes
pass(Reason::NodeConstraint {
node: node.clone(),
nc: (**nc).clone(),
})
},
Err(err) => fail(ValidatorError::RbeError(err)),
}
},
ShapeExpr::Shape(shape) => {
if shape.extends().is_empty() {
self.check_node_shape(idx, node, shape, schema, rdf, typing)
} else {
self.check_node_shape_extends(idx, node, shape, schema, rdf, typing)
}
},
ShapeExpr::External {} => {
let label = schema.shape_label_from_idx(idx);
let ctx = ExternalResolveCtx {
node,
shape_idx: *idx,
shape_label: label,
schema,
};
match self.config.external_resolvers().dispatch(&ctx) {
DispatchOutcome::Conformant { resolver, rationale } => {
debug!("EXTERNAL conformant for {node}@{idx} via '{resolver}': {rationale}");
pass(Reason::External {
node: node.clone(),
resolver,
rationale,
})
},
DispatchOutcome::NonConformant { resolver, rationale } => {
debug!("EXTERNAL non-conformant for {node}@{idx} via '{resolver}': {rationale}");
fail(ValidatorError::ExternalShapeRejected {
node: Box::new(node.clone()),
idx: *idx,
resolver,
rationale,
})
},
DispatchOutcome::Abstain => {
debug!("EXTERNAL unresolved for {node}@{idx}: no resolver answered");
fail(ValidatorError::ExternalShapeUnresolved {
node: Box::new(node.clone()),
idx: *idx,
})
},
}
},
ShapeExpr::Ref { idx } => self.check_node_ref(node, idx, typing),
ShapeExpr::Empty => pass(Reason::Empty { node: node.clone() }),
}
}
fn check_node_ref(&self, node: &Node, idx: &ShapeLabelIdx, typing: &mut RefTyping) -> Result<ValidationResult> {
/*debug!("Checking node {node} with shape ref {idx}"); */
// If the node is already in the typing, we can return true
if typing.contains(&(node.clone(), *idx)) {
pass(Reason::ShapeRef {
node: node.clone(),
idx: *idx,
})
} else {
let errors = typing.errors_for(&(node.clone(), *idx));
fail(ValidatorError::ShapeRefFailed {
node: Box::new(node.clone()),
idx: *idx,
errors: ValidatorErrors::new(errors),
})
}
}
pub(crate) fn check_node_shape<R>(
&self,
idx: &ShapeLabelIdx,
node: &Node,
shape: &Shape,
schema: &SchemaIR,
rdf: &R,
typing: &mut RefTyping,
) -> Result<ValidationResult>
where
R: QueryRDF + NeighsRDF,
{
// trace!("check_node_shape: node = {node}, shape = {idx} [No extends]");
let extra_preds = shape.extra().clone();
let mut candidate_preds = shape.preds();
for p in &extra_preds {
if !candidate_preds.contains(p) {
candidate_preds.push(p.clone());
}
}
let (values, reminder) = self.neighs(node, candidate_preds.clone(), rdf)?;
let values_ctx = values
.iter()
.map(|(p, v)| {
(
p.clone(),
v.clone(),
SemanticActionContext::triple(node, p, v).with_registry(schema.semantic_actions_registry_arc()),
)
})
.filter(|(pred, value, ctx)| {
// Strict predicates (not in EXTRA) always go into M^∈.
if !extra_preds.contains(pred) {
return true;
}
// Lenient predicates (in EXTRA): only values that satisfy a leaf condition
// participate in the RBE; non-matching values fall into M^∉ and are ignored.
matches_any_leaf(shape.triple_expr(), pred, value, ctx)
})
.collect::<Vec<_>>();
if shape.is_closed() && !reminder.is_empty() {
return fail(ValidatorError::ClosedShapeWithRemainderPreds {
remainder: Preds::new(reminder),
declared: Preds::new(candidate_preds),
});
}
check_expr_neigh(shape.triple_expr(), &values_ctx, node, shape, idx, typing)
}
pub(crate) fn check_node_shape_extends<R>(
&self,
idx: &ShapeLabelIdx,
node: &Node,
shape: &Shape,
schema: &SchemaIR,
rdf: &R,
typing: &mut RefTyping,
) -> Result<ValidationResult>
where
R: NeighsRDF + QueryRDF,
{
// trace!("check_node_shape_extends: node={node}, shape={idx}");
// When some extended parent resolves to several alternatives (it contains a
// ShapeOr), validate by trying every selection of one alternative per parent.
// Parents with a single alternative keep the historical code path below.
// See docs/src/internals/feasibility-model.md §3-§4.
let parent_alternatives: Vec<Vec<ExtendAlternative>> =
shape.extends().iter().map(|e| schema.extend_alternatives(e)).collect();
if parent_alternatives.iter().any(|alts| alts.len() > 1) {
return self.check_node_shape_extends_selections(
idx,
node,
shape,
schema,
rdf,
typing,
&parent_alternatives,
);
}
let extra_preds = shape.extra().clone();
let mut candidate_preds = Vec::from_iter(schema.get_preds_extends(idx));
for p in &extra_preds {
if !candidate_preds.contains(p) {
candidate_preds.push(p.clone());
}
}
/*trace!(
"Predicates in this shape with extends: [{}]",
candidate_preds.iter().map(|p| p.to_string()).join(", ")
);*/
let (values, reminder) = self.neighs(node, candidate_preds.clone(), rdf)?;
if shape.is_closed() && !reminder.is_empty() {
/*debug!(
"Closed shape {idx} with extends has remainder preds: [{}]",
reminder.iter().map(|p| p.to_string()).join(", ")
);*/
return fail(ValidatorError::ClosedShapeWithRemainderPreds {
remainder: Preds::new(reminder),
declared: Preds::new(candidate_preds),
});
}
/*if !reminder.is_empty() {
debug!(
"Shape {idx} has extra preds: [{}] but is not closed",
reminder.iter().map(|p| p.to_string()).join(", ")
);
}*/
/*debug!(
"Neighs of {node} [{}]",
values.iter().map(|(p, v)| format!("{p} {v}")).join(", ")
);*/
let triple_exprs = merge_ancestor_exprs(schema.get_triple_exprs(idx).unwrap(), schema);
/*debug!(
"Candidate triple exprs of {node}:\n{}",
triple_exprs
.iter()
.map(|(maybe_label, te)| format!(
" {} -> [{}]",
maybe_label.map(|l| l.to_string()).unwrap_or("_?".to_string()),
te.iter().map(|p| p.show_rbe_simplified()).join("\n")
))
.join("\n")
);*/
let values_ctx_raw: Vec<_> = values
.iter()
.map(|(p, v)| {
(
p.clone(),
v.clone(),
SemanticActionContext::triple(node, p, v).with_registry(schema.semantic_actions_registry_arc()),
)
})
.collect();
// Collect every triple-expression reachable from the parent shapes through the full
// extends / ShapeAnd / ShapeRef hierarchy. This is needed to distinguish two cases:
// (a) A triple that matches no leaf-bucket condition but IS covered by some transitive
// ancestor's condition. These are validated by check_node_extends_main_shape
// (exhaustive semantics) and must be excluded from the partition to avoid spurious
// partition failures.
// (b) A triple that matches no leaf-bucket condition and is NOT covered by any ancestor
// either. These are truly invalid and must remain so the partition correctly fails.
let all_ancestor_exprs: Vec<Expr> = {
let mut exprs = Vec::new();
let mut visited = HashSet::new();
for pi in triple_exprs.keys().filter_map(|k| *k) {
exprs.extend(collect_all_exprs_for_shape(&pi, schema, &mut visited));
}
// Include None (the shape's own) bucket exprs as well
if let Some(none_exprs) = triple_exprs.get(&None) {
exprs.extend(none_exprs.iter().cloned());
}
exprs
};
let values_ctx: Vec<_> = values_ctx_raw
.into_iter()
.filter(|(pred, value, ctx)| {
// Keep if the triple satisfies at least one leaf-bucket condition
// (conservatively: Ref conditions count as matching for M^∈ placement).
let matches_leaf = triple_exprs.values().any(|rbes| {
rbes.iter().any(|rbe| {
rbe.components().any(|(_, key, cond)| {
&key == pred && (cond_has_ref(&cond) || cond.matches(value, ctx).is_ok())
})
})
});
if matches_leaf {
return true;
}
// EXTRA predicate with no satisfying leaf → goes into M^∉, exclude from partition.
if extra_preds.contains(pred) {
return false;
}
// Triple is not needed by any leaf bucket.
// Keep it only if it is also NOT covered by any ancestor (case b: truly invalid).
// If it IS covered by an ancestor (case a), exclude it — the exhaustive check
// in check_node_extends_main_shape already handles it.
let covered_by_ancestor = all_ancestor_exprs.iter().any(|rbe| {
rbe.components()
.any(|(_, key, cond)| &key == pred && cond.matches(value, ctx).is_ok())
});
!covered_by_ancestor
})
.collect();
let mut reasons_parents = Vec::new();
tracing::trace!("Checking extends of shape {idx} for node {node}");
for e in shape.extends() {
tracing::trace!("Checking extends of shape {idx} for node {node} with extends {e}");
let result_parents = self.check_node_extends_main_shape(node, e, shape, schema, rdf, typing)?;
match result_parents {
Either::Left(errors) => {
/*debug!(
"Main shape {idx} or some of its ancestors failed for node {node}\nErrors: {}\nTyping: {}",
errors.iter().map(|err| format!("{err}")).join(", "),
typing.iter().map(|(n, l)| format!("{n}@{l}")).join(", ")
);*/
return Ok(Either::Left(vec![ValidatorError::ShapeExtendsError {
node: Box::new(node.clone()),
shape: Box::new(shape.clone()),
idx: *idx,
extends: *e,
errors: ValidatorErrors::new(errors),
}]));
},
Either::Right(reasons) => {
/*debug!(
"Main shape {idx} and its ancestors succeeded for node {node}\nReasons: {}\nTyping: {}",
reasons.iter().map(|r| format!("{r}")).join(", "),
typing.iter().map(|(n, l)| format!("{n}@{l}")).join(", ")
);*/
reasons_parents.push(Reason::ShapeExtends {
node: node.clone(),
shape: Box::new(shape.clone()),
reasons: Reasons::new(reasons),
});
},
}
}
// Feasibility guard: refute in time linear in the expressions when some bucket
// cannot be satisfied by any assignment of its candidate triples, instead of
// enumerating partitions. Sound: candidate counts over-approximate any share.
for rbes in triple_exprs.values() {
for rbe in rbes {
if !rbe.feasible_neighs(&values_ctx) {
return fail(ValidatorError::TripleExprRefuted {
node: Box::new(node.clone()),
idx: *idx,
});
}
}
}
let parts_iter = crate::class_partitions_iter(&values_ctx, &triple_exprs);
let mut parts_peekable = parts_iter.peekable();
if let Some(_parts) = parts_peekable.peek() {
// There are some partitions to check, we will check them one by one until we find one that works or we exhaust all of them. We use peekable to avoid computing the first partition twice (once for the debug message and once for the loop). We could also compute the first partition before the loop and then use a regular iterator, but this way we avoid computing any partition if there are no partitions at all.
/*debug!(
"Some partition found for node {node} and shape {idx}, showing the first one:\n{}",
parts
.iter()
.enumerate()
.map(|(npart, partition)| format!(" Part {npart}: {}\n", show_partition(partition)))
.join("\n")
);*/
let mut errors_in_partitions = Vec::new();
for (npart, partition) in parts_peekable.enumerate() {
let partition_display = create_partitions_display(&partition);
//debug!("Partition {npart}: {}", partition_display);
let mut ok_partition = true;
let mut errors_in_loop = Vec::new();
let mut reasons_in_loop = Vec::new();
for (maybe_label, rbes, neighs_subset) in partition.iter() {
let result = check_exprs_neigh(rbes, neighs_subset, node, shape, idx, typing)?;
match result {
Either::Right(reasons) => {
/*debug!(
" Part {npart}| Success component {}: neighs {}",
maybe_label.map(|l| l.to_string()).unwrap_or("[]".to_string()),
neighs_subset.iter().map(|(p, v, _ctx)| format!("{p} {v}")).join(", ")
);*/
reasons_in_loop.push(Reason::PartitionComponent {
maybe_label: *maybe_label,
node: node.clone(),
shape: Box::new(shape.clone()),
idx: *idx,
partition_idx: npart,
partition: partition_display.clone(),
neighs: neighs_subset.iter().map(|(p, v, _ctx)| format!("{p} {v}")).join(", "),
reasons: Reasons::new(reasons),
});
},
Either::Left(errs) => {
errors_in_loop.push(ValidatorError::PartitionComponentFailed {
maybe_label: *maybe_label,
node: Box::new(node.clone()),
shape: Box::new(shape.clone()),
idx: *idx,
partition_idx: npart,
partition: partition_display.clone(),
neighs: neighs_subset.iter().map(|(p, v, _ctx)| format!("{p} {v}")).join(", "),
errors: ValidatorErrors::new(errs),
});
ok_partition = false;
/*debug!(
" Part {npart}| Failed component {}: neighs {}",
maybe_label.map(|l| l.to_string()).unwrap_or("[]".to_string()),
neighs_subset.iter().map(|(p, v, _ctx)| format!("{p} {v}")).join(", ")
);*/
// We could collect errors here to provide more information about why the partition failed, but for now we just
// indicate that the partition failed without going into details about the failure of each triple expr
break;
},
}
}
if ok_partition {
// debug!(" Part {npart}| Partition succeeded",);
return pass(Reason::ShapeExtends {
node: node.clone(),
shape: Box::new(shape.clone()),
reasons: Reasons::new(reasons_in_loop),
});
} else {
// debug!(" Part {npart}| Partition failed",);
errors_in_partitions.push(ValidatorError::PartitionFailed {
node: Box::new(node.clone()),
shape: Box::new(shape.clone()),
idx: *idx,
partition: partition_display.clone(),
errors: ValidatorErrors::new(errors_in_loop),
});
}
}
// If we exhaust all partitions, the shape fails
// debug!(" Failed shape {idx}. All partitions failed",);
fail(ValidatorError::ShapeFailed {
node: Box::new(node.clone()),
shape: Box::new(shape.clone()),
idx: *idx,
errors: errors_in_partitions,
})
} else {
debug!("No partitions to check for node {node} and shape {idx}");
fail(ValidatorError::ShapeFailedNoPartitions {
node: Box::new(node.clone()),
shape: Box::new(shape.clone()),
idx: *idx,
})
}
}
/// Validates a shape with extends when some extended parent resolves to several
/// alternatives: tries every selection of one alternative per parent until one
/// conforms. See docs/src/internals/feasibility-model.md §3-§4.
fn check_node_shape_extends_selections<R>(
&self,
idx: &ShapeLabelIdx,
node: &Node,
shape: &Shape,
schema: &SchemaIR,
rdf: &R,
typing: &mut RefTyping,
parent_alternatives: &[Vec<ExtendAlternative>],
) -> Result<ValidationResult>
where
R: NeighsRDF + QueryRDF,
{
let mut selector = vec![0usize; parent_alternatives.len()];
let mut errors_selections = Vec::new();
loop {
// The current selection: one alternative per parent, merged (buckets deduplicated,
// so an ancestor reached through several parents becomes a single bucket).
let mut sigma = ExtendAlternative::default();
for (i, alts) in parent_alternatives.iter().enumerate() {
sigma = sigma.merge(&alts[selector[i]]);
}
match self.check_node_selection(idx, node, shape, schema, rdf, typing, &sigma)? {
Either::Right(reasons) => {
return pass(Reason::ShapeExtends {
node: node.clone(),
shape: Box::new(shape.clone()),
reasons: Reasons::new(reasons),
});
},
Either::Left(errors) => errors_selections.extend(errors),
}
// Advance to the next selection
let mut level = 0;
loop {
if level == selector.len() {
// All selections failed
return fail(ValidatorError::ShapeFailed {
node: Box::new(node.clone()),
shape: Box::new(shape.clone()),
idx: *idx,
errors: errors_selections,
});
}
selector[level] += 1;
if selector[level] < parent_alternatives[level].len() {
break;
}
selector[level] = 0;
level += 1;
}
}
}
/// Validates the node against one selection: the shape's own triple expression and the
/// selection's bucket shapes must be satisfied by a partition of the selection-local
/// neighbourhood, and the selection's constraints must hold for the node.
fn check_node_selection<R>(
&self,
idx: &ShapeLabelIdx,
node: &Node,
shape: &Shape,
schema: &SchemaIR,
rdf: &R,
typing: &mut RefTyping,
sigma: &ExtendAlternative,
) -> Result<ValidationResult>
where
R: NeighsRDF + QueryRDF,
{
// Partition buckets and matchable predicates are selection-local: an unselected
// ShapeOr branch contributes no candidates, and CLOSED is judged against the
// selected alphabet only.
let mut bucket_exprs: HashMap<Option<ShapeLabelIdx>, Vec<Expr>> = HashMap::new();
bucket_exprs.insert(None, vec![shape.triple_expr().clone()]);
let mut candidate_preds = shape.preds();
for b in sigma.bucket_shapes() {
if let Some(ShapeExpr::Shape(bucket_shape)) = schema.find_shape_idx(b).map(|info| info.expr()) {
bucket_exprs.insert(Some(*b), vec![bucket_shape.triple_expr().clone()]);
for p in bucket_shape.preds() {
if !candidate_preds.contains(&p) {
candidate_preds.push(p);
}
}
}
}
let extra_preds = shape.extra().clone();
for p in &extra_preds {
if !candidate_preds.contains(p) {
candidate_preds.push(p.clone());
}
}
let (values, reminder) = self.neighs(node, candidate_preds.clone(), rdf)?;
if shape.is_closed() && !reminder.is_empty() {
return fail(ValidatorError::ClosedShapeWithRemainderPreds {
remainder: Preds::new(reminder),
declared: Preds::new(candidate_preds),
});
}
let values_ctx: Vec<_> = values
.iter()
.map(|(p, v)| (p.clone(), v.clone(), SemanticActionContext::triple(node, p, v)))
.filter(|(pred, value, ctx)| {
let matches_leaf = bucket_exprs.values().any(|rbes| {
rbes.iter().any(|rbe| {
rbe.components().any(|(_, key, cond)| {
&key == pred && (cond_has_ref(&cond) || cond.matches(value, ctx).is_ok())
})
})
});
if matches_leaf {
return true;
}
// EXTRA predicate with no satisfying leaf goes into M^∉; other non-matching
// triples stay so the partition correctly fails (forced assignment).
!extra_preds.contains(pred)
})
.collect();
// Feasibility guard: refute the selection in time linear in the expressions when
// some bucket cannot be satisfied by any assignment of its candidate triples.
// Sound: the per-component candidate counts over-approximate any partition's share.
for rbes in bucket_exprs.values() {
for rbe in rbes {
if !rbe.feasible_neighs(&values_ctx) {
return Ok(Either::Left(vec![ValidatorError::TripleExprRefuted {
node: Box::new(node.clone()),
idx: *idx,
}]));
}
}
}
// The selection's constraint conjuncts must hold for the node
let mut reasons = Vec::new();
for c in sigma.constraints() {
match self.check_node_constraint_expr(c, node, schema, rdf, typing)? {
Either::Left(errors) => {
return Ok(Either::Left(vec![ValidatorError::ShapeExtendsError {
node: Box::new(node.clone()),
shape: Box::new(shape.clone()),
idx: *idx,
extends: *c,
errors: ValidatorErrors::new(errors),
}]));
},
Either::Right(rs) => reasons.extend(rs),
}
}
// Partition search over the selection's buckets
let parts_iter = crate::class_partitions_iter(&values_ctx, &bucket_exprs);
let mut parts_peekable = parts_iter.peekable();
if parts_peekable.peek().is_none() {
return fail(ValidatorError::ShapeFailedNoPartitions {
node: Box::new(node.clone()),
shape: Box::new(shape.clone()),
idx: *idx,
});
}
let mut errors_in_partitions = Vec::new();
for (npart, partition) in parts_peekable.enumerate() {
let partition_display = create_partitions_display(&partition);
let mut ok_partition = true;
let mut errors_in_loop = Vec::new();
let mut reasons_in_loop = Vec::new();
for (maybe_label, rbes, neighs_subset) in partition.iter() {
let result = check_exprs_neigh(rbes, neighs_subset, node, shape, idx, typing)?;
match result {
Either::Right(rs) => {
reasons_in_loop.push(Reason::PartitionComponent {
maybe_label: *maybe_label,
node: node.clone(),
shape: Box::new(shape.clone()),
idx: *idx,
partition_idx: npart,
partition: partition_display.clone(),
neighs: neighs_subset.iter().map(|(p, v, _ctx)| format!("{p} {v}")).join(", "),
reasons: Reasons::new(rs),
});
},
Either::Left(errs) => {
errors_in_loop.push(ValidatorError::PartitionComponentFailed {
maybe_label: *maybe_label,
node: Box::new(node.clone()),
shape: Box::new(shape.clone()),
idx: *idx,
partition_idx: npart,
partition: partition_display.clone(),
neighs: neighs_subset.iter().map(|(p, v, _ctx)| format!("{p} {v}")).join(", "),
errors: ValidatorErrors::new(errs),
});
ok_partition = false;
break;
},
}
}
if ok_partition {
reasons.extend(reasons_in_loop);
return Ok(Either::Right(reasons));
} else {
errors_in_partitions.push(ValidatorError::PartitionFailed {
node: Box::new(node.clone()),
shape: Box::new(shape.clone()),
idx: *idx,
partition: partition_display.clone(),
errors: ValidatorErrors::new(errors_in_loop),
});
}
}
Ok(Either::Left(errors_in_partitions))
}
/// Evaluates a conjunct constraint of a selection directly, without relying on
/// precomputed typing for anonymous sub-expressions.
fn check_node_constraint_expr<R>(
&self,
cidx: &ShapeLabelIdx,
node: &Node,
schema: &SchemaIR,
rdf: &R,
typing: &mut RefTyping,
) -> Result<ValidationResult>
where
R: NeighsRDF + QueryRDF,
{
let Some(se) = schema.find_shape_idx(cidx).map(|info| info.expr().clone()) else {
return pass(Reason::Empty { node: node.clone() });
};
match &se {
ShapeExpr::NodeConstraint(nc) => {
let ctx = SemanticActionContext::subject(node);
match nc.cond().matches(node, &ctx) {
Ok(_pending) => pass(Reason::NodeConstraint {
node: node.clone(),
nc: (**nc).clone(),
}),
Err(err) => fail(ValidatorError::RbeError(err)),
}
},
ShapeExpr::Shape(s) => {
if s.extends().is_empty() {
self.check_node_shape(cidx, node, s, schema, rdf, typing)
} else {
self.check_node_shape_extends(cidx, node, s, schema, rdf, typing)
}
},
ShapeExpr::ShapeNot { expr } => match self.check_node_constraint_expr(expr, node, schema, rdf, typing)? {
Either::Left(errors) => pass(Reason::ShapeNot {
node: node.clone(),
shape_expr: se.clone(),
errors_evidences: ValidatorErrors::new(errors),
}),
Either::Right(rs) => fail(ValidatorError::ShapeNotError {
node: Box::new(node.clone()),
shape_expr: Box::new(se.clone()),
reasons: Reasons::new(rs),
}),
},
ShapeExpr::ShapeAnd { exprs } => {
let mut reasons = Vec::new();
for e in exprs {
match self.check_node_constraint_expr(e, node, schema, rdf, typing)? {
Either::Left(errors) => return Ok(Either::Left(errors)),
Either::Right(rs) => reasons.extend(rs),
}
}
Ok(Either::Right(reasons))
},
ShapeExpr::ShapeOr { exprs } => {
let mut errors = Vec::new();
for e in exprs {
match self.check_node_constraint_expr(e, node, schema, rdf, typing)? {
Either::Right(reasons) => return Ok(Either::Right(reasons)),
Either::Left(errs) => errors.extend(errs),
}
}
Ok(Either::Left(errors))
},
ShapeExpr::Ref { idx } => self.check_node_constraint_expr(idx, node, schema, rdf, typing),
ShapeExpr::Empty => pass(Reason::Empty { node: node.clone() }),
ShapeExpr::External {} => self.check_node_shape_expr(cidx, node, &se, schema, rdf, typing),
}
}
fn check_node_extends_main_shape<R>(
&self,
node: &Node,
idx: &ShapeLabelIdx,
shape: &Shape,
schema: &SchemaIR,
rdf: &R,
typing: &mut RefTyping,
) -> Result<ValidationResult>
where
R: QueryRDF + NeighsRDF,
{
tracing::debug!("Checking node {node} against main shape {idx}, shape: {shape}");
if let Some((ncs, maybe_main_shape, rest_exprs)) = schema.get_main_shape_constraints(idx) {
tracing::debug!(
"Main shape {idx} has node constraints: [{}], main shape: {}, rest exprs: [{}]",
ncs.iter().map(|nc| nc.to_string()).join(", "),
maybe_main_shape
.clone()
.map(|ms| ms.to_string())
.unwrap_or("None".to_string()),
rest_exprs.iter().map(|e| e.to_string()).join(", ")
);
let mut errors = Vec::new();
let mut reasons = Vec::new();
if let Some(main_shape) = maybe_main_shape {
// For shapes with their own triple expression alongside extends (e.g.,
// EXTENDS @<Parent> { <p> [4] }), validate the triple expression using
// exhaustive semantics: pre-filter triples to those matching any component's
// value condition, then check cardinality. This handles open shapes correctly
// since extra values that don't match any constraint are simply ignored.
// Skip this check for shapes with shape-reference value conditions (MatchCond::Ref),
// which require the partition algorithm to correctly distribute triples.
let main_preds = main_shape.preds();
let no_shape_refs = main_shape
.triple_expr()
.components()
.all(|(_, _, cond)| !cond_has_ref(&cond));
if !main_preds.is_empty() && no_shape_refs {
let (all_values, _) = self.neighs(node, main_preds, rdf)?;
let all_values_ctx: Vec<_> = all_values
.iter()
.map(|(p, v)| {
(
p.clone(),
v.clone(),
SemanticActionContext::triple(node, p, v)
.with_registry(schema.semantic_actions_registry_arc()),
)
})
.collect();
// Keep only triples where the value satisfies some component's condition
let filtered: Vec<_> = all_values_ctx
.iter()
.filter(|(pred, value, ctx)| {
main_shape
.triple_expr()
.components()
.any(|(_, key, cond)| &key == pred && cond.matches(value, ctx).is_ok())
})
.cloned()
.collect();
match check_expr_neigh(main_shape.triple_expr(), &filtered, node, shape, idx, typing)? {
Either::Left(errs) => {
errors.push(ValidatorError::ParentShapeMainShapeFailed {
node: Box::new(node.clone()),
shape: Box::new(main_shape.clone()),
idx: *idx,
errors: ValidatorErrors::new(errs),
});
},
Either::Right(rs) => {
reasons.push(Reason::ParentShapeMainShapePassed {
node: node.clone(),
shape: Box::new(main_shape.clone()),
idx: *idx,
reasons: Reasons::new(rs),
});
},
}
}
// Recursively check node constraints and triple expressions through the
// ancestor extends chain.
for e in main_shape.extends() {
match self.check_node_extends_main_shape(node, e, &main_shape, schema, rdf, typing)? {
Either::Left(errs) => {
errors.push(ValidatorError::ParentShapeMainShapeFailed {
node: Box::new(node.clone()),
shape: Box::new(main_shape.clone()),
idx: *idx,
errors: ValidatorErrors::new(errs),
});
},
Either::Right(rs) => {
reasons.push(Reason::ParentShapeMainShapePassed {
node: node.clone(),
shape: Box::new(main_shape.clone()),
idx: *idx,
reasons: Reasons::new(rs),
});
},
}
}
}
// We also validate the node constraints of the main shape
for nc in ncs {
match nc.cond().matches(
node,
&SemanticActionContext::subject(node).with_registry(schema.semantic_actions_registry_arc()),
) {
Ok(_pending) => {
reasons.push(Reason::ParentShapeNodeConstraint {
node: node.clone(),
idx: *idx,
nc: nc.clone(),
});
},
Err(error) => {
errors.push(ValidatorError::ParentShapeNodeConstraintFailed {
node: Box::new(node.clone()),
idx: *idx,
nc: Box::new(nc.clone()),
error: error.to_string(),
});
},
}
}
// Validate NodeConstraints from ShapeRef AND-components (e.g. @<C> in a ShapeAnd).
// Triple constraints in the referenced shape are already included in the partition's
// triple_exprs (via get_triple_exprs → Ref.get_triple_exprs), so only the
// NodeConstraint parts need to be checked here.
for rest_se in &rest_exprs {
if let ShapeExpr::Ref { idx: ref_idx } = rest_se
&& let Some((ref_ncs, _, _)) = schema.get_main_shape_constraints(ref_idx)
{
for nc in ref_ncs {
match nc.cond().matches(
node,
&SemanticActionContext::subject(node).with_registry(schema.semantic_actions_registry_arc()),
) {
Ok(_) => {},
Err(error) => {
errors.push(ValidatorError::ParentShapeNodeConstraintFailed {
node: Box::new(node.clone()),
idx: *ref_idx,
nc: Box::new(nc.clone()),
error: error.to_string(),
});
},
}
}
}
}
if errors.is_empty() {
pass(Reason::ParentShapePassed {
node: node.clone(),
idx: *idx,
reasons: Reasons::new(reasons),
})
} else {
fail(ValidatorError::ParentShapeFailed {
node: Box::new(node.clone()),
idx: *idx,
errors: ValidatorErrors::new(errors),
})
}
} else {
tracing::error!("No info for shape {idx}. This is non-extendable");
fail(ValidatorError::ShapeExtendsNoMainShape {
idx: *idx,
node: Box::new(node.clone()),
})
}
}
fn cnv_iri<S>(&self, iri: S::IRI) -> Pred
where
S: NeighsRDF,
{
let iri_string = iri.as_str();
let iri_s = iri!(iri_string);
Pred::from(iri_s)
}
fn cnv_inverse_iri<S>(&self, iri: S::IRI) -> Pred
where
S: NeighsRDF,
{
let iri_string = iri.as_str();
let iri_s = iri!(iri_string);
Pred::new(iri_s, false)
}
fn cnv_object<S>(&self, term: &S::Term) -> Result<Node>
where
S: NeighsRDF,
{
let obj = term
.clone()
.try_into()
.map_err(|_| ValidatorError::TermToRDFNodeFailed {
term: format!("{term}"),
})?;
Ok(Node::from(obj))
}
/// Get the neighbours of a node for a list of predicates
/// Returns a tuple (values, remainder) where:
/// - values is a list of pairs (pred, node) where pred.is_direct() determines direction:
/// if true, node is the outgoing object; if false, node is the incoming subject
/// - remainder is the list of outgoing predicates on the node not present in preds as direct predicates
pub(crate) fn neighs<S>(&self, node: &Node, preds: Vec<Pred>, rdf: &S) -> Result<Neighs>
where
S: NeighsRDF,
{
let node_term = self.get_rdf_node(node, rdf);
let (outgoing_preds, incoming_preds): (Vec<_>, Vec<_>) = preds.iter().partition(|pred| pred.is_direct());
let outgoing_iris: Vec<S::IRI> = outgoing_preds.iter().map(|p| p.iri().clone().into()).collect();
let incoming_iris: Vec<S::IRI> = incoming_preds.iter().map(|p| p.iri().clone().into()).collect();
let mut result = Vec::new();
let mut reminder_preds = Vec::new();
if let Ok(subject) = S::term_as_subject(&node_term) {
let (outgoing_arcs, reminder) = rdf
.outgoing_arcs_from_list(&subject, &outgoing_iris)
.map_err(|e| self.cnv_err::<S>(e))?;
for (pred, values) in outgoing_arcs.into_iter() {
for obj in values.into_iter() {
let pred_s = self.cnv_iri::<S>(pred.clone());
let object = self.cnv_object::<S>(&obj)?;
result.push((pred_s, object))
}
}
for r in reminder {
let iri_r = self.cnv_iri::<S>(r.clone());
reminder_preds.push(iri_r);
}
}
if !incoming_iris.is_empty() {
let incoming_arcs = rdf
.incoming_arcs_from_list(&node_term, &incoming_iris)
.map_err(|e| self.cnv_err::<S>(e))?;
for (pred, subjects) in incoming_arcs.into_iter() {
for subj in subjects.into_iter() {
let pred_s = self.cnv_inverse_iri::<S>(pred.clone());
let subject_term: S::Term = subj.into();
let subject_node = self.cnv_object::<S>(&subject_term)?;
result.push((pred_s, subject_node))
}
}
}
Ok((result, reminder_preds))
}
fn cnv_err<S>(&self, err: S::Err) -> ValidatorError
where
S: NeighsRDF,
{
tracing::trace!("cnv_err: {err}");
ValidatorError::SRDFError { error: err.to_string() }
}
fn get_rdf_node<S>(&self, node: &Node, _rdf: &S) -> S::Term
where
S: NeighsRDF,
{
match node.as_object() {
Object::Iri(iri_s) => {
let iri: S::IRI = iri_s.clone().into();
iri.into().into()
},
Object::BlankNode(id) => {
let bnode: S::BNode = BlankNode::new(id);
bnode.into()
},
Object::Literal(lit) => {
let lit: S::Literal = lit.clone().into();
let term: S::Term = lit.into();
term
},
Object::Triple { .. } => todo!(),
}
}
pub fn insert_pending(&mut self, atom: &Atom) {
self.pending.insert((*atom).clone());
}
}
/// Recursively collects every `Expr` (triple-expression RbeTable) reachable from `idx` by
/// traversing ShapeAnd components, ShapeRef references, and extends chains. This gives the
/// complete set of conditions declared anywhere in the shape hierarchy rooted at `idx`, which
/// is used to decide whether a particular triple is "covered" by the extends chain (and thus
/// handled by check_node_extends_main_shape's exhaustive semantics) or truly undeclared.
fn collect_all_exprs_for_shape(
idx: &ShapeLabelIdx,
schema: &SchemaIR,
visited: &mut HashSet<ShapeLabelIdx>,
) -> Vec<Expr> {
if !visited.insert(*idx) {
return vec![];
}
if let Some(info) = schema.find_shape_idx(idx) {
match info.expr() {
ShapeExpr::Shape(shape) => {
let mut exprs = vec![shape.triple_expr().clone()];
for e in shape.extends() {
exprs.extend(collect_all_exprs_for_shape(e, schema, visited));
}
exprs
},
ShapeExpr::ShapeAnd { exprs } => exprs
.iter()
.flat_map(|e| collect_all_exprs_for_shape(e, schema, visited))
.collect(),
ShapeExpr::Ref { idx: ref_idx } => collect_all_exprs_for_shape(ref_idx, schema, visited),
ShapeExpr::NodeConstraint(_) | ShapeExpr::Empty | ShapeExpr::External {} => vec![],
_ => vec![],
}
} else {
vec![]
}
}
/// When a shape S extends parents P1, P2, … and some Pi is a transitive ancestor of Pj,
/// the partition would create separate buckets for Pi and Pj. Because each triple can only
/// go to one bucket, a triple that must satisfy BOTH Pi's and Pj's constraints would fail
/// (diamond inheritance).
///
/// Fix: merge the triple expressions of every "covered" parent (one that is a transitive
/// ancestor of another parent in the set) into the bucket of its leaf descendant. Only
/// leaf parents (those not subsumed by any other parent in the set) become partition buckets.
fn merge_ancestor_exprs(
triple_exprs: HashMap<Option<ShapeLabelIdx>, Vec<Expr>>,
schema: &SchemaIR,
) -> HashMap<Option<ShapeLabelIdx>, Vec<Expr>> {
let all_parents: Vec<ShapeLabelIdx> = triple_exprs.keys().filter_map(|k| *k).collect();
if all_parents.len() <= 1 {
return triple_exprs;
}
// For each parent, collect its transitive ancestors.
let parent_ancestors: Vec<(ShapeLabelIdx, HashSet<ShapeLabelIdx>)> = all_parents
.iter()
.map(|&pi| {
let ancestors: HashSet<ShapeLabelIdx> = schema.parents(&pi).into_iter().collect();
(pi, ancestors)
})
.collect();
// A parent pj is "covered" if some other parent pi has pj as a transitive ancestor
// (i.e. pi extends pj directly or transitively).
let mut covered: HashSet<ShapeLabelIdx> = HashSet::new();
for (pi, pi_ancestors) in &parent_ancestors {
for &pj in &all_parents {
if pj != *pi && pi_ancestors.contains(&pj) {
covered.insert(pj);
}
}
}
if covered.is_empty() {
return triple_exprs;
}
let mut result: HashMap<Option<ShapeLabelIdx>, Vec<Expr>> = HashMap::new();
// Keep the None (own shape) entry unchanged.
if let Some(exprs) = triple_exprs.get(&None) {
result.insert(None, exprs.clone());
}
// For each uncovered (leaf) parent, collect its own exprs plus the exprs of every
// covered ancestor that is reachable from it.
for (pi, pi_ancestors) in &parent_ancestors {
if !covered.contains(pi) {
let mut merged = triple_exprs.get(&Some(*pi)).cloned().unwrap_or_default();
for &pj in &all_parents {
if covered.contains(&pj)
&& pi_ancestors.contains(&pj)
&& let Some(pj_exprs) = triple_exprs.get(&Some(pj))
{
merged.extend(pj_exprs.iter().cloned());
}
}
result.insert(Some(*pi), merged);
}
}
result
}
fn pass(reason: Reason) -> Result<ValidationResult> {
Ok(Either::Right(vec![reason]))
}
fn fail(err: ValidatorError) -> Result<ValidationResult> {
Ok(Either::Left(vec![err]))
}
fn check_exprs_neigh(
exprs: &[Expr],
neighs: &[(Pred, Node, SemanticActionContext)],
node: &Node,
shape: &Shape,
idx: &ShapeLabelIdx,
typing: &RefTyping,
) -> Result<ValidationResult> {
for rbe in exprs.iter() {
let result = check_expr_neigh(rbe, neighs, node, shape, idx, typing)?;
if result.is_left() {
return fail(ValidatorError::ShapeFailed {
node: Box::new(node.clone()),
shape: Box::new(shape.clone()),
idx: *idx,
errors: result.left().unwrap().clone(),
});
}
}
pass(Reason::Shape {
node: node.clone(),
shape: Box::new(shape.clone()),
idx: *idx,
})
}
fn check_expr_neigh(
expr: &Expr,
neighs: &[(Pred, Node, SemanticActionContext)],
node: &Node,
shape: &Shape,
idx: &ShapeLabelIdx,
typing: &RefTyping,
) -> Result<ValidationResult> {
/*trace!(
"Checking expr {} with neighs: [{}]",
expr,
neighs.iter().map(|(p, o, _ctx)| format!("{p} {o}")).join(", ")
);*/
let mut result_iter = expr.matches(neighs.to_vec())?;
let first_result = result_iter.next();
if first_result.is_none() {
/*debug!(
"expr {expr} produced no candidates for neighs: [{}]",
neighs.iter().map(|(p, o, _ctx)| format!("{p} {o}")).join(", ")
);*/
let mut reasons: Vec<NoMatchReason> = result_iter
.failed_candidates()
.iter()
.map(|(candidate, predicate, value, error)| NoMatchReason::ConditionFailed {
candidate: candidate.clone(),
predicate: predicate.clone(),
value: value.clone(),
error: error.clone(),
})
.collect();
for (candidate, err) in result_iter.failed_cardinality() {
match expr.cardinality_violations(err) {
Ok(violations) => {
for (predicate, expected, current) in violations {
reasons.push(NoMatchReason::CardinalityFailed {
candidate: candidate.clone(),
predicate,
expected,
current,
});
}
},
Err(detail) => reasons.push(NoMatchReason::Other {
candidate: candidate.clone(),
detail,
}),
}
}
return fail(ValidatorError::NoMatchesFound {
node: Box::new(node.clone()),
shape: Box::new(shape.clone()),
idx: *idx,
reasons,
});
}
let mut errors = Vec::new();
for result in first_result.into_iter().chain(result_iter) {
/*trace!(
"Result of {expr} with neighs: {}: {:?}",
neighs.iter().map(|(p, o, _ctx)| format!("{p} {o}")).join(", "),
result
);*/
match result {
Ok(pending_values) => {
if !pending_values.is_empty() {
/*tracing::trace!(
"Pending values for expr {expr} with neighs: [{}]:\n{pending_values}",
neighs.iter().map(|(p, o, _ctx)| format!("{p} {o}")).join(", "),
);*/
let mut failed_pending = Vec::new();
// Check if all pending values are in typing
for (n, idx, ks) in pending_values.iter_vr() {
let pair = (n.clone(), *idx);
if !typing.contains(&pair) {
/*tracing::trace!(
"Pending value ({},{}) is not in typing, keys: [{}]",
n.clone(),
*idx,
ks.iter().map(|k| k.to_string()).join(", ")
);*/
failed_pending.push((
n.clone(),
*idx,
ks.iter().cloned().collect::<Vec<_>>(),
typing.errors_for(&pair),
))
// TODO: if (stop_at_first) break
// We don't need to compute all the failed pending values once we find the first pair
}
}
if failed_pending.is_empty() {
//tracing::trace!("All pending values were in typing {pending_values}");
return pass(Reason::Shape {
node: node.clone(),
shape: Box::new(shape.clone()),
idx: *idx,
// TODO: Add pending_values to reason
});
} else {
/*tracing::trace!(
"Failed pending values: {}",
failed_pending
.iter()
.map(|(n, idx, _ks)| format!("{n}@{idx}"))
.join(", ")
);*/
errors.push(ValidatorError::FailedPending {
failed_pending: failed_pending.clone(),
})
}
} else {
// No Pending values
return pass(Reason::Shape {
node: node.clone(),
shape: Box::new(shape.clone()),
idx: *idx,
});
}
},
Err(err) => {
// debug!("Result with error: {err}");
return fail(ValidatorError::RbeError(err));
},
}
}
// If we reach this point, all results have been processed and all of them have pending values that are not in typing, so the shape failed
// We can collect all the failed pending values from all the results and return them as errors
/*debug!(
"expr failed {expr} with neighs: [{}]. No matching found. Errors: [{}]",
neighs.iter().map(|(p, o, _ctx)| format!("{p} {o}")).join(", "),
errors.iter().map(|e| format!("{e}")).join(", ")
);*/
fail(ValidatorError::ShapeFailed {
node: Box::new(node.clone()),
shape: Box::new(shape.clone()),
idx: *idx,
errors,
})
}
type PartitionInfo = (
Option<ShapeLabelIdx>,
Vec<Expr>,
Vec<(Pred, Node, SemanticActionContext)>,
);
/*
fn show_partition(partition: &PartitionInfo) -> String {
let (maybe_label, _rbes, neighs_subset) = partition;
let label_str = maybe_label.map(|l| l.to_string()).unwrap_or("[]".to_string());
let neighs_str = neighs_subset.iter().map(|(p, o, _ctx)| format!("{p} {o}")).join(", ");
format!("{} -> [{}]", label_str, neighs_str)
}
fn show_result(
result: &Either<Vec<ValidatorError>, Vec<Reason>>,
nodes_prefixmap: &PrefixMap,
schema: &SchemaIR,
width: usize,
) -> Result<String> {
match result {
Either::Left(errors) => {
let es: Vec<Result<String>> = errors
.iter()
.map(|e| {
e.show_qualified(nodes_prefixmap, schema, width)
.map_err(ValidatorError::PrefixMapError)
})
.collect();
let vs: Vec<String> = es.into_iter().collect::<Result<Vec<_>>>()?;
Ok(vs.join(", "))
},
Either::Right(reasons) => {
let rs: Vec<Result<String>> = reasons
.iter()
.map(|r| {
r.show_qualified(nodes_prefixmap, schema, width)
.map_err(ValidatorError::PrefixMapError)
})
.collect();
let vs: Vec<String> = rs.into_iter().collect::<Result<Vec<_>>>()?;
Ok(vs.join(", "))
},
}
}*/
fn create_partitions_display(ps: &[PartitionInfo]) -> PartitionsDisplay {
let partitions_display: Vec<PartitionDisplay> = ps
.iter()
.map(|(maybe_label, rbes, neighs_subset)| PartitionDisplay::new(*maybe_label, rbes, neighs_subset))
.collect();
PartitionsDisplay::new(&partitions_display)
}
fn check_start_acts(start_acts: &[SemAct], _node: &Node, _idx: &ShapeLabelIdx, schema: &SchemaIR) -> Result<bool> {
let registry = schema.semantic_actions_registry();
let context = SemanticActionContext::new_start_act_context().with_registry(schema.semantic_actions_registry_arc());
for act in start_acts {
let parameter = act.code().map(|code| code.as_str());
let result = registry.run_action(act.name(), parameter, &context);
if let Err(err) = result {
tracing::error!("Start action {act} failed with error: {err}");
return Ok(false);
}
}
Ok(true)
}
/// Returns true if a MatchCond contains a shape reference (MatchCond::Ref),
/// which means the condition delegates to a shape's typing check rather than
/// directly verifying a value set.
fn cond_has_ref(cond: &MatchCond<Pred, Node, ShapeLabelIdx, SemanticActionContext, CondKind>) -> bool {
match cond {
MatchCond::Ref(_) => true,
MatchCond::And(vs) => vs.iter().any(cond_has_ref),
MatchCond::Single(_) => false,
}
}
/// Returns true if `(pred, value)` satisfies at least one leaf condition in `expr`.
///
/// For `MatchCond::Ref` leaves the value is kept in M^∈ conservatively — the RBE /
/// pending-typing path already handles shape-reference validation correctly.
fn matches_any_leaf(expr: &Expr, pred: &Pred, value: &Node, ctx: &SemanticActionContext) -> bool {
for (_, key, cond) in expr.components() {
if &key != pred {
continue;
}
if cond_has_ref(&cond) || cond.matches(value, ctx).is_ok() {
return true;
}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ValidatorConfig;
use rudof_iri::IriS;
use rudof_rdf::rdf_core::RDFFormat;
use rudof_rdf::rdf_impl::{OxigraphInMemory, ReaderMode};
// :alice :knows :bob .
// :carol :knows :alice .
// :alice :age 30 . (outgoing pred not requested in some tests)
const TEST_GRAPH: &str = r#"
prefix : <http://example.org/>
:alice :knows :bob .
:carol :knows :alice .
:alice :age 30 .
"#;
fn make_graph() -> OxigraphInMemory {
OxigraphInMemory::from_str(TEST_GRAPH, &RDFFormat::Turtle, None, &ReaderMode::Strict).unwrap()
}
fn engine() -> Engine {
Engine::new(&ValidatorConfig::default())
}
fn alice() -> Node {
Node::iri(IriS::new_unchecked("http://example.org/alice"))
}
fn bob() -> Node {
Node::iri(IriS::new_unchecked("http://example.org/bob"))
}
fn carol() -> Node {
Node::iri(IriS::new_unchecked("http://example.org/carol"))
}
fn pred_knows_direct() -> Pred {
Pred::new(IriS::new_unchecked("http://example.org/knows"), true)
}
fn pred_knows_inverse() -> Pred {
Pred::new(IriS::new_unchecked("http://example.org/knows"), false)
}
fn pred_age_direct() -> Pred {
Pred::new(IriS::new_unchecked("http://example.org/age"), true)
}
// Requesting only a direct predicate returns matching outgoing arcs;
// the remainder contains all other outgoing predicates on the node.
#[test]
fn test_neighs_direct_pred() {
let (values, remainder) = engine()
.neighs(&alice(), vec![pred_knows_direct()], &make_graph())
.unwrap();
assert_eq!(values.len(), 1);
assert!(values.contains(&(pred_knows_direct(), bob())));
// :age is an outgoing pred of alice that was not requested
assert_eq!(remainder.len(), 1);
assert!(remainder.contains(&pred_age_direct()));
}
// Requesting only an inverse predicate returns matching incoming arcs
// with is_direct = false; all outgoing preds go to the remainder.
#[test]
fn test_neighs_inverse_pred() {
let (values, remainder) = engine()
.neighs(&alice(), vec![pred_knows_inverse()], &make_graph())
.unwrap();
assert_eq!(values.len(), 1);
assert!(values.contains(&(pred_knows_inverse(), carol())));
// Both :knows and :age are outgoing preds of alice and none were requested directly
assert!(remainder.contains(&pred_knows_direct()));
assert!(remainder.contains(&pred_age_direct()));
}
// Requesting both a direct and an inverse predicate returns both kinds of arcs;
// only outgoing preds not explicitly requested as direct end up in remainder.
#[test]
fn test_neighs_mixed_direct_and_inverse() {
let preds = vec![pred_knows_direct(), pred_knows_inverse()];
let (values, remainder) = engine().neighs(&alice(), preds, &make_graph()).unwrap();
// outgoing :knows → bob + incoming :knows ← carol
assert_eq!(values.len(), 2);
assert!(values.contains(&(pred_knows_direct(), bob())));
assert!(values.contains(&(pred_knows_inverse(), carol())));
// :age was not requested as a direct pred, so it goes to remainder
assert_eq!(remainder.len(), 1);
assert!(remainder.contains(&pred_age_direct()));
}
// When preds is empty the values list is empty and every outgoing pred is in the remainder.
#[test]
fn test_neighs_empty_preds() {
let (values, remainder) = engine().neighs(&alice(), vec![], &make_graph()).unwrap();
assert!(values.is_empty());
assert!(remainder.contains(&pred_knows_direct()));
assert!(remainder.contains(&pred_age_direct()));
}
}