1 | package genius.core.protocol;
|
---|
2 |
|
---|
3 | import java.io.Serializable;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Arrays;
|
---|
6 | import java.util.HashMap;
|
---|
7 |
|
---|
8 | import genius.core.Domain;
|
---|
9 | import genius.core.NegotiationEventListener;
|
---|
10 | import genius.core.exceptions.Warning;
|
---|
11 | import genius.core.repository.AgentRepItem;
|
---|
12 | import genius.core.repository.ProfileRepItem;
|
---|
13 | import genius.core.repository.RepositoryFactory;
|
---|
14 | import genius.core.tournament.VariablesAndValues.AgentParamValue;
|
---|
15 | import genius.core.tournament.VariablesAndValues.AgentParameterVariable;
|
---|
16 | import genius.core.utility.AbstractUtilitySpace;
|
---|
17 | import genius.core.xml.SimpleElement;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Abstract class for the manager of protocols. Implement start() to define the
|
---|
21 | * protocol.
|
---|
22 | */
|
---|
23 | public abstract class Protocol implements Runnable, Serializable {
|
---|
24 | private static final long serialVersionUID = -7994042965683386295L;
|
---|
25 | protected Thread negoThread = null;
|
---|
26 | /**
|
---|
27 | * stopNegotiation indicates that the session has now ended. it is checked
|
---|
28 | * after every call to the agent, and if it happens to be true, session is
|
---|
29 | * immediately returned without any updates to the results list. This is
|
---|
30 | * because killing the thread in many cases will return Agent.getActions()
|
---|
31 | * but with a stale action. By setting stopNegotiation to true before
|
---|
32 | * killing, the agent will still immediately return.
|
---|
33 | */
|
---|
34 | public boolean stopNegotiation = false;
|
---|
35 | public boolean threadFinished = false;
|
---|
36 | private AgentRepItem[] agentRepItems;
|
---|
37 | private ProfileRepItem[] profileRepItems;
|
---|
38 | private String[] agentNames;
|
---|
39 | private HashMap<AgentParameterVariable, AgentParamValue>[] agentParams;
|
---|
40 |
|
---|
41 | /** -- **/
|
---|
42 | protected Domain domain;
|
---|
43 | private AbstractUtilitySpace[] agentUtilitySpaces;
|
---|
44 |
|
---|
45 | ArrayList<NegotiationEventListener> actionEventListener = new ArrayList<NegotiationEventListener>();
|
---|
46 |
|
---|
47 | private SimpleElement fRoot;
|
---|
48 |
|
---|
49 | public abstract String getName();
|
---|
50 |
|
---|
51 | protected int sessionNr = -1;
|
---|
52 | protected int totalSessions;
|
---|
53 |
|
---|
54 | public final void startSession() {
|
---|
55 | Thread protocolThread = new Thread(this);
|
---|
56 | protocolThread.start();
|
---|
57 | }
|
---|
58 |
|
---|
59 | public Protocol(AgentRepItem[] agentRepItems, ProfileRepItem[] profileRepItems,
|
---|
60 | HashMap<AgentParameterVariable, AgentParamValue>[] agentParams, int totalMatches) throws Exception {
|
---|
61 | this.agentRepItems = agentRepItems.clone();
|
---|
62 | this.profileRepItems = profileRepItems.clone();
|
---|
63 | this.totalSessions = totalMatches;
|
---|
64 | if (agentParams != null) {
|
---|
65 | this.agentParams = agentParams.clone();
|
---|
66 | } else {
|
---|
67 | this.agentParams = new HashMap[agentRepItems.length];
|
---|
68 | }
|
---|
69 | loadAgentsUtilitySpaces();
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected void loadAgentsUtilitySpaces() throws Exception {
|
---|
73 | if (domain == null)
|
---|
74 | domain = RepositoryFactory.get_domain_repos().getDomain(profileRepItems[0].getDomain());
|
---|
75 | agentNames = new String[profileRepItems.length];
|
---|
76 | agentNames[0] = "Agent A";
|
---|
77 | agentNames[1] = "Agent B";
|
---|
78 |
|
---|
79 | // load the utility space
|
---|
80 | agentUtilitySpaces = new AbstractUtilitySpace[profileRepItems.length];
|
---|
81 | for (int i = 0; i < profileRepItems.length; i++) {
|
---|
82 | ProfileRepItem profile = profileRepItems[i];
|
---|
83 | agentUtilitySpaces[i] = RepositoryFactory.get_domain_repos().getUtilitySpace(domain, profile);
|
---|
84 | }
|
---|
85 | return;
|
---|
86 |
|
---|
87 | }
|
---|
88 |
|
---|
89 | public void addNegotiationEventListener(NegotiationEventListener listener) {
|
---|
90 | if (!actionEventListener.contains(listener))
|
---|
91 | actionEventListener.add(listener);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public ArrayList<NegotiationEventListener> getNegotiationEventListeners() {
|
---|
95 | return (ArrayList<NegotiationEventListener>) (actionEventListener.clone());
|
---|
96 | }
|
---|
97 |
|
---|
98 | public void removeNegotiationEventListener(NegotiationEventListener listener) {
|
---|
99 | if (!actionEventListener.contains(listener))
|
---|
100 | actionEventListener.remove(listener);
|
---|
101 | }
|
---|
102 |
|
---|
103 | public Domain getDomain() {
|
---|
104 | return domain;
|
---|
105 | }
|
---|
106 |
|
---|
107 | public AgentRepItem getAgentRepItem(int index) {
|
---|
108 | return agentRepItems[index];
|
---|
109 | }
|
---|
110 |
|
---|
111 | public ProfileRepItem getProfileRepItems(int index) {
|
---|
112 | return profileRepItems[index];
|
---|
113 | }
|
---|
114 |
|
---|
115 | public String getAgentName(int index) {
|
---|
116 | return agentNames[index];
|
---|
117 | }
|
---|
118 |
|
---|
119 | public HashMap<AgentParameterVariable, AgentParamValue> getAgentParams(int index) {
|
---|
120 | return agentParams[index];
|
---|
121 | }
|
---|
122 |
|
---|
123 | public AbstractUtilitySpace getAgentUtilitySpaces(int index) {
|
---|
124 | return agentUtilitySpaces[index];
|
---|
125 | }
|
---|
126 |
|
---|
127 | public int getNumberOfAgents() {
|
---|
128 | return agentRepItems.length;
|
---|
129 | }
|
---|
130 |
|
---|
131 | public void stopNegotiation() {
|
---|
132 | if (negoThread != null && negoThread.isAlive()) {
|
---|
133 | try {
|
---|
134 | stopNegotiation = true; // see comments in sessionrunner..
|
---|
135 | negoThread.interrupt();
|
---|
136 | /*
|
---|
137 | * This will throw a ThreadDeath Error into the nego thread. The
|
---|
138 | * nego thread will catch this and exit immediately. Maybe it
|
---|
139 | * should not even try to catch that. Previously we tried
|
---|
140 | * negoThread.stop() as well but that was abandoned later.
|
---|
141 | */
|
---|
142 | } catch (Exception e) {
|
---|
143 | new Warning("problem stopping the nego", e);
|
---|
144 | }
|
---|
145 | }
|
---|
146 | return;
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public int hashCode() {
|
---|
151 | final int prime = 31;
|
---|
152 | int result = 1;
|
---|
153 | result = prime * result + Arrays.hashCode(agentNames);
|
---|
154 | result = prime * result + Arrays.hashCode(agentParams);
|
---|
155 | result = prime * result + Arrays.hashCode(agentRepItems);
|
---|
156 | result = prime * result + Arrays.hashCode(agentUtilitySpaces);
|
---|
157 | result = prime * result + ((domain == null) ? 0 : domain.hashCode());
|
---|
158 | result = prime * result + Arrays.hashCode(profileRepItems);
|
---|
159 | return result;
|
---|
160 | }
|
---|
161 |
|
---|
162 | @Override
|
---|
163 | public String toString() {
|
---|
164 | return Arrays.toString(agentRepItems) + " on " + Arrays.toString(profileRepItems);
|
---|
165 | }
|
---|
166 |
|
---|
167 | public int getSessionNumber() {
|
---|
168 | return sessionNr;
|
---|
169 | }
|
---|
170 |
|
---|
171 | public int getTotalSessions() {
|
---|
172 | return totalSessions;
|
---|
173 | }
|
---|
174 | } |
---|