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 {
@Nullable
private Executor scriptExecutor;
private String script;
private boolean isolated;
private static Logger LOGGER = LoggerFactory.getLogger(Equation.class.getSimpleName());
......@@ -70,6 +71,7 @@ public class Equation {
this.mapping = mapping;
this.lockedMapping = mapping;
this.script = script;
this.isolated = false;
}
/**
......@@ -81,6 +83,16 @@ public class Equation {
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
* mapping.
......@@ -100,7 +112,8 @@ public class Equation {
scriptExecutor.eval(this.script);
} 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();
......@@ -121,7 +134,11 @@ public class Equation {
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()) {
Variable<?> variable = consolidatedListeners.get(listener);
listener.inputChanged(new InputEvent(variable));
......
......@@ -555,12 +555,14 @@ public class EquationSystemBuilder {
private void addDependencies(Element equation) throws EquationSystemException {
String periodicity = equation.getAttributeValue(Externalization.PERIODIC_ATTRIBUTE);
boolean periodic = false;
if (!Strings.isNullOrEmpty(periodicity)) {
Matcher regex = booleanPattern.matcher(periodicity);
periodic = regex.matches();
}
addParameterDependencies(equation);
addResultDependencies(equation);
......@@ -582,6 +584,7 @@ public class EquationSystemBuilder {
LinkedHashSet<Variable<?>> equationInputs = new LinkedHashSet<>();
LinkedHashSet<Variable<?>> equationOutputs = new LinkedHashSet<>();
boolean lockToNesting = true;
String periodicity = equation.getAttributeValue(Externalization.PERIODIC_ATTRIBUTE);
boolean periodic = false;
......@@ -590,6 +593,12 @@ public class EquationSystemBuilder {
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)) {
if (independentVariables.containsKey(identifier)) {
......@@ -637,6 +646,7 @@ public class EquationSystemBuilder {
final LinearEquationSystem equationSystem = new LinearEquationSystem(lockToNesting);
Equation newEquation = new Equation(equationName, variableMapping, script);
newEquation.setExecutor(executors.get(executor));
newEquation.setIsolated(isolated);
equationSystem.addEquation(newEquation);
if (!periodic) {
......
......@@ -58,9 +58,7 @@ public class LinearEquationSystem extends System {
public LinearEquationSystem(boolean lockToNesting) {
super();
equations = new ConcurrentHashMap<>();
// scriptEngine = new ScriptEngineManager().getEngineByName("js"); //$NON-NLS-1$
this.lockSystemForNesting = lockToNesting;
// assert scriptEngine != null : "Script engine cannot be null;"; //$NON-NLS-1$
}
/**
......
......@@ -52,7 +52,7 @@ public class PythonExecutor extends Executor {
@Override
public void set(Variable<?> variable) {
if (variable instanceof VectorVariable) {
VectorVariable<?> vector = (VectorVariable<Double>) variable;
VectorVariable<?> vector = (VectorVariable<?>) variable;
if (!vector.isEmpty()) {
PyArray array = null;
......@@ -84,6 +84,9 @@ public class PythonExecutor extends Executor {
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 {
......
......@@ -373,6 +373,7 @@ public class SqlExecutor extends Executor {
// Suspend notification of input change listeners while populating vector
variable.suspendListenerNotification(true);
vector.clear();
for (Object[] values : columnValues) {
Object value = values[index];
vector.add(value);
......
......@@ -42,6 +42,7 @@ public class Externalization extends NLS {
public static String VECTOR_VARIABLE_CLASS;
public static String EQUATION_ELEMENT;
public static String MODE_ATTRIBUTE;
public static String EQUATIONS_ELEMENT;
public static String INVOKE_ELEMENT;
public static String EXECUTOR_ATTRIBUTE;
......@@ -75,6 +76,8 @@ public class Externalization extends NLS {
public static final String TEXT_TYPE = "text";
public static final String VECTOR_TYPE = "vector";
public static final String ISOLATED_VALUE = "isolated";
static {
// initialize resource bundle
......
......@@ -11,6 +11,7 @@ EXECUTOR_POSTFIX=Executor
EQUATIONS_ELEMENT=equations
EQUATION_ELEMENT=equation
MODE_ATTRIBUTE=mode
INVOKE_ELEMENT=invoke
EXECUTOR_ATTRIBUTE=executor
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