<commit_before>;; Copyright © 2016 Dynamic Object Language Labs Inc.
;;
;; This software is licensed under the terms of the
;; Apache License, Version 2.0 which can be found in
;; the file LICENSE at the root of this distribution.

(ns plan-schema.coerce
  (:require [clojure.set :as set]
    #?(:clj
            [clojure.pprint :refer [pprint]]
       :cljs [cljs.pprint :refer [pprint]])))

; TODO network and network-id serve the same purpose.
; Consider network-id deprecated and removed in future.
; TODO :type and :tpn-type serve the same purpose.
; Consider :tpn-type deprecated and removed in future.

(defn check-value-nil? [key value]
  (if (nil? value)
    (println "Warning: nil value: " key "->" value)))

(defn to-keyword [key value]
  (check-value-nil? key value)
  {key (keyword value)})

(defn to-set-of-keywords [key value]
  {key (into #{} (map keyword value))})

(defn to-vector-of-keywords [key value]
  {key (into [] (map keyword value))})

(defn to-boolean [key value]
  (if-not (instance? Boolean value)
    (println "Warning: to-boolean. value is not boolean."))
  {key value})

;;; Functions for each HTN and/or TPN key ;;;
(defmulti convert-property "Coercion functions for each key" (fn [key value]
                                                               key))

(defmethod convert-property :default [key value]
  (println "Warning: convert-property: No conversion for" key "->" value)
  {key value})

(defmethod convert-property nil [key value]
  (println "Error: Bad key nil:" key "->" value)
  {key value})

(defmethod convert-property :network-id [key value]
  (to-keyword key value))

(defmethod convert-property :network [key value]
  (to-keyword key value))

(defmethod convert-property :uid [key value]
  (to-keyword key value))

(defmethod convert-property :begin-node [key value]
  (to-keyword key value))

(defmethod convert-property :end-node [key value]
  (to-keyword key value))

(defmethod convert-property :controllable [key value]
  (to-boolean key value))

(defmethod convert-property :constraints [key value]
  (to-set-of-keywords key value))

(defmethod convert-property :label [key value]
  ;(println "Warning: Verify me" key "->" value);TODO
  {key value})

(defmethod convert-property :name [key value]
  {key value})

(defmethod convert-property :htn-node [key value]
  (to-keyword key value))

(defmethod convert-property :tpn-type [key value]
  ;(println "Warning: deprecated key" key (to-keyword key value));TODO
  (to-keyword key value))

(defmethod convert-property :type [key value]
  (to-keyword key value))

(defmethod convert-property :argsmap [key value]
  {key value})

(defmethod convert-property :command [key value]
  ;(println "Warning: deprecated key" key)                   ;TODO
  {key value})

(defmethod convert-property :display-name [key value]
  {key value})

(defmethod convert-property :activities [key value]
  (to-set-of-keywords key value))

(defmethod convert-property :incidence-set [key value]
  (to-set-of-keywords key value))

(defmethod convert-property :value [key value]
  (to-set-of-keywords key value))

(defmethod convert-property :args [key value]
  {key value})

(defmethod convert-property :htn-expanded-nonprimitive-task [key value]
  (to-keyword key value))<commit_msg>Fixed nil value bug for bounds.<commit_after>;; Copyright © 2016 Dynamic Object Language Labs Inc.
;;
;; This software is licensed under the terms of the
;; Apache License, Version 2.0 which can be found in
;; the file LICENSE at the root of this distribution.

(ns plan-schema.coerce
  (:require [clojure.set :as set]
    #?(:clj
            [clojure.pprint :refer [pprint]]
       :cljs [cljs.pprint :refer [pprint]])))

; TODO network and network-id serve the same purpose.
; Consider network-id deprecated and removed in future.
; TODO :type and :tpn-type serve the same purpose.
; Consider :tpn-type deprecated and removed in future.

(defn check-value-nil? [key value]
  (if (nil? value)
    (println "Warning: nil value: " key "->" value)))

(defn to-keyword [key value]
  (check-value-nil? key value)
  {key (keyword value)})

(defn to-set-of-keywords [key value]
  {key (into #{} (map keyword value))})

(defn to-vector-of-keywords [key value]
  {key (into [] (map keyword value))})

(defn to-boolean [key value]
  (if-not (instance? Boolean value)
    (println "Warning: to-boolean. value is not boolean."))
  {key value})

;;; Functions for each HTN and/or TPN key ;;;
(defmulti convert-property "Coercion functions for each key" (fn [key value]
                                                               key))

(defmethod convert-property :default [key value]
  (println "Warning: convert-property: No conversion for" key "->" value)
  {key value})

(defmethod convert-property nil [key value]
  (println "Error: Bad key nil:" key "->" value)
  {key value})

(defmethod convert-property :network-id [key value]
  (to-keyword key value))

(defmethod convert-property :network [key value]
  (to-keyword key value))

(defmethod convert-property :uid [key value]
  (to-keyword key value))

(defmethod convert-property :begin-node [key value]
  (to-keyword key value))

(defmethod convert-property :end-node [key value]
  (to-keyword key value))

(defmethod convert-property :controllable [key value]
  (to-boolean key value))

(defmethod convert-property :constraints [key value]
  (to-set-of-keywords key value))

(defmethod convert-property :label [key value]
  ;(println "Warning: Verify me" key "->" value);TODO
  {key value})

(defmethod convert-property :name [key value]
  {key value})

(defmethod convert-property :htn-node [key value]
  (to-keyword key value))

(defmethod convert-property :tpn-type [key value]
  ;(println "Warning: deprecated key" key (to-keyword key value));TODO
  (to-keyword key value))

(defmethod convert-property :type [key value]
  (to-keyword key value))

(defmethod convert-property :argsmap [key value]
  {key value})

(defmethod convert-property :command [key value]
  ;(println "Warning: deprecated key" key)                   ;TODO
  {key value})

(defmethod convert-property :display-name [key value]
  {key value})

(defmethod convert-property :activities [key value]
  (to-set-of-keywords key value))

(defmethod convert-property :incidence-set [key value]
  (to-set-of-keywords key value))

(defmethod convert-property :value [key value]
  {key value})

(defmethod convert-property :args [key value]
  {key value})

(defmethod convert-property :htn-expanded-nonprimitive-task [key value]
  (to-keyword key value))
{'fraction_non_alphanumeric': 0.1287264944198135, 'fraction_numerical': 0.001834581868215869, 'mean_word_length': 3.7926739926739925, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3922148', 'n_tokens_mistral': 2094, 'n_tokens_neox': 2056, 'n_words': 721}