EnergyModelFactory.java 4.32 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM 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 3 of the License, or
 * any later version.
 * 
 * PeerfactSim.KOM 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 PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package de.tud.kom.p2psim.impl.energy;

import java.awt.RadialGradientPaint;
import java.util.List;
import java.util.Random;
import java.util.Vector;

import org.apache.log4j.Logger;

import de.tud.kom.p2psim.api.common.SimHost;
import de.tud.kom.p2psim.api.energy.EnergyConfiguration;
import de.tud.kom.p2psim.api.energy.EnergyModel;
import de.tud.kom.p2psim.api.scenario.ConfigurationException;
import de.tud.kom.p2psim.impl.simengine.Simulator;
import de.tud.kom.p2psim.impl.util.logging.SimLogger;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.component.HostComponentFactory;

/**
 * This factory creates and configures an {@link EnergyModel} and its
 * components. Again, we use the LinkLayer-model of configuration presets to
 * ensure a proper configuration and integration, where the model is not
 * configured itself but the user select a configuration that provides some
 * checking of the selected parameters.
 * 
 * @author Bjoern Richerzhagen
 * @version 1.0, 25.02.2012
 */
public class EnergyModelFactory implements HostComponentFactory {
	
	private static Logger log = SimLogger.getLogger(EnergyModelFactory.class);
	
	private Random random;

	/**
	 * A List of the currently active MAC-Layer configurations
	 */
	private List<EnergyConfiguration> energyConfigurations;

	/**
	 * This is used to reset configurations if a new Group of Hosts is to be
	 * configured. We imply that the Model is fully configured for each Group or
	 * not at all to keep the settings of the previous group. 
	 */
	private boolean configurationsUsed = false;
	
	private Battery batteryModel = new SimpleBattery(186480000, 186480000);

	/**
	 * A new Factory
	 */
	public EnergyModelFactory() {
		energyConfigurations = new Vector<EnergyConfiguration>();
		random = Randoms.getRandom(EnergyModelFactory.class);
	}

	@Override
	public EnergyModel createComponent(Host pHost) {
		SimHost host = (SimHost) pHost;
		if (energyConfigurations.isEmpty()) {
			throw new ConfigurationException(
					"No Components were specified within the EnergyModel!\n"
							+ "Add at least one child element <Component class=\"...\" /> pointing to an EnergyConfiguration.");
		}
		configurationsUsed = true;

		// clone the battery from the provided model
		// FIXME Different Capacities for different nodes.
		double percentageOfFullBattery = (1 - random.nextDouble());
		if(percentageOfFullBattery < 0.3)
			percentageOfFullBattery = 0.3;
		double initialEnergy = 186480000 * percentageOfFullBattery;
		Battery bat = new SimpleBattery(186480000, initialEnergy);
//		Battery bat = batteryModel.clone();
		EnergyModel em = new ModularEnergyModel(host, bat);
		
		for (EnergyConfiguration config : energyConfigurations) {
			em.registerComponent(config.getConfiguredEnergyComponent(host));
		}
		return em;
	}

	/**
	 * Set the Battery model that is to be used in this simulation.
	 * 
	 * @param energy
	 */
	public void setBattery(Battery battery) {
		this.batteryModel = battery;
	}

	/**
	 * Load a configuration for this ModelFactory
	 * 
	 * @param energyConfig
	 */
	public void setComponent(EnergyConfiguration energyConfig) {
		if (configurationsUsed) {
			energyConfigurations.clear();
			configurationsUsed = false;
		}
		// check configuration
		if (!energyConfig.isWellConfigured()) {
			throw new ConfigurationException(
					"The EnergyModel was not well configured.\n"
							+ energyConfig.getHelp());
		}
		// just log the information
		log.info("EnergyModel created - some information:\n"
				+ energyConfig.getHelp());
		energyConfigurations.add(energyConfig);
	}


}