1 2 5 14 package org.jacorb.trading.constraint; 15 16 17 import java.io.*; 18 import java.util.*; 19 20 21 public class Preference 22 { 23 private PropertySchema m_schema; 24 private Expression m_expr; 25 private ValueType m_exprType; 26 private int m_prefType = Lex.PREF_FIRST; 27 28 29 private Preference() 30 { 31 } 32 33 34 public Preference(PropertySchema schema) 35 { 36 m_schema = schema; 37 } 38 39 40 public void parse(String pref) 41 throws ParseException 42 { 43 StringReader reader = new StringReader(pref); 44 Lex lex = new Lex(reader); 45 46 m_prefType = lex.getToken(); 47 switch (m_prefType) { 48 case Lex.PREF_MIN: 49 case Lex.PREF_MAX: { 50 m_expr = new Expression(m_schema); 51 52 lex.nextToken(); 55 m_exprType = m_expr.parse(lex); 56 57 if (! m_exprType.isNumber() || m_exprType.isSequence()) 60 throw new ParseException( 61 "min/max preference expression must be numeric"); 62 } 63 break; 64 65 case Lex.PREF_WITH: { 66 m_expr = new Expression(m_schema); 67 68 lex.nextToken(); 71 m_exprType = m_expr.parse(lex); 72 73 if (! ValueType.isCompatible(m_exprType.getId(), ValueType.BOOLEAN) || 76 m_exprType.isSequence()) 77 throw new ParseException( 78 "with preference expression must be boolean"); 79 } 80 break; 81 82 case Lex.PREF_RANDOM: 83 case Lex.PREF_FIRST: 84 break; 86 87 case Lex.END: 88 m_prefType = Lex.PREF_FIRST; 90 break; 91 92 default: 93 throw new ParseException("invalid preference expression"); 94 } 95 } 96 97 98 public Vector order(Vector sources) 99 { 100 Vector result = null; 101 102 switch (m_prefType) { 103 case Lex.PREF_MIN: 104 result = orderMin(sources); 105 break; 106 107 case Lex.PREF_MAX: 108 result = orderMax(sources); 109 break; 110 111 case Lex.PREF_WITH: 112 result = orderWith(sources); 113 break; 114 115 case Lex.PREF_RANDOM: 116 result = orderRandom(sources); 117 break; 118 119 case Lex.PREF_FIRST: 120 result = orderFirst(sources); 121 break; 122 } 123 124 return result; 125 } 126 127 128 protected class SourceValue 129 { 130 public PropertySource source; 131 public Value value; 132 133 public SourceValue(PropertySource src, Value nv) 134 { 135 source = src; 136 value = nv; 137 } 138 } 139 140 141 protected Vector orderMin(Vector sources) 142 { 143 Vector result = new Vector(); 144 145 Vector temp = (Vector)sources.clone(); 148 149 Vector values = new Vector(); 152 153 Enumeration e = sources.elements(); 154 while (e.hasMoreElements()) { 155 PropertySource src = (PropertySource)e.nextElement(); 156 Value v = m_expr.evaluate(src); 157 158 if (v != null) { 159 Enumeration n = values.elements(); 162 int pos = 0; 163 while (n.hasMoreElements()) { 164 SourceValue sv = (SourceValue)n.nextElement(); 165 if (v.lessThan(sv.value)) 166 break; 167 pos++; 168 } 169 170 values.insertElementAt(new SourceValue(src, v), pos); 172 173 temp.removeElement(src); 176 } 177 } 178 179 e = values.elements(); 181 while (e.hasMoreElements()) { 182 SourceValue sv = (SourceValue)e.nextElement(); 183 result.addElement(sv.source); 184 } 185 186 e = temp.elements(); 188 while (e.hasMoreElements()) 189 result.addElement(e.nextElement()); 190 191 return result; 192 } 193 194 195 protected Vector orderMax(Vector sources) 196 { 197 Vector result = new Vector(); 198 199 Vector temp = (Vector)sources.clone(); 202 203 Vector values = new Vector(); 206 207 Enumeration e = sources.elements(); 208 while (e.hasMoreElements()) { 209 PropertySource src = (PropertySource)e.nextElement(); 210 Value v = m_expr.evaluate(src); 211 212 if (v != null) { 213 Enumeration n = values.elements(); 216 int pos = 0; 217 while (n.hasMoreElements()) { 218 SourceValue sv = (SourceValue)n.nextElement(); 219 if (v.greaterThan(sv.value)) 220 break; 221 pos++; 222 } 223 224 values.insertElementAt(new SourceValue(src, v), pos); 226 227 temp.removeElement(src); 230 } 231 } 232 233 e = values.elements(); 235 while (e.hasMoreElements()) { 236 SourceValue sv = (SourceValue)e.nextElement(); 237 result.addElement(sv.source); 238 } 239 240 e = temp.elements(); 242 while (e.hasMoreElements()) 243 result.addElement(e.nextElement()); 244 245 return result; 246 } 247 248 249 protected Vector orderWith(Vector sources) 250 { 251 Vector result = new Vector(); 252 253 Vector temp = (Vector)sources.clone(); 256 257 Enumeration e = sources.elements(); 258 while (e.hasMoreElements()) { 259 PropertySource src = (PropertySource)e.nextElement(); 260 Value v = m_expr.evaluate(src); 261 262 if (v != null) { 263 Boolean b = (Boolean )v.getValue(); 264 if (b.booleanValue()) { 265 result.addElement(src); 267 268 temp.removeElement(src); 271 } 272 } 273 } 274 275 e = temp.elements(); 277 while (e.hasMoreElements()) 278 result.addElement(e.nextElement()); 279 280 return result; 281 } 282 283 284 protected Vector orderRandom(Vector sources) 285 { 286 Vector result = new Vector(); 287 288 int entries = sources.size(); 290 PropertySource[] arr = new PropertySource[entries]; 291 sources.copyInto((java.lang.Object [])arr); 292 293 Random rand = new Random(); 294 295 int count = 0; 297 while (count < entries) { 298 int idx = Math.abs(rand.nextInt()) % entries; 299 if (arr[idx] != null) { 301 result.addElement(arr[idx]); 302 arr[idx] = null; count++; 304 } 305 } 306 307 return result; 308 } 309 310 311 protected Vector orderFirst(Vector sources) 312 { 313 return sources; 314 } 315 316 317 338 } 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | Popular Tags |