| 1 | //! 预置本体包:建库时的起点。 |
| 2 | //! |
| 3 | //! 建库时**什么都不铺**:0009 删掉内置实体类、0010 与 `#125` 删掉种子关系、 |
| 4 | //! 0011 把 `mapped_to` 搬去语义层之后,播种机制整个退场。所以这些包不是 |
| 5 | //! 「补充」而是本体的**全部来源**。 |
| 6 | //! |
| 7 | //! 当初那十条种子关系一个类型签名都没有,而抽取提示词是支持签名的—— |
| 8 | //! `- buys_from (employee|team → *)`。没有签名,方向就只能靠散文描述, |
| 9 | //! 而散文约束不了方向。schema.org 的 1521 个属性里 1488 个带 |
| 10 | //! domain + range,方向是**声明的**不是描述的。见 `docs/decisions/0008`。 |
| 11 | //! |
| 12 | //! **文件内嵌进二进制**,不在运行时下载:README 承诺整套系统可以跑在完全离线的 |
| 13 | //! 内网环境,运行时抓取会让这句话失效。原文按 gzip 存放(1.7 MB → 316 KB), |
| 14 | //! 解压在 [`bytes`]。 |
| 15 | |
| 16 | use utopia_core::{AppError, AppResult}; |
| 17 | |
| 18 | /// 一个可选的预置本体。 |
| 19 | /// |
| 20 | /// `classes` / `properties` 是**抓取当天数过的展示数字**,给建库界面用; |
| 21 | /// 真正建了多少以导入返回的 plan 为准——投影只覆盖当下能消费的构造。 |
| 22 | pub struct Pack { |
| 23 | pub id: &'static str, |
| 24 | pub name: &'static str, |
| 25 | pub summary: &'static str, |
| 26 | /// 传给 `owl_import` 的文件名。**格式靠扩展名判定**(`RdfFormat::detect`), |
| 27 | /// 所以这里必须保留真实后缀。 |
| 28 | pub filename: &'static str, |
| 29 | pub classes: u32, |
| 30 | pub properties: u32, |
| 31 | gz: &'static [u8], |
| 32 | } |
| 33 | |
| 34 | pub const PACKS: &[Pack] = &[ |
| 35 | Pack { |
| 36 | id: "schema-org", |
| 37 | name: "schema.org", |
| 38 | summary: "People, organizations, products, events, creative works", |
| 39 | filename: "schema-org.ttl", |
| 40 | classes: 1010, |
| 41 | properties: 1676, |
| 42 | gz: include_bytes!("../packs/schema-org.ttl.gz"), |
| 43 | }, |
| 44 | Pack { |
| 45 | id: "w3c-org", |
| 46 | name: "W3C Org", |
| 47 | summary: "Departments, posts, memberships, reporting lines", |
| 48 | filename: "w3c-org.ttl", |
| 49 | classes: 13, |
| 50 | properties: 34, |
| 51 | gz: include_bytes!("../packs/w3c-org.ttl.gz"), |
| 52 | }, |
| 53 | Pack { |
| 54 | id: "prov-o", |
| 55 | name: "PROV-O", |
| 56 | summary: "Provenance: who produced what, when, from which source", |
| 57 | filename: "prov-o.ttl", |
| 58 | classes: 49, |
| 59 | properties: 69, |
| 60 | gz: include_bytes!("../packs/prov-o.ttl.gz"), |
| 61 | }, |
| 62 | Pack { |
| 63 | id: "foaf", |
| 64 | name: "FOAF", |
| 65 | summary: "People and social relations", |
| 66 | filename: "foaf.rdf", |
| 67 | classes: 12, |
| 68 | properties: 62, |
| 69 | gz: include_bytes!("../packs/foaf.rdf.gz"), |
| 70 | }, |
| 71 | Pack { |
| 72 | id: "iof-core", |
| 73 | name: "IOF Core", |
| 74 | summary: "Industrial manufacturing", |
| 75 | filename: "iof-core.rdf", |
| 76 | classes: 294, |
| 77 | properties: 75, |
| 78 | gz: include_bytes!("../packs/iof-core.rdf.gz"), |
| 79 | }, |
| 80 | ]; |
| 81 | |
| 82 | pub fn get(id: &str) -> Option<&'static Pack> { |
| 83 | PACKS.iter().find(|p| p.id == id) |
| 84 | } |
| 85 | |
| 86 | /// 解压出原文。**每次调用都解一遍**——建库是低频动作,不值得为它常驻 1.7 MB。 |
| 87 | pub fn bytes(pack: &Pack) -> AppResult<Vec<u8>> { |
| 88 | use std::io::Read; |
| 89 | let mut out = Vec::new(); |
| 90 | flate2::read::GzDecoder::new(pack.gz) |
| 91 | .read_to_end(&mut out) |
| 92 | .map_err(|e| AppError::Other(anyhow::anyhow!("本体包 {} 解压失败:{e}", pack.id)))?; |
| 93 | Ok(out) |
| 94 | } |
| 95 | |
| 96 | #[cfg(test)] |
| 97 | mod tests { |
| 98 | use super::*; |
| 99 | |
| 100 | /// 每个包都能解压,且解出来的不是空文件。 |
| 101 | /// `include_bytes!` 保证文件存在,但保证不了它是有效的 gzip。 |
| 102 | #[test] |
| 103 | fn every_pack_decompresses() { |
| 104 | for p in PACKS { |
| 105 | let b = bytes(p).unwrap_or_else(|e| panic!("{}: {e}", p.id)); |
| 106 | assert!(b.len() > 10_000, "{} 解出来只有 {} 字节", p.id, b.len()); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /// 文件名后缀决定格式判定,写错了整个包会被当成另一种语法送进解析器。 |
| 111 | #[test] |
| 112 | fn filenames_carry_a_format_suffix() { |
| 113 | for p in PACKS { |
| 114 | assert!( |
| 115 | p.filename.ends_with(".ttl") || p.filename.ends_with(".rdf"), |
| 116 | "{} 的文件名没有可判定的后缀:{}", |
| 117 | p.id, |
| 118 | p.filename |
| 119 | ); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | #[test] |
| 124 | fn ids_are_unique() { |
| 125 | let mut ids: Vec<_> = PACKS.iter().map(|p| p.id).collect(); |
| 126 | ids.sort_unstable(); |
| 127 | let n = ids.len(); |
| 128 | ids.dedup(); |
| 129 | assert_eq!(ids.len(), n, "包 id 有重复"); |
| 130 | } |
| 131 | } |