Commit 222e8a9f authored by Ole Voldsæter's avatar Ole Voldsæter

Merge branch '49-use-function-arg-schema' into 'develop'

Resolve "Use new schema for function arguments"

Closes #49

See merge request c2metadata/stata-sdtl-converter!19
parents 6db64478 be6f669a
Pipeline #14324 passed with stages
in 1 minute and 52 seconds
......@@ -122,19 +122,19 @@
:mod-varlist (vec (map #(if (= % (first args)) (second args) %) *variables*))})
:filter (fn [& args] (first args))
:expression (fn [& args] (first args))
:disjunction (expr-transform "or" false)
:conjunction (expr-transform "and" false)
:equality_expression (expr-transform "eq" false)
:not_equal_expression (expr-transform "ne" false)
:ge_expression (expr-transform "ge" false)
:le_expression (expr-transform "le" false)
:lt_expression (expr-transform "lt" false)
:gt_expression (expr-transform "gt" false)
:additive_expression (expr-transform "addition" false)
:subtractive_expression (expr-transform "subtraction" false)
:multiplicative_expression (expr-transform "multiplication" false)
:divisive_expression (expr-transform "division" false)
:power (expr-transform "power" false)
:disjunction (expr-transform "or")
:conjunction (expr-transform "and")
:equality_expression (expr-transform "eq")
:not_equal_expression (expr-transform "ne")
:ge_expression (expr-transform "ge")
:le_expression (expr-transform "le")
:lt_expression (expr-transform "lt")
:gt_expression (expr-transform "gt")
:additive_expression (expr-transform "addition")
:subtractive_expression (expr-transform "subtraction")
:multiplicative_expression (expr-transform "multiplication")
:divisive_expression (expr-transform "division")
:power (expr-transform "power")
:negation (fn [& args]
(if (second args)
{:function "not"
......
......@@ -54,15 +54,20 @@
:severity "error"}]}
)
(defn make-var-struct [varname]
{:variableName varname , "$type" "VariableSymbolExpression"})
(defn make-varlist-expression [varlist]
{"$type" "VariableListExpression" :variables (map (fn [varname] {:variableName varname , "$type" "VariableSymbolExpression"}) (expand-varlist varlist *variables*))}
)
(defn make-compute [varname funcname args filter]
{:command "compute"
:variable varname
:expression {:function funcname
:isSdtlName true
:arguments args
:arguments (map-indexed
(fn [i arg] {:argumentName (str "EXP" (inc i))
:argumentValue arg})
args
)
"$type" "FunctionCallExpression"}
:condition filter
:mod-varlist (vec (conj *variables* varname))}
......@@ -81,30 +86,25 @@
varname
"rowpctile"
(let [p (:p options [[50]])]
(into
[(-> p (first) (first))]
(map make-var-struct (expand-varlist varlist *variables*)))
[(make-varlist-expression varlist)
{"$type" "NumericConstantExpression" :value (-> p first first)}]
)
filter)
:rowtotal (make-compute
varname
"rowtotal"
(into
[(not (nil? (get-var-length-option :missing 1 options)))]
(map make-var-struct (expand-varlist varlist *variables*)))
[(make-varlist-expression varlist)]
filter)
:tag (make-compute
varname
"tag"
(into
[(not (nil? (get-var-length-option :missing 1 options)))]
(map make-var-struct (expand-varlist varlist *variables*)))
[(make-varlist-expression varlist)]
filter)
; default
(make-compute
varname
(get compute-funcs (keyword funcname))
(map make-var-struct (expand-varlist varlist *variables*))
[(make-varlist-expression varlist)]
filter
)
)
......@@ -113,7 +113,11 @@
(defn varlist-aggregate [by-clause varname funcname varlist filter options]
(case funcname
:mode (unsupported-func funcname)
(make-aggregate by-clause varname (get aggregate-funcs (keyword funcname)) (map make-var-struct (expand-varlist varlist *variables*)) filter)
(make-aggregate
by-clause varname
(get aggregate-funcs (keyword funcname))
[(make-varlist-expression varlist)]
filter)
)
)
......@@ -128,11 +132,7 @@
)
(defn egen-fill [& [varname numlist filter]]
(make-compute
varname
"fill"
(expand-numlist numlist)
filter)
(unsupported-func "fill")
)
(defn egen-mtr [& [varname exp1 exp2 filter]]
......@@ -153,17 +153,30 @@
s (get-var-length-option :std 1 options)
m (if m (get options m) [[0]])
s (if s (get options s) [[1]])]
[(-> m first first) (-> s first first) expr]
[expr
{"$type" "NumericConstantExpression" :value (-> m first first)}
{"$type" "NumericConstantExpression" :value (-> s first first)}]
)
filter)
:pctile (make-aggregate by-clause varname (get aggregate-funcs (keyword funcname)) [expr (-> (:p options [[50]]) first first)] filter)
:pctile (make-aggregate
by-clause
varname
(get aggregate-funcs (keyword funcname))
[expr
{"$type" "NumericConstantExpression" :value (-> (:p options [[50]]) first first)}]
filter)
;;;----- weird ones -------------
:rank (unsupported-func funcname)
:pc (unsupported-func funcname)
;;; default
(make-aggregate by-clause varname (get aggregate-funcs (keyword funcname)) [expr] filter)
(make-aggregate
by-clause
varname
(get aggregate-funcs (keyword funcname))
[expr]
filter)
)
)
)
\ No newline at end of file
......@@ -4,24 +4,41 @@
} stata2sdtl.functions)
(def function-mapping
{"abs" "absolute_value"
"cos" "cos"
"exp" "exponentiation"
"log10" "logarithm_base_10"
"mod" "modulo"
"ln" "natural_logarithm"
"log" "natural_logarithm"
"runiform" "random_variable_uniform"
"round" "round"
"sin" "sine"
"sqrt" "square_root"
"missing" "missing_list"}
{"abs" "absolute_value"
"cos" "cos"
"exp" "exponentiation"
"log10" "logarithm_base_10"
"mod" "modulo"
"ln" "natural_logarithm"
"log" "natural_logarithm"
"runiform" "random_variable_uniform"
"round" "round"
"sin" "sine"
"sqrt" "square_root"
"missing" "missing_list"
"or" "or"
"and" "and"
"eq" "eq"
"ne" "ne"
"ge" "ge"
"le" "le"
"lt" "lt"
"gt" "gt"
"addition" "addition"
"subtraction" "subtraction"
"multiplication" "multiplication"
"division" "division"
"power" "power"}
)
(defn function-lookup [& args]
(let [canonical (get function-mapping (first args))]
{:function (or canonical (first args))
:arguments (rest args)
:arguments (map-indexed
(fn [i arg] {:argumentName (str "EXP" (inc i))
:argumentValue arg})
(rest args)
)
:isSdtlName (some? canonical)
"$type" "FunctionCallExpression"}
)
......
......@@ -4,6 +4,7 @@
} stata2sdtl.transform-helpers
(:require
[stata2sdtl.utils :refer :all]
[stata2sdtl.functions :refer [function-lookup]]
)
)
......@@ -26,13 +27,10 @@
)
)
(defn expr-transform [name native]
(defn expr-transform [name]
(fn [& args]
(if (second args)
{:function name
:arguments args
:isSdtlName (not native)
"$type" "FunctionCallExpression"}
(apply function-lookup name args)
(first args)
)
)
......
......@@ -111,51 +111,65 @@
)
(is
(= (parse-and-transform "generate a=!(2^-5)/5*3+7-x < 5 | y if 1")
{:command "compute"
:condition {"$type" "NumericConstantExpression"
:value 1}
:expression {"$type" "FunctionCallExpression"
:arguments [{"$type" "FunctionCallExpression"
:arguments [{"$type" "FunctionCallExpression"
:arguments [{"$type" "FunctionCallExpression"
:arguments [{"$type" "FunctionCallExpression"
:arguments [{"$type" "FunctionCallExpression"
:arguments [{"$type" "FunctionCallExpression"
:arguments [{"$type" "NumericConstantExpression"
:value 2}
{"$type" "NumericConstantExpression"
:value -5}]
:function "power"
:isSdtlName true}]
:function "not"
:isSdtlName true}
{"$type" "NumericConstantExpression"
:value 5}]
:function "division"
:isSdtlName true}
{"$type" "NumericConstantExpression"
:value 3}]
:function "multiplication"
:isSdtlName true}
{"$type" "FunctionCallExpression"
:arguments [{"$type" "NumericConstantExpression"
:value 7}
{"$type" "VariableSymbolExpression"
:variableName "x"}]
:function "subtraction"
:isSdtlName true}]
:function "addition"
:isSdtlName true}
{"$type" "NumericConstantExpression"
:value 5}]
:function "lt"
:isSdtlName true}
{"$type" "VariableSymbolExpression"
:variableName "y"}]
:function "or"
:isSdtlName true}
:mod-varlist ["a"]
:variables ["a"]})
'{:command "compute"
:condition {"$type" "NumericConstantExpression"
:value 1}
:expression {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "FunctionCallExpression"
:arguments ({"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "NumericConstantExpression"
:value 2}}
{:argumentName "EXP2"
:argumentValue {"$type" "NumericConstantExpression"
:value -5}})
:function "power"
:isSdtlName true})
:function "not"
:isSdtlName true}}
{:argumentName "EXP2"
:argumentValue {"$type" "NumericConstantExpression"
:value 5}})
:function "division"
:isSdtlName true}}
{:argumentName "EXP2"
:argumentValue {"$type" "NumericConstantExpression"
:value 3}})
:function "multiplication"
:isSdtlName true}}
{:argumentName "EXP2"
:argumentValue {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "NumericConstantExpression"
:value 7}}
{:argumentName "EXP2"
:argumentValue {"$type" "VariableSymbolExpression"
:variableName "x"}})
:function "subtraction"
:isSdtlName true}})
:function "addition"
:isSdtlName true}}
{:argumentName "EXP2"
:argumentValue {"$type" "NumericConstantExpression"
:value 5}})
:function "lt"
:isSdtlName true}}
{:argumentName "EXP2"
:argumentValue {"$type" "VariableSymbolExpression"
:variableName "y"}})
:function "or"
:isSdtlName true}
:mod-varlist ["a"]
:variables ["a"]})
"Should be a complex compute with condition"
)
(is
......
......@@ -8,23 +8,25 @@
(is
(= (make-compute "some-var" "min" [5] 5)
'{:command "compute"
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments [5]
:function "min"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue 5})
:function "min"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
)
)
(deftest make-aggregate-test
(is
(= (make-aggregate ["a" "b"] "some-var" :mean 5 5)
(= (make-aggregate ["a" "b"] "some-var" :mean [5] 5)
'{:aggregateVariables [{:command "compute"
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments 5
:arguments ({:argumentName "EXP1"
:argumentValue 5})
:function :mean
:isSdtlName true}
:variable "some-var"}]
......@@ -38,107 +40,121 @@
(deftest varlist-compute-test
(is
(= (varlist-compute "some-var" :rowpctile ["a" "b"] nil nil)
'{:command "compute"
'{:command "compute"
:condition nil
:expression {"$type" "FunctionCallExpression"
:arguments [50
{"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"}]
:function "rowpctile"
:isSdtlName true}
:expression {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "VariableListExpression"
:variables ({"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"})}}
{:argumentName "EXP2"
:argumentValue {"$type" "NumericConstantExpression"
:value 50}})
:function "rowpctile"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
)
(is
(= (varlist-compute "some-var" :rowpctile ["a" "b"] 5 {:p [[10]]})
'{:command "compute"
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments [10
{"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"}]
:function "rowpctile"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
'{:command "compute"
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "VariableListExpression"
:variables ({"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"})}}
{:argumentName "EXP2"
:argumentValue {"$type" "NumericConstantExpression"
:value 10}})
:function "rowpctile"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
)
(is
(= (varlist-compute "some-var" :rowtotal ["a" "b"] nil nil)
'{:command "compute"
:condition nil
:expression {"$type" "FunctionCallExpression"
:arguments [false
{"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"}]
:function "rowtotal"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
'{:command "compute"
:condition nil
:expression {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "VariableListExpression"
:variables ({"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"})}})
:function "rowtotal"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
)
(is
(= (varlist-compute "some-var" :rowtotal ["a" "b"] 5 {:missing nil})
'{:command "compute"
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments [true
{"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"}]
:function "rowtotal"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
'{:command "compute"
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "VariableListExpression"
:variables ({"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"})}})
:function "rowtotal"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
)
(is
(= (varlist-compute "some-var" :tag ["a" "b"] nil nil)
'{:command "compute"
:condition nil
:expression {"$type" "FunctionCallExpression"
:arguments [false
{"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"}]
:function "tag"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
:condition nil
:expression {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "VariableListExpression"
:variables ({"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"})}})
:function "tag"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
)
(is
(= (varlist-compute "some-var" :tag ["a" "b"] 5 {:missing nil})
'{:command "compute"
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments [true
{"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"}]
:function "tag"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "VariableListExpression"
:variables ({"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"})}})
:function "tag"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
)
(is
(= (varlist-compute "some-var" "diff" ["a" "b"] 5 nil)
'{:command "compute"
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments ({"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"})
:function "diff"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
:condition 5
:expression {"$type" "FunctionCallExpression"
:arguments ({:argumentName "EXP1"
:argumentValue {"$type" "VariableListExpression"
:variables ({"$type" "VariableSymbolExpression"
:variableName "a"}
{"$type" "VariableSymbolExpression"
:variableName "b"})}})
:function "diff"
:isSdtlName true}
:mod-varlist ["some-var"]
:variable "some-var"})
)
)
......@@ -146,75 +162,96 @@
(is
(= (egen-expr nil "somevar" "std" 5 1 nil)
'{:command "compute"
:condition 1
:expression {"$type" "FunctionCallExpression"
:arguments [0
1
5]
:function "std_val"
:isSdtlName true}