]> matita.cs.unibo.it Git - helm.git/blob - helm/metadata/extractor/sthandler_ind.c
ocaml 3.09 transition
[helm.git] / helm / metadata / extractor / sthandler_ind.c
1 /*********************************************************************/
2 /*  Copyright (C) 2000, HELM Team                                    */ 
3 /*                                                                   */
4 /* This file is part of HELM, an Hypertextual, Electronic            */
5 /* Library of Mathematics, developed at the Computer Science         */
6 /* Department, University of Bologna, Italy.                         */
7 /*                                                                   */
8 /* HELM is free software; you can redistribute it and/or             */
9 /* modify it under the terms of the GNU General Public License       */
10 /* as published by the Free Software Foundation; either version 2    */
11 /* of the License, or (at your option) any later version.            */
12 /*                                                                   */
13 /* HELM is distributed in the hope that it will be useful,           */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of    */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     */
16 /* GNU General Public License for more details.                      */
17 /*                                                                   */
18 /* You should have received a copy of the GNU General Public License */
19 /* along with HELM; if not, write to the Free Software               */
20 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston,            */
21 /* MA  02111-1307, USA.                                              */
22 /*                                                                   */
23 /* For details, see the HELM World-Wide-Web page,                    */
24 /* http://cs.unibo.it/helm/.                                         */
25  /*********************************************************************/
26
27 /****************************************************************/
28 /*                        STHANDLER.C                           */
29 /****************************************************************/
30 /* This module supplies routines for symbol table handling.     */
31 /* - init_symbol_table(): it initializes the symbol table       */
32 /*                        to void.                              */
33 /* - search_bucket(): it searches the symbol table for the      */
34 /*                    bucket containing a given identifier, and */
35 /*                    inserts it if it is not present;          */
36 /****************************************************************/
37 /*           First draft 11/12/2001, by Andrea Asperti          */
38 /****************************************************************/
39
40 /****************************************************************/
41 /* 1. Inclusion of header files.                                */
42 /****************************************************************/
43
44 #include                <stdio.h>
45 #include                <malloc.h>
46
47 /****************************************************************/
48 /* 2. Declarations                                              */
49 /****************************************************************/
50
51
52 #define         DICTSIZE                        211
53 #define         HASH1                           4
54 #define         HASH2                           0xf0000000
55 #define         HASH3                           24
56 #define         EOS                             '\0'
57
58 #define                 INBODY    0
59 #define                 MAINHYP   1
60 #define                 INHYP     2
61 #define                 INCONCL   3
62 #define                 MAINCONCL 4
63 #define                 INTYPE    5
64 #define                 NOTFOUND  6
65
66 /****************************************************************/
67 /* 3. Types.                                                    */
68 /****************************************************************/
69
70 struct int_list {
71                 int                     val;
72                 struct int_list         *next;
73                   }; 
74
75 struct st_bucket {
76                 char                    *id;
77                                                 /* identifier */
78                 int                     main_depth;
79                 struct int_list         *depths;
80                 struct st_bucket        *next_st_bucket;
81                                                 /* next bucket in the list */
82                 struct st_bucket        *all_next;
83                                                /* all buckets in symbol
84                                                   table are linked together */
85                 int                     pos[5];
86
87                   };                              
88
89 struct st_bucket    *dictionary[DICTSIZE];
90                                /* pointers to bucket lists */
91
92 /****************************************************************/
93 /* 4. Local functions.                                          */
94 /****************************************************************/
95 struct int_list  *add(struct int_list  *,int);
96 int hash_pjw(char *id);
97
98 /****************************************************************/
99 /* 5. Definitions of functions to be exported.                  */
100 /****************************************************************/
101
102 struct st_bucket        *all;
103
104  /* The following function initializes the symbol table to NULL */
105 void init_symbol_table()
106 {
107         struct st_bucket        *st;
108         int                     i;
109
110         /* initialize the dictionary */
111         for (i = 0; i < DICTSIZE; i++)
112                 dictionary[i] = NULL;
113         all = NULL;
114 }
115
116  /* The following function searches the symbol table for an identifier */
117  /* and inserts it if it is not present. 
118  /* The bucket associated with the given identifier */
119  /* becomes the first one in its list. */
120
121 search_bucket(id, where, depth)
122         char            *id;
123                                         /* identifier */
124         int             where;
125         int             depth;
126 {
127         int             dict_index;
128                                         /* value returned by the */
129                                         /* hash function */
130         struct st_bucket
131                         *prev,
132                         *curr;
133
134         struct st_bucket *st;
135
136         /* apply the hash function */
137         dict_index = hash_pjw(id);
138         /* fprintf(stderr,"%d\n", dict_index); fflush(stdout); */
139         
140         /* scan the bucket list indicated by the hash function */
141         prev = curr = dictionary[dict_index];
142         while ((curr != NULL) && (strcmp(id, curr->id)))
143           {
144             prev = curr;
145             curr = curr->next_st_bucket;
146           }
147         if (curr == NULL)
148           /* the identifier is not in the list */
149           {
150             allocate_bucket(&st,id,where);
151             if (where == MAINCONCL)
152               st->main_depth = depth;
153             else if (where == MAINHYP)
154               st->depths = add(st->depths,depth);
155             move_bucket(st,dict_index);
156             return NOTFOUND;
157           }
158         else
159           /*
160              fprintf(stderr,"uno=%s\n", id);
161              fprintf(stderr,"st=%s\n", curr->id); fflush(stdout) */
162
163           /* the identifier is already in the list */
164           {
165             /* st = curr; */
166             curr->pos[where] = 1;
167             if (where >= 1) 
168               curr->pos[INBODY] = 0; /* it will never be set again to 1 */
169             if (where == MAINHYP)
170               curr->depths=add(curr->depths,depth); 
171             else if (where == MAINCONCL)
172               curr->main_depth = depth; 
173             if (prev != curr)
174               /* the identifier is not in the first position */
175               {
176                 prev->next_st_bucket = curr->next_st_bucket;
177                 move_bucket(curr,
178                             dict_index);
179               };
180             return where;
181           }
182 }
183
184 print_all(about,out,outrel,outsort)
185      char       *about;
186      FILE       *out,
187                 *outrel,
188                 *outsort;
189 {
190
191         int i;
192         struct st_bucket *curr;
193         curr = all;
194         while (curr != NULL)
195           {
196             for (i = 0; i < 5; ++i)
197               if ((curr->pos[i]) == 1)
198                 {
199                   if (i == MAINHYP)
200                     print_mainhyp(about,out,outrel,outsort,curr->id,curr->depths);
201                   else if (i == MAINCONCL)
202                     print_mainconcl(about,out,outrel,outsort,curr->id,curr->main_depth);
203                   else
204                     print_one(out,curr->id,i);
205                 }
206             curr = curr->all_next;
207           }
208 }
209
210
211 /****************************************************************/
212 /* 5. Definitions of functions local to the module.             */
213 /****************************************************************/
214
215 struct int_list  *add(l,m)
216      struct int_list    *l;
217      int                m;
218 {
219         struct int_list *curr;
220         /* scan the list looking for m */
221         curr = l;
222         while ((curr != NULL) && (m != (curr->val)))
223             curr = curr->next;
224         if (curr == NULL)
225           /* m is not in the list */
226           {
227             curr = (struct int_list *)malloc(sizeof(struct int_list));
228             curr->val = m;
229             curr->next = l;
230             return curr;
231           }
232         else
233         return l;
234        
235 }
236
237 print_mainhyp(about,out,outrel,outsort,uri,l)
238      char                *about;
239      FILE                *out,
240                          *outrel,
241                          *outsort;
242      char                *uri;
243      struct int_list     *l;
244 {
245     struct int_list *curr;
246     curr = l;
247     if (!strcmp(uri,"Rel"))
248       {
249         /* scan the list */
250         while (curr != NULL)
251           {
252             fprintf(outrel,"\t<h:Object rdf:about=\"");
253             fprintf(outrel,"%s",about);
254             fprintf(outrel,"\">\n");
255             fprintf(outrel,"\t\t<h:refRel rdf:parseType=\"Resource\">");
256             fprintf(outrel,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainHypothesis\"/>");
257             fprintf(outrel,"\n\t\t\t\t<h:depth>%d</h:depth>",curr->val);
258             fprintf(outrel,"\n\t\t</h:refRel>\n");
259             fprintf(outrel,"\t</h:Object>\n");
260             curr = curr->next;
261           }
262       }
263    else if ((!strcmp(uri,"Prop")) || (!strcmp(uri,"Type")) ||
264             (!strcmp(uri,"Set")))
265       {
266         /* scan the list */
267         while (curr != NULL)
268           {
269             fprintf(outsort,"\t<h:Object rdf:about=\"");
270             fprintf(outsort,"%s",about);
271             fprintf(outsort,"\">\n");
272             fprintf(outsort,"\t\t<h:refSort rdf:parseType=\"Resource\">");
273             fprintf(outsort,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainHypothesis\"/>");
274             fprintf(outsort,"\n\t\t\t\t<h:sort rdf:resource=\"&hns;%s\"/>",uri);
275             fprintf(outsort,"\n\t\t\t\t<h:depth>%d</h:depth>",curr->val);
276             fprintf(outsort,"\n\t\t</h:refSort>\n");
277             fprintf(outsort,"\t</h:Object>\n");
278             curr = curr->next;
279           }
280       }
281     else 
282      {
283         /* scan the list */
284         while (curr != NULL)
285           {
286             fprintf(out,"\t\t<h:refObj rdf:parseType=\"Resource\">");
287             fprintf(out,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainHypothesis\"/>");
288             fprintf(out,"\n\t\t\t\t<h:depth>%d</h:depth>",curr->val);
289             fprintf(out,"\n\t\t\t\t<h:occurrence><h:Object rdf:about=\"%s\"/></h:occurrence>",uri);
290             fprintf(out,"\n\t\t</h:refObj>\n");
291             curr = curr->next;
292           }
293       }
294 }
295
296 print_mainconcl(about,out,outrel,outsort,uri,depth)
297      char    *about;
298      FILE    *out,
299              *outrel,
300              *outsort;
301      char    *uri;
302      int     depth;
303      
304 {
305     if (!strcmp(uri,"Rel"))
306       { 
307         fprintf(outrel,"\t<h:Object rdf:about=\"");
308         fprintf(outrel,"%s",about);
309         fprintf(outrel,"\">\n");
310         fprintf(outrel,"\t\t<h:refRel rdf:parseType=\"Resource\">");
311         fprintf(outrel,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainConclusion\"/>");
312         fprintf(outrel,"\n\t\t\t\t<h:depth>%d</h:depth>",depth);
313         fprintf(outrel,"\n\t\t</h:refRel>\n");
314         fprintf(outrel,"\t</h:Object>\n");
315       }
316     else if ((!strcmp(uri,"Prop")) || (!strcmp(uri,"Type")) ||
317             (!strcmp(uri,"Set")))
318       {
319         fprintf(outsort,"\t<h:Object rdf:about=\"");
320         fprintf(outsort,"%s",about);
321         fprintf(outsort,"\">\n");
322         fprintf(outsort,"\t\t<h:refSort rdf:parseType=\"Resource\">");
323         fprintf(outsort,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainConclusion\"/>");
324         fprintf(outsort,"\n\t\t\t\t<h:sort rdf:resource=\"&hns;%s\"/>",uri);
325         fprintf(outsort,"\n\t\t\t\t<h:depth>%d</h:depth>",depth);
326         fprintf(outsort,"\n\t\t</h:refSort>\n");
327         fprintf(outsort,"\t</h:Object>\n");
328       }
329     else
330       {
331         fprintf(out,"\t\t<h:refObj rdf:parseType=\"Resource\">");
332         fprintf(out,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainConclusion\"/>");
333         fprintf(out,"\n\t\t\t\t<h:depth>%d</h:depth>",depth);
334         fprintf(out,"\n\t\t\t\t<h:occurrence><h:Object rdf:about=\"%s\"/></h:occurrence>",uri);
335         fprintf(out,"\n\t\t</h:refObj>\n");
336       }
337 }
338
339 // dome: cambiata per usare il modello con position
340 print_one(out,uri,pos)
341      FILE    *out;
342      char    *uri;
343      int     pos;
344 {
345     fprintf(out,"\t\t<h:refObj df:parseType=\"Resource\">");
346     fprintf(out,"\n\t\t\t\t<h:position rdf:resource=\"&hns;");
347     if (pos == INBODY)
348        fprintf(out,"InBody");
349     else if (pos == MAINHYP)
350        fprintf(out,"MainHypothesis");
351     else if (pos == INHYP)
352        fprintf(out,"InHypothesis");
353     else if (pos == INCONCL)
354        fprintf(out,"InConclusion");
355     else if (pos == MAINCONCL)
356        fprintf(out,"MainConclusion");
357     fprintf(out,"\"/>\n\t\t\t\t<h:occurrence><h:Object rdf:about=\"%s\"/></h:occurrence>\n\t\t</h:refObj>\n", uri);
358     
359 }
360
361  /* The following function allocates a bucket for an identifier. */
362 allocate_bucket(st, id, where)
363         struct st_bucket
364                         **st;
365                                         /* pointer to the bucket to be */
366                                         /* allocated */
367         char            *id;
368                                         /* identifier */
369         int             where;
370 {
371         int i;
372
373         *st = (struct st_bucket *)malloc(sizeof(struct st_bucket));
374         (*st)->id = (char *)malloc(sizeof('a')*(strlen(id) + 1));
375         strcpy((*st)->id,id);
376         (*st)->main_depth = 0;
377         (*st)->depths = NULL;
378         (*st)->next_st_bucket = NULL;
379         (*st)->all_next = all;
380         all = *st;
381         for (i = 0; i < 5; ++i)
382           (*st)->pos[i] = 0;
383         (*st)->pos[where] = 1;
384 }
385
386  /* The following function moves a bucket to the head of the */
387  /* list in which it lies. */
388 move_bucket(st, dict_index)
389         struct st_bucket 
390                         *st;
391                                         /* pointer to the bucket to */
392                                         /* be moved */
393         int             dict_index;
394                                         /* index corresponding to */
395                                         /* the list in which the */
396                                         /* bucket lies */
397 {
398         st->next_st_bucket = dictionary[dict_index];
399         dictionary[dict_index] = st;
400 }
401
402  /* The following function implements Weinberger's hash function. */
403 int
404 hash_pjw(id)
405         char            *id;
406                                         /* identifier to be hashed */
407 {
408         unsigned        h,
409                         g;
410
411         for (h = 0; *id != EOS; id++)
412         {
413                 h = (h << HASH1) + (*id);
414                 if (g = h & HASH2)
415                         h = h ^ (g >> HASH3) ^ g;
416         }
417         return(h % DICTSIZE);
418 }
419
420
421
422
423
424