23f3fbcc107809e98d4170e8dcdfbcd65016a8dc
[cql-java-moved-to-github.git] / src / org / z3950 / zing / cql / CQLProxNode.java
1 // $Id: CQLProxNode.java,v 1.5 2002-11-20 17:26:42 mike Exp $
2
3 package org.z3950.zing.cql;
4 import java.util.Vector;
5
6
7 /**
8  * Represents a proximity node in a CQL parse-tree.
9  * The left- and right-hand-sides must be satisfied by parts of the
10  * candidate records which are sufficiently close to each other, as
11  * specified by a set of proximity parameters.
12  *
13  * @version     $Id: CQLProxNode.java,v 1.5 2002-11-20 17:26:42 mike Exp $
14  */
15 public class CQLProxNode extends CQLBooleanNode {
16     ModifierSet ms;
17
18     /**
19      * Creates a new, <I>incomplete</I>, proximity node with the
20      * specified left-hand side.  No right-hand side is specified at
21      * this stage: that must be specified later, using the
22      * <TT>addSecondSubterm()</TT> method.  (That may seem odd, but
23      * it's just easier to write the parser that way.)
24      * <P>
25      * Proximity paramaters may be added at any time, before or after
26      * the right-hand-side sub-tree is added.
27      */
28     public CQLProxNode(CQLNode left) {
29         ms = new ModifierSet("prox");
30         this.left = left;
31         // this.right left unresolved for now ...
32     }
33
34     /**
35      * Sets the right-hand side of the proximity node whose
36      * left-hand-side was specified at creation time.
37      */
38     public void addSecondSubterm(CQLNode right) {
39         this.right = right;
40     }
41
42     /**
43      * Adds a modifier of the specified <TT>type</TT> and
44      * <TT>value</TT> to a proximity node.  Valid types are
45      * <TT>relation</TT>, <TT>distance</TT>, <TT>unit</TT> and
46      * <TT>ordering</TT>.
47      * <P>
48      * For information on the semantics of these paramaters, see
49      * <A href="http://zing.z3950.org/cql/intro.html#3.1"
50      *  >section 3.1 (Proximity)</A> of
51      * <I>A Gentle Introduction to CQL</I></A>.
52      */
53     public void addModifier(String type, String value) {
54         ms.addModifier(type, value);
55     }
56
57     /**
58      * Returns an array of the modifiers associated with a proximity
59      * node.
60      * @return
61      *  An array of modifiers, each represented by a two-element
62      *  <TT>Vector</TT>, in which element 0 is the modifier type
63      *  (e.g. <TT>distance</TT> or <TT>ordering</TT>) and element 1 is
64      *  the associated value (e.g. <TT>3</TT> or <TT>unordered</TT>).
65      */
66     public Vector[] getModifiers() {
67         return ms.getModifiers();
68     }
69
70     String op() {
71         return ms.toCQL();
72     }
73
74     String opXCQL(int level) {
75         System.err.println("in CQLProxNode.opXCQL()");
76         return ms.toXCQL(level, "boolean");
77     }
78
79     /*
80      * proximity ::= exclusion distance ordered relation which-code unit-code.
81      * exclusion ::= '1' | '0' | 'void'.
82      * distance ::= integer.
83      * ordered ::= '1' | '0'.
84      * relation ::= integer.
85      * which-code ::= 'known' | 'private' | integer.
86      * unit-code ::= integer.
87      */
88     String opPQF() {
89         String rel = ms.modifier("relation");
90         int relCode = 0;
91         if (rel.equals("<")) {
92             relCode = 1;
93         } else if (rel.equals("<=")) {
94             relCode = 2;
95         } else if (rel.equals("=")) {
96             relCode = 3;
97         } else if (rel.equals(">=")) {
98             relCode = 4;
99         } else if (rel.equals(">")) {
100             relCode = 5;
101         } else if (rel.equals("<>")) {
102             relCode = 6;
103         }
104
105         String unit = ms.modifier("unit");
106         int unitCode = 0;
107         if (unit.equals("word")) {
108             unitCode = 2;
109         } else if (unit.equals("sentence")) {
110             unitCode = 3;
111         } else if (unit.equals("paragraph")) {
112             unitCode = 4;
113         } else if (unit.equals("element")) {
114             unitCode = 8;
115         }
116
117         String res = "prox " +
118             "0 " +
119             ms.modifier("distance") + " " +
120             (ms.modifier("ordering").equals("ordered") ? 1 : 0) + " " +
121             relCode + " " +
122             "1 " +
123             unitCode;
124         
125         return res;
126     }
127 }