001/**
002 * Copyright (C) 2007-2011, 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 */
019
020package org.dllearner.experiments;
021
022import com.jamonapi.MonKeyImp;
023import com.jamonapi.Monitor;
024import com.jamonapi.MonitorComposite;
025import com.jamonapi.MonitorFactory;
026import org.apache.log4j.Logger;
027import org.dllearner.utilities.JamonMonitorLogger;
028
029import java.util.ArrayList;
030import java.util.HashMap;
031import java.util.List;
032import java.util.Map;
033
034/**
035 * An experiment has a certain configuration. In an iterated experiment or a
036 * experiment row a parameter can be altered or influenced by previous
037 * experiments with the same configuration. If you do not have an iterated
038 * experiment (or better only 1 iteration) set sizeOfResultVector to 1
039 * 
040 * @author Sebastian Hellmann <hellmann@informatik.uni-leipzig.de>
041 * 
042 */
043public class ExperimentConfiguration {
044        private static final Logger logger = Logger.getLogger(ExperimentConfiguration.class);
045
046        public final String experimentName;
047        public final int sizeOfResultVector;
048
049        protected List<MonitorComposite> mcs = new ArrayList<>();
050        protected Map<String, MonitorComposite> mcsMap = new HashMap<>();
051        protected Map<MonitorComposite, String> mcsMapRev = new HashMap<>();
052
053        /**
054         * sets sizeOfResultVector to 1, meaning no iterated experiments
055         * 
056         * @param experimentName
057         */
058        public ExperimentConfiguration(String experimentName) {
059                this(experimentName, 1);
060        }
061
062        public ExperimentConfiguration(String experimentName, int sizeOfResultVector) {
063                this.experimentName = experimentName;
064                this.sizeOfResultVector = sizeOfResultVector;
065        }
066
067        @Override
068        public String toString() {
069                return this.experimentName + " with " + sizeOfResultVector + " iterations";
070        }
071
072        public List<TableRowColumn> getTableRows() {
073                List<TableRowColumn> l = new ArrayList<>();
074                if (sizeOfResultVector == 1) {
075                        Monitor[] monitors = new Monitor[mcs.size()];
076                        for (int i = 0; i < monitors.length; i++) {
077                                monitors[i] = mcs.get(i).getMonitors()[0];
078                        }
079                        l.add(new TableRowColumn(monitors, experimentName, ""));
080
081                } else {
082                        for (MonitorComposite mc : mcs) {
083                                l.add(new TableRowColumn(mc.getMonitors(), experimentName, getRev(mc)));
084                        }
085                }
086
087                return l;
088        }
089
090        private MonitorComposite get(MonKeyImp m) {
091                return mcsMap.get(mon(m).getLabel());
092        }
093
094        private String getRev(MonitorComposite mc) {
095                return mcsMapRev.get(mc);
096        }
097
098        private void put(MonKeyImp m, MonitorComposite mc) {
099                mcsMap.put(mon(m).getLabel(), mc);
100        }
101
102        private void putRev(MonitorComposite mc, MonKeyImp m) {
103                mcsMapRev.put(mc, m.getLabel());
104        }
105
106        public void init(List<MonKeyImp> monkeys) {
107                for (MonKeyImp monKeyImp : monkeys) {
108                        init(monKeyImp);
109                }
110        }
111
112        public void init(MonKeyImp oldMonkey) {
113                Monitor[] marr = new Monitor[sizeOfResultVector];
114                for (int i = 0; i < sizeOfResultVector; i++) {
115                        MonKeyImp newMonKey = mon(oldMonkey, i);
116                        if (newMonKey.getUnits().equals(JamonMonitorLogger.MS)) {
117                                marr[i] = MonitorFactory.getTimeMonitor(newMonKey);
118                        } else {
119                                marr[i] = MonitorFactory.getMonitor(newMonKey);
120                        }
121                }
122                MonitorComposite m = new MonitorComposite(marr);
123                mcs.add(m);
124                put(oldMonkey, m);
125                putRev(m, oldMonkey);
126
127        }
128
129        protected MonKeyImp mon(MonKeyImp monkey) {
130                MonKeyImp m = (monkey.getLabel().startsWith(experimentName)) ? monkey : new MonKeyImp(experimentName
131                                + "_" + monkey.getLabel(), monkey.getUnits());
132                return m;
133        }
134
135        protected MonKeyImp mon(MonKeyImp oldMonkey, int index) {
136                // narrensicher
137                MonKeyImp newMonkey = mon(oldMonkey);
138                return new MonKeyImp(newMonkey.getLabel() + "_" + index, newMonkey.getUnits());
139        }
140
141        public void add(MonKeyImp monkey, int index, double value) {
142                try {
143                        get(monkey).getMonitors()[index].add(value);
144                } catch (IndexOutOfBoundsException e) {
145                        e.printStackTrace();
146                        logger.error("index too big, is: "+index+" max = " +  get(monkey).getMonitors().length);
147                }
148
149        }
150
151        public Monitor start(MonKeyImp monkey, int index) {
152                return get(monkey).getMonitors()[index].start();
153        }
154
155}