Commit fa9f11a3 authored by Eric Tobias's avatar Eric Tobias

Changed ConditionalCorina implementation to only carry one image and a simpler trigger

parent 58afc74b
......@@ -133,17 +133,17 @@ public class Equation {
lockedMapping = mapping.lock();
}
/**
* Method used to update the mapping with a variable, that is, to update the reference to a
* variable so that the mapping will correctly map all changes of the variable. <br>
* <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);
}
// /**
// * Method used to update the mapping with a variable, that is, to update the reference to a
// * variable so that the mapping will correctly map all changes of the variable. <br>
// * <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
......@@ -24,8 +24,6 @@ import lu.list.itis.dkd.dbc.annotation.NonNullByDefault;
import lu.list.itis.dkd.dbc.annotation.Nullable;
import lu.list.itis.dkd.tui.cps.variable.Variable;
import com.google.common.base.Preconditions;
import java.util.Collection;
import java.util.LinkedHashSet;
......@@ -51,7 +49,8 @@ public class Mapping {
* The dependent variable of the mapping.
*/
public Mapping(Collection<Variable> independentVariables, Variable dependentVariable) {
Preconditions.checkArgument(!independentVariables.isEmpty(), "The set of input variables cannot be empty."); //$NON-NLS-1$
// Preconditions.checkArgument(!independentVariables.isEmpty(), "The set of input variables
// cannot be empty."); //$NON-NLS-1$
this.independentVariables = independentVariables;
this.dependentVariable = dependentVariable;
}
......@@ -152,21 +151,23 @@ public class Mapping {
return new Mapping(clones, dependentVariable.clone());
}
/**
* Method for updating a reference to an independent variable. The method will add the variable
* to the collection of independent variables this mapping instance will contain <b>iff</b> the
* mapping already mapped the variable prior to this call. <br>
* <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 updateVariableReference(Variable variable) {
if (independentVariables.contains(variable)) {
independentVariables.remove(variable);
independentVariables.add(variable);
}
}
// /**
// * Method for updating a reference to an independent variable. The method will add the
// variable
// * to the collection of independent variables this mapping instance will contain <b>iff</b>
// the
// * mapping already mapped the variable prior to this call. <br>
// * <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 updateVariableReference(Variable variable) {
// if (independentVariables.contains(variable)) {
// independentVariables.remove(variable);
// independentVariables.add(variable);
// }
// }
}
\ No newline at end of file
......@@ -20,13 +20,9 @@ import lu.list.itis.dkd.tui.cps.variable.Variable;
import lu.list.itis.dkd.tui.utility.Point;
import lu.list.itis.dkd.tui.widget.corona.builder.ConditionalCoronaBuilder;
import com.google.common.base.Predicate;
import com.google.common.collect.Maps;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.util.Map;
import java.util.Set;
import java.util.LinkedHashSet;
/**
* @author Eric Tobias [eric.tobias@list.lu]
......@@ -35,22 +31,28 @@ import java.util.Set;
*/
@NonNullByDefault
public class ConditionalCorona extends Corona {
private Map<String, Variable> outputs;
private Set<Equation> equations;
private Map<Variable, java.awt.Image> images;
/**
* Predicate used to filter variables and check whether they contain <code>true</code> as a
* value if they are {@link BooleanVariable} instances in the first place. If the variable does
* not contain true or isn't a {@link BooleanVariable} instance, the predicate will return
* <code>false</code>.
*/
private Predicate<Variable> trigger = new Predicate<Variable>() {
@Override
public boolean apply(Variable variable) {
return variable instanceof BooleanVariable ? ((BooleanVariable) variable).getValue() : false;
}
};
/** The equation that will evaluate the trigger condition. */
private Equation trigger;
/** All variables that will be needed to evaluate the trigger condition. */
private LinkedHashSet<Variable> variables = new LinkedHashSet<>();
/** Variable used to easily gather the output from the trigger equation. */
private BooleanVariable triggerVariable = new BooleanVariable("Trigger", false); //$NON-NLS-1$
private java.awt.Image image;
// /**
// * Predicate used to filter variables and check whether they contain <code>true</code> as a
// * value if they are {@link BooleanVariable} instances in the first place. If the variable
// does
// * not contain true or isn't a {@link BooleanVariable} instance, the predicate will return
// * <code>false</code>.
// */
// private Predicate<Variable> triggerPredicate = new Predicate<Variable>() {
// @Override
// public boolean apply(Variable variable) {
// return variable instanceof BooleanVariable ? ((BooleanVariable) variable).getValue() : false;
// }
// };
/**
......@@ -60,15 +62,17 @@ public class ConditionalCorona extends Corona {
public ConditionalCorona(ConditionalCoronaBuilder builder) {
super(builder);
this.outputs = builder.outputs;
this.equations = builder.equations;
this.images = builder.images;
this.trigger = builder.trigger;
this.triggerVariable = builder.triggerVariable;
this.variables = builder.variables;
this.image = builder.image;
}
/** {@inheritDoc} */
@Override
public void paint(Graphics2D canvas) {
if (!active) {
trigger.evaluate();
if (!active || !triggerVariable.getValue()) {
return;
}
......@@ -85,42 +89,31 @@ public class ConditionalCorona extends Corona {
rotation.rotate(rotateWithHandle ? drawAt.getAngle() : initialTranslation != null ? initialTranslation.getAngle() : 0, centre.getX(), centre.getY());
rotation.translate(drawAt.x, drawAt.y);
updateTriggers();
for (java.awt.Image image : Maps.filterKeys(images, trigger).values()) {
AffineTransform innerRotation = (AffineTransform) rotation.clone();
if (spinOnCoronaCentre) {
innerRotation.rotate(rotateWithHandle ? -drawAt.getAngle() : 0);
innerRotation.translate(-image.getWidth(null) / 2, -image.getHeight(null) / 2);
}
innerRotation.transform(new Point(), drawAt);
if (spinOnCoronaCentre) {
rotation.rotate(rotateWithHandle ? -drawAt.getAngle() : 0);
rotation.translate(-image.getWidth(null) / 2, -image.getHeight(null) / 2);
}
if (Double.compare(initialRotation, 0d) != 0) {
AffineTransform transformation = new AffineTransform();
transformation.rotate(initialRotation, spinOnCoronaCentre ? drawAt.getX() + image.getWidth(null) / 2 : drawAt.getX(), spinOnCoronaCentre ? drawAt.getY() + image.getHeight(null) / 2 : drawAt.getY());
canvas.setTransform(transformation);
}
rotation.transform(new Point(), drawAt);
canvas.drawImage(image, (int) drawAt.x, (int) drawAt.y, null);
if (Double.compare(initialRotation, 0d) != 0) {
AffineTransform transformation = new AffineTransform();
transformation.rotate(initialRotation, spinOnCoronaCentre ? drawAt.getX() + image.getWidth(null) / 2 : drawAt.getX(), spinOnCoronaCentre ? drawAt.getY() + image.getHeight(null) / 2 : drawAt.getY());
canvas.setTransform(transformation);
}
canvas.drawImage(image, (int) drawAt.x, (int) drawAt.y, null);
canvas.setTransform(new AffineTransform());
}
private void updateTriggers() {
for (Equation equation : equations) {
equation.evaluate();
}
}
/**
* Method used to add a variable to the map of variables held by this corona.
* Method used to add a variable to the set of variables held by this corona.
*
* @param variable
* The variable to add.
*/
public void addVariable(Variable variable) {
equations.forEach(e -> e.updateMapping(variable));
variables.add(variable);
}
}
\ No newline at end of file
......@@ -14,9 +14,8 @@
package lu.list.itis.dkd.tui.widget.corona.builder;
import lu.list.itis.dkd.tui.cps.system.Equation;
import lu.list.itis.dkd.tui.cps.system.EquationSystemBuilder;
import lu.list.itis.dkd.tui.cps.system.EquationSystemException;
import lu.list.itis.dkd.tui.cps.system.LinearEquationSystem;
import lu.list.itis.dkd.tui.cps.system.Mapping;
import lu.list.itis.dkd.tui.cps.variable.BooleanVariable;
import lu.list.itis.dkd.tui.cps.variable.Variable;
import lu.list.itis.dkd.tui.exception.BuildException;
import lu.list.itis.dkd.tui.utility.Externalization;
......@@ -26,15 +25,14 @@ import org.jdom2.Element;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.LinkedHashSet;
import javax.imageio.ImageIO;
import javax.script.ScriptEngineManager;
/**
* {@link CoronaBuilder} extension building {@link ConditionalCorona} coronas.
*
* @author Eric Tobias [eric.tobias@list.lu]
* @since 1.3
* @version 1.3.1
......@@ -42,43 +40,38 @@ import javax.imageio.ImageIO;
* The concrete builder.
*/
public abstract class BaseConditionalCoronaBuilder<B extends BaseConditionalCoronaBuilder<B>> extends CoronaBuilder<B> {
public Map<String, Variable> outputs;
public Set<Equation> equations;
public Map<Variable, java.awt.Image> images = new HashMap<>();
ConcurrentHashMap<String, LinearEquationSystem> equationSystems;
/** The equation that will evaluate the trigger condition. */
public Equation trigger;
/** All variables that will be needed to evaluate the trigger condition. */
public LinkedHashSet<Variable> variables = new LinkedHashSet<>();
/** Variable used to easily gather the output from the trigger equation. */
public BooleanVariable triggerVariable = new BooleanVariable("Trigger", false); //$NON-NLS-1$
/** The image to show if the condition is true. */
public java.awt.Image image;
/**
* Constructor initializing all local fields.
*
* @param element
* The element harbouring, on child nodes, the necessary information to initialize all
* fields of the builder.
* @throws BuildException
* Exception raised when the building of a corona instance cannot complete successfully
* due to the violation of one or more contracts associated with the instance, including
* missing, incomplete, or erroneous parameters or values thereof
*/
public BaseConditionalCoronaBuilder(Element element) throws BuildException {
super(element);
EquationSystemBuilder equationSystemBuilder = new EquationSystemBuilder();
try {
equationSystemBuilder.buildFromElement(element);
} catch (EquationSystemException ese) {
throw new BuildException("The equation could not be built!", ese); //$NON-NLS-1$
}
outputs = equationSystemBuilder.getOutputVariables();
equations = equationSystemBuilder.getEquations();
equationSystems = equationSystemBuilder.getEquationSystem();
final String triggerCondition = element.getChildText(Externalization.TRIGGER_CONDITION_NODE);
trigger = new Equation(new Mapping(variables, triggerVariable), triggerCondition);
trigger.setScriptEngine(new ScriptEngineManager().getEngineByName("js")); //$NON-NLS-1$
try {
List<Element> imageElements = element.getChild(Externalization.IMAGES_ELEMENT).getChildren(Externalization.IMAGE_ELEMENT);
for (Element imageElement : imageElements) {
Variable trigger = outputs.get(imageElement.getChildText(Externalization.TRIGGER_ELEMENT));
java.awt.Image image = ImageIO.read(new File(imageElement.getChildText(Externalization.ASSET_ELEMENT)));
if (null == trigger || null == image) {
throw new BuildException("Neither the trigger nor the image may be null!"); //$NON-NLS-1$
}
images.put(trigger, image);
final Element imageNode = element.getChild(Externalization.IMAGE_ELEMENT);
if (null != imageNode) {
image = ImageIO.read(new File(imageNode.getText()));
}
} catch (NullPointerException npe) {
throw new BuildException("The provided image path may not be empty. To add no image, remove the \"image\" element!", npe); //$NON-NLS-1$
} catch (IllegalArgumentException iae) {
......@@ -88,6 +81,33 @@ public abstract class BaseConditionalCoronaBuilder<B extends BaseConditionalCoro
}
}
/**
* Method used to assign a triggering condition in the form of an equation to the
* {@link ConditionalCorona} instance that can be built using this builder.
*
* @param equation
* The equation expressing the trigger.
* @return An instance of this builder for chain calling.
*/
@SuppressWarnings("unchecked")
public B withTrigger(Equation equation) {
this.trigger = equation;
return (B) this;
}
/**
* Method for adding the image to show if the trigger is <code>true</code>.
*
* @param image
* The image to add to the corona.
* @return An instance of this builder for chain calling.
*/
@SuppressWarnings("unchecked")
public B withImage(@SuppressWarnings("hiding") java.awt.Image image) {
this.image = image;
return (B) this;
}
/** {@inheritDoc} */
@Override
public abstract ConditionalCorona build();
......
......@@ -19,6 +19,9 @@ import lu.list.itis.dkd.tui.widget.corona.ConditionalCorona;
import org.jdom2.Element;
/**
* Builder used to initialize a {@link ConditionalCoronaBuilder} instance that can be used to build
* {@link ConditionalCorona} instances.
*
* @author Eric Tobias [eric.tobias@list.lu]
* @since 1.3
* @version 1.3.1
......@@ -26,8 +29,15 @@ import org.jdom2.Element;
public class ConditionalCoronaBuilder extends BaseConditionalCoronaBuilder<ConditionalCoronaBuilder> {
/**
* Constructor forwarding the call to the super constructor.
*
* @param rootElement
* The element harbouring, on child nodes, the necessary information to initialize all
* fields of the builder.
* @throws BuildException
* Exception raised when the building of a corona instance cannot complete successfully
* due to the violation of one or more contracts associated with the instance, including
* missing, incomplete, or erroneous parameters or values thereof
*/
public ConditionalCoronaBuilder(Element rootElement) throws BuildException {
super(rootElement);
......
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