-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstanceRepository.java
More file actions
61 lines (50 loc) · 1.97 KB
/
Copy pathInstanceRepository.java
File metadata and controls
61 lines (50 loc) · 1.97 KB
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
package com.glaucoma.app;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Persiste y recupera instancias del problema ({@link ProblemInstance}) en formato JSON,
* dentro de la carpeta {@code instances}.
*/
public class InstanceRepository {
private static final Logger logger = LoggerFactory.getLogger(InstanceRepository.class);
/**
* Carga una instancia previamente guardada.
*
* @param filename nombre del fichero JSON dentro de la carpeta {@code instances}
* @return la instancia cargada, o {@code null} si ocurrió un error de lectura
*/
public static ProblemInstance loadFromJSON(String filename) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(new File("instances/" + filename), ProblemInstance.class);
} catch (IOException e) {
System.err.println("Error al cargar la instancia: " + e.getMessage());
return null;
}
}
/**
* Guarda una instancia en un fichero JSON con nombre único basado en fecha y hora.
*
* @param instance instancia del problema a guardar
* @param options opciones usadas para nombrar el fichero (cantidad de pacientes y días)
*/
public static void saveInstance(ProblemInstance instance, OptimizerOptions options) {
try {
Files.createDirectories(Paths.get("instances"));
String dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"));
String filename = String.format("instances/P%d_D%d_%s.json",
options.patientsQuantity(), options.totalDays(), dateTime);
new ObjectMapper().writerWithDefaultPrettyPrinter()
.writeValue(new File(filename), instance);
} catch (Exception e) {
logger.error("Error al guardar la instancia.", e);
}
}
}