Use a primary constructor and only do assigments
diff --git a/plugins/org.eclipse.epsilon.executors/src/org/eclipse/epsilon/executors/EpsilonExecutor.java b/plugins/org.eclipse.epsilon.executors/src/org/eclipse/epsilon/executors/EpsilonExecutor.java
index 77f3886..988cc93 100644
--- a/plugins/org.eclipse.epsilon.executors/src/org/eclipse/epsilon/executors/EpsilonExecutor.java
+++ b/plugins/org.eclipse.epsilon.executors/src/org/eclipse/epsilon/executors/EpsilonExecutor.java
@@ -45,6 +45,7 @@
*/
public class EpsilonExecutor implements Executor {
+ /** The Constant logger. */
private static final Logger logger = LoggerFactory.getLogger(EpsilonExecutor.class);
/**
@@ -53,14 +54,16 @@
private final EpsilonLanguageExecutor<?> languageExecutor;
/**
- * The Script to be executed. Alternatively a block of code can be provided, see {@link #code}
+ * The Script to be executed. Alternatively a block of code can be provided, see {@link #code}.
+ * If both are present, the Script takes priority.
*/
- private final Optional<Path> script;
+ private final Path script;
/**
* The Code to be executed. Alternatively a script file can be provided, see {@link #script}.
+ * If both are present, the Script takes priority
*/
- private final Optional<String> code;
+ private final String code;
/**
* The runtime parameters.
@@ -88,98 +91,132 @@
private final boolean profileExecution; // = false;
- private final Optional<ExecutionTimeData> timeData;
+ /** The time data. */
+ private final ExecutionTimeData timeData;
- // Constructors with script
+ /**
+ * Instantiates a new epsilon executor.
+ *
+ * @param languageExecutor the language executor
+ * @param scriptPath the script path
+ */
public EpsilonExecutor(
EpsilonLanguageExecutor<?> languageExecutor,
- Path scriptPath) throws EpsilonExecutorException {
- this(languageExecutor, scriptPath, Collections.emptySet(), Collections.emptyMap(), Collections.emptySet(), true, false);
+ Path scriptPath) {
+ this(languageExecutor, scriptPath, null, Collections.emptySet(), Collections.emptyMap(), Collections.emptySet(), true, false);
}
+ /**
+ * Instantiates a new epsilon executor.
+ *
+ * @param languageExecutor the language executor
+ * @param scriptPath the script path
+ * @param models the models to run the script against
+ */
public EpsilonExecutor(
EpsilonLanguageExecutor<?> languageExecutor,
Path scriptPath,
- Collection<IModel> models) throws EpsilonExecutorException {
- this(languageExecutor, scriptPath, models, Collections.emptyMap(), Collections.emptySet(), true, false);
+ Collection<IModel> models) {
+ this(languageExecutor, scriptPath, null, models, Collections.emptyMap(), Collections.emptySet(), true, false);
}
+ /**
+ * Instantiates a new epsilon executor.
+ *
+ * @param languageExecutor the language executor
+ * @param scriptPath the script path
+ * @param models the models to run the script against
+ * @param parameters parameters to pass to the execution
+ * @param nativeDelegates the native delegates required for execution
+ */
public EpsilonExecutor(
EpsilonLanguageExecutor<?> languageExecutor,
Path scriptPath,
Collection<IModel> models,
Map<String, Object> parameters,
- Collection<IToolNativeTypeDelegate> nativeDelegates) throws EpsilonExecutorException {
- this(languageExecutor, scriptPath, models, parameters, nativeDelegates, true, false);
+ Collection<IToolNativeTypeDelegate> nativeDelegates) {
+ this(languageExecutor, scriptPath, null, models, parameters, nativeDelegates, true, false);
}
-
+
+ /**
+ * Instantiates a new epsilon executor.
+ *
+ * @param languageExecutor the language executor
+ * @param code the code to execute
+ */
public EpsilonExecutor(
EpsilonLanguageExecutor<?> languageExecutor,
- Path scriptPath,
- Collection<IModel> models,
- Map<String, Object> parameters,
- Collection<IToolNativeTypeDelegate> nativeDelegates,
- boolean disposeModels,
- boolean profileExecution) throws EpsilonExecutorException {
- super();
- this.languageExecutor = languageExecutor;
- this.script = Optional.of(scriptPath);
- this.code = Optional.empty();
- this.models = new HashSet<>(models);
- this.parameters = parameters;
- this.nativeDelegates = new HashSet<>(nativeDelegates);
- this.disposeModels = disposeModels;
- this.profileExecution = profileExecution;
- this.timeData = profileExecution ? Optional.of(new ExecutionTimeData()) : Optional.empty();
- parseSource();
- }
-
-
- // Constructors with code
- public EpsilonExecutor(
- EpsilonLanguageExecutor<?> languageExecutor,
- String code) throws EpsilonExecutorException {
- this(languageExecutor, code, Collections.emptySet(), Collections.emptyMap(), Collections.emptySet(), true, false);
+ String code) {
+ this(languageExecutor, null, code, Collections.emptySet(), Collections.emptyMap(), Collections.emptySet(), true, false);
}
+ /**
+ * Instantiates a new epsilon executor.
+ *
+ * @param languageExecutor the language executor
+ * @param code the code to execute
+ * @param models the models to run the code against
+ */
public EpsilonExecutor(
EpsilonLanguageExecutor<?> languageExecutor,
String code,
- Collection<IModel> models) throws EpsilonExecutorException {
- this(languageExecutor, code, models, Collections.emptyMap(), Collections.emptySet(), true, false);
+ Collection<IModel> models) {
+ this(languageExecutor, null, code, models, Collections.emptyMap(), Collections.emptySet(), true, false);
}
+ /**
+ * Instantiates a new epsilon executor.
+ *
+ * @param languageExecutor the language executor
+ * @param code the code to execute
+ * @param models the models to run the script against
+ * @param parameters parameters to pass to the execution
+ * @param nativeDelegates the native delegates required for execution
+ */
public EpsilonExecutor(
EpsilonLanguageExecutor<?> languageExecutor,
String code,
Collection<IModel> models,
Map<String, Object> parameters,
- Collection<IToolNativeTypeDelegate> nativeDelegates) throws EpsilonExecutorException {
- this(languageExecutor, code, models, parameters, nativeDelegates, true, false);
+ Collection<IToolNativeTypeDelegate> nativeDelegates) {
+ this(languageExecutor, null, code, models, parameters, nativeDelegates, true, false);
}
-
+
+ /**
+ * Instantiates a new epsilon executor.
+ *
+ * @param languageExecutor the language executor
+ * @param scriptPath the script path
+ * @param code the code
+ * @param models the models
+ * @param parameters the parameters
+ * @param nativeDelegates the native delegates
+ * @param disposeModels the dispose models
+ * @param profileExecution the profile execution
+ * @throws EpsilonExecutorException the epsilon executor exception
+ */
public EpsilonExecutor(
- EpsilonLanguageExecutor<?> languageExecutor,
- String code,
- Collection<IModel> models,
- Map<String, Object> parameters,
- Collection<IToolNativeTypeDelegate> nativeDelegates,
- boolean disposeModels,
- boolean profileExecution) throws EpsilonExecutorException {
- super();
- this.languageExecutor = languageExecutor;
- this.script = Optional.empty();
- this.code = Optional.of(code);
- parseSource();
- this.models = new HashSet<>(models);
- this.parameters = parameters;
- this.nativeDelegates = new HashSet<>(nativeDelegates);
- this.disposeModels = disposeModels;
- this.profileExecution = profileExecution;
- this.timeData = profileExecution ? Optional.of(new ExecutionTimeData()) : Optional.empty();
- }
+ EpsilonLanguageExecutor<?> languageExecutor,
+ Path scriptPath,
+ String code,
+ Collection<IModel> models,
+ Map<String, Object> parameters,
+ Collection<IToolNativeTypeDelegate> nativeDelegates,
+ boolean disposeModels,
+ boolean profileExecution) {
+ super();
+ this.languageExecutor = languageExecutor;
+ this.script = scriptPath;
+ this.code = code;
+ this.models = new HashSet<>(models);
+ this.parameters = parameters;
+ this.nativeDelegates = new HashSet<>(nativeDelegates);
+ this.disposeModels = disposeModels;
+ this.profileExecution = profileExecution;
+ this.timeData = profileExecution ? new ExecutionTimeData() : null;
+ }
/**
* A class that keeps track of the execution durations of the different stages of execution, and
@@ -188,21 +225,44 @@
*
*/
public static class ExecutionTimeData {
- private static String logMessagesSeparator = "-----------------------------------------------------";
- private final String osNameAndVersion;
- private final String javaVersion;
- private final String cpuName;
- private final int logicalProcessors;
- private final String date;
+
+ /** The log messages separator. */
+ private static String logMessagesSeparator = "-----------------------------------------------------";
+
+ /** The os name and version. */
+ private final String osNameAndVersion;
+
+ /** The java version. */
+ private final String javaVersion;
+
+ /** The cpu name. */
+ private final String cpuName;
+
+ /** The logical processors. */
+ private final int logicalProcessors;
+
+ /** The date. */
+ private final String date;
+
+ /** The start nanos. */
private Long startNanos;
/**
* The measured execution information.
*/
private Map<String, Duration> profiledStages = new HashMap<>();
+
+ /** The profiled rules. */
private Map<String, Duration> profiledRules = new HashMap<>();
+
+ /** The started. */
private Map<String, Long> started = new HashMap<>();
+
+ /** The duration. */
private Duration duration;
+ /**
+ * Instantiates a new execution time data.
+ */
ExecutionTimeData() {
osNameAndVersion = getOsNameAndVersion();
javaVersion = getJavaVersion();
@@ -211,6 +271,9 @@
date = getTime();
}
+ /**
+ * Log start.
+ */
void logStart() {
startNanos = System.nanoTime();
logger.info(buildLines(
@@ -223,6 +286,9 @@
));
}
+ /**
+ * Log end.
+ */
void logEnd() {
long endTimeNanos = System.nanoTime();
this.duration = Duration.ofNanos(endTimeNanos-startNanos);
@@ -240,6 +306,12 @@
}
+ /**
+ * Builds the lines.
+ *
+ * @param lines the lines
+ * @return the string
+ */
String buildLines(Object... lines) {
StringBuilder linesAsStr = new StringBuilder();
String nL = System.lineSeparator();
@@ -249,16 +321,31 @@
return linesAsStr.toString();
}
+ /**
+ * Start stage.
+ *
+ * @param name the name
+ */
void startStage(String name) {
started.put(name, System.nanoTime());
}
+ /**
+ * End stage.
+ *
+ * @param name the name
+ */
void endStage(String name) {
long endTimeNanos = System.nanoTime();
Long startTime = started.getOrDefault(name, endTimeNanos);
profiledStages.put(name, Duration.ofNanos(endTimeNanos-startTime));
}
+ /**
+ * End module.
+ *
+ * @param languageExecutor the language executor
+ */
void endModule(EpsilonLanguageExecutor<?> languageExecutor) {
Optional<RuleProfiler> ruleProfiler = languageExecutor.getRuleProfiler();
if(ruleProfiler.isPresent()) {
@@ -275,7 +362,8 @@
/**
* Return the duration of the "prepareExecution" stage. A negative value indicates that the
* stage was not signalled as finished.
- * @return
+ *
+ * @return the prepare execution duration
*/
public Optional<Duration> getPrepareExecutionDuration() {
return Optional.ofNullable(profiledStages.get("prepareExecution"));
@@ -284,7 +372,8 @@
/**
* Return the duration of the "preProcess" stage. A negative value indicates that the
* stage was not signalled as finished.
- * @return
+ *
+ * @return the pre process duration
*/
public Optional<Duration> getPreProcessDuration() {
return Optional.ofNullable(profiledStages.get("preProcess"));
@@ -293,7 +382,8 @@
/**
* Return the duration of the "postProcess" stage. A negative value indicates that the
* stage was not signalled as finished.
- * @return
+ *
+ * @return the post process duration
*/
public Optional<Duration> getPostProcessDuration() {
return Optional.ofNullable(profiledStages.get("postProcess"));
@@ -302,31 +392,47 @@
/**
* Return the duration of the Epsilon module execution. A negative value indicates that the
* stage was not signalled as finished.
- * @return
+ *
+ * @return the scrpit execution duration
*/
public Optional<Duration> getScrpitExecutionDuration() {
return Optional.ofNullable(profiledStages.get("execute"));
}
+ /**
+ * Gets the total duration.
+ *
+ * @return the total duration
+ */
public Optional<Duration> getTotalDuration() {
return Optional.ofNullable(this.duration);
}
+ /**
+ * Gets the rule duration.
+ *
+ * @param name the name
+ * @return the rule duration
+ */
public Optional<Duration> getRuleDuration(String name) {
return Optional.ofNullable(profiledRules.get(name));
}
+ /**
+ * Gets the rules durations.
+ *
+ * @return the rules durations
+ */
public Iterator<Entry<String, Duration>> getRulesDurations() {
return profiledRules.entrySet().iterator();
}
-
}
@Override
public Optional<ExecutionTimeData> getExecutionTimeData() {
- return timeData;
+ return Optional.ofNullable(timeData);
}
/**
@@ -364,15 +470,15 @@
preProfile();
prepareExecution();
logger.info("Pre-process execution");
- timeData.ifPresent(td -> td.startStage("preProcess"));
+ startStage("preProcess");
languageExecutor.preProcess();
R result;
if (profileExecution) {
- timeData.ifPresent(td -> td.endStage("preProcess"));
+ endStage("preProcess");
}
try {
if (profileExecution) {
- timeData.ifPresent(td -> td.startStage("execute"));
+ startStage("execute");
}
// @SuppressWarnings("unchecked") OK because each engine has different return types.
result = (R) languageExecutor.execute();
@@ -384,24 +490,48 @@
}
finally {
if (profileExecution) {
- timeData.ifPresent(td -> td.endStage("execute"));
+ endStage("execute");
}
}
logger.info("Post-process execution.");
if (profileExecution) {
- timeData.ifPresent(td -> td.startStage("postProcess"));
+ startStage("postProcess");
}
languageExecutor.postProcess();
if (profileExecution) {
- timeData.ifPresent(td -> td.endStage("postProcess"));
+ endStage("postProcess");
}
if (profileExecution) {
- timeData.ifPresent(td -> td.endModule(languageExecutor));
- timeData.ifPresent(td -> td.logEnd());
+ if (timeData != null) {
+ timeData.endModule(languageExecutor);
+ timeData.logEnd();
+ }
}
return result;
}
+
+ /**
+ * Start stage.
+ *
+ * @param stageName the stage name
+ */
+ private void startStage(String stageName) {
+ if (timeData != null) {
+ timeData.startStage(stageName);
+ }
+ }
+
+ /**
+ * End stage.
+ *
+ * @param stageName the stage name
+ */
+ private void endStage(String stageName) {
+ if (timeData != null) {
+ timeData.endStage(stageName);
+ }
+ }
/**
* Disposes the executor. Implementing classes should perform any clean actions.
@@ -427,15 +557,27 @@
languageExecutor.dispose();
}
+ /**
+ * Pre profile.
+ */
private void preProfile() {
- timeData.ifPresent(ExecutionTimeData::logStart);
+ if (timeData != null) {
+ timeData.logStart();
+ }
}
+ /**
+ * Prepare execution.
+ *
+ * @throws EpsilonExecutorException the epsilon executor exception
+ */
private void prepareExecution() throws EpsilonExecutorException {
- if (!(script.isPresent() || code.isPresent())) {
+ if (!( (script == null) || (code == null) )) {
throw new EpsilonExecutorException("No script or code to execute");
}
- timeData.ifPresent(ei -> ei.startStage("prepareExecution"));
+ logger.info("Parsing source/code.");
+ parseSource();
+ startStage("prepareExecution");
logger.info("Adding models to executor");
languageExecutor.addModels(models);
logger.info("Adding parameters to context.");
@@ -444,26 +586,27 @@
logger.info("Adding Native Type Delegates");
languageExecutor.addNativeTypeDelegates(nativeDelegates);
if (profileExecution) {
- timeData.ifPresent(td -> td.endStage("prepareExecution"));
+ endStage("prepareExecution");
}
}
-
- // public Object getResult() {
- // return result;
- //}
+ /**
+ * Parses the source.
+ *
+ * @throws EpsilonExecutorException the epsilon executor exception
+ */
private void parseSource() throws EpsilonExecutorException {
// Think this should be part of constructor, so object is correct at construction
try {
- if (script.isPresent()) {
- languageExecutor.parse(script.get().toFile());
+ if (script != null) {
+ languageExecutor.parse(script.toFile());
}
else {
- languageExecutor.parse(code.get());
+ languageExecutor.parse(code);
}
}
catch (Exception e) {
- String culprit = script.isPresent() ? "script" : "code";
+ String culprit = script != null ? "script" : "code";
logger.error("Failed to parse provided {}", culprit, e);
throw new EpsilonExecutorException("Failed to parse script or code", e);
}