Changed the way implementationName - and version is set.
[yaz-moved-to-github.git] / retrieval / d1_doespec.c
1 /*
2  * Copyright (c) 1995-1999, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_doespec.c,v $
7  * Revision 1.13  1999-08-27 09:40:32  adam
8  * Renamed logf function to yaz_log. Removed VC++ project files.
9  *
10  * Revision 1.12  1999/04/23 13:34:33  adam
11  * Fixed bug in match_triple. Thanks to Franck Falcoz <franck@dtv.dk>.
12  *
13  * Revision 1.11  1997/11/06 11:36:44  adam
14  * Implemented variant match on simple elements -data1 tree and Espec-1.
15  *
16  * Revision 1.10  1997/10/02 12:10:24  quinn
17  * Attempt to fix bug in especs
18  *
19  * Revision 1.9  1997/09/17 12:10:35  adam
20  * YAZ version 1.4.
21  *
22  * Revision 1.8  1997/05/14 06:54:02  adam
23  * C++ support.
24  *
25  * Revision 1.7  1997/04/30 08:52:11  quinn
26  * Null
27  *
28  * Revision 1.6  1996/10/11  11:57:22  quinn
29  * Smallish
30  *
31  * Revision 1.5  1996/07/06  19:58:34  quinn
32  * System headerfiles gathered in yconfig
33  *
34  * Revision 1.4  1996/06/07  11:04:32  quinn
35  * Fixed tag->tagset dependency
36  *
37  * Revision 1.3  1995/11/13  09:27:33  quinn
38  * Fiddling with the variant stuff.
39  *
40  * Revision 1.2  1995/11/01  13:54:45  quinn
41  * Minor adjustments
42  *
43  * Revision 1.1  1995/11/01  11:56:07  quinn
44  * Added Retrieval (data management) functions en masse.
45  *
46  *
47  */
48
49
50 #include <assert.h>
51 #include <oid.h>
52 #include <log.h>
53 #include <proto.h>
54 #include <data1.h>
55
56 static int match_children(data1_handle dh, data1_node *n,
57                           Z_Espec1 *e, int i, Z_ETagUnit **t,
58     int num);
59
60 static int match_children_wildpath(data1_handle dh, data1_node *n,
61                                    Z_Espec1 *e, int i,
62                                    Z_ETagUnit **t, int num)
63 {
64     return 0;
65 }
66
67 /*
68  * Locate a specific triple within a variant.
69  * set is the set to look for, universal set is the set that applies to a
70  * triple with an unknown set.
71  */
72 static Z_Triple *find_triple(Z_Variant *var, oid_value universalset,
73     oid_value set, int zclass, int type)
74 {
75     int i;
76     oident *defaultsetent = oid_getentbyoid(var->globalVariantSetId);
77     oid_value defaultset = defaultsetent ? defaultsetent->value :
78         universalset;
79
80     for (i = 0; i < var->num_triples; i++)
81     {
82         oident *cursetent =
83             oid_getentbyoid(var->triples[i]->variantSetId);
84         oid_value curset = cursetent ? cursetent->value : defaultset;
85
86         if (set == curset &&
87             *var->triples[i]->zclass == zclass &&
88             *var->triples[i]->type == type)
89             return var->triples[i];
90     }
91     return 0;
92 }
93
94 static void mark_subtree(data1_node *n, int make_variantlist, int no_data,
95     int get_bytes, Z_Variant *vreq)
96 {
97     data1_node *c;
98
99 #if 1
100     if (n->which == DATA1N_tag)
101 #else
102     if (n->which == DATA1N_tag && (!n->child || n->child->which != DATA1N_tag))
103     /*
104      * This seems to cause multi-level elements to fall out when only a
105      * top-level elementRequest has been given... Problem is, I can't figure
106      * out what it was supposed to ACHIEVE.... delete when code has been
107      * verified.
108      */
109 #endif
110     {
111         n->u.tag.node_selected = 1;
112         n->u.tag.make_variantlist = make_variantlist;
113         n->u.tag.no_data_requested = no_data;
114         n->u.tag.get_bytes = get_bytes;
115     }
116
117     for (c = n->child; c; c = c->next)
118     {
119         if (c->which == DATA1N_tag && (!n->child ||
120             n->child->which != DATA1N_tag))
121         {
122             c->u.tag.node_selected = 1;
123             c->u.tag.make_variantlist = make_variantlist;
124             c->u.tag.no_data_requested = no_data;
125             c->u.tag.get_bytes = get_bytes;
126         }
127         mark_subtree(c, make_variantlist, no_data, get_bytes, vreq);
128     }
129 }
130
131
132 static void match_triple (data1_handle dh, Z_Variant *vreq,
133                           oid_value defsetval,
134                           oid_value var1, data1_node *n)
135 {
136     data1_node **c;
137
138     if (!(n = n->child))
139         return;
140     if (n->which != DATA1N_variant)
141         return;
142     c = &n->child;
143     while (*c)
144     {
145         int remove_flag = 0;
146         Z_Triple *r;
147         
148         assert ((*c)->which == DATA1N_variant);
149         
150         if ((*c)->u.variant.type->zclass->zclass == 4 &&
151             (*c)->u.variant.type->type == 1)
152         {
153             if ((r = find_triple(vreq, defsetval, var1, 4, 1)) &&
154                 (r->which == Z_Triple_internationalString))
155             {
156                 const char *string_value =
157                     r->value.internationalString;
158                 if (strcmp ((*c)->u.variant.value, string_value))
159                     remove_flag = 1;
160             }
161         }
162         if (remove_flag)
163         {
164             data1_free_tree (dh, *c);
165             *c = (*c)->next;
166         }
167         else
168         {
169             match_triple (dh, vreq, defsetval, var1, *c);
170             c = &(*c)->next;
171         }
172     }
173 }
174                                 
175 static int match_children_here (data1_handle dh, data1_node *n,
176                                 Z_Espec1 *e, int i,
177                                 Z_ETagUnit **t, int num)
178 {
179     int counter = 0, hits = 0;
180     data1_node *c;
181     Z_ETagUnit *tp = *t;
182     Z_Occurrences *occur;
183
184     for (c = n->child; c ; c = c->next)
185     {
186         data1_tag *tag = 0;
187         if (c->which != DATA1N_tag)
188             return 0;
189
190         if (tp->which == Z_ETagUnit_specificTag)
191         {
192             Z_SpecificTag *want = tp->u.specificTag;
193             occur = want->occurrences;
194             if (c->u.tag.element)
195                 tag = c->u.tag.element->tag;
196             if (*want->tagType != ((tag && tag->tagset) ? tag->tagset->type :
197                 3))
198                 continue;
199             if (want->tagValue->which == Z_StringOrNumeric_numeric)
200             {
201                 if (!tag || tag->which != DATA1T_numeric)
202                     continue;
203                 if (*want->tagValue->u.numeric != tag->value.numeric)
204                     continue;
205             }
206             else
207             {
208                 assert(want->tagValue->which == Z_StringOrNumeric_string);
209                 if (tag && tag->which != DATA1T_string)
210                     continue;
211                 if (data1_matchstr(want->tagValue->u.string,
212                     tag ? tag->value.string : c->u.tag.tag))
213                     continue;
214             }
215         }
216         else
217             occur = tp->u.wildThing;
218
219         /*
220          * Ok, so we have a matching tag. Are we within occurrences-range?
221          */
222         counter++;
223         if (occur && occur->which == Z_Occurrences_last)
224         {
225             yaz_log(LOG_WARN, "Can't do occurrences=last (yet)");
226             return 0;
227         }
228         if (!occur || occur->which == Z_Occurrences_all ||
229             (occur->which == Z_Occurrences_values && counter >=
230             *occur->u.values->start))
231         {
232             if (match_children(dh, c, e, i, t + 1, num - 1))
233             {
234                 c->u.tag.node_selected = 1;
235                 /*
236                  * Consider the variant specification if this is a complete
237                  * match.
238                  */
239                 if (num == 1)
240                 {
241                     int show_variantlist = 0;
242                     int no_data = 0;
243                     int get_bytes = -1;
244
245                     Z_Variant *vreq =
246                         e->elements[i]->u.simpleElement->variantRequest;
247                     oident *defset = oid_getentbyoid(e->defaultVariantSetId);
248                     oid_value defsetval = defset ? defset->value : VAL_NONE;
249                     oid_value var1 = oid_getvalbyname("Variant-1");
250
251                     if (!vreq)
252                         vreq = e->defaultVariantRequest;
253
254                     if (vreq)
255                     {
256                         Z_Triple *r;
257
258                         /*
259                          * 6,5: meta-data requested, variant list.
260                          */
261                         if (find_triple(vreq, defsetval, var1, 6, 5))
262                             show_variantlist = 1;
263                         /*
264                          * 9,1: Miscellaneous, no data requested.
265                          */
266                         if (find_triple(vreq, defsetval, var1, 9, 1))
267                             no_data = 1;
268
269                         /* howmuch */
270                         if ((r = find_triple(vreq, defsetval, var1, 5, 5)))
271                             if (r->which == Z_Triple_integer)
272                                 get_bytes = *r->value.integer;
273
274                         if (!show_variantlist)
275                             match_triple (dh, vreq, defsetval, var1, c);
276                     }
277                     mark_subtree(c, show_variantlist, no_data, get_bytes, vreq);
278                 }
279                 hits++;
280                 /*
281                  * have we looked at enough children?
282                  */
283                 if (!occur || (occur->which == Z_Occurrences_values &&
284                     (!occur->u.values->howMany ||
285                     counter - *occur->u.values->start >=
286                     *occur->u.values->howMany - 1)))
287                     return hits;
288             }
289         }
290     }
291     return hits;
292 }
293
294 static int match_children(data1_handle dh, data1_node *n, Z_Espec1 *e,
295                           int i, Z_ETagUnit **t, int num)
296 {
297     int res;
298
299     if (!num)
300         return 1;
301     switch (t[0]->which)
302     {
303         case Z_ETagUnit_wildThing:
304         case Z_ETagUnit_specificTag:
305             res = match_children_here(dh, n, e, i, t, num); break;
306         case Z_ETagUnit_wildPath:
307             res = match_children_wildpath(dh, n, e, i, t, num); break;
308         default:
309             abort();
310     }
311     return res;
312 }
313
314 int data1_doespec1 (data1_handle dh, data1_node *n, Z_Espec1 *e)
315 {
316     int i;
317
318     for (i = 0; i < e->num_elements; i++)
319     {
320         if (e->elements[i]->which != Z_ERequest_simpleElement)
321             return 100;
322         match_children(dh, n, e, i,
323                        e->elements[i]->u.simpleElement->path->tags,
324                        e->elements[i]->u.simpleElement->path->num_tags);
325     }
326     return 0;
327 }