3.2. Example

In this section, a few simple examples are given to show how to read attributes and references, how to change them and how to execute operations. It is essential to understand the underlying metamodel for all used objects. This example uses the SysMLPart metaclass.

Each metaclass has attributes and operations. Attributes may be single-valued or multi-valued, they might be of native types as string or number or of type of another metaclass (references). To access an attribute, just use the name of the attribute with either the 'dot' notation or in brackets (Java Script property accessors).

// read single value (string) using 'dot' notation
var name = part.name;

// write single value (string) using [] notation
part["name"] = "New Name";

In case the attribute is multi-valued, a list of values is returned. These lists can be read and traversed and also changed but cannot be replaced or overwritten. That means, a multi-valued attribute is never set the same way as a single-valued. The returned list has all methods and attributes of a java.util.List class as size(), get(int) but also clear() and toArray() to mention a few.

// read multi value (object) 
var ports = part.ports;
var numberOfPorts = ports.size();
var nameOfFirstPort = ports.get(0).name;

// write multi value (object);
var newPort = ...
ports.add(newPort);

The metaclass may additionally define a set of operations. Operations are called the same as normal script functions. The SysMLPart metclass for example offers an operation compareIL to compare integrity levels. It also inherits a few convenience operations from a base class:

// compare the part integrity level with a given one
var ilDiff = part.compareIL("A");
console.log(ilDiff);

// call an operation to navigate to the container
var parent = part.mediniGetContainer();