Skip to content

World's first open-source enterprise world model.

Declaring an inverse should not require writing SQL (#179)

WaylandYange2e272f9ad53
diff --git a/web/src/api.ts b/web/src/api.ts
index f6d92f50..32d364e7 100644
--- a/web/src/api.ts
+++ b/web/src/api.ts
@@ -291,7 +291,8 @@ export interface DerivedFact {
object: string;
predicate: string;
/** 靠哪条规则推的 */
- rule: "transitive" | "symmetric";
+ /** 靠哪条规则推的。后两种是 0017 补上的跨谓词规则 */
+ rule: "transitive" | "symmetric" | "inverse" | "sub_property";
valid_from: string | null;
valid_to: string | null;
confidence: number;
@@ -446,7 +447,11 @@ export interface OntologyDefect {
| "transitive_and_functional"
| "subclass_cycle"
| "disjoint_with_ancestor"
- | "inherits_disjoint";
+ | "inherits_disjoint"
+ // 0017 加的三类:都在谓词上,前两类关于逆,第三类是子属性成环
+ | "inverse_of_itself"
+ | "inverse_not_mutual"
+ | "sub_property_cycle";
subject_label: string | null;
other_label: string | null;
path_labels: string[];
@@ -642,6 +647,10 @@ export interface RelationTypeView {
is_symmetric: boolean;
is_asymmetric: boolean;
is_irreflexive: boolean;
+ /** 指向另一个关系的两条:`p⁻¹ = q` 与 `p ⊑ q`。是 id 不是布尔,
+ * 所以界面上是下拉框 */
+ inverse_of: string | null;
+ sub_property_of: string | null;
builtin: boolean;
description: string;
/** relation(宾语是实体)| attribute(宾语是字面值) */
@@ -1193,9 +1202,7 @@ export const api = {
* 那一批,把上限当成规模显示是这个接口从前最误导人的地方 */
total_nodes?: number;
total_edges?: number;
- }>(
- `/api/v1/kbs/${kbId}/graph/overview${limit ? `?limit=${limit}` : ""}`,
- ),
+ }>(`/api/v1/kbs/${kbId}/graph/overview${limit ? `?limit=${limit}` : ""}`),
/** 邻域视图**没有总数**:它本来就只是一小片,说「共 325 个」没有意义。
* 两个字段声明成可选,好让调用方与总览共用一个类型 */
graphNeighborhood: (kbId: string, entityId: string) =>
diff --git a/web/src/i18n/en.ts b/web/src/i18n/en.ts
index d27da455..51868268 100644
--- a/web/src/i18n/en.ts
+++ b/web/src/i18n/en.ts
@@ -47,6 +47,12 @@ export const en = {
"That class is already below this one — the hierarchy would loop.",
bad_lang: "Pick a supported language.",
attr_needs_class: "An attribute has to belong to a class.",
+ attr_has_no_link:
+ "An attribute has no inverse and no super-property — its value is a literal, not something to point back from.",
+ link_target_is_attr:
+ "Pick a relation, not an attribute — an attribute's value is a literal.",
+ sub_property_self: "A relation cannot be its own super-property.",
+ unknown_relation: "That relation is not in this knowledge base.",
entity_name_required: "Name cannot be empty.",
entity_name_too_long: "Name is too long — 100 characters at most.",
unknown_entity_type: "That class is not in this ontology.",
@@ -357,9 +363,11 @@ export const en = {
object_missing: "Relation had no object",
malformed_item: "The model's item did not fit the schema",
truncated_reply: "The model's reply was cut off",
- domain_mismatch: "The subject does not fit the relation, and swapping would not help",
+ domain_mismatch:
+ "The subject does not fit the relation, and swapping would not help",
not_an_entity_name: "That name is a sentence, not a thing",
- direction_corrected: "Subject and object were swapped to match the signature",
+ direction_corrected:
+ "Subject and object were swapped to match the signature",
} as Record<string, string>,
// 来源级重抽:不危险,只是费时费钱——轻确认,文案直说成本与保留项
reExtractSource: "Re-extract",
@@ -660,8 +668,14 @@ export const en = {
`${added} added · ${gone} retracted`,
derivedCapped: (n: number) => `${n} predicate(s) not closed fully`,
close: "Close",
- ruleTransitive: "transitive",
- ruleSymmetric: "symmetric",
+ // 派生边靠哪条规则来的。**四种都要有**——查不到的会退回原始 kind 串,
+ // 而那对读的人没有意义
+ ruleNames: {
+ transitive: "transitive",
+ symmetric: "symmetric",
+ inverse: "inverse",
+ sub_property: "sub-property",
+ } as Record<string, string | undefined>,
historyHint: "How this entity's record changed — and who changed it.",
historyEmpty: "Nothing recorded for this entity yet.",
historyKind: {
@@ -930,6 +944,16 @@ export const en = {
irreflexiveHint: "Nothing can point at itself through this relation.",
axiomConflict:
"Symmetric and asymmetric together hold only for a relation with no facts at all — one of the two is wrong.",
+ noLink: "None",
+ inverseOf: "Inverse",
+ inverseOfHint:
+ "The relation that says the same thing the other way round. Declare it on one side only — the other direction follows.",
+ subPropertyOf: "Super-property",
+ subPropertyOfHint:
+ "The broader relation this one is a special case of. Stating the specific one also states the broader one.",
+ linkMeansInverse: (p: string, q: string) =>
+ `A ${p} B also means B ${q} A.`,
+ linkMeansSuper: (p: string, q: string) => `A ${p} B also means A ${q} B.`,
usage: (n: number) => `${n} in use`,
builtin: "built-in",
save: "Save",
@@ -1181,6 +1205,9 @@ export const en = {
defectCycle: "subClassOf runs in a circle",
defectDisjointAncestor: "Disjoint with its own ancestor",
defectInheritsDisjoint: "Inherits from two disjoint classes",
+ defectInverseSelf: "Its own inverse — say symmetric instead",
+ defectInverseNotMutual: "The inverse does not point back",
+ defectSubPropertyCycle: "subPropertyOf runs in a circle",
defectNeverInstantiable: "no instance can ever satisfy it",
defectFixed: "I fixed the ontology",
defectAccepted: "Leave it",
diff --git a/web/src/i18n/zh.ts b/web/src/i18n/zh.ts
index 28ba6481..121a8518 100644
--- a/web/src/i18n/zh.ts
+++ b/web/src/i18n/zh.ts
@@ -44,6 +44,11 @@ export const zh: Strings = {
parent_cycle: "那个类已经在这个类下面了——层级会成环。",
bad_lang: "选一个支持的语言。",
attr_needs_class: "属性必须挂在某个类上。",
+ attr_has_no_link:
+ "属性没有逆,也没有父属性——它的宾语是字面值,谈不上反过来指回来。",
+ link_target_is_attr: "要选一条关系,不是属性——属性的值是字面量。",
+ sub_property_self: "一条关系不能是自己的父属性。",
+ unknown_relation: "这个知识库里没有这条关系。",
entity_name_required: "名称不能为空。",
entity_name_too_long: "名称太长了——最多 100 个字符。",
unknown_entity_type: "这个类不在当前本体里。",
@@ -609,8 +614,12 @@ export const zh: Strings = {
`新增 ${added} · 作废 ${gone}`,
derivedCapped: (n: number) => `${n} 个谓词没推完`,
close: "关闭",
- ruleTransitive: "传递",
- ruleSymmetric: "对称",
+ ruleNames: {
+ transitive: "传递",
+ symmetric: "对称",
+ inverse: "互逆",
+ sub_property: "子属性",
+ } as Record<string, string | undefined>,
historyHint: "这个实体的记录如何变化——以及是谁改的。",
historyEmpty: "这个实体还没有记录。",
historyKind: {
@@ -854,6 +863,15 @@ export const zh: Strings = {
irreflexiveHint: "任何东西不能通过这条关系指向自己。",
axiomConflict:
"对称与反对称同时成立,只对一条事实都没有的关系为真——两者必有一个写错了。",
+ noLink: "无",
+ inverseOf: "逆关系",
+ inverseOfHint:
+ "反过来说同一件事的那条关系。只需在一侧声明,另一个方向自动成立。",
+ subPropertyOf: "父属性",
+ subPropertyOfHint: "这条关系是谁的特例。说了具体的那条,宽泛的那条也成立。",
+ linkMeansInverse: (p: string, q: string) =>
+ `A ${p} B 同时也是 B ${q} A。`,
+ linkMeansSuper: (p: string, q: string) => `A ${p} B 同时也是 A ${q} B。`,
usage: (n: number) => `${n} 处在用`,
builtin: "内置",
save: "保存",
@@ -1067,6 +1085,9 @@ export const zh: Strings = {
defectCycle: "subClassOf 绕成了环",
defectDisjointAncestor: "与自己的祖先互斥",
defectInheritsDisjoint: "继承了两个互斥的类",
+ defectInverseSelf: "自己是自己的逆——写成「对称」更直白",
+ defectInverseNotMutual: "逆关系没有指回来",
+ defectSubPropertyCycle: "子属性绕成了环",
defectNeverInstantiable: "这个类永远不可能有实例",
defectFixed: "已去本体里改了",
defectAccepted: "先放着",
diff --git a/web/src/pages/Graph.tsx b/web/src/pages/Graph.tsx
index 915a0605..1ec96c23 100644
--- a/web/src/pages/Graph.tsx
+++ b/web/src/pages/Graph.tsx
@@ -2550,10 +2550,9 @@ function EntityPanel({
>();
for (const d of derived) {
const direction = d.subject_id === entityId ? "out" : "in";
- const rule =
- d.rule === "transitive"
- ? S.graph.ruleTransitive
- : S.graph.ruleSymmetric;
+ // 四条规则各有名字。**查不到就退回原始 kind 串**——那对读的人没有
+ // 意义,但比显示成另一条规则的名字诚实
+ const rule = S.graph.ruleNames[d.rule] ?? d.rule;
const key = `${direction}|${d.predicate}|${d.rule}`;
const cur = map.get(key);
if (cur) cur.rows.push(d);
diff --git a/web/src/pages/Ontology.tsx b/web/src/pages/Ontology.tsx
index d9f4ae84..f8cc609f 100644
--- a/web/src/pages/Ontology.tsx
+++ b/web/src/pages/Ontology.tsx
@@ -39,6 +39,7 @@ import {
Pager,
PageTitle,
RAIL_CLS,
+ SearchSelect,
cn,
pageSlice,
} from "../ui";
@@ -355,6 +356,7 @@ export function Ontology() {
kbId={kb.id}
existing={selectedProp}
allTypes={entity_types}
+ allRelations={relations}
onDone={(createdId) => {
if (sel?.kind === "new-relation")
setSel(
@@ -1150,12 +1152,16 @@ function PropertyForm({
kbId,
existing,
allTypes,
+ allRelations,
onDone,
onError,
}: {
kbId: string;
existing: RelationTypeView | null;
allTypes: EntityTypeView[];
+ /** 本库的关系(不含属性)。逆与子属性的下拉从这里取——**属性不在其中**,
+ * 它的宾语是字面值,反过来无从谈起 */
+ allRelations: RelationTypeView[];
onDone: (createdId?: string) => void;
onError: (e: unknown) => void;
}) {
@@ -1174,6 +1180,11 @@ function PropertyForm({
const [irreflexive, setIrreflexive] = useState(
existing?.is_irreflexive ?? false,
);
+ // 同一族的后两条,形状不同:指向另一个关系。空串 = 没声明
+ const [inverseOf, setInverseOf] = useState(existing?.inverse_of ?? "");
+ const [subPropertyOf, setSubPropertyOf] = useState(
+ existing?.sub_property_of ?? "",
+ );
const [description, setDescription] = useState(existing?.description ?? "");
const [domains, setDomains] = useState<string[]>(existing?.domains ?? []);
const [ranges, setRanges] = useState<string[]>(existing?.ranges ?? []);
@@ -1184,6 +1195,28 @@ function PropertyForm({
() => parentOptions(allTypes, undefined),
[allTypes],
);
+ // 两个下拉的选项:本库的其它关系。
+ //
+ // **自己不进列表**,两条都是。子属性指向自己数据库直接拒(那是个环);
+ // 逆指向自己在语义上合法——但它等于 `symmetric`,而那个复选框就在上面,
+ // 在这里提供第二条路只会让人写出 R0 要报的东西。
+ //
+ // 唯一的例外是**当前值就是自己**:OWL 导入进来的可以长这样,从列表里漏掉
+ // 它就会让下拉显示空白,而空白一保存就把已声明的抹了
+ const linkOptions = (current: string) => [
+ { value: "", label: S.ontology.noLink },
+ // 当前值就是自己时把自己补回列表,否则下拉显示空白,
+ // 而空白一保存就把已声明的抹了
+ ...(existing && current === existing.id
+ ? [{ value: existing.id, label: existing.label, hint: existing.key }]
+ : []),
+ ...allRelations
+ .filter((r) => r.id !== existing?.id)
+ .map((r) => ({ value: r.id, label: r.label, hint: r.key })),
+ ];
+ /** 下拉里选中那条的显示名。找不到就回落到 id——宁可难看,不要空着 */
+ const nameOf = (id: string) =>
+ allRelations.find((r) => r.id === id)?.label ?? id;
const toggle = (
set: React.Dispatch<React.SetStateAction<string[]>>,
id: string,
@@ -1201,6 +1234,10 @@ function PropertyForm({
is_symmetric: symmetric,
is_asymmetric: asymmetric,
is_irreflexive: irreflexive,
+ // 空串要变成 null 再送——服务端收 `Option<Uuid>`,
+ // `""` 解不成 UUID,会是一个 422 而不是「清空」
+ inverse_of: inverseOf || null,
+ sub_property_of: subPropertyOf || null,
description,
domains,
ranges,
@@ -1215,6 +1252,10 @@ function PropertyForm({
is_symmetric: symmetric,
is_asymmetric: asymmetric,
is_irreflexive: irreflexive,
+ // 空串要变成 null 再送——服务端收 `Option<Uuid>`,
+ // `""` 解不成 UUID,会是一个 422 而不是「清空」
+ inverse_of: inverseOf || null,
+ sub_property_of: subPropertyOf || null,
description,
domains,
ranges,
@@ -1371,6 +1412,66 @@ function PropertyForm({
{S.ontology.axiomConflict}
</p>
)}
+ {/* 同一组的后两条,只是形状不同:它们指向**另一个关系**,所以是下拉
+ 不是复选框。放在这里而不是单开一节——推理机的四种规则源里,两条是
+ 上面的勾,两条是下面的选,分开会让人以为它们是两回事(0002) */}
+ <div className="mt-3 space-y-2.5 border-t border-white/5 pt-3">
+ {(
+ [
+ [
+ inverseOf,
+ setInverseOf,
+ S.ontology.inverseOf,
+ S.ontology.inverseOfHint,
+ linkOptions(inverseOf),
+ ],
+ [
+ subPropertyOf,
+ setSubPropertyOf,
+ S.ontology.subPropertyOf,
+ S.ontology.subPropertyOfHint,
+ linkOptions(subPropertyOf),
+ ],
+ ] as const
+ ).map(([value, set, title, hint, options], i) => (
+ <div key={i}>
+ <div className="text-[13px] text-neutral-200">{title}</div>
+ <p className="text-[11px] leading-relaxed text-neutral-500 mb-1">
+ {hint}
+ </p>
+ <SearchSelect
+ value={value}
+ onChange={set}
+ options={options}
+ size="sm"
+ className="w-full"
+ placeholder={S.ontology.noLink}
+ />
+ </div>
+ ))}
+ {/* 选了之后当场把话说全。**这两条推出来的事实主宾未必同向**——
+ 逆要对调,子属性不对调,只看名字分不出来,写出来就分得出 */}
+ {(inverseOf || subPropertyOf) && (
+ <div className="text-[11px] leading-relaxed text-neutral-500 space-y-0.5">
+ {inverseOf && (
+ <div>
+ {S.ontology.linkMeansInverse(
+ label.trim() || key || "?",
+ nameOf(inverseOf),
+ )}
+ </div>
+ )}
+ {subPropertyOf && (
+ <div>
+ {S.ontology.linkMeansSuper(
+ label.trim() || key || "?",
+ nameOf(subPropertyOf),
+ )}
+ </div>
+ )}
+ </div>
+ )}
+ </div>
</div>
<div>
<label className={lbl}>{S.ontology.description}</label>
diff --git a/web/src/pages/Review.tsx b/web/src/pages/Review.tsx
index aae04a96..2c975218 100644
--- a/web/src/pages/Review.tsx
+++ b/web/src/pages/Review.tsx
@@ -476,6 +476,9 @@ function DefectRow({
subclass_cycle: S.review.defectCycle,
disjoint_with_ancestor: S.review.defectDisjointAncestor,
inherits_disjoint: S.review.defectInheritsDisjoint,
+ inverse_of_itself: S.review.defectInverseSelf,
+ inverse_not_mutual: S.review.defectInverseNotMutual,
+ sub_property_cycle: S.review.defectSubPropertyCycle,
}[d.kind];
// 后两类的后果值得写出来:不可满足的类不会报错,它只是永远空着
const unsatisfiable =