summaryrefslogtreecommitdiff
path: root/tests/examplefiles/test.groovy
diff options
context:
space:
mode:
Diffstat (limited to 'tests/examplefiles/test.groovy')
-rw-r--r--tests/examplefiles/test.groovy97
1 files changed, 97 insertions, 0 deletions
diff --git a/tests/examplefiles/test.groovy b/tests/examplefiles/test.groovy
new file mode 100644
index 00000000..903863d2
--- /dev/null
+++ b/tests/examplefiles/test.groovy
@@ -0,0 +1,97 @@
+// This source code comes from http://www.odelia-technologies.com/node/200
+
+package com.odelia.groovy.simpleworkflow
+
+
+class SimpleWorkflowEngine {
+ def workflowMap = [:]
+ def context = [:]
+ def beforeActivityName = 'beforeActivity'
+ def afterActivityName = 'afterActivity'
+
+ SimpleWorkflowEngine(workflow, context = [:]) {
+ this.context = context
+ parseWorkflow(workflow)
+ }
+
+ def parseWorkflow(workflow) {
+ workflowMap = new WorkflowParser().parse(workflow)
+ }
+
+ def getActivityValue(activity) {
+ assert activity instanceof String
+ if (!workflowMap[activity])
+ throw new RuntimeException("$activity activity doesn't exist")
+ workflowMap[activity]
+ }
+
+ def execute(activity, pause) {
+ if (workflowMap[beforeActivityName]) {
+ getActivityValue(beforeActivityName)(context, activity)
+ }
+
+ def activityValue = getActivityValue(activity)
+
+ // Determine the next activity to execute
+ def nextActivity
+ switch (activityValue) {
+ case String: nextActivity = activityValue; break
+ case Closure: nextActivity = activityValue(context); break
+ case Class: nextActivity = activityValue.newInstance()(context)
+ }
+
+ if (workflowMap[afterActivityName]) {
+ getActivityValue(afterActivityName)(context, activity, nextActivity)
+ }
+
+ if (!pause && nextActivity)
+ call(nextActivity)
+ else
+ nextActivity
+ }
+
+ def call(activity) {
+ execute(activity, false)
+ }
+
+ def nextActivity(activity) {
+ execute(activity, true)
+ }
+
+ static void main(String[] args) {
+ if (args.size() != 2) {
+ println 'Usage: com.odelia.groovy.simpleworkflow.SimpleWorkflowEngine <dsl_filename> <activity_name>'
+ return
+ }
+ SimpleWorkflowEngine.newInstance(new File(args[0]))(args[1])
+ }
+
+}
+
+private class WorkflowParser {
+ def map = [:]
+
+ def methodMissing(String name, args) {
+ map[name] = args[0]
+ }
+
+ def parse(Closure wf) {
+ wf.delegate = this
+ wf.resolveStrategy = Closure.DELEGATE_FIRST
+ wf()
+ map
+ }
+
+ def workflow = { it ->
+ it.delegate = this
+ it.resolveStrategy = Closure.DELEGATE_FIRST
+ it()
+ }
+
+ def parse(File workflowDef) {
+ def binding = new Binding([workflow: workflow])
+ def shell = new GroovyShell(binding)
+ shell.evaluate(workflowDef)
+ map
+ }
+} \ No newline at end of file