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/core/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 in Cameo/MagicDraw XMI or .mdzip is migrated to v2 notation by internal/core/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
- Variables
- func Advise(err error, advice string) error
- func Convert(name string, data []byte, from, to Format) ([]byte, error)
- func Destination(path string) (target string, info os.FileInfo, err error)
- func FormatNames() []string
- func IsExperimental(from, to Format) bool
- func Migrate(name string, data []byte, to Format) ([]byte, *migrate.Report, error)
- func Notice(from, to Format) string
- func Notices(from, to Format) []string
- func SysMLToRDF(name string, data []byte) (*rdf.Graph, error)
- func ToRDF(file *source.SourceFile, root *ast.RootNamespace) (*rdf.Graph, error)
- func ToSysML(graph *rdf.Graph) ([]byte, error)
- func WriteFile(path string, data []byte) (replaced bool, err error)
- type Format
- type NotWritableError
- type SyntaxError
- type UnknownFormatError
- type UnsupportedError
Constants ¶
const ExperimentalNotice = "RDF conversion is experimental: the mapping covers model structure and the " +
"behavior its bodies state, refuses what it cannot write back, and its vocabulary may change " +
"without a compatibility path; see docs/reference/rdf-mapping.md § Status"
ExperimentalNotice is the wording every surface reports the RDF mapping's status in, so the CLI, the REPL, the service and the docs agree.
const ExtensionAdvice = "name the file with a .sysml, .kerml or .ttl extension"
ExtensionAdvice is the remedy every surface shares: the file name says which format to write. A surface with a format flag names it alongside this.
const FormatList = "sysml, kerml, ttl, turtle, rdf, or xmi/mdzip (input only)"
FormatList is the wording every surface lists the format names in.
const MigrationNotice = "SysML v1 migration is experimental: the mapping covers structure, ports and " +
"connectors, requirements, constraints, instances and allocations, reports every element it " +
"approximates or leaves behind, and what it writes for a v1 element may change without a " +
"compatibility path; see docs/reference/sysml-v1-migration.md § Status"
MigrationNotice is the wording every surface reports the SysML v1 migration's status in.
Variables ¶
var ErrNoNotation = errors.New("no notation to write")
ErrNoNotation reports an element no notation can be written for: one the document holds no source of, as a symbol read from an index cache is.
Functions ¶
func Advise ¶
Advise returns err with the surface's remedy attached when it is an *UnknownFormatError, and unchanged otherwise.
func Convert ¶
Convert reads data in the from format and writes it in the to format. name is used in diagnostics and needs no relation to a file on disk.
Converting between two formats requires the input to be syntactically valid: a model with syntax errors is rejected, because the tree the parser recovers from broken input does not hold the declarations it could not read, and a graph built from it would be quietly missing them.
func Destination ¶ added in v0.4.2
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 FormatNames ¶ added in v0.4.3
func FormatNames() []string
FormatNames returns every name ParseFormat accepts, sorted.
func IsExperimental ¶
IsExperimental reports whether a conversion between these formats goes through the RDF mapping or the SysML v1 migration. Notation to notation does not.
func Migrate ¶ added in v0.6.0
Migrate reads a SysML v1 model in XMI and writes it in the to format, with the report of what each v1 element became.
func Notice ¶ added in v0.6.0
Notice joins the notices of a conversion into one string, one per line.
func Notices ¶ added in v0.6.0
Notices lists the experimental notices a conversion between these formats carries, migration first; empty when the conversion is stable.
func SysMLToRDF ¶
SysMLToRDF parses SysML notation and converts it to a graph.
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 ToSysML ¶
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 WriteFile ¶
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 Format ¶
type Format int
Format is one of the representations a model can be read from or written to.
func FormatOfPath ¶
FormatOfPath infers the format from a file extension, so that the common case needs no -from. A path that names no format yields an *UnknownFormatError carrying no advice; pass it through Advise to add the remedy the calling surface offers.
func ParseFormat ¶
ParseFormat resolves a format name, as given to `-convert`/`-from`.
type NotWritableError ¶ added in v0.6.0
type NotWritableError struct{ Format Format }
NotWritableError reports a request to write a format that is only read.
func (*NotWritableError) Error ¶ added in v0.6.0
func (e *NotWritableError) Error() string
type SyntaxError ¶
type SyntaxError struct {
Name string
Messages []string
// Diags are the diagnostics behind Messages, for a caller that reports them
// with their spans. Empty when the input is not notation, since a Turtle
// reader reports a message and no span.
Diags []parser.Diagnostic
// File is what Diags' spans point into; nil when Diags is empty.
File *source.SourceFile
}
SyntaxError reports that the input could not be read as its format. It lists every syntax error rather than only the first, so one conversion attempt shows everything that needs fixing.
func ConvertTolerant ¶
ConvertTolerant is Convert with one difference: notation converted back to notation is written even when the parser could not read all of it, and its syntax errors are returned as a warning instead of an error. That direction re-indents the input rather than building anything from the parse tree, so the output is exactly as valid as the input and refusing it would only strand work that exists nowhere else — which is why the REPL's `%save` uses it for a session buffer. Every other direction builds a graph from the tree, where declarations the parser could not read would be silently missing, so a broken model is still rejected.
func SysMLElement ¶
func SysMLElement(file *source.SourceFile, span source.Span) ([]byte, *SyntaxError, error)
SysMLElement writes the notation of one element of a document: the source at span, through the same writer a whole-document notation save goes through, so what one surface writes cannot drift from what another writes. Syntax errors are tolerated and returned as a warning, as ConvertTolerant does, since the span comes from a buffer that is written back as typed.
Trailing comments are dropped: a declaration's span ends where the next token begins, so it runs over the notes written for whatever follows it.
func (*SyntaxError) Error ¶
func (e *SyntaxError) Error() string
type UnknownFormatError ¶
type UnknownFormatError struct {
Path string
// NoExtension distinguishes a path with no extension from one whose
// extension names a format we do not write.
NoExtension bool
// Advice is the surface's remedy, appended as "…, so <advice>".
Advice string
}
UnknownFormatError reports that a path does not say which format to write. The remedy differs by surface — the command line has -convert/-from, the REPL only has the file name — so the caller supplies it with Advise.
func (*UnknownFormatError) Error ¶
func (e *UnknownFormatError) Error() string
type UnsupportedError ¶
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