source: src/test/java/exampleagentstest/AgentTest.java

Last change on this file was 177, checked in by Wouter Pasman, 6 years ago

#67 fixed BoaPartyExample

File size: 5.2 KB
Line 
1package exampleagentstest;
2
3import static org.junit.Assert.assertNotNull;
4import static org.mockito.Mockito.mock;
5import static org.mockito.Mockito.when;
6
7import java.io.IOException;
8import java.util.Arrays;
9import java.util.HashMap;
10import java.util.List;
11import java.util.Random;
12
13import org.junit.Before;
14import org.junit.Test;
15
16import genius.core.AgentID;
17import genius.core.Bid;
18import genius.core.DomainImpl;
19import genius.core.Global;
20import genius.core.actions.Accept;
21import genius.core.actions.Action;
22import genius.core.actions.EndNegotiation;
23import genius.core.actions.Offer;
24import genius.core.exceptions.InstantiateException;
25import genius.core.issue.Issue;
26import genius.core.issue.IssueDiscrete;
27import genius.core.issue.IssueInteger;
28import genius.core.issue.IssueReal;
29import genius.core.issue.Value;
30import genius.core.issue.ValueInteger;
31import genius.core.issue.ValueReal;
32import genius.core.parties.NegotiationInfo;
33import genius.core.parties.NegotiationParty;
34import genius.core.persistent.PersistentDataContainer;
35import genius.core.timeline.TimeLineInfo;
36import genius.core.utility.AbstractUtilitySpace;
37import genius.core.utility.AdditiveUtilitySpace;
38
39/**
40 * Test an example agent: try to load it, init it etc.
41 */
42public abstract class AgentTest {
43
44 private NegotiationParty party;
45 private Class<? extends NegotiationParty> partyclass;
46 private final String PARTY = "src/test/resources/partydomain/";
47 List<Class<? extends Action>> actions = Arrays.asList(Accept.class,
48 Offer.class, EndNegotiation.class);
49 private NegotiationInfo info;
50 protected PersistentDataContainer persistentData;
51 private AbstractUtilitySpace utilspace;
52 private Random rand = new Random();
53
54 public AgentTest(Class<? extends NegotiationParty> partyclass) {
55 this.partyclass = partyclass;
56 }
57
58 @Before
59 public void before() throws InstantiateException, IOException {
60 party = (NegotiationParty) Global
61 .loadObject(partyclass.getCanonicalName());
62
63 info = mock(NegotiationInfo.class);
64 when(info.getAgentID()).thenReturn(new AgentID("test"));
65 persistentData = mock(PersistentDataContainer.class);
66 when(info.getPersistentData()).thenReturn(persistentData);
67 utilspace = createUtilSpace();
68 when(info.getUtilitySpace()).thenReturn(utilspace);
69 TimeLineInfo timeline = mock(TimeLineInfo.class);
70 when(timeline.getTime()).thenReturn(0.2);
71 when(info.getTimeline()).thenReturn(timeline);
72
73 }
74
75 @Test
76 public void loadClassTest() throws InstantiateException {
77 }
78
79 @Test
80 public void getDescriptionTest() {
81 assertNotNull(party.getDescription());
82 }
83
84 @Test
85 public void getProtocolTest() {
86 assertNotNull(party.getProtocol());
87 }
88
89 @Test
90 public void initTest() throws IOException {
91 party.init(info);
92 }
93
94 @Test
95 public void getActionTest() {
96 party.init(info);
97 party.chooseAction(actions);
98 }
99
100 @Test
101 public void receiveMessageTest() {
102 party.init(info);
103 AgentID other = new AgentID("other");
104 party.receiveMessage(other, new Offer(other, generateRandomBid()));
105 }
106
107 /**
108 * this is tricky. We need a full-fledged utilspace as the agent gets
109 * cracking on it. mocking that would be difficult.
110 *
111 * @throws IOException
112 */
113 private AbstractUtilitySpace createUtilSpace() throws IOException {
114 DomainImpl domain = new DomainImpl(PARTY + "party_domain.xml");
115 return new AdditiveUtilitySpace(domain, PARTY + "party1_utility.xml");
116 }
117
118 protected Bid generateRandomBid() {
119 try {
120 // Pairs <issue number, chosen value string>
121 HashMap<Integer, Value> values = new HashMap<Integer, Value>();
122
123 // For each issue, put a random value
124 for (Issue currentIssue : utilspace.getDomain().getIssues()) {
125 values.put(currentIssue.getNumber(),
126 getRandomValue(currentIssue));
127 }
128
129 // return the generated bid
130 return new Bid(utilspace.getDomain(), values);
131
132 } catch (Exception e) {
133
134 // return empty bid if an error occurred
135 return new Bid(utilspace.getDomain());
136 }
137 }
138
139 /**
140 * Gets a random value for the given issue.
141 *
142 * @param currentIssue
143 * The issue to generate a random value for
144 * @return The random value generated for the issue
145 * @throws Exception
146 * if the issues type is not Discrete, Real or Integer.
147 */
148 protected Value getRandomValue(Issue currentIssue) throws Exception {
149
150 Value currentValue;
151 int index;
152
153 switch (currentIssue.getType()) {
154 case DISCRETE:
155 IssueDiscrete discreteIssue = (IssueDiscrete) currentIssue;
156 index = (rand.nextInt(discreteIssue.getNumberOfValues()));
157 currentValue = discreteIssue.getValue(index);
158 break;
159 case REAL:
160 IssueReal realIss = (IssueReal) currentIssue;
161 index = rand.nextInt(realIss.getNumberOfDiscretizationSteps()); // check
162 // this!
163 currentValue = new ValueReal(realIss.getLowerBound()
164 + (((realIss.getUpperBound() - realIss.getLowerBound()))
165 / (realIss.getNumberOfDiscretizationSteps()))
166 * index);
167 break;
168 case INTEGER:
169 IssueInteger integerIssue = (IssueInteger) currentIssue;
170 index = rand.nextInt(integerIssue.getUpperBound()
171 - integerIssue.getLowerBound() + 1);
172 currentValue = new ValueInteger(
173 integerIssue.getLowerBound() + index);
174 break;
175 default:
176 throw new Exception(
177 "issue type " + currentIssue.getType() + " not supported");
178 }
179
180 return currentValue;
181 }
182
183}
Note: See TracBrowser for help on using the repository browser.