1 | package genius.core.repository;
|
---|
2 |
|
---|
3 | import javax.xml.bind.annotation.XmlAttribute;
|
---|
4 | import javax.xml.bind.annotation.XmlRootElement;
|
---|
5 |
|
---|
6 | import agents.rlboa.RLBOA;
|
---|
7 | import genius.core.Agent;
|
---|
8 | import genius.core.Global;
|
---|
9 | import genius.core.exceptions.InstantiateException;
|
---|
10 | import genius.core.parties.Mediator;
|
---|
11 | import genius.core.parties.NegotiationParty;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * This repository item contains all info about a {@link NegotiationParty} that
|
---|
15 | * can be loaded. immutable.
|
---|
16 | */
|
---|
17 |
|
---|
18 | @SuppressWarnings("serial")
|
---|
19 | @XmlRootElement(name = "party")
|
---|
20 | public class PartyRepItem extends ParticipantRepItem {
|
---|
21 |
|
---|
22 | private final static String FAILED = "FAILED TO LOAD";
|
---|
23 | /**
|
---|
24 | * This can be two things:
|
---|
25 | * <ul>
|
---|
26 | * <li>a class path, eg "agents.anac.y2010.AgentFSEGA.AgentFSEGA". In this
|
---|
27 | * case, the agent must be on the class path to load.
|
---|
28 | * <li>a full path, eg
|
---|
29 | * "/Volumes/documents/NegoWorkspace3/NegotiatorGUI/src/agents/anac/y2010/AgentFSEGA/AgentFSEGA.java"
|
---|
30 | * . In this case, we can figure out the class path ourselves and load it.
|
---|
31 | * </ul>
|
---|
32 | */
|
---|
33 | @XmlAttribute
|
---|
34 | protected String classPath = "";
|
---|
35 |
|
---|
36 | @XmlAttribute
|
---|
37 | protected String strategyParameters = "";
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Name, also a short version of the class path.
|
---|
41 | */
|
---|
42 | private String partyName = FAILED;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * description of this agent. Cached, Not saved to XML.
|
---|
46 | */
|
---|
47 | private String description = FAILED;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * True if this party is a mediator.
|
---|
51 | */
|
---|
52 | private Boolean isMediator = false;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * needed to support XML de-serialization.
|
---|
56 | *
|
---|
57 | * @throws InstantiateException
|
---|
58 | */
|
---|
59 | @SuppressWarnings("unused")
|
---|
60 | private PartyRepItem() throws InstantiateException {
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | *
|
---|
65 | * @param path
|
---|
66 | * full.path.to.class or file name.
|
---|
67 | */
|
---|
68 | public PartyRepItem(String path) {
|
---|
69 | if (path == null) {
|
---|
70 | throw new NullPointerException(path = null);
|
---|
71 | }
|
---|
72 | classPath = path;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Init our fields to cache the party information. party must have been set
|
---|
77 | * before getting here.
|
---|
78 | *
|
---|
79 | * @param party
|
---|
80 | * @throws InstantiateException
|
---|
81 | */
|
---|
82 | @Override
|
---|
83 | protected NegotiationParty init() throws InstantiateException {
|
---|
84 | NegotiationParty party1 = super.init();
|
---|
85 | partyName = getRepName(party1);
|
---|
86 | description = party1.getDescription();
|
---|
87 | isMediator = party1 instanceof Mediator;
|
---|
88 | return party1;
|
---|
89 | }
|
---|
90 |
|
---|
91 | private String getRepName(NegotiationParty party)
|
---|
92 | {
|
---|
93 | String name = party.getClass().getSimpleName();
|
---|
94 | // Agents can implement getName() for a better party name
|
---|
95 | if (party instanceof Agent)
|
---|
96 | {
|
---|
97 | String agentName = ((Agent) party).getName();
|
---|
98 | if (agentName != null && !"".equals(agentName))
|
---|
99 | name = agentName;
|
---|
100 | }
|
---|
101 | return name;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * @return classpath, either as full package class path (with dots) or as an
|
---|
106 | * absolute path to a .class file.
|
---|
107 | */
|
---|
108 | public String getClassPath() {
|
---|
109 | return classPath;
|
---|
110 | }
|
---|
111 |
|
---|
112 | public String getName() {
|
---|
113 | try {
|
---|
114 | initSilent();
|
---|
115 | } catch (InstantiateException e) {
|
---|
116 | return FAILED + " " + classPath;
|
---|
117 | }
|
---|
118 | return partyName;
|
---|
119 | }
|
---|
120 |
|
---|
121 | public String getDescription() {
|
---|
122 | try {
|
---|
123 | initSilent();
|
---|
124 | } catch (InstantiateException e) {
|
---|
125 | return e.toString();
|
---|
126 | }
|
---|
127 | return description;
|
---|
128 | }
|
---|
129 |
|
---|
130 | public String toString() {
|
---|
131 | try {
|
---|
132 | initSilent();
|
---|
133 | } catch (InstantiateException e) {
|
---|
134 | return e.toString();
|
---|
135 | }
|
---|
136 | return "PartyRepositoryItem[" + partyName + "," + classPath + "," + description + ", is mediator="
|
---|
137 | + isMediator().toString() + "]";
|
---|
138 | }
|
---|
139 |
|
---|
140 | public Boolean isMediator() {
|
---|
141 | try {
|
---|
142 | initSilent();
|
---|
143 | } catch (InstantiateException e) {
|
---|
144 | return false; // guess, we can't do much here.
|
---|
145 | }
|
---|
146 | return isMediator;
|
---|
147 | }
|
---|
148 |
|
---|
149 | public NegotiationParty load() throws InstantiateException {
|
---|
150 |
|
---|
151 | NegotiationParty party = (NegotiationParty) Global.loadObject(classPath);
|
---|
152 |
|
---|
153 | // [09/08/18 jasperon] Added this and the property strategyParameters because we couldn't find
|
---|
154 | // any place where this method is called. the check for RLBOA is purposely
|
---|
155 | // narrow because of unkown effects on other agents.
|
---|
156 | if (party instanceof RLBOA) {
|
---|
157 | party = (NegotiationParty) Global.loadAgent(classPath, strategyParameters);
|
---|
158 | }
|
---|
159 |
|
---|
160 | return party;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * @return true if partyName and classPath equal. Note that partyName alone
|
---|
165 | * is sufficient to be equal as keys are unique.
|
---|
166 | */
|
---|
167 | @Override
|
---|
168 | public boolean equals(Object o) {
|
---|
169 | if (!(o instanceof PartyRepItem))
|
---|
170 | return false;
|
---|
171 | return classPath.equals(((PartyRepItem) o).classPath);
|
---|
172 | }
|
---|
173 |
|
---|
174 | @Override
|
---|
175 | public String getUniqueName() {
|
---|
176 | return Global.shortNameOfClass(classPath);
|
---|
177 | }
|
---|
178 |
|
---|
179 | @Override
|
---|
180 | public String getClassDescriptor() {
|
---|
181 | return getClassPath();
|
---|
182 | }
|
---|
183 | } |
---|