• 条件

我们可以用:if和:unless公共属性来进行条件判断,或者使用if,given,once或者equals(已经过时)关键字。

使用:if属性:

1 cursor do
2 participant 'customer'
3 rewind :if => '${not_enough_info} == true'
4 participant 'logistics'
5 end

当使用given表达式的时候:

 1 given do
2 that "${location} == paris" do
3 subprocess "notify_and_wait_for_pickup"
4 end
5 that "${state} == ready" do
6 subprocess "deliver"
7 end
8 # else...
9 subprocess "do_something_else"
10 end

看起来就是和which case的语法是一样的。given类似于which关键字,that类似于case关键字。当然在:if和:unless可以接收判断条件可以涉及到<,>,<= ,>=等。这个大家很熟悉了。只要能够返回true或者false就可以了。

  • 美元符号 

在我们以前所举的例子当中,把每个参与者或者每个子流程都写成固定的,就是这个样子:

1 sequence do
2 participant 'alfred'
3 participant 'bob'
4 end

但是在真正项目中使用的时候,参与者大都是一个动态的变量。应该是这样的:

1 sequence do
2 participant '${f:student}'
3 participant '${f:teacher}'
4 end

根据这个小例子的定义,我们可以看出这个工作流是从学生到老师,学生的真实姓名是存在这个工作流中的student字段,老师的具体名字是存放在工作条目中teacher字段中。

美元符号获取process的定义的${...}变量,或者在参与者中设置的process的变量。看下面的例子:

 1 require 'ruote'
2
3 # 定义一个引擎
4
5 engine = Ruote::Dashboard.new(
6 Ruote::Worker.new(
7 Ruote::HashStorage.new()))
8
9 # 注册参与者
10
11 engine.register_participant :alpha do |workitem|
12 workitem.fields['message'] = 'Dollar notation Test'
13 end
14
15 engine.register_participant :bravo do |workitem|
16 puts "I received a message from #{workitem.fields['message']}"
17 end
18
19 # 定义一个程序
20
21 pdef = Ruote.define :name => 'test' do
22 sequence do
23 set 'f:a' => 'Steven'
24 echo '${a}'
25 participant :alpha
26 echo '${message}'
27 participant :bravo
28 end
29 end
30
31 # 创建一个进程实例
32
33 wfid = engine.launch(pdef)
34
35 engine.wait_for(wfid)

查看输出结果:

1 Steven
2 Dollar notation Test
3 I received a message from Dollar notation Test

其中sequence表达式是指注册的参与者按照顺序依次执行。

变量和工作流中的字段:

workitem字段对于process实例最为常见,每一个workitem都有一个字段的集合,workitem和字段对于参与者可见。Process变量对于engine以外不可见,只是给route用来在process实例中做逻辑处理用。

 1 Ruote.process_definition :name => 'loan_approval', :revision => '1' do
2 cursor do
3 participant 'planning team'
4 concurrence do
5 participant 'ceo', :if => '${f:total} > 100000'
6 participant 'cfo'
7 end
8 rewind :unless => '${f:approved}'
9 participant 'execution team'
10 end
11 end

这个例子的process定义中,有两个字段可见,分别是total和approved。total由'planning team'设置,approved由'ceo'或者'cfo'设置。如果total大于100000,ceo和cfo都收到一个workitem。这两个参与者是并行执行的。

一直都在说workitem的fields,下面来开一下程序变量的使用方法,只是process定义部分的代码其他同上:

pdef = Ruote.define :name => 'test' do
set 'v:test' => 'process var'
echo '${v:test}'
end

输出结果:

1 process var

程序变量使用的情况不多,所以在我们写${xx}其实默认表达的是${f:xx}

还有一些稍微复杂一点的用法比如交叉或者带有复杂的key值,但是用法没有什么不一样。下面看一下workitem字段和process 变量的初始化的问题:

 1 require 'ruote'
2
3 # preparing the engine
4
5 engine = Ruote::Dashboard.new(
6 Ruote::Worker.new(
7 Ruote::HashStorage.new()))
8
9 # registering participants
10
11 engine.register_participant :alpha do |workitem|
12 workitem.fields['message'] += ' via alpha '
13 end
14
15 engine.register_participant :bravo do |workitem|
16 puts "I received a message #{workitem.fields['message']}"
17 end
18
19 # defining a process
20
21 pdef = Ruote.define :name => 'test' do
22 set 'field:message' => 'Helllo'
23 sequence do
24 participant :alpha
25 participant :bravo
26 end
27 end
28
29 # launching, creating a process instance
30
31 wfid = engine.launch(pdef)
32
33 engine.wait_for(wfid)

输出结果:

1 I received a message  Helllo via alpha 

在这个例子中,我们初始化了一个workitemfiled,还有没有其他办法呢?我们看一下Dashboar的lauch方法定义

1 launch(process_definition, fields={}, variables={}, root_stash=nil)

好吧我们可以这程序运行的时候生成workitem fileds和程序变量,将上面的程序做一点修改:

 1 require 'ruote'
