View Javadoc

1   package cz.cuni.amis.pogamut.base.agent.utils.runner.impl;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import cz.cuni.amis.pogamut.base.agent.params.IAgentParameters;
7   import cz.cuni.amis.pogamut.base.agent.utils.runner.IAgentDescriptor;
8   import cz.cuni.amis.pogamut.base.agent.utils.runner.IMultipleAgentRunner;
9   import cz.cuni.amis.pogamut.base.factory.guice.GuiceAgentModule;
10  
11  /**
12   * Base implementation of the {@link IAgentDescriptor}. It specify which agents and how many should
13   * be instantiated and which parameters they should be given. 
14   * <p><p>
15   * See the interface ({@link IAgentDescriptor}) for more info.
16   * <p><p>
17   * Have checked the javadoc for {@link IAgentDescriptor}? Good. Now you might be wondering, about number
18   * of agents that are going to be instantiated. Here are the scenarios again:
19   * <ol>
20   * <li>You will specify only number of agents via {@link AgentDescriptor#setCount(int)} - in this case, the {@link IMultipleAgentRunner} 
21   * will instantiate specified number of agents with default parameters</li>
22   * <li>You will specify only parameters of the agents via either {@link AgentDescriptor#setAgentParameters(IAgentParameters[])} or {@link AgentDescriptor#addParams(IAgentParameters...)} - 
23   * in this case, the {@link IMultipleAgentRunner} will instantiate the same number of agents as there are parameters</li>
24   * <li>You will specify both, count and parameters - in this case it depends on the relation between set count and number of parameters<p>
25   * If count > number of parameters - the {@link IMultipleAgentRunner} will instantiate 'count' of agents and first 'number of parameters' will have custom params as specified, rest will have defaults<p>
26   * If count == number of parameters - same as the case 2<p>
27   * If count < number of parameters - the {@link IMultipleAgentRunner} will instancitate 'count' of agents using first 'count' of 'parameters' passed
28   * </li> 
29   * </ol>
30   * The exact number of agents that will be instantiated can be obtained from the {@link AgentDescriptor#getCount()} that
31   * not necessarily match the number you will pass into it through {@link AgentDescriptor#setCount(int)} because of described
32   * possibilities.
33   * 
34   * @author Jimmy
35   *
36   * @param <PARAMS>
37   * @param <MODULE>
38   */
39  public class AgentDescriptor<PARAMS extends IAgentParameters, MODULE extends GuiceAgentModule> implements IAgentDescriptor<PARAMS, MODULE>{
40  
41  	private MODULE module = null;
42  	private List<PARAMS> params = new ArrayList<PARAMS>();
43  	private int count = -1;
44  	
45  	@Override
46  	public MODULE getAgentModule() {
47  		return module;
48  	}
49  
50  	/**
51  	 * Sets agent module to be used for the instantiation of the agent.
52  	 * <p><p>
53  	 * For more info see {@link IAgentDescriptor#getAgentModule()}.
54  	 * 
55  	 * @param module
56  	 * @return this instance
57  	 */
58  	public AgentDescriptor<PARAMS, MODULE> setAgentModule(MODULE module) {
59  		this.module = module;
60  		return this;
61  	}
62  	
63  	@Override
64  	public PARAMS[] getAgentParameters() {
65  		return (PARAMS[]) params.toArray(new IAgentParameters[0]);
66  	}
67  	
68  	/**
69  	 * Clears all the params stored within {@link AgentDescriptor} and assigns 'params'.
70  	 * <p><p>
71  	 * For more info see {@link IAgentDescriptor#getAgentParameters()}.
72  	 *  
73  	 * @param params
74  	 * @return
75  	 */
76  	public AgentDescriptor<PARAMS, MODULE> setAgentParameters(PARAMS[] params) {
77  		this.params.clear();
78  		if (params == null) return this;
79  		for (PARAMS param : params) {
80  			this.params.add(param);
81  		}
82  		return this;
83  	}
84  	
85  	/**
86  	 * Adds parameters for another agents. Note that the number of parameters must be the same as the count
87  	 * of agents specified via {@link AgentDescriptor#setCount(int)}.
88  	 * <p><p>
89  	 * For more info see {@link IAgentDescriptor#getAgentParameters()}.
90  	 * 
91  	 * @param params
92  	 * @return this instance
93  	 */
94  	public AgentDescriptor<PARAMS, MODULE> addParams(PARAMS... params) {
95  		if (params == null) return this;
96  		for (PARAMS param : params) {
97  			this.params.add(param);
98  		}
99  		return this;
100 	}
101 	
102 	/**
103 	 * Returns number of agents to be instantiated using current {@link AgentDescriptor#getAgentModule()}.
104 	 * <p><p> 
105      * If {@link AgentDescriptor#setCount(int)} is called with positive int param, that this will return the passed
106      * count. Otherwise it returns the {@link AgentDescriptor#params}.size(). I.e. the number of parameters you've added via {@link AgentDescriptor#addParams(IAgentParameters)} or set
107 	 * via {@link AgentDescriptor#setAgentParameters(IAgentParameters[])}.
108 	 * <p><p>
109 	 * For more info see {@link IAgentDescriptor#getCount()} and javadoc for {@link IAgentDescriptor} and {@link AgentDescriptor}.
110 	 */
111 	@Override
112 	public int getCount() {
113 		if (count == -1) return params.size();
114 		if (count > params.size()) return count;
115 		if (count == params.size()) return count;
116 		// count < params.size()
117 		return count;
118 	}
119 	
120 	/**
121 	 * Set number of agents to be instantiated.
122 	 * <p><p>
123 	 * For more info see {@link IAgentDescriptor#getCount()}.
124 	 * 
125 	 * @param count
126 	 * @return
127 	 */
128 	public AgentDescriptor<PARAMS, MODULE> setCount(int count) {
129 		this.count = count;
130 		return this;
131 	}
132 
133 }