我正试图为微波炉制造一台状态机。我设计它的时候,它现在只有两个功能:烘焙和清洁。它将有三个状态:闲置,烘焙和清洁。在转换到状态时,状态机将执行操作。操作是命令,例如运行计时器,锁上门,打开里面的灯等等。我试图将许多这样的命令分组到一个命令组中,并将该命令组传递给操作。事件是用户执行的事件,如开始烘焙,开始清理等。
我的命令分组逻辑正在返回语法错误。我被困在如何理解错误和如何解决这个问题上。
PFB我的元模型和烤箱模型。
元模型:
StateMachine:
'events'
events+=Event
'end'
'commands'
commands+=Command
'end'
('commandgroups'
commandGroupBlocks+=CommandGroupBlock
'end')?
states+=State
;
Keyword:
'end' | 'events' | 'state' | 'actions' | 'commandgroups' | 'group' | 'groupend'
;
Event:
name=SMID code=ID
;
Command:
name=SMID code=ID
;
CommandGroupBlock:
'group' name=SMID
groupCommands+=[Command|SMID]
'groupend'
;
State:
'state' name=ID
('actions' '{' actions+=[CommandGroupBlock|Command] '}')?
(transitions+=Transition)?
'end'
;
Transition:
event=[Event|SMID] '=>' to_state=[State]
;
SMID:
!Keyword ID
;
Comment:
/\/\*(.|\n)*?\*\//
;烤箱模型:
events
bakeOn E_BKON
cleanOn E_CLON
cleanOff E_CLOF
end
commands
timerInitial C_TMIN
timerRunning C_TMRN
lockDoor C_D1LK
unlockDoor C_D1UL
magnetronOn C_MGON
magnetronOff C_MGOF
internalLightOn C_ILON
internalLightOff C_ILOF
turnTableOn C_TTON
turnTableOff C_TTOF
cleanerOn C_CLON
cleanerOff C_CLOF
end
commandgroups
group resetCommands
unlockDoor
timerInitial
magnetronOff
internalLightOff
turnTableOff
cleanerOff
groupend
group commonActiveCommands
lockDoor
internalLightOn
timerRunning
groupend
end
state idle
actions {resetCommands}
bakeOn => baking
cleanOn => cleaning
end
state baking
actions {commonActiveCommand magnetronOn turnTableOn}
end
state cleaning
actions {commonActiveCommand cleanerOn turnTableOff}
cleanOff => idle
end我得到的错误是:
Traceback (most recent call last):
File "state_machine.py", line 70, in <module>
model = meta_model.model_from_file(sys.argv[1])
File "/workspace/dsl-state-machine-oven/demo/lib/python3.8/site-packages/textx/metamodel.py", line 661, in model_from_file
return self.internal_model_from_file(
File "/workspace/dsl-state-machine-oven/demo/lib/python3.8/site-packages/textx/metamodel.py", line 709, in internal_model_from_file
model = self._parser_blueprint.clone().get_model_from_str(
File "/workspace/dsl-state-machine-oven/demo/lib/python3.8/site-packages/textx/model.py", line 372, in get_model_from_str
self.parse(model_str, file_name=file_name)
File "/workspace/dsl-state-machine-oven/demo/lib/python3.8/site-packages/arpeggio/__init__.py", line 1525, in parse
self.parse_tree = self._parse()
File "/workspace/dsl-state-machine-oven/demo/lib/python3.8/site-packages/textx/model.py", line 332, in _parse
raise TextXSyntaxError(message=e.message,
textx.exceptions.TextXSyntaxError: /workspace/dsl-state-machine-oven/oven.sm:39:25: Expected ID => 'etCommands*} bakeOn'请帮我解决这个问题。
发布于 2022-08-28 15:36:56
你们关系很好。问题是,您试图从状态操作中引用命令或命令组。但语法不正确。
State:
'state' name=ID
('actions' '{' actions+=[CommandGroupBlock|Command] '}')?
(transitions+=Transition)?
'end'
;actions+=[CommandGroupBlock|Command]部分的意思是match one or more CommandGroupBlock using Command to match the name。
你真正想要的是:
GroupOrCommand: Command | CommandGroupBlock;
State:
'state' name=ID
('actions' '{' actions+=[GroupOrCommand|SMID] '}')?
(transitions+=Transition)?
'end'
;在这里,我们引入了一个新的抽象规则CommandOrGroup,然后我们使用SMID链接到这个规则来解析名称。
顺便说一句,模型中也有一个错误:commonActiveCommand -> commonActiveCommands。
https://stackoverflow.com/questions/73518617
复制相似问题