Elffuss BraidHub

kikocis/utopia public

dev · 6.9 KiBRawPermalink
1//! 0016 B3:`owl:disjointWith` 进消解——本体声明了互斥的两个类,同名也不进审阅队列。
2//!
3//! 消解判「两个类能不能指同一个东西」有三层:硬表 `CONFUSABLE_TYPE_KEYS`、类层级
4//! (同一支系当易混,#226)、本体声明的互斥。这里守的是**声明优先于前两层**:
5//!
6//! 1. 没声明时行为不变:organization vs project 照硬表进队列,corporation vs
7//! federal_agency 照类层级(共有非根祖先 organization)进队列。
8//! 2. 声明 organization ⟂ project 之后,同名的 organization / project 分开,不进队列。
9//! 3. 声明 corporation ⟂ agency 之后,federal_agency(agency 的子类)跟 corporation
10//! 也分开——**互斥是继承的**,声明在父类上就够。
11//!
12//! 没有 `UTOPIA_DATABASE_URL` 时跳过而不是失败。自建自拆,绝不碰已有的库。
13
14use sqlx::PgPool;
15use utopia_store::{ontology, resolution};
16use uuid::Uuid;
17
18struct Fx {
19 org: Uuid,
20 kb: Uuid,
21 organization: Uuid,
22 project: Uuid,
23 corporation: Uuid,
24 agency: Uuid,
25 federal_agency: Uuid,
26}
27
28async fn seed(pool: &PgPool) -> anyhow::Result<Fx> {
29 let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
30 // 顶上要有一个根:类层级那条规则里「共有的祖先」不算根(schema.org 里万物皆
31 // Thing,算上它 Person 与 Organization 也成了一家),所以 organization 得有父类
32 let (thing, organization, project, corporation, agency, federal_agency) = (
33 Uuid::now_v7(),
34 Uuid::now_v7(),
35 Uuid::now_v7(),
36 Uuid::now_v7(),
37 Uuid::now_v7(),
38 Uuid::now_v7(),
39 );
40 sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, 'disjoint-test')")
41 .bind(org)
42 .execute(pool)
43 .await?;
44 sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, 'disjoint-test')")
45 .bind(ws)
46 .bind(org)
47 .execute(pool)
48 .await?;
49 sqlx::query(
50 "INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, 'disjoint-test')",
51 )
52 .bind(kb)
53 .bind(ws)
54 .execute(pool)
55 .await?;
56 for (id, key) in [
57 (thing, "thing"),
58 (organization, "organization"),
59 (project, "project"),
60 (corporation, "corporation"),
61 (agency, "agency"),
62 (federal_agency, "federal_agency"),
63 ] {
64 sqlx::query("INSERT INTO entity_types (id, kb_id, key, label) VALUES ($1, $2, $3, $3)")
65 .bind(id)
66 .bind(kb)
67 .bind(key)
68 .execute(pool)
69 .await?;
70 }
71 for (child, parent) in [
72 (organization, thing),
73 (project, thing),
74 (corporation, organization),
75 (agency, organization),
76 (federal_agency, agency),
77 ] {
78 sqlx::query("INSERT INTO entity_type_parents (child_id, parent_id) VALUES ($1, $2)")
79 .bind(child)
80 .bind(parent)
81 .execute(pool)
82 .await?;
83 }
84 Ok(Fx {
85 org,
86 kb,
87 organization,
88 project,
89 corporation,
90 agency,
91 federal_agency,
92 })
93}
94
95async fn entity(pool: &PgPool, f: &Fx, name: &str, type_id: Uuid) -> anyhow::Result<Uuid> {
96 let id = Uuid::now_v7();
97 sqlx::query(
98 "INSERT INTO entities (id, kb_id, type_id, canonical_name) VALUES ($1, $2, $3, $4)",
99 )
100 .bind(id)
101 .bind(f.kb)
102 .bind(type_id)
103 .bind(name)
104 .execute(pool)
105 .await?;
106 Ok(id)
107}
108
109/// 消解一条 mention,回它挂上的「类型漂移」审核对指向谁
110async fn drift_reviews(
111 pool: &PgPool,
112 f: &Fx,
113 name: &str,
114 type_id: Uuid,
115) -> anyhow::Result<Vec<Uuid>> {
116 let r = resolution::resolve_mention(pool, f.kb, Some(type_id), name, None).await?;
117 assert!(
118 r.created,
119 "a cross-type same name is a new entity: keep apart, never merge"
120 );
121 Ok(r.reviews
122 .iter()
123 .filter(|x| x.reason.starts_with("type_drift|"))
124 .map(|x| x.other_id)
125 .collect())
126}
127
128#[tokio::test]
129async fn a_declared_disjointness_keeps_names_apart() -> anyhow::Result<()> {
130 let Ok(url) = std::env::var("UTOPIA_DATABASE_URL") else {
131 eprintln!("跳过:未设 UTOPIA_DATABASE_URL");
132 return Ok(());
133 };
134 let pool = PgPool::connect(&url).await?;
135 let f = seed(&pool).await?;
136
137 let run = async {
138 // 1. 没声明:硬表与类层级照旧
139 let orion = entity(&pool, &f, "Orion", f.organization).await?;
140 assert_eq!(
141 drift_reviews(&pool, &f, "Orion", f.project).await?,
142 vec![orion],
143 "organization vs project is confusable by the hard-coded list"
144 );
145 let acme = entity(&pool, &f, "Acme", f.corporation).await?;
146 assert_eq!(
147 drift_reviews(&pool, &f, "Acme", f.federal_agency).await?,
148 vec![acme],
149 "corporation vs federal_agency share the ancestor organization: kin, so Review"
150 );
151
152 // 2. 声明 organization ⟂ project:硬表说易混,本体说互斥——本体赢
153 ontology::set_disjoint_for(&pool, f.kb, f.organization, &[f.project]).await?;
154 let _vega = entity(&pool, &f, "Vega", f.organization).await?;
155 assert!(
156 drift_reviews(&pool, &f, "Vega", f.project)
157 .await?
158 .is_empty(),
159 "a declared disjointness wins over the hard-coded list"
160 );
161
162 // 3. 声明 corporation ⟂ agency:federal_agency 是 agency 的子类,互斥继承下来,
163 // 类层级说一家也不算
164 ontology::set_disjoint_for(&pool, f.kb, f.corporation, &[f.agency]).await?;
165 let _beta = entity(&pool, &f, "Beta", f.corporation).await?;
166 assert!(
167 drift_reviews(&pool, &f, "Beta", f.federal_agency)
168 .await?
169 .is_empty(),
170 "a disjointness declared on the parent reaches the child and wins over kinship"
171 );
172 // 反过来问也一样:表里两个方向各一行,继承沿另一头的祖先链走
173 let _gamma = entity(&pool, &f, "Gamma", f.federal_agency).await?;
174 assert!(
175 drift_reviews(&pool, &f, "Gamma", f.corporation)
176 .await?
177 .is_empty(),
178 "the declaration holds from either side"
179 );
180
181 // 4. 取消声明,回到没声明时的行为——编辑必须能撤
182 ontology::set_disjoint_for(&pool, f.kb, f.corporation, &[]).await?;
183 let delta = entity(&pool, &f, "Delta", f.corporation).await?;
184 assert_eq!(
185 drift_reviews(&pool, &f, "Delta", f.federal_agency).await?,
186 vec![delta],
187 "with the declaration gone, kinship sends the pair to Review again"
188 );
189 Ok::<_, anyhow::Error>(())
190 }
191 .await;
192
193 let _ = sqlx::query("DELETE FROM organizations WHERE id = $1")
194 .bind(f.org)
195 .execute(&pool)
196 .await;
197 run
198}