1 | package genius.core.repository;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.IOException;
|
---|
5 |
|
---|
6 | import javax.xml.bind.annotation.XmlAttribute;
|
---|
7 | import javax.xml.bind.annotation.XmlRootElement;
|
---|
8 |
|
---|
9 | import genius.core.Agent;
|
---|
10 | import genius.core.Global;
|
---|
11 | import genius.core.exceptions.InstantiateException;
|
---|
12 | import genius.core.exceptions.Warning;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * This repository item contains all info about an agent that can be loaded.
|
---|
16 | *
|
---|
17 | * @author wouter
|
---|
18 | */
|
---|
19 | @XmlRootElement
|
---|
20 | public class AgentRepItem implements RepItem {
|
---|
21 | private static final long serialVersionUID = 2395318378966487611L;
|
---|
22 | /**
|
---|
23 | * the key: short but unique name of the agent as it will be known in the
|
---|
24 | * nego system. This is an arbitrary but unique label for this TYPE of
|
---|
25 | * agent. Note that there may still be multiple actual agents of this type
|
---|
26 | * during a negotiation.
|
---|
27 | */
|
---|
28 | @XmlAttribute
|
---|
29 | private String agentName;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * This can now be two things:
|
---|
33 | * <ul>
|
---|
34 | * <li>a class path, eg "agents.anac.y2010.AgentFSEGA.AgentFSEGA". In this
|
---|
35 | * case, the agent must be on the class path to load.
|
---|
36 | * <li>a full path, eg
|
---|
37 | * "/Volumes/documents/NegoWorkspace3/NegotiatorGUI/src/agents/anac/y2010/AgentFSEGA/AgentFSEGA.java"
|
---|
38 | * . In this case, we can figure out the class path ourselves, but the ref
|
---|
39 | * is system dependent (backslashes on windows) and might be absolute path.
|
---|
40 | * </ul>
|
---|
41 | */
|
---|
42 | @XmlAttribute
|
---|
43 | private String classPath;
|
---|
44 | /** description of this agent */
|
---|
45 | @XmlAttribute
|
---|
46 | private String description;
|
---|
47 | /** Parameters of the agent, for example a concession parameter */
|
---|
48 | @XmlAttribute
|
---|
49 | private String params;
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * @return true if agentName and classPath equal. Note that agentName alone
|
---|
53 | * is sufficient to be equal as keys are unique.
|
---|
54 | */
|
---|
55 | public boolean equals(Object o) {
|
---|
56 | if (!(o instanceof AgentRepItem))
|
---|
57 | return false;
|
---|
58 | return agentName.equals(((AgentRepItem) o).agentName) && classPath.equals(((AgentRepItem) o).classPath);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * XML serializer needs this.
|
---|
63 | */
|
---|
64 | @SuppressWarnings("unused")
|
---|
65 | private AgentRepItem() {
|
---|
66 | }
|
---|
67 |
|
---|
68 | public AgentRepItem(String aName, String cPath, String desc) {
|
---|
69 | agentName = aName;
|
---|
70 | classPath = cPath;
|
---|
71 | description = desc;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public AgentRepItem(String aName, String cPath, String desc, String param) {
|
---|
75 | agentName = aName;
|
---|
76 | classPath = cPath;
|
---|
77 | description = desc;
|
---|
78 | params = param;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * construct the item given the file. We check that the file actually loads
|
---|
83 | * in and throw if we can't load it. name will be set to
|
---|
84 | * {@link Agent#getName()} of the file. description will be constructed
|
---|
85 | * using name and {@link Agent#getVersion()}.
|
---|
86 | *
|
---|
87 | *
|
---|
88 | * @param classFile
|
---|
89 | * @throws ClassNotFoundException
|
---|
90 | * @throws IllegalAccessException
|
---|
91 | * @throws InstantiationException
|
---|
92 | * @throws IllegalArgumentException
|
---|
93 | * @throws ClassCastException
|
---|
94 | * @throws IOException
|
---|
95 | */
|
---|
96 | public AgentRepItem(File classFile) throws InstantiationException, IllegalAccessException, ClassNotFoundException,
|
---|
97 | ClassCastException, IllegalArgumentException, IOException {
|
---|
98 | Agent agent = (Agent) Global.loadClassFromFile(classFile);
|
---|
99 | String version = agent.getVersion();
|
---|
100 | agentName = agent.getName();
|
---|
101 | if (agentName == null) {
|
---|
102 | agentName = classFile.getName();
|
---|
103 | }
|
---|
104 | String explain = agent.getSupportedNegotiationSetting().toExplainingString();
|
---|
105 | if (!explain.isEmpty()) {
|
---|
106 | agentName = agentName + "(" + explain + ")";
|
---|
107 | }
|
---|
108 | classPath = classFile.getCanonicalPath();
|
---|
109 |
|
---|
110 | description = agentName
|
---|
111 | + (version == null || version.equals("unknown") || version.isEmpty() ? "" : " (" + version + ")");
|
---|
112 |
|
---|
113 | }
|
---|
114 |
|
---|
115 | public String getName() {
|
---|
116 | return agentName;
|
---|
117 | }
|
---|
118 |
|
---|
119 | public String getClassPath() {
|
---|
120 | return classPath;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Get the version of this agent.
|
---|
125 | *
|
---|
126 | * @return version of this agent. Returns ERR if something goes wrong.
|
---|
127 | * Returns "" if version is null.
|
---|
128 | */
|
---|
129 | public String getVersion() {
|
---|
130 |
|
---|
131 | try {
|
---|
132 | String ver = getInstance().getVersion();
|
---|
133 | if (ver != null) {
|
---|
134 | return ver;
|
---|
135 | }
|
---|
136 | return "";
|
---|
137 | } catch (Exception e) {
|
---|
138 | new Warning("can't get version for " + agentName + " :", e);
|
---|
139 | e.printStackTrace();
|
---|
140 | }
|
---|
141 | return "ERR";
|
---|
142 | }
|
---|
143 |
|
---|
144 | public String getParams() {
|
---|
145 | return params;
|
---|
146 | }
|
---|
147 |
|
---|
148 | public String getDescription() {
|
---|
149 | return description;
|
---|
150 | }
|
---|
151 |
|
---|
152 | public String toString() {
|
---|
153 | return agentName;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Try to load the agent that this reference points to.
|
---|
158 | *
|
---|
159 | * @return {@link Agent}
|
---|
160 | * @throws InstantiateException
|
---|
161 | * if agent can't be loaded
|
---|
162 | */
|
---|
163 | public Agent getInstance() throws InstantiateException {
|
---|
164 | return Global.loadAgent(classPath);
|
---|
165 | }
|
---|
166 | } |
---|