Commit 64f96cf6 authored by Eric Tobias's avatar Eric Tobias

Minor changes

parent 0393e0df
...@@ -30,15 +30,15 @@ ...@@ -30,15 +30,15 @@
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<version>19.0</version> <version>19.0</version>
</dependency> </dependency>
<dependency>
<groupId>dk.ange</groupId>
<artifactId>javaoctave</artifactId>
<version>0.6.4</version>
</dependency>
<dependency> <dependency>
<groupId>lu.list.itis.dkd.tui</groupId> <groupId>lu.list.itis.dkd.tui</groupId>
<artifactId>tulip</artifactId> <artifactId>tulip</artifactId>
<version>2.2.0</version> <version>2.2.0</version>
</dependency> </dependency>
<dependency>
<groupId>dk.ange</groupId>
<artifactId>javaoctave</artifactId>
<version>0.6.5-SNAPSHOT</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
/** /**
* Copyright CRP Henri Tudor, 2014. * Copyright CRP Henri Tudor, 2014. All rights reserved. If you wish to use this code for any
* All rights reserved. If you wish to use this code for any purpose, * purpose, please contact CRP Henri Tudor's Technology Transfer Office : tto@tudor.lu
* please contact CRP Henri Tudor's Technology Transfer Office : tto@tudor.lu
*/ */
package lu.list.itis.dkd.cps.system; package lu.list.itis.dkd.cps.system;
import lu.list.itis.dkd.cps.variable.Variable;
import lu.list.itis.dkd.dbc.annotation.NonNullByDefault;
import lu.list.itis.dkd.dbc.annotation.Nullable;
import com.google.common.base.Preconditions;
import java.io.CharArrayWriter; import java.io.CharArrayWriter;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.logging.Level; import java.util.logging.Level;
...@@ -12,14 +17,9 @@ import java.util.logging.Logger; ...@@ -12,14 +17,9 @@ import java.util.logging.Logger;
import javax.script.ScriptEngine; import javax.script.ScriptEngine;
import com.google.common.base.Preconditions;
import dk.ange.octave.OctaveEngine; import dk.ange.octave.OctaveEngine;
import dk.ange.octave.type.Octave; import dk.ange.octave.type.Octave;
import dk.ange.octave.type.OctaveDouble; import dk.ange.octave.type.OctaveDouble;
import lu.list.itis.dkd.cps.variable.Variable;
import lu.list.itis.dkd.dbc.annotation.NonNullByDefault;
import lu.list.itis.dkd.dbc.annotation.Nullable;
/** /**
* @author Nico MACK [nico.mack@list.lu] * @author Nico MACK [nico.mack@list.lu]
...@@ -27,104 +27,93 @@ import lu.list.itis.dkd.dbc.annotation.Nullable; ...@@ -27,104 +27,93 @@ import lu.list.itis.dkd.dbc.annotation.Nullable;
* @version 1 * @version 1
*/ */
@NonNullByDefault @NonNullByDefault
public class OctaveEquation extends Equation public class OctaveEquation extends Equation {
{ private OctaveMapping mapping, lockedMapping;
private OctaveMapping mapping, lockedMapping; @Nullable
@Nullable private String script;
private String script; private OctaveEngine scriptEngine;
private OctaveEngine scriptEngine; private CharArrayWriter engineErrors;
private CharArrayWriter engineErrors;
private static Logger LOGGER = Logger.getLogger(OctaveEquation.class.getSimpleName());
private static Logger LOGGER = Logger.getLogger(OctaveEquation.class.getSimpleName());
/**
/** * Constructor
* Constructor *
* @param mapping * @param mapping
* The mapping of variables to take into account for the equation. * The mapping of variables to take into account for the equation.
* @param script * @param script
* The mathematical representation of the equation in string form. * The mathematical representation of the equation in string form. Note that the names of
* Note that the names of the variables in the script must coincide with * the variables in the script must coincide with the names of the variables in the
* the names of the variables in the mapping. * mapping.
*/ */
public OctaveEquation(OctaveMapping mapping, String script) public OctaveEquation(OctaveMapping mapping, String script) {
{ super(mapping, script);
super (mapping,script); this.mapping = mapping;
this.mapping = mapping; this.lockedMapping = mapping;
this.lockedMapping = mapping; this.script = script;
this.script = script; this.engineErrors = new CharArrayWriter();
this.engineErrors = new CharArrayWriter (); }
}
/**
/** * 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 * mapping.
* the variables provided by the mapping. */
*/ @SuppressWarnings("null")
@SuppressWarnings("null") public synchronized void evaluate() {
public synchronized void evaluate() LinkedHashSet<Variable> dependentVariables;
{ String evaluationErrors;
LinkedHashSet<Variable> dependentVariables;
String evaluationErrors; Preconditions.checkState(scriptEngine != null);
Preconditions.checkState(scriptEngine != null); try {
engineErrors.reset();
try { scriptEngine.setErrorWriter(engineErrors);
engineErrors.reset(); initParameters();
scriptEngine.setErrorWriter(engineErrors); scriptEngine.eval(this.script);
initParameters(); } catch (Exception exception) {
scriptEngine.eval(this.script); LOGGER.log(Level.SEVERE, "Error while evaluating script " + this.script, exception);
} }
catch (Exception exception)
{ dependentVariables = mapping.getDependentVariables();
LOGGER.log(Level.SEVERE,"Error while evaluating script " + this.script,exception); for (Variable variable : dependentVariables) {
} try {
OctaveDouble result = scriptEngine.get(OctaveDouble.class, variable.getName());
dependentVariables = mapping.getDependentVariables(); variable.setValue(result.get(1));
for (Variable variable: dependentVariables) } catch (Exception exception) {
{ LOGGER.log(Level.SEVERE, "Error while retrieving variable " + variable.getName(), exception);
try { }
OctaveDouble result = scriptEngine.get(OctaveDouble.class, variable.getName()); }
variable.setValue(result.get(1));
} evaluationErrors = engineErrors.toString();
catch (Exception exception)
{ if (evaluationErrors.length() > 0) {
LOGGER.log(Level.SEVERE,"Error while retrieving variable " + variable.getName(),exception); LOGGER.log(Level.WARNING, "Error while evaluating equation :" + evaluationErrors);
} }
}
evaluationErrors = engineErrors.toString(); }
if (evaluationErrors.length() > 0) /**
{ * Method used to replace variables with their actual values in the formula.
LOGGER.log(Level.WARNING,"Error while evaluating equation :" + evaluationErrors); *
} * @return The formula with variables replaced by their values, ready for evaluation.
*/
private void initParameters() {
} for (Variable variable : lockedMapping.getIndependentVariables()) {
Double numericValue = Double.valueOf(variable.getValue().toString());
/**
* Method used to replace variables with their actual values in the formula. scriptEngine.put(variable.getName(), Octave.scalar(numericValue));
* @return }
* The formula with variables replaced by their values, ready for evaluation. }
*/
private void initParameters() /**
{ * Method for setting the script engine to use.
for(Variable variable : lockedMapping.getIndependentVariables()) *
{ * @param engine
Double numericValue = Double.valueOf(variable.getValue().toString()); * The {@link ScriptEngine} instance to use for evaluating the scripted formulas.
*/
scriptEngine.put(variable.getName(),Octave.scalar(numericValue)); public void setScriptEngine(OctaveEngine engine) {
} scriptEngine = engine;
} }
/**
* Method for setting the script engine to use.
* @param engine
* The {@link ScriptEngine} instance to use for
* evaluating the scripted formulas.
*/
public void setScriptEngine (OctaveEngine engine)
{
scriptEngine = engine;
}
} }
\ No newline at end of file
...@@ -16,14 +16,18 @@ ...@@ -16,14 +16,18 @@
*/ */
package lu.list.itis.dkd.tui.widget; package lu.list.itis.dkd.tui.widget;
import lu.list.itis.dkd.cps.variable.NumericalVariable; import lu.list.itis.dkd.cps.variable.tangible.TangibleNumericalVariable;
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.adapter.TangibleObject; import lu.list.itis.dkd.tui.adapter.TangibleObject;
import lu.list.itis.dkd.tui.content.InformationProvider; import lu.list.itis.dkd.tui.content.InformationProvider;
import lu.list.itis.dkd.tui.content.InformationReceiver;
import lu.list.itis.dkd.tui.utility.Point; import lu.list.itis.dkd.tui.utility.Point;
import lu.list.itis.dkd.tui.widget.builder.BaseValueWidgetBuilder; import lu.list.itis.dkd.tui.widget.builder.BaseValueWidgetBuilder;
import lu.list.itis.dkd.tui.widget.builder.ValueWidgetBuilder; import lu.list.itis.dkd.tui.widget.builder.ValueWidgetBuilder;
import lu.list.itis.dkd.tui.widget.corona.Corona;
import java.awt.Graphics2D;
/** /**
* A simple widget that holds an integer value and may modify the value base on a rotation. * A simple widget that holds an integer value and may modify the value base on a rotation.
...@@ -46,7 +50,7 @@ public final class ValueWidget extends BaseWidget implements InformationProvider ...@@ -46,7 +50,7 @@ public final class ValueWidget extends BaseWidget implements InformationProvider
/** The value of the angle that is considered as one step. */ /** The value of the angle that is considered as one step. */
private double stepSize; private double stepSize;
/** The variable holding the value to be stored and manipulated by the widget. */ /** The variable holding the value to be stored and manipulated by the widget. */
private NumericalVariable variable; private TangibleNumericalVariable variable;
/** The base value to be set for the variable. */ /** The base value to be set for the variable. */
private final double baseValue; private final double baseValue;
/** Whether the value held by the widget is to be modified on a rotation of a handle. */ /** Whether the value held by the widget is to be modified on a rotation of a handle. */
...@@ -66,7 +70,7 @@ public final class ValueWidget extends BaseWidget implements InformationProvider ...@@ -66,7 +70,7 @@ public final class ValueWidget extends BaseWidget implements InformationProvider
this.lowerStopAngle = builder.lowerStopAngle; this.lowerStopAngle = builder.lowerStopAngle;
this.upperStopAngle = builder.upperStopAngle; this.upperStopAngle = builder.upperStopAngle;
this.stepSize = builder.stepSize; this.stepSize = builder.stepSize;
this.setVariable(builder.variable); this.variable = builder.variable;
this.modifyValueOnRotation = builder.modifyValueOnRotation; this.modifyValueOnRotation = builder.modifyValueOnRotation;
/** /**
...@@ -105,30 +109,44 @@ public final class ValueWidget extends BaseWidget implements InformationProvider ...@@ -105,30 +109,44 @@ public final class ValueWidget extends BaseWidget implements InformationProvider
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public @Nullable Double getInformation(Point position) { public @Nullable Double getInformation(Point position) {
return getInformation(); return variable.getInformation(position);
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public Double getInformation() { public Double getInformation() {
return getVariable().getValue(); return variable.getInformation();
} }
/** /**
* Simple getter method for variable. * Simple getter method for variable.
*
* @return The value of variable. * @return The value of variable.
*/ */
public NumericalVariable getVariable() { public TangibleNumericalVariable getVariable() {
return variable; return variable;
} }
/** /**
* Simple setter method for variable. * Simple setter method for variable.
*
* @param variable * @param variable
* The value to set variable to. * The value to set variable to.
* @return
*/ */
public NumericalVariable setVariable(NumericalVariable variable) { public TangibleNumericalVariable setVariable(TangibleNumericalVariable variable) {
this.variable = variable; this.variable = variable;
return variable; return variable;
} }
@SuppressWarnings("unchecked")
@Override
public void paint(Graphics2D canvas) {
for (Corona corona : coronas.values()) {
if (corona instanceof InformationReceiver<?>) {
((InformationReceiver<String>) corona).setInformation(variable.getInformation().toString());
}
}
super.paint(canvas);
}
} }
\ No newline at end of file
...@@ -16,8 +16,11 @@ ...@@ -16,8 +16,11 @@
*/ */
package lu.list.itis.dkd.tui.widget.builder; package lu.list.itis.dkd.tui.widget.builder;
import lu.list.itis.dkd.tui.exception.BuildException;
import lu.list.itis.dkd.tui.widget.ValueWidget; import lu.list.itis.dkd.tui.widget.ValueWidget;
import org.jdom2.Element;
/** /**
* The concrete builder that will build a new instance of {@link ValueWidget} with each call to * The concrete builder that will build a new instance of {@link ValueWidget} with each call to
* {@link #build()}. * {@link #build()}.
...@@ -28,9 +31,32 @@ import lu.list.itis.dkd.tui.widget.ValueWidget; ...@@ -28,9 +31,32 @@ import lu.list.itis.dkd.tui.widget.ValueWidget;
*/ */
public final class ValueWidgetBuilder extends BaseValueWidgetBuilder<ValueWidgetBuilder> { public final class ValueWidgetBuilder extends BaseValueWidgetBuilder<ValueWidgetBuilder> {
/**
* Simple no-arg constructor initializing all fields.
*/
public ValueWidgetBuilder() {
super();
}
/**
* Constructor initializing all fields from an {@link Element} containing as child elements all
* the information on fields to initialize.
*
* @param rootElement
* The element harbouring, on child nodes, the necessary information to initialize all
* fields of the builder.
* @throws BuildException
* Thrown when any of the fields fail to populate due to an error in reading information
* from the XML file.
*/
public ValueWidgetBuilder(Element rootElement) throws BuildException {
super(rootElement);
}
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public ValueWidget build() { public ValueWidget build() {
return new ValueWidget(this); return new ValueWidget(this);
} }
} }
\ No newline at end of file
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