Skip to content

Commit

Permalink
Added ScalarValue class
Browse files Browse the repository at this point in the history
  • Loading branch information
vruusmann committed Feb 16, 2019
1 parent 42b3a44 commit 6676312
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 148 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,11 @@ Core types:
* Class `org.jpmml.evaluator.OutputField` - Describes a secondary model result field
* Abstract class `org.jpmml.evaluator.FieldValue`
* Class `org.jpmml.evaluator.CollectionValue`
* Class `org.jpmml.evaluator.ContinuousValue`
* Abstract class `org.jpmml.evaluator.DiscreteValue`
* Class `org.jpmml.evaluator.CategoricalValue`
* Class `org.jpmml.evaluator.OrdinalValue`
* Abstract class `org.jpmml.evaluator.ScalarValue`
* Class `org.jpmml.evaluator.ContinuousValue`
* Abstract class `org.jpmml.evaluator.DiscreteValue`
* Class `org.jpmml.evaluator.CategoricalValue`
* Class `org.jpmml.evaluator.OrdinalValue`
* Utility class `org.jpmml.evaluator.EvaluatorUtil`
* Utility class `org.jpmml.evaluator.FieldValueUtil`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public Boolean evaluate(EvaluationContext context){
return null;
}

return evaluate(value.compareTo(getValue()));
return evaluate(value.compareToValue(getValue()));
}

