2019-08-09 10:08:34 +02:00
package eu.dnetlib.pace.tree.support ;
2019-12-13 11:30:02 +01:00
import com.fasterxml.jackson.databind.ObjectMapper ;
2019-10-29 16:38:42 +01:00
import eu.dnetlib.pace.config.Config ;
2019-08-09 10:08:34 +02:00
import eu.dnetlib.pace.config.PaceConfig ;
import eu.dnetlib.pace.model.MapDocument ;
import eu.dnetlib.pace.util.PaceException ;
2019-12-13 11:30:02 +01:00
2019-08-09 10:08:34 +02:00
import java.io.IOException ;
import java.io.Serializable ;
import java.util.List ;
public class TreeNodeDef implements Serializable {
private List < FieldConf > fields ;
private AggType aggregation ;
private double threshold ;
private String positive ;
private String negative ;
private String undefined ;
2019-11-07 12:47:12 +01:00
boolean ignoreUndefined ;
2019-08-09 10:08:34 +02:00
2019-11-07 12:47:12 +01:00
public TreeNodeDef ( List < FieldConf > fields , AggType aggregation , double threshold , String positive , String negative , String undefined , boolean ignoreUndefined ) {
2019-08-09 10:08:34 +02:00
this . fields = fields ;
this . aggregation = aggregation ;
this . threshold = threshold ;
this . positive = positive ;
this . negative = negative ;
this . undefined = undefined ;
2019-11-07 12:47:12 +01:00
this . ignoreUndefined = ignoreUndefined ;
2019-08-09 10:08:34 +02:00
}
public TreeNodeDef ( ) {
}
2019-10-29 16:38:42 +01:00
public TreeNodeStats evaluate ( MapDocument doc1 , MapDocument doc2 , Config conf ) {
2019-08-09 10:08:34 +02:00
2019-08-09 15:41:49 +02:00
TreeNodeStats stats = new TreeNodeStats ( ) ;
2019-08-09 10:08:34 +02:00
for ( FieldConf fieldConf : fields ) {
double weight = fieldConf . getWeight ( ) ;
2019-10-29 16:38:42 +01:00
double result = comparator ( fieldConf ) . compare ( doc1 . getFieldMap ( ) . get ( fieldConf . getField ( ) ) , doc2 . getFieldMap ( ) . get ( fieldConf . getField ( ) ) , conf ) ;
2019-08-09 10:08:34 +02:00
2019-12-18 16:19:36 +01:00
stats . addFieldStats ( fieldConf . getComparator ( ) + " on " + fieldConf . getField ( ) + " " + fields . indexOf ( fieldConf ) , new FieldStats ( weight , Double . parseDouble ( fieldConf . getParams ( ) . getOrDefault ( " threshold " , " 0.5 " ) ) , result , fieldConf . isCountIfUndefined ( ) , doc1 . getFieldMap ( ) . get ( fieldConf . getField ( ) ) , doc2 . getFieldMap ( ) . get ( fieldConf . getField ( ) ) ) ) ;
2019-08-09 10:08:34 +02:00
}
2019-08-09 15:41:49 +02:00
return stats ;
2019-08-09 10:08:34 +02:00
}
private Comparator comparator ( final FieldConf field ) {
return PaceConfig . resolver . getComparator ( field . getComparator ( ) , field . getParams ( ) ) ;
}
public List < FieldConf > getFields ( ) {
return fields ;
}
public void setFields ( List < FieldConf > fields ) {
this . fields = fields ;
}
public AggType getAggregation ( ) {
return aggregation ;
}
public void setAggregation ( AggType aggregation ) {
this . aggregation = aggregation ;
}
public double getThreshold ( ) {
return threshold ;
}
public void setThreshold ( double threshold ) {
this . threshold = threshold ;
}
public String getPositive ( ) {
return positive ;
}
public void setPositive ( String positive ) {
this . positive = positive ;
}
public String getNegative ( ) {
return negative ;
}
public void setNegative ( String negative ) {
this . negative = negative ;
}
public String getUndefined ( ) {
return undefined ;
}
public void setUndefined ( String undefined ) {
this . undefined = undefined ;
}
2019-11-07 12:47:12 +01:00
public boolean isIgnoreUndefined ( ) {
return ignoreUndefined ;
2019-08-09 10:08:34 +02:00
}
2019-11-07 12:47:12 +01:00
public void setIgnoreUndefined ( boolean ignoreUndefined ) {
this . ignoreUndefined = ignoreUndefined ;
2019-08-09 10:08:34 +02:00
}
@Override
public String toString ( ) {
try {
return new ObjectMapper ( ) . writeValueAsString ( this ) ;
} catch ( IOException e ) {
throw new PaceException ( " Impossible to convert to JSON: " , e ) ;
}
}
}