Switch to side-by-side view

--- a
+++ b/src/main/java/net/timbusproject/dpes/pi/kbserver/beans/StatisticsBean.java
@@ -0,0 +1,100 @@
+/**
+ * Copyright (c) 2013/2014 Verein zur Foerderung der IT-Sicherheit in Oesterreich (SBA).
+ * The work has been developed in the TIMBUS Project and the above-mentioned are Members of the TIMBUS Consortium.
+ * TIMBUS is supported by the European Union under the 7th Framework Programme for research and technological
+ * development and demonstration activities (FP7/2007-2013) under grant agreement no. 269940.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including without
+ * limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTIBITLY, or FITNESS FOR A PARTICULAR
+ * PURPOSE. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise,
+ * unless required by applicable law or agreed to in writing, shall any Contributor be liable for damages, including
+ * any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this
+ * License or out of the use or inability to use the Work.
+ * See the License for the specific language governing permissions and limitation under the License.
+ */
+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 static final String ONTOLOGY_FILE_NAME = "/kb.out.owl";
+
+    private final ToolKnowledgeBase toolKnowledgeBase;
+    private final List<String> toolNames;
+    private DescriptiveStatistics statistics;
+
+    private CartesianChartModel toolAlternativesChartModel;
+    private List<Integer> alternativeCounts;
+
+    public StatisticsBean() {
+        JenaQueryFacade queryFacade = new JenaQueryFacade(JenaQueryFacade.getModelFromPath(getClass().getResource(ONTOLOGY_FILE_NAME).getFile(), true));
+        toolKnowledgeBase = new TimbusToolKnowledgeBase(queryFacade);
+        toolNames = toolKnowledgeBase.getAllToolNames();
+    }
+
+    public DescriptiveStatistics getStatistics() {
+        if (statistics != null) return statistics;
+        List<Integer> alternativeCounts = getAlternativeCounts();
+        statistics = new DescriptiveStatistics();
+
+        for (Integer count : alternativeCounts) {
+            statistics.addValue(count);
+        }
+
+        return statistics;
+    }
+
+    private List<Integer> getAlternativeCounts() {
+        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;
+    }
+
+    public CartesianChartModel getToolAlternativesChartModel() {
+        if (toolAlternativesChartModel != null) return toolAlternativesChartModel;
+        ChartSeries toolAlternativesSeries = new ChartSeries();
+        toolAlternativesSeries.setLabel("Tool alternatives");
+        for (int i = 0; i < getAlternativeCounts().size(); ++i) {
+            toolAlternativesSeries.set(i, getAlternativeCounts().get(i));
+        }
+        toolAlternativesChartModel = new CartesianChartModel();
+        toolAlternativesChartModel.addSeries(toolAlternativesSeries);
+        return toolAlternativesChartModel;
+    }
+
+}