{-# LANGUAGE FlexibleContexts #-}------------------------------------------------------ ---- Statistics.hs: ---- Functions that collect and print out ---- statistics ---- ------------------------------------------------------{-
Copyright (C) GenI 2002-2005 (originally from HyLoRes)
Carlos Areces - areces@loria.fr - http://www.loria.fr/~arecesDaniel Gorin - dgorin@dc.uba.ar
Juan Heguiabehere - juanh@inf.unibz.it - http://www.inf.unibz.it/~juanh/Eric Kow - kow@loria.fr - http://www.loria.fr/~kowThis program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.
-}moduleNLP.GenI.Statistics(Statistics,StatisticsState,emptyStats,showFinalStats,initialStatisticsStateFor,addMetric,Metric(IntMetric),queryMetrics,updateMetrics,incrIntMetric,queryIntMetric,)whereimportControl.Monad.StateimportData.Maybe(mapMaybe)importText.JSON--------------------------------------------- Statistics are collections of Metrics-- which can be printed out (at regular intervals)-------------------------------------------newtypeStatistics=Stat{metrics::[Metric]}typeStatisticsStatea=forallm.(MonadStateStatisticsm)=>maupdateMetrics::(Metric->Metric)->Statistics->StatisticsupdateMetricsfstat=stat{metrics=mapf(metricsstat)}queryMetrics::(Metric->Maybea)->Statistics->[a]queryMetricsf=mapMaybef.metricsemptyStats::StatisticsemptyStats=Stat[]--------------------------- Monadic Statistics functions follow ------------------------------initialStatisticsStateFor::(MonadStateStatisticsm)=>(ma->Statistics->b)->ma->binitialStatisticsStateForf=flipfemptyStats-- | Adds a metric at the beginning of the list-- (note we reverse the order whene we want to print the metrics)addMetric::Metric->StatisticsState()addMetricnewMetric=modify(\stat->stat{metrics=newMetric:metricsstat})showFinalStats::Statistics->StringshowFinalStats=unlines.mapshow.reverse.metrics---------------------------------------------- Metrics--------------------------------------------dataMetric=IntMetricStringIntinstanceShowMetricwhereshow(IntMetricsx)=s++" : "++showxincrIntMetric::String->Int->Metric->MetricincrIntMetrickeyi(IntMetricsc)|s==key=IntMetrics(c+i)incrIntMetric__m=mqueryIntMetric::String->Metric->MaybeIntqueryIntMetrickey(IntMetricsc)|s==key=JustcqueryIntMetric__=Nothing--------------------------- JSON Output ------------------------------instanceJSONStatisticswherereadJSON_j=error"can't read GenI statistics from JSON yet; sorry"showJSON=JSObject.toJSObject.mapmetricToJSON.metrics-- not quite showJSON heremetricToJSON::Metric->(String,JSValue)metricToJSON(IntMetricsi)=(s,showJSONi)