21import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
22import static java.nio.file.StandardWatchEventKinds.OVERFLOW;
25import java.io.IOException;
26import java.nio.file.FileSystems;
27import java.nio.file.Path;
28import java.nio.file.WatchEvent;
29import java.nio.file.WatchKey;
30import java.nio.file.WatchService;
31import java.util.ArrayList;
32import java.util.Arrays;
33import java.util.HashSet;
35import java.util.logging.Level;
36import java.util.logging.Logger;
38import denoptim.constants.DENOPTIMConstants;
39import denoptim.exception.DENOPTIMException;
40import denoptim.exception.ExceptionUtils;
41import denoptim.io.DenoptimIO;
42import denoptim.logging.StaticLogger;
43import denoptim.programs.denovo.GAParameters;
58 private final WatchKey
key;
66 private final String
NL = System.getProperty(
"line.separator");
80 +
"for live instructions.");
81 this.watcher = FileSystems.getDefault().newWatchService();
90 @SuppressWarnings(
"unchecked")
103 for (WatchEvent<?> event :
key.pollEvents())
105 WatchEvent.Kind<?> kind =
event.kind();
109 if (kind == OVERFLOW) {
113 WatchEvent<Path> ev =
null;
115 ev = (WatchEvent<Path>) event;
116 }
catch (Throwable t) {
120 Path name = ev.context();
121 Path child =
pathname.resolve(name);
131 }
catch (InterruptedException e) {
133 }
catch (Throwable t) {
136 String msg = this.getClass().getSimpleName() +
" stopped! From "
137 +
"now on, we cannot listen to commands from the "
138 +
"interface." + DENOPTIMConstants.EOL +
" Cause: "
142 logger.log(Level.SEVERE, msg);
155 ArrayList<String> lines =
null;
159 logger.log(Level.WARNING,
"Unable to read file '"
160 + file.getAbsolutePath() +
"'. Any instruction contained "
161 +
"in that file is ignored. Hint: " + e.getMessage() +
NL);
166 if (lines.size() == 0)
169 logger.log(Level.WARNING,
"Empty instructions in '"
170 + file.getAbsolutePath() +
"'.");
173 for (String line : lines)
175 if (line.startsWith(
"STOP_GA"))
177 logger.log(Level.SEVERE,
"GA run will be "
178 +
"stopped upon external request from '"
179 + file.getAbsolutePath() +
"'." +
NL);
186 if (line.startsWith(
"REMOVE_CANDIDATE"))
188 String candIDs = line.substring(
189 "REMOVE_CANDIDATE".length()).trim();
190 logger.log(Level.SEVERE,
"Removing '"
191 + candIDs +
"' upon external request from '"
192 + file.getAbsolutePath() +
"'." +
NL);
193 String[] parts = candIDs.split(
"\\s+");
194 Set<String> candNames =
new HashSet<String>(
195 Arrays.asList(parts));
202 if (line.startsWith(
"ADD_CANDIDATE"))
204 String fileNamesLst = line.substring(
205 "ADD_CANDIDATE".length()).trim();
206 logger.log(Level.SEVERE,
"Adding candidates from '"
207 + fileNamesLst +
"' upon external request from '"
208 + file.getAbsolutePath() +
"'." +
NL);
209 String[] parts = fileNamesLst.split(
"\\s+");
210 Set<String> paths =
new HashSet<String>(Arrays.asList(parts));
224 this.watcher.close();
static String getStackTraceAsString(Throwable t)
Prints the stack trace of an exception into a string.
DENOPTIM's evolutionary algorithm.
void addCandidates(Set< String > pathNames)
Adds candidate IDs to the list of "to-be-included" candidates.
void removeCandidates(Set< String > candID)
Adds candidate IDs to the list of "to-be-removed" candidates.
Service that watches the interface folder (i.e., see GAParameters#interfaceDir) for instructions comi...
void processExternalCmdFile(File file)
final WatchService watcher
Logger logger
Program-specific logger.
void setReferenceToRunningEAlgorithm(EvolutionaryAlgorithm ea)
ExternalCmdsListener(Path pathname, Logger logger)
boolean intendedStop
Flag signaling that we are intentionally interrupting the service.
void run()
Starts listening for events and keeps listening in its own thread.
Utility methods for input/output.
static ArrayList< String > readList(String fileName)
Read list of data as text.
Logger class for DENOPTIM.
static final Logger appLogger