简体   繁体   中英

How are cardinalities written on XText from ecore meta model?

I have created an ecore metamodel for state machines. One state machine had 0..* states, 1..1 initial state, 1..1 final state and 1..* transitions. When I generate the XText grammar, I get something like this

StateMachine:
'{'
        ('states' '{' states+=State ( "," states+=State)* '}' )?
        'transitions' '{' transitions+=Transitions ( "," transitions+=Transitions)* '}' 
        'initialstate' initialstate=InitialState
        'finalstate' finalstate=FinalState
    '}';

Now being that states have a 0..* relation, shouldn't they only have the * operator which means 0 or more? Why do they also have the "?" operator which means 0 or 1? Furthermore, transitions have a 1..* relation, shouldn't they have the "+" operator instead of *?

Let's first take a look at transitions .

The rule is transitions+=Transitions ( "," transitions+=Transitions)* instead of (transitions+=Transitions)+ because that's a simple way to make sure transitions are separated by a comma ",". It can be read "at least one transition, then any number of transitions each prefixed by a comma" which matches a [1..*] cardinality.

The same goes for states :

  • ('states' '{'... '}' )? means that the whole block can be omitted when your state machine contains no state (which matches the 0 lower bound)
  • ('states' '{' states+=State... '}' )? means that if the states block is written then it must contains at least one state
  • ('states' '{' states+=State ( "," states+=State)* '}' )? means that if the states block is written then it must contain at least one state, and that more states can be specified by separating them with commas "," (which matches the * upper bound).

Personally I don't think you need the? Operator, because you have already used * operator which means, it can be 0. Yes the operator * is right, because you already forced at least one state to be declared and the other (comma separated) can be omitted

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM