Minor changes.
[yaz-moved-to-github.git] / zutil / logrpn.c
1 /*
2  * Copyright (C) 1995-1998, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: logrpn.c,v $
7  * Revision 1.1  1999-06-08 10:10:16  adam
8  * New sub directory zutil. Moved YAZ Compiler to be part of YAZ tree.
9  *
10  * Revision 1.1  1998/11/16 16:02:35  adam
11  * Added loggin utilies, log_rpn_query and log_scan_term. These used
12  * to be part of Zebra.
13  *
14  *
15  *
16  * Old logs from zlogs.c:
17  *
18  * Revision 1.9  1998/10/13 20:36:02  adam
19  * Changed "indent" format string in log messages.
20  *
21  * Revision 1.8  1998/04/02 14:35:29  adam
22  * First version of Zebra that works with compiled ASN.1.
23  *
24  * Revision 1.7  1998/01/29 13:40:11  adam
25  * Better logging for scan service.
26  *
27  * Revision 1.6  1997/09/29 09:06:41  adam
28  * Removed static var to make this module thread safe.
29  *
30  * Revision 1.5  1997/04/30 08:56:07  quinn
31  * null
32  *
33  * Revision 1.4  1996/10/08  09:41:25  quinn
34  * Fixed diagnostic.
35  *
36  * Revision 1.3  1996/03/20  09:36:40  adam
37  * Function dict_lookup_grep got extra parameter, init_pos, which marks
38  * from which position in pattern approximate pattern matching should occur.
39  * Approximate pattern matching is used in relevance=re-2.
40  *
41  * Revision 1.2  1996/01/03  16:22:11  quinn
42  * operator->roperator
43  *
44  * Revision 1.1  1995/11/16  17:00:55  adam
45  * Better logging of rpn query.
46  *
47  */
48 #include <stdio.h>
49 #include <assert.h>
50
51 #include <log.h>
52 #include <logrpn.h>
53
54 static void attrStr (int type, int value, enum oid_value ast, char *str)
55 {
56     *str = '\0';
57     switch (ast)
58     {
59     case VAL_BIB1:
60     case VAL_EXP1:
61     case VAL_GILS:
62         switch (type)
63         {
64         case 1:
65             sprintf (str, "use");
66             break;
67         case 2:
68             switch (value)
69             {
70             case 1:
71                 sprintf (str, "relation=Less than");
72                 break;
73             case 2:
74                 sprintf (str, "relation=Less than or equal");
75                 break;
76             case 3:
77                 sprintf (str, "relation=Equal");
78                 break;
79             case 4:
80                 sprintf (str, "relation=Greater or equal");
81                 break;
82             case 5:
83                 sprintf (str, "relation=Greater than");
84                 break;
85             case 6:
86                 sprintf (str, "relation=Not equal");
87                 break;
88             case 100:
89                 sprintf (str, "relation=Phonetic");
90                 break;
91             case 101:
92                 sprintf (str, "relation=Stem");
93                 break;
94             case 102:
95                 sprintf (str, "relation=Relevance");
96                 break;
97             case 103:
98                 sprintf (str, "relation=AlwaysMatches");
99                 break;
100             default:
101                 sprintf (str, "relation");
102             }
103             break;
104         case 3:
105             switch (value)
106             {
107             case 1:
108                 sprintf (str, "position=First in field");
109                 break;
110             case 2:
111                 sprintf (str, "position=First in any subfield");
112                 break;
113             case 3:
114                 sprintf (str, "position=Any position in field");
115                 break;
116             default:
117                 sprintf (str, "position");
118             }
119             break;
120         case 4:
121             switch (value)
122             {
123             case 1:
124                 sprintf (str, "structure=Phrase");
125                 break;
126             case 2:
127                 sprintf (str, "structure=Word");
128                 break;
129             case 3:
130                 sprintf (str, "structure=Key");
131                 break;
132             case 4:
133                 sprintf (str, "structure=Year");
134                 break;
135             case 5:
136                 sprintf (str, "structure=Date");
137                 break;
138             case 6:
139                 sprintf (str, "structure=Word list");
140                 break;
141             case 100:
142                 sprintf (str, "structure=Date (un)");
143                 break;
144             case 101:
145                 sprintf (str, "structure=Name (norm)");
146                 break;
147             case 102:
148                 sprintf (str, "structure=Name (un)");
149                 break;
150             case 103:
151                 sprintf (str, "structure=Structure");
152                 break;
153             case 104:
154                 sprintf (str, "structure=urx");
155                 break;
156             case 105:
157                 sprintf (str, "structure=free-form-text");
158                 break;
159             case 106:
160                 sprintf (str, "structure=document-text");
161                 break;
162             case 107:
163                 sprintf (str, "structure=local-number");
164                 break;
165             case 108:
166                 sprintf (str, "structure=string");
167                 break;
168             case 109:
169                 sprintf (str, "structure=numeric string");
170                 break;
171             default:
172                 sprintf (str, "structure");
173             }
174             break;
175         case 5:
176             switch (value)
177             {
178             case 1:
179                 sprintf (str, "truncation=Right");
180                 break;
181             case 2:
182                 sprintf (str, "truncation=Left");
183                 break;
184             case 3:
185                 sprintf (str, "truncation=Left&right");
186                 break;
187             case 100:
188                 sprintf (str, "truncation=Do not truncate");
189                 break;
190             case 101:
191                 sprintf (str, "truncation=Process #");
192                 break;
193             case 102:
194                 sprintf (str, "truncation=re-1");
195                 break;
196             case 103:
197                 sprintf (str, "truncation=re-2");
198                 break;
199             default:
200                 sprintf (str, "truncation");
201             }
202             break;
203         case 6:
204             switch (value)
205             {
206             case 1:
207                 sprintf (str, "completeness=Incomplete subfield");
208                 break;
209             case 2:
210                 sprintf (str, "completeness=Complete subfield");
211                 break;
212             case 3:
213                 sprintf (str, "completeness=Complete field");
214                 break;
215             default:
216                 sprintf (str, "completeness");
217             }
218             break;
219         }
220         break;
221     default:
222         break;
223     }
224     if (*str)
225         sprintf (str + strlen(str), " (%d=%d)", type, value);
226     else
227         sprintf (str, "%d=%d", type, value);
228 }
229
230 /*
231  * zlog_attributes: print attributes of term
232  */
233 static void zlog_attributes (Z_AttributesPlusTerm *t, int level,
234                              enum oid_value ast)
235 {
236     int of, i;
237     char str[80];
238 #ifdef ASN_COMPILED
239     int num_attributes = t->attributes->num_attributes;
240 #else
241     int num_attributes = t->num_attributes;
242 #endif
243     
244     for (of = 0; of < num_attributes; of++)
245     {
246         const char *attset_name = "";
247         Z_AttributeElement *element;
248 #ifdef ASN_COMPILED
249         element = t->attributes->attributes[of];
250 #else
251         element = t->attributeList[of];
252 #endif
253         if (element->attributeSet)
254         {
255             oident *attrset;
256             attrset = oid_getentbyoid (element->attributeSet);
257             attset_name = attrset->desc;
258         }
259         switch (element->which) 
260         {
261         case Z_AttributeValue_numeric:
262             attrStr (*element->attributeType,
263                      *element->value.numeric, ast, str);
264             logf (LOG_LOG, "%*.0s%s %s", level, "", attset_name, str);
265             break;
266         case Z_AttributeValue_complex:
267             logf (LOG_LOG, "%*.0s%s attributeType=%d complex",
268                   level, "", attset_name, *element->attributeType);
269             for (i = 0; i<element->value.complex->num_list; i++)
270             {
271                 if (element->value.complex->list[i]->which ==
272                     Z_StringOrNumeric_string)
273                     logf (LOG_LOG, "%*.0s  string: '%s'", level, "",
274                           element->value.complex->list[i]->u.string);
275                 else if (element->value.complex->list[i]->which ==
276                          Z_StringOrNumeric_numeric)
277                     logf (LOG_LOG, "%*.0s  numeric: '%d'", level, "",
278                           *element->value.complex->list[i]->u.numeric);
279             }
280             break;
281         default:
282             logf (LOG_LOG, "%.*s%s attribute unknown",
283                   level, "", attset_name);
284         }
285     }
286 }
287
288 static void zlog_structure (Z_RPNStructure *zs, int level, enum oid_value ast)
289 {
290     if (zs->which == Z_RPNStructure_complex)
291     {
292         switch (zs->u.complex->roperator->which)
293         {
294         case Z_Operator_and:
295             logf (LOG_LOG, "%*.0s and", level, "");
296             break;
297         case Z_Operator_or:
298             logf (LOG_LOG, "%*.0s or", level, "");
299             break;
300         case Z_Operator_and_not:
301             logf (LOG_LOG, "%*.0s and-not", level, "");
302             break;
303         default:
304             logf (LOG_LOG, "%*.0s unknown complex", level, "");
305             return;
306         }
307         zlog_structure (zs->u.complex->s1, level+2, ast);
308         zlog_structure (zs->u.complex->s2, level+2, ast);
309     }
310     else if (zs->which == Z_RPNStructure_simple)
311     {
312         if (zs->u.simple->which == Z_Operand_APT)
313         {
314             Z_AttributesPlusTerm *zapt = zs->u.simple->u.attributesPlusTerm;
315
316             if (zapt->term->which == Z_Term_general) 
317             {
318                 logf (LOG_LOG, "%*.0s term '%.*s' (general)", level, "",
319                       zapt->term->u.general->len, zapt->term->u.general->buf);
320             }
321             else
322             {
323                 logf (LOG_LOG, "%*.0s term (not general)", level, "");
324             }
325             zlog_attributes (zapt, level+2, ast);
326         }
327         else if (zs->u.simple->which == Z_Operand_resultSetId)
328         {
329             logf (LOG_LOG, "%*.0s set '%s'", level, "",
330                   zs->u.simple->u.resultSetId);
331         }
332         else
333             logf (LOG_LOG, "%*.0s unknown simple structure", level, "");
334     }
335     else
336         logf (LOG_LOG, "%*.0s unknown structure", level, "");
337 }
338
339 void log_rpn_query (Z_RPNQuery *rpn)
340 {
341     oident *attrset;
342     enum oid_value ast;
343     
344     attrset = oid_getentbyoid (rpn->attributeSetId);
345     if (attrset)
346     {
347         ast = attrset->value;
348         logf (LOG_LOG, "RPN query. Type: %s", attrset->desc);
349     } 
350     else
351     {
352         ast = VAL_NONE;
353         logf (LOG_LOG, "RPN query. Unknown type");
354     }
355     zlog_structure (rpn->RPNStructure, 0, ast);
356 }
357
358 void log_scan_term (Z_AttributesPlusTerm *zapt, oid_value ast)
359 {
360     int level = 0;
361     if (zapt->term->which == Z_Term_general) 
362     {
363         logf (LOG_LOG, "%*.0s term '%.*s' (general)", level, "",
364               zapt->term->u.general->len, zapt->term->u.general->buf);
365     }
366     else
367         logf (LOG_LOG, "%*.0s term (not general)", level, "");
368     zlog_attributes (zapt, level+2, ast);
369 }