Commit 0ae3a5f7 authored by Nico Mack's avatar Nico Mack

Implemented handling of MapVariables in PythonExecutor

General overhaul of java<=>python type coercion
parent 1bdfd4dd
'''
Created on Jan 5, 2018
@author: mack
'''
def ManipulateMap (M):
A = M
A["intField"]+=10
A["doubleField"]*=2
A["booleanField"] = not A["booleanField"]
A["stringField"] += " had a very big fall"
A["listField"].append(17)
A["listField"].append(19)
A["listField"].append(23)
'''print A["intField"]
print A["doubleField"]
print A["booleanField"]
print A["stringField"]
print A["listField"]'''
return A
......@@ -31,7 +31,7 @@ import java.util.concurrent.ConcurrentHashMap;
// * Class Definition and Members *
// ***************************************************************************
public class MapVariable<V, K> extends Variable<Map<K, V>> implements Map<K, V> {
public class MapVariable<K, V> extends Variable<Map<K, V>> implements Map<K, V> {
// ---------------------------------------------------------------------------
// ***************************************************************************
......@@ -178,7 +178,7 @@ public class MapVariable<V, K> extends Variable<Map<K, V>> implements Map<K, V>
@Override
public Variable<Map<K, V>> clone() {
MapVariable<V, K> cloned = new MapVariable<V, K>(this.name);
MapVariable<K, V> cloned = new MapVariable<>(this.name);
cloned.putAll(value);
return cloned;
}
......
......@@ -379,6 +379,22 @@ public class NumericalVariable extends Variable<Double> {
return this.normalizedValue;
}
// ---------------------------------------------------------------------------
/**
* Returns the constrained value of this variable. The constrained value is guaranteed to be in the
* range MIN <= x <= MAX, rounded to the nearest value as determined by the property number of
* decimals.
*
* @return the constrained value, i.e MIN <= x <= MAX rounded to the precision determined the number
* of decimals property.
* @see #setNumberOfDecimals
*/
// ---------------------------------------------------------------------------
public double getConstrainedValue() {
return this.normalizedValue * this.valueRange;
}
// ---------------------------------------------------------------------------
/**
*
......
package lu.list.itis.dkd.tui.marker;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import lu.list.itis.dkd.tui.content.InformationProvider;
import lu.list.itis.dkd.tui.content.InformationReceiver;
import lu.list.itis.dkd.tui.cps.variable.Variable;
......@@ -10,21 +7,19 @@ import lu.list.itis.dkd.tui.marker.builder.BaseInfoMarkerBuilder;
import lu.list.itis.dkd.tui.marker.builder.InfoMarkerBuilder;
import lu.list.itis.dkd.tui.utility.Point;
//***************************************************************************
//* Class Definition and Members *
//***************************************************************************
// ***************************************************************************
// * Class Definition and Members *
// ***************************************************************************
public class InfoMarker<T> extends TetherableMarker implements InformationReceiver<T>, InformationProvider<T> {
private T information;
private Variable<T> variable;
private T information;
private Variable<T> variable;
// ***************************************************************************
// * Constants *
// ***************************************************************************
// private static Logger LOGGER = LoggerFactory.getLogger(InfoMarker.class.getSimpleName());
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s) *
......@@ -32,11 +27,11 @@ public class InfoMarker<T> extends TetherableMarker implements InformationReceiv
// ---------------------------------------------------------------------------
@SuppressWarnings("unchecked")
public InfoMarker(BaseInfoMarkerBuilder<InfoMarkerBuilder> builder) {
super(builder);
this.information = (T) builder.information;
this.variable = (Variable<T>) builder.variable;
}
public InfoMarker(BaseInfoMarkerBuilder<InfoMarkerBuilder> builder) {
super(builder);
this.information = (T) builder.information;
this.variable = (Variable<T>) builder.variable;
}
// ---------------------------------------------------------------------------
// ***************************************************************************
......@@ -50,32 +45,31 @@ public class InfoMarker<T> extends TetherableMarker implements InformationReceiv
// ***************************************************************************
// ---------------------------------------------------------------------------
@Override
public T getInformation(Point position) {
return this.getInformation();
}
// ---------------------------------------------------------------------------
@Override
public T getInformation() {
if (this.variable != null) {
return this.variable.getValue();
} else {
return this.information;
}
}
// ---------------------------------------------------------------------------
@Override
public void setInformation(T information) {
if (this.variable != null) {
this.variable.setValue(information);
} else {
this.information = information;
}
}
@Override
public T getInformation(Point position) {
return this.getInformation();
}
// ---------------------------------------------------------------------------
@Override
public T getInformation() {
if (this.variable != null) {
return this.variable.getValue();
}
return this.information;
}
// ---------------------------------------------------------------------------
@Override
public void setInformation(T information) {
if (this.variable != null) {
this.variable.setValue(information);
} else {
this.information = information;
}
}
// ---------------------------------------------------------------------------
......
......@@ -14,8 +14,10 @@
package lu.list.itis.dkd.tui.cps.system.executor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import lu.list.itis.dkd.tui.cps.system.Import;
import lu.list.itis.dkd.tui.cps.variable.MapVariable;
import lu.list.itis.dkd.tui.cps.variable.SpatialVariable;
import lu.list.itis.dkd.tui.cps.variable.TextVariable;
import lu.list.itis.dkd.tui.utility.Point;
......@@ -106,4 +108,43 @@ public class PythonExecutorTest {
assertEquals("Humpty Dumpty", textC.getValue());
}
@Test
@SuppressWarnings("nls")
public void manipulateMapVariableTest() {
List<Double> primes = new ArrayList<>();
primes.add(2d);
primes.add(3d);
primes.add(5d);
primes.add(7d);
primes.add(11d);
primes.add(13d);
MapVariable<String, Object> map = new MapVariable<>("MyMap");
map.put("intField", Integer.valueOf(14));
map.put("doubleField", Double.valueOf(3.14));
map.put("booleanField", Boolean.TRUE);
map.put("stringField", "Humpty Dumpty");
map.put("listField", primes);
MapVariable<String, Object> result = new MapVariable<>("NewMap");
List<Import> imports = new ArrayList<>();
Import funtion = new Import("ManipulateMap", "Map");
imports.add(funtion);
PythonExecutor executor = new PythonExecutor(this.properties);
executor.resolve(imports);
executor.set(map);
executor.eval("NewMap = ManipulateMap(MyMap)");
executor.get(result);
assertEquals(24, result.get("intField"));
assertEquals(6.28d, (double) result.get("doubleField"), 0.0001);
assertEquals(Boolean.FALSE, result.get("booleanField"));
assertEquals("Humpty Dumpty had a very big fall", result.get("stringField"));
assertTrue(((List<?>) result.get("listField")).contains(19d));
assertTrue(((List<?>) result.get("listField")).contains(23d));
}
}
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