-
Notifications
You must be signed in to change notification settings - Fork 1
/
TaskAgent.java
131 lines (115 loc) · 4.81 KB
/
TaskAgent.java
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
package org.ag.common.agent;
import java.util.Collections;
import java.util.List;
import org.ag.common.env.Node;
import org.ag.common.task.Task;
import net.jcip.annotations.GuardedBy;
import net.jcip.annotations.ThreadSafe;
/**
* This class is a task-oriented agent. It contains a list of tasks that the agent is capable to execute. Only one task
* can be executed at time, the currentTask variable holds the current task in execution.
*
* <p>This class is thread-safe.</p>
*
* @author Luiz Filipe Abrahao <[email protected]>
*
*/
@ThreadSafe
public abstract class TaskAgent extends AbstractAgent {
@GuardedBy("this")
private Task currentTask;
protected final TaskAgentType agentType;
/**
* Construct an agent that is able to execute tasks with an id, a type, the node it will start from and whether it
* should keep track of the nodes it has visited or not.
*
* @param id agent's unique identifier.
* @param agentType agent's type.
* @param currentNode node the agent will start from.
* @param recordNodeHistory whether the agent should record the nodes it has visited or not.
*/
public TaskAgent(final String id, final TaskAgentType agentType,
final Node currentNode, final boolean recordNodeHistory) {
super(id, agentType, currentNode, recordNodeHistory);
this.agentType = agentType;
}
/**
* Construct an agent that is able to execute tasks with an id, a type, and whether it should keep track of the
* nodes it has visited or not.
*
* @param id agent's unique identifier.
* @param agentType agent's type.
* @param recordNodeHistory whether the agent should record the nodes it has visited or not.
*/
public TaskAgent(final String id, final TaskAgentType agentType,
final boolean recordNodeHistory) {
super(id, agentType, recordNodeHistory);
this.agentType = agentType;
}
/**
* Construct an agent that is able to execute tasks with an id, a type, agents created using this constructor will
* not record the nodes they have visited.
*
* @param id agent's unique identifier.
* @param agentType agent's type.
*/
public TaskAgent(final String id, final TaskAgentType agentType) {
super(id, agentType);
this.agentType = agentType;
}
/**
* Returns the tasks the agent is capable of performing depending on its type.
* <p>Note that the list is unmodifiable.</p>
*
* @return list of tasks agent is capable of performing.
*/
public List<Task> getTaskList() {
return Collections.unmodifiableList(agentType.getTasks());
}
/**
* Returns the task the agent is performing at the moment.
*
* @return task the agent is performing.
*/
public synchronized Task getCurrentTask() {
return currentTask;
}
/**
* Sets the task that the agent is performing at the moment. This does not make the agent to actually execute the
* task. This is set by the tasks or the decision controls that actually choose what the agent needs to execute
* next. It is useful to 'tell' the agent which task it is performing at the moment so it might be used for the
* decision making process of other tasks.
*
* @param currentTask tasks that the agent is performing.
*/
public synchronized void setCurrentTask(final Task currentTask) {
this.currentTask = currentTask;
}
/**
* Sets the task that the agent is performing at the moment. This does not make the agent to actually execute the
* task. This is set by the tasks or the decision controls that actually choose what the agent needs to execute
* next. It is useful to 'tell' the agent which task it is performing at the moment so it might be used for the
* decision making process of other tasks.
*
* @param taskName the name of the task the agent is performing.
*/
public synchronized void setCurrentTask(final String taskName) {
this.currentTask = this.getTaskByName(taskName);
}
/**
* Return the task with the given name if the agent is capable of performing it. In case it isn't it throws a
* runtime exception.
*
* @param name name of the task.
* @throws RuntimeException if the agent isn't capable of perform the task with given name.
* @return task with given name.
*/
public Task getTaskByName(final String name) {
for (Task task : agentType.getTasks()) {
if (task.getName().equals(name)) {
return task;
}
}
throw new RuntimeException(this.getId() + " is not capable of performing task " + name);
}
}