Repository navigation
Expand file tree
/
Copy pathTutorial.cs
More file actions
103 lines (93 loc) · 4.49 KB
/
Copy pathTutorial.cs
File metadata and controls
103 lines (93 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// SysML v2 SDK, C#: a first walk through a model.
//
// Run from the repository root:
//
// dotnet run --project examples/csharp reads examples/vehicle.full.json
// dotnet run --project examples/csharp -- --toolkit reads examples/vehicle.sysml through the SysML Toolkit
//
// The payload is a full-form interchange export any SysML v2 tool can produce; this one came
// from the SysML Toolkit. Its references into the standard library resolve against the library
// as JSON, published with each SDK release. In this repository both are generated by
// `python tools/export_example.py --library <sysml.library>`. The SysML Toolkit path loads the
// binding library (build abi/ first).
//
// Everything is spelled as in the OMG specification: ownedMember, ownedFeature, qualifiedName,
// type. The generated interfaces form the real metaclass hierarchy, so `is` and casts work.
using System;
using System.IO;
using System.Linq;
using OpenMBEE.SysML;
using SpecType = OpenMBEE.SysML.Type; // the metaclass Type shadows System.Type
var root = FindRepoRoot();
var model = args.Contains("--toolkit")
? new Model(ToolkitBackend.Open(Path.Combine(root, "examples", "vehicle.sysml")))
: Model.FromFullJson(File.ReadAllText(Path.Combine(root, "examples", "vehicle.full.json")),
PayloadLibrary.FromJson(File.ReadAllText(Path.Combine(root, "examples", "sysml.library.full.json"))));
// 1. Find an element by its qualified name, and confirm what it is.
var vehicle = (PartDefinition)model.Resolve("Vehicles::Vehicle")!;
Console.WriteLine($"{vehicle.qualifiedName} is a {vehicle.MetaclassName}");
// 2. The generated interfaces are the metaclass hierarchy.
var vehicles = (Namespace)model.Resolve("Vehicles")!;
Console.WriteLine("definitions: " + string.Join(", ",
vehicles.ownedMember.OfType<Definition>().Select(d => d.declaredName)));
// 3. Walk a definition's features and their declared types.
Console.WriteLine($"\nfeatures of {vehicle.declaredName}:");
foreach (var f in vehicle.ownedFeature)
{
string types;
try
{
var names = string.Join(", ", f.type.Select(t => t.declaredName));
types = names.Length == 0 ? "untyped" : names;
}
catch (NotImplementedInToolkitException e) // the SysML Toolkit cannot vouch for this value yet
{
types = $"({e.Message})";
}
Console.WriteLine($" {f.MetaclassName,-16} {f.declaredName ?? "(unnamed)",-10} : {types}");
}
// 4. Documentation is a member like any other.
foreach (var doc in vehicle.documentation) Console.WriteLine($"\ndoc: {doc.body?.Trim()}");
// 5. A usage and the definition it is typed by.
var myCar = (PartUsage)model.Resolve("Vehicles::myCar")!;
try
{
Console.WriteLine($"\n{myCar.qualifiedName} is typed by {myCar.type.First().qualifiedName}");
}
catch (NotImplementedInToolkitException e)
{
Console.WriteLine($"\n{myCar.qualifiedName}: {e.Message}");
}
// 6. Inheritance. Through the SysML Toolkit, inherited features are computed. A payload holds what
// the exporting tool wrote: this file was exported at the closure level, so it has them too; an
// export by the SysML Toolkit's command-line tool would list the owned side only. With the
// standard library, every part also inherits the library's features (`self`, `start`, ...);
// isLibraryElement tells them apart.
var sports = (PartDefinition)model.Resolve("Vehicles::SportsCar")!;
string inherited;
try
{
inherited = sports.inheritedFeature.Count(f => f.isLibraryElement != true).ToString(System.Globalization.CultureInfo.InvariantCulture);
}
catch (NotImplementedInToolkitException e)
{
inherited = $"({e.Message})";
}
Console.WriteLine($"\n{sports.declaredName} owns {sports.ownedFeature.Count} features; inherited: {inherited}");
// 7. Honest refusal. Through the SysML Toolkit, a member it cannot vouch for yet says so instead of
// answering, as the reads above show where it does. Through a payload there is nobody to say so:
// the file holds what the exporting tool wrote.
try
{
Console.WriteLine($"\npartDefinition of myCar: {string.Join(", ", myCar.partDefinition.Select(d => d.qualifiedName))}");
}
catch (NotImplementedInToolkitException e)
{
Console.WriteLine($"\nnot answered by the SysML Toolkit, and it says so: {e.Message}");
}
static string FindRepoRoot()
{
var d = new DirectoryInfo(AppContext.BaseDirectory);
while (d != null && !File.Exists(Path.Combine(d.FullName, "metamodel.json"))) d = d.Parent;
return d?.FullName ?? throw new InvalidOperationException("run from inside the sysml-sdk repository");
}