-
Notifications
You must be signed in to change notification settings - Fork 7
/
input.clj
178 lines (134 loc) · 5.65 KB
/
input.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
(ns com.yetanalytics.datasim.input
"Comprehensive specification of input"
(:require [clojure.spec.alpha :as s]
[com.yetanalytics.datasim.input.profile :as profile]
[com.yetanalytics.datasim.input.personae :as personae]
[com.yetanalytics.datasim.input.alignments :as alignments]
[com.yetanalytics.datasim.input.parameters :as params]
[com.yetanalytics.datasim.util.io :as dio]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Specs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(s/def ::profiles
::profile/profiles)
;; This is our system:
;; persona: a single Agent who is a member of a Group
;; personae: a Group that contains one or more Agent, i.e. persona
;; personae-array: an array of one or more Groups
(s/def ::personae-array
::personae/personae-array)
;; TODO: `::alignments` is the name of two separate things: the top-level
;; alignments input vector, and the inner vector associated with each actor.
;; Find separate names for each.
(s/def ::alignments
::alignments/alignment-vector)
(s/def ::parameters
::params/parameters)
(s/def :com.yetanalytics.datasim/input
(s/keys :req-un [::profiles
::personae-array
::alignments
::parameters]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Validation Functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; We need this function here since it's the only validation function that
;; makes use of two different parts of the input spec
(defn validate-pattern-filters
[{{:keys [gen-profiles gen-patterns]} :parameters
profiles :profiles}]
(concat (profile/validate-profile-filters profiles gen-profiles)
(profile/validate-pattern-filters profiles gen-patterns)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Input I/O
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- throw-unknown-key [type-k]
(throw (ex-info (format "Unknown key %s" type-k)
{:type ::unknown-key
:key type-k})))
(defn- throw-unknown-format [format-k]
(throw (ex-info (format "Unknown format %s" format-k)
{:type ::unknown-format
:format format-k})))
;; Read Input
(defmulti from-location
(fn [type-k format-k _] [type-k format-k]))
(defmethod from-location :default [type-k format-k _]
(if (#{:json} format-k)
(throw-unknown-key type-k)
(throw-unknown-format format-k)))
(defmethod from-location [:input :json] [_ _ location]
(-> (dio/read-json-location location)
(update :parameters params/apply-defaults)))
(defmethod from-location [:profile :json] [_ _ location]
(dio/read-json-location location))
(defmethod from-location [:profiles :json] [_ _ location]
(->> (dio/read-json-location location)
vec))
(defmethod from-location [:personae :json] [_ _ location]
(dio/read-json-location location))
(defmethod from-location [:personae-array :json] [_ _ location]
(->> (dio/read-json-location location)
vec))
(defmethod from-location [:alignments :json] [_ _ location]
(->> (dio/read-json-location location)
vec))
(defmethod from-location [:parameters :json] [_ _ location]
(->> (dio/read-json-location location)
params/apply-defaults))
;; Write to file
(defmulti to-file (fn [_ format-k _] format-k))
(defmethod to-file :default [_ format-k _]
(throw-unknown-format format-k))
(defmethod to-file :json [data _ location]
(dio/write-json-file data location))
;; Write to stdout
(defmulti to-out (fn [_ fmt-k] fmt-k))
(defmethod to-out :default [_ format-k _]
(throw-unknown-format format-k))
(defmethod to-out :json [data _]
(dio/write-json-stdout data))
;; Write to stderr
;; TODO: Currently unused
(defmulti to-err (fn [_ fmt-k] fmt-k))
(defmethod to-err :default [_ format-k _]
(throw-unknown-format format-k))
(defmethod to-err :json [data _]
(dio/write-json-stderr data))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Input Validation
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmulti validate
"Validate input using the FromInput protocol. Does no handling on result.
Returns a coll of maps on failure, `nil` on success."
(fn [type-k _] type-k))
(defmethod validate :default [type-k _]
(throw-unknown-key type-k))
(defmethod validate :input
[_ {:keys [profiles personae-array alignments parameters] :as input}]
(-> (concat (profile/validate-profiles profiles)
(personae/validate-personae-array personae-array)
(alignments/validate-alignments alignments)
(params/validate-parameters parameters)
(validate-pattern-filters input))
vec
not-empty))
(defmethod validate :profile [_ profile]
(profile/validate-profile profile))
(defmethod validate :personae [_ personae]
(personae/validate-personae personae))
(defmethod validate :alignments [_ alignments]
(alignments/validate-alignments alignments))
(defmethod validate :parameters [_ parameters]
(params/validate-parameters parameters))
(defn validate-throw
"Validate input using the FromInput protocol. Throw an exception if the input
isn't valid."
[type-k input]
(if-let [errors (not-empty (validate type-k input))]
(throw (ex-info (format "Validation Errors on %s" (name type-k))
{:type ::invalid-input
:key type-k
:input input
:errors errors}))
input))