98 lines
3.1 KiB
Go
98 lines
3.1 KiB
Go
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()
|
|
}
|