public FieldValue getValue(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public FieldValue create(DataType dataType, Object value){
}

static
private class CategoricalString extends CategoricalValue implements Scalar {
private class CategoricalString extends CategoricalValue {

CategoricalString(Object value){
super(DataType.STRING, value);
Expand All @@ -75,7 +75,7 @@ public String asString(){
}

static
private class CategoricalBoolean extends CategoricalValue implements Scalar {
private class CategoricalBoolean extends CategoricalValue {

CategoricalBoolean(Object value){
super(DataType.BOOLEAN, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ public class CollectionValue extends FieldValue {
}

CollectionValue(DataType dataType, OpType opType, List<?> ordering, Collection<?> value){
super(dataType, Objects.requireNonNull(value));
super(dataType, value);

opType = Objects.requireNonNull(opType);

setOpType(opType);
setOpType(Objects.requireNonNull(opType));

switch(opType){
case CONTINUOUS:
Expand All @@ -59,6 +57,16 @@ public class CollectionValue extends FieldValue {
}
}

@Override
public int compareToString(String string){
throw new EvaluationException("Collection value cannot be used in comparison operations");
}

@Override
public int compareToValue(FieldValue value){
throw new EvaluationException("Collection value cannot be used in comparison operations");
}

@Override
public int hashCode(){
List<?> ordering = getOrdering();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.dmg.pmml.DataType;
import org.dmg.pmml.OpType;

public class ContinuousValue extends FieldValue {
public class ContinuousValue extends ScalarValue {

ContinuousValue(DataType dataType, Object value){
super(dataType, value);
Expand Down Expand Up @@ -77,7 +77,7 @@ public FieldValue create(DataType dataType, Object value){
}

static
private class ContinuousInteger extends ContinuousValue implements Scalar {
private class ContinuousInteger extends ContinuousValue {

ContinuousInteger(Object value){
super(DataType.INTEGER, value);
Expand All @@ -95,7 +95,7 @@ public Integer asInteger(){
}

static
private class ContinuousFloat extends ContinuousValue implements Scalar {
private class ContinuousFloat extends ContinuousValue {

ContinuousFloat(Object value){
super(DataType.FLOAT, value);
Expand All @@ -113,7 +113,7 @@ public Float asFloat(){
}

static
private class ContinuousDouble extends ContinuousValue implements Scalar {
private class ContinuousDouble extends ContinuousValue {

ContinuousDouble(Object value){
super(DataType.DOUBLE, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.dmg.pmml.DataType;

abstract
public class DiscreteValue extends FieldValue {
public class DiscreteValue extends ScalarValue {

DiscreteValue(DataType dataType, Object value){
super(dataType, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public List<String> getWarnings(){
return this.warnings;
}

private static final FieldValue UNDECLARED_VALUE = new FieldValue(DataType.DOUBLE, Double.NaN){
private static final FieldValue UNDECLARED_VALUE = new ScalarValue(DataType.DOUBLE, Double.NaN){

@Override
public OpType getOpType(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,9 @@ public FieldValue evaluateAggregate(Aggregate aggregate, EvaluationContext conte
case AVERAGE:
return Functions.AVG.evaluate((List<FieldValue>)values);
case MIN:
return Collections.min((List<FieldValue>)values);
return Collections.min((List<ScalarValue>)values);
case MAX:
return Collections.max((List<FieldValue>)values);
return Collections.max((List<ScalarValue>)values);
default:
throw new UnsupportedAttributeException(aggregate, function);
}
Expand Down
126 changes: 13 additions & 113 deletions pmml-evaluator/src/main/java/org/jpmml/evaluator/FieldValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,23 @@
* @see FieldValueUtil
*/
abstract
public class FieldValue implements TypeInfo, Comparable<FieldValue>, Serializable {
public class FieldValue implements TypeInfo, Serializable {

private DataType dataType = null;

private Object value = null;


FieldValue(DataType dataType, Object value){
dataType = Objects.requireNonNull(dataType);
setDataType(Objects.requireNonNull(dataType));
setValue(Objects.requireNonNull(value));
}

if(value instanceof Collection){
// Ignored
} else
abstract
public int compareToString(String string);

{
value = filterValue(TypeUtil.parseOrCast(dataType, Objects.requireNonNull(value)));
}

setDataType(dataType);
setValue(value);
}
abstract
public int compareToValue(FieldValue value);

public FieldValue cast(DataType dataType, OpType opType){
boolean compatible = true;
Expand Down Expand Up @@ -138,7 +134,7 @@ public boolean equals(HasValue<?> hasValue){
public boolean equals(HasParsedValue<?> hasParsedValue){
FieldValue value = hasParsedValue.getValue(this);

return this.equals(value);
return equalsValue(value);
}

/**
Expand Down Expand Up @@ -168,6 +164,7 @@ public boolean isIn(HasValueSet<?> hasValueSet){
public boolean isIn(HasParsedValueSet<?> hasParsedValueSet){
Set<FieldValue> values = hasParsedValueSet.getValueSet(this);

// XXX
return values.contains(this);
}

Expand All @@ -190,7 +187,7 @@ public int compareTo(HasValue<?> hasValue){
public int compareTo(HasParsedValue<?> hasParsedValue){
FieldValue value = hasParsedValue.getValue(this);

return this.compareTo(value);
return compareToValue(value);
}

public boolean equalsString(String string){
Expand All @@ -199,17 +196,7 @@ public boolean equalsString(String string){
return (getValue()).equals(value);
}

/**
* <p>
* A value-safe replacement for {@link #equals(Object)}.
* </p>
*/
public boolean equalsValue(FieldValue value){

if(sameScalarType(value)){
return (getValue()).equals(value.getValue());
}

return equalsValue(value.getValue());
}

Expand Down Expand Up @@ -237,51 +224,12 @@ public boolean test(FieldValue value){
.anyMatch(predicate);
}

public int compareToString(String string){
Object value = TypeUtil.parse(getDataType(), string);

return ((Comparable)getValue()).compareTo(value);
}

/**
* <p>
* A value-safe replacement for {@link #compareTo(FieldValue)}
* </p>
*/
public int compareToValue(FieldValue value){

if(sameScalarType(value)){
return ((Comparable)getValue()).compareTo(value.getValue());
}

return compareToValue(value.getValue());
}

private int compareToValue(Object value){
value = TypeUtil.parseOrCast(getDataType(), value);

return ((Comparable)getValue()).compareTo(value);
}

public <V> V getMapping(HasParsedValueMapping<V> hasParsedValueMapping){
Map<FieldValue, V> values = hasParsedValueMapping.getValueMapping(this);

return values.get(this);
}

private boolean isScalar(){
return (this instanceof Scalar);
}

private boolean sameScalarType(FieldValue value){

if(isScalar()){
return (getClass()).equals(value.getClass());
}

return false;
}

public String asString(){
return (String)getValue(DataType.STRING);
}
Expand All @@ -293,7 +241,7 @@ public Number asNumber(){
return (Number)value;
}

return (Double)getValue(DataType.DOUBLE);
return (Number)getValue(DataType.DOUBLE);
}

public Integer asInteger(){
Expand Down Expand Up @@ -394,16 +342,6 @@ private Object getValue(DataType dataType){
}
}

@Override
public int compareTo(FieldValue that){

if((this.getOpType() != that.getOpType()) || (this.getDataType() != that.getDataType())){
throw new ClassCastException();
}

return compareToValue(that);
}

@Override
public int hashCode(){
return (31 * (getOpType().hashCode() ^ getDataType().hashCode())) + getValue().hashCode();
Expand All @@ -415,7 +353,7 @@ public boolean equals(Object object){
if(object instanceof FieldValue){
FieldValue that = (FieldValue)object;

return (this.getOpType() == that.getOpType()) && (this.getDataType() == that.getDataType()) && (this.getValue()).equals(that.getValue());
return (this.getOpType()).equals(that.getOpType()) && (this.getDataType()).equals(that.getDataType()) && (this.getValue()).equals(that.getValue());
}

return false;
Expand Down Expand Up @@ -506,42 +444,4 @@ private String ensureValue(HasValue<?> hasValue){

return value;
}

static
private Object filterValue(Object value){

if(value instanceof Float){
return filterValue((Float)value);
} else

if(value instanceof Double){
return filterValue((Double)value);
}

return value;
}

static
private Float filterValue(Float value){

if(value.doubleValue() == 0f){
return Numbers.FLOAT_ZERO;
}

return value;
}

static
private Double filterValue(Double value){

if(value.doubleValue() == 0d){
return Numbers.DOUBLE_ZERO;
}

return value;
}

static
interface Scalar {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,11 @@ private void verify(Object expected, Object actual, double precision, double zer
return;
} // End if

if(!(actual instanceof Collection)){
if(actual instanceof Collection){
// Ignored
} else

{
DataType dataType = TypeUtil.getDataType(actual);

expected = TypeUtil.parseOrCast(dataType, expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private int compare(List<?> ordering, Object left, Object right){
}

static
private class OrdinalString extends OrdinalValue implements Scalar {
private class OrdinalString extends OrdinalValue {

OrdinalString(List<?> ordering, Object value){
super(DataType.STRING, ordering, value);
Expand Down
Loading

0 comments on commit 6676312

Please sign in to comment.