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