Commit 902ad1cf authored by Nico Mack's avatar Nico Mack

Added valueChanged interaction to ValueCoronas

parent 59d7a28a
ON_POSITION_CHANGED=positionChanged
\ No newline at end of file
ON_POSITION_CHANGED=positionChanged
ON_VALUE_CHANGED=valueChanged
\ No newline at end of file
......@@ -37,6 +37,13 @@ import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.script.ScriptEngine;
......@@ -59,10 +66,15 @@ public class Equation {
@Nullable
private Executor scriptExecutor;
private String script;
private boolean isolated;
private boolean asynchronous;
private static final Logger LOGGER = LoggerFactory.getLogger(Equation.class.getSimpleName());
private static ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
private ScheduledExecutorService polling;
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s)
......@@ -86,6 +98,7 @@ public class Equation {
this.lockedMapping = mapping;
this.script = script;
this.isolated = false;
this.asynchronous = false;
}
// ---------------------------------------------------------------------------
......@@ -149,6 +162,85 @@ public class Equation {
}
}
// ---------------------------------------------------------------------------
private HashMap<InputChangeListener, List<Variable<?>>> synchronousEvaluate() {
try {
scriptExecutor.eval(this.script);
} catch (Exception exception) {
LOGGER.error("Error while evaluating script {}", this.script); //$NON-NLS-1$
LOGGER.error("Engine threw an exception {}", exception); //$NON-NLS-1$
}
return this.retrieveResults();
}
// ---------------------------------------------------------------------------
private void asynchronousEvaluate() {
Future<HashMap<InputChangeListener, List<Variable<?>>>> asyncTask;
polling = Executors.newSingleThreadScheduledExecutor();
try {
asyncTask = executor.submit(new Callable<HashMap<InputChangeListener, List<Variable<?>>>>() {
@Override
public HashMap<InputChangeListener, List<Variable<?>>> call() throws Exception {
scriptExecutor.eval(script);
return (retrieveResults());
}
});
} catch (Exception exception) {
LOGGER.error("Error while evaluating script {}", this.script); //$NON-NLS-1$
LOGGER.error("Engine threw an exception!", exception); //$NON-NLS-1$
return;
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Equation Executor Thread Pool Size = {}", executor.getPoolSize()); //$NON-NLS-1$
}
Runnable pollingTask = () -> {
while (!asyncTask.isDone()) {
try {
TimeUnit.MILLISECONDS.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
try {
processResults(asyncTask.get());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
LOGGER.error("Error while retrieving results!", e);
}
polling.shutdownNow();
};
polling.scheduleAtFixedRate(pollingTask, 0, 1000, TimeUnit.MILLISECONDS);
}
// ---------------------------------------------------------------------------
private void processResults(HashMap<InputChangeListener, List<Variable<?>>> consolidatedListeners) {
boolean consolidated = ((consolidatedListeners != null) && !consolidatedListeners.isEmpty());
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) {
this.notifyListeners(consolidatedListeners);
}
String evaluationErrors = scriptExecutor.getExecutionErrors();
if (evaluationErrors.length() > 0) {
this.logErrors(evaluationErrors);
}
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body
......@@ -192,45 +284,44 @@ public class Equation {
// ---------------------------------------------------------------------------
/**
* Method called to evaluate the script of the equation with the variables provided by the
* mapping.
* Specifies this equation to be evaluate asynchronously, i.e. its executor will be invoked in a
* dedicated thread.
*
* @param deferIt
*/
// ---------------------------------------------------------------------------
public synchronized void evaluate() {
String evaluationErrors;
Preconditions.checkState(scriptExecutor != null);
try {
scriptExecutor.resetExecutionErrors();
for (Declaration declaration : lockedMapping.getIndependentVariables()) {
scriptExecutor.set(declaration);
}
public void setAsynchronous(boolean deferIt) {
this.asynchronous = deferIt;
}
scriptExecutor.eval(this.script);
} catch (Exception exception) {
LOGGER.error("Error while evaluating script {}", this.script); //$NON-NLS-1$
LOGGER.error("Engine threw an exception {}", exception); //$NON-NLS-1$
}
// ---------------------------------------------------------------------------
HashMap<InputChangeListener, List<Variable<?>>> consolidatedListeners = this.retrieveResults();
public boolean isAsynchronous() {
return this.asynchronous;
}
boolean consolidated = ((consolidatedListeners != null) && !consolidatedListeners.isEmpty());
// ---------------------------------------------------------------------------
/**
* Method called to evaluate the script of the equation with the variables provided by the
* mapping.
*/
// ---------------------------------------------------------------------------
if (isolated && LOGGER.isInfoEnabled()) {
LOGGER.info("Equation {} evaluated in isolated mode! Dependent variables won't trigger dependent equations!", this.name); //$NON-NLS-1$
}
public synchronized void evaluate() {
Preconditions.checkState(scriptExecutor != null);
if (!isolated && consolidated) {
this.notifyListeners(consolidatedListeners);
for (Declaration declaration : lockedMapping.getIndependentVariables()) {
scriptExecutor.set(declaration);
}
evaluationErrors = scriptExecutor.getExecutionErrors();
HashMap<InputChangeListener, List<Variable<?>>> consolidatedListeners;
if (evaluationErrors.length() > 0) {
this.logErrors(evaluationErrors);
if (asynchronous) {
this.asynchronousEvaluate();
} else {
consolidatedListeners = this.synchronousEvaluate();
this.processResults(consolidatedListeners);
}
}
......
......@@ -35,10 +35,13 @@ public class CpsSignature extends NLS {
public static String ON_POSITION_CHANGED;
public static String ON_VALUE_CHANGED;
static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, CpsSignature.class);
}
private CpsSignature() {}
private CpsSignature() {
}
}
\ No newline at end of file
package lu.list.itis.dkd.tui.widget.corona;
import lu.list.itis.dkd.tui.cps.variable.NumericalVariable;
import lu.list.itis.dkd.tui.event.interaction.Interaction;
import lu.list.itis.dkd.tui.utility.AngleUtils;
import lu.list.itis.dkd.tui.utility.ColorPair;
import lu.list.itis.dkd.tui.utility.CpsSignature;
import lu.list.itis.dkd.tui.utility.Point;
import lu.list.itis.dkd.tui.utility.ShapeUtils;
import lu.list.itis.dkd.tui.utility.VariableFormat;
......@@ -428,10 +430,13 @@ public class ArcGraph extends ValueCorona {
this.setNormalizedValue(value);
}
this.label = this.variableFormat.format(this.variable);
if (textFont != null) {
this.labelMetrics = textFont.getLineMetrics(this.label, new FontRenderContext(null, true, true));
}
this.respondTo(new Interaction(CpsSignature.ON_VALUE_CHANGED));
}
}
......
......@@ -6,6 +6,8 @@ import lu.list.itis.dkd.tui.cps.InputEvent;
import lu.list.itis.dkd.tui.cps.system.VariableBased;
import lu.list.itis.dkd.tui.cps.variable.NumericalVariable;
import lu.list.itis.dkd.tui.cps.variable.Variable;
import lu.list.itis.dkd.tui.event.interaction.Interaction;
import lu.list.itis.dkd.tui.utility.CpsSignature;
import lu.list.itis.dkd.tui.widget.corona.builder.BaseValueCoronaBuilder;
import org.python.google.common.base.Preconditions;
......@@ -86,6 +88,7 @@ public class ValueCorona extends SelectableCorona implements InformationReceiver
public void setInformation(Object information) {
if (information != null) {
this.variable.setValue(this.variable.valueFromString(information.toString()));
this.respondTo(new Interaction(CpsSignature.ON_VALUE_CHANGED));
}
}
......
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