KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > constraint > Preference


1
2 // Copyright (C) 1998-1999
3
// Object Oriented Concepts, Inc.
4

5 // **********************************************************************
6
//
7
// Copyright (c) 1997
8
// Mark Spruiell (mark@intellisoft.com)
9
//
10
// See the COPYING file for more information
11
//
12
// **********************************************************************
13

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 JavaDoc 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             // ask the expression object to parse the input from the lexer;
53
// any exceptions from the expression object are propagated up
54
lex.nextToken();
55           m_exprType = m_expr.parse(lex);
56
57             // make sure that the type of the expression is numeric, and is not
58
// a sequence
59
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             // ask the expression object to parse the input from the lexer;
69
// any exceptions from the expression object are propagated up
70
lex.nextToken();
71           m_exprType = m_expr.parse(lex);
72
73             // make sure that the type of the expression is boolean, and is not
74
// a sequence
75
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           // nothing to do - no expression necessary
85
break;
86
87       case Lex.END:
88           // if preference is empty, 'first' is default
89
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       // make a copy of the sources list; when we're done, temp will
146
// hold the source objects for which evaluation failed
147
Vector temp = (Vector)sources.clone();
148
149       // so that we don't keep re-evaluating sources, we keep a list
150
// of source-value mappings
151
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           // evaluation was successful, so find the proper
160
// insertion point in values list
161
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           // insert new SourceValue struct into values list
171
values.insertElementAt(new SourceValue(src, v), pos);
172
173           // remove src from temp list so that we know we've
174
// dealt with it
175
temp.removeElement(src);
176       }
177     }
178
179       // iterate through values list, adding each source to result
180
e = values.elements();
181     while (e.hasMoreElements()) {
182       SourceValue sv = (SourceValue)e.nextElement();
183       result.addElement(sv.source);
184     }
185
186       // now append all remaining sources in temp to result
187
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       // make a copy of the sources list; when we're done, temp will
200
// hold the source objects for which evaluation failed
201
Vector temp = (Vector)sources.clone();
202
203       // so that we don't keep re-evaluating sources, we keep a list
204
// of source-value mappings
205
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           // evaluation was successful, so find the proper
214
// insertion point in values list
215
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           // insert new SourceValue struct into values list
225
values.insertElementAt(new SourceValue(src, v), pos);
226
227           // remove src from temp list so that we know we've
228
// dealt with it
229
temp.removeElement(src);
230       }
231     }
232
233       // iterate through values list, adding each source to result
234
e = values.elements();
235     while (e.hasMoreElements()) {
236       SourceValue sv = (SourceValue)e.nextElement();
237       result.addElement(sv.source);
238     }
239
240       // now append all remaining sources in temp to result
241
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       // make a copy of the sources list; when we're done, temp will
254
// hold the source objects for which evaluation failed
255
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 JavaDoc b = (Boolean JavaDoc)v.getValue();
264         if (b.booleanValue()) {
265             // constraint evaluated to true, so add source to result
266
result.addElement(src);
267
268             // remove src from temp list so that we know we've
269
// dealt with it
270
temp.removeElement(src);
271         }
272       }
273     }
274
275       // now append all remaining sources in temp to result
276
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       // create an array to hold each source in the list
289
int entries = sources.size();
290     PropertySource[] arr = new PropertySource[entries];
291     sources.copyInto((java.lang.Object JavaDoc[])arr);
292
293     Random rand = new Random();
294
295       // keep looping until we've processed all sources
296
int count = 0;
297     while (count < entries) {
298       int idx = Math.abs(rand.nextInt()) % entries;
299         // if we haven't seen this index before
300
if (arr[idx] != null) {
301         result.addElement(arr[idx]);
302         arr[idx] = null; // clear this entry
303
count++;
304       }
305     }
306
307     return result;
308   }
309
310
311   protected Vector orderFirst(Vector sources)
312   {
313     return sources;
314   }
315
316
317   /******************** comment out this line to enable main()
318
319   public static void main(String[] args)
320   {
321     if (args.length < 1) {
322       System.err.println("Usage: Preference expr");
323       System.exit(1);
324     }
325
326     Preference pref = new Preference(null);
327     try {
328       pref.parse(args[0]);
329       //boolean result = constr.evaluate(null);
330       //System.out.println("result = " + result);
331     }
332     catch (ParseException e) {
333       System.err.println("Parse error: " + e.getMessage());
334     }
335   }
336
337   /******************** comment out this line to enable main() */

338 }
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
Popular Tags