Skip to content

Commit

Permalink
Merge pull request #26 from ylando2/master
Browse files Browse the repository at this point in the history
Improve the binding of two variables.
  • Loading branch information
rbrush committed Nov 5, 2013
2 parents cf3beb4 + bdc0191 commit 0264d22
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/main/clojure/clara/rules/engine.clj
Original file line number Diff line number Diff line change
Expand Up @@ -546,16 +546,23 @@
containEq? (and (symbol? cmp) (let [cmp-str (name cmp)] (or (= cmp-str "=") (= cmp-str "=="))))
a-in-assigment (and containEq? (and (symbol? a) (assigment-set (keyword a))))
b-in-assigment (and containEq? (and (symbol? b) (assigment-set (keyword b))))]
(cond
a-in-assigment
(if b-in-assigment
(cons `(swap! ~'?__bindings__ assoc ~(keyword a) (~'?__bindings__ ~(keyword b))) compiled-rest)
(cons `(swap! ~'?__bindings__ assoc ~(keyword a) ~b) compiled-rest))
b-in-assigment
(cons `(swap! ~'?__bindings__ assoc ~(keyword b) ~a) compiled-rest)
;; not a unification
:else
(list (list 'if exp (cons 'do compiled-rest) nil))))))
(cond
a-in-assigment
(if b-in-assigment
`((let [a-exist# (contains? (deref ~'?__bindings__) ~(keyword a))
b-exist# (contains? (deref ~'?__bindings__) ~(keyword b))]
(when (and (not a-exist#) (not b-exist#)) (throw (Throwable. "Binding undefine variables")))
(when (not a-exist#) (swap! ~'?__bindings__ assoc ~(keyword a) ((deref ~'?__bindings__) ~(keyword b))))
(when (not b-exist#) (swap! ~'?__bindings__ assoc ~(keyword b) ((deref ~'?__bindings__) ~(keyword a))))
(if (or (not a-exist#) (not b-exist#) (= ((deref ~'?__bindings__) ~(keyword a)) ((deref ~'?__bindings__) ~(keyword b))))
(do ~@compiled-rest)
nil)))
(cons `(swap! ~'?__bindings__ assoc ~(keyword a) ~b) compiled-rest))
b-in-assigment
(cons `(swap! ~'?__bindings__ assoc ~(keyword b) ~a) compiled-rest)
;; not a unification
:else
(list (list 'if exp (cons 'do compiled-rest) nil))))))

(defn- compile-condition
"Returns a function definition that can be used in alpha nodes to test the condition."
Expand Down

0 comments on commit 0264d22

Please sign in to comment.