rdf

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: Apache-2.0 Imports: 8 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#"
	// OWL is the Web Ontology Language namespace; the SysML ontology types a
	// LiteralRational's value owl:real.
	OWL = "http://www.w3.org/2002/07/owl#"
	// 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:"
	// AnnotationJSON is the namespace of the JSON literals Flexo reads a collection
	// from (ANNOTATION_JSON in Namespaces.kt); <AnnotationJSON><key> holds the array.
	AnnotationJSON = "urn:sysmlv2:annotation:json:"
	// 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,
	"json":  AnnotationJSON,
}

DefaultPrefixes are the prefix bindings written on every serialized graph. The labels match the service's own prefix mapping.

Functions

func AnnotateCollections added in v0.5.0

func AnnotateCollections(g *Graph) error

AnnotateCollections adds the annotation literal for every sysml: property a subject states more than once, in triple order; an annotation already present is kept.

func CollectionJSON added in v0.5.0

func CollectionJSON(subject Term, values []Term) (string, error)

CollectionJSON spells the values of subject as the service stores a collection: a JSON array of {"@id": <id>} references and typed primitives.

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 IsAnnotationJSON added in v0.5.0

func IsAnnotationJSON(iri string) bool

IsAnnotationJSON reports whether iri is a collection annotation predicate.

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 ReferenceID added in v0.5.0

func ReferenceID(subject, target Term) string

ReferenceID spells target as the @id of a member of subject: the bare id within the subject's project scope, `<qualifier>:<id>` across scopes (an empty qualifier being the unscoped root), so the id alone names the target exactly.

func ScopeQualifier added in v0.4.3

func ScopeQualifier(org, projectID string) string

ScopeQualifier encodes a project scope as an IRI-safe qualifier. '.' never appears in an encoded id, so distinct scopes give distinct qualifiers.

func ValueJSON added in v0.5.0

func ValueJSON(subject, value Term) (string, error)

ValueJSON spells one value of subject as a collection member; two spellings compare by 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 AnnotationError added in v0.5.0

type AnnotationError struct {
	Subject string // subject IRI
	Key     string // property key: the annotation predicate's local name
	Note    string
}

AnnotationError is a collection annotation the reader cannot take as a collection: not exactly one literal, not the JSON array the service stores, or an array that repeats a member, which no set of triples can hold.

func (*AnnotationError) Error added in v0.5.0

func (e *AnnotationError) Error() string

type CollectionConflictError added in v0.5.0

type CollectionConflictError struct {
	Subject    string   // subject IRI
	Key        string   // property key: the local name of the sysml: predicate
	Triples    []string // members as the typed triples state them, as JSON
	Annotation []string // members as the annotation states them, as JSON
}

CollectionConflictError is a collection stated as typed triples and as a JSON annotation with different members; the reader refuses rather than pick one.

func (*CollectionConflictError) Error added in v0.5.0

func (e *CollectionConflictError) Error() string

type CollectionMember added in v0.5.0

type CollectionMember struct {
	ID      string // @id of a reference member, as ReferenceID spells it; empty for a literal
	Literal Term   // typed literal of a primitive member
}

CollectionMember is one parsed annotation member: a reference by id, or a literal.

func ParseCollectionJSON added in v0.5.0

func ParseCollectionJSON(text string) ([]CollectionMember, error)

ParseCollectionJSON reads a collection annotation: a JSON array of {"@id": …} objects and primitives, which is all the service stores.

func (CollectionMember) JSON added in v0.5.0

func (m CollectionMember) JSON() (string, error)

JSON spells the member back as it stands in the annotation.

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 ReconcileCollections added in v0.5.0

func ReconcileCollections(graph *Graph) (*Graph, error)

ReconcileCollections returns a graph whose sysml: triples state every annotated collection in annotation order: materialized if annotation-only, checked if both. A graph without annotations is returned as it is.

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) Has added in v0.5.0

func (g *Graph) Has(t Triple) bool

Has reports whether the graph contains t.

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 AnnotationJSONTerm added in v0.5.0

func AnnotationJSONTerm(key string) Term

AnnotationJSONTerm returns the annotation predicate carrying key's collection.

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 ElementIRIForID added in v0.4.3

func ElementIRIForID(id string) Term

ElementIRIForID returns the element IRI carrying an effective id directly: an annotated id when the element declares one, an encoded name otherwise.

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 OwningMembershipIRIOf added in v0.4.3

func OwningMembershipIRIOf(member Term) Term

OwningMembershipIRIOf derives the membership IRI from the member's own subject IRI, so it inherits the member's effective id and scope qualifier.

func ReferenceIRI added in v0.5.0

func ReferenceIRI(subject Term, id string) Term

ReferenceIRI is the element IRI an @id names from subject: the scope it spells, else the subject's own.

func ScopedElementIRIForID added in v0.4.3

func ScopedElementIRIForID(qualifier, id string) Term

ScopedElementIRIForID qualifies an element IRI with its project scope, for a graph carrying more than one scope, whose ids may legitimately repeat across scopes. The id still follows the last ':', so LocalName returns it.

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