From 4de8a8dce6a9aedbcc285010ab3d2dfd4126c7b3 Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 20 Sep 2026 15:56:37 +0800 Subject: [PATCH] =?UTF-8?q?feat(export):=20=E7=BA=AF=E5=87=BD=E6=95=B0=20T?= =?UTF-8?q?urtle=20=E5=BA=8F=E5=88=97=E5=8C=96(schema=E2=86=92owl:Class/Da?= =?UTF-8?q?tatypeProperty/ObjectProperty)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.8 --- server/ontology_export.go | 97 ++++++++++++++++++++++++++++++++++ server/ontology_export_test.go | 57 ++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 server/ontology_export.go create mode 100644 server/ontology_export_test.go diff --git a/server/ontology_export.go b/server/ontology_export.go new file mode 100644 index 0000000..e9ede04 --- /dev/null +++ b/server/ontology_export.go @@ -0,0 +1,97 @@ +package main + +import ( + "fmt" + "strings" +) + +// 本体 schema 的 W3C Turtle 导出(GB/T 48000.3-2026 §5.3:标准序列化存储和交换)。 +// 单向导出:平台是运行时权威,Turtle 只作交换/审查格式,不做导入。 + +type schemaPropExport struct { + IRI, APIName, DisplayName, Description, DataType string +} + +type schemaTypeExport struct { + IRI, APIName, DisplayName, Description string + Properties []schemaPropExport +} + +type schemaLinkExport struct { + IRI, APIName, DisplayName, Description, SourceIRI, TargetIRI string +} + +type schemaSnapshot struct { + BaseIRI string + Types []schemaTypeExport + Links []schemaLinkExport +} + +// turtleRange 平台 data_type → XSD 词表;未识别类型兜底 xsd:string(导出永不失败原则)。 +func turtleRange(dataType string) string { + switch dataType { + case "integer": + return "xsd:integer" + case "number": + return "xsd:decimal" + case "boolean": + return "xsd:boolean" + case "date": + return "xsd:date" + case "datetime": + return "xsd:dateTime" + default: // string / text / enum / json / geoshape / ... + return "xsd:string" + } +} + +// turtleLiteral 用 Go %q 生成 Turtle 合法字面量: +// \" \\ \n 转义与 Turtle 兼容;中文按原字符保留(%q 不转义可打印 Unicode)。 +func turtleLiteral(s string) string { return fmt.Sprintf("%q", s) } + +// buildTurtle 把 schema 快照序列化为 Turtle 文本。 +// 映射:ObjectType→owl:Class;数据 Property→owl:DatatypeProperty(domain=所属类); +// LinkType→owl:ObjectProperty(domain/range=两端类)。 +func buildTurtle(s schemaSnapshot) string { + var b strings.Builder + b.WriteString("@prefix owl: .\n") + b.WriteString("@prefix rdfs: .\n") + b.WriteString("@prefix xsd: .\n\n") + ont := strings.TrimSuffix(s.BaseIRI, "#") + fmt.Fprintf(&b, "<%s> a owl:Ontology ; rdfs:label \"ontology-platform schema export\" .\n\n", ont) + + for _, t := range s.Types { + fmt.Fprintf(&b, "<%s> a owl:Class ;\n", t.IRI) + fmt.Fprintf(&b, " rdfs:label %s", turtleLiteral(t.DisplayName)) + if t.Description != "" { + fmt.Fprintf(&b, " ;\n rdfs:comment %s", turtleLiteral(t.Description)) + } + b.WriteString(" .\n") + for _, p := range t.Properties { + fmt.Fprintf(&b, "<%s> a owl:DatatypeProperty ;\n", p.IRI) + fmt.Fprintf(&b, " rdfs:domain <%s> ;\n", t.IRI) + fmt.Fprintf(&b, " rdfs:range %s", turtleRange(p.DataType)) + if p.DisplayName != "" { + fmt.Fprintf(&b, " ;\n rdfs:label %s", turtleLiteral(p.DisplayName)) + } + if p.Description != "" { + fmt.Fprintf(&b, " ;\n rdfs:comment %s", turtleLiteral(p.Description)) + } + b.WriteString(" .\n") + } + b.WriteString("\n") + } + for _, l := range s.Links { + fmt.Fprintf(&b, "<%s> a owl:ObjectProperty ;\n", l.IRI) + fmt.Fprintf(&b, " rdfs:domain <%s> ;\n", l.SourceIRI) + fmt.Fprintf(&b, " rdfs:range <%s>", l.TargetIRI) + if l.DisplayName != "" { + fmt.Fprintf(&b, " ;\n rdfs:label %s", turtleLiteral(l.DisplayName)) + } + if l.Description != "" { + fmt.Fprintf(&b, " ;\n rdfs:comment %s", turtleLiteral(l.Description)) + } + b.WriteString(" .\n") + } + return b.String() +} diff --git a/server/ontology_export_test.go b/server/ontology_export_test.go new file mode 100644 index 0000000..8902754 --- /dev/null +++ b/server/ontology_export_test.go @@ -0,0 +1,57 @@ +package main + +import ( + "strings" + "testing" +) + +func TestTurtleRange(t *testing.T) { + want := map[string]string{ + "string": "xsd:string", "text": "xsd:string", "enum": "xsd:string", "json": "xsd:string", + "integer": "xsd:integer", "number": "xsd:decimal", + "boolean": "xsd:boolean", "date": "xsd:date", "datetime": "xsd:dateTime", + "未知类型": "xsd:string", // 兜底 + } + for dt, w := range want { + if got := turtleRange(dt); got != w { + t.Errorf("turtleRange(%q)=%q want %q", dt, got, w) + } + } +} + +func TestBuildTurtle(t *testing.T) { + s := schemaSnapshot{ + BaseIRI: "http://example.org/standard-ontology#", + Types: []schemaTypeExport{{ + IRI: "http://example.org/standard-ontology#Person", APIName: "Person", + DisplayName: "人物", Description: `带"引号` + "\n换行", + Properties: []schemaPropExport{{ + IRI: "http://example.org/standard-ontology#name", APIName: "name", + DisplayName: "姓名", DataType: "string", + }}, + }}, + Links: []schemaLinkExport{{ + IRI: "http://example.org/standard-ontology#knows", APIName: "knows", + DisplayName: "认识", SourceIRI: "http://example.org/standard-ontology#Person", + TargetIRI: "http://example.org/standard-ontology#Person", + }}, + } + out := buildTurtle(s) + for _, frag := range []string{ + "@prefix owl:", "@prefix rdfs:", "@prefix xsd:", + " a owl:Class", + `rdfs:label "人物"`, + `a owl:DatatypeProperty`, + `rdfs:range xsd:string`, + ` a owl:ObjectProperty`, + `rdfs:domain `, + } { + if !strings.Contains(out, frag) { + t.Errorf("输出缺少片段 %q\n---\n%s", frag, out) + } + } + // 引号与换行必须转义,不能产生裸断行 + if strings.Contains(out, `"带"引号`) || strings.Contains(out, "引号\n换行\"") { + t.Errorf("字面量未转义:\n%s", out) + } +}