Permalink
Browse files

实现的基础activities

Signed-off-by: Yin MingJun <yinmingjuncn@gmail.com>
  • Loading branch information...
1 parent 2803f06 commit 3dbed70fa368f18bbe586d5d21ca66b1f24fa827 @yinmingjun committed Mar 30, 2012
@@ -0,0 +1,51 @@
+/*
+* jsWorkFlow's core source code.
+* 2012.03.22: Create By Yin Mingjun - email: yinmingjuncn@gmail.com
+*
+* Copyright 2012, Yin MingJun - email: yinmingjuncn@gmail.com
+* Dual licensed under the MIT or GPL Version 2 licenses.
+* http://jquery.org/license
+*
+*/
+
+Type.registerNamespace('jsWorkFlow.Activities');
+
+//////////////////////////////////////////////////////////////////////////////////////////
+//DelayActivity,延迟执行时间
+//
+//TO 开发者:
+// DelayActivity延迟(推迟)执行的时间,单位是ms。
+jsWorkFlow.Activities.DelayActivity = function jsWorkFlow_Activities_DelayActivity(delayTime) {
+ jsWorkFlow.Activities.DelayActivity.initializeBase(this);
+ this._delayTime = delayTime;
+};
+
+function jsWorkFlow_Activities_DelayActivity$dispose() {
+ jsWorkFlow.Activities.DelayActivity.callBaseMethod(this, 'dispose');
+}
+
+//activity的状态机的启动入口,自动驱动activity的状态机进入运行状态。
+function jsWorkFlow_Activities_DelayActivity$execute(context) {
+ jsWorkFlow.Activities.DelayActivity.callBaseMethod(this, 'execute', [context]);
+
+ var doDelay = Function.createDelegate(this, this.doDelay);
+
+ //通过lamda设置回调
+ jsWorkFlow.setTimeout(function () {
+ //LOG
+ $jwf.endActivity(context);
+ }, this._delayTime);
+}
+
+jsWorkFlow.Activities.DelayActivity.prototype = {
+ _delayTime: null,
+ dispose: jsWorkFlow_Activities_DelayActivity$dispose,
+ //property
+ //method
+ execute: jsWorkFlow_Activities_DelayActivity$execute
+
+};
+
+jsWorkFlow.Activities.DelayActivity.registerClass('jsWorkFlow.Activities.DelayActivity', jsWorkFlow.Activity);
+
+
@@ -0,0 +1,83 @@
+/*
+* jsWorkFlow's core source code.
+* 2012.03.22: Create By Yin Mingjun - email: yinmingjuncn@gmail.com
+*
+* Copyright 2012, Yin MingJun - email: yinmingjuncn@gmail.com
+* Dual licensed under the MIT or GPL Version 2 licenses.
+* http://jquery.org/license
+*
+*/
+
+Type.registerNamespace('jsWorkFlow.Activities');
+
+//////////////////////////////////////////////////////////////////////////////////////////
+//FunctionActivityEventArgs,提供jsWorkFlow的FunctionActivity的事件响应参数
+//
+// TO 开发者:
+//
+jsWorkFlow.Activities.FunctionActivityEventArgs = function jsWorkFlow_Activities.FunctionActivityEventArgs(context, callbackData) {
+ jsWorkFlow.Activities.FunctionActivityEventArgs.initializeBase(this, [context, callbackData]);
+};
+
+function jsWorkFlow_Activities.FunctionActivityEventArgs$dispose() {
+ jsWorkFlow.Activities.FunctionActivityEventArgs.callBaseMethod(this, 'dispose');
+}
+
+function jsWorkFlow_Activities.FunctionActivityEventArgs$get_callbackData() {
+ return this.get_data();
+}
+
+jsWorkFlow.Activities.FunctionActivityEventArgs.prototype = {
+ dispose: jsWorkFlow_Activities.FunctionActivityEventArgs$dispose,
+ //property
+ get_callbackData: jsWorkFlow_Activities_FunctionActivityEventArgs$get_callbackData
+};
+
+jsWorkFlow.Activities.FunctionActivityEventArgs.registerClass('jsWorkFlow.Activities.FunctionActivityEventArgs', jsWorkFlow.ActivityEventArgs);
+
+
+//////////////////////////////////////////////////////////////////////////////////////////
+//FunctionActivity,将function call封装到一个activity之中
+//
+//TO 开发者:
+// function的调用形式为function fun(FunctionActivityEventArgs) { ... }。
+//
+jsWorkFlow.Activities.FunctionActivity = function jsWorkFlow_Activities_FunctionActivity(func, callbackData) {
+ this._func = func;
+ this._callbackData = callbackData;
+ jsWorkFlow.Activities.FunctionActivity.initializeBase(this);
+};
+
+function jsWorkFlow_Activities_FunctionActivity$dispose() {
+ jsWorkFlow.Activities.FunctionActivity.callBaseMethod(this, 'dispose');
+}
+
+//activity的状态机的启动入口,自动驱动activity的状态机进入运行状态。
+function jsWorkFlow_Activities_FunctionActivity$execute(context) {
+ //执行基类execute方法,会进入start状态
+ jsWorkFlow.Activities.FunctionActivity.callBaseMethod(this, 'execute', [context]);
+
+ //同步执行function
+ var eventArgs = new jsWorkFlow.Activities.FunctionActivityEventArgs(context, this._callbackData);
+
+ var result = this._func(eventArgs);
+
+ //将执行结果放到context之中
+ context.set_result(result);
+
+ //执行完毕,结束activity
+ $jwf.endActivity(context);
+}
+
+jsWorkFlow.Activities.FunctionActivity.prototype = {
+ _func: null,
+ _callbackData: null,
+ dispose: jsWorkFlow_Activities_FunctionActivity$dispose,
+ //property
+ //method
+ execute: jsWorkFlow_Activities_FunctionActivity$execute
+};
+
+jsWorkFlow.Activities.FunctionActivity.registerClass('jsWorkFlow.Activities.FunctionActivity', jsWorkFlow.Activity);
+
+
@@ -0,0 +1,166 @@
+/*
+* jsWorkFlow's core source code.
+* 2012.03.29: Create By Yin Mingjun - email: yinmingjuncn@gmail.com
+*
+* Copyright 2012, Yin MingJun - email: yinmingjuncn@gmail.com
+* Dual licensed under the MIT or GPL Version 2 licenses.
+* http://jquery.org/license
+*
+*/
+
+Type.registerNamespace('jsWorkFlow.Activities');
+
+//////////////////////////////////////////////////////////////////////////////////////////
+//IfElseActivity
+//
+//TO 开发者:
+// IfElseActivity是一个if...then...else分支的activity,包含3个部分,conditionActivity、
+//thenActivity和elseActivity,conditionActivity提供if所需要的分支条件,如果分支条件返回结果
+//评估是true,执行thenActivity,否则执行elseActivity。
+// conditionActivity、thenActivity和elseActivity都可以为空。
+// IfElseActivity将执行的activity(thenActivity或elseActivity)作为自己的返回值。
+jsWorkFlow.Activities.IfElseActivity = function jsWorkFlow_Activities_IfElseActivity(conditionActivity, thenActivity, elseActivity) {
+ jsWorkFlow.Activities.IfElseActivity.initializeBase(this);
+
+ this.set_conditionActivity(conditionActivity);
+ this.set_thenActivity(thenActivity);
+ this.set_elseActivity(elseActivity);
+
+ this._doEvalConditionCompleteHandler = Function.createDeledate(this, this.doEvalConditionCompleteHandler);
+ this._doExecuteBodyCompleteHandler = Function.createDeledate(this, this.doExecuteBodyCompleteHandler);
+};
+
+function jsWorkFlow_Activities_IfElseActivity$dispose() {
+ this._conditionActivity = null;
+ this._elseActivity = null;
+ this._thenActivity = null;
+ jsWorkFlow.Activities.IfElseActivity.callBaseMethod(this, 'dispose');
+}
+
+function jsWorkFlow_Activities_IfElseActivity$get_conditionActivity() {
+ return this._conditionActivity;
+}
+
+function jsWorkFlow_Activities_IfElseActivity$set_conditionActivity(value) {
+ this._conditionActivity = value;
+}
+
+function jsWorkFlow_Activities_IfElseActivity$get_thenActivity() {
+ return this._thenActivity;
+}
+
+function jsWorkFlow_Activities_IfElseActivity$set_thenActivity(value) {
+ this._thenActivity = value;
+}
+
+function jsWorkFlow_Activities_IfElseActivity$get_elseActivity() {
+ return this._elseActivity;
+}
+
+function jsWorkFlow_Activities_IfElseActivity$set_elseActivity(value) {
+ this._elseActivity = value;
+}
+
+function jsWorkFlow_Activities_IfElseActivity$doEvalCondition(context) {
+ //如果没有设置条件,认为为false,执行else分支
+ var activity = this._conditionActivity;
+
+ if (!activity) {
+ this.doExecuteBody(context, false);
+ return;
+ }
+
+ var application = context.get_application();
+ var activityExecutor = new jsWorkFlow.ActivityExecutor(application, activity);
+
+ //将上下文的数据存放在activityExecutor之中
+ activityExecutor.parentContext = context;
+
+ activityExecutor.add_postComplete(this._doEvalConditionCompleteHandler);
+
+ //OK, kick activityExecutor to run!
+ activityExecutor.execute();
+}
+
+function jsWorkFlow_Activities_IfElseActivity$doEvalConditionCompleteHandler(eventArgs) {
+ var context = eventArgs.get_context();
+ var executor = context.get_executor();
+ var parentContext = executor.parentContext;
+
+ //从context取执行结果
+ var condition = context.get_result();
+
+ //将condition传递给doExecuteBody继续执行
+ this.doExecuteBody(parentContext, condition);
+}
+
+function jsWorkFlow_Activities_IfElseActivity$doExecuteBody(context, condition) {
+ //如果没有设置条件,认为为false,执行else分支
+ var activity = (condition)? this._thenActivity: this._elseActivity;
+
+ if (!activity) {
+ //没有activity,结束执行
+ $jwf.endActivity(context);
+ return;
+ }
+
+ var application = context.get_application();
+ var activityExecutor = new jsWorkFlow.ActivityExecutor(application, activity);
+
+ //将上下文的数据存放在activityExecutor之中
+ activityExecutor.parentContext = context;
+
+ activityExecutor.add_postComplete(this._doExecuteBodyCompleteHandler);
+
+ //OK, kick activityExecutor to run!
+ activityExecutor.execute();
+}
+
+function jsWorkFlow_Activities_IfElseActivity$doExecuteBodyCompleteHandler(eventArgs) {
+ var context = eventArgs.get_context();
+ var executor = context.get_executor();
+ var parentContext = executor.parentContext;
+
+ //将activity的最后的执行结果作为当前activity的执行结果
+ var result = context.get_result();
+ parentContext.set_result(result);
+
+ //结束activity的执行
+ $jwf.endActivity(context);
+ return;
+}
+
+
+//activity的状态机的启动入口,自动驱动activity的状态机进入运行状态。
+function jsWorkFlow_Activities_IfElseActivity$execute(context) {
+ jsWorkFlow.Activities.IfElseActivity.callBaseMethod(this, 'execute', [context]);
+
+ //从执行条件开始kick out
+ this.doEvalCondition(context);
+}
+
+jsWorkFlow.Activities.IfElseActivity.prototype = {
+ _conditionActivity: null,
+ _thenActivity: null,
+ _elseActivity: null,
+ _doEvalConditionCompleteHandler: null,
+ _doExecuteBodyCompleteHandler: null,
+ dispose: jsWorkFlow_Activities_IfElseActivity$dispose,
+ //property
+ get_conditionActivity: jsWorkFlow_Activities_IfElseActivity$get_conditionActivity,
+ set_conditionActivity: jsWorkFlow_Activities_IfElseActivity$set_conditionActivity,
+ get_thenActivity: jsWorkFlow_Activities_IfElseActivity$get_thenActivity,
+ set_thenActivity: jsWorkFlow_Activities_IfElseActivity$set_thenActivity,
+ get_elseActivity: jsWorkFlow_Activities_IfElseActivity$get_elseActivity,
+ set_elseActivity: jsWorkFlow_Activities_IfElseActivity$set_elseActivity,
+ //method
+ doEvalCondition: jsWorkFlow_Activities_IfElseActivity$doEvalCondition,
+ doEvalConditionCompleteHandler: jsWorkFlow_Activities_IfElseActivity$doEvalConditionCompleteHandler,
+ doExecuteBody: jsWorkFlow_Activities_IfElseActivity$doExecuteBody,
+ doExecuteBodyCompleteHandler: jsWorkFlow_Activities_IfElseActivity$doExecuteBodyCompleteHandler,
+ execute: jsWorkFlow_Activities_IfElseActivity$execute
+};
+
+jsWorkFlow.Activities.IfElseActivity.registerClass('jsWorkFlow.Activities.IfElseActivity', jsWorkFlow.Activity);
+
+
@@ -0,0 +1,49 @@
+/*
+* jsWorkFlow's core source code.
+* 2012.03.22: Create By Yin Mingjun - email: yinmingjuncn@gmail.com
+*
+* Copyright 2012, Yin MingJun - email: yinmingjuncn@gmail.com
+* Dual licensed under the MIT or GPL Version 2 licenses.
+* http://jquery.org/license
+*
+*/
+
+Type.registerNamespace('jsWorkFlow.Activities');
+
+//////////////////////////////////////////////////////////////////////////////////////////
+//NoopActivity
+//
+//TO 开发者:
+// 一个空操作(noop)的activity,仅用于展示activity得到基本控制流程,可以当成activity的占位
+//符使用。
+//
+jsWorkFlow.Activities.NoopActivity = function jsWorkFlow_Activities_NoopActivity() {
+ jsWorkFlow.Activities.NoopActivity.initializeBase(this);
+
+};
+
+function jsWorkFlow_Activities_NoopActivity$dispose() {
+ jsWorkFlow.Activities.NoopActivity.callBaseMethod(this, 'dispose');
+}
+
+//activity的状态机的启动入口,自动驱动activity的状态机进入运行状态。
+function jsWorkFlow_Activities_NoopActivity$execute(context) {
+ jsWorkFlow.Activities.NoopActivity.callBaseMethod(this, 'execute', [context]);
+
+ //TODO:
+ // LOG noop message!
+
+ //执行完毕,结束activity
+ $jwf.endActivity(context);
+}
+
+jsWorkFlow.Activities.NoopActivity.prototype = {
+ dispose: jsWorkFlow_Activities_NoopActivity$dispose,
+ //property
+ //method
+ execute: jsWorkFlow_Activities_NoopActivity$execute
+};
+
+jsWorkFlow.Activities.NoopActivity.registerClass('jsWorkFlow.Activities.NoopActivity', jsWorkFlow.Activity);
+
+
Oops, something went wrong. Retry.

0 comments on commit 3dbed70

Please sign in to comment.