MPS 2023.3 Help

Constraint Rules

The Constraints Language suffers from several deficiencies, like the inability to specify error messages for individual constraints or the inability to define multiple separate rules for the same kind of constraint. The experimental Constraint Rules language attempts to overcome those. It is available in the Constraints aspect under the RulesConstraintsRules root:

Creating a new root

Can be child/parent/ancestor/root rules

The RulesConstraintsRules root chooses a minimalistic visual guidance:

Empty Constraints rule root

The definition is split into Rules blocks. Each of these rules blocks must be dedicated to one of four possible categories:

  • CanBeAncestor

  • CanBeChild

  • CanBeParent

  • CanBeRoot

Categories for rules blocks

Each Rules block contains members of two types:

Members of rules blocks
  • Variable definition - a reusable pre-calculated value that can be used in rules

  • Rule definition - a logical expression to evaluate as a constraint, with optional name for easy reference (e.g. from tests), an optional error message and an optional Applicability guard expression.

A sample definition

Implicit parameters of rules blocks

Each Rules block receive parameters that capture the context for evaluation in rules. These are available in the completion menu:

Use of implicit parameters

These parameters can also be used to form the rule's error messages:

Use of implicit parameters in error messages

Each kind of rules block offers a different set of implicit parameters:

  • CanBeRoot - concept, model

  • CanBeChild - childNode, parentNode, link

  • CanBeParent - childNode, parentNode, link, childConcept

  • CanBeAncestor - ancestorNode, parentNode, descendantNode, descendantConcept, link

Tracing constraint rules evaluation

The constraint rules definitions allow logging messages to be specified in the Inspector window for better tracing:

Rule tracing definition

You can use a single log statement or a BlockStatement to include a more complex logic.

Specify the feature to highlight

The error message for a constraint rule can be targeted for specific node’s properties or references.

Whole node highlighted

Instead of the error highlighting the whole node, it can be configured to only highlight a property or a reference:

A property of a node highlighted

The feature to highlight property may be set to restrict the error highlighting:

Restrict to a property

You can either refer to a property of a reference directly or by using an expression:

Restrict by expression to a property
Restrict by expression to a reference
Last modified: 07 March 2024