BrightSide Workbench Full Report + Source Code
AbstractIndicator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.indicator;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.stream.Collectors;
24 import java.util.stream.Stream;
25 import org.turro.elephant.db.SQLHelper;
26 import org.turro.elephant.db.WhereClause;
27 import org.turro.entities.Entities;
28 import org.turro.jpa.Dao;
29 import org.turro.matching.GenericSiblings;
30 import org.turro.matching.ProcessMatching;
31 import org.turro.ranking.ProcessRanking;
32 
37 public abstract class AbstractIndicator implements IElephantIndicator {
38 
39  protected List<IndicatorVariable> variables = new ArrayList<>();
40  protected String root, relatedRoot;
41 
42  @Override
43  public void initIndicator() {
44  initVariables();
45  }
46 
47  @Override
48  public void prepareIndicator() {
50  }
51 
52  @Override
53  public void generateSiblings() {
54  // Do nothing
55  }
56 
57  @Override
58  public String getRoot() {
59  return root;
60  }
61 
62  protected void addVariable(String name, VariableType type) {
63  variables.add(new IndicatorVariable(root, name, false, type));
64  }
65 
66  protected void addVariable(String name, VariableType type, boolean reversed) {
67  variables.add(new IndicatorVariable(root, name, false, type, reversed));
68  }
69 
70  protected void addVariable(String name, VariableType type, String avoid) {
71  variables.add(new IndicatorVariable(root, name, false, type, avoid));
72  }
73 
74  protected void addVariable(String name, boolean preprocess, VariableType type) {
75  variables.add(new IndicatorVariable(root, name, preprocess, type));
76  }
77 
78  protected void addVariable(String name, boolean preprocess, VariableType type, boolean reversed) {
79  variables.add(new IndicatorVariable(root, name, preprocess, type, reversed));
80  }
81 
82  protected void addVariable(String name, boolean preprocess, VariableType type, String avoid) {
83  variables.add(new IndicatorVariable(root, name, preprocess, type, avoid));
84  }
85 
86  protected void addExternalVariable(String root, String name) {
87  IndicatorVariable variable = Indicators.getVariable(root, name);
88  if(variable != null) variables.add(variable);
89  }
90 
91  @Override
92  public List<IndicatorVariable> getVariables() {
93  return variables.stream().filter(v -> root.equals(v.getRoot())).collect(Collectors.toList());
94  }
95 
96  @Override
97  public List<IndicatorVariable> getAllVariables() {
98  return variables;
99  }
100 
101  @Override
102  public IndicatorVariable getVariable(String name) {
103  return getVariables().stream().filter(v -> name.equals(v.getName())).findAny().get();
104  }
105 
106  @Override
107  public List<IndicatorVariable> getAllRankingVariables(String root) {
108  return variables.stream().filter(v -> v.permits(root) && v.getType().isRanking()).collect(Collectors.toList());
109  }
110 
111  @Override
112  public List<IndicatorVariable> getAllMatchingVariables(String root) {
113  return variables.stream().filter(v -> v.permits(root) && v.getType().isMatching()).collect(Collectors.toList());
114  }
115 
116  @Override
117  public double getValue(IndicatorVariable variable, Object entity) {
118  return 0.0;
119  }
120 
121  @Override
122  public double getValue(IndicatorVariable variable, Object entity, String relatedPath) {
123  return 0.0;
124  }
125 
126  @Override
127  public IndicatorResult readIndicator(String storeClass, String indicator) {
128  String v[] = indicator.split("\\s*\\.\\s*");
129  int index = 0;
130  WhereClause wc = new WhereClause();
131  if(v.length == 2) {
132  wc.addClause("select new org.turro.indicator.IndicatorResult(sum(ranking), count(ranking))");
133  } else {
134  wc.addClause("select new org.turro.indicator.IndicatorResult(sum(matching), count(matching))");
135  }
136  wc.addClause("from " + storeClass);
137  wc.addClause("where entityPath like :entityPath");
138  wc.addNamedValue("entityPath", convertPathToLike(v[index++], root));
139  if(v.length == 3) {
140  wc.addClause("and relatedPath like :relatedPath");
141  wc.addNamedValue("relatedPath", convertPathToLike(v[index++], relatedRoot));
142  }
143  wc.addClause("and concept like :indicator");
144  wc.addNamedValue("indicator", convertIndicatorToLike(v[index]));
145 
147  }
148 
149  @Override
150  public void preprocess(IndicatorVariable variable, IPreprocessor preprocessor, String entityRoot, String relatedRoot) {
151  if(variable.isPreprocess()) {
152  if(root.equals(variable.getRoot())) {
153  if(preprocessor instanceof ProcessRanking) {
154  processRankingVariable(createDao(), preprocessor, variable, entityRoot);
155  } else if(preprocessor instanceof ProcessMatching) {
156  processMatchingVariable(createDao(), preprocessor, variable, entityRoot, relatedRoot);
157  }
158  } else {
159  Indicators.getIndicator(variable).preprocess(variable, preprocessor, entityRoot, relatedRoot);
160  }
161  }
162  }
163 
164  @Override
165  public void postprocess(IndicatorVariable variable, IPreprocessor postprocessor, String entityRoot, String relatedRoot) {
166  if(postprocessor instanceof ProcessRanking) {
167  postprocessRankingVariable(createDao(), postprocessor, variable, entityRoot);
168  } else if(postprocessor instanceof ProcessMatching) {
169  postprocessMatchingVariable(createDao(), postprocessor, variable, entityRoot, relatedRoot);
170  }
171  }
172 
173  protected String getPath(Object entity) {
174  return (entity instanceof String) ? (String) entity : Entities.getController(entity).getPath();
175  }
176 
177  private String convertPathToLike(String value, String root) {
178  if(value.matches("\\/[a-z\\-\\*\\?]+\\/[0-9a-zA-Z\\*\\?]+")) {
179  return SQLHelper.convertToLike(value);
180  } else {
181  return "/" + root + "/" + SQLHelper.convertToLike(value);
182  }
183  }
184 
185  private String convertIndicatorToLike(String value) {
186  if("final".equals(value)) {
187  return "*";
188  } else {
189  return SQLHelper.convertToLike(value);
190  }
191  }
192 
193  /* Variables */
194 
195  protected abstract void initVariables();
196  protected abstract void initExternalVariables();
197 
198  /* Preprocessor */
199 
200  protected abstract void processRankingVariable(Dao dao, IPreprocessor preprocessor,
201  IndicatorVariable variable, String entityRoot);
202  protected abstract void processMatchingVariable(Dao dao, IPreprocessor preprocessor,
203  IndicatorVariable variable, String entityRoot, String relatedRoot);
204 
205  /* Postprocessor */
206 
207  protected void postprocessRankingVariable(Dao dao, IPreprocessor postprocessor,
208  IndicatorVariable variable, String entityRoot) {}
209  protected void postprocessMatchingVariable(Dao dao, IPreprocessor postprocessor,
210  IndicatorVariable variable, String entityRoot, String relatedRoot) {}
211 
212  /* Streams */
213 
214  @Override
215  public Stream<String> getEntityPaths(String root) {
216  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
217  }
218 
219  @Override
220  public Stream<GenericSiblings> getSiblingPaths(String entityPath) {
221  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
222  }
223 
224  /* Dao */
225 
226  private Dao _dao;
227 
228  public Dao getDao() {
229  if(_dao == null) {
230  _dao = createDao();
231  }
232  return _dao;
233  }
234 
235  protected abstract Dao createDao();
236 
237  /* Type checking */
238 
239  @Override
240  public boolean itsMine(String root) {
241  return this.root.equals(root);
242  }
243 
244  @Override
245  public boolean itsMine(IndicatorVariable variable) {
246  return getVariables().contains(variable);
247  }
248 
249 }
static String convertToLike(String value)
Definition: SQLHelper.java:33
void addNamedValue(String name, Object value)
static IElephantEntity getController(String path)
Definition: Entities.java:78
void preprocess(IndicatorVariable variable, IPreprocessor preprocessor, String entityRoot, String relatedRoot)
boolean itsMine(IndicatorVariable variable)
void addVariable(String name, VariableType type)
Stream< GenericSiblings > getSiblingPaths(String entityPath)
List< IndicatorVariable > getAllMatchingVariables(String root)
List< IndicatorVariable > getVariables()
void addVariable(String name, VariableType type, String avoid)
void postprocess(IndicatorVariable variable, IPreprocessor postprocessor, String entityRoot, String relatedRoot)
abstract void processMatchingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot, String relatedRoot)
Stream< String > getEntityPaths(String root)
IndicatorResult readIndicator(String storeClass, String indicator)
void addVariable(String name, boolean preprocess, VariableType type, String avoid)
abstract void processRankingVariable(Dao dao, IPreprocessor preprocessor, IndicatorVariable variable, String entityRoot)
void postprocessRankingVariable(Dao dao, IPreprocessor postprocessor, IndicatorVariable variable, String entityRoot)
double getValue(IndicatorVariable variable, Object entity)
void addVariable(String name, boolean preprocess, VariableType type)
void addVariable(String name, boolean preprocess, VariableType type, boolean reversed)
List< IndicatorVariable > getAllVariables()
void postprocessMatchingVariable(Dao dao, IPreprocessor postprocessor, IndicatorVariable variable, String entityRoot, String relatedRoot)
List< IndicatorVariable > getAllRankingVariables(String root)
double getValue(IndicatorVariable variable, Object entity, String relatedPath)
IndicatorVariable getVariable(String name)
void addExternalVariable(String root, String name)
void addVariable(String name, VariableType type, boolean reversed)
static IElephantIndicator getIndicator(String root)
Definition: Indicators.java:69
static IndicatorVariable getVariable(String root, String name)
Definition: Indicators.java:64
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
void preprocess(IndicatorVariable variable, IPreprocessor preprocessor, String entityRoot, String relatedRoot)