rlm@0: (ns coderloop.scientist2 rlm@0: (:refer-clojure :only []) rlm@0: (:require rlm.ns-rlm rlm.light-base)) rlm@0: (rlm.ns-rlm/ns-clone rlm.light-base) rlm@0: rlm@0: rlm@0: (use 'clojure.contrib.sql) rlm@0: (import 'org.gjt.mm.mysql.Driver) rlm@0: (use 'clojure.contrib.sql) rlm@0: (undef distinct case compile drop take sort) rlm@0: (use 'clojureql.core) rlm@0: (import 'java.io.File) rlm@0: (use '[clojure.java.io :only [copy]]) rlm@0: (use '[clojure.contrib.shell-out :only [sh]]) rlm@0: (use '[clojure.string :only [split]]) rlm@0: (use '[coderloop [utils :only [md5]]]) rlm@0: (import 'java.sql.Date) rlm@0: rlm@0: rlm@0: rlm@0: (def *port* 3306) rlm@0: rlm@0: (def *host* "localhost") rlm@0: rlm@0: (def *db-host* "localhost") rlm@0: rlm@0: (def *db-name* "clojure_rlm") rlm@0: rlm@0: (def *db* {:classname "com.mysql.jdbc.Driver" rlm@0: :subprotocol "mysql" rlm@0: :subname (str "//" *db-host* ":" *port* "/" *db-name*) rlm@0: :user "root" rlm@0: :password "sql1005025"}) rlm@0: rlm@0: (def databases [:essay_scientists :scientists :ideas :scientist_whereabouts]) rlm@0: rlm@0: rlm@0: rlm@0: rlm@0: (declare essays essay century contemps data eric scientists date->str date) rlm@0: rlm@0: (defn scientists [] rlm@0: @(table *db* :essay_scientists)) rlm@0: rlm@0: rlm@0: (defn contemps [s birth death] rlm@0: (sort-by (fn [s] [(:surname s) (:name s)]) rlm@0: (map #(select-keys % [:name :surname]) rlm@0: @(select rlm@0: (table *db* :scientists) rlm@0: (where rlm@0: (and rlm@0: (< :birth_date death) rlm@0: (< birth :death_date) rlm@0: (not (= :name (:name s))))))) )) rlm@0: rlm@0: (defn data [s] rlm@0: (let [locations rlm@0: (sort-by rlm@0: :immigration_date rlm@0: (map #(select-keys % [:immigration_date :emigration_date :country]) rlm@0: @(select rlm@0: (table *db* :scientist_whereabouts) rlm@0: (where rlm@0: (and rlm@0: (= :scientist_whereabouts.surname (:surname s)) rlm@0: (= :scientist_whereabouts.name (:name s))))))) rlm@0: ideas rlm@0: (sort-by rlm@0: #(.toLowerCase %) rlm@0: (map :idea rlm@0: @(select rlm@0: (table *db* :ideas) rlm@0: (where rlm@0: (and rlm@0: (= :ideas.surname (:surname s)) rlm@0: (= :ideas.name (:name s))))))) rlm@0: data rlm@0: (first rlm@0: @(select rlm@0: (table *db* :scientists) rlm@0: (where rlm@0: (and rlm@0: (= :scientists.surname (:surname s)) rlm@0: (= :scientists.name (:name s)))))) rlm@0: birth (:birth_date data) rlm@0: death (:death_date data) rlm@0: contemp (contemps s birth death)] rlm@0: (merge data {:locations locations :contemps contemp :ideas ideas}))) rlm@0: rlm@0: (defn date->str [#^java.sql.Date d] rlm@0: (format "%02d/%02d/%04d" (.getDate d) (inc (.getMonth d)) (+ 1900 (.getYear d)))) rlm@0: rlm@0: (def strcat (partial apply str)) rlm@0: rlm@0: (defn century [#^java.sql.Date d] rlm@0: (* 100 (clojure.core/unchecked-divide (+ 1900 (.getYear d)) 100))) rlm@0: rlm@0: (defn essay-map [s] rlm@0: (let [base (data s) rlm@0: locations rlm@0: (strcat rlm@0: (interpose rlm@0: ", " rlm@0: (map (fn [{:keys [emigration_date immigration_date country ]}] rlm@0: (str country " from " (date->str immigration_date) " to " rlm@0: (date->str emigration_date))) (:locations base)))) rlm@0: contemps rlm@0: (strcat rlm@0: (interpose rlm@0: ", " rlm@0: (map (fn [{:keys [name surname]}] rlm@0: (str name " " surname)) rlm@0: (:contemps base)))) rlm@0: birth-date (date->str (:birth_date base)) rlm@0: death-date (date->str (:death_date base)) rlm@0: century (century (:birth_date base)) rlm@0: lifespan rlm@0: (str "[" rlm@0: (:birth_place base) " " birth-date ", " rlm@0: (:death_place base) " " death-date rlm@0: "]") rlm@0: ideas (strcat (interpose ", " (:ideas base))) rlm@0: ] rlm@0: {:lifespan lifespan :name (:name base) :surname (:surname base) rlm@0: :contemps contemps :locations locations :century century :ideas ideas})) rlm@0: rlm@0: (defn essay [s] rlm@0: (let [{:keys [lifespan name surname contemps locations century ideas]} (essay-map s)] rlm@0: (str name " " surname " " lifespan " is one of the most famous scientists of " rlm@0: century ". " rlm@0: "Between all " name "'s ideas we mention: " ideas ". " rlm@0: name " lived in: " locations ". " rlm@0: name " was a contemporary of " contemps ".\n\n"))) rlm@0: rlm@0: (defn essays [] rlm@0: (map essay (scientists))) rlm@0: rlm@0: (defn print-essays [] rlm@0: (dorun (map print (essays)))) rlm@0: rlm@0: (defn essays->str [] rlm@0: (strcat (essays))) rlm@0: rlm@0: (defn main [[username password db-name]] rlm@0: (let [db-host *host* rlm@0: db-port *port* rlm@0: db {:classname "com.mysql.jdbc.Driver" rlm@0: :subprotocol "mysql" rlm@0: :subname (str "//" db-host ":" db-port "/" db-name) rlm@0: :user username rlm@0: :password password}] rlm@0: (binding [*db* db] rlm@0: (print-essays)))) rlm@0: rlm@0: (if (command-line?) rlm@0: (main *command-line-args*)) rlm@0: rlm@0: rlm@0: rlm@0: rlm@0: rlm@0: (def eric {:name "Erik 3802" :surname "Green 3802"}) rlm@0: (def date (java.sql.Date. 4564654645)) rlm@0: rlm@0: rlm@0: rlm@0: (defvar error-person rlm@0: {:name "Albert 3850" :surname "Newton 3850"} rlm@0: "this is the person in in which John 5750 is supposedly rlm@0: wrongly included my code says they're contempoaries, rlm@0: but the expected output disaggrees ") rlm@0: rlm@0: (defvar wrong-include rlm@0: {:name "John 5750" :surname "Newton 5750"} rlm@0: "this one is inculded and supposedly should not be.") rlm@0: rlm@0: ;;coderloop.scientist2> (select-keys (data error-person) [:birth_date :death_date]) rlm@0: ;;{:death_date #, :birth_date #} rlm@0: ;;coderloop.scientist2> (select-keys (data wrong-include) [:birth_date :death_date]) rlm@0: ;;{:death_date #, :birth_date #} rlm@0: rlm@0: ;; these exibit the same problem. They appear to be contemps, rlm@0: ;; but the expected output says that they're not! rlm@0: (defvar error-person2 rlm@0: {:name "Erik 3851" :surname "Smith 3851"}) rlm@0: rlm@0: (defvar wrong-include2 rlm@0: {:name "Gil 5751" :surname "Smith 5751"}) rlm@0: rlm@0: ;;coderloop.scientist2> (select-keys (data error-person2) [:birth_date :death_date]) rlm@0: ;;{:death_date #, :birth_date #} rlm@0: ;;coderloop.scientist2> (select-keys (data wrong-include2) [:birth_date :death_date]) rlm@0: ;;{:death_date #, :birth_date #} rlm@0: rlm@0: rlm@0: