summaryrefslogtreecommitdiff
path: root/tests/examplefiles/test.groovy
blob: 903863d27c72baa98c948c5fb741f1b6e43bea03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
    }
}