Elffuss BraidHub

kikocis/utopia public

Declaring an inverse should not require writing SQL (#179)
Changeset0dc0623dcff5dca4cbecf9a3a74314523ae16b992e8a1bab81ce58a3f1ab1b06
AuthorWaylandYang <145302500+WaylandYang@users.noreply.github.com> · 2026-09-02 17:42 +08:00
CommitterGitHub <noreply@github.com> · 2026-09-02 17:42 +08:00
Parents19a371c73146

Browse the files at this changeset

6 files changed

modified web/src/api.ts +12 −5
@@ -291,7 +291,8 @@
291291 object: string;
292292 predicate: string;
293293 /** 靠哪条规则推的 */
294- rule: "transitive" | "symmetric";
294+ /** 靠哪条规则推的。后两种是 0017 补上的跨谓词规则 */
295+ rule: "transitive" | "symmetric" | "inverse" | "sub_property";
295296 valid_from: string | null;
296297 valid_to: string | null;
297298 confidence: number;
@@ -446,7 +447,11 @@
446447 | "transitive_and_functional"
447448 | "subclass_cycle"
448449 | "disjoint_with_ancestor"
449- | "inherits_disjoint";
450+ | "inherits_disjoint"
451+ // 0017 加的三类:都在谓词上,前两类关于逆,第三类是子属性成环
452+ | "inverse_of_itself"
453+ | "inverse_not_mutual"
454+ | "sub_property_cycle";
450455 subject_label: string | null;
451456 other_label: string | null;
452457 path_labels: string[];
@@ -642,6 +647,10 @@
642647 is_symmetric: boolean;
643648 is_asymmetric: boolean;
644649 is_irreflexive: boolean;
650+ /** 指向另一个关系的两条:`p⁻¹ = q` 与 `p ⊑ q`。是 id 不是布尔,
651+ * 所以界面上是下拉框 */
652+ inverse_of: string | null;
653+ sub_property_of: string | null;
645654 builtin: boolean;
646655 description: string;
647656 /** relation(宾语是实体)| attribute(宾语是字面值) */
@@ -1193,9 +1202,7 @@
11931202 * 那一批,把上限当成规模显示是这个接口从前最误导人的地方 */
11941203 total_nodes?: number;
11951204 total_edges?: number;
1196- }>(
1197- `/api/v1/kbs/${kbId}/graph/overview${limit ? `?limit=${limit}` : ""}`,
1198- ),
1205+ }>(`/api/v1/kbs/${kbId}/graph/overview${limit ? `?limit=${limit}` : ""}`),
11991206 /** 邻域视图**没有总数**:它本来就只是一小片,说「共 325 个」没有意义。
12001207 * 两个字段声明成可选,好让调用方与总览共用一个类型 */
12011208 graphNeighborhood: (kbId: string, entityId: string) =>
modified web/src/i18n/en.ts +31 −4
@@ -47,6 +47,12 @@
4747 "That class is already below this one — the hierarchy would loop.",
4848 bad_lang: "Pick a supported language.",
4949 attr_needs_class: "An attribute has to belong to a class.",
50+ attr_has_no_link:
51+ "An attribute has no inverse and no super-property — its value is a literal, not something to point back from.",
52+ link_target_is_attr:
53+ "Pick a relation, not an attribute — an attribute's value is a literal.",
54+ sub_property_self: "A relation cannot be its own super-property.",
55+ unknown_relation: "That relation is not in this knowledge base.",
5056 entity_name_required: "Name cannot be empty.",
5157 entity_name_too_long: "Name is too long — 100 characters at most.",
5258 unknown_entity_type: "That class is not in this ontology.",
@@ -357,9 +363,11 @@
357363 object_missing: "Relation had no object",
358364 malformed_item: "The model's item did not fit the schema",
359365 truncated_reply: "The model's reply was cut off",
360- domain_mismatch: "The subject does not fit the relation, and swapping would not help",
366+ domain_mismatch:
367+ "The subject does not fit the relation, and swapping would not help",
361368 not_an_entity_name: "That name is a sentence, not a thing",
362- direction_corrected: "Subject and object were swapped to match the signature",
369+ direction_corrected:
370+ "Subject and object were swapped to match the signature",
363371 } as Record<string, string>,
364372 // 来源级重抽:不危险,只是费时费钱——轻确认,文案直说成本与保留项
365373 reExtractSource: "Re-extract",
@@ -660,8 +668,14 @@
660668 `${added} added · ${gone} retracted`,
661669 derivedCapped: (n: number) => `${n} predicate(s) not closed fully`,
662670 close: "Close",
663- ruleTransitive: "transitive",
664- ruleSymmetric: "symmetric",
671+ // 派生边靠哪条规则来的。**四种都要有**——查不到的会退回原始 kind 串,
672+ // 而那对读的人没有意义
673+ ruleNames: {
674+ transitive: "transitive",
675+ symmetric: "symmetric",
676+ inverse: "inverse",
677+ sub_property: "sub-property",
678+ } as Record<string, string | undefined>,
665679 historyHint: "How this entity's record changed — and who changed it.",
666680 historyEmpty: "Nothing recorded for this entity yet.",
667681 historyKind: {
@@ -930,6 +944,16 @@
930944 irreflexiveHint: "Nothing can point at itself through this relation.",
931945 axiomConflict:
932946 "Symmetric and asymmetric together hold only for a relation with no facts at all — one of the two is wrong.",
947+ noLink: "None",
948+ inverseOf: "Inverse",
949+ inverseOfHint:
950+ "The relation that says the same thing the other way round. Declare it on one side only — the other direction follows.",
951+ subPropertyOf: "Super-property",
952+ subPropertyOfHint:
953+ "The broader relation this one is a special case of. Stating the specific one also states the broader one.",
954+ linkMeansInverse: (p: string, q: string) =>
955+ `A ${p} B also means B ${q} A.`,
956+ linkMeansSuper: (p: string, q: string) => `A ${p} B also means A ${q} B.`,
933957 usage: (n: number) => `${n} in use`,
934958 builtin: "built-in",
935959 save: "Save",
@@ -1181,6 +1205,9 @@
11811205 defectCycle: "subClassOf runs in a circle",
11821206 defectDisjointAncestor: "Disjoint with its own ancestor",
11831207 defectInheritsDisjoint: "Inherits from two disjoint classes",
1208+ defectInverseSelf: "Its own inverse — say symmetric instead",
1209+ defectInverseNotMutual: "The inverse does not point back",
1210+ defectSubPropertyCycle: "subPropertyOf runs in a circle",
11841211 defectNeverInstantiable: "no instance can ever satisfy it",
11851212 defectFixed: "I fixed the ontology",
11861213 defectAccepted: "Leave it",
modified web/src/i18n/zh.ts +23 −2
@@ -44,6 +44,11 @@
4444 parent_cycle: "那个类已经在这个类下面了——层级会成环。",
4545 bad_lang: "选一个支持的语言。",
4646 attr_needs_class: "属性必须挂在某个类上。",
47+ attr_has_no_link:
48+ "属性没有逆,也没有父属性——它的宾语是字面值,谈不上反过来指回来。",
49+ link_target_is_attr: "要选一条关系,不是属性——属性的值是字面量。",
50+ sub_property_self: "一条关系不能是自己的父属性。",
51+ unknown_relation: "这个知识库里没有这条关系。",
4752 entity_name_required: "名称不能为空。",
4853 entity_name_too_long: "名称太长了——最多 100 个字符。",
4954 unknown_entity_type: "这个类不在当前本体里。",
@@ -609,8 +614,12 @@
609614 `新增 ${added} · 作废 ${gone}`,
610615 derivedCapped: (n: number) => `${n} 个谓词没推完`,
611616 close: "关闭",
612- ruleTransitive: "传递",
613- ruleSymmetric: "对称",
617+ ruleNames: {
618+ transitive: "传递",
619+ symmetric: "对称",
620+ inverse: "互逆",
621+ sub_property: "子属性",
622+ } as Record<string, string | undefined>,
614623 historyHint: "这个实体的记录如何变化——以及是谁改的。",
615624 historyEmpty: "这个实体还没有记录。",
616625 historyKind: {
@@ -854,6 +863,15 @@
854863 irreflexiveHint: "任何东西不能通过这条关系指向自己。",
855864 axiomConflict:
856865 "对称与反对称同时成立,只对一条事实都没有的关系为真——两者必有一个写错了。",
866+ noLink: "无",
867+ inverseOf: "逆关系",
868+ inverseOfHint:
869+ "反过来说同一件事的那条关系。只需在一侧声明,另一个方向自动成立。",
870+ subPropertyOf: "父属性",
871+ subPropertyOfHint: "这条关系是谁的特例。说了具体的那条,宽泛的那条也成立。",
872+ linkMeansInverse: (p: string, q: string) =>
873+ `A ${p} B 同时也是 B ${q} A。`,
874+ linkMeansSuper: (p: string, q: string) => `A ${p} B 同时也是 A ${q} B。`,
857875 usage: (n: number) => `${n} 处在用`,
858876 builtin: "内置",
859877 save: "保存",
@@ -1067,6 +1085,9 @@
10671085 defectCycle: "subClassOf 绕成了环",
10681086 defectDisjointAncestor: "与自己的祖先互斥",
10691087 defectInheritsDisjoint: "继承了两个互斥的类",
1088+ defectInverseSelf: "自己是自己的逆——写成「对称」更直白",
1089+ defectInverseNotMutual: "逆关系没有指回来",
1090+ defectSubPropertyCycle: "子属性绕成了环",
10701091 defectNeverInstantiable: "这个类永远不可能有实例",
10711092 defectFixed: "已去本体里改了",
10721093 defectAccepted: "先放着",
modified web/src/pages/Graph.tsx +3 −4
@@ -2550,10 +2550,9 @@
25502550 >();
25512551 for (const d of derived) {
25522552 const direction = d.subject_id === entityId ? "out" : "in";
2553- const rule =
2554- d.rule === "transitive"
2555- ? S.graph.ruleTransitive
2556- : S.graph.ruleSymmetric;
2553+ // 四条规则各有名字。**查不到就退回原始 kind 串**——那对读的人没有
2554+ // 意义,但比显示成另一条规则的名字诚实
2555+ const rule = S.graph.ruleNames[d.rule] ?? d.rule;
25572556 const key = `${direction}|${d.predicate}|${d.rule}`;
25582557 const cur = map.get(key);
25592558 if (cur) cur.rows.push(d);
modified web/src/pages/Ontology.tsx +101 −0
@@ -39,6 +39,7 @@
3939 Pager,
4040 PageTitle,
4141 RAIL_CLS,
42+ SearchSelect,
4243 cn,
4344 pageSlice,
4445 } from "../ui";
@@ -355,6 +356,7 @@
355356 kbId={kb.id}
356357 existing={selectedProp}
357358 allTypes={entity_types}
359+ allRelations={relations}
358360 onDone={(createdId) => {
359361 if (sel?.kind === "new-relation")
360362 setSel(
@@ -1150,12 +1152,16 @@
11501152 kbId,
11511153 existing,
11521154 allTypes,
1155+ allRelations,
11531156 onDone,
11541157 onError,
11551158 }: {
11561159 kbId: string;
11571160 existing: RelationTypeView | null;
11581161 allTypes: EntityTypeView[];
1162+ /** 本库的关系(不含属性)。逆与子属性的下拉从这里取——**属性不在其中**,
1163+ * 它的宾语是字面值,反过来无从谈起 */
1164+ allRelations: RelationTypeView[];
11591165 onDone: (createdId?: string) => void;
11601166 onError: (e: unknown) => void;
11611167 }) {
@@ -1174,6 +1180,11 @@
11741180 const [irreflexive, setIrreflexive] = useState(
11751181 existing?.is_irreflexive ?? false,
11761182 );
1183+ // 同一族的后两条,形状不同:指向另一个关系。空串 = 没声明
1184+ const [inverseOf, setInverseOf] = useState(existing?.inverse_of ?? "");
1185+ const [subPropertyOf, setSubPropertyOf] = useState(
1186+ existing?.sub_property_of ?? "",
1187+ );
11771188 const [description, setDescription] = useState(existing?.description ?? "");
11781189 const [domains, setDomains] = useState<string[]>(existing?.domains ?? []);
11791190 const [ranges, setRanges] = useState<string[]>(existing?.ranges ?? []);
@@ -1184,6 +1195,28 @@
11841195 () => parentOptions(allTypes, undefined),
11851196 [allTypes],
11861197 );
1198+ // 两个下拉的选项:本库的其它关系。
1199+ //
1200+ // **自己不进列表**,两条都是。子属性指向自己数据库直接拒(那是个环);
1201+ // 逆指向自己在语义上合法——但它等于 `symmetric`,而那个复选框就在上面,
1202+ // 在这里提供第二条路只会让人写出 R0 要报的东西。
1203+ //
1204+ // 唯一的例外是**当前值就是自己**:OWL 导入进来的可以长这样,从列表里漏掉
1205+ // 它就会让下拉显示空白,而空白一保存就把已声明的抹了
1206+ const linkOptions = (current: string) => [
1207+ { value: "", label: S.ontology.noLink },
1208+ // 当前值就是自己时把自己补回列表,否则下拉显示空白,
1209+ // 而空白一保存就把已声明的抹了
1210+ ...(existing && current === existing.id
1211+ ? [{ value: existing.id, label: existing.label, hint: existing.key }]
1212+ : []),
1213+ ...allRelations
1214+ .filter((r) => r.id !== existing?.id)
1215+ .map((r) => ({ value: r.id, label: r.label, hint: r.key })),
1216+ ];
1217+ /** 下拉里选中那条的显示名。找不到就回落到 id——宁可难看,不要空着 */
1218+ const nameOf = (id: string) =>
1219+ allRelations.find((r) => r.id === id)?.label ?? id;
11871220 const toggle = (
11881221 set: React.Dispatch<React.SetStateAction<string[]>>,
11891222 id: string,
@@ -1201,6 +1234,10 @@
12011234 is_symmetric: symmetric,
12021235 is_asymmetric: asymmetric,
12031236 is_irreflexive: irreflexive,
1237+ // 空串要变成 null 再送——服务端收 `Option<Uuid>`,
1238+ // `""` 解不成 UUID,会是一个 422 而不是「清空」
1239+ inverse_of: inverseOf || null,
1240+ sub_property_of: subPropertyOf || null,
12041241 description,
12051242 domains,
12061243 ranges,
@@ -1215,6 +1252,10 @@
12151252 is_symmetric: symmetric,
12161253 is_asymmetric: asymmetric,
12171254 is_irreflexive: irreflexive,
1255+ // 空串要变成 null 再送——服务端收 `Option<Uuid>`,
1256+ // `""` 解不成 UUID,会是一个 422 而不是「清空」
1257+ inverse_of: inverseOf || null,
1258+ sub_property_of: subPropertyOf || null,
12181259 description,
12191260 domains,
12201261 ranges,
@@ -1371,6 +1412,66 @@
13711412 {S.ontology.axiomConflict}
13721413 </p>
13731414 )}
1415+ {/* 同一组的后两条,只是形状不同:它们指向**另一个关系**,所以是下拉
1416+ 不是复选框。放在这里而不是单开一节——推理机的四种规则源里,两条是
1417+ 上面的勾,两条是下面的选,分开会让人以为它们是两回事(0002) */}
1418+ <div className="mt-3 space-y-2.5 border-t border-white/5 pt-3">
1419+ {(
1420+ [
1421+ [
1422+ inverseOf,
1423+ setInverseOf,
1424+ S.ontology.inverseOf,
1425+ S.ontology.inverseOfHint,
1426+ linkOptions(inverseOf),
1427+ ],
1428+ [
1429+ subPropertyOf,
1430+ setSubPropertyOf,
1431+ S.ontology.subPropertyOf,
1432+ S.ontology.subPropertyOfHint,
1433+ linkOptions(subPropertyOf),
1434+ ],
1435+ ] as const
1436+ ).map(([value, set, title, hint, options], i) => (
1437+ <div key={i}>
1438+ <div className="text-[13px] text-neutral-200">{title}</div>
1439+ <p className="text-[11px] leading-relaxed text-neutral-500 mb-1">
1440+ {hint}
1441+ </p>
1442+ <SearchSelect
1443+ value={value}
1444+ onChange={set}
1445+ options={options}
1446+ size="sm"
1447+ className="w-full"
1448+ placeholder={S.ontology.noLink}
1449+ />
1450+ </div>
1451+ ))}
1452+ {/* 选了之后当场把话说全。**这两条推出来的事实主宾未必同向**——
1453+ 逆要对调,子属性不对调,只看名字分不出来,写出来就分得出 */}
1454+ {(inverseOf || subPropertyOf) && (
1455+ <div className="text-[11px] leading-relaxed text-neutral-500 space-y-0.5">
1456+ {inverseOf && (
1457+ <div>
1458+ {S.ontology.linkMeansInverse(
1459+ label.trim() || key || "?",
1460+ nameOf(inverseOf),
1461+ )}
1462+ </div>
1463+ )}
1464+ {subPropertyOf && (
1465+ <div>
1466+ {S.ontology.linkMeansSuper(
1467+ label.trim() || key || "?",
1468+ nameOf(subPropertyOf),
1469+ )}
1470+ </div>
1471+ )}
1472+ </div>
1473+ )}
1474+ </div>
13741475 </div>
13751476 <div>
13761477 <label className={lbl}>{S.ontology.description}</label>
modified web/src/pages/Review.tsx +3 −0
@@ -476,6 +476,9 @@
476476 subclass_cycle: S.review.defectCycle,
477477 disjoint_with_ancestor: S.review.defectDisjointAncestor,
478478 inherits_disjoint: S.review.defectInheritsDisjoint,
479+ inverse_of_itself: S.review.defectInverseSelf,
480+ inverse_not_mutual: S.review.defectInverseNotMutual,
481+ sub_property_cycle: S.review.defectSubPropertyCycle,
479482 }[d.kind];
480483 // 后两类的后果值得写出来:不可满足的类不会报错,它只是永远空着
481484 const unsatisfiable =