2. Design your model

Think model first

Designing a model in System Runtime is very simple, you do not have to code but just to define your model in a human readable format called MSON.

MSON

What is MSON? MSON stands for Metamodel JavaScript Object Notation, it is a human readable format based on JSON that will help you to define your model.

For example, let's say we want to create Person class:

UML representation of Person class

UML representation of Person class

This is what we will write in MSON:

{
  "_name": "Person",
  "firstName": "property",
  "lastName": "property",
  "age": "property"
}

As you see, MSON is in fact a schema, a definition of a structure of an object. It shows what will be the properties of an object.

In our example:

  • _name: defined the name of the schema. The ‘_’ at the begins means that the property is internal and
  • firstName and lastName: they have property type, so they are the properties of the class.

In System Runtime, we use this schema to generate the model that contains the default value of each property:

{
  "_name": "Person",
  "_schema": "Person",
  "_inherit": [
    "_Component"
  ],
  "firstName": {
    "type": "any",
    "readOnly": false,
    "mandatory": false,
    "default": null
  },
  "lastName": {
    "type": "any",
    "readOnly": false,
    "mandatory": false,
    "default": null
  },
  "age": {
    "type": "any",
    "readOnly": false,
    "mandatory": false,
    "default": null
  }
}

In our case:

  • firstName, lastName are string and
  • age is a number,

So we need to override these values. To do so, we have to create a new MSON to override the generated model.

{
  "_name": "Person",
  "firstName": {
    "type": "string",
    "readOnly": false,
    "mandatory": false,
    "default": ""
  },
  "lastName": {
    "type": "string",
    "readOnly": false,
    "mandatory": false,
    "default": ""
  },
  "age": {
    "type": "number",
    "readOnly": false,
    "mandatory": false,
    "default": 0
  }
}

What kind of types can we use ?

System Runtime manages the following types: boolean, string, number, object, array, date and any.
You can also create your own type.

Loading your model

Once you have designed your model, you need to tell System Runtime to load it. To do so:

  • get System Runtime metamodel instance with runtime.require (System Runtime manages its own module system),
  • add your schemas files by calling its schema method
  • override your model files by calling its model method and
  • call create method to create the model.

Load a model with System Runtime metamodel:

let metamodel = runtime.require('metamodel');

metamodel.schema({
  '_name': 'Person',
  'firstName': 'property',
  'lastName': 'property',
  'age': 'property'
});

metamodel.model({
  '_name': 'Person',
  'firstName': {
    'type': 'string',
    'readOnly': false,
    'mandatory': false,
    'default': ''
  },
  'lastName': {
    'type': 'string',
    'readOnly': false,
    'mandatory': false,
    'default': ''
  },
  'age': {
    'type': 'number',
    'readOnly': false,
    'mandatory': false,
    'default': 0
  }
});

metamodel.create();

Once your model is created, you will be able to get the classes of your model and create components based on your model.

Design first, then code

System Runtime uses MSON files as the entry point of your system. You can not do anything without these files. So think well on how you will design your system.

Conclusion

In this article, we saw that:

  • models in System Runtime are defined with MSON, a format to create schemas,
  • models are generated based on the schemas definition and
  • you can override these generated models.

In a next article, we will introduce you how System Runtime uses these schemas to create components.


2. Design your model

Think model first