Skip to content

Commit

Permalink
Addresses #3006 by implementing a different "check in among" for floats
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Jul 5, 2020
1 parent b3d34ed commit 507895f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 48 deletions.
Expand Up @@ -14,6 +14,8 @@
import java.util.List;
import java.util.Set;

import com.vividsolutions.jts.util.NumberUtil;

import msi.gama.common.interfaces.IKeyword;
import msi.gama.common.util.StringUtils;
import msi.gama.precompiler.GamlAnnotations.doc;
Expand Down Expand Up @@ -343,11 +345,15 @@ public void setAndVerifyValue(final IScope scope, final Object val) {
}
}
}
if (getAmongValue(scope) != null && !getAmongValue(scope).isEmpty()) {
if (!getAmongValue(scope).contains(newValue)) {
newValue = getAmongValue(scope).get(0);
}
}

// See #3006
// final List among = getAmongValue(scope);
// if (among != null && !among.isEmpty()) {
// if (!getAmongValue(scope).contains(newValue)) {
// newValue = getAmongValue(scope).get(0);
// }
// }
newValue = filterWithAmong(scope, newValue);
if (value != UNDEFINED) {
for (final ParameterChangeListener listener : listeners) {
listener.changed(scope, newValue);
Expand All @@ -363,11 +369,29 @@ public void setAndVerifyValue(final IScope scope, final Object val) {
value = newValue;
}

private Object filterWithAmong(final IScope scope, final Object newValue) {
getAmongValue(scope);
if (amongValue == null || amongValue.isEmpty()) { return newValue; }
if (Types.FLOAT.equals(this.getType())) {
final double newDouble = Cast.asFloat(scope, newValue);
for (final Object o : amongValue) {
final Double d = Cast.asFloat(scope, o);
final Double tolerance = 0.0000001d;
if (NumberUtil.equalsWithTolerance(d, newDouble, tolerance)) { return d; }
}

} else {
if (amongValue.contains(newValue)) { return newValue; }
}
return amongValue.get(0);
}

@Override
public void setValue(final IScope scope, final Object val) {
if (val == UNDEFINED) {
if (getAmongValue(scope) != null) {
value = getAmongValue(scope).get(scope.getRandom().between(0, getAmongValue(scope).size() - 1));
getAmongValue(scope);
if (amongValue != null) {
value = amongValue.get(scope.getRandom().between(0, amongValue.size() - 1));
} else if (type.id() == IType.INT || type.id() == IType.FLOAT) {
value = drawRandomValue(scope);
} else if (type.id() == IType.BOOL) {
Expand Down
74 changes: 33 additions & 41 deletions msi.gama.headless/src/msi/gama/headless/common/DataTypeFactory.java
@@ -1,59 +1,51 @@
/*********************************************************************************************
*
*
* 'DataTypeFactory.java', in plugin 'msi.gama.headless', is part of the source code of the
* GAMA modeling and simulation platform.
* (v. 1.8.1)
*
* 'DataTypeFactory.java', in plugin 'msi.gama.headless', is part of the source code of the GAMA modeling and simulation
* platform. (v. 1.8.1)
*
* (c) 2007-2020 UMI 209 UMMISCO IRD/UPMC & Partners
*
*
* Visit https://github.com/gama-platform/gama for license information and developers contact.
*
*
*
*
**********************************************************************************************/
package msi.gama.headless.common;

//gama.getexperiment.getOutputManager...
// gama.getexperiment.getOutputManager...

public final class DataTypeFactory {

public static DataType getObjectMetaData(Object val)
{

public static DataType getObjectMetaData(final Object val) {
DataType type;
if(val==null)
{
return DataType.UNDEFINED;
if (val == null) { return DataType.UNDEFINED; }
if (val instanceof Integer || val instanceof Long) {
type = DataType.INT;
} else if (val instanceof Float || val instanceof Double) {
type = DataType.FLOAT;
} else if (val instanceof Boolean) {
type = DataType.BOOLEAN;
} else if (val instanceof String) {
type = DataType.STRING;
/*
* else if(val instanceof pams.common.dataTypes.Combined ) type=DataType.COMBINED;
*/
/*
* else if(val instanceof Display2D) type=DataType.DISPLAY2D;
*/
} else {
type = DataType.UNDEFINED;
}
if(val instanceof Integer||val instanceof Long)
type=DataType.INT;
else if(val instanceof Float||val instanceof Double)
type=DataType.FLOAT;
else if(val instanceof Boolean)
type=DataType.BOOLEAN;
else if(val instanceof String )
type=DataType.STRING;
/*else if(val instanceof pams.common.dataTypes.Combined )
type=DataType.COMBINED;*/
/*else if(val instanceof Display2D)
type=DataType.DISPLAY2D;
*/

else type=DataType.UNDEFINED;
return type;
}

public static Object getObjectFromText(String val, DataType t)
{
if(t.equals(DataType.INT))
return Integer.valueOf(val);
if(t.equals(DataType.BOOLEAN))
return Boolean.valueOf(val);
if(t.equals(DataType.FLOAT))
return Float.valueOf(val);
if(t.equals(DataType.STRING))
return val;

public static Object getObjectFromText(final String val, final DataType t) {
if (t.equals(DataType.INT)) { return Integer.valueOf(val); }
if (t.equals(DataType.BOOLEAN)) { return Boolean.valueOf(val); }
// See #3006
if (t.equals(DataType.FLOAT)) { return Double.valueOf(val); }
if (t.equals(DataType.STRING)) { return val; }
return val;
}


}

0 comments on commit 507895f

Please sign in to comment.