rlm@0
|
1 (ns coderloop.scientist2
|
rlm@0
|
2 (:refer-clojure :only [])
|
rlm@0
|
3 (:require rlm.ns-rlm rlm.light-base))
|
rlm@0
|
4 (rlm.ns-rlm/ns-clone rlm.light-base)
|
rlm@0
|
5
|
rlm@0
|
6
|
rlm@0
|
7 (use 'clojure.contrib.sql)
|
rlm@0
|
8 (import 'org.gjt.mm.mysql.Driver)
|
rlm@0
|
9 (use 'clojure.contrib.sql)
|
rlm@0
|
10 (undef distinct case compile drop take sort)
|
rlm@0
|
11 (use 'clojureql.core)
|
rlm@0
|
12 (import 'java.io.File)
|
rlm@0
|
13 (use '[clojure.java.io :only [copy]])
|
rlm@0
|
14 (use '[clojure.contrib.shell-out :only [sh]])
|
rlm@0
|
15 (use '[clojure.string :only [split]])
|
rlm@0
|
16 (use '[coderloop [utils :only [md5]]])
|
rlm@0
|
17 (import 'java.sql.Date)
|
rlm@0
|
18
|
rlm@0
|
19
|
rlm@0
|
20
|
rlm@0
|
21 (def *port* 3306)
|
rlm@0
|
22
|
rlm@0
|
23 (def *host* "localhost")
|
rlm@0
|
24
|
rlm@0
|
25 (def *db-host* "localhost")
|
rlm@0
|
26
|
rlm@0
|
27 (def *db-name* "clojure_rlm")
|
rlm@0
|
28
|
rlm@0
|
29 (def *db* {:classname "com.mysql.jdbc.Driver"
|
rlm@0
|
30 :subprotocol "mysql"
|
rlm@0
|
31 :subname (str "//" *db-host* ":" *port* "/" *db-name*)
|
rlm@0
|
32 :user "root"
|
rlm@0
|
33 :password "sql1005025"})
|
rlm@0
|
34
|
rlm@0
|
35 (def databases [:essay_scientists :scientists :ideas :scientist_whereabouts])
|
rlm@0
|
36
|
rlm@0
|
37
|
rlm@0
|
38
|
rlm@0
|
39
|
rlm@0
|
40 (declare essays essay century contemps data eric scientists date->str date)
|
rlm@0
|
41
|
rlm@0
|
42 (defn scientists []
|
rlm@0
|
43 @(table *db* :essay_scientists))
|
rlm@0
|
44
|
rlm@0
|
45
|
rlm@0
|
46 (defn contemps [s birth death]
|
rlm@0
|
47 (sort-by (fn [s] [(:surname s) (:name s)])
|
rlm@0
|
48 (map #(select-keys % [:name :surname])
|
rlm@0
|
49 @(select
|
rlm@0
|
50 (table *db* :scientists)
|
rlm@0
|
51 (where
|
rlm@0
|
52 (and
|
rlm@0
|
53 (< :birth_date death)
|
rlm@0
|
54 (< birth :death_date)
|
rlm@0
|
55 (not (= :name (:name s))))))) ))
|
rlm@0
|
56
|
rlm@0
|
57 (defn data [s]
|
rlm@0
|
58 (let [locations
|
rlm@0
|
59 (sort-by
|
rlm@0
|
60 :immigration_date
|
rlm@0
|
61 (map #(select-keys % [:immigration_date :emigration_date :country])
|
rlm@0
|
62 @(select
|
rlm@0
|
63 (table *db* :scientist_whereabouts)
|
rlm@0
|
64 (where
|
rlm@0
|
65 (and
|
rlm@0
|
66 (= :scientist_whereabouts.surname (:surname s))
|
rlm@0
|
67 (= :scientist_whereabouts.name (:name s)))))))
|
rlm@0
|
68 ideas
|
rlm@0
|
69 (sort-by
|
rlm@0
|
70 #(.toLowerCase %)
|
rlm@0
|
71 (map :idea
|
rlm@0
|
72 @(select
|
rlm@0
|
73 (table *db* :ideas)
|
rlm@0
|
74 (where
|
rlm@0
|
75 (and
|
rlm@0
|
76 (= :ideas.surname (:surname s))
|
rlm@0
|
77 (= :ideas.name (:name s)))))))
|
rlm@0
|
78 data
|
rlm@0
|
79 (first
|
rlm@0
|
80 @(select
|
rlm@0
|
81 (table *db* :scientists)
|
rlm@0
|
82 (where
|
rlm@0
|
83 (and
|
rlm@0
|
84 (= :scientists.surname (:surname s))
|
rlm@0
|
85 (= :scientists.name (:name s))))))
|
rlm@0
|
86 birth (:birth_date data)
|
rlm@0
|
87 death (:death_date data)
|
rlm@0
|
88 contemp (contemps s birth death)]
|
rlm@0
|
89 (merge data {:locations locations :contemps contemp :ideas ideas})))
|
rlm@0
|
90
|
rlm@0
|
91 (defn date->str [#^java.sql.Date d]
|
rlm@0
|
92 (format "%02d/%02d/%04d" (.getDate d) (inc (.getMonth d)) (+ 1900 (.getYear d))))
|
rlm@0
|
93
|
rlm@0
|
94 (def strcat (partial apply str))
|
rlm@0
|
95
|
rlm@0
|
96 (defn century [#^java.sql.Date d]
|
rlm@0
|
97 (* 100 (clojure.core/unchecked-divide (+ 1900 (.getYear d)) 100)))
|
rlm@0
|
98
|
rlm@0
|
99 (defn essay-map [s]
|
rlm@0
|
100 (let [base (data s)
|
rlm@0
|
101 locations
|
rlm@0
|
102 (strcat
|
rlm@0
|
103 (interpose
|
rlm@0
|
104 ", "
|
rlm@0
|
105 (map (fn [{:keys [emigration_date immigration_date country ]}]
|
rlm@0
|
106 (str country " from " (date->str immigration_date) " to "
|
rlm@0
|
107 (date->str emigration_date))) (:locations base))))
|
rlm@0
|
108 contemps
|
rlm@0
|
109 (strcat
|
rlm@0
|
110 (interpose
|
rlm@0
|
111 ", "
|
rlm@0
|
112 (map (fn [{:keys [name surname]}]
|
rlm@0
|
113 (str name " " surname))
|
rlm@0
|
114 (:contemps base))))
|
rlm@0
|
115 birth-date (date->str (:birth_date base))
|
rlm@0
|
116 death-date (date->str (:death_date base))
|
rlm@0
|
117 century (century (:birth_date base))
|
rlm@0
|
118 lifespan
|
rlm@0
|
119 (str "["
|
rlm@0
|
120 (:birth_place base) " " birth-date ", "
|
rlm@0
|
121 (:death_place base) " " death-date
|
rlm@0
|
122 "]")
|
rlm@0
|
123 ideas (strcat (interpose ", " (:ideas base)))
|
rlm@0
|
124 ]
|
rlm@0
|
125 {:lifespan lifespan :name (:name base) :surname (:surname base)
|
rlm@0
|
126 :contemps contemps :locations locations :century century :ideas ideas}))
|
rlm@0
|
127
|
rlm@0
|
128 (defn essay [s]
|
rlm@0
|
129 (let [{:keys [lifespan name surname contemps locations century ideas]} (essay-map s)]
|
rlm@0
|
130 (str name " " surname " " lifespan " is one of the most famous scientists of "
|
rlm@0
|
131 century ". "
|
rlm@0
|
132 "Between all " name "'s ideas we mention: " ideas ". "
|
rlm@0
|
133 name " lived in: " locations ". "
|
rlm@0
|
134 name " was a contemporary of " contemps ".\n\n")))
|
rlm@0
|
135
|
rlm@0
|
136 (defn essays []
|
rlm@0
|
137 (map essay (scientists)))
|
rlm@0
|
138
|
rlm@0
|
139 (defn print-essays []
|
rlm@0
|
140 (dorun (map print (essays))))
|
rlm@0
|
141
|
rlm@0
|
142 (defn essays->str []
|
rlm@0
|
143 (strcat (essays)))
|
rlm@0
|
144
|
rlm@0
|
145 (defn main [[username password db-name]]
|
rlm@0
|
146 (let [db-host *host*
|
rlm@0
|
147 db-port *port*
|
rlm@0
|
148 db {:classname "com.mysql.jdbc.Driver"
|
rlm@0
|
149 :subprotocol "mysql"
|
rlm@0
|
150 :subname (str "//" db-host ":" db-port "/" db-name)
|
rlm@0
|
151 :user username
|
rlm@0
|
152 :password password}]
|
rlm@0
|
153 (binding [*db* db]
|
rlm@0
|
154 (print-essays))))
|
rlm@0
|
155
|
rlm@0
|
156 (if (command-line?)
|
rlm@0
|
157 (main *command-line-args*))
|
rlm@0
|
158
|
rlm@0
|
159
|
rlm@0
|
160
|
rlm@0
|
161
|
rlm@0
|
162
|
rlm@0
|
163 (def eric {:name "Erik 3802" :surname "Green 3802"})
|
rlm@0
|
164 (def date (java.sql.Date. 4564654645))
|
rlm@0
|
165
|
rlm@0
|
166
|
rlm@0
|
167
|
rlm@0
|
168 (defvar error-person
|
rlm@0
|
169 {:name "Albert 3850" :surname "Newton 3850"}
|
rlm@0
|
170 "this is the person in in which John 5750 is supposedly
|
rlm@0
|
171 wrongly included my code says they're contempoaries,
|
rlm@0
|
172 but the expected output disaggrees ")
|
rlm@0
|
173
|
rlm@0
|
174 (defvar wrong-include
|
rlm@0
|
175 {:name "John 5750" :surname "Newton 5750"}
|
rlm@0
|
176 "this one is inculded and supposedly should not be.")
|
rlm@0
|
177
|
rlm@0
|
178 ;;coderloop.scientist2> (select-keys (data error-person) [:birth_date :death_date])
|
rlm@0
|
179 ;;{:death_date #<Date 0226-10-18>, :birth_date #<Date 0166-10-16>}
|
rlm@0
|
180 ;;coderloop.scientist2> (select-keys (data wrong-include) [:birth_date :death_date])
|
rlm@0
|
181 ;;{:death_date #<Date 0226-01-01>, :birth_date #<Date 0166-12-30>}
|
rlm@0
|
182
|
rlm@0
|
183 ;; these exibit the same problem. They appear to be contemps,
|
rlm@0
|
184 ;; but the expected output says that they're not!
|
rlm@0
|
185 (defvar error-person2
|
rlm@0
|
186 {:name "Erik 3851" :surname "Smith 3851"})
|
rlm@0
|
187
|
rlm@0
|
188 (defvar wrong-include2
|
rlm@0
|
189 {:name "Gil 5751" :surname "Smith 5751"})
|
rlm@0
|
190
|
rlm@0
|
191 ;;coderloop.scientist2> (select-keys (data error-person2) [:birth_date :death_date])
|
rlm@0
|
192 ;;{:death_date #<Date 0227-10-19>, :birth_date #<Date 0167-10-17>}
|
rlm@0
|
193 ;;coderloop.scientist2> (select-keys (data wrong-include2) [:birth_date :death_date])
|
rlm@0
|
194 ;;{:death_date #<Date 0227-01-02>, :birth_date #<Date 0167-12-31>}
|
rlm@0
|
195
|
rlm@0
|
196
|
rlm@0
|
197
|