-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
394 lines (343 loc) · 17.4 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
-- =========================================== --
-- === Denise Marzorati - FCEIA - 2017 === --
-- ===== Módulo principal - "Intérprete" ===== --
-- =========================================== --
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
-- Módulos propios
import Common
import Draw
import Eval
import GA
import Parse
import ParseSVG
import ParseSVGElements as PSE
import Polygons as Pol --(embedPols, takeOut, rotate, getRotateds, getContainers)
import PrettyPrint as PP
import Shrink
-- Módulos prestados
import Control.Exception
import Control.Monad.ST
import Data.ByteString as BS (readFile)
import Data.Char (isSpace)
import Data.List as L
import Data.Maybe (fromJust, isJust, isNothing)
import Data.Set as Set
import Data.String as S
import Data.Text as T (unpack, append)
import Text.PrettyPrint.HughesPJ as PJ (render, float)
import Text.XmlHtml as XML
import System.Console.Readline
import System.Directory (doesFileExist)
import System.Environment (getArgs)
import System.IO as SIO
-- Definiciones de tipos datos --
---------------------------------
data Command = Help
| Browse
| Clear
| SetK String
| Load String
| LoadC String
| LoadPo String
| LoadLines String
| LoadPa String
| LoadAll String
| DefC String
| DefP String
| Draw String
| Noop
data Search = R -- Rectangle
| Po -- Polygon
| Pl -- Polyline
| Pa -- Path
| All -- Everything
deriving Show
data State = S { k :: Float, nuc :: Int, sc :: Set Container, sp :: Polygons } deriving Show
-- Funciones --
---------------
main :: IO ()
main = do printHello
args <- getArgs
readEvalPrintLoop args (S {k = 0, nuc = 0, sc = Set.empty, sp = []})
printHello :: IO ()
printHello = do SIO.putStrLn "\n*** Intérprete de polígonos ***"
SIO.putStrLn ":? para desplegar ayuda\n"
readEvalPrintLoop :: [String] -> State -> IO ()
readEvalPrintLoop args s =
do maybeLine <- readline ">> "
case maybeLine of
Nothing -> return ()
Just ":q" -> return ()
Just line -> do addHistory line
c <- interpretCommand line
s' <- handleCommand c s
readEvalPrintLoop args s'
data InteractiveCommand = Cmd [String] String (String -> Command) String
commands :: [InteractiveCommand]
commands =
[Cmd [":help", ":?"] "" (const Help) "-> Ayuda",
Cmd [":browse", ":b"] "" (const Browse) "-> Imprimir las definiciones actuales",
Cmd [":clear", ":c"] "" (const Clear) "-> Borrar todas las definiciones realizadas (tanto de archivos como en el intérprete)",
Cmd [":setkerf", ":sk"] "<float>" SetK "-> Fijar el valor del kerf a <float>",
Cmd [":load",":l"] "<file>" Load "-> Cargar el archivo <file>",
Cmd [":loadcontainers", ":lc"] "<file>" LoadC "-> Cargar los rectángulos definidos en el archivo SVG <file>",
Cmd [":loadpolygons", ":lpo"] "<file>" LoadPo "-> Cargar los polígonos definidos en el archivo SVG <file>",
Cmd [":loadpolylines",":lpl"] "<file>" LoadLines "-> Cargar los polylines definidos en el archivo SVG <file>",
Cmd [":loadpaths",":lpa"] "<file>" LoadPa "-> Cargar los paths definidos en el archivo SVG <file>",
Cmd [":loadall", ":la"] "<file>" LoadAll "-> Cargar los rectángulos, polígonos y paths definidos en el archivo SVG <file>",
Cmd [":defc", ":dc"] "<expr>" (\x -> DefC ("cdef " ++ x)) "-> Definir el rectángulo <expr> desde el intérprete",
Cmd [":defp ", ":dp"] "<expr>" (\x -> DefP ("pdef " ++ x)) "-> Definir el polígono <expr> desde el intérprete",
Cmd [":draw", ":d"] "<c> <m> <t> <p> <s>" Draw
("-> Ejecutar el algoritmo con todos los elementos cargados (c: contenedor seleccionado, " ++
"m: cardinal del conjunto poblacional, t: cantidad de iteraciones, p : probilidad de " ++
"rotación, s: nombre del archivo de destino)")]
interpretCommand :: String -> IO Command
interpretCommand s = if isPrefixOf ":" s
then do let (cmd, t) = break isSpace s
t' = dropWhile isSpace t
let matching = L.filter (\(Cmd cs _ _ _) -> any (cmd ==) cs) commands
case matching of
[] ->
do putStrLn "Comando desconocido, por favor reintente"
return Noop
[Cmd _ _ f _] ->
return (f t')
_ -> do putStrLn ("Comando ambiguo")
return Noop
else do putStrLn "Comando desconocido, por favor reintente"
return Noop
handleCommand :: Command -> State -> IO State
handleCommand cmd s =
case cmd of
Help -> do printHelp
return s
Browse -> do printEnv s
return s
Clear -> return (s {k = 0, sc = Set.empty, nuc = 0, sp = []})
SetK k -> case parseFloat k of
Parse.Ok v ->
checkKerf v s
Parse.Failed str ->
do putStrLn str
return s
Load f -> parseFiles (words f) s
LoadC f -> parseSVGFiles (words f) s R
LoadPo f -> parseSVGFiles (words f) s Po
LoadLines f -> parseSVGFiles (words f) s Pl
LoadPa f -> parseSVGFiles (words f) s Pa
LoadAll f -> parseSVGFiles (words f) s All
DefP e -> case parseDefs e 1 of
Parse.Ok v ->
checkPolygons (L.map evalDefP v) s
Parse.Failed str ->
do SIO.putStrLn str
return s
DefC e -> case parseDefs e 1 of
Parse.Ok v ->
checkCons (L.map evalDefC v) s
Parse.Failed str ->
do SIO.putStrLn str
return s
Draw a -> do evalState (words a) s
return s
Noop -> return s
printHelp :: IO ()
printHelp = do putStrLn "Los comandos disponibles son los siguientes:"
putStrLn ""
mapM_ putStrLn (L.map (\x -> Main.complete (concat (intersperse ", " (getNames x))) ++ getHelp x) commands)
complete :: String -> String
complete s = s ++ (replicate (25 - length s) ' ')
getNames :: InteractiveCommand -> [String]
getNames (Cmd s _ _ _) = s
getHelp :: InteractiveCommand -> String
getHelp (Cmd _ _ _ h) = h
printEnv :: State -> IO ()
printEnv s = do putStrLn "\n************************************************************************"
putStr "Kerf: "
print (k s)
putStrLn ""
putStrLn "Contenedores: "
putStrLn (PJ.render (printContainers (toList (sc s))))
putStrLn "Polígonos: "
putStr (PJ.render (printPolygons (sp s)))
putStrLn "************************************************************************\n"
-- ** Comando :d **
evalState :: [String] -> State -> IO ()
evalState str s = do a <- parseArgs str s
case a of
Just (con, m, t, pro, file) ->
do let eP = embedPols (sp s)
let res = geneticAlgorithm (L.map fstThree eP) con m t pro
case res of
Just v ->
do let order = L.map rid v
r <- getRotateds v (L.map fstThree eP)
pols <- getPols eP r v
pols' <- orderByGA pols order
let closer = shrink ([], pols') con
let sol = getSolution (L.map p closer)
putStrLn (PJ.render (printSolution sol))
SIO.writeFile file (draw con closer (k s))
return ()
Nothing ->
do SIO.putStrLn "\nLos polígonos especificados no entran en el contenedor"
print con
return ()
_ ->
return ()
fstThree :: (a, b, c) -> a
fstThree (a, b, c) = a
-- No se permite repetir nombre de archivos para no sobreescribir algún archivo que sirva como fuente
parseArgs :: [String] -> State -> IO (Maybe (Container, Int, Int, Float, String))
parseArgs [c, m, t, p, s] st =
case reads c :: [(Int, String)] of
[(c, "")] ->
let con = Set.filter (\x -> rid x == c) (sc st)
in if Set.null con
then printiarg
else
case reads m :: [(Int, String)] of
[(n1, "")] ->
case reads t :: [(Int, String)] of
[(n2, "")] ->
case parseFloat p of
Parse.Ok v ->
case reads s :: [(String, String)] of
[(st, "")] ->
do b <- doesFileExist st
case b of
True -> do SIO.putStrLn "\nArchivo existente, elija otro nombre\n"
return Nothing
False -> if n1 < 1 || n2 < 1 || v < 0 || v > 1
then printiarg
else return (Just (findMin con, n1, n2, v, st))
Parse.Failed str -> do SIO.putStrLn str
return Nothing
_ -> printiarg
_ -> printiarg
_ -> printiarg
parseArgs _ _ = printiarg
printiarg :: IO (Maybe a)
printiarg = do SIO.putStrLn invalidarg
return Nothing
invalidarg = "\nArgumento inválido, por favor reintente\n"
-- ** Parsing de archivos **
parseSVGFiles :: [String] -> State -> Search -> IO State
parseSVGFiles [] s _ = return s
parseSVGFiles (f:fs) s t = do s' <- parseSVGFile f s t
parseSVGFiles fs s' t
-- El tercer argumento es para saber qué clase de elementos se busca dentro del archivo SVG
parseSVGFile :: String -> State -> Search -> IO State
parseSVGFile f s t = do putStrLn $ "Abriendo " ++ f ++ "..."
if L.isSuffixOf ".svg" f
then do str <- catch (BS.readFile f)
(\e -> do let err = show (e :: IOException)
SIO.hPutStr stderr ("*** Error: no se pudo abrir" ++ err ++ "\n")
return "")
case parseXML "" str of
Left st -> do putStrLn $ f ++ st
return s
Right d ->
let dcon = docContent d
r = L.map T.unpack (getElements dcon lookR "rect")
po = L.map T.unpack (getElements dcon lookP "polygon")
pl = L.map T.unpack (L.map (T.append "pl ") (getElements dcon lookP "polyline"))
pa = L.map T.unpack (getElements dcon lookD "path")
in case t of
R -> parse r s t
Po -> parse po s t
Pl -> parse pl s t
Pa -> parse pa s t
All -> do s1 <- parse r s R
s2 <- parse po s1 Po
s3 <- parse pl s2 Pl
parse pa s3 Pa
else do putStrLn "*** Error: no es un archivo SVG"
return s
parse [] s t = return s
parse (x:xs) s t = case t of
R -> case parseRect x of
PSE.Ok v ->
do s' <- checkCons [evalRect v] s
parse xs s' t
PSE.Failed str ->
do SIO.putStrLn str
parse xs s t
Po -> case parsePolygon x of
PSE.Ok v ->
do s' <- checkPolygons [evalSVGPol v (k s)] s
parse xs s' t
PSE.Failed str ->
do SIO.putStrLn str
parse xs s t
Pl -> case parsePolyLine x of
PSE.Ok v ->
do s' <- checkPolygons [evalSVGPol v (k s)] s
parse xs s' t
PSE.Failed str ->
do SIO.putStrLn str
parse xs s t
Pa -> case parsePath x of
PSE.Ok v ->
do s' <- checkPolygons [evalPath v (k s)] s
parse xs s' t
PSE.Failed str ->
do SIO.putStrLn str
parse xs s t
parseFiles :: [String] -> State -> IO State
parseFiles [] s = return s
parseFiles (f:fs) s = do s' <- parseFile f s
parseFiles fs s'
parseFile :: String -> State -> IO State
parseFile f s = do putStrLn $ "Abriendo " ++ f ++ "..."
str <- catch (SIO.readFile f)
(\e -> do let err = show (e :: IOException)
hPutStr stderr ("*** Error: no se pudo abrir " ++ err ++ "\n")
return "")
if str == ""
then return s
else case parseMac str of
Parse.Failed st -> do putStrLn $ f ++ ": " ++ st
return s
Parse.Ok v -> do s1 <- checkKerf (getKerf v) s
let eMac = evalMac v
s2 <- checkCons (fst eMac) s1
checkPolygons (snd eMac) s2
getDefs :: Machine -> [Def]
getDefs (Kerf _ ds) = ds
getKerf :: Machine -> Float
getKerf (Kerf k _) = k
setKerf :: Machine -> Float -> Machine
setKerf (Kerf _ ds) k = Kerf k ds
-- ** Chequeo de tipos **
checkKerf :: Float -> State -> IO State
checkKerf v s = if v >= 0
then return (s {k = v, sp = L.map (addKerf (v - (k s)) areaSigned) (sp s)})
else do putStrLn "El valor del kerf debe ser mayor o igual a 0"
return s
checkCons :: Containers -> State -> IO State
checkCons cs s = if L.null n
then res
else do putStrLn "Los siguientes contenedores no son válidos: "
putStrLn $ PJ.render (printContainers n)
res
where (j, n) = sepJN cs checkCon
res = return (insertC j s)
insertC :: Containers -> State -> State
insertC [] s = s
insertC (c:cs) s = insertC cs (s {sc = Set.insert (c {rid = nuc s}) (sc s), nuc = nuc s + 1})
checkPolygons ps s = catch (checkPols ps s checkPol)
(\e -> do let err = show (e :: ErrorCall)
checkPols ps s checkPolSlow)
checkPols ps s f = if L.null n
then res
else do putStrLn "Los siguientes polígonos no son válidos: "
putStrLn $ PJ.render (printPolygons n)
res
where (j, n) = sepJN ps f
res = return (s {sp = sp s ++ j})
-- El lado izquierdo son los polígonos "legales"
sepJN :: [a] -> (a -> Maybe a) -> ([a], [a])
sepJN xs f = L.partition (isJust . f) xs