Contents > 8 Defining Custom Design Metrics and Rules > 8.3 Definition of Design Rules > 8.3.3 Projection for Rules
8.3.3 Projection for Rules
The projection procedure for rules calculates a set projection for a
model element (see Section 8.2.1 "Projection"), and reports any elements
contained in there as violation of the rule. Consider this example:
<rule name="MissingGuard" domain="state" category="Correctness"
severity="1-high">
<description>If there are two or more transitions from a choice
state, they all must have guards.</description>
<projection precondition="kind='choice' and Outgoing>1"
relation="transsource" target="transition" condition="Guards=0"
</rule>
Attributes relation, target, and
condition define the projection, and are processed
exactly the same way as we know them from set projections. In the
above example, the projection yields, for a state, the set of outgoing
transitions that have no guard.
In the context of a rule, a projection can have three additional
attributes:
- precondition: A condition expression that is
evaluated for the model element about to be checked. If the condition
is false, the rule will not be checked. In the example above, the rule
is only applicable if the state is a choice state
(kind='choice') and has at least two outgoing transitions
(Outgoing>1).
- value: defines the value of the rule that is
displayed to the user. This is a metric expression, and is evaluated
for the model elements returned by the projection. If the projection
returns a valueset, no value needs to be specified - the value of
the rule is the value returned by the projection.
- mincnt: defines the minimum number of times an element
must be included in the resulting multiset to be reported. You can use
this feature to find and report duplicates, as shown in the next
example. The default value is 1 (report all elements).
<rule name="DupName" domain="state" category="Correctness" severity="1-high">
<description>The compound state has two or more states of
the same name.</description>
<projection relation="context" target="state" condition="name!=''"
valueset="name" mincnt="2" />
</rule>
For rule "DupName", the projection defines the value set of names of
the states of a compound state (omitting anonymous states). By setting
mincnt="2", only names that occur two times or more will be
reported.
Note the following restrictions that apply in the context of a rule
projection:
- The resulting set is always a multiset.
- Attribute "recurse" is not allowed for rules. If you need the rule
to operate on a recursively defined set, define the set using a
regular set projection (Section 8.2.1 "Projection"), and feed that set into the
rule projection with the "relset" attribute.