Commit cb5e1e6b authored by Nico Mack's avatar Nico Mack

Added additional Trace logging. Improved handling of notification

suspension. Moved variable value class variable into Variable class
parent 53ed2271
...@@ -48,7 +48,7 @@ public class InputEvent extends EventObject { ...@@ -48,7 +48,7 @@ public class InputEvent extends EventObject {
* @param tick * @param tick
* The turn index the event was triggered at. * The turn index the event was triggered at.
*/ */
public InputEvent(Variable source, long tick) { public InputEvent(Variable<?> source, long tick) {
super(source); super(source);
this.tick = tick; this.tick = tick;
} }
...@@ -59,7 +59,7 @@ public class InputEvent extends EventObject { ...@@ -59,7 +59,7 @@ public class InputEvent extends EventObject {
* @param source * @param source
* The source of the event. * The source of the event.
*/ */
public InputEvent(Variable source) { public InputEvent(Variable<?> source) {
super(source); super(source);
this.tick = -1; this.tick = -1;
} }
...@@ -72,4 +72,9 @@ public class InputEvent extends EventObject { ...@@ -72,4 +72,9 @@ public class InputEvent extends EventObject {
public long getTick() { public long getTick() {
return tick; return tick;
} }
@Override
public Variable<?> getSource() {
return (Variable<?>) this.source;
}
} }
\ No newline at end of file
...@@ -46,10 +46,10 @@ import javax.script.ScriptEngine; ...@@ -46,10 +46,10 @@ import javax.script.ScriptEngine;
*/ */
@NonNullByDefault @NonNullByDefault
public class Equation { public class Equation {
private String name;
private Mapping mapping; private Mapping mapping;
private Mapping lockedMapping; private Mapping lockedMapping;
@Nullable @Nullable
// private ScriptEngine scriptEngine;
private Executor scriptExecutor; private Executor scriptExecutor;
private String script; private String script;
...@@ -65,7 +65,8 @@ public class Equation { ...@@ -65,7 +65,8 @@ public class Equation {
* the variables in the script must coincide with the names of the variables in the * the variables in the script must coincide with the names of the variables in the
* mapping. * mapping.
*/ */
public Equation(Mapping mapping, String script) { public Equation(String name, Mapping mapping, String script) {
this.name = name;
this.mapping = mapping; this.mapping = mapping;
this.lockedMapping = mapping; this.lockedMapping = mapping;
this.script = script; this.script = script;
...@@ -85,14 +86,6 @@ public class Equation { ...@@ -85,14 +86,6 @@ public class Equation {
* mapping. * mapping.
*/ */
public void evaluate() { public void evaluate() {
// Preconditions.checkState(scriptEngine != null);
// try {
// mapping.getDependentVariable().setValue(scriptEngine.eval(replaceVariables()));
// } catch (ScriptException e) {
// Logger.getLogger(Equation.class.getSimpleName()).log(Level.SEVERE, "Exception while
// executing script!", e); //$NON-NLS-1$
// }
LinkedHashSet<Variable<?>> dependentVariables; LinkedHashSet<Variable<?>> dependentVariables;
String evaluationErrors; String evaluationErrors;
...@@ -117,7 +110,7 @@ public class Equation { ...@@ -117,7 +110,7 @@ public class Equation {
consolidatedListeners = new HashMap<>(); consolidatedListeners = new HashMap<>();
for (Variable<?> variable : dependentVariables) { for (Variable<?> variable : dependentVariables) {
variable.setConsolidated(consolidated); variable.suspendListenerNotification(consolidated);
if (consolidated) if (consolidated)
consolidatedListeners = variable.consolidateListeners(consolidatedListeners); consolidatedListeners = variable.consolidateListeners(consolidatedListeners);
try { try {
...@@ -125,7 +118,7 @@ public class Equation { ...@@ -125,7 +118,7 @@ public class Equation {
} catch (Exception exception) { } catch (Exception exception) {
LOGGER.error("Error while retrieving variable {}", variable.getName(), exception); //$NON-NLS-1$ LOGGER.error("Error while retrieving variable {}", variable.getName(), exception); //$NON-NLS-1$
} }
variable.setConsolidated(false); variable.suspendListenerNotification(false);
} }
if (consolidated && consolidatedListeners != null) { if (consolidated && consolidatedListeners != null) {
...@@ -147,35 +140,6 @@ public class Equation { ...@@ -147,35 +140,6 @@ public class Equation {
} }
} }
/**
* Method used to replace variables with their actual values in the formula.
*
* @return The formula with variables replaced by their values, ready for evaluation.
*/
// private String replaceVariables() {
// String result = new String(script);
//
// for (Variable variable : lockedMapping.getIndependentVariables()) {
// result = result.replaceAll(variable.getName(), variable.getValue().toString());
// }
//
// for (Variable variable : lockedMapping.getDependentVariables()) {
// result = result.replaceAll(variable.getName(), variable.getValue().toString());
// }
//
// assert result != null;
// return result;
// }
/**
* Method for setting the script engine to use.
*
* @param engine
* The {@link ScriptEngine} instance to use for evaluating the scripted formulas.
*/
// public void setScriptEngine(ScriptEngine engine) {
// scriptEngine = engine;
// }
/** /**
* Method for setting the script engine to use. * Method for setting the script engine to use.
* *
...@@ -207,17 +171,8 @@ public class Equation { ...@@ -207,17 +171,8 @@ public class Equation {
lockedMapping = mapping.lock(); lockedMapping = mapping.lock();
} }
// /** @Override
// * Method used to update the mapping with a variable, that is, to update the reference to a public String toString() {
// * variable so that the mapping will correctly map all changes of the variable. <br> return this.name;
// * <br> }
// * <u>Note:</u> This method depends on the equality of variables being by name and unit.
// *
// * @param variable
// * The variable to update the reference for.
// * @see Variable#equals(Object)
// */
// public void updateMapping(Variable variable) {
// mapping.updateVariableReference(variable);
// }
} }
\ No newline at end of file
...@@ -635,7 +635,7 @@ public class EquationSystemBuilder { ...@@ -635,7 +635,7 @@ public class EquationSystemBuilder {
Mapping variableMapping = new Mapping(equationInputs, equationOutputs); Mapping variableMapping = new Mapping(equationInputs, equationOutputs);
final LinearEquationSystem equationSystem = new LinearEquationSystem(lockToNesting); final LinearEquationSystem equationSystem = new LinearEquationSystem(lockToNesting);
Equation newEquation = new Equation(variableMapping, script); Equation newEquation = new Equation(equationName, variableMapping, script);
newEquation.setExecutor(executors.get(executor)); newEquation.setExecutor(executors.get(executor));
equationSystem.addEquation(newEquation); equationSystem.addEquation(newEquation);
......
...@@ -22,10 +22,12 @@ package lu.list.itis.dkd.tui.cps.system; ...@@ -22,10 +22,12 @@ package lu.list.itis.dkd.tui.cps.system;
import lu.list.itis.dkd.dbc.annotation.NonNullByDefault; import lu.list.itis.dkd.dbc.annotation.NonNullByDefault;
import lu.list.itis.dkd.tui.cps.InputEvent; import lu.list.itis.dkd.tui.cps.InputEvent;
import lu.list.itis.dkd.tui.cps.variable.Variable;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
/** /**
...@@ -42,6 +44,8 @@ public class LinearEquationSystem extends System { ...@@ -42,6 +44,8 @@ public class LinearEquationSystem extends System {
private ConcurrentHashMap<Mapping, Equation> equations; private ConcurrentHashMap<Mapping, Equation> equations;
// private ScriptEngine scriptEngine; // private ScriptEngine scriptEngine;
private static final Logger LOGGER = LoggerFactory.getLogger(LinearEquationSystem.class.getSimpleName());
/** /**
* Constructor issuing a super call and initialising the map of equations. * Constructor issuing a super call and initialising the map of equations.
* *
...@@ -76,6 +80,9 @@ public class LinearEquationSystem extends System { ...@@ -76,6 +80,9 @@ public class LinearEquationSystem extends System {
@Override @Override
public synchronized void clockTicked(long tick) { public synchronized void clockTicked(long tick) {
super.clockTicked(tick); super.clockTicked(tick);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Clock tick at {}...", tick); //$NON-NLS-1$
}
for (Equation equation : equations.values()) { for (Equation equation : equations.values()) {
equation.evaluate(); equation.evaluate();
...@@ -92,7 +99,10 @@ public class LinearEquationSystem extends System { ...@@ -92,7 +99,10 @@ public class LinearEquationSystem extends System {
super.inputChanged(input); super.inputChanged(input);
for (Equation equation : equations.values()) { for (Equation equation : equations.values()) {
if (equation.catersTo((Variable<?>) input.getSource())) { if (equation.catersTo(input.getSource())) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Equation {} triggered by variable {}...", equation.toString(), input.getSource().getName()); //$NON-NLS-1$
}
equation.evaluate(); equation.evaluate();
} }
} }
......
...@@ -63,7 +63,7 @@ public class PythonExecutor extends Executor { ...@@ -63,7 +63,7 @@ public class PythonExecutor extends Executor {
} }
engine.set(variable.getName(), array); engine.set(variable.getName(), array);
if (LOGGER.isTraceEnabled()) { if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Parameter {} = {}", variable.getName(), list.toString()); //$NON-NLS-1$ LOGGER.trace("Parameter -> {} = {}", variable.getName(), list.toString()); //$NON-NLS-1$
} }
} }
} else { } else {
...@@ -71,7 +71,7 @@ public class PythonExecutor extends Executor { ...@@ -71,7 +71,7 @@ public class PythonExecutor extends Executor {
Double numericValue = Double.valueOf(variable.getValue().toString()); Double numericValue = Double.valueOf(variable.getValue().toString());
engine.set(variable.getName(), new PyFloat(numericValue)); engine.set(variable.getName(), new PyFloat(numericValue));
if (LOGGER.isTraceEnabled()) { if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Parameter {} = {}", variable.getName(), variable.getValue()); //$NON-NLS-1$ LOGGER.trace("Parameter -> {} = {}", variable.getName(), variable.getValue()); //$NON-NLS-1$
} }
} }
} }
...@@ -82,7 +82,7 @@ public class PythonExecutor extends Executor { ...@@ -82,7 +82,7 @@ public class PythonExecutor extends Executor {
engine.exec(script); engine.exec(script);
elapsed = System.currentTimeMillis() - elapsed; elapsed = System.currentTimeMillis() - elapsed;
if (LOGGER.isTraceEnabled()) { if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Eval => {} | took {} ms!", script, elapsed); //$NON-NLS-1$ LOGGER.trace("Eval took {} ms | {}", elapsed, script); //$NON-NLS-1$
} }
return true; return true;
} }
...@@ -97,13 +97,13 @@ public class PythonExecutor extends Executor { ...@@ -97,13 +97,13 @@ public class PythonExecutor extends Executor {
PyTuple tuple = (PyTuple) engine.get(tupleMatcher.group(1)); PyTuple tuple = (PyTuple) engine.get(tupleMatcher.group(1));
result = tuple.__getattr__(tupleMatcher.group(2)); result = tuple.__getattr__(tupleMatcher.group(2));
if (LOGGER.isTraceEnabled()) { if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Result {} = {}.{} = {}", variable.getName(), tupleMatcher.group(1), tupleMatcher.group(2), result); //$NON-NLS-1$ LOGGER.trace("Result <- {} = {}.{} = {}", variable.getName(), tupleMatcher.group(1), tupleMatcher.group(2), result); //$NON-NLS-1$
} }
} }
} else { } else {
result = engine.get(variable.getName()); result = engine.get(variable.getName());
if (LOGGER.isTraceEnabled()) { if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Result {} = {}", variable.getName(), result); //$NON-NLS-1$ LOGGER.trace("Result <- {} = {}", variable.getName(), result); //$NON-NLS-1$
} }
} }
......
...@@ -293,7 +293,7 @@ public class SqlExecutor extends Executor { ...@@ -293,7 +293,7 @@ public class SqlExecutor extends Executor {
public void set(Variable<?> variable) { public void set(Variable<?> variable) {
parameters.put(variable.getName(), variable.getValue()); parameters.put(variable.getName(), variable.getValue());
if (LOGGER.isTraceEnabled()) { if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Parameter {} = {}", variable.getName(), variable.getValue()); //$NON-NLS-1$ LOGGER.trace("Parameter -> {} = {}", variable.getName(), variable.getValue()); //$NON-NLS-1$
} }
} }
...@@ -302,6 +302,7 @@ public class SqlExecutor extends Executor { ...@@ -302,6 +302,7 @@ public class SqlExecutor extends Executor {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public boolean eval(String script) { public boolean eval(String script) {
long elapsed = System.currentTimeMillis();
Connection connection = this.getConnectionFromPool(); Connection connection = this.getConnectionFromPool();
List<String> variables = new ArrayList<String>(); List<String> variables = new ArrayList<String>();
String queryString = this.interpolate(script, variables, false); String queryString = this.interpolate(script, variables, false);
...@@ -318,6 +319,11 @@ public class SqlExecutor extends Executor { ...@@ -318,6 +319,11 @@ public class SqlExecutor extends Executor {
columnNames = this.getColumnNames(rows); columnNames = this.getColumnNames(rows);
columnTypes = this.getColumnTypes(rows); columnTypes = this.getColumnTypes(rows);
columnValues = this.convertResults(rows); columnValues = this.convertResults(rows);
elapsed = System.currentTimeMillis() - elapsed;
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Eval took {} ms | {}", elapsed, script); //$NON-NLS-1$
}
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.error("Failed to execute query!", e); //$NON-NLS-1$ LOGGER.error("Failed to execute query!", e); //$NON-NLS-1$
success = false; success = false;
...@@ -366,10 +372,13 @@ public class SqlExecutor extends Executor { ...@@ -366,10 +372,13 @@ public class SqlExecutor extends Executor {
if (variable instanceof VectorVariable) { if (variable instanceof VectorVariable) {
VectorVariable<?> vector = (VectorVariable<?>) variable; VectorVariable<?> vector = (VectorVariable<?>) variable;
// Suspend notification of input change listeners while populating vector
variable.suspendListenerNotification(true);
for (Object[] values : columnValues) { for (Object[] values : columnValues) {
Object value = values[index]; Object value = values[index];
vector.add(value); vector.add(value);
} }
variable.suspendListenerNotification(false);
vector.notifyInputChangeListeners(); vector.notifyInputChangeListeners();
} else { } else {
LOGGER.warn("Query returned multiple rows but specified variable {} is scalar!", variable.getName()); //$NON-NLS-1$ LOGGER.warn("Query returned multiple rows but specified variable {} is scalar!", variable.getName()); //$NON-NLS-1$
...@@ -380,6 +389,9 @@ public class SqlExecutor extends Executor { ...@@ -380,6 +389,9 @@ public class SqlExecutor extends Executor {
variable.setValueFromObject(values[index]); variable.setValueFromObject(values[index]);
} }
} }
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Result <- {} = {}", variable.getName(), variable.getValue()); //$NON-NLS-1$
}
return variable; return variable;
} }
......
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
*/ */
package lu.list.itis.dkd.tui.cps.variable; package lu.list.itis.dkd.tui.cps.variable;
import lu.list.itis.dkd.tui.cps.InputEvent;
import lu.list.itis.dkd.tui.cps.utility.Externalization; import lu.list.itis.dkd.tui.cps.utility.Externalization;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -35,8 +34,6 @@ import java.util.Vector; ...@@ -35,8 +34,6 @@ import java.util.Vector;
*/ */
public class BooleanVariable extends Variable<Boolean> { public class BooleanVariable extends Variable<Boolean> {
protected boolean value;
/** /**
* @param name * @param name
* @param value * @param value
...@@ -49,19 +46,6 @@ public class BooleanVariable extends Variable<Boolean> { ...@@ -49,19 +46,6 @@ public class BooleanVariable extends Variable<Boolean> {
this.value = value; this.value = value;
} }
/** {@inheritDoc} */
@Override
public Boolean getValue() {
return value;
}
/** {@inheritDoc} */
@Override
public void setValue(Boolean newValue) {
this.value = newValue;
this.notifyInputChangeListeners(new InputEvent(this));
}
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public void setValueFromObject(Object object) { public void setValueFromObject(Object object) {
......
...@@ -22,7 +22,6 @@ package lu.list.itis.dkd.tui.cps.variable; ...@@ -22,7 +22,6 @@ package lu.list.itis.dkd.tui.cps.variable;
import lu.list.itis.dkd.dbc.annotation.NonNullByDefault; import lu.list.itis.dkd.dbc.annotation.NonNullByDefault;
import lu.list.itis.dkd.dbc.annotation.Nullable; import lu.list.itis.dkd.dbc.annotation.Nullable;
import lu.list.itis.dkd.tui.cps.InputEvent;
import lu.list.itis.dkd.tui.cps.utility.Externalization; import lu.list.itis.dkd.tui.cps.utility.Externalization;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -41,7 +40,6 @@ import java.util.Vector; ...@@ -41,7 +40,6 @@ import java.util.Vector;
*/ */
@NonNullByDefault @NonNullByDefault
public class NumericalVariable extends Variable<Double> { public class NumericalVariable extends Variable<Double> {
protected double value;
protected double minValue; protected double minValue;
protected double maxValue; protected double maxValue;
protected double scale; protected double scale;
...@@ -151,13 +149,11 @@ public class NumericalVariable extends Variable<Double> { ...@@ -151,13 +149,11 @@ public class NumericalVariable extends Variable<Double> {
// Double newValue; // Double newValue;
// newValue = (Double) this.valueFromString(value.toString()); // newValue = (Double) this.valueFromString(value.toString());
if (newValue < this.minValue) if (newValue < this.minValue)
this.value = this.minValue; newValue = this.minValue;
else if (newValue > this.maxValue) else if (newValue > this.maxValue)
this.value = this.maxValue; newValue = this.maxValue;
else
this.value = newValue;
this.notifyInputChangeListeners(new InputEvent(this)); super.setValue(newValue);
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
......
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
package lu.list.itis.dkd.tui.cps.variable; package lu.list.itis.dkd.tui.cps.variable;
import lu.list.itis.dkd.dbc.annotation.Nullable; import lu.list.itis.dkd.dbc.annotation.Nullable;
import lu.list.itis.dkd.tui.cps.InputEvent;
import lu.list.itis.dkd.tui.cps.utility.Externalization; import lu.list.itis.dkd.tui.cps.utility.Externalization;
import lu.list.itis.dkd.tui.utility.Point; import lu.list.itis.dkd.tui.utility.Point;
...@@ -32,8 +31,6 @@ import java.util.regex.Pattern; ...@@ -32,8 +31,6 @@ import java.util.regex.Pattern;
*/ */
public class SpatialVariable extends Variable<Point> { public class SpatialVariable extends Variable<Point> {
private Point point;
private static final Logger LOGGER = LoggerFactory.getLogger(SpatialVariable.class.getSimpleName()); private static final Logger LOGGER = LoggerFactory.getLogger(SpatialVariable.class.getSimpleName());
private static final Pattern SPATIAL_STRING_PATTERN = Pattern.compile("([0-9]+\\.?[0-9]*)\\s*,\\s*([0-9]+\\.?[0-9]*)(\\s*,\\s*([0-9]+\\.?[0-9]*))?", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$ private static final Pattern SPATIAL_STRING_PATTERN = Pattern.compile("([0-9]+\\.?[0-9]*)\\s*,\\s*([0-9]+\\.?[0-9]*)(\\s*,\\s*([0-9]+\\.?[0-9]*))?", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
...@@ -45,7 +42,7 @@ public class SpatialVariable extends Variable<Point> { ...@@ -45,7 +42,7 @@ public class SpatialVariable extends Variable<Point> {
*/ */
public SpatialVariable(String name, String unit, Point value) { public SpatialVariable(String name, String unit, Point value) {
super(Externalization.SPATIAL_TYPE, name, unit); super(Externalization.SPATIAL_TYPE, name, unit);
this.point = value; this.value = value;
} }
/** /**
...@@ -56,7 +53,7 @@ public class SpatialVariable extends Variable<Point> { ...@@ -56,7 +53,7 @@ public class SpatialVariable extends Variable<Point> {
*/ */
@Override @Override
public int hashCode() { public int hashCode() {
return this.point.hashCode(); return this.value.hashCode();
} }
/** /**
...@@ -75,25 +72,13 @@ public class SpatialVariable extends Variable<Point> { ...@@ -75,25 +72,13 @@ public class SpatialVariable extends Variable<Point> {
if (object instanceof SpatialVariable) { if (object instanceof SpatialVariable) {
SpatialVariable variable = (SpatialVariable) object; SpatialVariable variable = (SpatialVariable) object;
if (name.equals(variable.getName()) && unit.equals(variable.getUnit()) && variable.getValue().equals(point)) { if (name.equals(variable.getName()) && unit.equals(variable.getUnit()) && variable.getValue().equals(value)) {
return true; return true;
} }
} }
return false; return false;
} }
/** {@inheritDoc} */
@Override
public Point getValue() {
return this.point;
}
/** {@inheritDoc} */
@Override
public void setValue(Point value) {
this.point = value;
this.notifyInputChangeListeners(new InputEvent(this));
}