diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/ScoreCalculator.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/ScoreCalculator.java index 0acb52daae..df84c2196c 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/ScoreCalculator.java +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/ScoreCalculator.java @@ -79,7 +79,7 @@ public abstract class ScoreCalculator { if (correlation == Correlation.POSITIVE) { // 正相关 BigDecimal x = coefficient.multiply(convertValue2BigDecimal(sourceValue).subtract(minValue)); - BigDecimal score = minScore.add(x, MathContext.DECIMAL32); + BigDecimal score = minScore.add(x); return score; } else if (correlation == Correlation.NEGATIVE) { // 负相关 diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/ScoreConstants.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/ScoreConstants.java new file mode 100644 index 0000000000..1eecc8cf36 --- /dev/null +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/ScoreConstants.java @@ -0,0 +1,13 @@ +package com.epmet.support.normalizing; + +import java.math.BigDecimal; + +/** + * 分值常量 + */ +public class ScoreConstants { + + public static final BigDecimal MIN_SCORE = new BigDecimal(0); + public static final BigDecimal MAX_SCORE = new BigDecimal(100); + +} diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/batch/BatchScoreCalculator.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/batch/BatchScoreCalculator.java new file mode 100644 index 0000000000..85b8cf501d --- /dev/null +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/batch/BatchScoreCalculator.java @@ -0,0 +1,35 @@ +package com.epmet.support.normalizing.batch; + +import com.epmet.support.normalizing.ScoreCalculator; + +import java.math.BigDecimal; +import java.util.List; +import java.util.stream.Collectors; + +public class BatchScoreCalculator { + + /** + * 每个指标都是一条数据,每一条数据中包含该指标的一组样本 + */ + private List indexVOS; + + /** + * 执行计算 + * @return 每一条都是一个指标的,所有样本对应的,得分值 + */ + public List exec(List indexVOS) { + this.indexVOS = indexVOS; + + return indexVOS.stream().map(i -> { + String indexId = i.getIndexId(); + List indexValues = i.getIndexValues(); + BigDecimal weight = i.getWeight(); + ScoreCalculator scoreCalculator = i.getScoreCalculator(); + + List scores4OneIndex = scoreCalculator.normalize(indexValues, weight); + + return new IndexOutputVO(indexId, scores4OneIndex); + }).collect(Collectors.toList()); + } + +} diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/batch/IndexInputVO.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/batch/IndexInputVO.java new file mode 100644 index 0000000000..984388d07d --- /dev/null +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/batch/IndexInputVO.java @@ -0,0 +1,39 @@ +package com.epmet.support.normalizing.batch; + +import com.epmet.support.normalizing.ScoreCalculator; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.math.BigDecimal; +import java.util.List; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class IndexInputVO { + + /** + * 指标标记,由使用者传入,用以标记该条指标的独有特性,一般用id或者code + * 非必填 + */ + private String indexId; + + /** + * 指标的样本值 + */ + private List indexValues; + + /** + * 权重 + */ + private BigDecimal weight; + + private ScoreCalculator scoreCalculator; + + public IndexInputVO(List indexValues, BigDecimal weight, ScoreCalculator scoreCalculator) { + this.indexValues = indexValues; + this.weight = weight; + this.scoreCalculator = scoreCalculator; + } +} diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/batch/IndexOutputVO.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/batch/IndexOutputVO.java new file mode 100644 index 0000000000..ab85a8cabf --- /dev/null +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/support/normalizing/batch/IndexOutputVO.java @@ -0,0 +1,23 @@ +package com.epmet.support.normalizing.batch; + +import lombok.AllArgsConstructor; +import lombok.Data; + +import java.math.BigDecimal; +import java.util.List; + +@Data +@AllArgsConstructor +public class IndexOutputVO { + + /** + * 指标标记,由使用者传入,用以标记该条指标的独有特性,一般用id或者code + * 非必填 + */ + private String indexId; + + /** + * 指标的样本值 + */ + private List indexScores; +} diff --git a/epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/normalizing/DemoScoreCal.java b/epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/normalizing/DemoScoreCal.java index 14ebc4685c..178468b0a0 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/normalizing/DemoScoreCal.java +++ b/epmet-module/data-statistical/data-statistical-server/src/test/java/com/epmet/stats/test/normalizing/DemoScoreCal.java @@ -1,26 +1,29 @@ package com.epmet.stats.test.normalizing; -import com.epmet.support.normalizing.Correlation; -import com.epmet.support.normalizing.DoubleScoreCalculator; -import com.epmet.support.normalizing.IntegerScoreCalculator; -import com.epmet.support.normalizing.ScoreCalculator; +import com.epmet.support.normalizing.*; +import com.epmet.support.normalizing.batch.BatchScoreCalculator; +import com.epmet.support.normalizing.batch.IndexInputVO; +import com.epmet.support.normalizing.batch.IndexOutputVO; +import org.junit.Test; import java.math.BigDecimal; import java.util.Arrays; +import java.util.List; public class DemoScoreCal { public static void main(String[] args) { //demoInteger(); - demoIntegerPartical(); + //demoIntegerPartical(); //demoDouble(); } /** * 整数类型,完整源数据列表的归一算法 */ - public static void demoInteger() { - Integer[] iArray = {4,8,1,3,2}; + @Test + public void demoInteger() { + Integer[] iArray = {4, 8, 1, 3, 2}; BigDecimal minScore = new BigDecimal(5); BigDecimal maxScore = new BigDecimal(10); @@ -32,12 +35,13 @@ public class DemoScoreCal { /** * 整数类型,基于边界值的部分列表的的归一算法 */ - public static void demoIntegerPartical() { - Integer[] iArray = {4,1,2}; + @Test + public void demoIntegerPartical() { + Integer[] iArray = {4, 1, 8}; BigDecimal minScore = new BigDecimal(5); BigDecimal maxScore = new BigDecimal(10); - ScoreCalculator sc = new IntegerScoreCalculator(1, 8 ,minScore, maxScore, Correlation.NEGATIVE); + ScoreCalculator sc = new IntegerScoreCalculator(1, 8, minScore, maxScore, Correlation.NEGATIVE); BigDecimal[] scores = sc.normalize(iArray);// 此处也可以直接使用list参数的重载方法,计算阶段没有任何区别,区别在于new IntegerScoreCalculator()阶段 Arrays.stream(scores).forEach(s -> System.out.println(s)); } @@ -46,7 +50,7 @@ public class DemoScoreCal { * double类型归一算法 */ public static void demoDouble() { - Double[] iArray = {1.9,8.9,1.0,3.0,2.0}; + Double[] iArray = {1.9, 8.9, 1.0, 3.0, 2.0}; BigDecimal minScore = new BigDecimal(5); BigDecimal maxScore = new BigDecimal(10); @@ -61,6 +65,31 @@ public class DemoScoreCal { System.out.println(normalize2); } + /** + * 批量计算demo + */ + @Test + public void testBatchCalculate() throws InterruptedException { + //{4,8,1,3,2} + + // 每个指标需要单独的分支计算器,因为每个指标的最大最小值是不同的 + ScoreCalculator sc1 = new IntegerScoreCalculator(1, 8, ScoreConstants.MIN_SCORE, ScoreConstants.MAX_SCORE, Correlation.NEGATIVE); + ScoreCalculator sc2 = new IntegerScoreCalculator(1, 8, ScoreConstants.MIN_SCORE, ScoreConstants.MAX_SCORE, Correlation.NEGATIVE); + + // 每个指标的信息,包括样本列表,权重,指标标记 + IndexInputVO index1VO = new IndexInputVO<>("aaa", Arrays.asList(4, 1, 8), new BigDecimal(1), sc1); + IndexInputVO index2VO = new IndexInputVO<>("bbb", Arrays.asList(1, 8, 3), new BigDecimal(1), sc2); + + List indexInputVOS = Arrays.asList(index1VO, index2VO); + + BatchScoreCalculator batchScoreCalculator = new BatchScoreCalculator(); + List result = batchScoreCalculator.exec(indexInputVOS); + + System.err.println("--------------------------------"); + result.stream().forEach(r -> System.out.println(r)); + Thread.sleep(10); + System.err.println("--------------------------------"); + } }