-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
237 lines (198 loc) Β· 6.78 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
module DiseaseSpecification where
import Control.Monad
import Data.Time.Calendar
import Data.Time.Clock
import System.IO
import Text.Printf
type Name = String
type Virus = String
type Symptoms = [String]
data Quarantine
= Yes
| No
deriving (Eq, Show, Read)
type Date = (Integer, Int, Int)
type EndDate = Date
type Connections = [Name]
type Disease = (Name, Virus, Symptoms, Quarantine)
type Patient = (Name, Symptoms, Date)
type PatientQuarantine = (Name, String, EndDate, Connections)
-- insert new disease
insertDisease :: IO ()
insertDisease = do
putStr "Name: "
n <- getLine
putStr "Virus: "
v <- getLine
s <- addSymptoms []
putStr "Quarantine: "
q <- getLine
appendFile "data/diseases.txt" $ printf "%s %s %s %s\n" n v (join' "-" s) q
putStr "Insert another one? (y or n) "
resp <- getLine
when (resp == "y" || resp == "Y") insertDisease
-- insert new patient
insertPatient :: IO ()
insertPatient = do
putStr "Name: "
n <- getLine
-- add symptoms
s <- addSymptoms []
date <- utctDay <$> getCurrentTime
diseases <- loadDiseases
let ds = head $ map (`findDiseases` diseases) s
if null ds
then appendFile "data/patients.txt" $
printf "%s %s %s\n" n (join' "-" s) (show date)
else do
c <- addConnections []
let disease = getDiseaseName $ head ds
appendFile "data/patients.txt" $
printf "%s %s %s\n" n (join' "-" s) (show date)
appendFile "data/quarantines.txt" $
printf "%s %s %s %s\n" n disease (show $ addDays 40 date) (join' "-" c)
putStr "Insert another one? (y or n) "
resp <- getLine
when (resp == "y" || resp == "Y") insertPatient
updateQuarantine :: IO ()
updateQuarantine = do
date <- utctDay <$> getCurrentTime
q <- loadQuarantines
let expiredQuarantines =
filter (\(n, v, d, c) -> diffDays (read d :: Day) date >= 0) q
writeFile "data/quarantines.txt" $ save expiredQuarantines
return ()
generateGraph :: IO ()
generateGraph = do
q <- loadQuarantines
let connections = map (\(n, v, d, c) -> (n, wordsWhen (== '-') c)) q
writeFile "data/connections.dot" $ graph connections
graph list = printf "Digraph{ %s }\n" $ unwords $ map code list
code (n, c) = unwords $ map (printf "\"%s\" -> \"%s\"\n" n) c
save [] = ""
save ((n, v, d, c):xs) =
n ++ "\t" ++ v ++ "\t" ++ d ++ "\t" ++ c ++ "\n" ++ save xs
-- add connections
addConnections :: Connections -> IO Connections
addConnections xs = do
putStr "Add a person that you have contact with: "
connection <- getLine
let connections = xs ++ [connection]
putStr "Insert another connection? (y or n) "
resp <- getLine
if resp == "y" || resp == "Y"
then addConnections connections
else return connections
-- add symptons
addSymptoms :: Symptoms -> IO Symptoms
addSymptoms xs = do
putStr "Symptom: "
symptom <- getLine
let symptoms = xs ++ [symptom]
putStr "Insert another symptom? (y or n) "
resp <- getLine
if resp == "y" || resp == "Y"
then addSymptoms symptoms
else return symptoms
getDiseases [] = []
getDiseases ([name, virus, symptoms, quarantine]:xs) =
(name, virus, wordsWhen (== '-') symptoms, read quarantine :: Quarantine) :
getDiseases xs
loadDiseases = do
diseases <- readFile "data/diseases.txt"
return $ getDiseases $ map words $ lines diseases
printDiseases [] = ""
printDiseases ((name, virus, symptoms, quarantine):xs) =
"Diseases- name= " ++ name ++ ", virus= " ++ virus ++ "\n" ++ printDiseases xs
getPatients [] = []
getPatients ([name, symptoms, consultDate]:xs) =
( name
, wordsWhen (== '-') symptoms
, stringToDate $ wordsWhen (== '-') consultDate) :
getPatients xs
loadPatients = do
patients <- readFile "data/patients.txt"
return $ getPatients $ map words $ lines patients
getQuarantines [] = []
getQuarantines ([name, disease, endDate, connections]:xs) =
(name, disease, endDate, connections) : getQuarantines xs
loadQuarantines = do
quarantines <- readFile' "data/quarantines.txt"
return $ getQuarantines $ map words $ lines quarantines
findPatients :: Name -> [Patient] -> [Patient]
findPatients p [] = []
findPatients p xs = filter (\(n, s, c) -> n == p) xs
findDiseases :: String -> [Disease] -> [Disease]
findDiseases s = filter (\(n, v, x, q) -> s `elem` x && q == Yes)
getDiseaseName :: Disease -> Name
getDiseaseName (n, v, s, q) = n
getDiseaseVirus :: Disease -> Virus
getDiseaseVirus (n, v, s, q) = v
getPatientSymptoms :: Patient -> Symptoms
getPatientSymptoms (n, s, d) = s
-- utils
wordsWhen :: (Char -> Bool) -> String -> [String]
wordsWhen p s =
case dropWhile p s of
"" -> []
s' -> w : wordsWhen p s''
where (w, s'') = break p s'
join' sep =
foldr
(\a b ->
a ++
if b == ""
then b
else sep ++ b)
""
date :: IO Date -- :: (year, month, day)
date = toGregorian . utctDay <$> getCurrentTime
stringToDate :: [String] -> Date
stringToDate [y, m, d] = (read y :: Integer, read m :: Int, read d :: Int)
readFile' filename =
withFile filename ReadMode $ \handle -> do
theContent <- hGetContents handle
mapM return theContent
countQuarantinePatients [] = 0
countQuarantinePatients xs = foldr (\x -> (+) 1) 0 xs
main :: IO ()
main = do
putStrLn "1 - Insert a new disease"
putStrLn "2 - Insert a new patient"
putStrLn "3 - Find the patient's virus"
putStrLn "4 - Number of patients in quarantine"
putStrLn "5 - Update the quarantine by the current date"
putStrLn "6 - Generate graph of all connections"
putStr "Option: "
resp <- getLine
if resp == "1"
then insertDisease
else if resp == "2"
then insertPatient
else if resp == "3"
then do
putStr "Patient name: "
patientName <- getLine
allPatients <- loadPatients
let patients = findPatients patientName allPatients
if null patients
then putStrLn "Patient not found"
else do
diseases <- loadDiseases
let symptoms = getPatientSymptoms $ head patients
let ds = map (`findDiseases` diseases) symptoms
if null ds
then error "Disease not found"
else print $ getDiseaseVirus $ head $ head ds
else if resp == "4"
then do
quarantines <- loadQuarantines
print $ countQuarantinePatients quarantines
else if resp == "5"
then updateQuarantine
else if resp == "6"
then generateGraph
else error "Wrong option"
putStr "Want to continue? "
resp <- getLine
when (resp == "y" || resp == "Y") main