2
3 # preparing the engine
4
5 engine = Ruote::Dashboard.new(
6 Ruote::Worker.new(
7 Ruote::HashStorage.new()))
8
9 # registering participants
10
11 engine.register_participant :alpha do |workitem|
12 workitem.fields['message'] += ' via alpha '
13 end
14
15 engine.register_participant :bravo do |workitem|
16 puts "I received a message #{workitem.fields['message']}"
17 end
18
19 # defining a process
20
21 pdef = Ruote.define :name => 'test' do
22 sequence do
23 participant :alpha
24 participant :bravo
25 end
26 end
27
28 # launching, creating a process instance
29
30 wfid = engine.launch(pdef,'message'=>'Hello')
31
32 engine.wait_for(wfid)

输出结果:

1 I received a message  Hello via alpha 

同样可以。

workflow engine Ruote初体验之三(条件与美元符号)的更多相关文章

  1. workflow engine Ruote初体验之一(概念)

    由于最近自己写点小东西,需要有工作流程管理方面的应用,所有的环境为Ruby on rails,所有在选择流程引擎的时候选择了ruote,但是对于ruote是完全陌生的,所以在这里记下点滴,如果理解的不 ...

  2. workflow engine Ruote初体验之二(通用属性)

    罗列一下表达式所支持的属性: :timeout :if/ unless :forget :lose :flank :on_error :on_cancel :on_timeout :tag :filt ...

  3. workflow engine Ruote 安装

    今天在安装gem安装Ruote的过程中遇到问题,改用bundle安装: steven@steven-Latitude-D630:/usr$ sudo mkdir bundel [sudo] passw ...

  4. MEF初体验之三:Exports声明

    组合部件通过[ExportAttribute]声明exports.在MEF中,有这么几种成员可声明exports的方式:组合部件(类).字段.属性和方法.我们来看下ExportAttribute类的声 ...

  5. kvm初体验之三:vm的安装及管理

    Host: CentOS release 6.4 (Final) Guest: CentOS release 6.6 (Final) 全程以root身份操作 1. host上创建桥br0 参考< ...

  6. Node.js 网页瘸腿爬虫初体验

    延续上一篇,想把自己博客的文档标题利用Node.js的request全提取出来,于是有了下面的初哥爬虫,水平有限,这只爬虫目前还有点瘸腿,请看官你指正了. // 内置http模块,提供了http服务器 ...

  7. Spring boot缓存初体验

    spring boot缓存初体验 1.项目搭建 使用MySQL作为数据库,spring boot集成mybatis来操作数据库,所以在使用springboot的cache组件时,需要先搭建一个简单的s ...

  8. Flume日志采集系统——初体验(Logstash对比版)

    这两天看了一下Flume的开发文档,并且体验了下Flume的使用. 本文就从如下的几个方面讲述下我的使用心得: 初体验--与Logstash的对比 安装部署 启动教程 参数与实例分析 Flume初体验 ...

  9. Java8初体验(一)lambda表达式语法

    感谢同事[天锦]的投稿.投稿请联系 tengfei@ifeve.com 本文主要记录自己学习Java8的历程,方便大家一起探讨和自己的备忘.因为本人也是刚刚开始学习Java8,所以文中肯定有错误和理解 ...

随机推荐

  1. activity切换交互动画

    activity切换的时候,想要有动画,那么... 1.想要有效果的activity设置theme <activity android:name=".MainActivity" ...

  2. MySQL基础9-主键约束、外键约束、等值连接查询、一对一和多对多关系

    1.主键约束和外键约束 外键约束 * 外键必须是另一表的主键的值(外键要引用主键!) * 外键可以重复 * 外键可以为空 * 一张表中可以有多个外键! 概念模型在数据库中成为表 数据库表中的多对一关系 ...

  3. leetcode 【 Reverse Nodes in k-Group 】 python 实现

    原题: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list ...

  4. CSS视觉格式化模型

    CSS视觉格式化模型(visual formatting model)是用来处理文档并将它显示在视觉媒体上的机制.这是CSS 2.1的一个基础概念.视觉格式化模型根据CSS盒模型为文档的每个元素生成0 ...

  5. Python面向对象之类的继承(2)

    1.除了封装,Python面向对象还有继承这一功能,如下代码,这是简单的继承功能. class Animal: def chi(self): print(self.name+' 吃') def he( ...

  6. re.search 与 re.match的区别

    search ⇒ find something anywhere in the string and return a match object. match ⇒ find something at ...

  7. LoadRunner中请求HTTPS页面。

    哎,真是服了.国内网站上写的解决方法如此的粗糙. 如果用loadrunner访问HTTPS网页时出现:shut connection during attempt to negotiate SSL s ...

  8. Codeforces Round #321 (Div. 2) C dfs处理(双向边叶子节点的判断)

    C. Kefa and Park time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. svg图片做图标字体

    https://icomoon.io 这个网站,把svg变图标

  10. jsp导出table数据excel表

    <html> <head> <meta http-equiv="content-Type" content="text/html;chars ...