EnergyModelFactory.java 5 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
/*
 * 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.util.List;
import java.util.Random;
import java.util.Vector;

import de.tud.kom.p2psim.api.common.SimHost;
28
import de.tud.kom.p2psim.api.energy.Battery;
29
30
31
import de.tud.kom.p2psim.api.energy.EnergyConfiguration;
import de.tud.kom.p2psim.api.energy.EnergyModel;
import de.tud.kom.p2psim.api.scenario.ConfigurationException;
32
import de.tud.kom.p2psim.impl.energy.models.ComponentBasedEnergyModel;
33
import de.tudarmstadt.maki.simonstrator.api.Host;
34
35
import de.tudarmstadt.maki.simonstrator.api.Monitor;
import de.tudarmstadt.maki.simonstrator.api.Monitor.Level;
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
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 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;
	
65
66
67
68
	private boolean useRandomBatteryStartConfiguration = false;
	private double minimumStartEnergyLevel = 0.3;
		
	
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
	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;
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
		
		Battery bat;
		
		if(useRandomBatteryStartConfiguration)
		{
			// clone the battery from the provided model
			// FIXME Different Capacities for different nodes.
			double percentageOfFullBattery = (1 - random.nextDouble());
			if(percentageOfFullBattery < minimumStartEnergyLevel)
				percentageOfFullBattery = minimumStartEnergyLevel;
			double initialEnergy = batteryModel.getMaximumEnergyLevel() * percentageOfFullBattery;
			bat = new SimpleBattery(batteryModel.getMaximumEnergyLevel(), initialEnergy);
		}
		else {			
			bat = batteryModel.clone();
		}
104

105
		EnergyModel em = new ComponentBasedEnergyModel(host, bat);
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
137
138
		
		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
139
140
141
		Monitor.log(EnergyModelFactory.class, Level.INFO,
				"EnergyModel created - some information:\n %s",
				energyConfig.getHelp());
142
143
144
		energyConfigurations.add(energyConfig);
	}

145
	public void setMinimumStartEnergyLevel(double level) {
146
147
148
149
150
151
152
153
154
155
		if(level > 1.0) {
			this.minimumStartEnergyLevel = 1.0;
		}
		else if(level < 0.0) {
			this.minimumStartEnergyLevel = 0.0;
		}
		else {
			this.minimumStartEnergyLevel = level;
		}
	}
156
157
	
	public void setUseRandomBatteryStartConfiguration(boolean randomStart) {
158
159
		this.useRandomBatteryStartConfiguration = randomStart;
	}
160
161

}