简体   繁体   English

如何从ecore meta model 在XText 上写入基数?

[英]How are cardinalities written on XText from ecore meta model?

I have created an ecore metamodel for state machines.我为 state 机器创建了一个 ecore 元模型。 One state machine had 0..* states, 1..1 initial state, 1..1 final state and 1..* transitions.一台 state 机器具有 0..* 状态、1..1 初始 state、1..1 最终 state 和 1..* 转换。 When I generate the XText grammar, I get something like this当我生成 XText 语法时,我得到了这样的东西

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?现在由于状态具有 0..* 关系,它们不应该只有 * 表示 0 或更多的运算符吗? Why do they also have the "?"为什么他们也有“?” operator which means 0 or 1?运算符表示 0 还是 1? Furthermore, transitions have a 1..* relation, shouldn't they have the "+" operator instead of *?此外,转换具有 1..* 关系,它们不应该使用“+”运算符而不是 * 吗?

Let's first take a look at transitions .我们先来看看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 ",".规则是transitions+=Transitions ( "," transitions+=Transitions)*而不是(transitions+=Transitions)+因为这是确保转换用逗号 "," 分隔的简单方法。 It can be read "at least one transition, then any number of transitions each prefixed by a comma" which matches a [1..*] cardinality.它可以读作“至少一个转换,然后是任意数量的转换,每个转换都以逗号为前缀”,它与 [1..*] 基数匹配。

The same goes for states : states也是如此:

  • ('states' '{'... '}' )? means that the whole block can be omitted when your state machine contains no state (which matches the 0 lower bound)表示当您的 state 机器不包含 state (与 0 下限匹配)时,可以省略整个块
  • ('states' '{' states+=State... '}' )? means that if the states block is written then it must contains at least one state表示如果状态块被写入,那么它必须包含至少一个 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).表示如果写入状态块,则它必须包含至少一个 state,并且可以通过用逗号分隔它们来指定更多状态“,”(与 * 上限匹配)。

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运算符,因为您已经使用了 * 运算符,这意味着它可以为 0。是的,运算符 * 是正确的,因为您已经强制声明了至少一个 state 并且可以省略另一个(逗号分隔)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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