-
Notifications
You must be signed in to change notification settings - Fork 2
/
core.clj
147 lines (115 loc) · 4.02 KB
/
core.clj
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
(ns vent.core
(:require
[vent.util
:refer [invoke-highest-arity
deep-merge]]))
(defprotocol Action
(execute [this context]))
(defprotocol Gatherer
(add-context-to [this context]))
(defrecord MergingFunctionBackedGatherer [f merger]
Gatherer
(add-context-to [_ context]
(let [merger (or merger deep-merge)
additional (invoke-highest-arity f context)]
(merger context additional))))
(defmacro gatherer [bindings & rest]
`(map->MergingFunctionBackedGatherer
{:f (fn ~bindings ~@rest)}))
(defrecord FunctionBackedAction [f]
Action
(execute [_ context]
(invoke-highest-arity f context)))
(defmacro action [bindings & rest]
`(->FunctionBackedAction (fn ~bindings ~@rest)))
(def ^:private default-event-type-fn
(fn [event] (keyword (get-in event [:payload :type]))))
(def ^:private default-event-channel-fn
(fn [event] (keyword (get-in event [:channel]))))
(defn- having-key [seq key]
(filterv #(contains? % key) seq))
(defn- merge-over [seq-of-maps]
(apply merge seq-of-maps))
(defn- collect-from
([fragments key] (collect-from fragments key :defaults {}))
([fragments key & {:keys [defaults]}]
(let [relevant (concat [{key defaults}] (having-key fragments key))
extracted {key (merge-over (mapv key relevant))}]
extracted)))
(defn- return-on-match [rule event {:keys [event-type-fn]}]
(and
(= (:event-type rule) (event-type-fn event))
rule))
(defn- resolve-handlers [rule handlers-key event context]
(map
#(invoke-highest-arity (:handler %) event context)
(handlers-key rule)))
(defn create-ruleset [& fragments]
(let [options (collect-from fragments :options
:defaults {:event-channel-fn default-event-channel-fn
:event-type-fn default-event-type-fn})
rules (collect-from fragments :rules)]
(merge options rules)))
(defmacro defruleset [name & forms]
`(def ~name
(create-ruleset ~@forms)))
(defn create-plan [& {:keys [gatherers actions]
:or {gatherers []
actions []}}]
{:gatherers (into [] gatherers)
:actions (into [] actions)})
(defn options [& {:as options}]
{:options options})
(defn from [channel & event-rules]
{:rules {(keyword channel) event-rules}})
(defn on [event-type & handlers]
{:event-type event-type
:gatherer-handlers (filterv #(= (:type %) :gatherer) handlers)
:action-handlers (filterv #(= (:type %) :action) handlers)})
(defn act [act-handler]
{:type :action
:handler act-handler})
(defn gather [gather-handler]
{:type :gatherer
:handler gather-handler})
(defn- rule->plan [event context]
(fn [rule]
(let [gatherers
(resolve-handlers
rule :gatherer-handlers
event context)
actions
(resolve-handlers
rule :action-handlers
event context)]
(create-plan
:gatherers gatherers
:actions actions))))
(defn- rule-for-event? [event options]
(fn [rule] (return-on-match rule event options)))
(defn determine-plans [ruleset event context]
(let [{:keys [options rules]} ruleset
{:keys [event-channel-fn]} options
channel-rules (or ((event-channel-fn event) rules) [])
event-rules
(filterv (rule-for-event? event options) channel-rules)
event-plans
(mapv (rule->plan event context) event-rules)]
event-plans))
(defn execute-action [action context]
(execute action context))
(defn execute-gatherers [gatherers context]
(reduce
(fn [accumulated-context gatherer]
(add-context-to gatherer accumulated-context))
context
gatherers))
(defn execute-plan [plan context]
(let [full-context
(execute-gatherers (:gatherers plan) context)]
(mapv #(execute-action % full-context) (:actions plan))))
(defn execute-plans [plans context]
(mapv #(execute-plan % context) plans))
(defn react-to [ruleset event context]
(let [plans (determine-plans ruleset event context)]
(execute-plans plans context)))