Skip to content

Instantly share code, notes, and snippets.

@hiredman
Created June 2, 2014 18:07
Show Gist options
  • Save hiredman/05befd5b39eef89b86ca to your computer and use it in GitHub Desktop.
Save hiredman/05befd5b39eef89b86ca to your computer and use it in GitHub Desktop.
core.logic for joinery
#!/usr/bin/java -jar clojure-1.7.0-master-SNAPSHOT.jar
(let [pom-uber-jar
(str "http://thelibraryofcongress.s3.amazonaws.com/"
"pomegranate-0.0.13-SNAPSHOT-jar-with-dependencies.jar")
cl (java.net.URLClassLoader. (into-array [(java.net.URL. pom-uber-jar)]))
cx (.getContextClassLoader (Thread/currentThread))]
(push-thread-bindings {clojure.lang.Compiler/LOADER cl})
(.setContextClassLoader (Thread/currentThread) cl)
(try
(require '[cemerick.pomegranate :as pom])
(finally
(.setContextClassLoader (Thread/currentThread) cx)
(pop-thread-bindings))))
(pom/add-dependencies :coordinates '[[org.clojure/core.logic "0.8.7"]]
:repositories (merge
cemerick.pomegranate.aether/maven-central
{"clojars" "http://clojars.org/repo"}))
;; computer, design me a work bench
(require '[clojure.core.logic :as l]
'[clojure.core.logic.fd :as fd]
'[clojure.pprint :as pp])
(defn pos-int-vals [m]
(reduce l/composeg
(for [v (vals m)]
(fd/in v (fd/interval 0 Integer/MAX_VALUE)))))
(defn system [& names]
(into {} (for [name names]
[name (l/lvar)])))
(def bench-system
(system
:leg-height
:total-top-laminate-element-length
:long-runner-count
:total-two-by-four-length-inches
:long-runner-length
:desk-height
:top-laminate-elements
:total-short-runner-length
:total-two-by-four-length-feet
:desk-length
:total-runner-length
:total-two-by-four-length
:total-four-by-four-weight-grams
:short-runner-length
:total-long-runner-length
:total-four-by-four-length-feet
:total-two-by-four-weight-grams
:total-four-by-four-length
:total-weight-grams
:desk-depth
:two-by-four-width
:total-four-by-four-length-inches
:two-by-four-depth
:top-height
:short-runner-count))
;; dimensions are in tenths of an inch (38 inches -> 380)
;; a slick units system would be super nice
(defn solve-bench [bench]
(first
(l/run
1 [q]
(pos-int-vals bench)
(l/== (:two-by-four-depth bench) 35)
(l/== (:two-by-four-width bench) 15)
(l/== (:long-runner-count bench) 4)
(l/== (:short-runner-count bench) 4)
(fd/* (:top-laminate-elements bench)
(:two-by-four-width bench)
(:desk-depth bench))
(fd/* (:top-laminate-elements bench)
(:desk-length bench)
(:total-top-laminate-element-length bench))
(l/== (:top-height bench)
(:two-by-four-depth bench))
(fd/+ (:top-height bench)
(:leg-height bench)
(:desk-height bench))
;; the 4x4s run the entire span because I plan on doing through
;; tenons
(l/== (:long-runner-length bench) (:desk-length bench))
(l/== (:short-runner-length bench) (:desk-depth bench))
(fd/* (:short-runner-count bench)
(:short-runner-length bench)
(:total-short-runner-length bench))
(fd/* (:long-runner-count bench)
(:long-runner-length bench)
(:total-long-runner-length bench))
(fd/+ (:total-long-runner-length bench)
(:total-short-runner-length bench)
(:total-runner-length bench))
(l/== (:total-four-by-four-length bench)
(:total-runner-length bench))
(l/== (:total-two-by-four-length bench)
(:total-top-laminate-element-length bench))
(fd/* 10
(:total-two-by-four-length-inches bench)
(:total-two-by-four-length bench))
(fd/* 12
(:total-two-by-four-length-feet bench)
(:total-two-by-four-length-inches bench))
(fd/* 10
(:total-four-by-four-length-inches bench)
(:total-four-by-four-length bench))
(fd/* 12
(:total-four-by-four-length-feet bench)
(:total-four-by-four-length-inches bench))
;; using approximate weight per foot
(fd/* 540
(:total-two-by-four-length-feet bench)
(:total-two-by-four-weight-grams bench))
(fd/* 1360
(:total-four-by-four-length-feet bench)
(:total-four-by-four-weight-grams bench))
(fd/+ (:total-two-by-four-weight-grams bench)
(:total-four-by-four-weight-grams bench)
(:total-weight-grams bench))
(l/== q bench))))
;; a small bench
(pp/pprint
(solve-bench (assoc bench-system
;; 2 foot depth
:desk-depth 240
;; 4 feet long
:desk-length 480
;; 34 inches high
:desk-height 340)))
;; =>
;; {:leg-height 305,
;; :total-top-laminate-element-length 7680,
;; :long-runner-count 4,
;; :total-two-by-four-length-inches 768,
;; :long-runner-length 480,
;; :desk-height 340,
;; :top-laminate-elements 16,
;; :total-short-runner-length 960,
;; :total-two-by-four-length-feet 64,
;; :desk-length 480,
;; :total-runner-length 2880,
;; :total-two-by-four-length 7680,
;; :total-four-by-four-weight-grams 32640,
;; :short-runner-length 240,
;; :total-long-runner-length 1920,
;; :total-four-by-four-length-feet 24,
;; :total-two-by-four-weight-grams 34560,
;; :total-four-by-four-length 2880,
;; :total-weight-grams 67200,
;; :desk-depth 240,
;; :two-by-four-width 15,
;; :total-four-by-four-length-inches 288,
;; :two-by-four-depth 35,
;; :top-height 35,
;; :short-runner-count 4}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment