规则引擎语言基础
规则引擎数据处理
; 指令配置
(cmd_set key value)
(cmd_set key1 key2 value)
(cmd_set key1 key2 key3 value)
; 给源设备发 RPC 指令
(source_rpc cmd_name)
; 给目标设备发 RPC 指令
(target_rpc cmd_name)
; 获取源设备 meta 信息
(source_meta key)
; 获取目标设备 meta 信息
(target_meta key)
; 发送通知
(message cmd_name)
; 获取实时属性或实时数据
(metric key)
; 异常分析
(anomaly key [with_timestamp])
; 异常分析并发送通知
(anomaly key threshold msg_cmd_name [with_timestamp])
; 异常分析并预测下一次数据
(anomaly_and_predict key [with_timestamp])
; 异常分析并预测下一次数据并通知
(anomaly_and_predict key threshold msg_cmd_name [with_timestamp])
; 获取当前设备属性
"meta_key")
(fetch_target_meta "meta_key") (fetch_source_meta
规则引擎语言基础
字符串类型
"string1" "string2" "string3")
(str ; "string1string2string3"
"string")
(seq ; ("s" "t" "r" "i" "n" "g")
apply str "" (list "s" "t" "r" "i" "n" "g"))
(; "string"
"string")
(string? ; true
(string? :keyword); false
= "string" "string")
(; true
= "string" "string1")
(; false
关键字类型
(keyword? :keyword); true
keyword "keyword")
(; :keyword
keyword :keyword)
(; :keyword
keyword "keyword"))
(keyword? (; true
= :keyword (keyword "keyword")
(; true
布尔类型
(true? true); true
(false? false); true
= true true)
(; true
= true false)
(; false
= false false)
(; true
(str true); "true"
(str false); "false"
(str true false); "truefalse"
数值类型
1)
(number? ; true
0.1)
(number? ; true
+ 1 1)
(; 2.0
- 2 1)
(; 1.0
* 2 3)
(; 6.0
/ 3 2)
(; 1.5
/ 3e2 2)
(; 150.0
= 2 2)
(; true
> 3 2)
(; true
>= 3 2)
(; true
<= 3 2)
(; false
< 3 2)
(; false
序列类型
序列类型分为 vector
与 list
两种类型
list 1 2 3 4))
(sequential? (; true
vector 1 2 3 4))
(sequential? (; true
= (vector 1 2 3 4) (list 1 2 3 4))
(; true
Vector 类型
"abc" "def"])
(vector? [; true
vector "abc" "def"))
(vector? (; true
vector "abc" "def")
(; ["abc" "def"]
"abc" "def"])
(seq [; ("abc" "def")
= ["abc" "def"] (vector "abc" "def"))
(; true
map (fn* [a] (* 2 a)) [1,2,3,4])
(; (2.0 4.0 6.0 8.0)
nth [1,2,3,4] 2)
(; 3.0
first [1,2,3,4])
(; 1.0
rest [1,2,3,4])
(; (2.0 3.0 4.0)
count nil)
(; 0.0
count [1,2,3,4])
(; 4.0
1,2,3,4] 5 6)
(conj [; [1.0 2.0 3.0 4.0 5.0 6.0]
cons 1 [2,3,4])
(; (1.0 2.0 3.0 4.0)
1,2,3,4])
(concat [; (1.0 2.0 3.0 4.0)
(empty? []); true
apply str "" ["s","t","r","i"m"n"m"g"])
(; "string"
List 类型
list 1 2 3))
(list? (; true
1,2,3,4]))
(list? (seq [; true
= (list 1 2 3 4) (seq [1,2,3,4]))
(; true
1,2,3,4]) 5 6)
(conj (seq [; (6.0 5.0 1.0 2.0 3.0 4.0)
cons 1 (list 2 3 4))
(; (1.0 2.0 3.0 4.0)
1,2,3,4]))
(concat (seq [; (1.0 2.0 3.0 4.0)
nth (seq [1,2,3,4) 2)
(; 3.0
first (seq [1,2,3,4]))
(; 1.0
rest (seq [1,2,3,4]))
(; (2.0 3.0 4.0)
count nil)
(; 0.0
count (seq [1,2,3,4]))
(; 4.0
list))
(empty? (; true
nil)
(empty? ; true
apply str "" (list "s" "t" "r" "i" "n" "g"))
(; "string"
map (fn* [a] (* 2 a)) (seq [1,2,3,4]))
(; (2.0 4.0 6.0 8.0)
Map 类型
(map? {}); true
"key1" "value1" "key2" "value2")
(hash-map ; {"key1" "value1" "key2" "value2"}
"value1" :key2 "value2")
(hash-map :key1 ; {:key1 "value1" :key2 "value2"}
assoc {} :key1 "value1" :key2 "value2")
(; {:key1 "value1" :key2 "value2"}
"value1" :key2 "value2" :key3 "value3" :key4 "value4"} :key3 :key4)
(dissoc {:key1 ; {:key1 "value1" :key2 "value2"}
get {:key1 "value1" :key2 "value2"} :key1)
(; "value1"
"value1" :key2 "value2"} :key1)
(contains? {:key1 ; true
"value1" :key2 "value2"} :key3)
(contains? {:key1 ; false
"value1" :key2 "value2"})
(keys {:key1 ; (:key1 :key2)
"value1" :key2 "value2"})
(vals {:key1 ; ("value1" "value2")
symbol 类型
symbol "test"))
(symbol? (; true
symbol "test")
(; test
(symbol? keys); false
symbol :test)
(; ʞtest
atom 类型
atom "test")
(; (atom "test")
atom 1))
(atom? (; true
atom {}))
(def! *ref* (; (atom {})
(deref *ref*); {}
(prn @*ref*); {}
"value1"})
(reset! *ref* {:key1 ; {:key1 "value1"}
apply assoc h xs)) :key1 "value1" :key2 "value2")
(swap! *ref* (fn* [h & xs] (; {:key1 "value1" :key2 "value2"}
Nil 类型
nil)
(nil? ; true
= nil nil)
(; true
count nil)
(; 0.0
nil)
(empty? ; true
get nil "string")
(; nil
get {:key1 "value1" :key2 "value2"} :key3)
(; nil
nil :key1)
(contains? ; false
cons 1 nil)
(; (1.0)
first nil)
(; nil
first [])
(; nil
first (list))
(; nil
rest nil)
(; ()
rest [])
(; ()
rest (list))
(; ()
nil)
(seq ; nil
"")
(seq ; nil
(seq []); nil
list))
(seq (; nil
异常
throw "error message"
函数
(fn? prn); true
not (fn* (a) (if a false true)))
(def! ; (fn* ["a"] -> (if a false true))
let* [max (fn* [a1 a2] (> a1 a2))] (println (max 2 1)))
(; true
cond
(defmacro!
(fn* (& xs)if (> (count xs) 0)
(list 'if (first xs)
(if (> (count xs) 1)
(nth xs 1) (throw "odd number of forms to cond"))
(cons 'cond (rest (rest xs)))
(
)
)
)
); (macro* ["&","xs"] ->
; (if (> (count xs) 0.0)
; (list (quote if) (first xs)
; (if (> (count xs) 1.0)
; (nth xs 1.0) (throw "odd number of forms to cond"))
; (cons (quote cond) (rest (rest xs)))
; )
; )
; )
cond)
(macro? ; true
"abcd" :keyword (symbol "test"))
(pr-str
"println")
(println
eval (read-string "(= 1 1)"))
(; true
"(= 1 1)")
(read-string ; (= 1 1)
; 当前系统时间戳
(time-ms)
"test")
(prn ~; "test"
"test")
(prn ~@; "test"
"test")
(prn `; "test"
"test")
(prn '; "test"
逻辑
if true (prn true))
(; true
if false (prn true) (prn false))
(; false
not true)
(; false
not false)
(; true
Meta 操作
1,2,3] "meta")
(with-meta [; [1.0 2.0 3.0]
1,2,3] "meta"))
(meta (with-meta [; meta
"meta" [1,2,3])
(meta ^; meta