001/**
002 * Copyright (C) 2007 - 2016, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.dllearner.utilities;
020
021import java.io.File;
022import java.util.Iterator;
023import java.util.LinkedList;
024import java.util.List;
025import java.util.SortedSet;
026import java.util.TreeSet;
027
028import org.dllearner.algorithms.ocel.OCEL;
029import org.dllearner.kb.extraction.ExtractionAlgorithm;
030import org.dllearner.kb.manipulator.Manipulator;
031import org.dllearner.kb.manipulator.TypeFilterRule;
032import org.dllearner.kb.sparql.Cache;
033import org.dllearner.kb.sparql.SparqlQuery;
034
035import com.jamonapi.Monitor;
036import com.jamonapi.MonitorFactory;
037
038/**
039 * provides convenience functions for timelogs
040 *
041 */
042public class JamonMonitorLogger {
043
044
045        public static final String MS = "ms.";
046        public static final String SECONDS = "sec.";
047        public static final String COUNT = "count";
048        public static final String DOUBLE = "double";
049        public static final String PERCENTAGE = "%";
050        
051        public static List<Monitor> getMonitors(){
052                //MonitorFactory mf=(MonitorFactory)MonitorFactory.getFactory();
053                LinkedList<Monitor> l= new LinkedList<>();
054                
055                @SuppressWarnings("unchecked")
056                Iterator<Monitor> it = MonitorFactory.getFactory().iterator();
057                        //mf.iterator();
058                while (it.hasNext()) {
059                        Monitor monitor = it.next();
060                        
061                        l.add(monitor);
062                }
063                
064                return l;
065        }
066        
067        
068        
069        public static void printAllSortedByLabel() {
070                
071                        System.out.println(getStringForAllSortedByLabel());
072                
073        }
074        
075        public static String getStringForAllSortedByLabel() {
076                List<Monitor> l= getMonitors();
077                SortedSet<String> sset = new TreeSet<>();
078                StringBuilder sbuf = new StringBuilder();
079                for (Monitor monitor : l) {
080                        sset.add(monitor.toString());
081                }
082                for (String onemon : sset) {
083                        sbuf.append(onemon).append("\n");
084                }
085                return sbuf.toString();
086        }
087        
088        public static String convMonitorToString (Monitor m) {
089                String retVal = m.getLabel()+"|\t";
090                String unit = m.getUnits();
091                retVal+=unit+"|\t";
092                long content = new Double(m.getTotal()).longValue();
093                content = content / (1000*1000);
094                String contentstr = (unit.equals(MS))? Helper.prettyPrintNanoSeconds(content ) : m.getHits()+"" ;
095                retVal+= "total:"+contentstr+"|\t";
096                
097                long avg = new Double(m.getAvg()).longValue();
098                avg = avg / (1000*1000);
099                String avgstr = (unit.equals(MS))? Helper.prettyPrintNanoSeconds(avg ) : avg+"" ;
100                retVal+= "avg:"+avgstr+"|\t";
101                
102                return retVal;
103        }
104        
105        
106        
107        @SuppressWarnings("all")
108        public static String getMonitorPrefix(Class clazz){
109                String retval="";
110                if (clazz == SparqlQuery.class) {
111                        retval= "Sparql:";
112                } else if (clazz == Cache.class) {
113                        retval= "Sparql:";
114                }else if (clazz == ExtractionAlgorithm.class) {
115                        retval= "Extraction:";
116                } else if (clazz == Manipulator.class) {
117                        retval= "Extraction:";
118//              Jens: temporarily commented out, because "core" does not depend on "interfaces"         
119//              } else if (clazz == Start.class) {
120//                      retval= "Init:";
121                } else if (clazz == TypeFilterRule.class) {
122                        retval= "Extraction:";
123                } else if (clazz == SparqlQuery.class) {
124                        retval= "sparql:";
125                } else if (clazz == SparqlQuery.class) {
126                        retval= "sparql:";
127                } else if (clazz == OCEL.class) {
128                        retval= "Learning:";
129                } else if (clazz == SparqlQuery.class) {
130                        retval= "sparql:";
131                } else {
132                        retval= "undefined:";
133                }
134                return retval+clazz.getSimpleName()+":";
135        }
136        
137        
138        
139        @SuppressWarnings("all")
140        public static  Monitor getTimeMonitor(Class clazz, String label) {
141                String labeltmp = getMonitorPrefix(clazz)+label;
142                return MonitorFactory.getTimeMonitor(labeltmp);
143                
144        }
145        
146        @SuppressWarnings("all")
147        public static  Monitor getStatisticMonitor( String label) {
148                return MonitorFactory.getMonitor(label, "double");
149        }
150        @SuppressWarnings("all")
151        public static  Monitor getStatisticMonitor( String label, String unit) {
152                return MonitorFactory.getMonitor(label, unit);
153                
154        }
155        
156        @SuppressWarnings("all")
157        public static void increaseCount (Class clazz, String label) {
158                // MonitorFactory.getMonitor(getMonitorPrefix(clazz)+label, "#").add(1.0);
159                 Monitor m =  MonitorFactory.getMonitor(getMonitorPrefix(clazz)+label, "count");
160                // System.out.println(m);
161                 m.setHits(m.getHits()+1);
162                //System.out.println(m);
163        }
164        
165        public static void writeHTMLReport(String filename){
166                File jamonlog = new File(filename);
167                Files.createFile(jamonlog, MonitorFactory.getReport());
168                Files.appendToFile(jamonlog, "<xmp>\n"+JamonMonitorLogger.getStringForAllSortedByLabel());
169        }
170        
171        
172        
173        
174}