export

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 28 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 export saves a SysML v2 model to a file and converts between the two representations OpenSysML can write: SysML textual notation and RDF Turtle. It also reads a third, SysML v1 in XMI, which is migrated to v2 notation on the way in.

SysML output

Saving a model that came from source writes that source, re-indented by internal/syntax/format. Printing the AST instead would drop comments, notes and anything the parser recorded as an ErrorNode, so a save has to keep the token stream (see the format package doc).

RDF output

The graph uses the SysML vocabulary and element IRIs of the Flexo MMS SysML v2 service (https://www.omg.org/spec/SysML# and urn:sysmlv2:element:), so a converted model loads into that service's triplestore. Elements are addressed by qualified name (`urn:sysmlv2:element:Demo::Vehicle`), which makes the IRIs stable across conversions of the same model rather than newly generated each time.

Each element carries its metaclass as rdf:type and its declaration as SysML metamodel properties: declaredName, declaredShortName, owningNamespace, visibility, direction, the feature flags, the typing and specialization clauses, multiplicity bounds and its value; expression-valued positions are expression trees. Properties the metamodel does not define live in a separate urn:opensysml:sysml: namespace so a consumer can tell them from the standard vocabulary: memberIndex (declaration order, which the notation is sensitive to and RDF is not), hasBody, the end forms of heads that bind ends, and each element's lines as written, comments included, as sourceText and sourceTail.

XMI input

SysML v1 as UML XMI (OMG XMI 2.5.1, the Eclipse UML2 .uml serialization Papyrus writes, or a .mdzip archive holding it) is migrated to v2 notation by internal/translate/migrate, then takes the notation path. XMI is never written.

RDF back to notation

The structural triples are authoritative. An element is written from its source text while that text still states what the graph states — so comments and layout survive a round trip of an unedited model — and canonically where the two disagree or the graph carries no text, as one from another tool does not. docs/reference/rdf-mapping.md documents the mapping and its limitations.

Index

Constants

View Source
const AnnotationSuffix = "_an"

AnnotationSuffix names the Annotation by which a dependency owns a prefix metadata usage, after the usage it owns, as `_om` names a membership.

View Source
const CompactAPIJSONFormat = "api-json-compact/1"

CompactAPIJSONFormat names the document WriteAPIJSONCompact writes.

View Source
const RootNamespaceSuffix = "_ns"

RootNamespaceSuffix ends the id of the unnamed Namespace that owns a document's top-level elements in the API element form. An encoded id never ends in a lone '_', so the id collides with no element's or membership's.

Variables

This section is empty.

Functions

func APIJSONToSysML added in v0.9.2

func APIJSONToSysML(data []byte, warn func(string)) ([]byte, error)

APIJSONToSysML reads an API element document and converts its owned graph.

func DeclarationNames added in v0.9.2

func DeclarationNames(file *source.SourceFile, root *ast.RootNamespace) (map[source.Span]string, error)

DeclarationNames maps declarations to their RDF qualified names by span. Unnamed declarations use the export's positional `@N` identity.

func DerivedSatellite

func DerivedSatellite(metaclass string) bool

DerivedSatellite reports whether metaclass is one the encoder derives from its side tables rather than a declaration — the materialized relationship elements, a conjugated port definition and its conjugation, a multiplicity range, or the membership relating an expression to its referent. A member element of such a metaclass carries a qualified name and stays addressable.

func Destination

func Destination(path string) (target string, info os.FileInfo, err error)

Destination returns the file a save should write and what is there now, which is nil when nothing is. A symlink resolves to what it points at, so saving over a linked model updates the model instead of unlinking it; a link that resolves to nothing is written where it points.

func IsPositionalIdentity added in v0.9.2

func IsPositionalIdentity(name string) bool

IsPositionalIdentity reports whether a name contains a canonical `@N` segment.

func IsSourceRangeProperty added in v0.9.2

func IsSourceRangeProperty(iri string) bool

IsSourceRangeProperty reports whether iri is one of the four integers that place a declaration in its document: layout, which reading ignores.

func LibraryReference added in v0.9.2

func LibraryReference(graph *rdf.Graph, subject rdf.Term) bool

LibraryReference reports whether subject names a library element the graph references rather than declares: it has no owner or owned relationships.

func ModelToRDFWith added in v0.9.2

func ModelToRDFWith(documents []ModelDocument, form IDForm) (*rdf.Graph, error)

ModelToRDFWith converts the documents of one model to a single graph. They are analyzed together, as a workspace analyzes them, and a reference from one document to an element another declares links the subject that document writes for it, as a reference within one document does. Each document's root elements name it (sysx:sourceDocument), since one graph no longer keeps the documents apart. A Referenced document's elements are left out; its declarations' ids are still checked against those the written documents mint.

func NormativeSubject

func NormativeSubject(id, qualifiedName string) bool

NormativeSubject reports whether id is the id the norm fixes for the subject a graph names qualifiedName: the library element the encoder writes under that name, or its owning membership, which names nothing. A user element that carries a library uuid is not the library element, so its id stays its own.

func PositionalIdentities added in v0.9.2

func PositionalIdentities(index *symbols.Index, docs []PositionalDocument, identifies func(*symbols.Symbol) bool) (map[*symbols.Symbol]string, map[string]*symbols.Symbol)

PositionalIdentities maps symbols without qualified identities to export names.

func ReadAPIJSON

func ReadAPIJSON(data []byte) (*rdf.Graph, error)

ReadAPIJSON parses the SysML v2 API's element form — a JSON array of element objects, or a single element object — into the same graph ParseTurtle yields: the "@type" and "@id" of each object and each metamodel key in document order. It is the inverse of WriteAPIJSON, so a graph it builds writes the same elements back; the unnamed root Namespace a document wraps its top-level elements in is dropped, as the Turtle form does not carry it.

func ToRDF

func ToRDF(file *source.SourceFile, root *ast.RootNamespace) (*rdf.Graph, error)

ToRDF converts a parsed document into an RDF graph. file must be the source the tree was parsed from: every element and expression carries the notation it was written as alongside its structural triples, so the conversion needs the bytes as well as the tree.

func ToRDFWith

func ToRDFWith(file *source.SourceFile, root *ast.RootNamespace, form IDForm) (*rdf.Graph, error)

ToRDFWith is ToRDF with the id form the caller asks for.

func ToSysML

func ToSysML(graph *rdf.Graph) ([]byte, error)

ToSysML converts an RDF graph back into SysML v2 source text. An element comes back as its sysx:sourceText, byte for byte, while that still states what the graph states, and in canonical notation otherwise.

A subject whose metaclass this mapping does not know, or which lacks the properties needed to rebuild its declaration, is reported as an UnsupportedError: a converted file that dropped an element would be worse than a failed conversion.

func ToSysMLWarn added in v0.9.2

func ToSysMLWarn(graph *rdf.Graph, warn func(string)) ([]byte, error)

ToSysMLWarn converts a graph to notation and reports non-fatal resolutions.

func WriteAPIJSON

func WriteAPIJSON(graph *rdf.Graph) ([]byte, error)

WriteAPIJSON serializes a graph as the SysML v2 API's element form: a JSON array of objects carrying "@type", "@id" and the metamodel properties as keys, the shape GET /projects/{p}/commits/{c}/elements serves. It is the same mapping the Turtle writer spells: the subjects of the graph in order, their sysml: and sysx: properties as values, each collection as the array its json: annotation states, and each multi-valued metamodel property as an array even when no annotation states it. The document's top-level elements are owned by the unnamed root Namespace the pilot's documents carry, which the Turtle form leaves out (see docs/reference/rdf-mapping.md).

func WriteAPIJSONCompact added in v0.10.0

func WriteAPIJSONCompact(graph *rdf.Graph, opts CompactAPIJSONOptions) ([]byte, error)

WriteAPIJSONCompact writes the elements WriteAPIJSON writes in a form built to be small: no indentation, each element id written once, and every reference spelled by the position of its id. The document is one object:

{"format":"api-json-compact/1",
 "ids":[<id>, …],               // the id table
 "elementCount":N,              // ids[0..N) are the elements, in order
 "elementIdsAreIds":true,       // present only when elementId is left out (below)
 "referenceKeys":[<key>, …],    // keys whose values are bare handles
 "elements":[{"@type":…,…}, …]}

Element i has the id ids[i], so it carries no "@id". Under a reference key a reference is the integer index into ids (an array of them for a collection); anywhere else it is {"@id": <index>}. ids beyond elementCount are elements the conversion references but does not write. Everything else, scalars and {"@ref": name} included, is spelled as in the element form. When every element's "elementId" is its "@id", as it is unless a document declared another, "elementId" is left out and "elementIdsAreIds" says so: a reader restores it from the table.

func WriteFile

func WriteFile(path string, data []byte) (replaced bool, err error)

WriteFile writes a converted model to path, reporting whether it replaced a file that was already there.

A regular file is written atomically: the bytes go to a temporary file in the same directory, are flushed, and are renamed over the destination, so an interrupted write leaves the previous model intact rather than a truncated or empty one. An existing file keeps its permissions, and a symlink is written through rather than replaced.

Anything that is not a regular file — a terminal, a pipe, /dev/null, a process substitution — is written directly, since there is no previous content to protect and replacing it by rename would destroy the pipe or device the user pointed at.

Types

type APIJSONReadError added in v0.9.2

type APIJSONReadError struct {
	// contains filtered or unexported fields
}

APIJSONReadError reports a failure to parse an API element document.

func (*APIJSONReadError) Error added in v0.9.2

func (e *APIJSONReadError) Error() string

func (*APIJSONReadError) Unwrap added in v0.9.2

func (e *APIJSONReadError) Unwrap() error

type CompactAPIJSONOptions added in v0.10.0

type CompactAPIJSONOptions struct {
	// OmitDerived leaves out every property the metamodel derives, except the
	// ones named in KeepDerived. A derived property restates what the owned
	// properties already say, so a reader that follows ownership, typing and
	// specialization does not need it.
	OmitDerived bool
	// KeepDerived names the derived properties still written when OmitDerived is set.
	KeepDerived []string
}

CompactAPIJSONOptions chooses what the compact form leaves out.

type ElementIDs added in v0.9.2

type ElementIDs struct {
	// contains filtered or unexported fields
}

ElementIDs reports the elementId a conversion of a model writes for each of its elements, in the qualified id form. It is read from that conversion itself — ToRDF for one document, modelToRDF (ModelToRDFWith) for several, the paths Convert takes — so a library file's copy, a declared id and scope qualification come out as they are written, and a model the conversion refuses has no ids at all.

func NewElementIDs added in v0.9.2

func NewElementIDs(documents []ModelDocument) (*ElementIDs, error)

NewElementIDs converts the documents of one model and records the elementId written for each element by its qualified (or positional) name. The error is the conversion's: a model Convert refuses has no elementIds to report.

func NewElementIDsOfDocument added in v0.9.2

func NewElementIDsOfDocument(name string, data []byte) (*ElementIDs, error)

NewElementIDsOfDocument is NewElementIDs for a model of one document, which is converted from its text as a conversion of one document is: parsed afresh, in the language its name gives it, then written alone (ToRDF). A document the parser cannot read whole converts to nothing, so it is refused.

func (*ElementIDs) Of added in v0.9.2

func (ids *ElementIDs) Of(fqn string) (string, bool)

Of is the elementId written for the element named fqn: the model's own as the conversion wrote it, or, for a standard library element the model does not declare, the normative id every reference to it carries. False where the conversion writes none, as for a library element a model annotates with an id of its own, whose references are written by name.

func (*ElementIDs) OfDeclaration added in v0.9.2

func (ids *ElementIDs) OfDeclaration(decl ast.Node, fqn string) (string, bool)

OfDeclaration is the elementId written for the element declared at decl, named fqn: the one its own declaration was written with where the conversion encoded that node, else Of(fqn). Two identity scopes may declare one qualified name, and the declaration tells their ids apart where the name cannot.

type IDForm

type IDForm int

IDForm is how the encoder spells the derived ids of elements, memberships and expression nodes that no ElementId annotation declares.

const (
	// IDQualifiedName derives each id from the element's qualified name, so an
	// id reads where its element sits in the model. The default.
	IDQualifiedName IDForm = iota
	// IDUUID derives each id as a name-based uuid the way the SysML v2 library
	// convention does: the model's package namespace is
	// uuid5(NamespaceURL, elementIRI(root)), and every derived id under it is
	// uuid5(pkg, the qualified-name-derived local id it otherwise carries).
	IDUUID
)

func ParseIDForm

func ParseIDForm(s string) (IDForm, bool)

ParseIDForm reads the -id flag's argument.

type ModelDocument added in v0.9.2

type ModelDocument struct {
	File *source.SourceFile
	Root *ast.RootNamespace
	// Referenced is a document analyzed with the model but not written: a
	// reference into it links the id its element is written under on its own.
	Referenced bool
}

ModelDocument is one parsed document of a model converted as a whole.

type PositionalDocument added in v0.9.2

type PositionalDocument struct {
	File *source.SourceFile
	Root *symbols.Scope
}

PositionalDocument pairs a source file with the scope containing its symbols.

type Staged added in v0.9.2

type Staged struct {
	// contains filtered or unexported fields
}

Staged is a file written beside its destination but not yet renamed over it, so a set of files can be committed together or discarded.

func Stage added in v0.9.2

func Stage(path string, data []byte) (*Staged, error)

Stage writes data to a temporary file beside path, ready to Commit; path must be absent or a regular file, as there is nothing else to rename over.

func (*Staged) Commit added in v0.9.2

func (s *Staged) Commit() error

Commit renames the staged file over its destination.

func (*Staged) Discard added in v0.9.2

func (s *Staged) Discard()

Discard removes the staged file, leaving its destination as it was.

type UnsupportedError

type UnsupportedError struct {
	What string
	Note string
}

UnsupportedError reports a construct the conversion cannot represent. It names the element so the user can find it, rather than converting a model that silently lost part of itself.

func (*UnsupportedError) Error

func (e *UnsupportedError) Error() string

Jump to

Keyboard shortcuts

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