1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package zeus.actors.rtn.util;
25
26 import java.util.*;
27 import zeus.util.*;
28 import zeus.actors.*;
29
30 /***
31 * This class defines a negotation strategy, i.e. how the bidding strategy
32 * changes with time and or bids received. Following initialisation,
33 * negotiation proceeds via, first a call to the <code> evaluateFirst </code>
34 * methods, the followed by zero or more calls to the <code> evaluateNext
35 * </code> method. These two call return the following message types:
36 * <table>
37 * <tr> <td> <code> OK </code> </td> <td> the negotiation has
38 * terminated successfully </td> </tr>
39 * <tr> <td> <code> FAIL </code> </td> <td> the negotaition failed </td>
40 * </tr> <tr> <td> <code> WAIT </code> </td> <td> wait until some timeout
41 * period </td> </tr>
42 * <tr> <td> <code> MESSAGE </code> </td> <td> a message needs to be sent
43 * </td> </tr>
44 * </table>
45 *
46 * @see StrategyEvaluatorList
47 */
48 public abstract class StrategyEvaluator {
49 public static final int OK = 0;
50 public static final int FAIL = 1;
51 public static final int WAIT = 2;
52 public static final int MESSAGE = 3;
53
54 protected AgentContext context = null;
55 protected StrategyEvaluatorList evaluators = null;
56 protected boolean isActive = true;
57 protected Vector goals = null;
58 protected ProtocolDbResult protocolInfo = null;
59
60 public void set(AgentContext context) {
61 this.context = context;
62 }
63 public void set(StrategyEvaluatorList evaluators) {
64 this.evaluators = evaluators;
65 }
66
67 public boolean isActive() { return isActive; }
68 public Vector getGoals() { return goals; }
69 public ProtocolDbResult getProtocolInfo() { return protocolInfo; }
70
71 public abstract int evaluateFirst(Vector goals, ProtocolDbResult info);
72 public abstract int evaluateNext(DelegationStruct ds);
73
74 protected String getParam(String param, String default_value) {
75 if ( protocolInfo == null )
76 return default_value;
77 else {
78 String obj = (String)protocolInfo.parameters.get(param);
79 if ( obj == null ) return default_value;
80 return obj;
81 }
82 }
83 protected int getIntParam(String param, int default_value) {
84 if ( protocolInfo == null )
85 return default_value;
86 else {
87 String obj = (String)protocolInfo.parameters.get(param);
88 if ( obj == null ) return default_value;
89 try {
90 return Integer.parseInt(obj);
91 }
92 catch(NumberFormatException e) {
93 Core.USER_ERROR("NumberFormat error in parameter " + param +
94 "=" + obj +
95 " of strategy " + protocolInfo.strategy +
96 " of protocol " + protocolInfo.protocol +
97 " -- integer value expected ");
98 return default_value;
99 }
100 }
101 }
102 protected double getDoubleParam(String param, double default_value) {
103 if ( protocolInfo == null )
104 return default_value;
105 else {
106 String obj = (String)protocolInfo.parameters.get(param);
107 if ( obj == null ) return default_value;
108 try {
109 return (Double.valueOf(obj)).doubleValue();
110 }
111 catch(NumberFormatException e) {
112 Core.USER_ERROR("NumberFormat error in parameter " + param +
113 "=" + obj +
114 " of strategy " + protocolInfo.strategy +
115 " of protocol " + protocolInfo.protocol +
116 " -- double value expected ");
117 return default_value;
118 }
119 }
120 }
121 protected boolean getBooleanParam(String param, boolean default_value) {
122 if ( protocolInfo == null )
123 return default_value;
124 else {
125 String obj = (String)protocolInfo.parameters.get(param);
126 if ( obj == null ) return default_value;
127 try {
128 return (Boolean.valueOf(obj)).booleanValue();
129 }
130 catch(NumberFormatException e) {
131 Core.USER_ERROR("NumberFormat error in parameter " + param +
132 "=" + obj +
133 " of strategy " + protocolInfo.strategy +
134 " of protocol " + protocolInfo.protocol +
135 " -- boolean value expected ");
136 return default_value;
137 }
138 }
139 }
140
141 }