Commit e6e2480f authored by Joachim Schunk's avatar Joachim Schunk
Browse files

delete unneeded files & functions

parent d3937e10
Pipeline #106480 passed with stages
in 2 minutes and 14 seconds
(ns leukipp.components.exercise-types.calculation.migrate
(:require
[leukipp.components.exercise-types.multistep-calculation.migrate
:refer
[convert-number-to-object modify-exercise-field-vec set-current-scheme]]))
(def exercise-scheme-version "2020-09-07")
(defn modify-calculations [exercise]
(partial modify-exercise-field-vec exercise [:core :calculations]))
(defn set-missing-error-relative [exercise]
((modify-calculations exercise) #(assoc % :error-relative (or (:error-relative %) 1))))
(defn convert-numbers [exercise]
((modify-calculations exercise) convert-number-to-object))
(defn normalize-units [exercise]
(if (get-in exercise [:core :with-units])
((modify-calculations exercise) #(assoc % :units (or (:units %) [])))
((modify-calculations exercise) #(dissoc % :units))))
(defn remove-unneeded-ooms [exercise]
(if-not (get-in exercise [:core :with-oom])
((modify-calculations exercise) #(dissoc % :order-of-magnitude))
exercise))
(defn remove-unneeded-error-keys [exercise]
((modify-calculations exercise) #(dissoc % :error)))
(defn migrate-exercise [exercise]
(->> exercise
remove-unneeded-error-keys
remove-unneeded-ooms
normalize-units
convert-numbers
set-missing-error-relative
(set-current-scheme "calculation" exercise-scheme-version)))
(ns leukipp.components.exercise-types.calculation.migrate-answer
(:require
[clojure.set :refer [difference]]
[leukipp.components.exercise-types.multistep-calculation.migrate
:refer
[convert-number-to-object migrate-object]]))
(def answer-scheme-version "2020-09-07")
(defn set-current-scheme [answer]
(assoc answer :answer-scheme {:type "calculation" :version answer-scheme-version}))
(defn convert-calculation-numbers [answer]
(update answer :calculations (fn [calcs] (migrate-object convert-number-to-object calcs))))
(defn add-missing-calculation-objects [calculation-ids answer]
(let [calculations (:calculations answer)
missing-ids (difference calculation-ids (set (keys calculations)))]
(update answer :calculations (fn [calcs] (reduce (fn [res k] (assoc res k {:number nil}))
calcs missing-ids)))))
(defn wrap-answer-calculations [answer]
(if-not (contains? answer :calculations)
(let [wrapped-answer {}]
(assoc wrapped-answer :calculations answer))
answer))
(defn migrate-answer [calculation-ids answer]
(->> answer
wrap-answer-calculations
(add-missing-calculation-objects calculation-ids)
convert-calculation-numbers
set-current-scheme))
(ns leukipp.components.exercise-types.multistep-calculation.migrate
(:require
[clojure.string :as string]
[clojure.tools.reader.edn :as edn]
[leukipp.components.helper :refer [find-in-vec-by-id]]))
(def exercise-scheme-version "2019-11-22")
(defn set-current-scheme [type version exercise]
(-> exercise
(assoc-in [:authoring :migration-status] :current)
(assoc :exercise-scheme {:type type :version version})))
(defn normalize-decimal-separator [number-string]
(if (string? number-string)
(string/replace number-string #"," ".")
number-string))
(defn migrate-object [migrate-fn obj]
(reduce-kv (fn [res-obj obj-k obj-v] (assoc res-obj obj-k (migrate-fn obj-v))) {} obj))
(defn migrate-vector [migrate-fn vec]
(reduce (fn [res-vec obj] (conj res-vec (migrate-fn obj))) [] vec))
(defn convert-to-precision [precision-key offset obj]
(if (contains? obj precision-key)
(if-let [precision (precision-key obj)]
(-> obj
(dissoc precision-key)
(assoc :precision (str (+ (edn/read-string precision) offset))))
(dissoc obj precision-key))
obj))
(defn convert-number-obj [obj]
(if (contains? obj :number)
(let [significand (normalize-decimal-separator (:number obj))
order-of-magnitude (or (:order-of-magnitude obj) "0")]
{:significand significand :order-of-magnitude order-of-magnitude})
obj))
(defn convert-number-to-object [obj]
(if (contains? obj :number)
(if-not (map? (:number obj))
(let [significand (normalize-decimal-separator (:number obj))
order-of-magnitude (or (:order-of-magnitude obj) "0")]
(-> obj
(dissoc :order-of-magnitude)
(assoc :number {:significand significand :order-of-magnitude order-of-magnitude})))
(dissoc obj :order-of-magnitude))
obj))
(defn extract-identifier [id]
(let [id-string (if (keyword? id) (name id) id)]
(when (not-empty id-string)
(last (re-find #"(^[a-z]+)(\d{4}.*$)" id-string)))))
(defn generate-id [identifier prefix-map]
(when identifier
(when-let [new-prefix (get prefix-map identifier)]
(keyword (str new-prefix identifier)))))
(defn modify-exercise-field-vec [exercise path mod-fn]
(update-in exercise path (partial migrate-vector mod-fn)))
(defn select-core-keys [exercise]
(let [base-keys [:task-description :type :with-oom :with-units :hide-assignments :phrasing :assocs
:params :assignments :steps]]
(update exercise :core #(select-keys % base-keys))))
(defn select-assoc-keys [exercise]
(let [base-keys [:id :sign :description]]
(modify-exercise-field-vec exercise [:core :assocs] (fn [assoc] (select-keys assoc base-keys)))))
(defn select-param-keys [exercise]
(let [base-keys [:id :sign :idx :assoc-id :units]]
(modify-exercise-field-vec exercise [:core :params]
(fn [param] (if (contains? param :variable-id)
(select-keys param (conj base-keys :variable-id))
(select-keys param (conj base-keys :number)))))))
(defn select-assignment-keys [exercise]
(let [base-keys [:id :sign :idx :type :units]]
(modify-exercise-field-vec exercise [:core :assignments]
(fn [assignment]
(case (:type assignment)
:formula (select-keys assignment (concat base-keys [:formula :calc-mapping]))
:constant (select-keys assignment base-keys)
assignment)))))
(defn select-step-keys [exercise]
(let [assignments (get-in exercise [:core :assignments])
base-keys [:id :assoc-id :assignment-id]]
(modify-exercise-field-vec exercise [:core :steps]
(fn [step] (case (:type (find-in-vec-by-id assignments (:assignment-id step)))
:formula (select-keys step (conj base-keys :var-mapping))
:constant (select-keys step (conj base-keys :number))
nil (select-keys step base-keys)
step)))))
(defn select-variable-keys [exercise]
(let [base-keys [:id :human-id :type :data]]
(modify-exercise-field-vec exercise [:variables] (fn [variable] (select-keys variable base-keys)))))
(defn select-calc-mapping-keys [exercise]
(let [base-keys [:units :precision]]
(modify-exercise-field-vec exercise [:core :assignments]
(fn [assignment]
(if (:calc-mapping assignment)
(update assignment :calc-mapping
(partial migrate-object
(fn [calc-mapping] (select-keys calc-mapping base-keys))))
assignment)))))
(defn select-var-mapping-keys [exercise]
(let [base-keys [:var-type :var-id]]
(modify-exercise-field-vec exercise [:core :steps]
(fn [step]
(if (:var-mapping step)
(update step :var-mapping
(partial migrate-object
(fn [var-mapping] (select-keys var-mapping base-keys))))
step)))))
(defn convert-var-mapping-id [exercise]
(modify-exercise-field-vec exercise [:core :steps]
(fn [step]
(let [var-mapping (:var-mapping step)]
(if (not-empty var-mapping)
(update step :var-mapping
(partial migrate-object
(fn [var-map]
(if-let [var-id (:id var-map)]
(-> var-map
(dissoc :id)
(assoc :var-id var-id))
(dissoc var-map :id)))))
step)))))
(defn convert-assignments-precision [exercise]
(modify-exercise-field-vec exercise [:core :assignments]
(fn [assignment]
(if (:calc-mapping assignment)
(update assignment :calc-mapping
(partial migrate-object
(partial convert-to-precision :decimal-places 0)))
assignment))))
(defn convert-variables-precision [exercise]
(modify-exercise-field-vec exercise [:variables]
(fn [variable]
(if (= (:type variable) :range)
(update variable :data (partial convert-to-precision :significant-digits -1))
variable))))
(defn convert-variable-numbers [exercise]
(modify-exercise-field-vec exercise [:variables]
(fn [var]
(case (:type var)
:range (update var :data
(partial reduce-kv
(fn [res-obj obj-k obj-v]
(if (#{:min :max} obj-k)
(assoc res-obj obj-k (convert-number-obj obj-v))
(assoc res-obj obj-k obj-v)))
{}))
:list (update var :data
(partial migrate-vector
convert-number-to-object))))))
(defn convert-numbers [field-key exercise]
(modify-exercise-field-vec exercise [:core field-key] convert-number-to-object))
(defn rename-and-collect-ids [path prefix {:keys [exercise prefix-map]}]
(loop [field-vec (get-in exercise path)
idx (dec (count field-vec))
prefix-map prefix-map]
(if (neg? idx)
{:exercise (assoc-in exercise path field-vec)
:prefix-map prefix-map}
(let [identifier (extract-identifier (get-in field-vec [idx :id]))
modified-field-vec (assoc-in field-vec [idx :id] (str prefix identifier))
modified-prefix-map (assoc prefix-map identifier prefix)]
(recur modified-field-vec (dec idx) modified-prefix-map)))))
(defn rename-param-id-refs [{:keys [exercise prefix-map]}]
{:exercise
(modify-exercise-field-vec exercise [:core :params]
(fn [param]
(let [assoc-identifier (extract-identifier (:assoc-id param))]
(if-let [variable-identifier (extract-identifier (:variable-id param))]
(-> param
(assoc :assoc-id (generate-id assoc-identifier prefix-map))
(assoc :variable-id (generate-id variable-identifier prefix-map)))
(assoc param :assoc-id (generate-id assoc-identifier prefix-map))))))
:prefix-map prefix-map})
(defn rename-step-id-refs [{:keys [exercise prefix-map]}]
{:exercise
(modify-exercise-field-vec
exercise [:core :steps]
(fn [step]
(let [assoc-identifier (extract-identifier (:assoc-id step))
assignment-identifier (extract-identifier (:assignment-id step))]
(if-let [var-mapping (:var-mapping step)]
(let [modified-var-mapping
(migrate-object
(fn [m]
(let [var-identifier (extract-identifier (:var-id m))]
(assoc m :var-id (generate-id var-identifier prefix-map))))
var-mapping)]
(-> step
(assoc :var-mapping modified-var-mapping)
(assoc :assoc-id (generate-id assoc-identifier prefix-map))
(assoc :assignment-id (generate-id assignment-identifier prefix-map))))
(-> step
(assoc :assoc-id (generate-id assoc-identifier prefix-map))
(assoc :assignment-id (generate-id assignment-identifier prefix-map)))))))
:prefix-map prefix-map})
(defn rename-authoring-keys [{:keys [exercise prefix-map]}]
{:exercise
(update exercise :authoring
#(reduce-kv (fn [res-obj obj-k obj-v]
(let [key-identifier (extract-identifier obj-k)]
(if key-identifier
(assoc res-obj (generate-id key-identifier prefix-map) obj-v)
(assoc res-obj obj-k obj-v))))
{} %))})
(defn rename-ids [exercise]
(:exercise (->> {:exercise exercise :prefix-map {}}
(rename-and-collect-ids [:variables] "mscv")
(rename-and-collect-ids [:core :assocs] "msca")
(rename-and-collect-ids [:core :params] "mscp")
(rename-and-collect-ids [:core :assignments] "mscai")
(rename-and-collect-ids [:core :steps] "mscs")
rename-param-id-refs
rename-step-id-refs
rename-authoring-keys)))
(defn migrate-exercise [exercise]
(->> exercise
convert-var-mapping-id
convert-assignments-precision
convert-variables-precision
convert-variable-numbers
(convert-numbers :params)
(convert-numbers :assignments)
(convert-numbers :steps)
select-core-keys
select-assoc-keys
select-param-keys
select-assignment-keys
select-step-keys
select-variable-keys
select-calc-mapping-keys
select-var-mapping-keys
rename-ids
(set-current-scheme "multistep-calculation" exercise-scheme-version)))
(ns leukipp.components.exercise-types.multistep-calculation.migrate-answer)
(def answer-scheme-version "2020-05-20")
(defn set-current-scheme [answer]
(assoc answer :answer-scheme {:type "multistep-calculation" :version answer-scheme-version}))
(defn wrap-answer-steps [answer]
(if-not (contains? answer :steps)
(let [wrapped-answer {}]
(assoc wrapped-answer :steps answer))
answer))
(defn vectorize-var-mappings [answer]
(update answer :steps
#(reduce (fn [res step]
(conj res
(if-let [var-mapping (:var-mapping step)]
(-> step
(dissoc :var-mapping)
(assoc :vars (reduce-kv (fn [res _ v] (conj res v)) [] var-mapping)))
step)))
[] %)))
(defn migrate-answer [answer]
(-> answer
wrap-answer-steps
vectorize-var-mappings
set-current-scheme))
......@@ -2,14 +2,7 @@
#?@
(:clj
[(:require
[clojure.math.numeric-tower :as math]
[clojure.tools.logging :as log])]))
(defn debug-message [message]
#?(:clj
(log/debug message)
:cljs
(js/console.debug message)))
[clojure.math.numeric-tower :as math])]))
(defn double-division
"returns double-value in any case"
......@@ -80,49 +73,6 @@
(defn find-keys-by-val [map val]
(reduce-kv (fn [res k v] (if (= v val) (conj res k) res)) #{} map))
(defn scheme-version-check [subject scheme-key current-version]
(let [subject-version (or (get-in subject [scheme-key :version]) "")
valid-subject-version? (re-matches #"^\d{4}-\d{2}-\d{2}$" subject-version)
valid-current-version? (re-matches #"^\d{4}-\d{2}-\d{2}$" current-version)]
(if (and valid-subject-version? valid-current-version?)
(let [comp-val (compare subject-version current-version)]
(if (pos? comp-val)
(do
(debug-message "Incompatible scheme-versions (subject-version more recent than current-version)!")
:incompatible)
(if (neg? comp-val) :migrate :current)))
(do (when-not valid-subject-version? (debug-message "Invalid subject-scheme-version declaration!"))
(when-not valid-current-version? (debug-message "Invalid current-scheme-version declaration!"))
:invalid))))
(defn scheme-type-check [subject scheme-key valid-type]
(if (and (map? subject) (contains? subject scheme-key))
(if (= (get-in subject [scheme-key :type]) valid-type)
:identical
(do
(debug-message "Subject-scheme-version is different from valid-scheme-type!")
:different))
:missing))
(defn get-migration-status [type-status subject scheme-key current-version]
(case type-status
:identical (scheme-version-check subject scheme-key current-version)
:missing :migrate
nil))
(defn set-migration-status [exercise scheme-key valid-type current-version]
(let [type-status (scheme-type-check exercise scheme-key valid-type)
migration-status (get-migration-status type-status exercise scheme-key current-version)]
(assoc-in exercise [:authoring :migration-status] migration-status)))
(defn migrate-if-necessary [subject scheme-key valid-type current-version migrate-fn]
(let [type-status (scheme-type-check subject scheme-key valid-type)
migration-status (get-migration-status type-status subject scheme-key current-version)]
(case migration-status
:migrate (migrate-fn subject)
:current subject
nil)))
(defn dissoc-in
([map path]
(let [last-key (last path)]
......
(ns leukipp.components.exercise-types.calculation.components.migration
(:require
[leukipp.components.exercise-types.calculation.migrate
:refer [migrate-exercise]]))
(defn migrate-exercise-atom [exercise-atom]
(reset! exercise-atom (migrate-exercise @exercise-atom)))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment