]> matita.cs.unibo.it Git - helm.git/blob - helm/metadata/create_V7_mowgli/METADATA/sthandler.c
removed dependency on netclient, use http_client module from ocaml-http
[helm.git] / helm / metadata / create_V7_mowgli / METADATA / sthandler.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                <postgresql/libpq-fe.h>
46 #include                <malloc.h>
47
48 /****************************************************************/
49 /* 2. Declarations                                              */
50 /****************************************************************/
51
52
53 #define         DICTSIZE                        211
54 #define         HASH1                           4
55 #define         HASH2                           0xf0000000
56 #define         HASH3                           24
57 #define         EOS                             '\0'
58
59 #define                 INBODY    0
60 #define                 MAINHYP   1
61 #define                 INHYP     2
62 #define                 INCONCL   3
63 #define                 MAINCONCL 4
64 #define                 INTYPE    5
65 #define                 NOTFOUND  6
66
67 /****************************************************************/
68 /* 3. Types.                                                    */
69 /****************************************************************/
70
71 struct int_list {
72                 int                     val;
73                 struct int_list         *next;
74                   }; 
75
76 struct st_bucket {
77                 char                    *id;
78                                                 /* identifier */
79                 int                     main_depth;
80                 struct int_list         *depths;
81                 struct st_bucket        *next_st_bucket;
82                                                 /* next bucket in the list */
83                 struct st_bucket        *all_next;
84                                                /* all buckets in symbol
85                                                   table are linked together */
86                 int                     pos[5];
87
88                   };                              
89
90 struct st_bucket    *dictionary[DICTSIZE];
91                                /* pointers to bucket lists */
92
93 /****************************************************************/
94 /* 4. Local functions.                                          */
95 /****************************************************************/
96 struct int_list  *add(struct int_list  *,int);
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); */
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              printf("uno=%s\n", id);
161              printf("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,dict_index);
178               };
179             return where;
180           }
181 }
182
183 print_all(about,conn)
184      char       *about;
185      PGconn     *conn;
186 {
187
188         int i;
189         struct st_bucket *curr;
190         curr = all;
191         while (curr != NULL)
192           {
193             for (i = 0; i < 5; ++i)
194               if ((curr->pos[i]) == 1)
195                 {
196                   if (i == MAINHYP)
197                     print_mainhyp(about,conn,curr->id,curr->depths);
198                   else if (i == MAINCONCL)
199                     print_mainconcl(about,conn,curr->id,curr->main_depth);
200                   else
201                     print_one(conn,about,curr->id,i);
202                 }
203             curr = curr->all_next;
204           }
205 }
206
207
208 /****************************************************************/
209 /* 5. Definitions of functions local to the module.             */
210 /****************************************************************/
211
212 void exit_nicely(PGconn *conn)
213 {
214     PQfinish(conn);
215     exit(1);
216 }
217
218 struct int_list  *add(l,m)
219      struct int_list    *l;
220      int                m;
221 {
222         struct int_list *curr;
223         /* scan the list looking for m */
224         curr = l;
225         while ((curr != NULL) && (m != (curr->val)))
226             curr = curr->next;
227         if (curr == NULL)
228           /* m is not in the list */
229           {
230             curr = (struct int_list *)malloc(sizeof(struct int_list));
231             curr->val = m;
232             curr->next = l;
233             return curr;
234           }
235         else
236         return l;
237        
238 }
239
240 print_mainhyp(about,conn,uri,l)
241      char                *about;
242      PGconn              *conn;
243      char                *uri;
244      struct int_list     *l;
245 {
246     PGresult   *res;
247     char       *command = (char *)malloc((sizeof('a')*200));
248     struct int_list *curr;
249     curr = l;
250     if (!strcmp(uri,"Rel"))
251       {
252         /* scan the list */
253         while (curr != NULL)
254           {
255             size_t len = strlen(about) + 1;
256             char *qabout = malloc (sizeof(char) * len * 2);
257             PQescapeString(qabout,about,len);
258             sprintf(command,"INSERT INTO refRel values ('%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainHypothesis', %d)",qabout,curr->val);
259             /* fprintf(stderr, "%s\n", command);  */
260             res = PQexec(conn, command);
261             if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
262             {
263               fprintf(stderr, "BEGIN command failed\n");
264               PQclear(res);
265               exit_nicely(conn);
266             }
267             curr = curr->next;
268           }
269       }
270    else if ((!strcmp(uri,"Prop")) || (!strcmp(uri,"Type")) ||
271             (!strcmp(uri,"Set")))
272       {
273         /* scan the list */
274         while (curr != NULL)
275           {
276             size_t len = strlen(about) + 1;
277             char *qabout = malloc (sizeof(char) * len * 2);
278             PQescapeString(qabout,about,len);
279              sprintf(command,"INSERT INTO refSort values ('%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainHypothesis', %d, '%s')",qabout,curr->val,uri);
280              /* fprintf(stderr, "%s\n", command); */
281             res = PQexec(conn, command);
282             if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
283             {
284               fprintf(stderr, "BEGIN command failed\n");
285               PQclear(res);
286               exit_nicely(conn);
287             }
288             curr = curr->next;
289           }
290       }
291     else 
292      {
293         /* scan the list */
294         while (curr != NULL)
295           {
296             size_t len = strlen(about) + 1;
297             char *qabout = malloc (sizeof(char) * len * 2);
298             char *quri;
299             PQescapeString(qabout,about,len);
300             len = strlen(uri) + 1;
301             quri = malloc (sizeof(char) * len * 2);
302             PQescapeString(quri,uri,len);
303             sprintf(command,"INSERT INTO refObj values ('%s', '%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainHypothesis', %d)",qabout,quri,curr->val);
304             /* fprintf(stderr, "%s\n", command); */
305             res = PQexec(conn, command);
306             if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
307             {
308               fprintf(stderr, "BEGIN command failed\n");
309               PQclear(res);
310               exit_nicely(conn);
311             }
312             curr = curr->next;
313           }
314       }
315 }
316
317 print_mainconcl(about,conn,uri,depth)
318      char    *about;
319      PGconn  *conn;
320      char    *uri;
321      int     depth;
322      
323 {
324     PGresult   *res;
325     char       *command = (char *)malloc((sizeof('a')*200));
326     /* fprintf(stderr,"about = %s\n",about); */
327     if (!strcmp(uri,"Rel"))
328       { 
329         size_t len = strlen(about) + 1;
330         char *qabout = malloc (sizeof(char) * len * 2);
331         PQescapeString(qabout,about,len);
332         sprintf(command,"INSERT INTO refRel values ('%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainConclusion', %d)",qabout,depth);
333         /* fprintf(stderr, "%s\n", command); */
334         res = PQexec(conn, command);
335       }
336     else if ((!strcmp(uri,"Prop")) || (!strcmp(uri,"Type")) ||
337             (!strcmp(uri,"Set")))
338       {
339         size_t len = strlen(about) + 1;
340         char *qabout = malloc (sizeof(char) * len * 2);
341         PQescapeString(qabout,about,len);
342         sprintf(command,"INSERT INTO refSort values ('%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainConclusion', %d, '%s')",qabout,depth,uri);
343         /* fprintf(stderr, "%s\n", command); */
344         res = PQexec(conn, command);
345       }
346     else
347       {
348         size_t len = strlen(about) + 1;
349         char *qabout = malloc (sizeof(char) * len * 2);
350         char *quri;
351         PQescapeString(qabout,about,len);
352         len = strlen(uri) + 1;
353         quri = malloc (sizeof(char) * len * 2);
354         PQescapeString(quri,uri,len);
355         sprintf(command,"INSERT INTO refObj values ('%s', '%s','http://www.cs.unibo.it/helm/schemas/schema-helm#MainConclusion', %d)",qabout,quri,depth); 
356         /* fprintf(stderr, "%s\n", command); */
357         res = PQexec(conn, command);
358       }
359     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
360       {
361         fprintf(stderr, "BEGIN command failed\n");
362         PQclear(res);
363          exit_nicely(conn);
364       }
365     /* fprintf(stderr,"FINITO\n"); */
366 }
367
368 // dome: cambiata per usare il modello con position
369 print_one(conn,about,uri,pos)
370      PGconn  *conn;
371      char    *about,
372              *uri;
373      int     pos;
374 {
375     PGresult   *res;
376     char       *command = (char *)malloc((sizeof('a')*200));
377     char       *position = (char *)malloc((sizeof('a')*20));
378     size_t len = strlen(about) + 1;
379     char *qabout = malloc (sizeof(char) * len * 2);
380     char *quri;
381     PQescapeString(qabout,about,len);
382     len = strlen(uri) + 1;
383     quri = malloc (sizeof(char) * len * 2);
384     PQescapeString(quri,uri,len);
385     if (pos == INBODY)
386        position="InBody";
387     else if (pos == MAINHYP)
388        position="MainHypothesis";  /* This should never happen */
389     else if (pos == INHYP)
390        position="InHypothesis";
391     else if (pos == INCONCL)
392        position="InConclusion";
393     else if (pos == MAINCONCL)
394        position="MainConclusion";  /* This should never happen */
395     sprintf(command,"INSERT INTO refObj values ('%s', '%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#%s')",qabout,quri,position);
396     /* fprintf(stderr, "%s\n", command); */
397     res = PQexec(conn, command);
398     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
399       {
400         fprintf(stderr, "BEGIN command failed\n");
401         PQclear(res);
402          exit_nicely(conn);
403       }
404 }
405
406  /* The following function allocates a bucket for an identifier. */
407 allocate_bucket(st, id, where)
408         struct st_bucket
409                         **st;
410                                         /* pointer to the bucket to be */
411                                         /* allocated */
412         char            *id;
413                                         /* identifier */
414         int             where;
415 {
416         int i;
417
418         *st = (struct st_bucket *)malloc(sizeof(struct st_bucket));
419         (*st)->id = (char *)malloc(sizeof('a')*(strlen(id) + 1));
420         strcpy((*st)->id,id);
421         (*st)->main_depth = 0;
422         (*st)->depths = NULL;
423         (*st)->next_st_bucket = NULL;
424         (*st)->all_next = all;
425         all = *st;
426         for (i = 0; i < 5; ++i)
427           (*st)->pos[i] = 0;
428         (*st)->pos[where] = 1;
429 }
430
431  /* The following function moves a bucket to the head of the */
432  /* list in which it lies. */
433 move_bucket(st, dict_index)
434         struct st_bucket 
435                         *st;
436                                         /* pointer to the bucket to */
437                                         /* be moved */
438         int             dict_index;
439                                         /* index corresponding to */
440                                         /* the list in which the */
441                                         /* bucket lies */
442 {
443         st->next_st_bucket = dictionary[dict_index];
444         dictionary[dict_index] = st;
445 }
446
447  /* The following function implements Weinberger's hash function. */
448 int
449 hash_pjw(id)
450         char            *id;
451                                         /* identifier to be hashed */
452 {
453         unsigned        h,
454                         g;
455
456         for (h = 0; *id != EOS; id++)
457         {
458                 h = (h << HASH1) + (*id);
459                 if (g = h & HASH2)
460                         h = h ^ (g >> HASH3) ^ g;
461         }
462         return(h % DICTSIZE);
463 }
464
465
466
467
468
469