--- a/src/main/java/net/timbusproject/dpes/pi/kbserver/beans/StatisticsBean.java
+++ b/src/main/java/net/timbusproject/dpes/pi/kbserver/beans/AlternativeStatisticsBase.java
@@ -17,138 +17,71 @@
*/
package net.timbusproject.dpes.pi.kbserver.beans;
-import ch.lambdaj.function.convert.Converter;
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.sba_research.timbus.kb.ToolKnowledgeBase;
import org.sba_research.timbus.kb.timbus.TimbusToolKnowledgeBase;
-import org.sbaresearch.owl.InvalidQueryException;
import org.sbaresearch.owl.JenaQueryFacade;
import org.slf4j.LoggerFactory;
-import javax.faces.bean.ApplicationScoped;
-import javax.faces.bean.ManagedBean;
import java.io.Serializable;
-import java.util.Collections;
import java.util.List;
-import static ch.lambdaj.Lambda.convert;
-
-@ManagedBean
-@ApplicationScoped
-@SuppressWarnings("unused")
-public class StatisticsBean implements Serializable {
- private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(StatisticsBean.class);
+public abstract class AlternativeStatisticsBase implements Serializable {
+ private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(AlternativeStatisticsBase.class);
public static final String ONTOLOGY_FILE_NAME = "/freebaseExtensions.owl";
- private final ToolKnowledgeBase toolKnowledgeBase;
- private final List<String> toolNames;
+ protected final ToolKnowledgeBase toolKnowledgeBase;
+ protected final List<String> toolNames;
+ protected final List<String> formatIDs;
+ protected List<Integer> alternativeCounts;
+
+ private DescriptiveStatistics statistics;
private final JenaQueryFacade queryFacade;
- private final List<String> formatIDs;
- private DescriptiveStatistics toolAlternativeStatistics;
+ private CartesianChartModel alternativesChartModel;
- private CartesianChartModel toolAlternativesChartModel;
- private List<Integer> alternativeCounts;
- private DescriptiveStatistics formatAlternativeStatistics;
- private List<Integer> alternativeFormatCounts;
- private CartesianChartModel formatAlternativesChartModel;
-
- public StatisticsBean() {
+ public AlternativeStatisticsBase() {
queryFacade = new JenaQueryFacade(JenaQueryFacade.getModelFromPath(getClass().getResource(ONTOLOGY_FILE_NAME).getFile(), true));
toolKnowledgeBase = new TimbusToolKnowledgeBase(queryFacade);
toolNames = getAllToolNames();
formatIDs = toolKnowledgeBase.getAllFormatIDs();
}
- public DescriptiveStatistics getToolAlternativeStatistics() {
- if (toolAlternativeStatistics != null) return toolAlternativeStatistics;
- List<Integer> alternativeCounts = getToolAlternativeCounts();
- toolAlternativeStatistics = new DescriptiveStatistics();
+ @SuppressWarnings("unused")
+ public DescriptiveStatistics getAlternativeStatistics() {
+ if (statistics != null) return statistics;
+ List<Integer> alternativeCounts = getAlternativeCounts();
+ statistics = new DescriptiveStatistics();
for (Integer count : alternativeCounts) {
- toolAlternativeStatistics.addValue(count);
+ statistics.addValue(count);
}
- return toolAlternativeStatistics;
+ return statistics;
}
- private List<Integer> getToolAlternativeCounts() {
- if (alternativeCounts != null) return alternativeCounts;
- alternativeCounts = convert(toolNames, new Converter<String, Integer>() {
- @Override
- public Integer convert(String tool) {
- try {
- // TODO: this really is slow, transform to a query
- return toolKnowledgeBase.getAlternativeTools(tool).size();
- } catch (InvalidQueryException e) {
- LOG.error("Unable to get alternatives for: " + tool + ", reason: " + e.getMessage());
- return 0;
- }
- }
- });
- Collections.sort(alternativeCounts);
- return alternativeCounts;
+ protected abstract List<Integer> getAlternativeCounts();
+
+ @SuppressWarnings("unused")
+ public CartesianChartModel getAlternativesChartModel() {
+ if (alternativesChartModel != null) return alternativesChartModel;
+ ChartSeries alternativeSeries = new ChartSeries();
+ alternativeSeries.setLabel("Alternatives");
+ for (int i = 0; i < getAlternativeCounts().size(); ++i) {
+ alternativeSeries.set(i, getAlternativeCounts().get(i));
+ }
+ alternativesChartModel = new CartesianChartModel();
+ alternativesChartModel.addSeries(alternativeSeries);
+ return alternativesChartModel;
}
- public CartesianChartModel getToolAlternativesChartModel() {
- if (toolAlternativesChartModel != null) return toolAlternativesChartModel;
- ChartSeries toolAlternativesSeries = new ChartSeries();
- toolAlternativesSeries.setLabel("Tool alternatives");
- for (int i = 0; i < getToolAlternativeCounts().size(); ++i) {
- toolAlternativesSeries.set(i, getToolAlternativeCounts().get(i));
- }
- toolAlternativesChartModel = new CartesianChartModel();
- toolAlternativesChartModel.addSeries(toolAlternativesSeries);
- return toolAlternativesChartModel;
- }
-
- public DescriptiveStatistics getFormatAlternativeStatistics() {
- if (formatAlternativeStatistics != null) return formatAlternativeStatistics;
- List<Integer> alternativeCounts = getFormatAlternativeCounts();
- formatAlternativeStatistics = new DescriptiveStatistics();
-
- for (Integer count : alternativeCounts) {
- formatAlternativeStatistics.addValue(count);
- }
-
- return formatAlternativeStatistics;
- }
-
- private List<Integer> getFormatAlternativeCounts() {
- if (alternativeFormatCounts != null) return alternativeFormatCounts;
- alternativeFormatCounts = convert(formatIDs, new Converter<String, Integer>() {
- @Override
- public Integer convert(String formatID) {
- try {
- // TODO: this really is slow, transform to a query
- return toolKnowledgeBase.getConvertToMigrationOptions(formatID).size(); // TODO: also support convertFrom
- } catch (InvalidQueryException e) {
- LOG.error("Unable to get alternatives for: " + formatID + ", reason: " + e.getMessage());
- return 0;
- }
- }
- });
- Collections.sort(alternativeFormatCounts);
- return alternativeFormatCounts;
- }
-
- public CartesianChartModel getFormatAlternativesChartModel() {
- if (formatAlternativesChartModel != null) return formatAlternativesChartModel;
- ChartSeries formatAlternativeSeries = new ChartSeries();
- formatAlternativeSeries.setLabel("Format alternatives");
- for (int i = 0; i < getFormatAlternativeCounts().size(); ++i) {
- formatAlternativeSeries.set(i, getFormatAlternativeCounts().get(i));
- }
- formatAlternativesChartModel = new CartesianChartModel();
- formatAlternativesChartModel.addSeries(formatAlternativeSeries);
- return formatAlternativesChartModel;
- }
-
+ @SuppressWarnings("unused")
public List<String> getAllTools() {
return toolNames;
}
+ @SuppressWarnings("unused")
public List<String> getAllFormatIDs() { return formatIDs; }
public Integer round(float value) {