| 1 | //! 包与包之间的同名词处置。 |
| 2 | //! |
| 3 | //! 导入撞 key 时,`owl_import` 的默认判断是 [`Disposition::KeyTaken`]——跳过并报告, |
| 4 | //! 不自动加后缀。那条理由("重导入认不出自己上次建的是哪个")针对的是**猜**出来的 |
| 5 | //! 后缀;本模块给的是**声明**的处置,重导入结果一样,所以不受那条约束。 |
| 6 | //! |
| 7 | //! 为什么需要它:预制包两两撞名约 20 处,而且分两类—— |
| 8 | //! |
| 9 | //! - `org:Organization` 与 `schema:Organization` 是**同一个东西**,跳过是对的, |
| 10 | //! 但不该报成"冲突"让用户去裁一件没得裁的事 |
| 11 | //! - `org:role`(组织里的职位)与 `schema:role`(演员饰演的角色)**只是同名**, |
| 12 | //! 跳过等于丢掉 W3C Org 存在的理由 |
| 13 | //! |
| 14 | //! 只覆盖预制包。用户手动导入的词汇表不在这里——那是明确的意图,撞名该报给他看。 |
| 15 | |
| 16 | /// 同名词的处置。 |
| 17 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 18 | pub enum Alignment { |
| 19 | /// 同义:已有的那个就是它,不必再建。跳过,但记作"已对齐"而非"冲突" |
| 20 | SameAs, |
| 21 | /// 同名不同义:改用这个 key 建出来 |
| 22 | Rename(&'static str), |
| 23 | } |
| 24 | |
| 25 | /// (进来的 IRI, 已占位的 IRI) → 处置。 |
| 26 | /// |
| 27 | /// 上游自己声明过的对齐优先抄:W3C Org 的文档声明了与 FOAF 的对应, |
| 28 | /// PROV-O 声明了与 FOAF、Dublin Core 的对应。这里只记我们的包之间实际会撞的那些。 |
| 29 | const TABLE: &[(&str, &str, Alignment)] = &[ |
| 30 | // ── W3C Org × schema.org ────────────────────────────────────────── |
| 31 | ( |
| 32 | "http://www.w3.org/ns/org#Organization", |
| 33 | "https://schema.org/Organization", |
| 34 | Alignment::SameAs, |
| 35 | ), |
| 36 | ( |
| 37 | "http://www.w3.org/ns/org#identifier", |
| 38 | "https://schema.org/identifier", |
| 39 | Alignment::SameAs, |
| 40 | ), |
| 41 | ( |
| 42 | "http://www.w3.org/ns/org#location", |
| 43 | "https://schema.org/location", |
| 44 | Alignment::SameAs, |
| 45 | ), |
| 46 | // org:Role 是"职位所承载的角色"(与 Post、Membership 配套), |
| 47 | // schema:role 是创作作品里的饰演关系。同名,无关 |
| 48 | ( |
| 49 | "http://www.w3.org/ns/org#Role", |
| 50 | "https://schema.org/Role", |
| 51 | Alignment::Rename("org_role"), |
| 52 | ), |
| 53 | // org:member 是带任期的成员关系(经由 Membership 具体化), |
| 54 | // schema:member 是泛指的从属。粒度不同,两个都要 |
| 55 | ( |
| 56 | "http://www.w3.org/ns/org#member", |
| 57 | "https://schema.org/member", |
| 58 | Alignment::Rename("org_member"), |
| 59 | ), |
| 60 | ( |
| 61 | "http://www.w3.org/ns/org#memberOf", |
| 62 | "https://schema.org/memberOf", |
| 63 | Alignment::Rename("org_member_of"), |
| 64 | ), |
| 65 | // ── PROV-O × schema.org ─────────────────────────────────────────── |
| 66 | // prov:Agent 是 Person 与 Organization 的**超类**,不是同义词。 |
| 67 | // 判成 SameAs 会把"施事者"这个抽象层整个抹掉 |
| 68 | ( |
| 69 | "http://www.w3.org/ns/prov#Agent", |
| 70 | "https://schema.org/agent", |
| 71 | Alignment::Rename("prov_agent"), |
| 72 | ), |
| 73 | // ── FOAF × schema.org ───────────────────────────────────────────── |
| 74 | ( |
| 75 | "http://xmlns.com/foaf/0.1/Person", |
| 76 | "https://schema.org/Person", |
| 77 | Alignment::SameAs, |
| 78 | ), |
| 79 | ( |
| 80 | "http://xmlns.com/foaf/0.1/Organization", |
| 81 | "https://schema.org/Organization", |
| 82 | Alignment::SameAs, |
| 83 | ), |
| 84 | ( |
| 85 | "http://xmlns.com/foaf/0.1/Project", |
| 86 | "https://schema.org/Project", |
| 87 | Alignment::SameAs, |
| 88 | ), |
| 89 | ( |
| 90 | "http://xmlns.com/foaf/0.1/name", |
| 91 | "https://schema.org/name", |
| 92 | Alignment::SameAs, |
| 93 | ), |
| 94 | ( |
| 95 | "http://xmlns.com/foaf/0.1/knows", |
| 96 | "https://schema.org/knows", |
| 97 | Alignment::SameAs, |
| 98 | ), |
| 99 | ( |
| 100 | "http://xmlns.com/foaf/0.1/givenName", |
| 101 | "https://schema.org/givenName", |
| 102 | Alignment::SameAs, |
| 103 | ), |
| 104 | ( |
| 105 | "http://xmlns.com/foaf/0.1/familyName", |
| 106 | "https://schema.org/familyName", |
| 107 | Alignment::SameAs, |
| 108 | ), |
| 109 | ( |
| 110 | "http://xmlns.com/foaf/0.1/gender", |
| 111 | "https://schema.org/gender", |
| 112 | Alignment::SameAs, |
| 113 | ), |
| 114 | ( |
| 115 | "http://xmlns.com/foaf/0.1/logo", |
| 116 | "https://schema.org/logo", |
| 117 | Alignment::SameAs, |
| 118 | ), |
| 119 | ( |
| 120 | "http://xmlns.com/foaf/0.1/thumbnail", |
| 121 | "https://schema.org/thumbnail", |
| 122 | Alignment::SameAs, |
| 123 | ), |
| 124 | ( |
| 125 | "http://xmlns.com/foaf/0.1/title", |
| 126 | "https://schema.org/title", |
| 127 | Alignment::SameAs, |
| 128 | ), |
| 129 | ( |
| 130 | "http://xmlns.com/foaf/0.1/member", |
| 131 | "https://schema.org/member", |
| 132 | Alignment::SameAs, |
| 133 | ), |
| 134 | // foaf:Agent 与 prov:Agent 同义(PROV-O 官方就是这么对齐的), |
| 135 | // 但都不等于 schema:agent |
| 136 | ( |
| 137 | "http://xmlns.com/foaf/0.1/Agent", |
| 138 | "https://schema.org/agent", |
| 139 | Alignment::Rename("foaf_agent"), |
| 140 | ), |
| 141 | // foaf:status 是即时通讯时代的在线状态,schema:status 是订单/动作状态 |
| 142 | ( |
| 143 | "http://xmlns.com/foaf/0.1/status", |
| 144 | "https://schema.org/status", |
| 145 | Alignment::Rename("foaf_status"), |
| 146 | ), |
| 147 | ]; |
| 148 | |
| 149 | /// 撞名时查这张表。两个 IRI 都不在预制包里就返回 `None`,走原来的 `KeyTaken`。 |
| 150 | pub fn lookup(incoming_iri: &str, existing_iri: &str) -> Option<Alignment> { |
| 151 | TABLE |
| 152 | .iter() |
| 153 | .find(|(a, b, _)| *a == incoming_iri && *b == existing_iri) |
| 154 | .map(|(_, _, al)| *al) |
| 155 | } |
| 156 | |
| 157 | #[cfg(test)] |
| 158 | mod tests { |
| 159 | use super::*; |
| 160 | |
| 161 | // 命名空间前缀只在测试里拼 IRI 用;表里写的是完整 IRI, |
| 162 | // 因为那是要跟投影结果逐字符比对的东西,拼接会掩盖笔误 |
| 163 | const SCHEMA: &str = "https://schema.org/"; |
| 164 | const ORG: &str = "http://www.w3.org/ns/org#"; |
| 165 | const PROV: &str = "http://www.w3.org/ns/prov#"; |
| 166 | const FOAF: &str = "http://xmlns.com/foaf/0.1/"; |
| 167 | |
| 168 | #[test] |
| 169 | fn same_as_and_rename_are_both_reachable() { |
| 170 | assert_eq!( |
| 171 | lookup( |
| 172 | &format!("{ORG}Organization"), |
| 173 | &format!("{SCHEMA}Organization") |
| 174 | ), |
| 175 | Some(Alignment::SameAs) |
| 176 | ); |
| 177 | assert_eq!( |
| 178 | lookup(&format!("{ORG}Role"), &format!("{SCHEMA}Role")), |
| 179 | Some(Alignment::Rename("org_role")) |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | /// 不在表里的组合必须落回 `KeyTaken`——**默认是报告冲突,不是猜** |
| 184 | #[test] |
| 185 | fn unknown_pairs_fall_through() { |
| 186 | assert_eq!( |
| 187 | lookup("http://example.com/a#Foo", &format!("{SCHEMA}Foo")), |
| 188 | None |
| 189 | ); |
| 190 | assert_eq!( |
| 191 | lookup(&format!("{PROV}Entity"), "http://example.com/b#Entity"), |
| 192 | None |
| 193 | ); |
| 194 | } |
| 195 | |
| 196 | /// 方向敏感:表是 (进来的, 已占的),反过来查不到。 |
| 197 | /// 装包顺序不同时命中的是不同的条目,不该靠对称性蒙混 |
| 198 | #[test] |
| 199 | fn lookup_is_directional() { |
| 200 | assert!(lookup( |
| 201 | &format!("{SCHEMA}Organization"), |
| 202 | &format!("{ORG}Organization") |
| 203 | ) |
| 204 | .is_none()); |
| 205 | } |
| 206 | |
| 207 | /// Rename 出来的 key 必须过 `validate_key` 那套约束:小写字母数字下划线、不超 40 |
| 208 | #[test] |
| 209 | fn renamed_keys_are_valid() { |
| 210 | for (_, _, al) in TABLE { |
| 211 | if let Alignment::Rename(k) = al { |
| 212 | assert!(k.len() <= 40, "{k} 超过 40 字符"); |
| 213 | assert!( |
| 214 | k.chars() |
| 215 | .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_'), |
| 216 | "{k} 含非法字符" |
| 217 | ); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | #[test] |
| 223 | fn no_duplicate_pairs() { |
| 224 | let mut seen: Vec<(&str, &str)> = TABLE.iter().map(|(a, b, _)| (*a, *b)).collect(); |
| 225 | let n = seen.len(); |
| 226 | seen.sort_unstable(); |
| 227 | seen.dedup(); |
| 228 | assert_eq!(seen.len(), n, "对齐表里有重复的 (进来, 已占) 组合"); |
| 229 | } |
| 230 | |
| 231 | /// FOAF 常量在测试外没被引用,这里确认它拼对了 |
| 232 | #[test] |
| 233 | fn foaf_namespace_is_used() { |
| 234 | assert!(lookup(&format!("{FOAF}Person"), &format!("{SCHEMA}Person")).is_some()); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /// 对齐表里的 IRI 必须真的出现在包里。 |
| 239 | /// |
| 240 | /// 表是手写的字符串,写错一个字符它就静默失效——撞名照样走 `KeyTaken`, |
| 241 | /// 只是再也不会命中。上游改前缀(schema.org 从 `http://` 迁到 `https://` 就发生过) |
| 242 | /// 也是同一种失效。所以拿真包跑一遍投影,逐条核对。 |
| 243 | #[cfg(test)] |
| 244 | mod against_real_packs { |
| 245 | use super::*; |
| 246 | use std::collections::HashSet; |
| 247 | use utopia_ingest::ontology_rdf::{project, RdfFormat}; |
| 248 | |
| 249 | /// 把所有预制包投影一遍,收集出现过的 IRI |
| 250 | fn all_iris() -> HashSet<String> { |
| 251 | let mut out = HashSet::new(); |
| 252 | for p in crate::ontology_packs::PACKS { |
| 253 | let bytes = crate::ontology_packs::bytes(p).expect(p.id); |
| 254 | let fmt = RdfFormat::detect(p.filename, &bytes); |
| 255 | let proj = project(&bytes, fmt).unwrap_or_else(|e| panic!("{} 投影失败:{e}", p.id)); |
| 256 | out.extend(proj.classes.iter().map(|c| c.iri.clone())); |
| 257 | out.extend(proj.properties.iter().map(|p| p.iri.clone())); |
| 258 | } |
| 259 | out |
| 260 | } |
| 261 | |
| 262 | #[test] |
| 263 | fn every_alignment_iri_exists_in_some_pack() { |
| 264 | let iris = all_iris(); |
| 265 | let mut missing = Vec::new(); |
| 266 | for (incoming, existing, _) in TABLE { |
| 267 | if !iris.contains(*incoming) { |
| 268 | missing.push(*incoming); |
| 269 | } |
| 270 | if !iris.contains(*existing) { |
| 271 | missing.push(*existing); |
| 272 | } |
| 273 | } |
| 274 | assert!( |
| 275 | missing.is_empty(), |
| 276 | "对齐表里这些 IRI 在任何预制包里都找不到,表已失效:\n {}", |
| 277 | missing.join("\n ") |
| 278 | ); |
| 279 | } |
| 280 | } |