rdf

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 28, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package rdf provides the minimal RDF graph model, Turtle writer and Turtle reader that the SysML v2 ↔ RDF conversion in internal/core/export is built on.

The model is deliberately small: an ordered list of triples over IRIs and literals. Blank nodes, RDF collections and named graphs are not represented at all, so a Turtle document that uses them is rejected with a diagnostic rather than silently losing data — a converted model that quietly dropped statements would be worse than one that refused to convert.

Index

Constants

View Source
const (
	// RDFNS is the RDF syntax namespace.
	RDFNS = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	// XSD is the XML Schema datatype namespace.
	XSD = "http://www.w3.org/2001/XMLSchema#"
	// SysML is the OMG SysML vocabulary namespace, used for metaclasses and
	// metamodel properties.
	SysML = "https://www.omg.org/spec/SysML#"
	// Element is the base IRI for the elements of a converted model.
	Element = "urn:sysmlv2:element:"
	// OpenSysML namespaces the few properties this tool needs that the SysML
	// metamodel does not define: declaration order, and the source text of
	// expressions. They are namespaced separately so a consumer can tell our
	// additions from the standard vocabulary and ignore them.
	OpenSysML = "urn:opensysml:sysml:"
	// Expression is the base IRI for the nodes of an expression graph: the
	// subexpressions of a value, bound, guard or condition, which are elements
	// of the abstract syntax without a qualified name of their own.
	Expression = "urn:opensysml:expr:"
	// LegacyExtension is the extension namespace this tool wrote before the
	// project was renamed. It is recognized only so a graph carrying it can be
	// refused rather than read as if its properties were absent.
	LegacyExtension = "urn:systemica:sysml:"
)

Namespace IRIs. The SysML vocabulary, element base and prefix labels match the ones the Flexo MMS SysML v2 service writes into its triplestore (Namespaces.kt), so a graph produced here loads into that service and a graph read out of it converts back to source here.

View Source
const ExpressionPrefix = "expr"

ExpressionPrefix is the prefix label bound to the expression namespace. It is written only on a graph that carries an expression graph.

View Source
const RDFType = RDFNS + "type"

RDFType is the rdf:type predicate IRI.

Variables

View Source
var DefaultPrefixes = map[string]string{
	"rdf":   RDFNS,
	"xsd":   XSD,
	"sysml": SysML,
	"elmt":  Element,
	"sysx":  OpenSysML,
}

DefaultPrefixes are the prefix bindings written on every serialized graph.

Functions

func DecodeElementID added in v0.1.1

func DecodeElementID(id string) (string, bool)

DecodeElementID reverses EncodeElementID, reporting whether id is a well-formed encoding. A malformed escape, a byte outside [A-Za-z0-9_-] or an invalid UTF-8 result is rejected rather than guessed at.

func DecodeExpressionNodeID added in v0.3.0

func DecodeExpressionNodeID(id string) (string, []string, bool)

DecodeExpressionNodeID reverses ExpressionNodeID, returning the qualified name of the element the node belongs to and the positions leading down to it. An encoded id can hold `_p` itself — `::p` encodes to `__p` — so the owner is the shortest prefix before a separator that is a well-formed id on its own; a `_p` inside an encoded id leaves a prefix ending in a lone '_', which never is.

func DecodeOwningMembershipID added in v0.3.0

func DecodeOwningMembershipID(id string) (string, bool)

DecodeOwningMembershipID reverses OwningMembershipID, returning the qualified name of the member the membership owns.

func EncodeElementID added in v0.1.1

func EncodeElementID(qualifiedName string) string

EncodeElementID encodes a qualified name as an element id in [A-Za-z0-9_-]+: `::` becomes `__`, a byte in [A-Za-z0-9-] stands for itself, and every other byte — a literal '_' included — becomes '_' plus two lowercase hex digits.

func ExpressionNodeID added in v0.3.0

