Commit a439fabe authored by Nico Mack's avatar Nico Mack

Added isolation mode to equations. Better support for datatypes in

Python Executor
parent 05795c44
...@@ -52,6 +52,7 @@ public class Equation { ...@@ -52,6 +52,7 @@ public class Equation {
@Nullable @Nullable
private Executor scriptExecutor; private Executor scriptExecutor;
private String script; private String script;
private boolean isolated;
private static Logger LOGGER = LoggerFactory.getLogger(Equation.class.getSimpleName()); private static Logger LOGGER = LoggerFactory.getLogger(Equation.class.getSimpleName());
...@@ -70,6 +71,7 @@ public class Equation { ...@@ -70,6 +71,7 @@ public class Equation {
this.mapping = mapping; this.mapping = mapping;
this.lockedMapping = mapping; this.lockedMapping = mapping;
this.script = script; this.script = script;
this.isolated = false;
} }
/** /**
...@@ -81,6 +83,16 @@ public class Equation { ...@@ -81,6 +83,16 @@ public class Equation {
return mapping; return mapping;
} }
/**
* Allows to isolate this equation. An isolated equation will not notify dependent equations
* when evaluation entailed changes in results.
*
* @param isolateIt
*/
public void setIsolated(boolean isolateIt) {
this.isolated = isolateIt;
}
/** /**
* Method called to evaluate the script of the equation with the variables provided by the * Method called to evaluate the script of the equation with the variables provided by the
* mapping. * mapping.
...@@ -100,7 +112,8 @@ public class Equation { ...@@ -100,7 +112,8 @@ public class Equation {
scriptExecutor.eval(this.script); scriptExecutor.eval(this.script);
} catch (Exception exception) { } catch (Exception exception) {
LOGGER.error("Error while evaluating script {}", this.script, exception); //$NON-NLS-1$ LOGGER.error("Error while evaluating script {}", this.script); //$NON-NLS-1$
LOGGER.error("Engine threw an exception", exception); //$NON-NLS-1$
} }
dependentVariables = mapping.getDependentVariables(); dependentVariables = mapping.getDependentVariables();
...@@ -121,7 +134,11 @@ public class Equation { ...@@ -121,7 +134,11 @@ public class Equation {
variable.suspendListenerNotification(false); variable.suspendListenerNotification(false);
} }
if (consolidated && consolidatedListeners != null) { if (isolated && LOGGER.isInfoEnabled()) {
LOGGER.info("Equation {} evaluated in isolated mode! Dependent variables won't trigger dependent equations!", this.name); //$NON-NLS-1$
}
if (!isolated && consolidated && consolidatedListeners != null) {
for (InputChangeListener listener : consolidatedListeners.keySet()) { for (InputChangeListener listener : consolidatedListeners.keySet()) {
Variable<?> variable = consolidatedListeners.get(listener); Variable<?> variable = consolidatedListeners.get(listener);
listener.inputChanged(new InputEvent(variable)); listener.inputChanged(new InputEvent(variable));
......
...@@ -555,12 +555,14 @@ public class EquationSystemBuilder { ...@@ -555,12 +555,14 @@ public class EquationSystemBuilder {
private void addDependencies(Element equation) throws EquationSystemException { private void addDependencies(Element equation) throws EquationSystemException {
String periodicity = equation.getAttributeValue(Externalization.PERIODIC_ATTRIBUTE); String periodicity = equation.getAttributeValue(Externalization.PERIODIC_ATTRIBUTE);
boolean periodic = false; boolean periodic = false;
if (!Strings.isNullOrEmpty(periodicity)) { if (!Strings.isNullOrEmpty(periodicity)) {
Matcher regex = booleanPattern.matcher(periodicity); Matcher regex = booleanPattern.matcher(periodicity);
periodic = regex.matches(); periodic = regex.matches();
} }
addParameterDependencies(equation); addParameterDependencies(equation);
addResultDependencies(equation); addResultDependencies(equation);
...@@ -582,6 +584,7 @@ public class EquationSystemBuilder { ...@@ -582,6 +584,7 @@ public class EquationSystemBuilder {
LinkedHashSet<Variable<?>> equationInputs = new LinkedHashSet<>(); LinkedHashSet<Variable<?>> equationInputs = new LinkedHashSet<>();
LinkedHashSet<Variable<?>> equationOutputs = new LinkedHashSet<>(); LinkedHashSet<Variable<?>> equationOutputs = new LinkedHashSet<>();
boolean lockToNesting = true; boolean lockToNesting = true;
String periodicity = equation.getAttributeValue(Externalization.PERIODIC_ATTRIBUTE); String periodicity = equation.getAttributeValue(Externalization.PERIODIC_ATTRIBUTE);
boolean periodic = false; boolean periodic = false;
...@@ -590,6 +593,12 @@ public class EquationSystemBuilder { ...@@ -590,6 +593,12 @@ public class EquationSystemBuilder {
periodic = regex.matches(); periodic = regex.matches();
} }
String mode = equation.getAttributeValue(Externalization.MODE_ATTRIBUTE);
boolean isolated = false;
if (!Strings.isNullOrEmpty(mode)) {
isolated = Externalization.ISOLATED_VALUE.equals(mode);
}
for (String identifier : equationParameters.get(equationName)) { for (String identifier : equationParameters.get(equationName)) {
if (independentVariables.containsKey(identifier)) { if (independentVariables.containsKey(identifier)) {
...@@ -637,6 +646,7 @@ public class EquationSystemBuilder { ...@@ -637,6 +646,7 @@ public class EquationSystemBuilder {
final LinearEquationSystem equationSystem = new LinearEquationSystem(lockToNesting); final LinearEquationSystem equationSystem = new LinearEquationSystem(lockToNesting);
Equation newEquation = new Equation(equationName, variableMapping, script); Equation newEquation = new Equation(equationName, variableMapping, script);
newEquation.setExecutor(executors.get(executor)); newEquation.setExecutor(executors.get(executor));
newEquation.setIsolated(isolated);
equationSystem.addEquation(newEquation); equationSystem.addEquation(newEquation);
if (!periodic) { if (!periodic) {
......
...@@ -58,9 +58,7 @@ public class LinearEquationSystem extends System { ...@@ -58,9 +58,7 @@ public class LinearEquationSystem extends System {
public LinearEquationSystem(boolean lockToNesting) { public LinearEquationSystem(boolean lockToNesting) {
super(); super();
equations = new ConcurrentHashMap<>(); equations = new ConcurrentHashMap<>();
// scriptEngine = new ScriptEngineManager().getEngineByName("js"); //$NON-NLS-1$
this.lockSystemForNesting = lockToNesting; this.lockSystemForNesting = lockToNesting;
// assert scriptEngine != null : "Script engine cannot be null;"; //$NON-NLS-1$
} }
/** /**
......
...@@ -52,7 +52,7 @@ public class PythonExecutor extends Executor { ...@@ -52,7 +52,7 @@ public class PythonExecutor extends Executor {
@Override @Override
public void set(Variable<?> variable) { public void set(Variable<?> variable) {
if (variable instanceof VectorVariable) { if (variable instanceof VectorVariable) {
VectorVariable<?> vector = (VectorVariable<Double>) variable; VectorVariable<?> vector = (VectorVariable<?>) variable;
if (!vector.isEmpty()) { if (!vector.isEmpty()) {
PyArray array = null; PyArray array = null;
...@@ -84,6 +84,9 @@ public class PythonExecutor extends Executor { ...@@ -84,6 +84,9 @@ public class PythonExecutor extends Executor {
LOGGER.info("Parameter -> {} = {}", variable.getName(), list.toString()); //$NON-NLS-1$ LOGGER.info("Parameter -> {} = {}", variable.getName(), list.toString()); //$NON-NLS-1$
} }
} }
} else {
LOGGER.warn("Empty Vector {}", variable.getName()); //$NON-NLS-1$
engine.set(variable.getName(), new PyArray(Object.class, 0));
} }
} else { } else {
......
...@@ -373,6 +373,7 @@ public class SqlExecutor extends Executor { ...@@ -373,6 +373,7 @@ public class SqlExecutor extends Executor {
// Suspend notification of input change listeners while populating vector // Suspend notification of input change listeners while populating vector
variable.suspendListenerNotification(true); variable.suspendListenerNotification(true);
vector.clear();
for (Object[] values : columnValues) { for (Object[] values : columnValues) {
Object value = values[index]; Object value = values[index];
vector.add(value); vector.add(value);
......
...@@ -42,6 +42,7 @@ public class Externalization extends NLS { ...@@ -42,6 +42,7 @@ public class Externalization extends NLS {
public static String VECTOR_VARIABLE_CLASS; public static String VECTOR_VARIABLE_CLASS;
public static String EQUATION_ELEMENT; public static String EQUATION_ELEMENT;
public static String MODE_ATTRIBUTE;
public static String EQUATIONS_ELEMENT; public static String EQUATIONS_ELEMENT;
public static String INVOKE_ELEMENT; public static String INVOKE_ELEMENT;
public static String EXECUTOR_ATTRIBUTE; public static String EXECUTOR_ATTRIBUTE;
...@@ -75,6 +76,8 @@ public class Externalization extends NLS { ...@@ -75,6 +76,8 @@ public class Externalization extends NLS {
public static final String TEXT_TYPE = "text"; public static final String TEXT_TYPE = "text";
public static final String VECTOR_TYPE = "vector"; public static final String VECTOR_TYPE = "vector";
public static final String ISOLATED_VALUE = "isolated";
static { static {
// initialize resource bundle // initialize resource bundle
......
...@@ -11,6 +11,7 @@ EXECUTOR_POSTFIX=Executor ...@@ -11,6 +11,7 @@ EXECUTOR_POSTFIX=Executor
EQUATIONS_ELEMENT=equations EQUATIONS_ELEMENT=equations
EQUATION_ELEMENT=equation EQUATION_ELEMENT=equation
MODE_ATTRIBUTE=mode
INVOKE_ELEMENT=invoke INVOKE_ELEMENT=invoke
EXECUTOR_ATTRIBUTE=executor EXECUTOR_ATTRIBUTE=executor
NAME_ATTRIBUTE=name NAME_ATTRIBUTE=name
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment