-
Notifications
You must be signed in to change notification settings - Fork 7
/
template.clj
141 lines (122 loc) · 5.33 KB
/
template.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
(ns com.yetanalytics.datasim.xapi.profile.template
"Creation of `statement-base-map` and `parsed-rules-map` for Profile
compilation."
(:require [clojure.spec.alpha :as s]
[com.yetanalytics.pan.objects.template :as template]
[com.yetanalytics.datasim.xapi.rule :as rule]
[com.yetanalytics.datasim.xapi.profile :as-alias profile]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Specs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(s/def ::template ::template/template)
(s/def ::statement-base-map
(s/map-of ::template/id map?))
(s/def ::parsed-rules-map
(s/map-of ::template/id (s/every ::rule/parsed-rule)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Statement Base
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- activity-type->activity-base
[activity-type]
{"definition" {"type" activity-type}})
(defn- usage-type->attachment-base
[attachment-usage-type]
{"usageType" attachment-usage-type})
(s/fdef template->statement-base
:args (s/cat :template ::template/template)
:ret map?)
(defn template->statement-base
"Form the base of a statement from the Determining Properties of
the Template. Elements of array-valued properties (the context
activity types and the attachment usage types) are added in order."
[{profile-version-id :inScheme
verb-id :verb
object-activity-type :objectActivityType
category-activity-types :contextCategoryActivityType
grouping-activity-types :contextGroupingActivityType
parent-activity-types :contextParentActivityType
other-activity-types :contextOtherActivityType
attachment-usage-types :attachmentUsageType
;; TODO: StatementRef properties
;; object-statement-ref :objectStatementRefTemplate
;; context-statement-ref :contextStatementRefTemplate
}]
(cond-> {}
verb-id
(assoc-in ["verb" "id"] verb-id)
object-activity-type
(assoc-in ["object" "definition" "type"] object-activity-type)
category-activity-types
(assoc-in ["context" "contextActivities" "category"]
(mapv activity-type->activity-base category-activity-types))
grouping-activity-types
(assoc-in ["context" "contextActivities" "grouping"]
(mapv activity-type->activity-base grouping-activity-types))
parent-activity-types
(assoc-in ["context" "contextActivities" "parent"]
(mapv activity-type->activity-base parent-activity-types))
other-activity-types
(assoc-in ["context" "contextActivities" "other"]
(mapv activity-type->activity-base other-activity-types))
attachment-usage-types
(assoc-in ["attachments"]
(mapv usage-type->attachment-base attachment-usage-types))
profile-version-id ; always true
(update-in ["context" "contextActivities" "category"]
(fnil conj [])
{"id" profile-version-id})))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Statement Rule Application
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(s/fdef template->parsed-rules
:args (s/cat :type-iri-map ::type-iri-map
:activity-map ::activity-map
:template ::template/template)
:ret (s/every ::rule/parsed-rule))
(defn template->parsed-rules
"Return a collection of parsed rules derived from the template `rules`.
Uses the object Determining Properties to assist with rule parsing."
[{object-activity-type :objectActivityType
object-statement-ref :objectStatementRefTemplate
rules :rules}]
(cond
object-activity-type
(rule/parse-rules :activity-type rules)
object-statement-ref
(rule/parse-rules :statement-ref rules)
:else
(rule/parse-rules rules)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Profile Templates -> Statement Base + Parsed Rules
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(s/fdef create-statement-base-map
:args (s/cat :type-iri-map ::profile/type-iri-map)
:ret ::statement-base-map)
(defn create-statement-base-map
"Given Statement Templates in `type-iri-map`, return a map from those
templates' IDs to the base xAPI Statements they form from their
Determining Properties and inScheme."
[type-iri-map]
(-> type-iri-map
(get "StatementTemplate")
(update-vals template->statement-base)))
(s/fdef create-parsed-rules-map
:args (s/cat :type-iri-map? ::profile/type-iri-map)
:ret ::parsed-rules-map)
(defn create-parsed-rules-map
"Given Statement Templates in `type-iri-map`, return a map from those
templates' IDs to those their parsed rules"
[type-iri-map]
(-> type-iri-map
(get "StatementTemplate")
(update-vals template->parsed-rules)))
(s/fdef update-parsed-rules-map
:args (s/cat :profile-map ::profile/profile-map
:parsed-rules-map ::parsed-rules-map)
:ret ::parsed-rules-map)
(defn update-parsed-rules-map
"Use information from `profile-map` to complete the rules in
`parsed-rules-map` by adding additional valuesets or spec generators."
[profile-map parsed-rules-map]
(update-vals parsed-rules-map
(partial rule/add-rules-valuegen profile-map)))