Commit 53ec1984 authored by Nico Mack's avatar Nico Mack

Added conditional Scene content class

parent bedcc13f
package lu.list.itis.dkd.tui.content;
import lu.list.itis.dkd.tui.content.builder.BaseConditionalSceneBuilder;
import lu.list.itis.dkd.tui.cps.InputChangeListener;
import lu.list.itis.dkd.tui.cps.InputEvent;
import lu.list.itis.dkd.tui.cps.system.Declaration;
import lu.list.itis.dkd.tui.cps.system.Equation;
import lu.list.itis.dkd.tui.cps.system.Mapping;
import lu.list.itis.dkd.tui.cps.system.VariableBased;
import lu.list.itis.dkd.tui.cps.system.executor.JavascriptExecutor;
import lu.list.itis.dkd.tui.cps.variable.BooleanVariable;
import lu.list.itis.dkd.tui.cps.variable.Variable;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
// ***************************************************************************
// * Class Definition and Members *
// ***************************************************************************
public class ConditonalScene extends Scene implements VariableBased, InputChangeListener {
private HashMap<String, Variable<?>> parameters;
private String expression;
private Equation condition;
private BooleanVariable satisfied = new BooleanVariable(SATISFIED, false);
// ***************************************************************************
// * Constants *
// ***************************************************************************
private static final String SATISFIED = "Satisfied"; //$NON-NLS-1$
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s)
// ***************************************************************************
// ---------------------------------------------------------------------------
public ConditonalScene(BaseConditionalSceneBuilder<?> builder) {
super(builder);
this.parameters = builder.parameters;
this.expression = builder.expression;
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Primitive(s)
// ***************************************************************************
// ---------------------------------------------------------------------------
private void setupCondition() {
LinkedHashSet<Declaration> inputs = new LinkedHashSet<>();
LinkedHashSet<Declaration> outputs = new LinkedHashSet<>();
for (Variable<?> parameter : parameters.values()) {
inputs.add(new Declaration(parameter));
}
outputs.add(new Declaration(satisfied));
condition = new Equation(this.identifier, new Mapping(inputs, outputs), this.expression);
condition.setExecutor(new JavascriptExecutor());
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body
// ***************************************************************************
// ---------------------------------------------------------------------------
@Override
public void initialise() {
this.setupCondition();
}
// ---------------------------------------------------------------------------
@Override
public List<Variable<?>> connectWithSystemVariables(Map<String, Variable<?>> systemVariables) {
List<Variable<?>> connected = new ArrayList<>();
if (systemVariables != null) {
for (String identifier : parameters.keySet()) {
if (systemVariables.containsKey(identifier)) {
Variable<?> variable = systemVariables.get(identifier);
this.parameters.put(identifier, variable);
connected.add(variable);
variable.addListener(this);
}
}
}
return connected;
}
// ---------------------------------------------------------------------------
@Override
public List<Variable<?>> getDeclaredVariables() {
return new ArrayList<>(parameters.values());
}
// ---------------------------------------------------------------------------
@Override
public void paint(Graphics2D canvas) {
if (this.isVisible()) {
super.paint(canvas);
}
}
// ---------------------------------------------------------------------------
public boolean isVisible() {
return (this.satisfied.getValue() != null) ? this.satisfied.getValue() : false;
}
// ---------------------------------------------------------------------------
@Override
public void inputChanged(InputEvent input) {
this.condition.evaluate();
}
// ---------------------------------------------------------------------------
}
package lu.list.itis.dkd.tui.content.builder;
import lu.list.itis.dkd.dbc.annotation.Nullable;
import lu.list.itis.dkd.tui.bootstrapping.BootstrapCallback;
import lu.list.itis.dkd.tui.bootstrapping.BootstrapContext;
import lu.list.itis.dkd.tui.bootstrapping.BootstrappingUtils;
import lu.list.itis.dkd.tui.content.ConditonalScene;
import lu.list.itis.dkd.tui.cps.utility.EquationSystemBundle;
import lu.list.itis.dkd.tui.cps.variable.NumericalVariable;
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;
import org.jdom2.Element;
import java.util.HashMap;
// ***************************************************************************
// * Class Definition and Members *
// ***************************************************************************
public abstract class BaseConditionalSceneBuilder<B extends BaseConditionalSceneBuilder<B>> extends BaseSceneBuilder<B> {
public HashMap<String, Variable<?>> parameters;
public String expression;
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s) *
// ***************************************************************************
// ---------------------------------------------------------------------------
public BaseConditionalSceneBuilder() {
parameters = new HashMap<>();
expression = Externalization.EMPTY_STRING;
drawPriority = 0;
}
// ---------------------------------------------------------------------------
/**
* Constructor initializing all fields from an {@link Element} containing as child elements all the
* information on fields to initialize. This constructor will add all associated handles as keys in
* the position map.
*
* @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 BaseConditionalSceneBuilder(Element rootElement) throws BuildException {
super(rootElement);
this.buildFromBootstrap(rootElement, null, null);
}
// ---------------------------------------------------------------------------
/**
* Constructor initializing all fields from an {@link Element} containing as child elements all the
* information on fields to initialize. This constructor will add all associated handles as keys in
* the position map.
*
* @param rootElement
* The element harbouring, on child nodes, the necessary information to initialize all fields
* of the builder.
* @param context
* @param callback
* @throws BuildException
* Thrown when any of the fields fail to populate due to an error in reading information
* from the XML file.
*/
// ---------------------------------------------------------------------------
public BaseConditionalSceneBuilder(Element rootElement, BootstrapContext context, BootstrapCallback callback) throws BuildException {
super(rootElement, context, callback);
this.buildFromBootstrap(rootElement, context, callback);
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Primitive(s) *
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
*
* @param rootElement
* @param context
* @param callback
* @throws BuildException
*/
// ---------------------------------------------------------------------------
private void buildFromBootstrap(@Nullable Element rootElement, BootstrapContext context, BootstrapCallback callback) throws BuildException {
drawPriority = BootstrappingUtils.getContentAsInteger(rootElement, Externalization.DRAW_PRIORITY_NODE, BootstrappingUtils.OPTIONAL, 0, context);
expression = BootstrappingUtils.getContentAsString(rootElement, Externalization.TRIGGER_CONDITION_NODE, BootstrappingUtils.MANDATORY, null, context);
parameters = new HashMap<>();
for (Element parameterNode : rootElement.getChild(EquationSystemBundle.PARAMETERS_ELEMENT).getChildren(EquationSystemBundle.PARAMETER_ELEMENT)) {
String identifier = BootstrappingUtils.getAttributeAsString(parameterNode, EquationSystemBundle.NAME_ATTRIBUTE, BootstrappingUtils.MANDATORY, null);
double initial = BootstrappingUtils.getAttributeAsDouble(parameterNode, EquationSystemBundle.INITIAL_ATTRIBUTE, BootstrappingUtils.OPTIONAL, 0d);
parameters.put(identifier, new NumericalVariable(identifier, Externalization.EMPTY_STRING, initial));
}
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body *
// ***************************************************************************
// ---------------------------------------------------------------------------
public abstract ConditonalScene build() throws BuildException;
// ---------------------------------------------------------------------------
// ***************************************************************************
// * End of Class *
// ***************************************************************************
// ---------------------------------------------------------------------------
}
package lu.list.itis.dkd.tui.content.builder;
import org.jdom2.Element;
import lu.list.itis.dkd.tui.bootstrapping.BootstrapCallback;
import lu.list.itis.dkd.tui.bootstrapping.BootstrapContext;
import lu.list.itis.dkd.tui.content.ConditonalScene;
import lu.list.itis.dkd.tui.exception.BuildException;
public class ConditionalSceneBuilder extends BaseConditionalSceneBuilder <ConditionalSceneBuilder> {
// ***************************************************************************
// * Constants *
// ***************************************************************************
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s) *
// ***************************************************************************
// ---------------------------------------------------------------------------
public ConditionalSceneBuilder() {
super();
}
// ---------------------------------------------------------------------------
public ConditionalSceneBuilder(Element rootElement) throws BuildException {
super(rootElement);
}
// ---------------------------------------------------------------------------
public ConditionalSceneBuilder(Element rootElement, BootstrapContext context, BootstrapCallback callback) throws BuildException {
super(rootElement, context, callback);
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Primitives *
// ***************************************************************************
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body *
// ***************************************************************************
// ---------------------------------------------------------------------------
@Override
public ConditonalScene build() throws BuildException {
return new ConditonalScene(this);
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * End of Class *
// ***************************************************************************
// ---------------------------------------------------------------------------
}
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