Skip to content

Instantly share code, notes, and snippets.

View WilliamParker's full-sized avatar

William Parker WilliamParker

  • London, United Kingdom
View GitHub Profile
clara.test-rules> (clara.rules/defrule lost-game
[?ships <- (acc/count) :from [Ship]]
[?destroyed <- (acc/count) :from [Destroyed]]
[:test (= ?ships ?destroyed)]
=>
(insert! (->Loser)))
#'clara.test-rules/lost-game
clara.test-rules> (-> (mk-session [lost-game]) .rulebase :alpha-roots (get Ship))
({:env nil,
:children
user=> (throw (ex-info "Deliberate exception" {:hello :world}))
ExceptionInfo Deliberate exception clojure.core/ex-info (core.clj:4617)
user=> *e
#error {
:cause "Deliberate exception"
:data {:hello :world}
:via
[{:type clojure.lang.ExceptionInfo
:message "Deliberate exception"
user> (require 'clara.test-rules)
nil
user> (in-ns 'clara.test-rules)
#namespace[clara.test-rules]
clara.test-rules> (defquery query1
"Doc here"
[]
[?fact <- :a])
#'clara.test-rules/query1
clara.test-rules> (-> (mk-session [query1] :fact-type-fn :clara-fact-type)
@WilliamParker
WilliamParker / repl_session.clj
Created July 8, 2016 14:13
Demo of session immutatibility in clara-rules
clara.test-rules> (com/clear-session-cache!)
{}
clara.test-rules> (defrule first->second-rule
[First]
=>
(insert! (->Second)))
#'clara.test-rules/first->second-rule
clara.test-rules> (defquery second-query
[]
[Second])
(defn constant-accum [v]
(accumulate
:initial-value v
:reduce-fn (constantly v)
:combine-fn (constantly v)
:retract-fn (constantly v)
:convert-return-fn identity))
(deftest nil-accumulate-node-test
(let [r1 (dsl/parse-rule [[?r <- (constant-accum nil) :from [Cold]]]
@WilliamParker
WilliamParker / IBenchmark.java
Created April 22, 2016 20:24
See clara-rules issue 182
package clara.benchmark;
import java.util.Iterator;
/**
* Benchmark interface.
*/
public interface IBenchmark<T, E> {
/**
@WilliamParker
WilliamParker / example.clj
Created April 15, 2016 02:46
Example demonstrating use of clojure.lang.Keyword as the fact type in Clara rule constraints
clara.test-rules> (defrule arule [clojure.lang.Keyword (= :a)] => (println "I fired"))
#'clara.test-rules/arule
clara.test-rules> (-> (mk-session [arule]) (insert :a) (fire-rules))
I fired
#object[clara.rules.engine.LocalSession 0x2c848a4e "clara.rules.engine.LocalSession@2c848a4e"]
clara.test-rules> (-> (mk-session [arule]) (insert :b) (fire-rules))
I fired
#object[clara.rules.engine.LocalSession 0x107e6fae "clara.rules.engine.LocalSession@107e6fae"]
clara.test-rules> (defrule arule2 [clojure.lang.Keyword (= this :a)] => (println "I fired"))
#'clara.test-rules/arule2
@WilliamParker
WilliamParker / perf_test.clj
Created February 10, 2016 16:35
Clojure vec vs list pop performance test
;; Determine the relative performance of turning a vector into a list
;; and popping items off the list versus just using the vector.
;; The list approach is asymptotically faster, but the idea is to determine
;; at what point the overhead of creating a new list is worth it.
(defn time-list [n]
(let [init-vec (mapv (constantly 1)
(range n))]
(time (loop [l (into () init-vec)]
(let [r (rest l)
@WilliamParker
WilliamParker / gist:b109dba95f36187f54d0
Created January 12, 2016 21:42
Test namespace for duplicate rules in clara-rules
(ns clara.examples.duplicates
(:require [clara.rules.accumulators :as acc]
[clara.rules :refer :all]
[clara.rules.compiler :as c]))
(defrecord Temperature [t])
(defrecord Cold [t])
(defrule is-cold?
@WilliamParker
WilliamParker / init.java
Created December 28, 2015 16:31
Decompiled namespace class initializer for unmap-test.core
package unmap_test;
import clojure.lang.AFn;
import clojure.lang.Compiler;
import clojure.lang.IFn;
import clojure.lang.IPersistentMap;
import clojure.lang.Keyword;
import clojure.lang.LockingTransaction;
import clojure.lang.RT;
import clojure.lang.Symbol;