Member and_not in Z_Operator is kept for backwards compatibility.
[yaz-moved-to-github.git] / zutil / yaz-ccl.c
1 /*
2  * Copyright (c) 1996-2001, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: yaz-ccl.c,v $
7  * Revision 1.12  2001-03-07 13:24:40  adam
8  * Member and_not in Z_Operator is kept for backwards compatibility.
9  * Added support for definition of CCL operators in field spec file.
10  *
11  * Revision 1.11  2001/02/21 13:46:54  adam
12  * C++ fixes.
13  *
14  * Revision 1.10  2001/02/20 11:23:50  adam
15  * Updated ccl_pquery to consider local attribute set too.
16  *
17  * Revision 1.9  2000/11/27 14:16:55  adam
18  * Fixed bug in ccl_rpn_simple regarding resultSetId's.
19  *
20  * Revision 1.8  2000/11/16 13:03:13  adam
21  * Function ccl_rpn_query sets attributeSet to Bib-1.
22  *
23  * Revision 1.7  2000/11/16 09:58:02  adam
24  * Implemented local AttributeSet setting for CCL field maps.
25  *
26  * Revision 1.6  2000/02/02 15:13:23  adam
27  * Minor change.
28  *
29  * Revision 1.5  2000/01/31 13:15:22  adam
30  * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
31  * that some characters are not surrounded by spaces in resulting term.
32  * ILL-code updates.
33  *
34  * Revision 1.4  1999/12/20 15:20:13  adam
35  * Implemented ccl_pquery to convert from CCL tree to prefix query.
36  *
37  * Revision 1.3  1999/11/30 13:47:12  adam
38  * Improved installation. Moved header files to include/yaz.
39  *
40  * Revision 1.2  1999/06/16 12:00:08  adam
41  * Added proximity.
42  *
43  * Revision 1.1  1999/06/08 10:12:43  adam
44  * Moved file to be part of zutil (instead of util).
45  *
46  * Revision 1.13  1998/03/31 15:13:20  adam
47  * Development towards compiled ASN.1.
48  *
49  * Revision 1.12  1998/02/11 11:53:36  adam
50  * Changed code so that it compiles as C++.
51  *
52  * Revision 1.11  1997/11/24 11:33:57  adam
53  * Using function odr_nullval() instead of global ODR_NULLVAL when
54  * appropriate.
55  *
56  * Revision 1.10  1997/09/29 08:58:25  adam
57  * Fixed conversion of trees so that true copy is made.
58  *
59  * Revision 1.9  1997/06/23 10:31:25  adam
60  * Added ODR argument to ccl_rpn_query and ccl_scan_query.
61  *
62  * Revision 1.8  1996/10/29 13:36:27  adam
63  * Added header.
64  *
65  */
66
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70
71 #include <yaz/yaz-ccl.h>
72
73 static Z_RPNStructure *ccl_rpn_structure (ODR o, struct ccl_rpn_node *p);
74
75 static Z_AttributesPlusTerm *ccl_rpn_term (ODR o, struct ccl_rpn_node *p)
76 {
77     struct ccl_rpn_attr *attr;
78     int num = 0;
79     Z_AttributesPlusTerm *zapt;
80     Odr_oct *term_octet;
81     Z_Term *term;
82     Z_AttributeElement **elements;
83
84     zapt = (Z_AttributesPlusTerm *)odr_malloc (o, sizeof(*zapt));
85
86     term_octet = (Odr_oct *)odr_malloc (o, sizeof(*term_octet));
87
88     term = (Z_Term *)odr_malloc (o, sizeof(*term));
89
90     for (attr = p->u.t.attr_list; attr; attr = attr->next)
91         num++;
92     if (!num)
93         elements = (Z_AttributeElement**)odr_nullval();
94     else
95     {
96         int i = 0;
97         elements = (Z_AttributeElement **)
98             odr_malloc (o, num*sizeof(*elements));
99         for (attr = p->u.t.attr_list; attr; attr = attr->next, i++)
100         {
101             elements[i] = (Z_AttributeElement *)
102                 odr_malloc (o, sizeof(**elements));
103             elements[i]->attributeType =
104                 (int *)odr_malloc(o, sizeof(int));
105             *elements[i]->attributeType = attr->type;
106             elements[i]->attributeSet = 0;
107             if (attr->set && *attr->set)
108             {
109                 int value = oid_getvalbyname (attr->set);
110
111                 if (value != VAL_NONE)
112                 {
113                     int oid[OID_SIZE];
114                     struct oident ident;
115
116                     ident.oclass = CLASS_ATTSET;
117                     ident.proto = PROTO_Z3950;
118                     ident.value = (oid_value) value;
119                     elements[i]->attributeSet =
120                         odr_oiddup (o, oid_ent_to_oid (&ident, oid));
121                 }
122             }
123             elements[i]->which = Z_AttributeValue_numeric;
124             elements[i]->value.numeric =
125                 (int *)odr_malloc (o, sizeof(int));
126             *elements[i]->value.numeric = attr->value;
127         }
128     }
129 #ifdef ASN_COMPILED
130     zapt->attributes = (Z_AttributeList *)
131         odr_malloc (o, sizeof(*zapt->attributes));
132     zapt->attributes->num_attributes = num;
133     zapt->attributes->attributes = elements;
134 #else
135     zapt->num_attributes = num;
136     zapt->attributeList = elements;
137 #endif    
138     zapt->term = term;
139     term->which = Z_Term_general;
140     term->u.general = term_octet;
141     term_octet->len = term_octet->size = strlen (p->u.t.term);
142     term_octet->buf = (unsigned char *)odr_malloc (o, term_octet->len+1);
143     strcpy ((char*) term_octet->buf, p->u.t.term);
144     return zapt;
145 }
146
147 static Z_Operand *ccl_rpn_simple (ODR o, struct ccl_rpn_node *p)
148 {
149     Z_Operand *zo;
150
151     zo = (Z_Operand *)odr_malloc (o, sizeof(*zo));
152
153     switch (p->kind)
154     {
155     case CCL_RPN_TERM:
156         zo->which = Z_Operand_APT;
157         zo->u.attributesPlusTerm = ccl_rpn_term (o, p);
158         break;
159     case CCL_RPN_SET:
160         zo->which = Z_Operand_resultSetId;
161         zo->u.resultSetId = odr_strdup (o, p->u.setname);
162         break;
163     default:
164         return 0;
165     }
166     return zo;
167 }
168
169 static Z_Complex *ccl_rpn_complex (ODR o, struct ccl_rpn_node *p)
170 {
171     Z_Complex *zc;
172     Z_Operator *zo;
173
174     zc = (Z_Complex *)odr_malloc (o, sizeof(*zc));
175     zo = (Z_Operator *)odr_malloc (o, sizeof(*zo));
176
177     zc->roperator = zo;
178     switch (p->kind)
179     {
180     case CCL_RPN_AND:
181         zo->which = Z_Operator_and;
182         zo->u.and_not = odr_nullval();
183         break;
184     case CCL_RPN_OR:
185         zo->which = Z_Operator_or;
186         zo->u.and_not = odr_nullval();
187         break;
188     case CCL_RPN_NOT:
189         zo->which = Z_Operator_and_not;
190         zo->u.and_not = odr_nullval();
191         break;
192     case CCL_RPN_PROX:
193         zo->which = Z_Operator_prox;
194         zo->u.prox = (Z_ProximityOperator *)
195             odr_malloc (o, sizeof(*zo->u.prox));
196         zo->u.prox->exclusion = 0;
197
198         zo->u.prox->distance = (int *)
199             odr_malloc (o, sizeof(*zo->u.prox->distance));
200         *zo->u.prox->distance = 2;
201
202         zo->u.prox->ordered = (bool_t *)
203             odr_malloc (o, sizeof(*zo->u.prox->ordered));
204         *zo->u.prox->ordered = 0;
205
206         zo->u.prox->relationType = (int *)
207             odr_malloc (o, sizeof(*zo->u.prox->relationType));
208 #ifdef ASN_COMPILED
209         *zo->u.prox->relationType = Z_ProximityOperator_Prox_lessThan;
210         zo->u.prox->which = Z_ProximityOperator_known;
211         zo->u.prox->u.known = 
212             (Z_ProxUnit *) odr_malloc (o, sizeof(*zo->u.prox->u.known));
213         *zo->u.prox->u.known = Z_ProxUnit_word;
214 #else
215         *zo->u.prox->relationType = Z_Prox_lessThan;
216         zo->u.prox->which = Z_ProxCode_known;
217         zo->u.prox->proximityUnitCode = (int*)
218             odr_malloc (o, sizeof(*zo->u.prox->proximityUnitCode));
219         *zo->u.prox->proximityUnitCode = Z_ProxUnit_word;
220 #endif
221         break;
222     default:
223         return 0;
224     }
225     zc->s1 = ccl_rpn_structure (o, p->u.p[0]);
226     zc->s2 = ccl_rpn_structure (o, p->u.p[1]);
227     return zc;
228 }
229
230 static Z_RPNStructure *ccl_rpn_structure (ODR o, struct ccl_rpn_node *p)
231 {
232     Z_RPNStructure *zs;
233
234     zs = (Z_RPNStructure *)odr_malloc (o, sizeof(*zs));
235     switch (p->kind)
236     {
237     case CCL_RPN_AND:
238     case CCL_RPN_OR:
239     case CCL_RPN_NOT:
240     case CCL_RPN_PROX:
241         zs->which = Z_RPNStructure_complex;
242         zs->u.complex = ccl_rpn_complex (o, p);
243         break;
244     case CCL_RPN_TERM:
245     case CCL_RPN_SET:
246         zs->which = Z_RPNStructure_simple;
247         zs->u.simple = ccl_rpn_simple (o, p);
248         break;
249     default:
250         return 0;
251     }
252     return zs;
253 }
254
255 Z_RPNQuery *ccl_rpn_query (ODR o, struct ccl_rpn_node *p)
256 {
257     Z_RPNQuery *zq;
258     oident bib1;
259     int oid[OID_SIZE];
260     bib1.proto = PROTO_Z3950;
261     bib1.oclass = CLASS_ATTSET;
262     bib1.value = VAL_BIB1;
263
264     zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq));
265     zq->attributeSetId = odr_oiddup (o, oid_ent_to_oid (&bib1, oid));
266     zq->RPNStructure = ccl_rpn_structure (o, p);
267     return zq;
268 }
269
270 Z_AttributesPlusTerm *ccl_scan_query (ODR o, struct ccl_rpn_node *p)
271 {
272     if (p->kind != CCL_RPN_TERM)
273         return NULL;
274     return ccl_rpn_term (o, p);
275 }
276
277 static void ccl_pquery_complex (WRBUF w, struct ccl_rpn_node *p)
278 {
279     switch (p->kind)
280     {
281     case CCL_RPN_AND:
282         wrbuf_puts (w, "@and ");
283         break;
284     case CCL_RPN_OR:
285         wrbuf_puts(w, "@or ");
286         break;
287     case CCL_RPN_NOT:
288         wrbuf_puts(w, "@not ");
289         break;
290     case CCL_RPN_PROX:
291         wrbuf_puts(w, "@prox 0 2 0 1 known 2 ");
292         break;
293     default:
294         wrbuf_puts(w, "@ bad op (unknown) ");
295     };
296     ccl_pquery(w, p->u.p[0]);
297     ccl_pquery(w, p->u.p[1]);
298 }
299         
300 void ccl_pquery (WRBUF w, struct ccl_rpn_node *p)
301 {
302     struct ccl_rpn_attr *att;
303
304     switch (p->kind)
305     {
306     case CCL_RPN_AND:
307     case CCL_RPN_OR:
308     case CCL_RPN_NOT:
309     case CCL_RPN_PROX:
310         ccl_pquery_complex (w, p);
311         break;
312     case CCL_RPN_SET:
313         wrbuf_puts (w, "@set ");
314         wrbuf_puts (w, p->u.setname);
315         wrbuf_puts (w, " ");
316         break;
317     case CCL_RPN_TERM:
318         for (att = p->u.t.attr_list; att; att = att->next)
319         {
320             char tmpattr[128];
321             wrbuf_puts (w, "@attr ");
322             if (att->set)
323             {
324                 wrbuf_puts (w, att->set);
325                 wrbuf_puts (w, " ");
326             }
327             sprintf(tmpattr, "%d=%d ", att->type, att->value);
328             wrbuf_puts (w, tmpattr);
329         }
330         wrbuf_puts (w, "{");
331         wrbuf_puts (w, p->u.t.term);
332         wrbuf_puts (w, "} ");
333         break;
334     }
335 }