Commit ca7ca039 authored by maquil's avatar maquil

Merge branch 'dev-nico' of https://git.list.lu/nui/TULIP-CPS.git into dev-nico

parents 6cb56c6b 13665e57
......@@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>lu.list.itis.dkd.tui</groupId>
<artifactId>tulip-cps</artifactId>
<version>1.3.1</version>
<version>1.4.0</version>
<name>TULIP Complex Problem Solving</name>
<licenses>
......@@ -59,6 +59,27 @@
<artifactId>jython-standalone</artifactId>
<version>2.7.1b3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.23</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.8</version>
<scope>runtime</scope>
</dependency>
</dependencies>
......
......@@ -27,9 +27,10 @@ import lu.list.itis.dkd.tui.cps.variable.Variable;
import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.LinkedHashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
......@@ -49,7 +50,7 @@ public class Equation {
private Executor scriptExecutor;
private String script;
private static Logger LOGGER = Logger.getLogger(Equation.class.getSimpleName());
private static Logger LOGGER = LoggerFactory.getLogger(Equation.class.getSimpleName());
/**
* Constructor
......@@ -103,7 +104,7 @@ public class Equation {
scriptExecutor.eval(this.script);
} catch (Exception exception) {
LOGGER.log(Level.SEVERE, "Error while evaluating script " + this.script, exception); //$NON-NLS-1$
LOGGER.error("Error while evaluating script {}", this.script, exception); //$NON-NLS-1$
}
dependentVariables = mapping.getDependentVariables();
......@@ -111,18 +112,18 @@ public class Equation {
try {
variable = scriptExecutor.get(variable);
} catch (Exception exception) {
LOGGER.log(Level.SEVERE, "Error while retrieving variable " + variable.getName(), exception); //$NON-NLS-1$
LOGGER.error("Error while retrieving variable {}", variable.getName(), exception); //$NON-NLS-1$
}
}
evaluationErrors = scriptExecutor.getExecutionErrors();
if (evaluationErrors.length() > 0) {
LOGGER.log(Level.WARNING, "Error while evaluating equation :" + evaluationErrors); //$NON-NLS-1$
LOGGER.log(Level.WARNING, "Script =" + this.script); //$NON-NLS-1$
LOGGER.warn("Error while evaluating equation : {}", evaluationErrors); //$NON-NLS-1$
LOGGER.warn("Script = {}", this.script); //$NON-NLS-1$
LinkedHashSet<Variable> independentVariables = mapping.getIndependentVariables();
for (Variable variable : independentVariables) {
LOGGER.log(Level.INFO, variable.getName() + " = " + variable.getValue()); //$NON-NLS-1$
LOGGER.trace("{} = {}", variable.getName(), variable.getValue()); //$NON-NLS-1$
}
}
}
......
......@@ -24,7 +24,7 @@ import lu.list.itis.dkd.dbc.annotation.NonNullByDefault;
import lu.list.itis.dkd.tui.cps.Phenomenon;
import lu.list.itis.dkd.tui.cps.system.executor.Executor;
import lu.list.itis.dkd.tui.cps.utility.Externalization;
import lu.list.itis.dkd.tui.cps.variable.BooleanVariable;
import lu.list.itis.dkd.tui.cps.variable.NumericalVariable;
import lu.list.itis.dkd.tui.cps.variable.Variable;
import com.google.common.base.Strings;
......@@ -33,10 +33,13 @@ import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
......@@ -44,8 +47,6 @@ import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -80,13 +81,12 @@ public class EquationSystemBuilder {
private ConcurrentHashMap<String, Phenomenon> phenomena;
private static final Pattern booleanPattern = Pattern.compile("true|yes|1", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
private static final Logger logger = Logger.getLogger(EquationSystemBuilder.class.getSimpleName());
private static final Logger logger = LoggerFactory.getLogger(EquationSystemBuilder.class.getSimpleName());
/**
* Constructor initializing all fields.
*/
public EquationSystemBuilder(Properties properties) {
this.properties = properties;
this.scriptExecutor = this.instantiateExecutor(properties);
......@@ -119,7 +119,7 @@ public class EquationSystemBuilder {
.getConstructor(Properties.class)
.newInstance(props);
} catch (Exception exception) {
logger.log(Level.SEVERE, "Failed to instantiate executor class " + executorClass + "!", exception); //$NON-NLS-1$ //$NON-NLS-2$
logger.error("Failed to instantiate executor class {}!", executorClass, exception); //$NON-NLS-1$
}
return executor;
......@@ -127,21 +127,122 @@ public class EquationSystemBuilder {
// ---------------------------------------------------------------------------
private Variable instantiateVariable(Properties properties, String name, String unit, double value) {
private Variable instantiateVariable(String name, String type, String unit, String value) {
Variable variable = null;
String variableClass = properties.getProperty(Externalization.VARIABLE_CLASS);
String variableClass = "undefined"; //$NON-NLS-1$
try {
variable = (Variable) Class.forName(variableClass)
.getConstructor(String.class, String.class, double.class)
.newInstance(name, unit, value);
switch (type) {
case Externalization.LOGIC_TYPE:
default:
variableClass = properties.getProperty(Externalization.LOGIC_VARIABLE_CLASS);
variable = (Variable) Class.forName(variableClass)
.getConstructor(String.class, String.class, boolean.class)
.newInstance(name, unit, false);
if (!Strings.isNullOrEmpty(value))
variable.setValue(variable.valueFromString(value));
break;
case Externalization.NUMERIC_TYPE:
variableClass = properties.getProperty(Externalization.NUMERIC_VARIABLE_CLASS);
variable = (Variable) Class.forName(variableClass)
.getConstructor(String.class, String.class, double.class)
.newInstance(name, unit, 0);
if (!Strings.isNullOrEmpty(value))
variable.setValue(variable.valueFromString(value));
}
} catch (Exception exception) {
logger.log(Level.SEVERE, "Failed to instantiate variable class " + variableClass + "!", exception); //$NON-NLS-1$ //$NON-NLS-2$
logger.error("Failed to instantiate variable class {}!", variableClass, exception); //$NON-NLS-1$
}
return variable;
}
// ---------------------------------------------------------------------------
private HashMap<String, String> extractAttributesForVariables(Element element) {
HashMap<String, String> attributes = new HashMap<String, String>();
String attribute;
attribute = element.getChildText(Externalization.NAME_ATTRIBUTE);
if (Strings.isNullOrEmpty(attribute))
attribute = element.getAttributeValue(Externalization.NAME_ATTRIBUTE);
attributes.put(Externalization.NAME_ATTRIBUTE, attribute);
attribute = element.getChildText(Externalization.TYPE_ATTRIBUTE);
if (Strings.isNullOrEmpty(attribute))
attribute = element.getAttributeValue(Externalization.TYPE_ATTRIBUTE);
if (Strings.isNullOrEmpty(attribute))
attribute = Externalization.NUMERIC_TYPE;
attributes.put(Externalization.TYPE_ATTRIBUTE, attribute);
attribute = element.getChildText(Externalization.UNIT_ATTRIBUTE);
if (Strings.isNullOrEmpty(attribute))
attribute = element.getAttributeValue(Externalization.UNIT_ATTRIBUTE);
if (Strings.isNullOrEmpty(attribute))
attribute = Variable.NO_UNIT;
attributes.put(Externalization.UNIT_ATTRIBUTE, attribute);
attribute = element.getChildText(Externalization.INITIAL_ATTRIBUTE);
if (Strings.isNullOrEmpty(attribute))
attribute = element.getAttributeValue(Externalization.INITIAL_ATTRIBUTE);
attributes.put(Externalization.INITIAL_ATTRIBUTE, attribute);
attribute = element.getChildText(Externalization.MINIMUM_ATTRIBUTE);
if (Strings.isNullOrEmpty(attribute))
attribute = element.getAttributeValue(Externalization.MINIMUM_ATTRIBUTE);
attributes.put(Externalization.MINIMUM_ATTRIBUTE, attribute);
attribute = element.getChildText(Externalization.MAXIMUM_ATTRIBUTE);
if (Strings.isNullOrEmpty(attribute))
attribute = element.getAttributeValue(Externalization.MAXIMUM_ATTRIBUTE);
attributes.put(Externalization.MAXIMUM_ATTRIBUTE, attribute);
attribute = element.getChildText(Externalization.EQUATE_ATTRIBUTE);
if (Strings.isNullOrEmpty(attribute))
attribute = element.getAttributeValue(Externalization.EQUATE_ATTRIBUTE);
attributes.put(Externalization.EQUATE_ATTRIBUTE, attribute);
return attributes;
}
// ---------------------------------------------------------------------------
private Variable createVariable(HashMap<String, String> attributes) {
Variable variable;
// String scale = xmlElement.getAttributeValue(Externalization.SCALE_ATTRIBUTE);
// String decimals = xmlElement.getAttributeValue(Externalization.DECIMALS_ATTRIBUTE);
variable = this.instantiateVariable(attributes.get(Externalization.NAME_ATTRIBUTE),
attributes.get(Externalization.TYPE_ATTRIBUTE),
attributes.get(Externalization.UNIT_ATTRIBUTE),
attributes.get(Externalization.INITIAL_ATTRIBUTE));
if (Externalization.NUMERIC_TYPE.equals(attributes.get(Externalization.TYPE_ATTRIBUTE))) {
NumericalVariable numericVariable = (NumericalVariable) variable;
if (!Strings.isNullOrEmpty(attributes.get(Externalization.MINIMUM_ATTRIBUTE)))
numericVariable.setMinValue((Double) numericVariable.valueFromString(attributes.get(Externalization.MINIMUM_ATTRIBUTE)));
if (!Strings.isNullOrEmpty(attributes.get(Externalization.MAXIMUM_ATTRIBUTE)))
numericVariable.setMinValue((Double) numericVariable.valueFromString(attributes.get(Externalization.MAXIMUM_ATTRIBUTE)));
if (!Strings.isNullOrEmpty(attributes.get(Externalization.EQUATE_ATTRIBUTE)))
numericVariable.setEquate(attributes.get(Externalization.EQUATE_ATTRIBUTE));
variable = numericVariable;
}
return variable;
}
/**
* Checks whether the specified equation creates a dependency cycle. Dependency cycles occur
* when two equations are mutually dependent, i.e. equation 1 has an input parameter being the
......@@ -153,9 +254,9 @@ public class EquationSystemBuilder {
* otherwise.
*/
private boolean hasDependencyCycle(Element equation) {
String equationName = equation.getChildText(Externalization.NAME_ELEMENT);
String equationName = equation.getChildText(Externalization.NAME_ATTRIBUTE);
if (Strings.isNullOrEmpty(equationName))
equationName = equation.getAttributeValue(Externalization.NAME_ELEMENT);
equationName = equation.getAttributeValue(Externalization.NAME_ATTRIBUTE);
boolean dependencyCycle = false;
for (String parameter : equationParameters.get(equationName)) {
......@@ -192,24 +293,17 @@ public class EquationSystemBuilder {
}
List<Element> outputElements = outputs.get(0).getChildren(Externalization.OUTPUT_ELEMENT);
HashMap<String, String> outputAttributes;
for (Element output : outputElements) {
String name = output.getChildText(Externalization.NAME_ELEMENT);
if (Strings.isNullOrEmpty(name))
name = output.getAttributeValue(Externalization.NAME_ELEMENT);
String unit = output.getChildText(Externalization.UNIT_ELEMENT);
if (Strings.isNullOrEmpty(unit))
unit = output.getAttributeValue(Externalization.UNIT_ELEMENT);
if (Strings.isNullOrEmpty(unit))
unit = Variable.NO_UNIT;
String initialValue = output.getChildText(Externalization.INITIAL_ATTRIBUTE);
if (Strings.isNullOrEmpty(initialValue))
initialValue = output.getAttributeValue(Externalization.INITIAL_ATTRIBUTE);
if (Strings.isNullOrEmpty(name)) {
outputAttributes = this.extractAttributesForVariables(output);
String identifier = outputAttributes.get(Externalization.NAME_ATTRIBUTE);
if (Strings.isNullOrEmpty(identifier)) {
throw new EquationSystemException("For each output the name needs to be defined!"); //$NON-NLS-1$
}
addVariable(outputVariables, name, initialValue, unit, null);
outputVariables.put(identifier, this.createVariable(outputAttributes));
}
}
......@@ -230,7 +324,7 @@ public class EquationSystemBuilder {
resolveDependencies();
if (!this.allOutputsAssigned()) {
logger.warning("Not all defined outputs are assigned to an equation!"); //$NON-NLS-1$
logger.warn("Not all defined outputs are assigned to an equation!"); //$NON-NLS-1$
// throw new EquationSystemException (message);
}
}
......@@ -240,9 +334,9 @@ public class EquationSystemBuilder {
List<Element> importDeclarations;
List<Import> imports;
equationName = equation.getAttributeValue(Externalization.NAME_ELEMENT);
equationName = equation.getAttributeValue(Externalization.NAME_ATTRIBUTE);
if (Strings.isNullOrEmpty(equationName))
equationName = equation.getAttributeValue(Externalization.NAME_ELEMENT);
equationName = equation.getAttributeValue(Externalization.NAME_ATTRIBUTE);
Element importsTag = equation.getChild(Externalization.IMPORTS_ELEMENT);
if (importsTag != null) {
......@@ -259,40 +353,30 @@ public class EquationSystemBuilder {
}
private void addParameterDependencies(Element equation) {
String equationName = equation.getChildText(Externalization.NAME_ELEMENT);
String equationName = equation.getChildText(Externalization.NAME_ATTRIBUTE);
if (Strings.isNullOrEmpty(equationName))
equationName = equation.getAttributeValue(Externalization.NAME_ELEMENT);
equationName = equation.getAttributeValue(Externalization.NAME_ATTRIBUTE);
List<Element> parameters = equation.getChild(Externalization.PARAMETERS_ELEMENT).getChildren(Externalization.PARAMETER_ELEMENT);
List<String> _equationParameters = new ArrayList<String>();
HashMap<String, String> parameterAttributes;
for (Element parameter : parameters) {
String parameterName = parameter.getChildText(Externalization.NAME_ELEMENT);
if (Strings.isNullOrEmpty(parameterName))
parameterName = parameter.getAttributeValue(Externalization.NAME_ELEMENT);
parameterAttributes = this.extractAttributesForVariables(parameter);
String identifier = parameterAttributes.get(Externalization.NAME_ATTRIBUTE);
String parameterUnit = parameter.getChildText(Externalization.UNIT_ELEMENT);
if (Strings.isNullOrEmpty(parameterUnit))
parameterUnit = parameter.getAttributeValue(Externalization.UNIT_ELEMENT);
if (Strings.isNullOrEmpty(parameterUnit))
parameterUnit = Variable.NO_UNIT;
_equationParameters.add(identifier);
String initialValue = parameter.getChildText(Externalization.INITIAL_ATTRIBUTE);
if (Strings.isNullOrEmpty(initialValue))
initialValue = parameter.getAttributeValue(Externalization.INITIAL_ATTRIBUTE);
_equationParameters.add(parameterName);
if (parameterDependencies.containsKey(parameterName)) {
parameterDependencies.get(parameterName).add(equationName);
if (parameterDependencies.containsKey(identifier)) {
parameterDependencies.get(identifier).add(equationName);
} else {
List<String> dependencies = new ArrayList<String>();
dependencies.add(equationName);
parameterDependencies.put(parameterName, dependencies);
parameterDependencies.put(identifier, dependencies);
}
if (!dependentVariables.containsKey(parameterName) && !independentVariables.containsKey(parameterName)) {
addVariable(independentVariables, parameterName, initialValue, parameterUnit, null);
if (!dependentVariables.containsKey(identifier) && !independentVariables.containsKey(identifier)) {
independentVariables.put(identifier, this.createVariable(parameterAttributes));
}
}
equationParameters.put(equationName, _equationParameters);
......@@ -315,77 +399,68 @@ public class EquationSystemBuilder {
* @param equate
* The (optional) equate
*/
private void addVariable(Map<String, Variable> map, String name, Object initial, String unit, String equate) {
Variable newVariable = null;
if (unit != null) {
switch (unit) {
case "boolean": //$NON-NLS-1$
newVariable = new BooleanVariable(name, false);
newVariable.setEquate(equate);
break;
default:
newVariable = instantiateVariable(properties, name, unit, 0);
newVariable.setEquate(equate);
break;
}
} else {
newVariable = instantiateVariable(properties, name, unit, 0);
newVariable.setEquate(equate);
map.put(name, newVariable);
}
if (initial != null) {
newVariable.setValue(initial);
}
map.put(name, newVariable);
}
// private void addVariable(Map<String, Variable> map, String name, Object initial, String unit,
// String equate) {
// Variable newVariable = null;
// if (unit != null) {
// switch (unit) {
// case "boolean": //$NON-NLS-1$
// newVariable = new BooleanVariable(name, false);
// newVariable.setEquate(equate);
// break;
// default:
// newVariable = instantiateVariable(properties, name, unit, 0);
// newVariable.setEquate(equate);
// break;
// }
// } else {
// newVariable = instantiateVariable(properties, name, unit, 0);
// newVariable.setEquate(equate);
// map.put(name, newVariable);
// }
// if (initial != null) {
// newVariable.setValue(initial);
// }
// map.put(name, newVariable);
// }
private void addResultDependencies(Element equation) {
String equationName = equation.getChildText(Externalization.NAME_ELEMENT);
String equationName = equation.getChildText(Externalization.NAME_ATTRIBUTE);
if (Strings.isNullOrEmpty(equationName))
equationName = equation.getAttributeValue(Externalization.NAME_ELEMENT);
equationName = equation.getAttributeValue(Externalization.NAME_ATTRIBUTE);
List<Element> results = equation.getChild(Externalization.RESULTS_ELEMENT).getChildren(Externalization.RESULT_ELEMENT);
List<String> _equationResults = new ArrayList<String>();
HashMap<String, String> resultAttributes;
for (Element result : results) {
String resultName = result.getChildText(Externalization.NAME_ELEMENT);
if (Strings.isNullOrEmpty(resultName))
resultName = result.getAttributeValue(Externalization.NAME_ELEMENT);
String resultUnit = result.getChildText(Externalization.UNIT_ELEMENT);
if (Strings.isNullOrEmpty(resultUnit))
resultUnit = result.getAttributeValue(Externalization.UNIT_ELEMENT);
String equate = result.getChildText(Externalization.EQUATE_ATTRIBUTE);
if (Strings.isNullOrEmpty(equate))
equate = result.getAttributeValue(Externalization.EQUATE_ATTRIBUTE);
resultAttributes = this.extractAttributesForVariables(result);
String identifier = resultAttributes.get(Externalization.NAME_ATTRIBUTE);
_equationResults.add(resultName);
_equationResults.add(identifier);
if (resultDependencies.containsKey(resultName)) {
resultDependencies.get(resultName).add(equationName);
if (resultDependencies.containsKey(identifier)) {
resultDependencies.get(identifier).add(equationName);
} else {
List<String> dependencies = new ArrayList<String>();
dependencies.add(equationName);
resultDependencies.put(resultName, dependencies);
resultDependencies.put(identifier, dependencies);
}
if (!dependentVariables.containsKey(resultName)) {
if (!dependentVariables.containsKey(identifier)) {
Variable output = null;
if (outputVariables.containsKey(resultName)) {
output = outputVariables.get(resultName);
output.setEquate(equate);
dependentVariables.put(resultName, output);
if (outputVariables.containsKey(identifier)) {
output = outputVariables.get(identifier);
output.setEquate(resultAttributes.get(Externalization.EQUATE_ATTRIBUTE));
dependentVariables.put(identifier, output);
} else {
addVariable(dependentVariables, resultName, null, resultUnit, equate);
dependentVariables.put(identifier, this.createVariable(resultAttributes));
}
}
if (this.independentVariables.containsKey(resultName)) {
this.independentVariables.remove(resultName);
if (this.independentVariables.containsKey(identifier)) {
this.independentVariables.remove(identifier);
}
}
equationResults.put(equationName, _equationResults);
}
......@@ -403,18 +478,18 @@ public class EquationSystemBuilder {
if (!periodic) {
if (hasDependencyCycle(equation)) {
String equationName = equation.getChildText(Externalization.NAME_ELEMENT);
String equationName = equation.getChildText(Externalization.NAME_ATTRIBUTE);
if (Strings.isNullOrEmpty(equationName))
equationName = equation.getAttributeValue(Externalization.NAME_ELEMENT);
equationName = equation.getAttributeValue(Externalization.NAME_ATTRIBUTE);
throw new EquationSystemException("Dependency Cycle detected for equation " + equationName); //$NON-NLS-1$
}
}
}
private void createEquations(Element equation) throws EquationSystemException {
String equationName = equation.getChildText(Externalization.NAME_ELEMENT);
String equationName = equation.getChildText(Externalization.NAME_ATTRIBUTE);
if (Strings.isNullOrEmpty(equationName))
equationName = equation.getAttributeValue(Externalization.NAME_ELEMENT);
equationName = equation.getAttributeValue(Externalization.NAME_ATTRIBUTE);
LinkedHashSet<Variable> equationInputs = new LinkedHashSet<>();
LinkedHashSet<Variable> equationOutputs = new LinkedHashSet<>();
......@@ -437,7 +512,7 @@ public class EquationSystemBuilder {
lockToNesting = false;
} else {
StringBuilder message = new StringBuilder("Undefined parameter " + identifier + " encountered! Check equation file!"); //$NON-NLS-1$ //$NON-NLS-2$
logger.severe(message.toString());
logger.error(message.toString());
throw new EquationSystemException(message.toString());
}
}
......@@ -447,7 +522,7 @@ public class EquationSystemBuilder {
equationOutputs.add(dependentVariables.get(identifier));
} else {
StringBuilder message = new StringBuilder("Undefined result " + identifier + " encountered! Check equation file!"); //$NON-NLS-1$ //$NON-NLS-2$
logger.severe(message.toString());
logger.error(message.toString());
throw new EquationSystemException(message.toString());
}
}
......@@ -505,7 +580,7 @@ public class EquationSystemBuilder {
StringBuilder message = new StringBuilder("Redefinition of equation for variable ") //$NON-NLS-1$
.append(identifier).append("! Offending equation is ").append(equationName).append("!"); //$NON-NLS-1$ //$NON-NLS-2$
logger.warning(message.toString());
logger.warn(message.toString());
throw new EquationSystemException(message.toString());
}
this.rootEquations.put(identifier, equation);
......@@ -535,7 +610,7 @@ public class EquationSystemBuilder {
try {
equationDocument = builder.build(equationFile);
} catch (IOException | JDOMException e) {
logger.log(Level.SEVERE, "Error occured while trying to process the object bootstrapping file!", e); //$NON-NLS-1$
logger.error("Error occured while trying to process the object bootstrapping file!", e); //$NON-NLS-1$
throw new EquationSystemException("The document containing the bootstrapping information could not be processed!"); //$NON-NLS-1$
}
......
......@@ -3,15 +3,17 @@ package lu.list.itis.dkd.tui.cps.system.executor;
import lu.list.itis.dkd.tui.cps.system.Import;
import lu.list.itis.dkd.tui.cps.variable.Variable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.CharArrayWriter;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;
abstract public class Executor {
protected CharArrayWriter executionErrors;
private static Logger LOGGER = Logger.getLogger(Executor.class.getSimpleName());
private static Logger LOGGER = LoggerFactory.getLogger(Executor.class.getSimpleName());
public Executor() {
executionErrors = new CharArrayWriter();
......@@ -32,7 +34,7 @@ abstract public class Executor {
@SuppressWarnings("unused")
public void resolve(List<Import> imports) {
LOGGER.warning("Imports not supported by Executor!"); //$NON-NLS-1$
LOGGER.warn("Imports not supported by Executor!"); //$NON-NLS-1$
}
abstract public void set(Variable variable);
......
......@@ -2,8 +2,10 @@ package lu.list.itis.dkd.tui.cps.system.executor;
import lu.list.itis.dkd.tui.cps.variable.Variable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Properties;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
......@@ -13,7 +15,7 @@ public class JavascriptExecutor extends Executor {
private ScriptEngine engine;
private Object result;
private static Logger LOGGER = Logger.getLogger(JavascriptExecutor.class.getSimpleName());
private static Logger LOGGER = LoggerFactory.getLogger(JavascriptExecutor.class.getSimpleName());
public JavascriptExecutor() {
engine = new ScriptEngineManager().getEngineByName("js"); //$NON-NLS-1$
......@@ -28,14 +30,24 @@ public class JavascriptExecutor extends Executor {
@Override
public void set(Variable variable) {
engine.put(variable.getName(), variable.getValue());
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Parameter {} = {}", variable.getName(), variable.getValue()); //$NON-NLS-1$
}
}
@Override
public boolean eval(String script) {
try {
long elapsed = System.currentTimeMillis();
result = engine.eval(script);
elapsed = System.currentTimeMillis() - elapsed;
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Eval => {} | took {} ms!", script, elapsed); //$NON-NLS-1$
}
} catch (ScriptException exception) {
this.executionErrors.append(exception.toString());
LOGGER.warn("Failed to eval script => {}", script); //$NON-NLS-1$
LOGGER.warn("Caused by...", exception); //$NON-NLS-1$
return false;
}
return true;
......@@ -44,6 +56,9 @@ public class JavascriptExecutor extends Executor {
@Override
public Variable get(Variable variable) {
variable.setValue(result);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Result {} = {}", variable.getName(), result); //$NON-NLS-1$
}
return variable;
}
}
......@@ -2,6 +2,9 @@ package lu.list.itis.dkd.tui.cps.system.executor;
import lu.list.itis.dkd.tui.cps.variable.Variable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.Properties;
......@@ -16,14 +19,25 @@ public class OctaveExecutor extends Executor {
private static final String SCRIPT_ENGINE_EXECUTABLE = "octave.executable"; //$NON-NLS-1$
private static final String SCRIPT_ENGINE_WORKING_DIR = "octave.workingDir"; //$NON-NLS-1$