文档中心
DOCUMENT CENTER
规则引擎语言基础

规则引擎数据处理

; 指令配置
(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])

; 获取当前设备属性
(fetch_target_meta "meta_key")
(fetch_source_meta "meta_key")

规则引擎语言基础

字符串类型

(str "string1" "string2" "string3")
; "string1string2string3"

(seq "string")
; ("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"

数值类型

(number? 1)
; true

(number? 0.1)
; 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

序列类型

序列类型分为 vectorlist 两种类型

(sequential? (list 1 2 3 4))
; true

(sequential? (vector 1 2 3 4))
; true

(= (vector 1 2 3 4) (list 1 2 3 4))
; true

Vector 类型

(vector? ["abc" "def"])
; true

(vector? (vector "abc" "def"))
; true

(vector "abc" "def")
; ["abc" "def"]

(seq ["abc" "def"])
; ("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

(conj [1,2,3,4] 5 6)
; [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)

(concat [1,2,3,4])
; (1.0 2.0 3.0 4.0)

(empty? [])
; true

(apply str "" ["s","t","r","i"m"n"m"g"])
; "string"

List 类型

(list? (list 1 2 3))
; true

(list? (seq [1,2,3,4]))
; true

(= (list 1 2 3 4) (seq [1,2,3,4]))
; true

(conj (seq [1,2,3,4]) 5 6)
; (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)

(concat (seq [1,2,3,4]))
; (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

(empty? (list))
; true

(empty? nil)
; 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

(hash-map "key1" "value1" "key2" "value2")
; {"key1" "value1" "key2" "value2"}

(hash-map :key1 "value1" :key2 "value2")
; {:key1 "value1" :key2 "value2"}

(assoc {} :key1 "value1" :key2 "value2")
; {:key1 "value1" :key2 "value2"}

(dissoc {:key1 "value1" :key2 "value2" :key3 "value3" :key4 "value4"} :key3 :key4)
; {:key1 "value1" :key2 "value2"}


(get {:key1 "value1" :key2 "value2"} :key1)
; "value1"

(contains?  {:key1 "value1" :key2 "value2"} :key1)
; true

(contains?  {:key1 "value1" :key2 "value2"} :key3)
; false

(keys  {:key1 "value1" :key2 "value2"})
; (:key1 :key2)

(vals  {:key1 "value1" :key2 "value2"})
; ("value1" "value2")

symbol 类型

(symbol? (symbol "test"))
; true

(symbol "test")
; test

(symbol? keys)
; false

(symbol :test)
; ʞtest

atom 类型

(atom "test")
; (atom "test")

(atom? (atom 1))
; true

(def! *ref* (atom {}))
; (atom {})

(deref *ref*)
; {}

(prn @*ref*)
; {}

(reset! *ref* {:key1 "value1"})
; {:key1 "value1"}

(swap! *ref* (fn* [h & xs] (apply assoc h xs)) :key1 "value1" :key2 "value2")
; {:key1 "value1" :key2 "value2"}

Nil 类型

(nil? nil)
; true

(= nil nil)
; true

(count nil)
; 0.0

(empty? nil)
; true

(get nil "string")
; nil

(get {:key1 "value1" :key2 "value2"} :key3)
; nil

(contains?  nil :key1)
; false

(cons 1 nil)
; (1.0)

(first nil)
; nil

(first [])
; nil

(first (list))
; nil

(rest nil)
; ()

(rest [])
; ()

(rest (list))
; ()

(seq nil)
; nil

(seq "")
; nil

(seq [])
; nil

(seq (list))
; nil

异常

throw "error message"

函数

(fn? prn)
; true

(def! not (fn* (a) (if a false true)))
; (fn* ["a"] -> (if a false true))

(let* [max (fn* [a1 a2] (> a1 a2))] (println (max 2 1)))
; true

(defmacro! cond
  (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)))
;     )
;   )
; )

(macro? cond)
; true

(pr-str "abcd" :keyword (symbol "test"))

(println "println")

(eval (read-string "(= 1 1)"))
; true

(read-string "(= 1 1)")
; (= 1 1)

(time-ms) ; 当前系统时间戳

(prn ~"test")
; "test"

(prn ~@"test")
; "test"

(prn `"test")
; "test"

(prn '"test")
; "test"

逻辑

(if true (prn true))
; true

(if false (prn true) (prn false))
; false

(not true)
; false

(not false)
; true

Meta 操作

(with-meta [1,2,3] "meta")
; [1.0 2.0 3.0]

(meta (with-meta [1,2,3] "meta"))
; meta

(meta ^"meta" [1,2,3])
; meta