gpt4 book ai didi

How can i create a state machine using on the fly using yaml configuration, where i can store and use multiple config files(如何使用YAML配置动态创建状态机,在其中我可以存储和使用多个配置文件)

转载 作者:bug小助手 更新时间:2023-10-25 12:17:07 27 4
gpt4 key购买 nike



I have a requirement in my rails application to build a FSM and that FSM should be configurable. In my application i am creating a employee exit workflow that includes some processes needs to be completed before exit of an employee from an organization. And catch is each organization of the application should be able to store their own resignation configuration, for example which events should be triggered when this state transitioned. Their can be external events on which state can transition etc.

我在我的Rails应用程序中有一个构建FSM的需求,并且该FSM应该是可配置的。在我的应用程序中,我创建了一个员工离职工作流,其中包括在员工从组织离职之前需要完成的一些流程。需要注意的是,应用程序的每个组织都应该能够存储自己的辞职配置,例如,当此状态转换时应触发哪些事件。它们可以是状态可以转换的外部事件,等等。


I tried aasm gem but i didn't found any feature that allows dynamically creating states

我尝试了AASM gem,但没有找到任何允许动态创建状态的功能


更多回答

Check this out and see if it is useful for your case: github.com/geekq/workflow

查看以下内容,看看它是否对您的案例有用:githorb.com/geekq/Workflow

优秀答案推荐

Something along these lines should allow you use a yaml file in the appropriate model

这些内容应该允许您在适当的模型中使用YAML文件


include AASM


def apply_custom_configuration(config_file_path)
config = YAML.load_file(config_file_path)

aasm do
config['states'].each do |state|
aasm_state state['name'].to_sym, initial: state['initial'] || false
end

config['events'].each do |event|
aasm_event event['name'].to_sym do
transitions from: event['from'].map(&:to_sym), to: event['to'].to_sym
end
end
end
end

Then something along these lines can be used where the actual employee exit is performed.

然后,在执行实际的员工离职时,可以使用这些方法。


config_file_path = Rails.root.join('config', 'employee_exit_workflows', 'organization1.yaml')
employee_exit = EmployeeExit.new
employee_exit.apply_custom_configuration(config_file_path)

How about persisting the custom workflows in a database?

如何在数据库中持久化自定义工作流?


To persist the data in your database:

要将数据保存在数据库中,请执行以下操作:



  1. Creates a custom_workflows model

  2. Serialise the YAML

  3. Load and save the YAML configuration


Here's a snippet for saving it to the database:

以下是将其保存到数据库的代码片段:


config = YAML.load_file('path_to_your_yaml_file.yaml')
custom_workflow = CustomWorkflow.new(organization: 'org_name', config: config)
custom_workflow.save

The snippet above assumes the new table has just two fields but you can modify it to suit your needs.

上面的代码片段假定新表只有两个字段,但您可以根据需要对其进行修改。


You can then retrieve a custom workflow thus:

然后,您可以检索自定义工作流,如下所示:


workflows = CustomWorkflow.where(organization: 'org_name')

更多回答

Yes persisting the user defined workflow is a very good alternative. Could you please guide me how can i persist them in my postgresql DB. what will be the schema of the custom workflows? Thanks

是的,持久化用户定义的工作流是一个非常好的选择。您能指导我如何在我的PostgreSQL数据库中持久化它们吗?自定义工作流的架构是什么?谢谢

@MukulJangid I've modified the answer to give you an idea on how to save it to a database

@MukulJangid我已经修改了答案,让您了解如何将其保存到数据库

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com