01: package org.ofbiz.rules.parse.tokens;
02:
03: import java.util.*;
04: import org.ofbiz.rules.parse.*;
05:
06: /**
07: * <p><b>Title:</b> Word
08: * <p><b>Description:</b> None
09: * <p>Copyright (c) 1999 Steven J. Metsker.
10: * <p>Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
11: *
12: * <p>Permission is hereby granted, free of charge, to any person obtaining a
13: * copy of this software and associated documentation files (the "Software"),
14: * to deal in the Software without restriction, including without limitation
15: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16: * and/or sell copies of the Software, and to permit persons to whom the
17: * Software is furnished to do so, subject to the following conditions:
18: *
19: * <p>The above copyright notice and this permission notice shall be included
20: * in all copies or substantial portions of the Software.
21: *
22: * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
26: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
27: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
28: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29: *
30: * <br>
31: * A Word matches a word from a token assembly.
32: *
33: * @author Steven J. Metsker
34: * @version 1.0
35: */
36: public class Word extends Terminal {
37:
38: /**
39: * Returns true if an assembly's next element is a word.
40: *
41: * @param object an element from an assembly
42: *
43: * @return true, if an assembly's next element is a word
44: */
45: protected boolean qualifies(Object o) {
46: Token t = (Token) o;
47:
48: return t.isWord();
49: }
50:
51: /**
52: * Create a set with one random word (with 3 to 7
53: * characters).
54: */
55: public List randomExpansion(int maxDepth, int depth) {
56: int n = (int) (5.0 * Math.random()) + 3;
57:
58: char[] letters = new char[n];
59:
60: for (int i = 0; i < n; i++) {
61: int c = (int) (26.0 * Math.random()) + 'a';
62:
63: letters[i] = (char) c;
64: }
65:
66: List v = new ArrayList();
67:
68: v.add(new String(letters));
69: return v;
70: }
71:
72: /**
73: * Returns a textual description of this parser.
74: *
75: * @param vector a list of parsers already printed in
76: * this description
77: *
78: * @return string a textual description of this parser
79: *
80: * @see Parser#toString()
81: */
82: public String unvisitedString(List visited) {
83: return "Word";
84: }
85: }
|