func ExpressionNodeID(owner, position string) string

ExpressionNodeID returns the id of the expression node at position under the element or outer node with id owner. Positions nest, so the ids compose, and the result stays in [A-Za-z0-9_-]+ so a consumer restricting ids to that can address the node.

func LocalName

func LocalName(iri string) string

LocalName returns the part of iri after the last '#', '/' or ':' — the metaclass or property name for a vocabulary IRI, or the encoded id for an element IRI.

func OwningMembershipID added in v0.3.0

func OwningMembershipID(memberQualifiedName string) string

OwningMembershipID returns the id of the OwningMembership that owns the member with the given qualified name. The membership sits between exactly one namespace and one member, so the member's own identity determines it.

func WriteTurtle

func WriteTurtle(g *Graph) []byte

WriteTurtle serializes g as a Turtle document: the prefix bindings the graph carries, then one block per subject in insertion order, with the subject's predicates grouped and separated by ';'.

Types

type Graph

type Graph struct {

	// Prefixes maps prefix label to namespace IRI for serialization. It never
	// affects the meaning of the graph.
	Prefixes map[string]string
	// contains filtered or unexported fields
}

Graph is an ordered set of triples. Insertion order is preserved so that a serialized document is stable, and duplicates are dropped so that repeated conversion of the same model is idempotent.

func NewGraph

func NewGraph() *Graph

NewGraph returns an empty graph carrying the SysML prefix bindings.

func ParseTurtle

func ParseTurtle(data []byte) (*Graph, error)

ParseTurtle reads the subset of Turtle that WriteTurtle emits: @prefix and @base directives, IRI and prefixed-name terms, quoted literals with optional datatype or language tag, the `a` keyword, and the ';' and ',' groupings.

Blank nodes ('[]', '_:x'), collections ('( ... )') and the numeric and boolean literal shorthands are rejected rather than approximated: this reader exists to convert a graph back into source, and a construct it cannot represent has to surface as an error instead of a silently smaller model.

func (*Graph) Add

func (g *Graph) Add(subject, predicate, object Term)

Add appends a triple unless the graph already contains it.

func (*Graph) AddTriple

func (g *Graph) AddTriple(t Triple)

AddTriple appends t unless the graph already contains it.

func (*Graph) BoolValue

func (g *Graph) BoolValue(subject Term, predicate string) bool

BoolValue returns the boolean value of (subject, predicate), defaulting to false when absent. Any lexical form other than "true" or "1" is false, which matches the way the exporter only ever writes true.

func (*Graph) HasProperty added in v0.3.0

func (g *Graph) HasProperty(subject Term, predicate string) bool

HasProperty reports whether the subject states the predicate at all.

func (*Graph) Len

func (g *Graph) Len() int

Len returns the number of triples.

func (*Graph) Lexical

func (g *Graph) Lexical(subject Term, predicate string) (string, bool)

Lexical returns the lexical form of the first object of (subject, predicate), for a property whose value is read as text regardless of whether it was written as an IRI or a literal.

func (*Graph) Object

func (g *Graph) Object(subject Term, predicate string) (Term, bool)

Object returns the first object of (subject, predicate) and whether one exists. A property the mapping treats as single-valued is read with this.

func (*Graph) Objects

func (g *Graph) Objects(subject Term, predicate string) []Term

Objects returns the objects of every (subject, predicate) statement, in insertion order.

func (*Graph) Subjects

func (g *Graph) Subjects() []Term

Subjects returns every distinct subject IRI in insertion order.

func (*Graph) Triples

func (g *Graph) Triples() []Triple

Triples returns the triples in insertion order. The result aliases the graph's storage and must not be mutated.

func (*Graph) Type

func (g *Graph) Type(subject Term) string

Type returns the first rdf:type of subject, or "" when it has none.

type ParseError

type ParseError struct {
	Line int
	Msg  string
}

