Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/com/heuristiclab/okb/ecj/OKBStatistics.java @ 6327

Last change on this file since 6327 was 6327, checked in by bfarka, 13 years ago

added dokumentation for the service #1441

File size: 4.2 KB
Line 
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package com.heuristiclab.okb.ecj;
6
7import com.heuristiclab.okb.service.administration.AdministrationServiceUtil;
8import com.heurisitclab.okb.service.runcreation.Algorithm;
9import com.heurisitclab.okb.service.runcreation.RunCreationUtil;
10import com.heuristiclab.okb.service.administration.Platform;
11
12import java.util.Map;
13
14import ec.EvolutionState;
15import ec.Statistics;
16import ec.util.Parameter;
17
18import java.beans.XMLEncoder;
19import java.io.ByteArrayOutputStream;
20import java.util.HashMap;
21
22/**
23 *
24 * @author fab
25 * Implementation of statics which are sending the parameters and results
26 * to the OKB Serverices
27 *
28 */
29public class OKBStatistics extends Statistics {
30
31    /**
32     * Plattformname which has to be in already in the service
33     */
34    public static final String ECJ_PLATFORM_NAME = "ECJ";
35    /**
36     * Name for parameters in the param file
37     */
38    public static final String OKB_PARAMETER_NAME = "okb.parameter";
39    /**
40     * name of the seed paramter in the params file
41     */
42    public static final String OKB_PARAMETER_SEED = "okb.parameter.seed";
43    /**
44     * username
45     */
46    public static final String OKB_USER = "okb.username";
47    /**
48     * password
49     */
50    public static final String OKB_PASSWORD = "okb.password";
51    /**
52     * current Platform
53     * */
54    private Platform ecjPlatform;
55    /**
56     * current Alogrithm
57     */
58    private Algorithm algorithm;
59
60    /**
61     * Setup of statistic, username and password are set
62     *
63     * platform an algorithm are fetched from the service
64     * if the platform or algorithm are missing a illegal arugement excption is thorwn
65     **/
66    @Override
67    public void setup(EvolutionState state, Parameter base) {
68        super.setup(state, base);
69        handler.UserNameHandler.setUser(getUsername(state));
70        handler.UserNameHandler.setPassword(getPassword(state));
71
72
73        ecjPlatform = AdministrationServiceUtil.getPlattform(ECJ_PLATFORM_NAME);
74        algorithm = RunCreationUtil.getAlgorithm(state.evaluator.getClass().getName(), ecjPlatform.getName());
75    }
76
77    /**
78     * Submitting the results to the service
79     * @param state
80     * @param result
81     */
82    @Override
83    public void finalStatistics(EvolutionState state, int result) {
84        super.finalStatistics(state, result);
85
86
87        Map<String, String> parameters = getParametersToSend(state);
88        ByteArrayOutputStream o = new ByteArrayOutputStream();
89        new XMLEncoder(o).writeObject(state);
90        RunCreationUtil.createRun(parameters, result, new String(o.toByteArray()), getSeed(state), algorithm.getId(), ecjPlatform.getId());
91
92    }
93
94    /**
95     * getting the username out of the config
96     **/
97    protected final String getUsername(EvolutionState state) {
98        return state.parameters.getString(new Parameter(OKB_USER), null);
99    }
100
101    /**
102     * getting the password out of the config
103     *
104     */
105    private String getPassword(EvolutionState state) {
106        return state.parameters.getString(new Parameter(OKB_PASSWORD), null);
107    }
108
109    /**
110     * getting the default seed out of the config
111     * @throws  IllegalArgumentException if the seed is missing
112     */
113    private int getSeed(EvolutionState state) {
114        int returnValue = 0;
115        String seedName = state.parameters.getString(new Parameter(OKB_PARAMETER_SEED), null);
116        if (seedName == null) {
117            throw new IllegalArgumentException("no seed given!");
118        }
119
120        returnValue = state.parameters.getInt(new Parameter(seedName), null);
121
122        return returnValue;
123    }
124
125    /**
126     * getting the parameters out of the config
127     **/
128    private Map<String, String> getParametersToSend(EvolutionState state) {
129        Map<String, String> returnValue = new HashMap<String, String>();
130        for (String name : state.parameters.getString(new Parameter(OKB_PARAMETER_NAME), null).split(";")) {
131            String tmp = state.parameters.getString(new Parameter(name.trim()), null);
132            if (tmp == null) {
133                returnValue.put(name, name);
134            } else {
135                returnValue.put(name, tmp);
136            }
137        }
138        return returnValue;
139    }
140}
Note: See TracBrowser for help on using the repository browser.