Skip to content

Instantly share code, notes, and snippets.

@samflores
Created January 20, 2016 18:11
Show Gist options
  • Save samflores/f0167601f67db8b64749 to your computer and use it in GitHub Desktop.
Save samflores/f0167601f67db8b64749 to your computer and use it in GitHub Desktop.
(ns rules
(:require [clara.rules :refer :all]))
(defrecord Person [name gender])
(defrecord Relation [person-a degree person-b])
(defrecord Father [father descendant])
(defrecord Mother [mother descendant])
(defrule is-father ""
[Person (= :male gender) (= ?father name)]
[Relation (= :parent-of degree) (= ?father person-a) (= ?descendant person-b)]
=>
(insert! (->Father ?father ?descendant)))
(defrule is-mother ""
[Person (= :female gender) (= ?mother name)]
[Relation (= :parent-of degree) (= ?mother person-a) (= ?descendant person-b)]
=>
(insert! (->Mother ?mother ?descendant)))
(defrule is-grandfather ""
[Father (= ?grandfather father) (= ?parent descendant)]
[:or
[Father (= ?parent father) (= ?grandchild descendant)]
[Mother (= ?parent mother) (= ?grandchild descendant)]]
=>
(println ?grandfather "is grandfather of" ?grandchild))
(def kdb (atom (mk-session 'rules)))
(defn run []
(-> (mk-session 'rules)
(insert
(->Person "John" :male)
(->Person "Mary" :female)
(->Relation "John" :parent-of "Mary")
(->Relation "Mary" :parent-of "Peter")
(->Relation "Mary" :parent-of "Paul"))
(fire-rules)))
;; user> (run)
;; => John is grandfather of Peter
;; => john is grandfather of Paul
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment