19 package org.turro.ranking;
21 import java.util.List;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import java.util.stream.Stream;
25 import org.turro.action.IProcess;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.elephant.db.WhereClause;
28 import org.turro.indicator.Formulas;
29 import org.turro.indicator.IElephantIndicator;
30 import org.turro.indicator.IPreprocessor;
31 import org.turro.indicator.IndicatorVariable;
32 import org.turro.indicator.Indicators;
33 import org.turro.indicator.VariableType;
34 import org.turro.jpa.Dao;
35 import org.turro.math.Zero;
76 wc.
addClause(
"where entityPath = :entityPath");
82 public void process(Stream<T> stream, List<IndicatorVariable> variables) {
87 }
catch(Exception ex) {
93 public void processFor(T entity, List<IndicatorVariable> variables) {
95 variables.forEach(v -> {
96 if(!v.isPreprocess()) {
113 ranking =
getDao().saveObject(ranking);
119 variables.forEach(v -> {
120 if(v.isPreprocess()) {
121 IElephantIndicator ei = Indicators.getIndicator(v);
122 ei.preprocess(v, this, entitiesRoot(), null);
128 variables.forEach(v -> {
159 private List<IndicatorVariable> variables;
161 private void calculateRanking() {
165 try(Stream<IRanking> tuples = getIndicatorTuples()) {
166 tuples.forEach(t -> doCalculate(t));
174 private Stream<IRanking> getIndicatorTuples() {
175 WhereClause wc =
new WhereClause();
176 wc.addClause(
"select m from " +
instanceClass().getSimpleName() +
" m");
177 wc.addClause(
"order by entityPath, concept");
178 return createDao().stream(IRanking.class, wc, 1000);
181 private void doCalculate(IRanking ranking) {
182 if(ranking ==
null || (previous !=
null && !sameTuple(previous, ranking))) {
183 set.addMissing(variables);
185 IndicatorVariable.result(
indicatorsRoot(), VariableType.RANKING_VARIABLE));
187 if(!Zero.near(result.getRanking(), 2)) {
190 set =
new RankingSet();
192 if(ranking !=
null) {
198 private boolean sameTuple(IRanking ranking1, IRanking ranking2) {
199 if(ranking1 ==
null || ranking2 ==
null) {
202 return ranking1.
getEntityPath().equals(ranking2.getEntityPath());
static String logMsg(String msg)
void addClause(String clause)
void addNamedValue(String name, Object value)
static boolean hasDirectProcess(List< IndicatorVariable > variables)
static double getValue(Object entity, IndicatorVariable variable)
static List< IndicatorVariable > getRankingVariables(String root)
static IElephantIndicator getIndicator(String root)
abstract String getPath(Object object)
int executeUpdate(String query)
void poolObject(Object obj, int poolSize)
int executeNativeUpdate(SqlClause sc, Object... pars)
static boolean near(double value, int digits)
abstract Stream< T > getEntityStream()
void startProcessFor(Object entity)
void process(Stream< T > stream, List< IndicatorVariable > variables)
void processFor(T entity, List< IndicatorVariable > variables)
void prepareEntity(T entity)
abstract Class instanceClass()
void finishPreprocessor()
abstract String entitiesRoot()
void prepareProcess(List< IndicatorVariable > variables)
void postProcess(List< IndicatorVariable > variables)
abstract IRanking createRankingInstance()
abstract String indicatorsRoot()
IRanking process(T entity, IndicatorVariable variable)
IRanking createRankingInstance(String entityPath, IndicatorVariable variable)
double calculate(RankingSet set)
void poolInstance(Object value)
void postprocess(IndicatorVariable variable, IPreprocessor preprocessor, String entityRoot, String relatedRoot)
void setConcept(String concept)
void setEntityPath(String entityPath)
void setRanking(double value)