aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/modules/Module.java
blob: 5dea93e1592dbf6fc34e71d16a7e8417282817ec (plain)
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
package jcgp.backend.modules;

import java.util.ArrayList;

import jcgp.backend.parameters.Parameter;
import jcgp.backend.resources.Resources;

/**
 * This class defines the expected behaviour of a module. Generally, modules
 * are entities which contain parameters; these can be retrieved using
 * {@code getLocalParameters()}. GUIs should make use of this getter
 * to display visual parameter controls to users. Subclasses don't have direct
 * access to the list; instead they must use {@code registerParameter()} (ideally
 * in the constructor) to make sure the parameters are returned.
 * <br>
 * In addition, implementations of {@code Module} should specify a module name
 * in their constructor using {@code setName()}. If a name is not provided, 
 * the simple name of the class will be used.
 * <br>
 * All modules, by definition, contain a reference to the experiment's resources, which
 * must be passed at construction. The resources should be accessed with {@code getResources()}.
 * 
 * @see Parameter
 * @author Eduardo Pedroni
 *
 */
public abstract class Module {
	
	private ArrayList<Parameter<?>> localParameters = new ArrayList<Parameter<?>>();
	private String name = getClass().getSimpleName();
	private Resources resources;
	
	/**
	 * Makes a new instance of this class. This should never
	 * be called directly, and should instead be used by subclasses.
	 * 
	 * @param resources a reference to the experiment's resources.
	 */
	protected Module(Resources resources) {
		this.resources = resources;
	}
		
	/**
	 * This method is used by the GUI in order to build visual
	 * representations of all parameters used by the module.
	 * Therefore, any parameters returned here will be displayed
	 * visually.
	 * 
	 * @return a list of generic parameters exposed by the module.
	 */
	public ArrayList<Parameter<?>> getLocalParameters() {
		return localParameters;
	}
	
	/**
	 * Adds one or more parameters to the list of local parameters.
	 * The same parameter cannot be added twice.
	 * <br><br>
	 * Implementations of {@code Module} should use this module
	 * to register any parameters they wish to expose to the user
	 * if a GUI is in use.
	 * 
	 * @param newParameters the parameter(s) to add to the list.
	 */
	protected void registerParameters(Parameter<?>... newParameters) {
		for (int i = 0; i < newParameters.length; i++) {
			if (!localParameters.contains(newParameters[i])) {
				localParameters.add(newParameters[i]);
			}
		}
	}
	
	/**
	 * Sets the name of the module, for GUI display.
	 * If no name is set, the simple name of the class
	 * is be used instead.
	 * 
	 * @param name the name to set.
	 */
	protected void setName(String name) {
		this.name = name;
	}
	
	/**
	 * @return the experiment's resources.
	 */
	protected Resources getResources() {
		return resources;
	}
	
	@Override
	public String toString() {
		return name;
	}
	
}