$darkmode
DENOPTIM
CEBLUtils.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2019 Marco Foscato <marco.foscato@uib.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.combinatorial;
20
21import java.io.File;
22import java.util.ArrayList;
23import java.util.regex.Pattern;
24
25import org.apache.commons.io.FileUtils;
26
27import com.google.gson.Gson;
28import com.google.gson.GsonBuilder;
29
30import denoptim.constants.DENOPTIMConstants;
31import denoptim.exception.DENOPTIMException;
32import denoptim.files.SingletonFileAccess;
33import denoptim.graph.DGraph;
34import denoptim.io.DenoptimIO;
35import denoptim.programs.combinatorial.CEBLParameters;
36
37
44public class CEBLUtils
45{
46
47//------------------------------------------------------------------------------
48
54 public static String getNameOfStorageDir(CEBLParameters settings, int level)
55 {
56 String dirName = settings.getDBRoot()
57 + DENOPTIMConstants.FSEP
58 + DENOPTIMConstants.FSEIDXNAMEROOT
59 + level;
60 return dirName;
61 }
62
63//------------------------------------------------------------------------------
64
69 public static int getGraphIdFromStorageFile(String fileName)
71 {
72 String msg ="";
73 if (fileName.contains(Pattern.quote(DENOPTIMConstants.SERGFILENAMEROOT))
74 && fileName.contains(Pattern.quote(".")))
75 {
76 msg = "Failed attempt to extract a graphId from String '"
77 + fileName + "'";
78 throw new DENOPTIMException(msg);
79 }
80 String[] p1 = fileName.split(Pattern.quote(
82 String[] p2 = p1[1].split(Pattern.quote("."));
83 int graphId = -1;
84 try
85 {
86 graphId = Integer.parseInt(p2[0]);
87 }
88 catch (Throwable t)
89 {
90 throw new DENOPTIMException(msg);
91 }
92 return graphId;
93 }
94
95//------------------------------------------------------------------------------
96
101 public static String getBaseNameOfStorageFile(int graphId)
102 {
103 String baseName = DENOPTIMConstants.SERGFILENAMEROOT
104 + graphId + "."
106 return baseName;
107 }
108
109//------------------------------------------------------------------------------
110
115 public static String getNameOfStorageFile(CEBLParameters settings, int level,
116 int graphId)
117 {
118 String fileName = settings.getDBRoot()
119 + DENOPTIMConstants.FSEP
120 + DENOPTIMConstants.FSEIDXNAMEROOT
121 + level
122 + DENOPTIMConstants.FSEP
123 + getBaseNameOfStorageFile(graphId);
124 return fileName;
125 }
126
127//------------------------------------------------------------------------------
128
133 public static String getNameOfStorageIndexFile(CEBLParameters settings,
134 int level)
135 {
136 String fileName = settings.getDBRoot()
137 + DENOPTIMConstants.FSEP
138 + DENOPTIMConstants.FSEIDXNAMEROOT
139 + level
140 + DENOPTIMConstants.FSEP
141 + DENOPTIMConstants.FSEIDXNAMEROOT
142 + level + ".txt";
143 return fileName;
144 }
145
146//------------------------------------------------------------------------------
147
156 protected static void storeAllGraphsOfLevel(CEBLParameters settings,
157 ArrayList<DGraph> lstGraphs, int level) throws DENOPTIMException
158 {
159 for (DGraph g : lstGraphs)
160 {
161 //NOTE: the arraylist is supposed to hold the indeces used to
162 // create the next combination of fragments, but this method
163 // is used only for graphs built with the base scaffolds or
164 // the naked root graphs. Therefore there is no set of indeces
165 // to store and we fed the method with an empty array.
166 // NOTE2: the root Id is set to zero for the same reason.
167
168 DGraph c = g.clone();
169
170 storeGraphOfLevel(settings, c,level,0,new ArrayList<Integer>());
171 }
172 }
173
174//------------------------------------------------------------------------------
175
187 protected static void storeGraphOfLevel(CEBLParameters settings,
188 DGraph graph, int level, int rootId,
189 ArrayList<Integer> nextIds) throws DENOPTIMException
190 {
191 String outDir = getNameOfStorageDir(settings, level);
192 if (!denoptim.files.FileUtils.checkExists(outDir))
193 {
194 try
195 {
196 FileUtils.forceMkdir(new File(outDir));
197 }
198 catch (Throwable t)
199 {
200 String msg = "Cannot create folder " + outDir;
201 throw new DENOPTIMException(msg,t);
202 }
203 }
204
205 String fileSer = getNameOfStorageFile(settings, level,
206 graph.getGraphId());
207 String indexFile = getNameOfStorageIndexFile(settings, level);
208 String indexLine = graph.toString() + " => " + graph.getGraphId() + " "
209 + rootId + " " + nextIds;
210
211 SingletonFileAccess.getInstance().writeData(fileSer, graph.toJson(),
212 false);
213 SingletonFileAccess.getInstance().writeData(indexFile, indexLine, true);
214 }
215
216//------------------------------------------------------------------------------
217
222 protected static void serializeCheckPoint(CEBLParameters settings)
223 throws DENOPTIMException
224 {
225 Gson writer = new GsonBuilder().setPrettyPrinting().create();
226 DenoptimIO.writeData(settings.getCheckPointName(), writer.toJson(
227 settings.getCheckPoint()), false);
228 }
229
230//------------------------------------------------------------------------------
231
237 public static CheckPoint deserializeCheckpoint(String file)
238 throws DENOPTIMException
239 {
240 String s = DenoptimIO.readText(file);
241 Gson writer = new GsonBuilder().create();
242 CheckPoint chkpt = writer.fromJson(s, CheckPoint.class);
243 return chkpt;
244 }
245
246//------------------------------------------------------------------------------
247
248}
Helper methods for the exploration of the fragment space.
Definition: CEBLUtils.java:45
static CheckPoint deserializeCheckpoint(String file)
Converts a text file into the corresponding checkpoint object.
Definition: CEBLUtils.java:237
static String getNameOfStorageFile(CEBLParameters settings, int level, int graphId)
Definition: CEBLUtils.java:115
static void storeAllGraphsOfLevel(CEBLParameters settings, ArrayList< DGraph > lstGraphs, int level)
Serialize all DENOPTIMGraphs to file.
Definition: CEBLUtils.java:156
static String getNameOfStorageIndexFile(CEBLParameters settings, int level)
Definition: CEBLUtils.java:133
static String getBaseNameOfStorageFile(int graphId)
Definition: CEBLUtils.java:101
static String getNameOfStorageDir(CEBLParameters settings, int level)
Definition: CEBLUtils.java:54
static void serializeCheckPoint(CEBLParameters settings)
Store the checkpoint in a text file with json format.
Definition: CEBLUtils.java:222
static void storeGraphOfLevel(CEBLParameters settings, DGraph graph, int level, int rootId, ArrayList< Integer > nextIds)
Serialize a DENOPTIMGraph to a file.
Definition: CEBLUtils.java:187
static int getGraphIdFromStorageFile(String fileName)
Definition: CEBLUtils.java:69
Object collecting information needed to restart a FragSpaceExplorer job.
Definition: CheckPoint.java:40
General set of constants used in DENOPTIM.
static final String SERGFILENAMEEXT
Extension filenames of serialized graphs.
static final String SERGFILENAMEROOT
Prefix filenames of serialized graphs.
Singleton for synchronizing multi-thread safe file access.
static SingletonFileAccess getInstance()
Returns the single instance of this class.
synchronized void writeData(String fileName, String data, boolean append)
Write data as an unformatted string to a text file.
Container for the list of vertices and the edges that connect them.
Definition: DGraph.java:102
DGraph clone()
Returns almost "deep-copy" of this graph.
Definition: DGraph.java:3186
Utility methods for input/output.
static String readText(String fileName)
Read text from file.
static void writeData(String fileName, String data, boolean append)
Write text-like data file.
Parameters controlling execution of the combinatorial algorithm for exploration of a fragment space b...