Commit 336e9f8c authored by Nico Mack's avatar Nico Mack

Implementation of MapVariable

Extension of VariableBased Interface
parent 1cf4b590
......@@ -223,6 +223,18 @@ public class EquationSystemBuilder {
variable.setValue(variable.valueFromString(value));
break;
case EquationSystemBundle.MAP_TYPE:
variableClass = properties.getProperty(EquationSystemBundle.MAP_VARIABLE_CLASS);
variable = (Variable<T>) Class.forName(variableClass)
.getConstructor(String.class)
.newInstance(name);
if (!Strings.isNullOrEmpty(value))
variable.setValue(variable.valueFromString(value));
break;
}
} catch (Exception exception) {
LOGGER.error("Failed to instantiate variable class {}!", variableClass, exception); //$NON-NLS-1$
......
......@@ -26,4 +26,6 @@ import java.util.Map;
public interface VariableBased {
public List<Variable<?>> connectWithSystemVariables(Map<String, Variable<?>> systemVariables);
public List<Variable<?>> getDeclaredVariables();
}
......@@ -40,6 +40,7 @@ public class EquationSystemBundle extends NLS {
public static String TEXT_VARIABLE_CLASS;
public static String SPATIAL_VARIABLE_CLASS;
public static String VECTOR_VARIABLE_CLASS;
public static String MAP_VARIABLE_CLASS;
public static String EQUATION_ELEMENT;
public static String MODE_ATTRIBUTE;
......@@ -77,6 +78,7 @@ public class EquationSystemBundle extends NLS {
public static final String SPATIAL_TYPE = "spatial";
public static final String TEXT_TYPE = "text";
public static final String VECTOR_TYPE = "vector";
public static final String MAP_TYPE = "map";
public static final String ISOLATED_VALUE = "isolated";
......
......@@ -4,6 +4,7 @@ LOGIC_VARIABLE_CLASS=logic.variable.class
TEXT_VARIABLE_CLASS=text.variable.class
SPATIAL_VARIABLE_CLASS=spatial.variable.class
VECTOR_VARIABLE_CLASS=vector.variable.class
MAP_VARIABLE_CLASS=map.variable.class
EXECUTOR_NAMESPACE=lu.list.itis.dkd.tui.cps.system.executor
NAMESPACE_SEPARATOR=.
......
/**
* Copyright Luxembourg Institute of Science and Technology, 2017. All rights reserved. If you wish
* to use this code for any purpose, please contact the author(s).
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package lu.list.itis.dkd.tui.cps.variable;
import lu.list.itis.dkd.tui.cps.utility.EquationSystemBundle;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author mack
* @since [major].[minor]
* @version [major].[minor].[micro]
* @param <V>
* @param <K>
*/
// ***************************************************************************
// * Class Definition and Members *
// ***************************************************************************
public class MapVariable<V, K> extends Variable<Map<K, V>> implements Map<K, V> {
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s) *
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* @param type
* @param name
* @param unit
*/
// ---------------------------------------------------------------------------
public MapVariable(String name) {
super(EquationSystemBundle.MAP_TYPE, name, null);
value = new ConcurrentHashMap<K, V>();
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body *
// ***************************************************************************
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public int size() {
return value.size();
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public boolean isEmpty() {
return value.isEmpty();
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public boolean containsKey(Object key) {
return value.containsKey(key);
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public boolean containsValue(Object soughtValue) {
return value.containsValue(soughtValue);
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public V get(Object key) {
return value.get(key);
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public V put(K key, V newValue) {
return value.put(key, newValue);
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public V remove(Object key) {
return value.remove(key);
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public void putAll(Map<? extends K, ? extends V> subMap) {
value.putAll(subMap);
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public void clear() {
value.clear();
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public Set<K> keySet() {
return value.keySet();
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public Collection<V> values() {
return value.values();
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public Set<Entry<K, V>> entrySet() {
return value.entrySet();
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public void setValueFromObject(Object object) {
// TODO Auto-generated method stub
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public Variable<Map<K, V>> clone() {
MapVariable<V, K> cloned = new MapVariable<V, K>(this.name);
cloned.putAll(value);
return cloned;
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public Map<K, V> valueFromString(String stringValue) {
// TODO Auto-generated method stub
return null;
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public Map<K, V> valueFromObject(Object objectValue) {
// TODO Auto-generated method stub
return null;
}
}
......@@ -392,7 +392,7 @@ public abstract class Variable<V> implements Cloneable {
if (variable.getClass().isAssignableFrom(clazz)) {
casted = (V) variable;
} else {
LOGGER.error("Variable {} of type {} could not be cast to NumericalVariable!", variable.getName(), variable.getClass().getSimpleName()); //$NON-NLS-1$
LOGGER.error("Variable {} of type {} could not be cast to {}!", variable.getName(), variable.getClass().getSimpleName(), clazz.getName()); //$NON-NLS-1$
}
return casted;
}
......
......@@ -60,30 +60,6 @@ public class DisplayMarker extends InfoMarker implements InputChangeListener, Va
// ***************************************************************************
// ---------------------------------------------------------------------------
// public void setVariables(Collection<Variable<?>> newVariables) {
// Preconditions.checkArgument(variables == null, "Set of variables can't be null!"); //$NON-NLS-1$
//
// this.variables = new HashMap<String, NumericalVariable>();
// for (Variable<?> variable : newVariables) {
// this.variables.put(variable.getName(), (NumericalVariable) variable);
// }
//
// List<ValueCorona> displays = this.getCoronas(ValueCorona.class);
// for (ValueCorona display : displays) {
// Variable<?> variable = display.getVariable();
// if (variable != null) {
// String variableName = variable.getName();
// if (this.variables.containsKey(variableName)) {
// display.setVariable(this.variables.get(variableName));
// } else {
// LOGGER.warn("Corona requires variable {} unkown to display widget!", variableName); //$NON-NLS-1$
// }
// }
// }
// }
// ---------------------------------------------------------------------------
public Collection<Variable<?>> getVariables() {
Collection<Variable<?>> variables = new ArrayList<>();
this.dispatcher.clear();
......@@ -144,5 +120,17 @@ public class DisplayMarker extends InfoMarker implements InputChangeListener, Va
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public List<Variable<?>> getDeclaredVariables() {
List<Variable<?>> declared = new ArrayList<>();
for (Variable<?> variable : this.variables.values()) {
declared.add(variable);
}
return declared;
}
}
......@@ -74,10 +74,6 @@ public class DisplayWidget extends StatefulWidget implements InputChangeListener
this.setupDispatcher();
this.variables = new HashMap<>();
// if (builder.variables != null) {
// this.setVariables(builder.variables);
// }
}
// ---------------------------------------------------------------------------
......@@ -96,14 +92,16 @@ public class DisplayWidget extends StatefulWidget implements InputChangeListener
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* iterates over all value coronas assigned to this widget and populates the dispatcher table,
* using variable names as keys and associating depending coronas as values
* iterates over all value coronas assigned to this widget and populates the dispatcher table, using
* variable names as keys and associating depending coronas as values
*/
// ---------------------------------------------------------------------------
private void setupDispatcher() {
this.dispatcher = TreeMultimap.create();
// TODO : Should be refactored using VariableBased Interface
List<ValueCorona> displays = this.getCoronas(ValueCorona.class);
for (ValueCorona display : displays) {
Variable<?> variable = display.getVariable();
......@@ -169,33 +167,18 @@ public class DisplayWidget extends StatefulWidget implements InputChangeListener
}
// ---------------------------------------------------------------------------
// @Deprecated
// public void setVariables(Collection<Variable<?>> newVariables) {
// Preconditions.checkArgument(newVariables != null, "Set of variables can't be null!");
// //$NON-NLS-1$
//
// assert newVariables != null;
//
// this.variables = new HashMap<>();
// for (Variable<?> variable : newVariables) {
// this.variables.put(variable.getName(), (NumericalVariable) variable);
// variable.addListener(this);
// }
//
// List<ValueCorona> displays = this.getCoronas(ValueCorona.class);
// for (ValueCorona display : displays) {
// Variable<?> variable = display.getVariable();
// if (variable != null) {
// String variableName = variable.getName();
// if (this.variables.containsKey(variableName)) {
// display.setVariable(this.variables.get(variableName));
// } else {
// LOGGER.warn("Corona requires variable {} unkown to display widget!", variableName);
// //$NON-NLS-1$
// }
// }
// }
// }
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public List<Variable<?>> getDeclaredVariables() {
List<Variable<?>> declared = new ArrayList<>();
for (Variable<?> variable : this.variables.values()) {
declared.add(variable);
}
return declared;
}
// ---------------------------------------------------------------------------
......@@ -212,6 +195,7 @@ public class DisplayWidget extends StatefulWidget implements InputChangeListener
}
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * End of Class *
......
......@@ -182,6 +182,20 @@ public class ListWidget extends TetherableWidget implements InformationProvider<
return connected;
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public List<Variable<?>> getDeclaredVariables() {
List<Variable<?>> declared = new ArrayList<>();
for (Variable<?> variable : this.variables.values()) {
declared.add(variable);
}
return declared;
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
......
......@@ -375,6 +375,17 @@ public class ValueWidget extends TetherableWidget implements InformationProvider
return connected;
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public List<Variable<?>> getDeclaredVariables() {
List<Variable<?>> declared = new ArrayList<>();
declared.add(variable);
return declared;
}
// ---------------------------------------------------------------------------
@Override
......
......@@ -115,6 +115,17 @@ public class ValueCorona extends SelectableCorona implements InformationReceiver
return connected;
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public List<Variable<?>> getDeclaredVariables() {
List<Variable<?>> declared = new ArrayList<>();
declared.add(this.variable);
return declared;
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * End of Class *
......
......@@ -147,13 +147,24 @@ public class ValueRangeCorona extends SelectableCorona implements InformationRec
}
// ---------------------------------------------------------------------------
@Override
// ---------------------------------------------------------------------------
public List<Variable<?>> getDeclaredVariables() {
List<Variable<?>> declared = new ArrayList<>();
declared.add(this.lowerBoundVariable);
declared.add(this.upperBoundVariable);
return declared;
}
// ---------------------------------------------------------------------------
@Override
// ---------------------------------------------------------------------------
public ValueRangeCorona clone() {
return new ValueRangeCorona(this);
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * End of Class *
......
......@@ -117,6 +117,19 @@ public class VariableHtmlBox extends HtmlBox implements VariableBased, InputChan
}
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public List<Variable<?>> getDeclaredVariables() {
List<Variable<?>> declared = new ArrayList<>();
for (Variable<?> parameter : this.parameters.values()) {
declared.add(parameter);
}
return declared;
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * 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