ParseError reports the line at which a Turtle document could not be read.

func (*ParseError) Error

func (e *ParseError) Error() string

type Term

type Term struct {
	Kind TermKind
	// Value is the IRI for TermIRI, or the lexical form for TermLiteral.
	Value string
	// Datatype is the datatype IRI of a literal. Empty means xsd:string for a
	// plain literal, and is always empty when Lang is set.
	Datatype string
	// Lang is the BCP 47 language tag of a literal, empty when untagged.
	Lang string
}

Term is one RDF node: an IRI or a literal.

func Bool

func Bool(value bool) Term

Bool returns an xsd:boolean literal.

func ElementIRI

func ElementIRI(qualifiedName string) Term

ElementIRI returns the element IRI for a fully-qualified SysML name: the element namespace followed by EncodeElementID of the name, so the id after the last ':' is readable, deterministic, and valid for a consumer that restricts ids to [A-Za-z0-9_-]+.

func ExpressionIRI added in v0.2.0

func ExpressionIRI(owner Term, path string) Term

ExpressionIRI returns the IRI of one node of an expression graph: the id of the term it hangs off — an element or an outer subexpression — with the position it holds, so its identity comes from where it sits in the model rather than being minted, and its id is valid where an element id is.

func IRI

func IRI(iri string) Term

IRI returns an IRI term.

func Int

func Int(value int) Term

Int returns an xsd:integer literal.

func OpenSysMLTerm

func OpenSysMLTerm(name string) Term

OpenSysMLTerm returns the IRI of a name in the OpenSysML extension namespace.

func OwningMembershipIRI added in v0.3.0

func OwningMembershipIRI(qualifiedName string) Term

OwningMembershipIRI returns the IRI of the OwningMembership through which a namespace owns the member named by qualifiedName. It is in the element namespace, since a membership is an element of the abstract syntax in its own right, and its id can never collide with an element's.

func String

func String(value string) Term

String returns a plain string literal.

func SysMLTerm

func SysMLTerm(name string) Term

SysMLTerm returns the IRI of a name in the SysML vocabulary, used for both metaclasses (`sysml:PartUsage`) and properties (`sysml:declaredName`).

func TypedLiteral

func TypedLiteral(value, datatype string) Term

TypedLiteral returns a literal with an explicit datatype IRI.

func (Term) Equal

func (t Term) Equal(other Term) bool

Equal reports whether two terms are identical, including datatype and tag.

func (Term) IsIRI

func (t Term) IsIRI() bool

IsIRI reports whether t is an IRI term.

func (Term) IsLiteral

func (t Term) IsLiteral() bool

IsLiteral reports whether t is a literal term.

func (Term) String

func (t Term) String() string

String renders the term in Turtle-ish form for diagnostics and tests.

type TermKind

type TermKind int

TermKind discriminates the two node kinds this model represents.

const (
	// TermIRI is an absolute IRI reference.
	TermIRI TermKind = iota
	// TermLiteral is a lexical value with an optional datatype IRI or
	// language tag (never both, per RDF 1.1).
	TermLiteral
)

type Triple

type Triple struct {
	Subject   Term
	Predicate Term
	Object    Term
}

Triple is one subject-predicate-object statement. Subjects and predicates are always IRIs in this model.

Directories

Path Synopsis
Package ontology holds the SysML v2 metamodel term table generated from the Open-MBEE OWL rendering, plus the domain/range check built on it.
Package ontology holds the SysML v2 metamodel term table generated from the Open-MBEE OWL rendering, plus the domain/range check built on it.
gen command
Command gen writes internal/core/rdf/ontology/table.go from a local checkout of https://github.com/Open-MBEE/sysmlv2-rdf-ontology, which is not vendored here.
Command gen writes internal/core/rdf/ontology/table.go from a local checkout of https://github.com/Open-MBEE/sysmlv2-rdf-ontology, which is not vendored here.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL