Elffuss BraidHub

kikocis/utopia public

The axioms an ontology states about itself now survive the import (#121)

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Changeset13bbeae640fa839043f362bd28c262e5b3410ff2e48e703b21f97a91f9914c04
AuthorWaylandYang <145302500+WaylandYang@users.noreply.github.com> · 2026-08-31 02:43 +08:00
CommitterGitHub <noreply@github.com> · 2026-08-31 02:43 +08:00
Parents13c1f26cdd05

Browse the files at this changeset

3 files changed

modified Cargo.lock +1 −0
@@ -4778,6 +4778,7 @@
47784778 "chardetng",
47794779 "csv",
47804780 "encoding_rs",
4781+ "flate2",
47814782 "infer",
47824783 "oxrdf",
47834784 "oxrdfxml",
modified crates/utopia-ingest/Cargo.toml +4 −0
@@ -20,3 +20,7 @@
2020 oxrdfxml.workspace = true
2121 oxrdf.workspace = true
2222 tracing.workspace = true
23+
24+[dev-dependencies]
25+# 只给 against_real_packs 用:包是 gz 存的,测试要解开它们
26+flate2.workspace = true
modified crates/utopia-ingest/src/ontology_rdf.rs +210 −2
@@ -20,6 +20,9 @@
2020 pub description: String,
2121 /// `rdfs:subClassOf` 的全部父类 IRI(多继承在这里是常态)
2222 pub parents: Vec<String>,
23+ /// `owl:disjointWith` 的对端 IRI。**两个方向都收**——公理是对称的,
24+ /// 而词表通常只写一遍
25+ pub disjoint_with: Vec<String>,
2326 }
2427
2528 /// 投影出来的一个属性(对象属性 → 关系,数据属性 → 属性)。
@@ -34,6 +37,12 @@
3437 pub is_datatype: bool,
3538 pub functional: bool,
3639 pub inverse_functional: bool,
40+ /// OWL 属性公理。一致性检查(0002 R0)的判定依据:没有它们,
41+ /// `A part_of B` 与 `B part_of A` 同时存在到底是矛盾还是正常,无从判起
42+ pub transitive: bool,
43+ pub symmetric: bool,
44+ pub asymmetric: bool,
45+ pub irreflexive: bool,
3746 pub domains: Vec<String>,
3847 pub ranges: Vec<String>,
3948 /// **多条 range 是并集还是交集**。`rdfs:range` 写多条是交集
@@ -208,6 +217,10 @@
208217 let mut data_props: BTreeSet<String> = BTreeSet::new();
209218 let mut functional: BTreeSet<String> = BTreeSet::new();
210219 let mut inverse_functional: BTreeSet<String> = BTreeSet::new();
220+ let mut transitive: BTreeSet<String> = BTreeSet::new();
221+ let mut symmetric: BTreeSet<String> = BTreeSet::new();
222+ let mut asymmetric: BTreeSet<String> = BTreeSet::new();
223+ let mut irreflexive: BTreeSet<String> = BTreeSet::new();
211224 let mut plain_props: BTreeSet<String> = BTreeSet::new();
212225 let mut datatype_roots: BTreeSet<String> = BTreeSet::new();
213226 for t in &triples {
@@ -239,6 +252,25 @@
239252 x if x == format!("{OWL}InverseFunctionalProperty") => {
240253 inverse_functional.insert(t.subject.clone());
241254 }
255+ // 属性公理:R0 的一致性检查靠它们才谈得上判定。
256+ //
257+ // **五个随包词表只覆盖了其中一半**(TransitiveProperty 14 条、
258+ // SymmetricProperty 1 条,而 Asymmetric 与 Irreflexive 一条没有),
259+ // 但那五个只是冷启动的底座。这条线的终点是把**企业自己的本体**
260+ // 导进来(0001 开篇:FIBO、行业标准、Protégé 自建),那些里
261+ // 反对称与非自反是常见声明。按 OWL 收全,而不是按手上这几个包收。
262+ x if x == format!("{OWL}TransitiveProperty") => {
263+ transitive.insert(t.subject.clone());
264+ }
265+ x if x == format!("{OWL}SymmetricProperty") => {
266+ symmetric.insert(t.subject.clone());
267+ }
268+ x if x == format!("{OWL}AsymmetricProperty") => {
269+ asymmetric.insert(t.subject.clone());
270+ }
271+ x if x == format!("{OWL}IrreflexiveProperty") => {
272+ irreflexive.insert(t.subject.clone());
273+ }
242274 // 词汇表自报的数据类型。这里只收显式声明的那几个根,
243275 // 子类(Integer ⊂ Number、URL ⊂ Text)等父类图收完再往下传。
244276 // **标记类自己也收**:schema:DataType 的声明是 `a rdfs:Class`,
@@ -256,6 +288,7 @@
256288 let mut labels: BTreeMap<String, Vec<(String, Option<String>)>> = BTreeMap::new();
257289 let mut comments: BTreeMap<String, Vec<(String, Option<String>)>> = BTreeMap::new();
258290 let mut parents: BTreeMap<String, Vec<String>> = BTreeMap::new();
291+ let mut disjoint: BTreeMap<String, Vec<String>> = BTreeMap::new();
259292 let mut domains: BTreeMap<String, Vec<String>> = BTreeMap::new();
260293 let mut ranges: BTreeMap<String, Vec<String>> = BTreeMap::new();
261294 // rangeIncludes 用过的属性,它的 range 按并集读
@@ -270,6 +303,7 @@
270303 || p == format!("{RDFS}range")
271304 || is_schema(p, "domainIncludes")
272305 || is_schema(p, "rangeIncludes")
306+ || p == format!("{OWL}disjointWith")
273307 };
274308 for t in &triples {
275309 let p = t.predicate.as_str();
@@ -284,6 +318,20 @@
284318 .or_default()
285319 .push(l.clone());
286320 }
321+ } else if p == format!("{OWL}disjointWith") {
322+ // **两个方向都记。** `owl:disjointWith` 是对称的,而词表通常只写一遍
323+ // (W3C Org 的 Role/Membership/Site/ChangeEvent 四者两两互斥,只写了六行)。
324+ // 只按写的方向存,查"A 与 B 互斥吗"就得看调用方碰巧从哪一头问
325+ if let Some(o) = &t.object_iri {
326+ disjoint
327+ .entry(t.subject.clone())
328+ .or_default()
329+ .push(o.clone());
330+ disjoint
331+ .entry(o.clone())
332+ .or_default()
333+ .push(t.subject.clone());
334+ }
287335 } else if p == format!("{RDFS}subClassOf") {
288336 if let Some(o) = &t.object_iri {
289337 parents
@@ -359,6 +407,13 @@
359407 label: pick_lang(labels.get(iri)).unwrap_or_else(|| local_name(iri).to_string()),
360408 description: pick_lang(comments.get(iri)).unwrap_or_default(),
361409 parents: parents.get(iri).cloned().unwrap_or_default(),
410+ disjoint_with: {
411+ // 去重:两个方向都收之后,词表若两边都写过就会重复
412+ let mut d = disjoint.get(iri).cloned().unwrap_or_default();
413+ d.sort();
414+ d.dedup();
415+ d
416+ },
362417 iri: iri.clone(),
363418 });
364419 }
@@ -392,6 +447,10 @@
392447 is_datatype,
393448 functional: functional.contains(iri),
394449 inverse_functional: inverse_functional.contains(iri),
450+ transitive: transitive.contains(iri),
451+ symmetric: symmetric.contains(iri),
452+ asymmetric: asymmetric.contains(iri),
453+ irreflexive: irreflexive.contains(iri),
395454 domains: domains.get(iri).cloned().unwrap_or_default(),
396455 ranges: rs,
397456 ranges_union: union_ranged.contains(iri),
@@ -798,6 +857,7 @@
798857 rdfs:range ex:Person .
799858 ex:salary a owl:DatatypeProperty ; rdfs:domain ex:Employee .
800859 ex:Employee owl:disjointWith ex:Contractor .
860+ ex:Employee owl:equivalentClass ex:Staff .
801861 "#;
802862
803863 #[test]
@@ -820,10 +880,13 @@
820880 let sal = p.properties.iter().find(|x| x.key == "salary").unwrap();
821881 assert!(sal.is_datatype);
822882
823- // 消费不了的公理进报告,不是丢弃也不是报错
883+ // 消费不了的公理进报告,不是丢弃也不是报错。
884+ //
885+ // `disjointWith` 曾经在这份名单上,现在被消费了(它是一致性检查的判定
886+ // 依据)——所以这里改用一个仍然消费不了的公理来守这条性质本身
824887 assert!(p
825888 .unprojected
826- .contains_key("http://www.w3.org/2002/07/owl#disjointWith"));
889+ .contains_key("http://www.w3.org/2002/07/owl#equivalentClass"));
827890 }
828891
829892 #[test]
@@ -1041,3 +1104,148 @@
10411104 assert!(first_works.iri.starts_with("https://home.example/"));
10421105 }
10431106 }
1107+
1108+#[cfg(test)]
1109+mod axioms {
1110+ use super::*;
1111+
1112+ /// OWL 的属性公理与类互斥都要投影出来——它们是一致性检查(0002 R0)的**判定
1113+ /// 依据**。没有它们,`A part_of B` 与 `B part_of A` 同时成立到底是矛盾还是
1114+ /// 正常,无从判起:`alias_of` 双向是对的,`produces` 双向几乎肯定是错的,
1115+ /// 而区分这两者的东西只能来自本体。
1116+ const AX: &str = r#"
1117+ @prefix owl: <http://www.w3.org/2002/07/owl#> .
1118+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
1119+ @prefix ex: <http://acme.example/ax#> .
1120+
1121+ ex:Person a owl:Class ; owl:disjointWith ex:Organization .
1122+ ex:Organization a owl:Class .
1123+ ex:Document a owl:Class .
1124+
1125+ ex:partOf a owl:ObjectProperty, owl:TransitiveProperty, owl:AsymmetricProperty .
1126+ ex:aliasOf a owl:ObjectProperty, owl:SymmetricProperty .
1127+ ex:reportsTo a owl:ObjectProperty, owl:IrreflexiveProperty .
1128+ ex:plain a owl:ObjectProperty .
1129+ "#;
1130+
1131+ fn proj() -> OwlProjection {
1132+ project(AX.as_bytes(), RdfFormat::Turtle).unwrap()
1133+ }
1134+
1135+ #[test]
1136+ fn property_axioms_survive_the_projection() {
1137+ let p = proj();
1138+ let by = |k: &str| p.properties.iter().find(|x| x.key == k).unwrap().clone();
1139+
1140+ let part_of = by("part_of");
1141+ assert!(part_of.transitive, "TransitiveProperty 要落到属性上");
1142+ assert!(part_of.asymmetric, "AsymmetricProperty 同上");
1143+ assert!(!part_of.symmetric);
1144+
1145+ assert!(
1146+ by("alias_of").symmetric,
1147+ "对称属性双向出现是正确的,不是矛盾"
1148+ );
1149+ assert!(by("reports_to").irreflexive, "非自反:自己汇报给自己是矛盾");
1150+
1151+ // 没声明的一律为假——**默认不是"未知"而是"没这条公理"**。
1152+ // OWL 是开放世界,但一致性检查只能按写下来的判:没写就是没有依据,
1153+ // 而没有依据时不报矛盾,比猜一个公理出来安全
1154+ let plain = by("plain");
1155+ assert!(!plain.transitive && !plain.symmetric);
1156+ assert!(!plain.asymmetric && !plain.irreflexive);
1157+ }
1158+
1159+ /// **两个方向都要有。** 词表通常只写一遍(W3C Org 把四个类两两互斥写成六行),
1160+ /// 只按书写方向存,查"A 与 B 互斥吗"就取决于调用方碰巧从哪一头问。
1161+ #[test]
1162+ fn disjointness_is_recorded_from_both_ends() {
1163+ let p = proj();
1164+ let d = |k: &str| {
1165+ p.classes
1166+ .iter()
1167+ .find(|c| c.key == k)
1168+ .unwrap()
1169+ .disjoint_with
1170+ .clone()
1171+ };
1172+ assert_eq!(d("person"), vec!["http://acme.example/ax#Organization"]);
1173+ assert_eq!(d("organization"), vec!["http://acme.example/ax#Person"]);
1174+ assert!(d("document").is_empty(), "没声明互斥的类不该凭空多出来");
1175+ }
1176+}
1177+
1178+/// 拿**真包**验一遍,而不只是夹具。
1179+///
1180+/// 先例在 `pack_alignment::against_real_packs`:那一版靠真包抓出了四个凭记忆
1181+/// 写错的 IRI。夹具只能证明"我写的 Turtle 我自己解析得了",真包能证明
1182+/// "官方文件里那些写法我们接得住"——两者不是一回事。
1183+#[cfg(test)]
1184+mod against_real_packs {
1185+ use super::*;
1186+ use std::io::Read;
1187+
1188+ fn load(name: &str) -> Vec<u8> {
1189+ let path = concat!(env!("CARGO_MANIFEST_DIR"), "/../utopia-server/packs/");
1190+ let f = std::fs::File::open(format!("{path}{name}")).expect("包文件在");
1191+ let mut out = Vec::new();
1192+ flate2::read::GzDecoder::new(f)
1193+ .read_to_end(&mut out)
1194+ .expect("解压");
1195+ out
1196+ }
1197+
1198+ /// W3C Org 把 Organization / Role / Membership / Site / ChangeEvent 五者两两
1199+ /// 互斥,官方文件里每对只写一次。**五个类每个都该看到四个对端**——只按书写
1200+ /// 方向存的话,先被写到的那几个会缺对端,而缺哪个取决于文件里的行序。
1201+ ///
1202+ /// (写这条断言时我按 grep 目测数成了"四者",真包当场纠正:`Organization`
1203+ /// 也在互斥集里。夹具证明不了这种事。)
1204+ #[test]
1205+ fn w3c_org_declares_four_mutually_disjoint_classes() {
1206+ let p = project(&load("w3c-org.ttl.gz"), RdfFormat::Turtle).unwrap();
1207+ for key in ["organization", "role", "membership", "site", "change_event"] {
1208+ let c = p
1209+ .classes
1210+ .iter()
1211+ .find(|c| c.key == key)
1212+ .unwrap_or_else(|| panic!("{key} 该被投影出来"));
1213+ assert_eq!(
1214+ c.disjoint_with.len(),
1215+ 4,
1216+ "{key} 该与另外四个互斥,实得 {:?}",
1217+ c.disjoint_with
1218+ );
1219+ }
1220+ }
1221+
1222+ /// IOF Core 声明了一批传递属性(before/after/occursDuring…)。
1223+ /// 这是 R0 环检测唯一有真实依据的地方
1224+ #[test]
1225+ fn iof_core_declares_transitive_properties() {
1226+ let p = project(&load("iof-core.rdf.gz"), RdfFormat::RdfXml).unwrap();
1227+ let n = p.properties.iter().filter(|x| x.transitive).count();
1228+ assert!(n >= 8, "IOF 该有一批传递属性,实得 {n}");
1229+ }
1230+
1231+ /// FOAF 的 Person ⊥ Organization / Document —— **最常撞的那一对**。
1232+ /// `classify_type_drift` 里那张手写表(person vs organization 判 Disjoint)
1233+ /// 想表达的就是它,注释里也写着"公理落库后这张表应改从本体读"
1234+ #[test]
1235+ fn foaf_says_a_person_is_not_an_organization() {
1236+ let p = project(&load("foaf.rdf.gz"), RdfFormat::RdfXml).unwrap();
1237+ let person = p
1238+ .classes
1239+ .iter()
1240+ .find(|c| c.key == "person")
1241+ .expect("foaf:Person");
1242+ assert!(
1243+ person
1244+ .disjoint_with
1245+ .iter()
1246+ .any(|d| d.ends_with("Organization")),
1247+ "foaf:Person 该与 Organization 互斥,实得 {:?}",
1248+ person.disjoint_with
1249+ );
1250+ }
1251+}