Skip to content

Commit

Permalink
#17 guard predicate wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ojow committed Jun 20, 2018
1 parent b285d5d commit b801269
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
1 change: 1 addition & 0 deletions re/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
[io.prometheus/simpleclient_hotspot "0.4.0"]
[io.prometheus/simpleclient_jetty "0.4.0"]
[org.clojure/core.cache "0.7.1"]
[org.clojure/core.match "0.3.0-alpha5"]

[com.360dialog/oam "1.1-SNAPSHOT"]
[cc.qbits/alia-all "4.2.1"]
Expand Down
4 changes: 3 additions & 1 deletion re/src/crm/orc/crm.orc
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ def and((p, p_immutable), (q, q_immutable)) =
(p && q, (p_immutable && q_immutable) || (~p && p_immutable) || (~q && q_immutable))
def or((p, p_immutable), (q, q_immutable)) =
(p || q, (p_immutable && q_immutable) || (p && p_immutable) || (q && q_immutable))
def not((p, p_immutable)) = (~p, p_immutable)
def not((p, p_immutable)) = (~p, p_immutable)

def receiveEvent(pattern) = Coeffect({. kind = "subscription", pattern = pattern .})
63 changes: 52 additions & 11 deletions re/src/re/handler/json2orc.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
(ns re.handler.json2orc
(:require [integrant.core :as ig])
(:require [clojure.java.shell :as shell]))
(:require [integrant.core :as ig]
[clojure.java.shell :as shell]
[clojure.string :as str]
[clojure.core.match :refer [match]]))

(def ^:private immutable-props
#{:device.id
:device.ifa
:device.os
:device.make
:device.manufacturer
:device.brand
:device.model
:device.type
:device.carrier
:device.browser.name
:device.hwv
:device.h
:device.w
:device.ppi
:device.jailbroken
:device.language})

(defn compile-orc [orc]
(shell/sh
Expand All @@ -19,26 +39,47 @@
(defn add-segments [campaign]
campaign)

(defn guard_predicate [campaign]
" and((true, true), not((true, true)))\n")
(defn literal [e]
(cond
(string? e) (str "\"" e "\"")
true e))

(defn binary [lhs op rhs]
(str "(`re.entity`()." (name lhs) op (literal rhs) ", " (contains? immutable-props lhs) ")"))

(defn operation [op lhs-expr]
(match [op]
[{:eq rhs-expr}] (binary lhs-expr " = " rhs-expr)))

(defn guard_predicate [condition]
(str/join " "
(map (fn [kv]
(match kv
[:AND [cond]] (guard_predicate cond)
[:AND [f & r]] (str "and(" (guard_predicate f) ", " (guard_predicate {:AND r}) ")")
[:OR [cond]] (guard_predicate cond)
[:OR [f & r]] (str "or(" (guard_predicate f) ", " (guard_predicate {:OR r}) ")")
[:NOT cond] (str "not(" (guard_predicate cond) ")")
[property op] (operation op property)))
condition)))



(defn convert [json]
(let [campaign (add-segments json)
orc (str (header campaign)
"refer from crm (and, or, not)\n"
"def receiveEvent(pattern) = Coeffect({. kind = \"subscription\", pattern = pattern .})\n"
"refer from crm (and, or, not, receiveEvent)\n"
"\n"
"def proceedWithCampaign() = signal\n"
"def waitForChanges() =\n"
" receiveEvent(\"\\\\{.+\\\\}\\\\{.+\\\\}\\\\{events\\\\.SDKEvent\\\\}\")\n"
"def guard_predicate() = \n"
(guard_predicate campaign)
"def waitForChanges() = receiveEvent(\"\\\\{.+\\\\}\\\\{.+\\\\}\\\\{events\\\\.SDKEvent\\\\}\")\n"
"def guard_predicate() = " (guard_predicate (:targeting campaign)) "\n"
"def guard() =\n"
" val (satisfied, immutable) = guard_predicate()\n"
" if (satisfied) then proceedWithCampaign()\n"
" else if (~immutable) then waitForChanges() >> guard()\n"
" else stop\n"
"\n"
"guard()")
"guard()\n")
compile-result (compile-orc orc)
result {:orc orc}]
(if (= (:exit compile-result) 0)
Expand Down

0 comments on commit b801269

Please sign in to comment.