feat(export): 纯函数 Turtle 序列化(schema→owl:Class/DatatypeProperty/ObjectProperty)
Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
1 parent
ba699ded3b
commit
4de8a8dce6
2 files changed
+154
No files matched your search
@@ -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: <http://www.w3.org/2002/07/owl#> .\n")
|
||||
b.WriteString("@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n")
|
||||
b.WriteString("@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\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()
|
||||
}
|
||||
@@ -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:",
|
||||
"<http://example.org/standard-ontology#Person> a owl:Class",
|
||||
`rdfs:label "人物"`,
|
||||
`a owl:DatatypeProperty`,
|
||||
`rdfs:range xsd:string`,
|
||||
`<http://example.org/standard-ontology#knows> a owl:ObjectProperty`,
|
||||
`rdfs:domain <http://example.org/standard-ontology#Person>`,
|
||||
} {
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user